logfile

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2022 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// InitialLogFileId initial log file id: 0.
	InitialLogFileId = 0

	// FilePrefix log file prefix.
	FilePrefix = "log."
)
View Source
const MaxHeaderSize = 25

MaxHeaderSize max entry header size. crc32 typ kSize vSize expiredAt

4    +   1   +   5   +   5    +    10      = 25 (refer to binary.MaxVarintLen32 and binary.MaxVarintLen64)

Variables

View Source
var (
	// ErrInvalidCrc invalid crc.
	ErrInvalidCrc = errors.New("logfile: invalid crc")

	// ErrWriteSizeNotEqual write size is not equal to entry size.
	ErrWriteSizeNotEqual = errors.New("logfile: write size is not equal to entry size")

	// ErrEndOfEntry end of entry in log file.
	ErrEndOfEntry = errors.New("logfile: end of entry in log file")

	// ErrUnsupportedIoType unsupported io type, only mmap and fileIO now.
	ErrUnsupportedIoType = errors.New("unsupported io type")

	// ErrUnsupportedLogFileType unsupported log file type, only WAL and ValueLog now.
	ErrUnsupportedLogFileType = errors.New("unsupported log file type")
)
View Source
var (
	FileNamesMap = map[FileType]string{
		Strs: "log.strs.",
		List: "log.list.",
		Hash: "log.hash.",
		Sets: "log.sets.",
		ZSet: "log.zset.",
	}

	// FileTypesMap name->type
	FileTypesMap = map[string]FileType{
		"strs": Strs,
		"list": List,
		"hash": Hash,
		"sets": Sets,
		"zset": ZSet,
	}
)

Functions

func EncodeEntry

func EncodeEntry(e *LogEntry) ([]byte, int)

EncodeEntry will encode entry into a byte slice. The encoded Entry looks like: +-------+--------+----------+------------+-----------+-------+---------+ | crc | type | key size | value size | expiresAt | key | value | +-------+--------+----------+------------+-----------+-------+---------+ |------------------------HEADER----------------------|

|--------------------------crc check---------------------------|

Types

type EntryType

type EntryType byte

EntryType type of Entry.

const (
	// TypeDelete represents entry type is delete.
	TypeDelete EntryType = iota + 1
	// TypeListMeta represents entry is list meta.
	TypeListMeta
)

type FileType

type FileType int8

FileType represents different types of log file: wal and value log.

const (
	Strs FileType = iota
	List
	Hash
	Sets
	ZSet
)

type IOType

type IOType int8

IOType represents different types of file io: FileIO(standard file io) and MMap(Memory Map).

const (
	// FileIO standard file io.
	FileIO IOType = iota
	// MMap Memory Map.
	MMap
)

type LogEntry

type LogEntry struct {
	Key       []byte
	Value     []byte
	ExpiredAt int64 // time.Unix
	Type      EntryType
}

LogEntry is the data will be appended in log file.

type LogFile

type LogFile struct {
	sync.RWMutex
	Fid        uint32
	WriteAt    int64
	IoSelector ioselector.IOSelector
}

LogFile is an abstraction of a disk file, entry`s read and write will go through it.

func OpenLogFile

func OpenLogFile(path string, fid uint32, fsize int64, ftype FileType, ioType IOType) (lf *LogFile, err error)

OpenLogFile open an existing or create a new log file. fsize must be a postitive number.And we will create io selector according to ioType.

func (*LogFile) Close

func (lf *LogFile) Close() error

Close current log file.

func (*LogFile) Delete

func (lf *LogFile) Delete() error

Delete delete current log file. File can`t be retrieved if do this, so use it carefully.

func (*LogFile) Read

func (lf *LogFile) Read(offset int64, size uint32) ([]byte, error)

Read a byte slice in the log file at offset, slice length is the given size. It returns the byte slice and error, if any.

func (*LogFile) ReadLogEntry

func (lf *LogFile) ReadLogEntry(offset int64) (*LogEntry, int64, error)

ReadLogEntry read a LogEntry from log file at offset. It returns a LogEntry, entry size and an error, if any. If offset is invalid, the err is io.EOF.

func (*LogFile) Sync

func (lf *LogFile) Sync() error

Sync commits the current contents of the log file to stable storage.

func (*LogFile) Write

func (lf *LogFile) Write(buf []byte) error

Write a byte slice at the end of log file. Returns an error, if any.

Jump to

Keyboard shortcuts

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