dymokomi/luce-prism
Source of release 0.6.0
0.6.0 / src / wal / bytes.lucb
## Bounded owned buffers; checksums detect accidents, not malicious rewriting.
import memory
from luce_std import utf8
pub let invalid: ErrorCode = ErrorCode.package(1)
pub let limit: ErrorCode = ErrorCode.package(2)
pub let closed: ErrorCode = ErrorCode.package(3)
pub let conflict: ErrorCode = ErrorCode.package(4)
pub let busy: ErrorCode = ErrorCode.package(5)
pub let corrupt: ErrorCode = ErrorCode.package(6)
pub let uncertain: ErrorCode = ErrorCode.package(7)
pub let timed_out: ErrorCode = ErrorCode.package(8)
pub let max_key: usize = 1024
pub let max_value: usize = 1048576
pub let max_frame: usize = 16777216
pub let max_records: usize = 1000000
pub struct Buffer:
pub var data: u8[]
pub var count: usize
pub func reserve(count: usize) -> !:
if count > max_frame:
error(limit, "database transaction exceeds 16 MiB")
if count <= self.data.length:
return
var capacity = self.data.length * 2
if capacity < 64: capacity = 64
if capacity < count: capacity = count
if capacity > max_frame: capacity = max_frame
let next = try new u8[capacity] --- in memory.heap
memory.copy(next, self.data, self.count)
if self.data.length > 0: free(self.data) in memory.heap
self.data = next
pub func put(value: const u8[]) -> !:
if value.length == 0: return
let total = self.count +? value.length else error(limit, "database byte count overflow")
try self.reserve(total)
memory.copy(self.data[self.count..], value, value.length)
self.count = total
pub func uint(value: u64, width: usize) -> !:
var bytes: u8[8]
for i in 0..<width: bytes[i] = (u8)(value >> (i * 8))
try self.put(bytes[..<width])
pub func span() -> const u8[]:
if self.count == 0: return b""
return self.data[..<self.count]
pub func close():
if self.data.length > 0: free(self.data) in memory.heap
var empty: u8[]
self.data = empty
self.count = 0
pub func copied(value: const u8[]) -> Buffer!:
var out: Buffer
errdefer out.close()
try out.put(value)
return out
pub func compare(a: const u8[], b: const u8[]) -> i32:
let count = a.length if a.length < b.length else b.length
for i in 0..<count:
if a[i] < b[i]: return -1
if a[i] > b[i]: return 1
return -1 if a.length < b.length else (1 if a.length > b.length else 0)
pub func valid_key(key: str) -> !:
if key.length == 0 or key.length > max_key or not utf8.valid(key.bytes):
error(invalid, "database keys must be nonempty UTF-8 of at most 1024 bytes")
pub func checksum(bytes: const u8[]) -> u32:
var crc: u32 = 4294967295
for byte in bytes:
crc ^= (u32)byte
for bit in 0..<8:
crc = (crc >> 1) ^ (3988292384 if (crc & 1) != 0 else 0)
return crc ^ 4294967295
## Incremental equivalent used for bounded-memory checkpoint framing.
pub struct Crc32:
var state: u32 = 4294967295
pub func update(data: const u8[]):
for byte in data:
self.state ^= (u32)byte
for bit in 0..<8:
self.state = (self.state >> 1) ^ (3988292384 if (self.state & 1) != 0 else 0)
pub func finish() -> u32:
return self.state ^ 4294967295
pub struct Reader:
pub var bytes: const u8[]
pub var offset: usize
pub func take(count: usize) -> const u8[]!:
if count > self.bytes.length - self.offset:
error(corrupt, "truncated database record")
let start = self.offset
self.offset += count
return self.bytes[start..<self.offset]
pub func uint(width: usize) -> u64!:
let data = try self.take(width)
var value: u64 = 0
for i in 0..<width: value |= (u64)data[i] << (i * 8)
return value