fixedkv

package module
v0.0.0-...-ee7333b Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2025 License: MIT Imports: 8 Imported by: 0

README

fixed-kv

fixed-kv is a in memory disk persisted key value storage engine of fixed size (4KB), with a dead simple binary format.

A new fixedkv instance will use in memory btree for Get and Set operations. Closing the fixedkv will write to disk where.

Data can be read using the fixedkv.Open(). This will load the database in read only mode, to allow reading of keys and values again.

To edit a database, create a new database using fixedkv.New() and manually insert from disked backed copy.

Format v1

First 96 bytes of the file is used for storing the header.

<----------Header---------->
| version | #keys | unused | key-values |
| 4B      | 2B    | 90B    | ...        |
  • File format version
  • Number of keys
  • Unused bytes for future use
  • The stored key/value pairs of variable length key and values
Key Value format v1
| key len | value len | key | val |
|   2B    |    2B     | ... | ... |

TODO

  • bounds checking for insert operations to keep within 4KB size

Documentation

Index

Constants

View Source
const DBName = "FixedKV database"
View Source
const DBNameOffset = 6
View Source
const DefaultDegree = 32
View Source
const DefaultSize = 4096
View Source
const HeaderSize = 96
View Source
const KeyCountOffset = 4

Variables

View Source
var ErrDatabaseClosed = errors.New("database closed")
View Source
var ErrInvalidHeader = errors.New("invalid header format")
View Source
var ErrReadonlyDb = errors.New("readonly db")

Functions

This section is empty.

Types

type FixedKV

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

func Open

func Open(name string) (*FixedKV, error)

Creates a new FixedKV database file, will write and flush header data to disk.

func (*FixedKV) Close

func (kv *FixedKV) Close() error

func (*FixedKV) Get

func (kv *FixedKV) Get(key string) ([]byte, bool)

func (*FixedKV) Save

func (kv *FixedKV) Save() error

Flushes In-memory KV Database to disk in ascending key order Must be called or dataloss will occur

func (*FixedKV) Set

func (kv *FixedKV) Set(key string, value []byte) ([]byte, bool, error)

Set inserts a new key-value pair. If file is readonly then set will error with ErrReadonlyDb. If setting a non existen key then nil value, false for replaced value and nil error. If value is replaced then previous value, true and no error is returned.

func (*FixedKV) Version

func (kv *FixedKV) Version() string

type KVReader

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

func OpenReader

func OpenReader(name string) (*KVReader, error)

Open a Fixed KV database for reading

func (*KVReader) Get

func (r *KVReader) Get(key string) ([]byte, bool)

Get an item

func (*KVReader) Keys

func (r *KVReader) Keys() []string

Get all keys in the Fixed KV database

func (*KVReader) Values

func (r *KVReader) Values() [][]byte

Get a copy of all values in the Fixed KV database

Directories

Path Synopsis
cmd
fixedkv command

Jump to

Keyboard shortcuts

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