autofile

package
v0.0.0-...-d8f8cc4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 14, 2020 License: GPL-3.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AutoFile

type AutoFile struct {
	ID   string
	Path string
	// contains filtered or unexported fields
}

Automatically closes and re-opens file for writing. This is useful for using a log file with the logrotate tool.

func OpenAutoFile

func OpenAutoFile(path string) (af *AutoFile, err error)

func (*AutoFile) Close

func (af *AutoFile) Close() error

func (*AutoFile) Size

func (af *AutoFile) Size() (int64, error)

func (*AutoFile) Sync

func (af *AutoFile) Sync() error

func (*AutoFile) Write

func (af *AutoFile) Write(b []byte) (n int, err error)

type Group

type Group struct {
	help.BaseService

	ID   string
	Head *AutoFile // The head AutoFile to write to

	Dir string // Directory that contains .Head
	// contains filtered or unexported fields
}

You can open a Group to keep restrictions on an AutoFile, like the maximum size of each chunk, and/or the total amount of bytes stored in the group.

The first file to be written in the Group.Dir is the head file.

Dir/
- <HeadPath>

Once the Head file reaches the size limit, it will be rotated.

Dir/
- <HeadPath>.000   // First rolled file
- <HeadPath>       // New head path, starts empty.
									 // The implicit index is 001.

As more files are written, the index numbers grow...

Dir/
- <HeadPath>.000   // First rolled file
- <HeadPath>.001   // Second rolled file
- ...
- <HeadPath>       // New head path

The Group can also be used to binary-search for some line, assuming that marker lines are written occasionally.

func OpenGroup

func OpenGroup(headPath string) (g *Group, err error)

OpenGroup creates a new Group with head at headPath. It returns an error if it fails to open head file.

func (*Group) Close

func (g *Group) Close()

Close closes the head file. The group must be stopped by this moment.

func (*Group) FindLast

func (g *Group) FindLast(prefix string) (match string, found bool, err error)

Searches backwards for the last line in Group with prefix. Scans each file forward until the end to find the last match.

func (*Group) Flush

func (g *Group) Flush() error

Flush writes any buffered data to the underlying file and commits the current content of the file to stable storage.

func (*Group) HeadSizeLimit

func (g *Group) HeadSizeLimit() int64

HeadSizeLimit returns the current head size limit.

func (*Group) MaxIndex

func (g *Group) MaxIndex() int

MaxIndex returns index of the last file in the group.

func (*Group) MinIndex

func (g *Group) MinIndex() int

MinIndex returns index of the first file in the group.

func (*Group) NewReader

func (g *Group) NewReader(index int) (*GroupReader, error)

NewReader returns a new group reader. CONTRACT: Caller must close the returned GroupReader.

func (*Group) OnStart

func (g *Group) OnStart() error

OnStart implements Service by starting the goroutine that checks file and group limits.

func (*Group) OnStop

func (g *Group) OnStop()

OnStop implements Service by stopping the goroutine described above. NOTE: g.Head must be closed separately using Close.

func (*Group) ReadGroupInfo

func (g *Group) ReadGroupInfo() GroupInfo

Returns info after scanning all files in g.Head's dir.

func (*Group) RotateFile

func (g *Group) RotateFile()

RotateFile causes group to close the current head and assign it some index. Note it does not create a new head.

func (*Group) Search

func (g *Group) Search(prefix string, cmp SearchFunc) (*GroupReader, bool, error)

Searches for the right file in Group, then returns a GroupReader to start streaming lines. Returns true if an exact match was found, otherwise returns the next greater line that starts with prefix. CONTRACT: Caller must close the returned GroupReader

func (*Group) SetHeadSizeLimit

func (g *Group) SetHeadSizeLimit(limit int64)

SetHeadSizeLimit allows you to overwrite default head size limit - 10MB.

func (*Group) SetTotalSizeLimit

func (g *Group) SetTotalSizeLimit(limit int64)

SetTotalSizeLimit allows you to overwrite default total size limit of the group - 1GB.

func (*Group) TotalSizeLimit

func (g *Group) TotalSizeLimit() int64

TotalSizeLimit returns total size limit of the group.

func (*Group) Write

func (g *Group) Write(p []byte) (nn int, err error)

Write writes the contents of p into the current head of the group. It returns the number of bytes written. If nn < len(p), it also returns an error explaining why the write is short. NOTE: Writes are buffered so they don't write synchronously TODO: Make it halt if space is unavailable

func (*Group) WriteLine

func (g *Group) WriteLine(line string) error

WriteLine writes line into the current head of the group. It also appends "\n". NOTE: Writes are buffered so they don't write synchronously TODO: Make it halt if space is unavailable

type GroupInfo

type GroupInfo struct {
	MinIndex  int   // index of the first file in the group, including head
	MaxIndex  int   // index of the last file in the group, including head
	TotalSize int64 // total size of the group
	HeadSize  int64 // size of the head
}

GroupInfo holds information about the group.

type GroupReader

type GroupReader struct {
	*Group
	// contains filtered or unexported fields
}

GroupReader provides an interface for reading from a Group.

func (*GroupReader) Close

func (gr *GroupReader) Close() error

Close closes the GroupReader by closing the cursor file.

func (*GroupReader) CurIndex

func (gr *GroupReader) CurIndex() int

CurIndex returns cursor's file index.

func (*GroupReader) PushLine

func (gr *GroupReader) PushLine(line string)

PushLine makes the given line the current one, so the next time somebody calls ReadLine, this line will be returned. panics if called twice without calling ReadLine.

func (*GroupReader) Read

func (gr *GroupReader) Read(p []byte) (n int, err error)

Read implements io.Reader, reading bytes from the current Reader incrementing index until enough bytes are read.

func (*GroupReader) ReadLine

func (gr *GroupReader) ReadLine() (string, error)

ReadLine reads a line (without delimiter). just return io.EOF if no new lines found.

func (*GroupReader) SetIndex

func (gr *GroupReader) SetIndex(index int) error

SetIndex sets the cursor's file index to index by opening a file at this position.

type SearchFunc

type SearchFunc func(line string) (int, error)

Returns -1 if line comes after, 0 if found, 1 if line comes before.

func MakeSimpleSearchFunc

func MakeSimpleSearchFunc(prefix string, target int) SearchFunc

A simple SearchFunc that assumes that the marker is of form <prefix><number>. For example, if prefix is '#HEIGHT:', the markers of expected to be of the form:

#HEIGHT:1 ... #HEIGHT:2 ...

type SighupWatcher

type SighupWatcher struct {
	// contains filtered or unexported fields
}

Watchces for SIGHUP events and notifies registered AutoFiles

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL