fixregsto

package module
v0.1.0-beta.3 Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2022 License: MIT Imports: 11 Imported by: 1

README

FixRegSto

FixRegSto

NOTE THIS IS NOT YET PRODUCTION READY

This library allows to store fixed length binary blobs in "rotating log" style in different kind of storage mediums in reliable way. This is meant for logging small data droplets that have to be persisted even in case of embedded systems.

Following core specifications

  • Reliability is the first priority (copy on write with fsync)
  • Store fixed length records on file, write size quantized N*record size
  • Does not care about content of file, just fixed record size
  • Predictable storage consumption (all files are same size)
  • Create interface and support other storage hardware (I2C eeprom?, block device?, raw NAND)
  • No data deletion, except old data removal when limit is reached
  • Fixed file size limit (tries match erase size and/or minimum file size)

Allowing only fixed size records to rotating log might sound restrictive but it provides

  • Faster way to seek data
  • Predictability
  • Efficient storage usage

Compression and coding features

  • gz compression support for history data
  • possible to arrange bits for better compression

Typical use case for fixed size record is for storing vital information like events, counters etc.. Datapoints that are critical for operation but very old entries are not anymore relevant.

How to use

There is interface FixRegSto for accessing stored data. FixRegSto implements ReadWriteSeeker interface. (exception is that it access complete records so N*recordSize quantities). It is possible to read latest data with ioutil.ReadAll IF recordSize is power of two. (ReadAll queries with power of two size chunks)

There are now two implementations

  • FileStorage for for file based persistent disk storage.
    • Does copy on write and fsync. Tries to be atomic
    • Creates number of equal size storage files named with increasing _0,_1,_2 numbering.
    • Set fileMaxSize to N times minimum file size (typical 4k) or page size and get efficient storage
  • MemLoop for memory based volatile storage

Check ./example on this repository

FileStorage

File storage is now only permanent storage option.

type FileStorageConf struct {
	Name         string //Numbering _0, _1,_2 etc..
	RecordSize   int64  //One entry is this long, prefer power of two
	MaxFileCount int64  //TODO if 0? no at least 1

	FileMaxSize int64 //How many bytes. Prefer multiple of 512 (erase blocks size optimal)
	Path        string

	//Compression settings
	CompressionMethod string //Empty or "gz"
	BitSlices         []int  //Empty array no slicing. Else give bitlengths (usually bit size of each variable in record)
}

If CompressionMethod is set to "gz", files are compressed. Bit slices describes how file is splitted and arranged. Typical use would be in case of struct, set bitslices as array of variable sizes. In case of array of structs, variables are places to next to each other than concatting struct after struct. This conding might improve compression ratio at some cases

Documentation

Overview

Bit operations. Used for arranging bits for better compression

Simple file based Fixed size Record Storage implementation, syncronized copy on write+ filesync

Memory based loop For testing, volatile storage etc...

Misc util functions

Index

Constants

View Source
const COMPRESSIONMETHOD_GZ = "gz" //TODO other compression methods?

Variables

This section is empty.

Functions

This section is empty.

Types

type FileStorage

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

FileStorage, includes conf and cached data

func (*FileStorage) GetFirst

func (p *FileStorage) GetFirst(nRecords int64) ([]byte, error)

GetFirst nRecords without moving seek cursor

func (*FileStorage) GetLatest

func (p *FileStorage) GetLatest(nRecords int64) ([]byte, error)

func (*FileStorage) Len

func (p *FileStorage) Len() (int64, error)

Len returns how many records are stored

func (*FileStorage) Read

func (p *FileStorage) Read(arr []byte) (n int, err error)

Read implements Reader interface. Except only array length must be multiple of recordsize for normal operation

func (*FileStorage) ReadAll

func (p *FileStorage) ReadAll() ([]byte, error)

ReadAll gets all content. Use with caution, small storages

func (*FileStorage) Seek

func (p *FileStorage) Seek(offset int64, whence int) (int64, error)

Seeks, For implementing seeker interface Seeks file with byte by byte but rounds up new position where record starts (or ends)

func (*FileStorage) Write

func (p *FileStorage) Write(raw []byte) (n int, err error)

Write implements writer interface. Only complete records are accepted

type FileStorageConf

type FileStorageConf struct {
	Name         string //Numbering _0, _1,_2 etc..
	RecordSize   int64  //One entry is this long, prefer power of two
	MaxFileCount int64  //TODO if 0? no at least 1

	FileMaxSize int64 //How many bytes. Prefer multiple of 512 (erase blocks size optimal)
	Path        string

	//Compression settings
	CompressionMethod string //Empty or "gz"
	BitSlices         []int  //Empty array no slicing.Else give bitlengths (usually bit size of each variable in record)
}

FileStorageConf tells what kind of FileStorage instance is going to be created Name, is prefix for filename. Remember not to use same name (and path) in other databases or files RecordSize, is what your application needs (size of struct in bytes?) MaxFileCount, how many storage files exists on disk (plus "work file" without number) FileMaxSize, how many bytes one file takes up. Prefer some multiple of minimum file size on filesystem Path, path to directory where data is stored

func (*FileStorageConf) BaseFileName

func (p *FileStorageConf) BaseFileName() string

func (*FileStorageConf) CheckErrors

func (p *FileStorageConf) CheckErrors() error

CheckErrors tell is there problems with configuration

func (*FileStorageConf) GetNumberRangeOnDisk

func (p *FileStorageConf) GetNumberRangeOnDisk() (int64, int64, int64, error)

getNumberRangeOnDisk, function gets minimum and maximum number in storage files and count of files (count is important if missing files in between? Also decides is delete needed)

func (*FileStorageConf) InitFileStorage

func (p *FileStorageConf) InitFileStorage() (FileStorage, error)

InitFileStorage, Call this method after creating FileStorageConf. This creates dir if required

func (*FileStorageConf) ReadFileWithNumber

func (p *FileStorageConf) ReadFileWithNumber(fileNumber int64) ([]byte, error)

Uses conf. Does not include state like FileStorage

type FixRegSto

type FixRegSto interface {
	Write(raw []byte) (n int, err error)      //size must be recordsize*N
	Len() (int64, error)                      //Number of records
	GetLatest(nRecords int64) ([]byte, error) //Without chancing read pointer
	GetFirst(nRecords int64) ([]byte, error)  //Without chancing read pointer
	Seek(offset int64, whence int) (int64, error)
	Read(arr []byte) (n int, err error)
	ReadAll() ([]byte, error)
}

FixRegSto implements ReadWriteSeeker interface

type Memloop

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

func (*Memloop) GetFirst

func (p *Memloop) GetFirst(nRecords int64) ([]byte, error)

func (*Memloop) GetLatest

func (p *Memloop) GetLatest(nRecords int64) ([]byte, error)

func (*Memloop) Len

func (p *Memloop) Len() (int64, error)

func (*Memloop) Read

func (p *Memloop) Read(arr []byte) (n int, err error)

func (*Memloop) ReadAll

func (p *Memloop) ReadAll() ([]byte, error)

ReadAll gets all content. Use with caution, small storages

func (*Memloop) Seek

func (p *Memloop) Seek(offset int64, whence int) (int64, error)

func (*Memloop) Write

func (p *Memloop) Write(raw []byte) (n int, err error)

size must be recordsize*N

type MemloopConf

type MemloopConf struct {
	RecordSize int64 //One entry is this long
	MaxRecords int64
}

func (*MemloopConf) InitMemLoop

func (p *MemloopConf) InitMemLoop() (Memloop, error)

Jump to

Keyboard shortcuts

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