logfile

package
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2023 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// FilePrefix log file prefix. Full name of a file for example file of strings is like: "path/log.strs.00000001".
	FilePrefix = "log."
)
View Source
const MaxHeaderSize = 25

MaxHeaderSize max entry header size. 4 + 1 + 10 + 10 + 3 + 5 + 5 = 38 crc stat ExpiredAt TxID TxStatus kSize vSize (refer to binary.MaxVarintLen32 and binary.MaxVarintLen64)

Variables

View Source
var (
	// ErrLogEndOfFile read to the end of the logEntry file
	ErrLogEndOfFile = errors.New("logfile: end of logEntry file")

	// ErrIllegalFileSize illegal file size
	ErrIllegalFileSize = errors.New("logfile: illegal file size")

	// ErrUnsupportedIoType ioType not supported
	ErrUnsupportedIoType = errors.New("logfile: ioType not supported")

	// ErrUnsupportedFileType fileType not supported
	ErrUnsupportedFileType = errors.New("logfile fileType not supported")

	// 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")
)
View Source
var (
	//  convert string in filename to FType
	FileTypesMap = map[string]FType{
		"strs": Strs,
		"list": List,
		"hash": Hash,
		"set":  Set,
		"zset": ZSet,
	}
	FileNamesMap = map[FType]string{
		Strs: "log.strs.",
		List: "log.list.",
		Hash: "log.hash.",
		Set:  "log.set.",
		ZSet: "log.zset.",
	}
)

Functions

func EncodeEntry

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

EncodeEntry encodes LogEntry into binary form, returns binary LogEntry and the size of LogEntry.

Types

type FType

type FType uint8

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

const (
	Strs FType = iota
	List
	Hash
	Set
	ZSet
)

type IOType

type IOType uint8

IOType represents different types of file io: FileIO(standard file io).

const (
	// FileIO standard file io.
	FileIO IOType = iota
	// MMap memory map
	Mmap
)

type LogEntry

type LogEntry struct {
	ExpiredAt int64    // expire time
	Stat      Status   // delete or list meta
	TxID      uint64   // transaction id
	TxStat    TxStatus // committed / uncommitted

	Key   []byte // key
	Value []byte // value
	// contains filtered or unexported fields
}

LogEntry is the data will be appended in log file.

type LogFile

type LogFile struct {
	Fid          uint32
	Offset       int64 // WriteAt
	IoController iocontroller.IOController
	Mu           sync.RWMutex
}

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

func Open

func Open(path string, fid uint32, fsize int64, ftype FType, ioType IOType) (*LogFile, error)

Open opens an existing or create a new log file. fsize must be a postitive number.And we will create io controller 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.

func (*LogFile) ReadLogEntry

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

ReadLogEntry read a LogEntry from log file at offset. it returns LogEntry, entrySize and err if any

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.

type Status

type Status uint8

Status of LogEntry.

const (
	// SDelete represents entry has been deleted.
	SDelete Status = iota + 1
	// SListMeta represents entry is list meta.
	SListMeta
)

type TxStatus

type TxStatus uint16

TxStatus of LogEntry

const (
	//  TxCommited represents transaction has been commited
	TxCommited TxStatus = iota + 1
	//  TxUnCommited represents transaction has not been commited
	TxUncommited
)

Jump to

Keyboard shortcuts

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