2022-12-02 21:37:56 +05:00
|
|
|
# zkv
|
|
|
|
|
|
|
|
Simple key-value store for single-user applications.
|
|
|
|
|
|
|
|
## Pros
|
|
|
|
|
2022-12-03 12:41:11 +05:00
|
|
|
* Simple one file structure
|
|
|
|
* Internal Zstandard compression by [klauspost/compress/zstd](https://github.com/klauspost/compress/tree/master/zstd)
|
2022-12-02 21:37:56 +05:00
|
|
|
* Threadsafe operations through `sync.RWMutex`
|
|
|
|
|
|
|
|
## Cons
|
|
|
|
|
2022-12-10 22:00:42 +05:00
|
|
|
* Index stored in memory (`map[key hash (28 bytes)]file offset (int64)`)
|
2022-12-10 21:39:24 +05:00
|
|
|
* Need to read the whole file on store open to create file index (you can use index file options to avoid this)
|
2022-12-02 21:37:56 +05:00
|
|
|
* No way to recover disk space from deleted records
|
|
|
|
* Write/Delete operations block Read and each other operations
|
2022-12-10 22:00:42 +05:00
|
|
|
* Need to decode whole file until stored value
|
2022-12-02 21:37:56 +05:00
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
|
|
|
Create or open existing file:
|
|
|
|
|
|
|
|
```go
|
2022-12-09 20:05:57 +05:00
|
|
|
db, err := zkv.Open("path to file")
|
2022-12-02 21:37:56 +05:00
|
|
|
```
|
|
|
|
|
|
|
|
Data operations:
|
|
|
|
|
|
|
|
```go
|
|
|
|
// Write data
|
|
|
|
err = db.Set(key, value) // key and value can be any of type
|
|
|
|
|
|
|
|
// Read data
|
|
|
|
var value ValueType
|
2022-12-03 20:57:50 +05:00
|
|
|
err = db.Get(key, &value)
|
2022-12-02 21:37:56 +05:00
|
|
|
|
|
|
|
// Delete data
|
|
|
|
err = db.Delete(key)
|
|
|
|
```
|
|
|
|
|
2022-12-03 20:57:50 +05:00
|
|
|
Other methods:
|
|
|
|
|
|
|
|
```go
|
|
|
|
// Flush data to disk
|
|
|
|
err = db.Flush()
|
2022-12-05 21:26:54 +05:00
|
|
|
|
|
|
|
// Backup data to another file
|
|
|
|
err = db.Backup("new/file/path")
|
2022-12-03 20:57:50 +05:00
|
|
|
```
|
|
|
|
|
2022-12-10 21:39:24 +05:00
|
|
|
## Store options
|
|
|
|
|
|
|
|
```go
|
|
|
|
type Options struct {
|
|
|
|
// Maximum number of concurrent reads
|
|
|
|
MaxParallelReads int
|
|
|
|
|
|
|
|
// Compression level
|
|
|
|
CompressionLevel zstd.EncoderLevel
|
|
|
|
|
|
|
|
// Memory write buffer size in bytes
|
|
|
|
MemoryBufferSize int
|
|
|
|
|
|
|
|
// Disk write buffer size in bytes
|
|
|
|
DiskBufferSize int
|
|
|
|
|
|
|
|
// Use index file
|
|
|
|
UseIndexFile bool
|
|
|
|
}
|
|
|
|
|
|
|
|
```
|
|
|
|
|
2022-12-02 21:37:56 +05:00
|
|
|
## File structure
|
|
|
|
|
|
|
|
Record is `encoding/gob` structure:
|
|
|
|
|
|
|
|
| Field | Description | Size |
|
|
|
|
| ---------- | ---------------------------------- | -------- |
|
|
|
|
| Type | Record type | uint8 |
|
|
|
|
| KeyHash | Key hash | 28 bytes |
|
|
|
|
| ValueBytes | Value gob-encoded bytes | variable |
|
|
|
|
|
|
|
|
File is log stuctured list of commands:
|
|
|
|
|
|
|
|
| Field | Description | Size |
|
|
|
|
| -------| ------------------------ | -------- |
|
|
|
|
| Length | Record body bytes length | int64 |
|
|
|
|
| Body | Gob-encoded record | variable |
|
2022-12-03 21:03:27 +05:00
|
|
|
|
2022-12-10 22:00:08 +05:00
|
|
|
## Resource consumption
|
|
|
|
|
|
|
|
Store requirements:
|
|
|
|
|
|
|
|
* around 300 Mb of RAM per 1 million of keys
|
|
|
|
* around 34 Mb of disk space for index file per 1 million of keys
|
|
|
|
|
2022-12-03 21:03:27 +05:00
|
|
|
## TODO
|
|
|
|
|
2022-12-07 21:06:36 +05:00
|
|
|
- [ ] Add recovery previous state of store file on write error
|
2022-12-10 22:00:42 +05:00
|
|
|
- [ ] Add fast file seek to value (add compressed block start position)
|