storage

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2023 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// InitialLogField intializes log file ig with 0.
	InitialLogField = 0

	// FilePrefix defines the log file prefix.
	FilePrefix = "log."
)
View Source
const MaxMetaSize = 21

MaxMetaSize defines the max size of entry header.

The structure of entry header is as follows:
+-----------+-----------+-----------+-----------+-----------+
|   crc32   |    type   |  keySize  | valueSize | expiredAt |
+-----------+-----------+-----------+-----------+-----------+
|   uint32  |    byte   |   uint32  |   uint32  |   int64   |
+-----------+-----------+-----------+-----------+-----------+
So, MaxMetaSize = 4 + 1 + 4 + 4 + 8 = 21.

Variables

View Source
var (
	// ErrInvalidCrc represents invalid crc.
	ErrInvalidCrc = errors.New("logfile: invalid crc")
	// ErrWriteSizeNotEqual represents write size is not equal yp entry size.
	ErrWriteSizeNotEqual = errors.New("logfile: write size is not equal yp entry size")
	// ErrEndOfEntry represents end of entry in log file.
	ErrEndOfEntry = errors.New("logfile: end of entry in log file")
	// ErrUnsupportedIOType represents unsupported io type, only support mmap and fileIO now.
	ErrUnsupportedIOType = errors.New("logfile: unsupported io type")
	// ErrUnsupportedLogFileType represents unsupported log file type, only support WAL and ValueLog now.
	ErrUnsupportedLogFileType = errors.New("logfile: 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 = 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:
	+-----------+-----------+-----------+-----------+-----------+-----------+-----------+
	|   crc32   |    type   |  KeySize  | ValueSize | expiredAt |    key    |   value   |
	+-----------+-----------+-----------+-----------+-----------+-----------+-----------+
	|<------------------------META INFO------------------------>|
             |<---------------------------CRC CHECK SUM---------------------------->|

Types

type EntryType

type EntryType byte

EntryType defines the type of log 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 represents deifferent types of log file: wal and value log.

const (
	Strs FileType = iota
	List
	Hash
	Sets
	ZSet
)

type IOType

type IOType int8

IOType represents

const (
	FileIO IOType = iota
	MMap
)

type LogEntry

type LogEntry struct {
	Key       []byte
	Value     []byte
	ExpiredAt int64
	Type      EntryType
}

LogEntry is the data which 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) (logFile *LogFile, err error)

OpenLogFile opens an existing log file or creates a new log file.

func (*LogFile) Close

func (lf *LogFile) Close() error

Close closes current logh file.

func (*LogFile) Delete

func (lf *LogFile) Delete() error

Delete deletes current log file. This is an irreversible operation. Please consider carefully.

func (*LogFile) Read

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

Read reads a byte slice in the log file at offset

func (*LogFile) ReadLogEntry

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

ReadLogEntry reads a LogEntry from log file at offset.

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 writes a byte alice at the end oflog file. Returns an error, if any.

Jump to

Keyboard shortcuts

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