sqlite3

package
v0.0.0-...-16f22ad Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2025 License: MIT Imports: 21 Imported by: 0

Documentation

Overview

Initial implementation was assisted by Deepseek R1 14B running locally and OpenAI GPT-4o mini using the following prompt: {{prompt}} I need to write a small golang library that uses sqlite3 to expose an KV interface.

Basic requirements: 1. The key for each entry is an arbitrary binary blob; 2. Each KV entry supports 3 types of values: Simple, Prefix, and Lease; 3. Simple value stores an arbitrary binary blob, and be updated on key conflict; 4. Prefix value stores an array of strings. The values need to be unique within each entry. However, different keys may have the same values in Prefix; 5. Lease value stores an uint64. This also needs to support an atomic update with the business logic done in go rather than SQL; 6. Each KV entry may have all three types value at the same time. For example, operating on a Simple value should not affect the values in Prefix nor Lease; 7. Read operations should return zero values and should not error when the corresponding value type is not found; 8. If inserting a value that already exists in the Prefix within each key, returns an error; 9. In addition to support three types of values for each key, each key will need to have a KeyTracker, where it will mark what value types this key has. KeyTracker also stores the hash of the key via a custom function that outputs uint64; 10. Multiple keys may have a same hash value in KeyTracker due to the hash function used. It is important to only calculate and store the hash when the KeyTracker was first created. 11. When there are no longer Simple, Prefix, or Lease values under a key, the KeyTracker will also need to be deleted; 12. Avoid using AUTO_INCREMENT and use composite keys when possible.

Additional requirements: 1. The KV interface also expects functions to export and restore KV entries. This is needed for backup and restore. 2. The library needs to be concurrency safe. Avoid using mutex in go, and use transaction where possible.

Please implement the library with the specified requirements. If possible, please the GORM ORM library. {{/prompt}}

Some tests and optimizations were assisted by OpenAI GPT-4o mini.

Index

Constants

View Source
const (
	SimpleFlag = 1 << iota
	PrefixFlag
	LeaseFlag
)

Variables

This section is empty.

Functions

func Initialize

func Initialize(cacheDir string) error

Types

type Config

type Config struct {
	Logger  *zap.Logger
	HasnFn  chord.HashFn
	DataDir string
}

type KeyTracker

type KeyTracker struct {
	Key   []byte `gorm:"primaryKey"`
	Hash  uint64 `gorm:"index:idx_hash,sort:asc"`
	Flags uint8  // Bitmask: 1=Simple, 2=Prefix, 4=Lease
}

type LeaseEntry

type LeaseEntry struct {
	Owner []byte `gorm:"primaryKey"`
	Token uint64
}

type PrefixEntry

type PrefixEntry struct {
	Prefix []byte `gorm:"primaryKey"`
	Child  []byte `gorm:"primaryKey"`
}

type SimpleEntry

type SimpleEntry struct {
	Key   []byte `gorm:"primaryKey"`
	Value []byte
}

type SqliteKV

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

func New

func New(cfg Config) (*SqliteKV, error)

func (*SqliteKV) Acquire

func (s *SqliteKV) Acquire(ctx context.Context, lease []byte, ttl time.Duration) (uint64, error)

func (*SqliteKV) Delete

func (s *SqliteKV) Delete(ctx context.Context, key []byte) error

func (*SqliteKV) Export

func (s *SqliteKV) Export(ctx context.Context, keys [][]byte) ([]*protocol.KVTransfer, error)

func (*SqliteKV) Get

func (s *SqliteKV) Get(ctx context.Context, key []byte) ([]byte, error)

func (*SqliteKV) Import

func (s *SqliteKV) Import(ctx context.Context, keys [][]byte, values []*protocol.KVTransfer) error

func (*SqliteKV) ListKeys

func (s *SqliteKV) ListKeys(ctx context.Context, prefix []byte) ([]*protocol.KeyComposite, error)

func (*SqliteKV) PrefixAppend

func (s *SqliteKV) PrefixAppend(ctx context.Context, prefix []byte, child []byte) error

func (*SqliteKV) PrefixContains

func (s *SqliteKV) PrefixContains(ctx context.Context, prefix []byte, child []byte) (bool, error)

func (*SqliteKV) PrefixList

func (s *SqliteKV) PrefixList(ctx context.Context, prefix []byte) ([][]byte, error)

func (*SqliteKV) PrefixRemove

func (s *SqliteKV) PrefixRemove(ctx context.Context, prefix []byte, child []byte) error

func (*SqliteKV) Put

func (s *SqliteKV) Put(ctx context.Context, key []byte, value []byte) error

func (*SqliteKV) RangeKeys

func (s *SqliteKV) RangeKeys(ctx context.Context, low uint64, high uint64) ([][]byte, error)

func (*SqliteKV) Release

func (s *SqliteKV) Release(ctx context.Context, lease []byte, token uint64) error

func (*SqliteKV) RemoveKeys

func (s *SqliteKV) RemoveKeys(ctx context.Context, keys [][]byte) error

func (*SqliteKV) Renew

func (s *SqliteKV) Renew(ctx context.Context, lease []byte, ttl time.Duration, prevToken uint64) (uint64, error)

Jump to

Keyboard shortcuts

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