An append-only key-value storage engine in MoonBit based on Bitcask architecture.
Dependencies
moon add wallll-wal6/moonkvimport {
"wallll-wal6/moonkv" @moonkv,
}let db = @moonkv.DB::open("my_db", 10 * 1024 * 1024)
db.put("user_name", @moonkv.string_to_bytes("Alice"))
let value = db.get("user_name")
println(@moonkv.bytes_to_string(value))
db.close()db.put_ttl("temporary", @moonkv.string_to_bytes("value"), 5L)
let batch = db.new_write_batch()
batch.put("balance", @moonkv.string_to_bytes("100"))
batch.delete("old_balance")
batch.commit()///|
let page = db.scan_prefix("user:", 100)
///|
let next_page = db.scan_after("user:", page[page.length() - 1].0, 100)
///|
let range = db.scan_range("user:", "user;", 100)
///|
let page = db.scan_page("user:", "", 100)
///|
let stats = db.stats()
///|
let verified = db.verify()
///|
let fingerprint = db.fingerprint()moon run cmd/main -- ./data_dir put mykey "Hello MoonBit"
moon run cmd/main -- ./data_dir get mykey
moon run cmd/main -- ./data_dir put-ttl token "temporary" 2
moon run cmd/main -- ./data_dir delete mykey
moon run cmd/main -- ./data_dir merge
moon run cmd/main -- ./data_dir scan user: 100
moon run cmd/main -- ./data_dir delete-prefix session: 100
moon run cmd/main -- ./data_dir stats
moon run cmd/main -- ./data_dir verify
moon run cmd/main -- ./data_dir fingerprintbash scripts/benchmark.sh 10000
# Windows PowerShell:
.\scripts\benchmark.ps1 -Records 10000bash scripts/cross-process-recovery.shmoon fmt --check
moon check --target all --deny-warn
moon build --target all
moon test --target all --deny-warn
moon info
git diff --exit-codepub suberror MoonKVError {
IOError(String)
KeyNotFound(String)
CorruptedDatabase(String)
InvalidRecord(String)
}pub struct DB {
dir : String
keydir : Keydir
active_file_id : Int
active_file_offset : Int
max_file_size : Int
logical_clock : Int64
}fn DB::scan_after(self : DB, prefix : String, cursor : String, limit : Int) -> Array[(String, Bytes)] raise MoonKVErrorfn DB::scan_page(self : DB, prefix : String, cursor : String, limit : Int) -> ScanPage raise MoonKVErrorfn DB::scan_prefix(self : DB, prefix : String, limit : Int) -> Array[(String, Bytes)] raise MoonKVErrorfn DB::scan_range(self : DB, start_key : String, end_key : String, limit : Int) -> Array[(String, Bytes)] raise MoonKVErrorpub struct DBStats {
key_count : Int
live_value_bytes : Int64
active_file_id : Int
active_file_offset : Int
max_file_size : Int
logical_clock : Int64
}pub struct HintRecord {
timestamp : Int64
expires_at : Int64
key_sz : Int
val_sz : Int
value_pos : Int64
key : Bytes
}pub struct KeyEntry {
file_id : Int
value_sz : Int
value_pos : Int64
timestamp : Int64
expires_at : Int64
}pub struct Record {
checksum : Int
timestamp : Int64
expires_at : Int64
key : Bytes
value : Bytes
is_tombstone : Bool
}fn bytes_to_string(bytes : Bytes) -> Stringfn fnv1a_bytes(bytes : Bytes, offset : Int, len : Int) -> Intfn get_int32(bytes : Bytes, offset : Int) -> Intfn get_int64(bytes : Bytes, offset : Int) -> Int64fn merge_bytes(b1 : Bytes, b2 : Bytes) -> Bytesfn serialize_hint_record(timestamp : Int64, expires_at : Int64, key_sz : Int, val_sz : Int, value_pos : Int64, key : Bytes) -> Bytesfn string_to_bytes(s : String) -> BytesAn append-only key-value storage engine in MoonBit based on Bitcask architecture.
Dependencies