Compare commits
9 Commits
2d70ba28c0
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| e4b82c247a | |||
| 3f7f4b6a6e | |||
| 512d1bc96e | |||
| 7c9247fd9c | |||
| 1b9bea7f53 | |||
| 2cdd53ac8b | |||
| 7132fc4017 | |||
| aa7fa3f004 | |||
| f1a87052bc |
90
concat_project.sh
Executable file
90
concat_project.sh
Executable file
@@ -0,0 +1,90 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Output file
|
||||
OUTPUT_FILE="project_context.txt"
|
||||
|
||||
# Directories to exclude
|
||||
EXCLUDE_DIRS=("zig-out" "data" ".git" "node_modules" ".zig-cache" "tests")
|
||||
|
||||
# File extensions to include (add more as needed)
|
||||
INCLUDE_EXTENSIONS=("zig" "Makefile")
|
||||
|
||||
# Special files to include (without extension)
|
||||
INCLUDE_FILES=("build.zig" "build.zig.zon" "Makefile")
|
||||
|
||||
# Clear the output file
|
||||
> "$OUTPUT_FILE"
|
||||
|
||||
# Function to check if directory should be excluded
|
||||
should_exclude_dir() {
|
||||
local dir="$1"
|
||||
for exclude in "${EXCLUDE_DIRS[@]}"; do
|
||||
if [[ "$dir" == *"/$exclude"* ]] || [[ "$dir" == "$exclude"* ]]; then
|
||||
return 0
|
||||
fi
|
||||
done
|
||||
return 1
|
||||
}
|
||||
|
||||
# Function to check if file should be included
|
||||
should_include_file() {
|
||||
local file="$1"
|
||||
local basename=$(basename "$file")
|
||||
|
||||
# Check if it's in the special files list
|
||||
for special in "${INCLUDE_FILES[@]}"; do
|
||||
if [[ "$basename" == "$special" ]]; then
|
||||
return 0
|
||||
fi
|
||||
done
|
||||
|
||||
# Check extension
|
||||
local ext="${file##*.}"
|
||||
for include_ext in "${INCLUDE_EXTENSIONS[@]}"; do
|
||||
if [[ "$ext" == "$include_ext" ]]; then
|
||||
return 0
|
||||
fi
|
||||
done
|
||||
|
||||
return 1
|
||||
}
|
||||
|
||||
# Add header
|
||||
echo "# Project: zyna-db" >> "$OUTPUT_FILE"
|
||||
echo "# Generated: $(date)" >> "$OUTPUT_FILE"
|
||||
echo "" >> "$OUTPUT_FILE"
|
||||
echo "================================================================================" >> "$OUTPUT_FILE"
|
||||
echo "" >> "$OUTPUT_FILE"
|
||||
|
||||
# Find and concatenate files
|
||||
while IFS= read -r -d '' file; do
|
||||
# Get directory path
|
||||
dir=$(dirname "$file")
|
||||
|
||||
# Skip excluded directories
|
||||
if should_exclude_dir "$dir"; then
|
||||
continue
|
||||
fi
|
||||
|
||||
# Check if file should be included
|
||||
if should_include_file "$file"; then
|
||||
echo "Adding: $file"
|
||||
|
||||
# Add file delimiter
|
||||
echo "================================================================================" >> "$OUTPUT_FILE"
|
||||
echo "FILE: $file" >> "$OUTPUT_FILE"
|
||||
echo "================================================================================" >> "$OUTPUT_FILE"
|
||||
echo "" >> "$OUTPUT_FILE"
|
||||
|
||||
# Add file contents
|
||||
cat "$file" >> "$OUTPUT_FILE"
|
||||
|
||||
# Add spacing
|
||||
echo "" >> "$OUTPUT_FILE"
|
||||
echo "" >> "$OUTPUT_FILE"
|
||||
fi
|
||||
done < <(find . -type f -print0 | sort -z)
|
||||
|
||||
echo ""
|
||||
echo "Done! Output written to: $OUTPUT_FILE"
|
||||
echo "File size: $(du -h "$OUTPUT_FILE" | cut -f1)"
|
||||
253
src/bench.zig
253
src/bench.zig
@@ -3,6 +3,7 @@ const std = @import("std");
|
||||
const rocksdb = @import("rocksdb.zig");
|
||||
const storage = @import("dynamodb/storage.zig");
|
||||
const types = @import("dynamodb/types.zig");
|
||||
const json = @import("dynamodb/json.zig");
|
||||
|
||||
const BenchResult = struct {
|
||||
name: []const u8,
|
||||
@@ -23,18 +24,6 @@ const BenchResult = struct {
|
||||
}
|
||||
};
|
||||
|
||||
fn runBench(name: []const u8, ops: u64, func: anytype) BenchResult {
|
||||
const start = std.time.nanoTimestamp();
|
||||
func();
|
||||
const end = std.time.nanoTimestamp();
|
||||
|
||||
return BenchResult{
|
||||
.name = name,
|
||||
.ops = ops,
|
||||
.duration_ns = @intCast(end - start),
|
||||
};
|
||||
}
|
||||
|
||||
pub fn main() !void {
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
defer _ = gpa.deinit();
|
||||
@@ -45,33 +34,27 @@ pub fn main() !void {
|
||||
std.debug.print(" ZynamoDB Performance Benchmarks\n", .{});
|
||||
std.debug.print("=" ** 70 ++ "\n\n", .{});
|
||||
|
||||
// Setup
|
||||
const path = "/tmp/bench_zynamodb";
|
||||
defer std.fs.deleteTreeAbsolute(path) catch {};
|
||||
|
||||
// Raw RocksDB benchmarks
|
||||
std.debug.print("RocksDB Raw Operations:\n", .{});
|
||||
std.debug.print("-" ** 70 ++ "\n", .{});
|
||||
|
||||
try benchRocksDBWrites(allocator, path);
|
||||
try benchRocksDBReads(allocator, path);
|
||||
try benchRocksDBBatch(allocator, path);
|
||||
try benchRocksDBScan(allocator, path);
|
||||
try benchRocksDBWrites(allocator);
|
||||
try benchRocksDBReads(allocator);
|
||||
try benchRocksDBBatch(allocator);
|
||||
|
||||
std.debug.print("\n", .{});
|
||||
|
||||
// Storage engine benchmarks
|
||||
std.debug.print("Storage Engine Operations:\n", .{});
|
||||
std.debug.print("\nStorage Engine Operations:\n", .{});
|
||||
std.debug.print("-" ** 70 ++ "\n", .{});
|
||||
|
||||
try benchStoragePutItem(allocator, path);
|
||||
try benchStorageGetItem(allocator, path);
|
||||
try benchStorageScan(allocator, path);
|
||||
try benchStoragePutItem(allocator);
|
||||
try benchStorageGetItem(allocator);
|
||||
try benchStorageScan(allocator);
|
||||
|
||||
std.debug.print("\n" ++ "=" ** 70 ++ "\n", .{});
|
||||
}
|
||||
|
||||
fn benchRocksDBWrites(allocator: std.mem.Allocator, base_path: []const u8) !void {
|
||||
fn benchRocksDBWrites(allocator: std.mem.Allocator) !void {
|
||||
_ = allocator;
|
||||
const path = "/tmp/bench_rocksdb_writes";
|
||||
defer std.fs.deleteTreeAbsolute(path) catch {};
|
||||
@@ -83,29 +66,26 @@ fn benchRocksDBWrites(allocator: std.mem.Allocator, base_path: []const u8) !void
|
||||
var key_buf: [32]u8 = undefined;
|
||||
var val_buf: [256]u8 = undefined;
|
||||
|
||||
const result = runBench("Sequential Writes", ops, struct {
|
||||
fn run(d: *rocksdb.DB, kb: *[32]u8, vb: *[256]u8, n: u64) void {
|
||||
const start = std.time.nanoTimestamp();
|
||||
var i: u64 = 0;
|
||||
while (i < n) : (i += 1) {
|
||||
const key = std.fmt.bufPrint(kb, "key_{d:0>10}", .{i}) catch continue;
|
||||
const val = std.fmt.bufPrint(vb, "value_{d}_padding_data_to_make_it_realistic", .{i}) catch continue;
|
||||
d.put(key, val) catch {};
|
||||
while (i < ops) : (i += 1) {
|
||||
const key = std.fmt.bufPrint(&key_buf, "key_{d:0>10}", .{i}) catch continue;
|
||||
const val = std.fmt.bufPrint(&val_buf, "value_{d}_padding_data", .{i}) catch continue;
|
||||
db.put(key, val) catch {};
|
||||
}
|
||||
}
|
||||
}.run, .{ &db, &key_buf, &val_buf, ops });
|
||||
const end = std.time.nanoTimestamp();
|
||||
|
||||
_ = base_path;
|
||||
const result = BenchResult{ .name = "Sequential Writes", .ops = ops, .duration_ns = @intCast(end - start) };
|
||||
result.print();
|
||||
}
|
||||
|
||||
fn benchRocksDBReads(allocator: std.mem.Allocator, base_path: []const u8) !void {
|
||||
fn benchRocksDBReads(allocator: std.mem.Allocator) !void {
|
||||
const path = "/tmp/bench_rocksdb_reads";
|
||||
defer std.fs.deleteTreeAbsolute(path) catch {};
|
||||
|
||||
var db = try rocksdb.DB.open(path, true);
|
||||
defer db.close();
|
||||
|
||||
// First write some data
|
||||
var key_buf: [32]u8 = undefined;
|
||||
var val_buf: [256]u8 = undefined;
|
||||
|
||||
@@ -117,27 +97,24 @@ fn benchRocksDBReads(allocator: std.mem.Allocator, base_path: []const u8) !void
|
||||
try db.put(key, val);
|
||||
}
|
||||
|
||||
// Now benchmark reads
|
||||
var prng = std.Random.DefaultPrng.init(12345);
|
||||
const random = prng.random();
|
||||
|
||||
const result = runBench("Random Reads", ops, struct {
|
||||
fn run(d: *rocksdb.DB, alloc: std.mem.Allocator, kb: *[32]u8, r: std.Random, n: u64) void {
|
||||
const start = std.time.nanoTimestamp();
|
||||
var j: u64 = 0;
|
||||
while (j < n) : (j += 1) {
|
||||
const idx = r.intRangeAtMost(u64, 0, n - 1);
|
||||
const key = std.fmt.bufPrint(kb, "key_{d:0>10}", .{idx}) catch continue;
|
||||
const val = d.get(alloc, key) catch continue;
|
||||
if (val) |v| alloc.free(v);
|
||||
while (j < ops) : (j += 1) {
|
||||
const idx = random.intRangeAtMost(u64, 0, ops - 1);
|
||||
const key = std.fmt.bufPrint(&key_buf, "key_{d:0>10}", .{idx}) catch continue;
|
||||
const val = db.get(allocator, key) catch continue;
|
||||
if (val) |v| allocator.free(v);
|
||||
}
|
||||
}
|
||||
}.run, .{ &db, allocator, &key_buf, random, ops });
|
||||
const end = std.time.nanoTimestamp();
|
||||
|
||||
_ = base_path;
|
||||
const result = BenchResult{ .name = "Random Reads", .ops = ops, .duration_ns = @intCast(end - start) };
|
||||
result.print();
|
||||
}
|
||||
|
||||
fn benchRocksDBBatch(allocator: std.mem.Allocator, base_path: []const u8) !void {
|
||||
fn benchRocksDBBatch(allocator: std.mem.Allocator) !void {
|
||||
_ = allocator;
|
||||
const path = "/tmp/bench_rocksdb_batch";
|
||||
defer std.fs.deleteTreeAbsolute(path) catch {};
|
||||
@@ -149,132 +126,85 @@ fn benchRocksDBBatch(allocator: std.mem.Allocator, base_path: []const u8) !void
|
||||
var key_buf: [32]u8 = undefined;
|
||||
var val_buf: [256]u8 = undefined;
|
||||
|
||||
const result = runBench("Batch Writes", ops, struct {
|
||||
fn run(d: *rocksdb.DB, kb: *[32]u8, vb: *[256]u8, n: u64) void {
|
||||
const start = std.time.nanoTimestamp();
|
||||
var batch = rocksdb.WriteBatch.init() orelse return;
|
||||
defer batch.deinit();
|
||||
|
||||
var i: u64 = 0;
|
||||
while (i < n) : (i += 1) {
|
||||
const key = std.fmt.bufPrint(kb, "batch_key_{d:0>10}", .{i}) catch continue;
|
||||
const val = std.fmt.bufPrint(vb, "batch_value_{d}", .{i}) catch continue;
|
||||
while (i < ops) : (i += 1) {
|
||||
const key = std.fmt.bufPrint(&key_buf, "batch_key_{d:0>10}", .{i}) catch continue;
|
||||
const val = std.fmt.bufPrint(&val_buf, "batch_value_{d}", .{i}) catch continue;
|
||||
batch.put(key, val);
|
||||
}
|
||||
batch.write(&db) catch {};
|
||||
const end = std.time.nanoTimestamp();
|
||||
|
||||
batch.write(d) catch {};
|
||||
}
|
||||
}.run, .{ &db, &key_buf, &val_buf, ops });
|
||||
|
||||
_ = base_path;
|
||||
const result = BenchResult{ .name = "Batch Writes", .ops = ops, .duration_ns = @intCast(end - start) };
|
||||
result.print();
|
||||
}
|
||||
|
||||
fn benchRocksDBScan(allocator: std.mem.Allocator, base_path: []const u8) !void {
|
||||
_ = allocator;
|
||||
const path = "/tmp/bench_rocksdb_scan";
|
||||
defer std.fs.deleteTreeAbsolute(path) catch {};
|
||||
|
||||
var db = try rocksdb.DB.open(path, true);
|
||||
defer db.close();
|
||||
|
||||
// Write data
|
||||
var key_buf: [32]u8 = undefined;
|
||||
var val_buf: [256]u8 = undefined;
|
||||
|
||||
const ops: u64 = 10000;
|
||||
var i: u64 = 0;
|
||||
while (i < ops) : (i += 1) {
|
||||
const key = try std.fmt.bufPrint(&key_buf, "scan_key_{d:0>10}", .{i});
|
||||
const val = try std.fmt.bufPrint(&val_buf, "scan_value_{d}", .{i});
|
||||
try db.put(key, val);
|
||||
}
|
||||
|
||||
const result = runBench("Full Scan", ops, struct {
|
||||
fn run(d: *rocksdb.DB, n: u64) void {
|
||||
_ = n;
|
||||
var iter = rocksdb.Iterator.init(d) orelse return;
|
||||
defer iter.deinit();
|
||||
|
||||
iter.seekToFirst();
|
||||
var count: u64 = 0;
|
||||
while (iter.valid()) {
|
||||
_ = iter.key();
|
||||
_ = iter.value();
|
||||
count += 1;
|
||||
iter.next();
|
||||
}
|
||||
}
|
||||
}.run, .{ &db, ops });
|
||||
|
||||
_ = base_path;
|
||||
result.print();
|
||||
}
|
||||
|
||||
fn benchStoragePutItem(allocator: std.mem.Allocator, base_path: []const u8) !void {
|
||||
_ = base_path;
|
||||
fn benchStoragePutItem(allocator: std.mem.Allocator) !void {
|
||||
const path = "/tmp/bench_storage_put";
|
||||
defer std.fs.deleteTreeAbsolute(path) catch {};
|
||||
|
||||
var engine = try storage.StorageEngine.init(allocator, path);
|
||||
defer engine.deinit();
|
||||
|
||||
const key_schema = [_]types.KeySchemaElement{
|
||||
.{ .attribute_name = "pk", .key_type = .HASH },
|
||||
};
|
||||
const attr_defs = [_]types.AttributeDefinition{
|
||||
.{ .attribute_name = "pk", .attribute_type = .S },
|
||||
};
|
||||
const key_schema = [_]types.KeySchemaElement{.{ .attribute_name = "pk", .key_type = .HASH }};
|
||||
const attr_defs = [_]types.AttributeDefinition{.{ .attribute_name = "pk", .attribute_type = .S }};
|
||||
|
||||
_ = try engine.createTable("BenchTable", &key_schema, &attr_defs);
|
||||
|
||||
const ops: u64 = 5000;
|
||||
var item_buf: [512]u8 = undefined;
|
||||
|
||||
const start = std.time.nanoTimestamp();
|
||||
var i: u64 = 0;
|
||||
while (i < ops) : (i += 1) {
|
||||
const item = std.fmt.bufPrint(&item_buf, "{{\"pk\":{{\"S\":\"user{d:0>10}\"}},\"name\":{{\"S\":\"User {d}\"}},\"email\":{{\"S\":\"user{d}@example.com\"}}}}", .{ i, i, i }) catch continue;
|
||||
var item = types.Item.init(allocator);
|
||||
defer json.deinitItem(&item, allocator);
|
||||
|
||||
var pk_buf: [32]u8 = undefined;
|
||||
const pk_str = std.fmt.bufPrint(&pk_buf, "user{d:0>10}", .{i}) catch continue;
|
||||
const pk_owned = allocator.dupe(u8, pk_str) catch continue;
|
||||
|
||||
const pk_name = allocator.dupe(u8, "pk") catch continue;
|
||||
item.put(pk_name, types.AttributeValue{ .S = pk_owned }) catch continue;
|
||||
|
||||
engine.putItem("BenchTable", item) catch {};
|
||||
}
|
||||
const end = std.time.nanoTimestamp();
|
||||
|
||||
const result = BenchResult{
|
||||
.name = "PutItem",
|
||||
.ops = ops,
|
||||
.duration_ns = @intCast(end - start),
|
||||
};
|
||||
const result = BenchResult{ .name = "PutItem", .ops = ops, .duration_ns = @intCast(end - start) };
|
||||
result.print();
|
||||
}
|
||||
|
||||
fn benchStorageGetItem(allocator: std.mem.Allocator, base_path: []const u8) !void {
|
||||
_ = base_path;
|
||||
fn benchStorageGetItem(allocator: std.mem.Allocator) !void {
|
||||
const path = "/tmp/bench_storage_get";
|
||||
defer std.fs.deleteTreeAbsolute(path) catch {};
|
||||
|
||||
var engine = try storage.StorageEngine.init(allocator, path);
|
||||
defer engine.deinit();
|
||||
|
||||
const key_schema = [_]types.KeySchemaElement{
|
||||
.{ .attribute_name = "pk", .key_type = .HASH },
|
||||
};
|
||||
const attr_defs = [_]types.AttributeDefinition{
|
||||
.{ .attribute_name = "pk", .attribute_type = .S },
|
||||
};
|
||||
const key_schema = [_]types.KeySchemaElement{.{ .attribute_name = "pk", .key_type = .HASH }};
|
||||
const attr_defs = [_]types.AttributeDefinition{.{ .attribute_name = "pk", .attribute_type = .S }};
|
||||
|
||||
_ = try engine.createTable("BenchTable", &key_schema, &attr_defs);
|
||||
|
||||
// Write data first
|
||||
const ops: u64 = 5000;
|
||||
var item_buf: [512]u8 = undefined;
|
||||
var key_buf: [128]u8 = undefined;
|
||||
|
||||
var i: u64 = 0;
|
||||
while (i < ops) : (i += 1) {
|
||||
const item = try std.fmt.bufPrint(&item_buf, "{{\"pk\":{{\"S\":\"user{d:0>10}\"}},\"data\":{{\"S\":\"test\"}}}}", .{i});
|
||||
try engine.putItem("BenchTable", item);
|
||||
var item = types.Item.init(allocator);
|
||||
defer json.deinitItem(&item, allocator);
|
||||
|
||||
var pk_buf: [32]u8 = undefined;
|
||||
const pk_str = std.fmt.bufPrint(&pk_buf, "user{d:0>10}", .{i}) catch continue;
|
||||
const pk_owned = allocator.dupe(u8, pk_str) catch continue;
|
||||
const pk_name = allocator.dupe(u8, "pk") catch continue;
|
||||
item.put(pk_name, types.AttributeValue{ .S = pk_owned }) catch continue;
|
||||
|
||||
engine.putItem("BenchTable", item) catch {};
|
||||
}
|
||||
|
||||
// Benchmark reads
|
||||
var prng = std.Random.DefaultPrng.init(12345);
|
||||
const random = prng.random();
|
||||
|
||||
@@ -282,60 +212,61 @@ fn benchStorageGetItem(allocator: std.mem.Allocator, base_path: []const u8) !voi
|
||||
i = 0;
|
||||
while (i < ops) : (i += 1) {
|
||||
const idx = random.intRangeAtMost(u64, 0, ops - 1);
|
||||
const key = std.fmt.bufPrint(&key_buf, "{{\"pk\":{{\"S\":\"user{d:0>10}\"}}}}", .{idx}) catch continue;
|
||||
const item = engine.getItem("BenchTable", key) catch continue;
|
||||
if (item) |v| allocator.free(v);
|
||||
|
||||
var key_item = types.Item.init(allocator);
|
||||
defer json.deinitItem(&key_item, allocator);
|
||||
|
||||
var pk_buf: [32]u8 = undefined;
|
||||
const pk_str = std.fmt.bufPrint(&pk_buf, "user{d:0>10}", .{idx}) catch continue;
|
||||
const pk_owned = allocator.dupe(u8, pk_str) catch continue;
|
||||
const pk_name = allocator.dupe(u8, "pk") catch continue;
|
||||
key_item.put(pk_name, types.AttributeValue{ .S = pk_owned }) catch continue;
|
||||
|
||||
const item = engine.getItem("BenchTable", key_item) catch continue;
|
||||
if (item) |it| {
|
||||
var it_mut = it;
|
||||
json.deinitItem(&it_mut, allocator);
|
||||
}
|
||||
}
|
||||
const end = std.time.nanoTimestamp();
|
||||
|
||||
const result = BenchResult{
|
||||
.name = "GetItem",
|
||||
.ops = ops,
|
||||
.duration_ns = @intCast(end - start),
|
||||
};
|
||||
const result = BenchResult{ .name = "GetItem", .ops = ops, .duration_ns = @intCast(end - start) };
|
||||
result.print();
|
||||
}
|
||||
|
||||
fn benchStorageScan(allocator: std.mem.Allocator, base_path: []const u8) !void {
|
||||
_ = base_path;
|
||||
fn benchStorageScan(allocator: std.mem.Allocator) !void {
|
||||
const path = "/tmp/bench_storage_scan";
|
||||
defer std.fs.deleteTreeAbsolute(path) catch {};
|
||||
|
||||
var engine = try storage.StorageEngine.init(allocator, path);
|
||||
defer engine.deinit();
|
||||
|
||||
const key_schema = [_]types.KeySchemaElement{
|
||||
.{ .attribute_name = "pk", .key_type = .HASH },
|
||||
};
|
||||
const attr_defs = [_]types.AttributeDefinition{
|
||||
.{ .attribute_name = "pk", .attribute_type = .S },
|
||||
};
|
||||
const key_schema = [_]types.KeySchemaElement{.{ .attribute_name = "pk", .key_type = .HASH }};
|
||||
const attr_defs = [_]types.AttributeDefinition{.{ .attribute_name = "pk", .attribute_type = .S }};
|
||||
|
||||
_ = try engine.createTable("BenchTable", &key_schema, &attr_defs);
|
||||
|
||||
// Write data first
|
||||
const ops: u64 = 5000;
|
||||
var item_buf: [512]u8 = undefined;
|
||||
|
||||
var i: u64 = 0;
|
||||
while (i < ops) : (i += 1) {
|
||||
const item = try std.fmt.bufPrint(&item_buf, "{{\"pk\":{{\"S\":\"user{d:0>10}\"}},\"data\":{{\"S\":\"test\"}}}}", .{i});
|
||||
try engine.putItem("BenchTable", item);
|
||||
var item = types.Item.init(allocator);
|
||||
defer json.deinitItem(&item, allocator);
|
||||
|
||||
var pk_buf: [32]u8 = undefined;
|
||||
const pk_str = std.fmt.bufPrint(&pk_buf, "user{d:0>10}", .{i}) catch continue;
|
||||
const pk_owned = allocator.dupe(u8, pk_str) catch continue;
|
||||
const pk_name = allocator.dupe(u8, "pk") catch continue;
|
||||
item.put(pk_name, types.AttributeValue{ .S = pk_owned }) catch continue;
|
||||
|
||||
engine.putItem("BenchTable", item) catch {};
|
||||
}
|
||||
|
||||
// Benchmark scan
|
||||
const start = std.time.nanoTimestamp();
|
||||
const items = try engine.scan("BenchTable", null);
|
||||
var result_scan = try engine.scan("BenchTable", null, null);
|
||||
const end = std.time.nanoTimestamp();
|
||||
|
||||
// Cleanup
|
||||
for (items) |item| allocator.free(item);
|
||||
allocator.free(items);
|
||||
result_scan.deinit(allocator);
|
||||
|
||||
const result = BenchResult{
|
||||
.name = "Scan (full table)",
|
||||
.ops = ops,
|
||||
.duration_ns = @intCast(end - start),
|
||||
};
|
||||
const result = BenchResult{ .name = "Scan (full table)", .ops = ops, .duration_ns = @intCast(end - start) };
|
||||
result.print();
|
||||
}
|
||||
|
||||
491
src/dynamodb/expression.zig
Normal file
491
src/dynamodb/expression.zig
Normal file
@@ -0,0 +1,491 @@
|
||||
/// DynamoDB Expression Parser
|
||||
/// Parses KeyConditionExpression, FilterExpression, ProjectionExpression, etc.
|
||||
/// Replaces the temporary string-search hack with proper expression parsing.
|
||||
const std = @import("std");
|
||||
const types = @import("types.zig");
|
||||
const json_module = @import("json.zig");
|
||||
|
||||
// ============================================================================
|
||||
// Key Condition Expression Parsing
|
||||
// ============================================================================
|
||||
|
||||
/// Parsed key condition for Query operations
|
||||
pub const KeyCondition = struct {
|
||||
/// Partition key attribute name (from ExpressionAttributeNames or direct)
|
||||
pk_name: []const u8,
|
||||
/// Partition key value (owned)
|
||||
pk_value: types.AttributeValue,
|
||||
/// Sort key condition (optional)
|
||||
sk_condition: ?SortKeyCondition,
|
||||
|
||||
pub fn deinit(self: *KeyCondition, allocator: std.mem.Allocator) void {
|
||||
json_module.deinitAttributeValue(&self.pk_value, allocator);
|
||||
if (self.sk_condition) |*sk| {
|
||||
sk.deinit(allocator);
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the raw partition key value bytes (for building storage keys)
|
||||
pub fn getPkBytes(self: *const KeyCondition) ![]const u8 {
|
||||
return switch (self.pk_value) {
|
||||
.S => |s| s,
|
||||
.N => |n| n,
|
||||
.B => |b| b,
|
||||
else => error.InvalidKeyType,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
/// Sort key condition operators
|
||||
pub const SortKeyOperator = enum {
|
||||
EQ, // =
|
||||
LT, // <
|
||||
LE, // <=
|
||||
GT, // >
|
||||
GE, // >=
|
||||
BETWEEN, // BETWEEN x AND y
|
||||
BEGINS_WITH, // begins_with(sk, prefix)
|
||||
};
|
||||
|
||||
/// Parsed sort key condition
|
||||
pub const SortKeyCondition = struct {
|
||||
/// Sort key attribute name
|
||||
sk_name: []const u8,
|
||||
/// Comparison operator
|
||||
operator: SortKeyOperator,
|
||||
/// Primary value (or lower bound for BETWEEN) - owned
|
||||
value: types.AttributeValue,
|
||||
/// Upper bound for BETWEEN operator - owned
|
||||
value2: ?types.AttributeValue,
|
||||
|
||||
pub fn deinit(self: *SortKeyCondition, allocator: std.mem.Allocator) void {
|
||||
json_module.deinitAttributeValue(&self.value, allocator);
|
||||
if (self.value2) |*v2| {
|
||||
json_module.deinitAttributeValue(v2, allocator);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/// Parse a KeyConditionExpression with ExpressionAttributeNames and ExpressionAttributeValues
|
||||
/// Returns owned KeyCondition - caller must call deinit()
|
||||
///
|
||||
/// Supported formats:
|
||||
/// - "pk = :pk"
|
||||
/// - "#pk = :pk"
|
||||
/// - "pk = :pk AND sk = :sk"
|
||||
/// - "pk = :pk AND sk > :sk"
|
||||
/// - "pk = :pk AND sk BETWEEN :sk1 AND :sk2"
|
||||
/// - "pk = :pk AND begins_with(sk, :prefix)"
|
||||
pub fn parseKeyConditionExpression(
|
||||
allocator: std.mem.Allocator,
|
||||
expression: []const u8,
|
||||
attribute_names: ?std.StringHashMap([]const u8),
|
||||
attribute_values: std.StringHashMap(types.AttributeValue),
|
||||
) !KeyCondition {
|
||||
var tokenizer = Tokenizer.init(expression);
|
||||
|
||||
// Parse partition key condition: pk_name = :pk_value
|
||||
const pk_name_token = tokenizer.nextToken() orelse return error.InvalidExpression;
|
||||
const pk_name = resolveAttributeName(pk_name_token, attribute_names) orelse return error.InvalidExpression;
|
||||
|
||||
const eq_token = tokenizer.nextToken() orelse return error.InvalidExpression;
|
||||
if (!std.mem.eql(u8, eq_token, "=")) return error.InvalidExpression;
|
||||
|
||||
const pk_value_token = tokenizer.nextToken() orelse return error.InvalidExpression;
|
||||
var pk_value = try resolveAttributeValue(allocator, pk_value_token, attribute_values);
|
||||
errdefer json_module.deinitAttributeValue(&pk_value, allocator);
|
||||
|
||||
// Check for AND (sort key condition)
|
||||
var sk_condition: ?SortKeyCondition = null;
|
||||
if (tokenizer.nextToken()) |and_token| {
|
||||
if (!std.ascii.eqlIgnoreCase(and_token, "AND")) {
|
||||
return error.InvalidExpression;
|
||||
}
|
||||
|
||||
sk_condition = try parseSortKeyCondition(allocator, &tokenizer, attribute_names, attribute_values);
|
||||
}
|
||||
|
||||
return KeyCondition{
|
||||
.pk_name = pk_name,
|
||||
.pk_value = pk_value,
|
||||
.sk_condition = sk_condition,
|
||||
};
|
||||
}
|
||||
|
||||
fn parseSortKeyCondition(
|
||||
allocator: std.mem.Allocator,
|
||||
tokenizer: *Tokenizer,
|
||||
attribute_names: ?std.StringHashMap([]const u8),
|
||||
attribute_values: std.StringHashMap(types.AttributeValue),
|
||||
) !SortKeyCondition {
|
||||
const first_token = tokenizer.nextToken() orelse return error.InvalidExpression;
|
||||
|
||||
// Check for begins_with(sk, :value)
|
||||
if (std.ascii.eqlIgnoreCase(first_token, "begins_with")) {
|
||||
return try parseBeginsWith(allocator, tokenizer, attribute_names, attribute_values);
|
||||
}
|
||||
|
||||
// Otherwise it's: sk_name operator :value
|
||||
const sk_name = resolveAttributeName(first_token, attribute_names) orelse return error.InvalidExpression;
|
||||
|
||||
const op_token = tokenizer.nextToken() orelse return error.InvalidExpression;
|
||||
const operator = parseOperator(op_token) orelse return error.InvalidExpression;
|
||||
|
||||
const value_token = tokenizer.nextToken() orelse return error.InvalidExpression;
|
||||
var value = try resolveAttributeValue(allocator, value_token, attribute_values);
|
||||
errdefer json_module.deinitAttributeValue(&value, allocator);
|
||||
|
||||
// Check for BETWEEN ... AND ...
|
||||
var value2: ?types.AttributeValue = null;
|
||||
if (operator == .BETWEEN) {
|
||||
const and_token = tokenizer.nextToken() orelse return error.InvalidExpression;
|
||||
if (!std.ascii.eqlIgnoreCase(and_token, "AND")) {
|
||||
return error.InvalidExpression;
|
||||
}
|
||||
|
||||
const value2_token = tokenizer.nextToken() orelse return error.InvalidExpression;
|
||||
value2 = try resolveAttributeValue(allocator, value2_token, attribute_values);
|
||||
}
|
||||
|
||||
return SortKeyCondition{
|
||||
.sk_name = sk_name,
|
||||
.operator = operator,
|
||||
.value = value,
|
||||
.value2 = value2,
|
||||
};
|
||||
}
|
||||
|
||||
fn parseBeginsWith(
|
||||
allocator: std.mem.Allocator,
|
||||
tokenizer: *Tokenizer,
|
||||
attribute_names: ?std.StringHashMap([]const u8),
|
||||
attribute_values: std.StringHashMap(types.AttributeValue),
|
||||
) !SortKeyCondition {
|
||||
// Expect: ( sk_name , :value )
|
||||
const lparen = tokenizer.nextToken() orelse return error.InvalidExpression;
|
||||
if (!std.mem.eql(u8, lparen, "(")) return error.InvalidExpression;
|
||||
|
||||
const sk_name_token = tokenizer.nextToken() orelse return error.InvalidExpression;
|
||||
const sk_name = resolveAttributeName(sk_name_token, attribute_names) orelse return error.InvalidExpression;
|
||||
|
||||
const comma = tokenizer.nextToken() orelse return error.InvalidExpression;
|
||||
if (!std.mem.eql(u8, comma, ",")) return error.InvalidExpression;
|
||||
|
||||
const value_token = tokenizer.nextToken() orelse return error.InvalidExpression;
|
||||
var value = try resolveAttributeValue(allocator, value_token, attribute_values);
|
||||
errdefer json_module.deinitAttributeValue(&value, allocator);
|
||||
|
||||
const rparen = tokenizer.nextToken() orelse return error.InvalidExpression;
|
||||
if (!std.mem.eql(u8, rparen, ")")) return error.InvalidExpression;
|
||||
|
||||
return SortKeyCondition{
|
||||
.sk_name = sk_name,
|
||||
.operator = .BEGINS_WITH,
|
||||
.value = value,
|
||||
.value2 = null,
|
||||
};
|
||||
}
|
||||
|
||||
fn parseOperator(token: []const u8) ?SortKeyOperator {
|
||||
if (std.mem.eql(u8, token, "=")) return .EQ;
|
||||
if (std.mem.eql(u8, token, "<")) return .LT;
|
||||
if (std.mem.eql(u8, token, "<=")) return .LE;
|
||||
if (std.mem.eql(u8, token, ">")) return .GT;
|
||||
if (std.mem.eql(u8, token, ">=")) return .GE;
|
||||
if (std.ascii.eqlIgnoreCase(token, "BETWEEN")) return .BETWEEN;
|
||||
return null;
|
||||
}
|
||||
|
||||
fn resolveAttributeName(token: []const u8, names: ?std.StringHashMap([]const u8)) ?[]const u8 {
|
||||
if (token.len > 0 and token[0] == '#') {
|
||||
// Expression attribute name placeholder
|
||||
if (names) |n| {
|
||||
return n.get(token);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
// Direct attribute name
|
||||
return token;
|
||||
}
|
||||
|
||||
fn resolveAttributeValue(
|
||||
allocator: std.mem.Allocator,
|
||||
token: []const u8,
|
||||
values: std.StringHashMap(types.AttributeValue),
|
||||
) !types.AttributeValue {
|
||||
if (token.len > 0 and token[0] == ':') {
|
||||
// Expression attribute value placeholder
|
||||
const original = values.get(token) orelse return error.MissingAttributeValue;
|
||||
return try json_module.deepCopyAttributeValue(allocator, original);
|
||||
}
|
||||
return error.InvalidExpression;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Request Parsing Helpers
|
||||
// ============================================================================
|
||||
|
||||
/// Parse ExpressionAttributeNames from request body
|
||||
/// Returns null if not present
|
||||
pub fn parseExpressionAttributeNames(
|
||||
allocator: std.mem.Allocator,
|
||||
request_body: []const u8,
|
||||
) !?std.StringHashMap([]const u8) {
|
||||
const parsed = std.json.parseFromSlice(std.json.Value, allocator, request_body, .{}) catch return null;
|
||||
defer parsed.deinit();
|
||||
|
||||
const root = switch (parsed.value) {
|
||||
.object => |o| o,
|
||||
else => return null,
|
||||
};
|
||||
|
||||
const names_val = root.get("ExpressionAttributeNames") orelse return null;
|
||||
const names_obj = switch (names_val) {
|
||||
.object => |o| o,
|
||||
else => return null,
|
||||
};
|
||||
|
||||
var result = std.StringHashMap([]const u8).init(allocator);
|
||||
errdefer {
|
||||
var iter = result.iterator();
|
||||
while (iter.next()) |entry| {
|
||||
allocator.free(entry.key_ptr.*);
|
||||
allocator.free(entry.value_ptr.*);
|
||||
}
|
||||
result.deinit();
|
||||
}
|
||||
|
||||
var iter = names_obj.iterator();
|
||||
while (iter.next()) |entry| {
|
||||
const key = try allocator.dupe(u8, entry.key_ptr.*);
|
||||
errdefer allocator.free(key);
|
||||
|
||||
const value = switch (entry.value_ptr.*) {
|
||||
.string => |s| try allocator.dupe(u8, s),
|
||||
else => {
|
||||
allocator.free(key);
|
||||
continue;
|
||||
},
|
||||
};
|
||||
|
||||
try result.put(key, value);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Parse ExpressionAttributeValues from request body
|
||||
/// Returns owned HashMap - caller must free
|
||||
pub fn parseExpressionAttributeValues(
|
||||
allocator: std.mem.Allocator,
|
||||
request_body: []const u8,
|
||||
) !std.StringHashMap(types.AttributeValue) {
|
||||
const parsed = std.json.parseFromSlice(std.json.Value, allocator, request_body, .{}) catch
|
||||
return std.StringHashMap(types.AttributeValue).init(allocator);
|
||||
defer parsed.deinit();
|
||||
|
||||
const root = switch (parsed.value) {
|
||||
.object => |o| o,
|
||||
else => return std.StringHashMap(types.AttributeValue).init(allocator),
|
||||
};
|
||||
|
||||
const values_val = root.get("ExpressionAttributeValues") orelse
|
||||
return std.StringHashMap(types.AttributeValue).init(allocator);
|
||||
const values_obj = switch (values_val) {
|
||||
.object => |o| o,
|
||||
else => return std.StringHashMap(types.AttributeValue).init(allocator),
|
||||
};
|
||||
|
||||
var result = std.StringHashMap(types.AttributeValue).init(allocator);
|
||||
errdefer {
|
||||
var iter = result.iterator();
|
||||
while (iter.next()) |entry| {
|
||||
allocator.free(entry.key_ptr.*);
|
||||
json_module.deinitAttributeValue(entry.value_ptr, allocator);
|
||||
}
|
||||
result.deinit();
|
||||
}
|
||||
|
||||
var iter = values_obj.iterator();
|
||||
while (iter.next()) |entry| {
|
||||
const key = try allocator.dupe(u8, entry.key_ptr.*);
|
||||
errdefer allocator.free(key);
|
||||
|
||||
var value = json_module.parseAttributeValue(allocator, entry.value_ptr.*) catch continue;
|
||||
errdefer json_module.deinitAttributeValue(&value, allocator);
|
||||
|
||||
try result.put(key, value);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Parse KeyConditionExpression string from request body
|
||||
pub fn parseKeyConditionExpressionString(
|
||||
request_body: []const u8,
|
||||
) ?[]const u8 {
|
||||
// Use a simple search to avoid allocation for this common operation
|
||||
const marker = "\"KeyConditionExpression\"";
|
||||
const start_idx = std.mem.indexOf(u8, request_body, marker) orelse return null;
|
||||
|
||||
// Find the colon after the key
|
||||
const colon_idx = std.mem.indexOfPos(u8, request_body, start_idx + marker.len, ":") orelse return null;
|
||||
|
||||
// Find the opening quote
|
||||
var pos = colon_idx + 1;
|
||||
while (pos < request_body.len and request_body[pos] != '"') : (pos += 1) {}
|
||||
if (pos >= request_body.len) return null;
|
||||
pos += 1; // Skip opening quote
|
||||
|
||||
// Find the closing quote (handle escaped quotes)
|
||||
const value_start = pos;
|
||||
while (pos < request_body.len) {
|
||||
if (request_body[pos] == '"' and (pos == 0 or request_body[pos - 1] != '\\')) {
|
||||
return request_body[value_start..pos];
|
||||
}
|
||||
pos += 1;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// Convenience function to parse and evaluate a complete Query key condition
|
||||
/// Returns owned KeyCondition - caller must call deinit()
|
||||
pub fn parseQueryKeyCondition(
|
||||
allocator: std.mem.Allocator,
|
||||
request_body: []const u8,
|
||||
) !?KeyCondition {
|
||||
// Parse expression string
|
||||
const expression = parseKeyConditionExpressionString(request_body) orelse return null;
|
||||
|
||||
// Parse attribute names (optional)
|
||||
var attr_names = try parseExpressionAttributeNames(allocator, request_body);
|
||||
defer if (attr_names) |*names| {
|
||||
deinitExpressionAttributeNames(names, allocator);
|
||||
};
|
||||
|
||||
// Parse attribute values
|
||||
var attr_values = try parseExpressionAttributeValues(allocator, request_body);
|
||||
defer deinitExpressionAttributeValues(&attr_values, allocator);
|
||||
|
||||
return try parseKeyConditionExpression(allocator, expression, attr_names, attr_values);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Simple Tokenizer
|
||||
// ============================================================================
|
||||
|
||||
const Tokenizer = struct {
|
||||
input: []const u8,
|
||||
pos: usize,
|
||||
|
||||
pub fn init(input: []const u8) Tokenizer {
|
||||
return .{ .input = input, .pos = 0 };
|
||||
}
|
||||
|
||||
pub fn nextToken(self: *Tokenizer) ?[]const u8 {
|
||||
// Skip whitespace
|
||||
while (self.pos < self.input.len and std.ascii.isWhitespace(self.input[self.pos])) {
|
||||
self.pos += 1;
|
||||
}
|
||||
|
||||
if (self.pos >= self.input.len) return null;
|
||||
|
||||
const start = self.pos;
|
||||
|
||||
// Single-character tokens
|
||||
const c = self.input[self.pos];
|
||||
if (c == '(' or c == ')' or c == ',') {
|
||||
self.pos += 1;
|
||||
return self.input[start..self.pos];
|
||||
}
|
||||
|
||||
// Two-character operators
|
||||
if (self.pos + 1 < self.input.len) {
|
||||
const two = self.input[self.pos .. self.pos + 2];
|
||||
if (std.mem.eql(u8, two, "<=") or std.mem.eql(u8, two, ">=") or std.mem.eql(u8, two, "<>")) {
|
||||
self.pos += 2;
|
||||
return two;
|
||||
}
|
||||
}
|
||||
|
||||
// Single-character operators
|
||||
if (c == '=' or c == '<' or c == '>') {
|
||||
self.pos += 1;
|
||||
return self.input[start..self.pos];
|
||||
}
|
||||
|
||||
// Identifier or keyword (includes :placeholder and #name)
|
||||
while (self.pos < self.input.len) {
|
||||
const ch = self.input[self.pos];
|
||||
if (std.ascii.isAlphanumeric(ch) or ch == '_' or ch == ':' or ch == '#' or ch == '-') {
|
||||
self.pos += 1;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (self.pos > start) {
|
||||
return self.input[start..self.pos];
|
||||
}
|
||||
|
||||
// Unknown character, skip it
|
||||
self.pos += 1;
|
||||
return self.nextToken();
|
||||
}
|
||||
};
|
||||
|
||||
// ============================================================================
|
||||
// Helpers for freeing parsed expression data
|
||||
// ============================================================================
|
||||
|
||||
pub fn deinitExpressionAttributeNames(names: *std.StringHashMap([]const u8), allocator: std.mem.Allocator) void {
|
||||
var iter = names.iterator();
|
||||
while (iter.next()) |entry| {
|
||||
allocator.free(entry.key_ptr.*);
|
||||
allocator.free(entry.value_ptr.*);
|
||||
}
|
||||
names.deinit();
|
||||
}
|
||||
|
||||
pub fn deinitExpressionAttributeValues(values: *std.StringHashMap(types.AttributeValue), allocator: std.mem.Allocator) void {
|
||||
var iter = values.iterator();
|
||||
while (iter.next()) |entry| {
|
||||
allocator.free(entry.key_ptr.*);
|
||||
json_module.deinitAttributeValue(entry.value_ptr, allocator);
|
||||
}
|
||||
values.deinit();
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Tests
|
||||
// ============================================================================
|
||||
|
||||
test "tokenizer basic" {
|
||||
var t = Tokenizer.init("pk = :pk AND sk > :sk");
|
||||
|
||||
try std.testing.expectEqualStrings("pk", t.nextToken().?);
|
||||
try std.testing.expectEqualStrings("=", t.nextToken().?);
|
||||
try std.testing.expectEqualStrings(":pk", t.nextToken().?);
|
||||
try std.testing.expectEqualStrings("AND", t.nextToken().?);
|
||||
try std.testing.expectEqualStrings("sk", t.nextToken().?);
|
||||
try std.testing.expectEqualStrings(">", t.nextToken().?);
|
||||
try std.testing.expectEqualStrings(":sk", t.nextToken().?);
|
||||
try std.testing.expect(t.nextToken() == null);
|
||||
}
|
||||
|
||||
test "tokenizer begins_with" {
|
||||
var t = Tokenizer.init("pk = :pk AND begins_with(sk, :prefix)");
|
||||
|
||||
try std.testing.expectEqualStrings("pk", t.nextToken().?);
|
||||
try std.testing.expectEqualStrings("=", t.nextToken().?);
|
||||
try std.testing.expectEqualStrings(":pk", t.nextToken().?);
|
||||
try std.testing.expectEqualStrings("AND", t.nextToken().?);
|
||||
try std.testing.expectEqualStrings("begins_with", t.nextToken().?);
|
||||
try std.testing.expectEqualStrings("(", t.nextToken().?);
|
||||
try std.testing.expectEqualStrings("sk", t.nextToken().?);
|
||||
try std.testing.expectEqualStrings(",", t.nextToken().?);
|
||||
try std.testing.expectEqualStrings(":prefix", t.nextToken().?);
|
||||
try std.testing.expectEqualStrings(")", t.nextToken().?);
|
||||
try std.testing.expect(t.nextToken() == null);
|
||||
}
|
||||
@@ -1,169 +1,314 @@
|
||||
/// DynamoDB API request handlers
|
||||
/// DynamoDB API request handlers with proper concurrency support
|
||||
/// - Uses request-scoped arena allocator for temporary allocations
|
||||
/// - Proper expression parsing for Query operations
|
||||
/// - Correct key type reconstruction for pagination
|
||||
const std = @import("std");
|
||||
const http = @import("../http.zig");
|
||||
const storage = @import("storage.zig");
|
||||
const types = @import("types.zig");
|
||||
const json = @import("json.zig");
|
||||
const expression = @import("expression.zig");
|
||||
const key_codec = @import("../key_codec.zig");
|
||||
|
||||
pub const ApiHandler = struct {
|
||||
engine: *storage.StorageEngine,
|
||||
allocator: std.mem.Allocator,
|
||||
main_allocator: std.mem.Allocator,
|
||||
|
||||
const Self = @This();
|
||||
|
||||
pub fn init(allocator: std.mem.Allocator, engine: *storage.StorageEngine) Self {
|
||||
return .{
|
||||
.engine = engine,
|
||||
.allocator = allocator,
|
||||
};
|
||||
pub fn init(main_allocator: std.mem.Allocator, engine: *storage.StorageEngine) Self {
|
||||
return .{ .engine = engine, .main_allocator = main_allocator };
|
||||
}
|
||||
|
||||
pub fn handle(self: *Self, request: *const http.Request) http.Response {
|
||||
var response = http.Response.init(self.allocator);
|
||||
pub fn handleRequest(ctx: *anyopaque, request: *const http.Request, request_alloc: std.mem.Allocator) http.Response {
|
||||
const self: *Self = @ptrCast(@alignCast(ctx));
|
||||
return self.handle(request, request_alloc);
|
||||
}
|
||||
|
||||
// Add standard DynamoDB headers
|
||||
fn handle(self: *Self, request: *const http.Request, request_alloc: std.mem.Allocator) http.Response {
|
||||
var response = http.Response.init(request_alloc);
|
||||
response.addHeader("Content-Type", "application/x-amz-json-1.0") catch {};
|
||||
response.addHeader("x-amzn-RequestId", "local-request-id") catch {};
|
||||
|
||||
// Get operation from X-Amz-Target header
|
||||
const target = request.getHeader("X-Amz-Target") orelse {
|
||||
return self.errorResponse(&response, .ValidationException, "Missing X-Amz-Target header");
|
||||
return self.errorResponse(&response, .ValidationException, "Missing X-Amz-Target header", request_alloc);
|
||||
};
|
||||
|
||||
const operation = types.Operation.fromTarget(target);
|
||||
|
||||
switch (operation) {
|
||||
.CreateTable => self.handleCreateTable(request, &response),
|
||||
.DeleteTable => self.handleDeleteTable(request, &response),
|
||||
.DescribeTable => self.handleDescribeTable(request, &response),
|
||||
.ListTables => self.handleListTables(request, &response),
|
||||
.PutItem => self.handlePutItem(request, &response),
|
||||
.GetItem => self.handleGetItem(request, &response),
|
||||
.DeleteItem => self.handleDeleteItem(request, &response),
|
||||
.Query => self.handleQuery(request, &response),
|
||||
.Scan => self.handleScan(request, &response),
|
||||
.Unknown => {
|
||||
return self.errorResponse(&response, .ValidationException, "Unknown operation");
|
||||
},
|
||||
else => {
|
||||
return self.errorResponse(&response, .ValidationException, "Operation not implemented");
|
||||
},
|
||||
.CreateTable => self.handleCreateTable(request, &response, request_alloc),
|
||||
.DeleteTable => self.handleDeleteTable(request, &response, request_alloc),
|
||||
.DescribeTable => self.handleDescribeTable(request, &response, request_alloc),
|
||||
.ListTables => self.handleListTables(request, &response, request_alloc),
|
||||
.PutItem => self.handlePutItem(request, &response, request_alloc),
|
||||
.GetItem => self.handleGetItem(request, &response, request_alloc),
|
||||
.DeleteItem => self.handleDeleteItem(request, &response, request_alloc),
|
||||
.Query => self.handleQuery(request, &response, request_alloc),
|
||||
.Scan => self.handleScan(request, &response, request_alloc),
|
||||
.Unknown => return self.errorResponse(&response, .ValidationException, "Unknown operation", request_alloc),
|
||||
else => return self.errorResponse(&response, .ValidationException, "Operation not implemented", request_alloc),
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
fn handleCreateTable(self: *Self, request: *const http.Request, response: *http.Response) void {
|
||||
// Parse table name from request body
|
||||
const table_name = extractJsonString(request.body, "TableName") orelse {
|
||||
_ = self.errorResponse(response, .ValidationException, "Missing TableName");
|
||||
fn handleCreateTable(self: *Self, request: *const http.Request, response: *http.Response, request_alloc: std.mem.Allocator) void {
|
||||
const parsed = std.json.parseFromSlice(std.json.Value, request_alloc, request.body, .{}) catch {
|
||||
_ = self.errorResponse(response, .ValidationException, "Invalid JSON", request_alloc);
|
||||
return;
|
||||
};
|
||||
defer parsed.deinit();
|
||||
|
||||
const root = switch (parsed.value) {
|
||||
.object => |o| o,
|
||||
else => {
|
||||
_ = self.errorResponse(response, .ValidationException, "Request must be an object", request_alloc);
|
||||
return;
|
||||
},
|
||||
};
|
||||
|
||||
const table_name_val = root.get("TableName") orelse {
|
||||
_ = self.errorResponse(response, .ValidationException, "Missing TableName", request_alloc);
|
||||
return;
|
||||
};
|
||||
const table_name = switch (table_name_val) {
|
||||
.string => |s| s,
|
||||
else => {
|
||||
_ = self.errorResponse(response, .ValidationException, "TableName must be a string", request_alloc);
|
||||
return;
|
||||
},
|
||||
};
|
||||
|
||||
const key_schema = self.parseKeySchema(root, request_alloc) catch |err| {
|
||||
const msg = switch (err) {
|
||||
error.MissingKeySchema => "Missing KeySchema",
|
||||
error.InvalidKeySchema => "Invalid KeySchema format",
|
||||
error.NoHashKey => "KeySchema must contain exactly one HASH key",
|
||||
error.MultipleHashKeys => "KeySchema can only contain one HASH key",
|
||||
error.MultipleRangeKeys => "KeySchema can only contain one RANGE key",
|
||||
error.InvalidKeyType => "Invalid KeyType (must be HASH or RANGE)",
|
||||
else => "Invalid KeySchema",
|
||||
};
|
||||
_ = self.errorResponse(response, .ValidationException, msg, request_alloc);
|
||||
return;
|
||||
};
|
||||
|
||||
// Simplified: create with default key schema
|
||||
const key_schema = [_]types.KeySchemaElement{
|
||||
.{ .attribute_name = "pk", .key_type = .HASH },
|
||||
const attr_defs = self.parseAttributeDefinitions(root, request_alloc) catch |err| {
|
||||
const msg = switch (err) {
|
||||
error.MissingAttributeDefinitions => "Missing AttributeDefinitions",
|
||||
error.InvalidAttributeDefinitions => "Invalid AttributeDefinitions format",
|
||||
error.InvalidAttributeType => "Invalid AttributeType (must be S, N, or B)",
|
||||
error.DuplicateAttributeName => "Duplicate attribute name in AttributeDefinitions",
|
||||
else => "Invalid AttributeDefinitions",
|
||||
};
|
||||
const attr_defs = [_]types.AttributeDefinition{
|
||||
.{ .attribute_name = "pk", .attribute_type = .S },
|
||||
_ = self.errorResponse(response, .ValidationException, msg, request_alloc);
|
||||
return;
|
||||
};
|
||||
|
||||
const desc = self.engine.createTable(table_name, &key_schema, &attr_defs) catch |err| {
|
||||
self.validateKeyAttributesDefined(key_schema, attr_defs) catch |err| {
|
||||
const msg = switch (err) {
|
||||
error.KeyAttributeNotDefined => "Key attribute not defined in AttributeDefinitions",
|
||||
else => "Schema validation failed",
|
||||
};
|
||||
_ = self.errorResponse(response, .ValidationException, msg, request_alloc);
|
||||
return;
|
||||
};
|
||||
|
||||
const desc = self.engine.createTable(table_name, key_schema, attr_defs) catch |err| {
|
||||
switch (err) {
|
||||
storage.StorageError.TableAlreadyExists => {
|
||||
_ = self.errorResponse(response, .ResourceInUseException, "Table already exists");
|
||||
_ = self.errorResponse(response, .ResourceInUseException, "Table already exists", request_alloc);
|
||||
},
|
||||
else => {
|
||||
_ = self.errorResponse(response, .InternalServerError, "Failed to create table");
|
||||
_ = self.errorResponse(response, .InternalServerError, "Failed to create table", request_alloc);
|
||||
},
|
||||
}
|
||||
return;
|
||||
};
|
||||
|
||||
// Build response
|
||||
const resp_body = std.fmt.allocPrint(
|
||||
self.allocator,
|
||||
request_alloc,
|
||||
"{{\"TableDescription\":{{\"TableName\":\"{s}\",\"TableStatus\":\"{s}\",\"CreationDateTime\":{d}}}}}",
|
||||
.{ desc.table_name, desc.table_status.toString(), desc.creation_date_time },
|
||||
) catch {
|
||||
_ = self.errorResponse(response, .InternalServerError, "Serialization failed");
|
||||
_ = self.errorResponse(response, .InternalServerError, "Serialization failed", request_alloc);
|
||||
return;
|
||||
};
|
||||
defer self.allocator.free(resp_body);
|
||||
|
||||
response.setBody(resp_body) catch {};
|
||||
}
|
||||
|
||||
fn handleDeleteTable(self: *Self, request: *const http.Request, response: *http.Response) void {
|
||||
const table_name = extractJsonString(request.body, "TableName") orelse {
|
||||
_ = self.errorResponse(response, .ValidationException, "Missing TableName");
|
||||
fn parseKeySchema(self: *Self, root: std.json.ObjectMap, allocator: std.mem.Allocator) ![]types.KeySchemaElement {
|
||||
_ = self;
|
||||
const key_schema_val = root.get("KeySchema") orelse return error.MissingKeySchema;
|
||||
const key_schema_array = switch (key_schema_val) {
|
||||
.array => |a| a,
|
||||
else => return error.InvalidKeySchema,
|
||||
};
|
||||
|
||||
if (key_schema_array.items.len == 0 or key_schema_array.items.len > 2) return error.InvalidKeySchema;
|
||||
|
||||
var key_schema = std.ArrayList(types.KeySchemaElement).init(allocator);
|
||||
errdefer {
|
||||
for (key_schema.items) |ks| allocator.free(ks.attribute_name);
|
||||
key_schema.deinit();
|
||||
}
|
||||
|
||||
var hash_count: u32 = 0;
|
||||
var range_count: u32 = 0;
|
||||
|
||||
for (key_schema_array.items) |item| {
|
||||
const obj = switch (item) {
|
||||
.object => |o| o,
|
||||
else => return error.InvalidKeySchema,
|
||||
};
|
||||
|
||||
const attr_name_val = obj.get("AttributeName") orelse return error.InvalidKeySchema;
|
||||
const attr_name_str = switch (attr_name_val) {
|
||||
.string => |s| s,
|
||||
else => return error.InvalidKeySchema,
|
||||
};
|
||||
const attr_name = try allocator.dupe(u8, attr_name_str);
|
||||
errdefer allocator.free(attr_name);
|
||||
|
||||
const key_type_val = obj.get("KeyType") orelse return error.InvalidKeySchema;
|
||||
const key_type_str = switch (key_type_val) {
|
||||
.string => |s| s,
|
||||
else => return error.InvalidKeySchema,
|
||||
};
|
||||
|
||||
const key_type = if (std.mem.eql(u8, key_type_str, "HASH")) types.KeyType.HASH else if (std.mem.eql(u8, key_type_str, "RANGE")) types.KeyType.RANGE else return error.InvalidKeyType;
|
||||
|
||||
switch (key_type) {
|
||||
.HASH => hash_count += 1,
|
||||
.RANGE => range_count += 1,
|
||||
}
|
||||
try key_schema.append(.{ .attribute_name = attr_name, .key_type = key_type });
|
||||
}
|
||||
|
||||
if (hash_count == 0) return error.NoHashKey;
|
||||
if (hash_count > 1) return error.MultipleHashKeys;
|
||||
if (range_count > 1) return error.MultipleRangeKeys;
|
||||
|
||||
return key_schema.toOwnedSlice();
|
||||
}
|
||||
|
||||
fn parseAttributeDefinitions(self: *Self, root: std.json.ObjectMap, allocator: std.mem.Allocator) ![]types.AttributeDefinition {
|
||||
_ = self;
|
||||
const attr_defs_val = root.get("AttributeDefinitions") orelse return error.MissingAttributeDefinitions;
|
||||
const attr_defs_array = switch (attr_defs_val) {
|
||||
.array => |a| a,
|
||||
else => return error.InvalidAttributeDefinitions,
|
||||
};
|
||||
|
||||
if (attr_defs_array.items.len == 0) return error.InvalidAttributeDefinitions;
|
||||
|
||||
var attr_defs = std.ArrayList(types.AttributeDefinition).init(allocator);
|
||||
errdefer {
|
||||
for (attr_defs.items) |ad| allocator.free(ad.attribute_name);
|
||||
attr_defs.deinit();
|
||||
}
|
||||
|
||||
var seen = std.StringHashMap(void).init(allocator);
|
||||
defer seen.deinit();
|
||||
|
||||
for (attr_defs_array.items) |item| {
|
||||
const obj = switch (item) {
|
||||
.object => |o| o,
|
||||
else => return error.InvalidAttributeDefinitions,
|
||||
};
|
||||
|
||||
const attr_name_val = obj.get("AttributeName") orelse return error.InvalidAttributeDefinitions;
|
||||
const attr_name_str = switch (attr_name_val) {
|
||||
.string => |s| s,
|
||||
else => return error.InvalidAttributeDefinitions,
|
||||
};
|
||||
|
||||
if (seen.contains(attr_name_str)) return error.DuplicateAttributeName;
|
||||
try seen.put(attr_name_str, {});
|
||||
|
||||
const attr_name = try allocator.dupe(u8, attr_name_str);
|
||||
errdefer allocator.free(attr_name);
|
||||
|
||||
const attr_type_val = obj.get("AttributeType") orelse return error.InvalidAttributeDefinitions;
|
||||
const attr_type_str = switch (attr_type_val) {
|
||||
.string => |s| s,
|
||||
else => return error.InvalidAttributeDefinitions,
|
||||
};
|
||||
|
||||
const attr_type = if (std.mem.eql(u8, attr_type_str, "S")) types.ScalarAttributeType.S else if (std.mem.eql(u8, attr_type_str, "N")) types.ScalarAttributeType.N else if (std.mem.eql(u8, attr_type_str, "B")) types.ScalarAttributeType.B else return error.InvalidAttributeType;
|
||||
|
||||
try attr_defs.append(.{ .attribute_name = attr_name, .attribute_type = attr_type });
|
||||
}
|
||||
|
||||
return attr_defs.toOwnedSlice();
|
||||
}
|
||||
|
||||
fn validateKeyAttributesDefined(self: *Self, key_schema: []const types.KeySchemaElement, attr_defs: []const types.AttributeDefinition) !void {
|
||||
_ = self;
|
||||
for (key_schema) |key_elem| {
|
||||
var found = false;
|
||||
for (attr_defs) |attr_def| {
|
||||
if (std.mem.eql(u8, key_elem.attribute_name, attr_def.attribute_name)) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!found) return error.KeyAttributeNotDefined;
|
||||
}
|
||||
}
|
||||
|
||||
fn handleDeleteTable(self: *Self, request: *const http.Request, response: *http.Response, request_alloc: std.mem.Allocator) void {
|
||||
const table_name = json.parseTableName(request_alloc, request.body) catch {
|
||||
_ = self.errorResponse(response, .ValidationException, "Invalid request or missing TableName", request_alloc);
|
||||
return;
|
||||
};
|
||||
|
||||
self.engine.deleteTable(table_name) catch |err| {
|
||||
switch (err) {
|
||||
storage.StorageError.TableNotFound => {
|
||||
_ = self.errorResponse(response, .ResourceNotFoundException, "Table not found");
|
||||
},
|
||||
else => {
|
||||
_ = self.errorResponse(response, .InternalServerError, "Failed to delete table");
|
||||
},
|
||||
storage.StorageError.TableNotFound => _ = self.errorResponse(response, .ResourceNotFoundException, "Table not found", request_alloc),
|
||||
else => _ = self.errorResponse(response, .InternalServerError, "Failed to delete table", request_alloc),
|
||||
}
|
||||
return;
|
||||
};
|
||||
|
||||
const resp_body = std.fmt.allocPrint(
|
||||
self.allocator,
|
||||
"{{\"TableDescription\":{{\"TableName\":\"{s}\",\"TableStatus\":\"DELETING\"}}}}",
|
||||
.{table_name},
|
||||
) catch return;
|
||||
defer self.allocator.free(resp_body);
|
||||
|
||||
const resp_body = std.fmt.allocPrint(request_alloc, "{{\"TableDescription\":{{\"TableName\":\"{s}\",\"TableStatus\":\"DELETING\"}}}}", .{table_name}) catch return;
|
||||
response.setBody(resp_body) catch {};
|
||||
}
|
||||
|
||||
fn handleDescribeTable(self: *Self, request: *const http.Request, response: *http.Response) void {
|
||||
const table_name = extractJsonString(request.body, "TableName") orelse {
|
||||
_ = self.errorResponse(response, .ValidationException, "Missing TableName");
|
||||
fn handleDescribeTable(self: *Self, request: *const http.Request, response: *http.Response, request_alloc: std.mem.Allocator) void {
|
||||
const table_name = json.parseTableName(request_alloc, request.body) catch {
|
||||
_ = self.errorResponse(response, .ValidationException, "Invalid request or missing TableName", request_alloc);
|
||||
return;
|
||||
};
|
||||
|
||||
const desc = self.engine.describeTable(table_name) catch |err| {
|
||||
switch (err) {
|
||||
storage.StorageError.TableNotFound => {
|
||||
_ = self.errorResponse(response, .ResourceNotFoundException, "Table not found");
|
||||
},
|
||||
else => {
|
||||
_ = self.errorResponse(response, .InternalServerError, "Failed to describe table");
|
||||
},
|
||||
storage.StorageError.TableNotFound => _ = self.errorResponse(response, .ResourceNotFoundException, "Table not found", request_alloc),
|
||||
else => _ = self.errorResponse(response, .InternalServerError, "Failed to describe table", request_alloc),
|
||||
}
|
||||
return;
|
||||
};
|
||||
|
||||
const resp_body = std.fmt.allocPrint(
|
||||
self.allocator,
|
||||
"{{\"Table\":{{\"TableName\":\"{s}\",\"TableStatus\":\"{s}\",\"ItemCount\":{d},\"TableSizeBytes\":{d}}}}}",
|
||||
.{ desc.table_name, desc.table_status.toString(), desc.item_count, desc.table_size_bytes },
|
||||
) catch return;
|
||||
defer self.allocator.free(resp_body);
|
||||
|
||||
const resp_body = std.fmt.allocPrint(request_alloc, "{{\"Table\":{{\"TableName\":\"{s}\",\"TableStatus\":\"{s}\",\"ItemCount\":{d},\"TableSizeBytes\":{d}}}}}", .{ desc.table_name, desc.table_status.toString(), desc.item_count, desc.table_size_bytes }) catch return;
|
||||
response.setBody(resp_body) catch {};
|
||||
}
|
||||
|
||||
fn handleListTables(self: *Self, request: *const http.Request, response: *http.Response) void {
|
||||
fn handleListTables(self: *Self, request: *const http.Request, response: *http.Response, request_alloc: std.mem.Allocator) void {
|
||||
_ = request;
|
||||
|
||||
const tables = self.engine.listTables() catch {
|
||||
_ = self.errorResponse(response, .InternalServerError, "Failed to list tables");
|
||||
_ = self.errorResponse(response, .InternalServerError, "Failed to list tables", request_alloc);
|
||||
return;
|
||||
};
|
||||
defer {
|
||||
for (tables) |t| self.allocator.free(t);
|
||||
self.allocator.free(tables);
|
||||
for (tables) |t| self.main_allocator.free(t);
|
||||
self.main_allocator.free(tables);
|
||||
}
|
||||
|
||||
var buf = std.ArrayList(u8){};
|
||||
defer buf.deinit(self.allocator);
|
||||
const writer = buf.writer(self.allocator);
|
||||
var buf = std.ArrayList(u8).init(request_alloc);
|
||||
defer buf.deinit();
|
||||
const writer = buf.writer();
|
||||
|
||||
writer.writeAll("{\"TableNames\":[") catch return;
|
||||
for (tables, 0..) |table, i| {
|
||||
@@ -171,55 +316,32 @@ pub const ApiHandler = struct {
|
||||
writer.print("\"{s}\"", .{table}) catch return;
|
||||
}
|
||||
writer.writeAll("]}") catch return;
|
||||
|
||||
response.setBody(buf.items) catch {};
|
||||
}
|
||||
|
||||
fn handlePutItem(self: *Self, request: *const http.Request, response: *http.Response) void {
|
||||
const table_name = extractJsonString(request.body, "TableName") orelse {
|
||||
_ = self.errorResponse(response, .ValidationException, "Missing TableName");
|
||||
fn handlePutItem(self: *Self, request: *const http.Request, response: *http.Response, request_alloc: std.mem.Allocator) void {
|
||||
const table_name = json.parseTableName(request_alloc, request.body) catch {
|
||||
_ = self.errorResponse(response, .ValidationException, "Invalid request or missing TableName", request_alloc);
|
||||
return;
|
||||
};
|
||||
|
||||
// Extract Item from request - simplified extraction
|
||||
const item_start = std.mem.indexOf(u8, request.body, "\"Item\":") orelse {
|
||||
_ = self.errorResponse(response, .ValidationException, "Missing Item");
|
||||
var item = json.parseItemFromRequest(request_alloc, request.body) catch |err| {
|
||||
const msg = switch (err) {
|
||||
error.MissingItem => "Missing Item field",
|
||||
error.InvalidRequest => "Invalid request format",
|
||||
else => "Invalid Item format",
|
||||
};
|
||||
_ = self.errorResponse(response, .ValidationException, msg, request_alloc);
|
||||
return;
|
||||
};
|
||||
defer json.deinitItem(&item, request_alloc);
|
||||
|
||||
// Find matching brace for Item value
|
||||
var brace_count: i32 = 0;
|
||||
var item_json_start: usize = 0;
|
||||
var item_json_end: usize = 0;
|
||||
|
||||
for (request.body[item_start..], 0..) |char, i| {
|
||||
if (char == '{') {
|
||||
if (brace_count == 0) item_json_start = item_start + i;
|
||||
brace_count += 1;
|
||||
} else if (char == '}') {
|
||||
brace_count -= 1;
|
||||
if (brace_count == 0) {
|
||||
item_json_end = item_start + i + 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (item_json_start == 0 or item_json_end == 0) {
|
||||
_ = self.errorResponse(response, .ValidationException, "Invalid Item format");
|
||||
return;
|
||||
}
|
||||
|
||||
const item_json = request.body[item_json_start..item_json_end];
|
||||
|
||||
self.engine.putItem(table_name, item_json) catch |err| {
|
||||
self.engine.putItem(table_name, item) catch |err| {
|
||||
switch (err) {
|
||||
storage.StorageError.TableNotFound => {
|
||||
_ = self.errorResponse(response, .ResourceNotFoundException, "Table not found");
|
||||
},
|
||||
else => {
|
||||
_ = self.errorResponse(response, .InternalServerError, "Failed to put item");
|
||||
},
|
||||
storage.StorageError.TableNotFound => _ = self.errorResponse(response, .ResourceNotFoundException, "Table not found", request_alloc),
|
||||
storage.StorageError.MissingKeyAttribute => _ = self.errorResponse(response, .ValidationException, "Item missing required key attribute", request_alloc),
|
||||
storage.StorageError.InvalidKey => _ = self.errorResponse(response, .ValidationException, "Invalid key format", request_alloc),
|
||||
else => _ = self.errorResponse(response, .InternalServerError, "Failed to put item", request_alloc),
|
||||
}
|
||||
return;
|
||||
};
|
||||
@@ -227,97 +349,69 @@ pub const ApiHandler = struct {
|
||||
response.setBody("{}") catch {};
|
||||
}
|
||||
|
||||
fn handleGetItem(self: *Self, request: *const http.Request, response: *http.Response) void {
|
||||
const table_name = extractJsonString(request.body, "TableName") orelse {
|
||||
_ = self.errorResponse(response, .ValidationException, "Missing TableName");
|
||||
fn handleGetItem(self: *Self, request: *const http.Request, response: *http.Response, request_alloc: std.mem.Allocator) void {
|
||||
const table_name = json.parseTableName(request_alloc, request.body) catch {
|
||||
_ = self.errorResponse(response, .ValidationException, "Invalid request or missing TableName", request_alloc);
|
||||
return;
|
||||
};
|
||||
|
||||
// Extract Key from request
|
||||
const key_start = std.mem.indexOf(u8, request.body, "\"Key\":") orelse {
|
||||
_ = self.errorResponse(response, .ValidationException, "Missing Key");
|
||||
var key = json.parseKeyFromRequest(request_alloc, request.body) catch |err| {
|
||||
const msg = switch (err) {
|
||||
error.MissingKey => "Missing Key field",
|
||||
error.InvalidRequest => "Invalid request format",
|
||||
else => "Invalid Key format",
|
||||
};
|
||||
_ = self.errorResponse(response, .ValidationException, msg, request_alloc);
|
||||
return;
|
||||
};
|
||||
defer json.deinitItem(&key, request_alloc);
|
||||
|
||||
var brace_count: i32 = 0;
|
||||
var key_json_start: usize = 0;
|
||||
var key_json_end: usize = 0;
|
||||
|
||||
for (request.body[key_start..], 0..) |char, i| {
|
||||
if (char == '{') {
|
||||
if (brace_count == 0) key_json_start = key_start + i;
|
||||
brace_count += 1;
|
||||
} else if (char == '}') {
|
||||
brace_count -= 1;
|
||||
if (brace_count == 0) {
|
||||
key_json_end = key_start + i + 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const key_json = request.body[key_json_start..key_json_end];
|
||||
|
||||
const item = self.engine.getItem(table_name, key_json) catch |err| {
|
||||
const item = self.engine.getItem(table_name, key) catch |err| {
|
||||
switch (err) {
|
||||
storage.StorageError.TableNotFound => {
|
||||
_ = self.errorResponse(response, .ResourceNotFoundException, "Table not found");
|
||||
},
|
||||
else => {
|
||||
_ = self.errorResponse(response, .InternalServerError, "Failed to get item");
|
||||
},
|
||||
storage.StorageError.TableNotFound => _ = self.errorResponse(response, .ResourceNotFoundException, "Table not found", request_alloc),
|
||||
storage.StorageError.MissingKeyAttribute => _ = self.errorResponse(response, .ValidationException, "Key missing required attributes", request_alloc),
|
||||
storage.StorageError.InvalidKey => _ = self.errorResponse(response, .ValidationException, "Invalid key format", request_alloc),
|
||||
else => _ = self.errorResponse(response, .InternalServerError, "Failed to get item", request_alloc),
|
||||
}
|
||||
return;
|
||||
};
|
||||
|
||||
if (item) |i| {
|
||||
defer self.allocator.free(i);
|
||||
const resp = std.fmt.allocPrint(self.allocator, "{{\"Item\":{s}}}", .{i}) catch return;
|
||||
defer self.allocator.free(resp);
|
||||
defer json.deinitItem(&i, self.main_allocator);
|
||||
const item_json = json.serializeItem(request_alloc, i) catch {
|
||||
_ = self.errorResponse(response, .InternalServerError, "Failed to serialize item", request_alloc);
|
||||
return;
|
||||
};
|
||||
const resp = std.fmt.allocPrint(request_alloc, "{{\"Item\":{s}}}", .{item_json}) catch return;
|
||||
response.setBody(resp) catch {};
|
||||
} else {
|
||||
response.setBody("{}") catch {};
|
||||
}
|
||||
}
|
||||
|
||||
fn handleDeleteItem(self: *Self, request: *const http.Request, response: *http.Response) void {
|
||||
const table_name = extractJsonString(request.body, "TableName") orelse {
|
||||
_ = self.errorResponse(response, .ValidationException, "Missing TableName");
|
||||
fn handleDeleteItem(self: *Self, request: *const http.Request, response: *http.Response, request_alloc: std.mem.Allocator) void {
|
||||
const table_name = json.parseTableName(request_alloc, request.body) catch {
|
||||
_ = self.errorResponse(response, .ValidationException, "Invalid request or missing TableName", request_alloc);
|
||||
return;
|
||||
};
|
||||
|
||||
const key_start = std.mem.indexOf(u8, request.body, "\"Key\":") orelse {
|
||||
_ = self.errorResponse(response, .ValidationException, "Missing Key");
|
||||
var key = json.parseKeyFromRequest(request_alloc, request.body) catch |err| {
|
||||
const msg = switch (err) {
|
||||
error.MissingKey => "Missing Key field",
|
||||
error.InvalidRequest => "Invalid request format",
|
||||
else => "Invalid Key format",
|
||||
};
|
||||
_ = self.errorResponse(response, .ValidationException, msg, request_alloc);
|
||||
return;
|
||||
};
|
||||
defer json.deinitItem(&key, request_alloc);
|
||||
|
||||
var brace_count: i32 = 0;
|
||||
var key_json_start: usize = 0;
|
||||
var key_json_end: usize = 0;
|
||||
|
||||
for (request.body[key_start..], 0..) |char, i| {
|
||||
if (char == '{') {
|
||||
if (brace_count == 0) key_json_start = key_start + i;
|
||||
brace_count += 1;
|
||||
} else if (char == '}') {
|
||||
brace_count -= 1;
|
||||
if (brace_count == 0) {
|
||||
key_json_end = key_start + i + 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const key_json = request.body[key_json_start..key_json_end];
|
||||
|
||||
self.engine.deleteItem(table_name, key_json) catch |err| {
|
||||
self.engine.deleteItem(table_name, key) catch |err| {
|
||||
switch (err) {
|
||||
storage.StorageError.TableNotFound => {
|
||||
_ = self.errorResponse(response, .ResourceNotFoundException, "Table not found");
|
||||
},
|
||||
else => {
|
||||
_ = self.errorResponse(response, .InternalServerError, "Failed to delete item");
|
||||
},
|
||||
storage.StorageError.TableNotFound => _ = self.errorResponse(response, .ResourceNotFoundException, "Table not found", request_alloc),
|
||||
storage.StorageError.MissingKeyAttribute => _ = self.errorResponse(response, .ValidationException, "Key missing required attributes", request_alloc),
|
||||
storage.StorageError.InvalidKey => _ = self.errorResponse(response, .ValidationException, "Invalid key format", request_alloc),
|
||||
else => _ = self.errorResponse(response, .InternalServerError, "Failed to delete item", request_alloc),
|
||||
}
|
||||
return;
|
||||
};
|
||||
@@ -325,76 +419,251 @@ pub const ApiHandler = struct {
|
||||
response.setBody("{}") catch {};
|
||||
}
|
||||
|
||||
fn handleQuery(self: *Self, request: *const http.Request, response: *http.Response) void {
|
||||
const table_name = extractJsonString(request.body, "TableName") orelse {
|
||||
_ = self.errorResponse(response, .ValidationException, "Missing TableName");
|
||||
// =========================================================================
|
||||
// FIX B: handleQuery previously had a use-after-free bug.
|
||||
//
|
||||
// The old code did:
|
||||
// const pk_value = if (key_condition) |*kc| blk: {
|
||||
// defer kc.deinit(request_alloc); // frees kc.pk_value's backing memory
|
||||
// break :blk kc.getPkBytes() catch ... // returns borrowed slice into kc.pk_value
|
||||
// };
|
||||
// engine.query(table_name, pk_value, ...) // pk_value is dangling!
|
||||
//
|
||||
// getPkBytes() returns a borrowed pointer into kc.pk_value (e.g. the .S
|
||||
// slice). But kc.deinit() frees that memory via deinitAttributeValue.
|
||||
// So by the time we call engine.query(), pk_value points to freed memory.
|
||||
//
|
||||
// Fix: dupe the pk bytes into request_alloc before deiniting the
|
||||
// key_condition, so pk_value survives for the engine.query() call.
|
||||
// =========================================================================
|
||||
fn handleQuery(self: *Self, request: *const http.Request, response: *http.Response, request_alloc: std.mem.Allocator) void {
|
||||
const table_name = json.parseTableName(request_alloc, request.body) catch {
|
||||
_ = self.errorResponse(response, .ValidationException, "Invalid request or missing TableName", request_alloc);
|
||||
return;
|
||||
};
|
||||
|
||||
// Simplified: extract partition key from KeyConditionExpression
|
||||
// In production, would need full expression parsing
|
||||
const pk_value = extractJsonString(request.body, ":pk") orelse "default";
|
||||
|
||||
const items = self.engine.query(table_name, pk_value, null) catch |err| {
|
||||
// Get table metadata for key schema and attribute definitions
|
||||
var metadata = self.engine.getTableMetadata(table_name) catch |err| {
|
||||
switch (err) {
|
||||
storage.StorageError.TableNotFound => {
|
||||
_ = self.errorResponse(response, .ResourceNotFoundException, "Table not found");
|
||||
},
|
||||
else => {
|
||||
_ = self.errorResponse(response, .InternalServerError, "Query failed");
|
||||
},
|
||||
storage.StorageError.TableNotFound => _ = self.errorResponse(response, .ResourceNotFoundException, "Table not found", request_alloc),
|
||||
else => _ = self.errorResponse(response, .InternalServerError, "Failed to access table", request_alloc),
|
||||
}
|
||||
return;
|
||||
};
|
||||
defer {
|
||||
for (items) |item| self.allocator.free(item);
|
||||
self.allocator.free(items);
|
||||
}
|
||||
defer metadata.deinit(self.main_allocator);
|
||||
|
||||
self.writeItemsResponse(response, items);
|
||||
}
|
||||
|
||||
fn handleScan(self: *Self, request: *const http.Request, response: *http.Response) void {
|
||||
const table_name = extractJsonString(request.body, "TableName") orelse {
|
||||
_ = self.errorResponse(response, .ValidationException, "Missing TableName");
|
||||
// Parse KeyConditionExpression properly
|
||||
var key_condition = expression.parseQueryKeyCondition(request_alloc, request.body) catch |err| {
|
||||
const msg = switch (err) {
|
||||
error.InvalidExpression => "Invalid KeyConditionExpression",
|
||||
error.MissingAttributeValue => "Missing value in ExpressionAttributeValues",
|
||||
else => "Failed to parse KeyConditionExpression",
|
||||
};
|
||||
_ = self.errorResponse(response, .ValidationException, msg, request_alloc);
|
||||
return;
|
||||
};
|
||||
|
||||
const items = self.engine.scan(table_name, null) catch |err| {
|
||||
// FIX B: Extract pk bytes and dupe them BEFORE deiniting key_condition.
|
||||
// getPkBytes() returns a borrowed slice into kc.pk_value; we must copy
|
||||
// it into request_alloc so it survives past kc.deinit().
|
||||
const pk_value = if (key_condition) |*kc| blk: {
|
||||
defer kc.deinit(request_alloc);
|
||||
|
||||
const borrowed_pk = kc.getPkBytes() catch {
|
||||
_ = self.errorResponse(response, .ValidationException, "Invalid partition key type", request_alloc);
|
||||
return;
|
||||
};
|
||||
|
||||
// Dupe into request_alloc so it outlives kc.deinit()
|
||||
break :blk request_alloc.dupe(u8, borrowed_pk) catch {
|
||||
_ = self.errorResponse(response, .InternalServerError, "Allocation failed", request_alloc);
|
||||
return;
|
||||
};
|
||||
} else {
|
||||
_ = self.errorResponse(response, .ValidationException, "Missing KeyConditionExpression", request_alloc);
|
||||
return;
|
||||
};
|
||||
// pk_value is now owned by request_alloc — safe to use until arena dies
|
||||
|
||||
const limit = json.parseLimit(request_alloc, request.body) catch null;
|
||||
|
||||
// Parse ExclusiveStartKey with proper type handling
|
||||
var start_key_opt = json.parseExclusiveStartKey(request_alloc, request.body, metadata.key_schema) catch |err| {
|
||||
const msg = switch (err) {
|
||||
error.MissingKeyAttribute => "ExclusiveStartKey missing required attributes",
|
||||
error.InvalidKeyType => "ExclusiveStartKey has invalid key type",
|
||||
else => "Invalid ExclusiveStartKey format",
|
||||
};
|
||||
_ = self.errorResponse(response, .ValidationException, msg, request_alloc);
|
||||
return;
|
||||
};
|
||||
defer if (start_key_opt) |*key| key.deinit(request_alloc);
|
||||
|
||||
var start_key_binary: ?[]u8 = null;
|
||||
defer if (start_key_binary) |k| request_alloc.free(k);
|
||||
|
||||
if (start_key_opt) |start_key| {
|
||||
const key_values = start_key.getValues() catch {
|
||||
_ = self.errorResponse(response, .ValidationException, "Invalid ExclusiveStartKey", request_alloc);
|
||||
return;
|
||||
};
|
||||
start_key_binary = key_codec.buildDataKey(request_alloc, table_name, key_values.pk, key_values.sk) catch {
|
||||
_ = self.errorResponse(response, .InternalServerError, "Failed to encode start key", request_alloc);
|
||||
return;
|
||||
};
|
||||
}
|
||||
|
||||
var result = self.engine.query(table_name, pk_value, limit, start_key_binary) catch |err| {
|
||||
switch (err) {
|
||||
storage.StorageError.TableNotFound => {
|
||||
_ = self.errorResponse(response, .ResourceNotFoundException, "Table not found");
|
||||
},
|
||||
else => {
|
||||
_ = self.errorResponse(response, .InternalServerError, "Scan failed");
|
||||
},
|
||||
storage.StorageError.TableNotFound => _ = self.errorResponse(response, .ResourceNotFoundException, "Table not found", request_alloc),
|
||||
else => _ = self.errorResponse(response, .InternalServerError, "Query failed", request_alloc),
|
||||
}
|
||||
return;
|
||||
};
|
||||
defer {
|
||||
for (items) |item| self.allocator.free(item);
|
||||
self.allocator.free(items);
|
||||
defer result.deinit(self.main_allocator);
|
||||
|
||||
self.writeItemsResponseWithPagination(response, result.items, result.last_evaluated_key, &metadata, request_alloc);
|
||||
}
|
||||
|
||||
self.writeItemsResponse(response, items);
|
||||
fn handleScan(self: *Self, request: *const http.Request, response: *http.Response, request_alloc: std.mem.Allocator) void {
|
||||
const table_name = json.parseTableName(request_alloc, request.body) catch {
|
||||
_ = self.errorResponse(response, .ValidationException, "Invalid request or missing TableName", request_alloc);
|
||||
return;
|
||||
};
|
||||
|
||||
var metadata = self.engine.getTableMetadata(table_name) catch |err| {
|
||||
switch (err) {
|
||||
storage.StorageError.TableNotFound => _ = self.errorResponse(response, .ResourceNotFoundException, "Table not found", request_alloc),
|
||||
else => _ = self.errorResponse(response, .InternalServerError, "Failed to access table", request_alloc),
|
||||
}
|
||||
return;
|
||||
};
|
||||
defer metadata.deinit(self.main_allocator);
|
||||
|
||||
const limit = json.parseLimit(request_alloc, request.body) catch null;
|
||||
|
||||
var start_key_opt = json.parseExclusiveStartKey(request_alloc, request.body, metadata.key_schema) catch |err| {
|
||||
const msg = switch (err) {
|
||||
error.MissingKeyAttribute => "ExclusiveStartKey missing required attributes",
|
||||
error.InvalidKeyType => "ExclusiveStartKey has invalid key type",
|
||||
else => "Invalid ExclusiveStartKey format",
|
||||
};
|
||||
_ = self.errorResponse(response, .ValidationException, msg, request_alloc);
|
||||
return;
|
||||
};
|
||||
defer if (start_key_opt) |*key| key.deinit(request_alloc);
|
||||
|
||||
var start_key_binary: ?[]u8 = null;
|
||||
defer if (start_key_binary) |k| request_alloc.free(k);
|
||||
|
||||
if (start_key_opt) |start_key| {
|
||||
const key_values = start_key.getValues() catch {
|
||||
_ = self.errorResponse(response, .ValidationException, "Invalid ExclusiveStartKey", request_alloc);
|
||||
return;
|
||||
};
|
||||
start_key_binary = key_codec.buildDataKey(request_alloc, table_name, key_values.pk, key_values.sk) catch {
|
||||
_ = self.errorResponse(response, .InternalServerError, "Failed to encode start key", request_alloc);
|
||||
return;
|
||||
};
|
||||
}
|
||||
|
||||
fn writeItemsResponse(self: *Self, response: *http.Response, items: []const []const u8) void {
|
||||
var buf = std.ArrayList(u8){};
|
||||
defer buf.deinit(self.allocator);
|
||||
const writer = buf.writer(self.allocator);
|
||||
var result = self.engine.scan(table_name, limit, start_key_binary) catch |err| {
|
||||
switch (err) {
|
||||
storage.StorageError.TableNotFound => _ = self.errorResponse(response, .ResourceNotFoundException, "Table not found", request_alloc),
|
||||
else => _ = self.errorResponse(response, .InternalServerError, "Scan failed", request_alloc),
|
||||
}
|
||||
return;
|
||||
};
|
||||
defer result.deinit(self.main_allocator);
|
||||
|
||||
self.writeItemsResponseWithPagination(response, result.items, result.last_evaluated_key, &metadata, request_alloc);
|
||||
}
|
||||
|
||||
fn writeItemsResponseWithPagination(
|
||||
self: *Self,
|
||||
response: *http.Response,
|
||||
items: []const types.Item,
|
||||
last_evaluated_key_binary: ?[]const u8,
|
||||
metadata: *const storage.TableMetadata,
|
||||
request_alloc: std.mem.Allocator,
|
||||
) void {
|
||||
var buf = std.ArrayList(u8).init(request_alloc);
|
||||
defer buf.deinit();
|
||||
const writer = buf.writer();
|
||||
|
||||
writer.writeAll("{\"Items\":[") catch return;
|
||||
for (items, 0..) |item, i| {
|
||||
if (i > 0) writer.writeByte(',') catch return;
|
||||
writer.writeAll(item) catch return;
|
||||
json.serializeItemToWriter(writer, item, request_alloc) catch return;
|
||||
}
|
||||
writer.print("],\"Count\":{d},\"ScannedCount\":{d}}}", .{ items.len, items.len }) catch return;
|
||||
writer.print("],\"Count\":{d},\"ScannedCount\":{d}", .{ items.len, items.len }) catch return;
|
||||
|
||||
if (last_evaluated_key_binary) |binary_key| {
|
||||
var key = self.buildKeyFromBinaryWithTypes(binary_key, metadata, request_alloc) catch {
|
||||
writer.writeAll("}") catch {};
|
||||
response.setBody(buf.items) catch {};
|
||||
return;
|
||||
};
|
||||
defer key.deinit(request_alloc);
|
||||
|
||||
const lek_json = json.serializeLastEvaluatedKey(request_alloc, key, metadata.key_schema) catch {
|
||||
writer.writeAll("}") catch {};
|
||||
response.setBody(buf.items) catch {};
|
||||
return;
|
||||
};
|
||||
|
||||
writer.print(",\"LastEvaluatedKey\":{s}", .{lek_json}) catch {};
|
||||
}
|
||||
|
||||
writer.writeAll("}") catch return;
|
||||
response.setBody(buf.items) catch {};
|
||||
}
|
||||
|
||||
fn errorResponse(self: *Self, response: *http.Response, err_type: types.DynamoDBErrorType, message: []const u8) http.Response {
|
||||
/// Build a Key struct from binary storage key with correct attribute types
|
||||
/// Uses attribute_definitions from metadata to determine S/N/B type
|
||||
/// (Fix C: already uses metadata types — no change needed here)
|
||||
fn buildKeyFromBinaryWithTypes(
|
||||
self: *Self,
|
||||
binary_key: []const u8,
|
||||
metadata: *const storage.TableMetadata,
|
||||
allocator: std.mem.Allocator,
|
||||
) !types.Key {
|
||||
_ = self;
|
||||
var decoder = key_codec.KeyDecoder.init(binary_key);
|
||||
|
||||
// Skip entity type
|
||||
_ = try decoder.readEntityType();
|
||||
// Skip table name
|
||||
_ = try decoder.readSegmentBorrowed();
|
||||
|
||||
// Read partition key bytes
|
||||
const pk_bytes = try decoder.readSegmentBorrowed();
|
||||
|
||||
// Read sort key bytes if present
|
||||
var sk_bytes: ?[]const u8 = null;
|
||||
if (decoder.hasMore()) {
|
||||
sk_bytes = try decoder.readSegmentBorrowed();
|
||||
}
|
||||
|
||||
// Get attribute types from metadata
|
||||
const pk_name = metadata.getPartitionKeyName() orelse return error.InvalidKey;
|
||||
const pk_type = metadata.getAttributeType(pk_name) orelse return error.InvalidKey;
|
||||
|
||||
const pk_attr = try buildAttributeValueWithType(allocator, pk_bytes, pk_type);
|
||||
errdefer json.deinitAttributeValue(&pk_attr, allocator);
|
||||
|
||||
var sk_attr: ?types.AttributeValue = null;
|
||||
if (sk_bytes) |sk| {
|
||||
const sk_name = metadata.getSortKeyName() orelse return error.InvalidKey;
|
||||
const sk_type = metadata.getAttributeType(sk_name) orelse return error.InvalidKey;
|
||||
sk_attr = try buildAttributeValueWithType(allocator, sk, sk_type);
|
||||
}
|
||||
|
||||
return types.Key{ .pk = pk_attr, .sk = sk_attr };
|
||||
}
|
||||
|
||||
fn errorResponse(self: *Self, response: *http.Response, err_type: types.DynamoDBErrorType, message: []const u8, request_alloc: std.mem.Allocator) http.Response {
|
||||
_ = self;
|
||||
response.setStatus(switch (err_type) {
|
||||
.ResourceNotFoundException => .not_found,
|
||||
.ResourceInUseException => .conflict,
|
||||
@@ -402,38 +671,22 @@ pub const ApiHandler = struct {
|
||||
else => .internal_server_error,
|
||||
});
|
||||
|
||||
const body = err_type.toErrorResponse(message, self.allocator) catch return response.*;
|
||||
const body = err_type.toErrorResponse(message, request_alloc) catch return response.*;
|
||||
response.setBody(body) catch {};
|
||||
self.allocator.free(body);
|
||||
return response.*;
|
||||
}
|
||||
};
|
||||
|
||||
fn extractJsonString(json_data: []const u8, key: []const u8) ?[]const u8 {
|
||||
// Search for "key":"value" pattern
|
||||
var search_buf: [256]u8 = undefined;
|
||||
const search = std.fmt.bufPrint(&search_buf, "\"{s}\":\"", .{key}) catch return null;
|
||||
|
||||
const start = std.mem.indexOf(u8, json_data, search) orelse return null;
|
||||
const value_start = start + search.len;
|
||||
const value_end = std.mem.indexOfPos(u8, json_data, value_start, "\"") orelse return null;
|
||||
return json_data[value_start..value_end];
|
||||
}
|
||||
|
||||
// Global handler for use with http.Server
|
||||
var global_handler: ?*ApiHandler = null;
|
||||
|
||||
pub fn setGlobalHandler(handler: *ApiHandler) void {
|
||||
global_handler = handler;
|
||||
}
|
||||
|
||||
pub fn httpHandler(request: *const http.Request, allocator: std.mem.Allocator) http.Response {
|
||||
if (global_handler) |h| {
|
||||
return h.handle(request);
|
||||
}
|
||||
|
||||
var response = http.Response.init(allocator);
|
||||
response.setStatus(.internal_server_error);
|
||||
response.setBody("{\"error\":\"Handler not initialized\"}") catch {};
|
||||
return response;
|
||||
/// Build an AttributeValue with the correct type (S, N, or B) from raw bytes
|
||||
fn buildAttributeValueWithType(
|
||||
allocator: std.mem.Allocator,
|
||||
bytes: []const u8,
|
||||
attr_type: types.ScalarAttributeType,
|
||||
) !types.AttributeValue {
|
||||
const owned = try allocator.dupe(u8, bytes);
|
||||
return switch (attr_type) {
|
||||
.S => types.AttributeValue{ .S = owned },
|
||||
.N => types.AttributeValue{ .N = owned },
|
||||
.B => types.AttributeValue{ .B = owned },
|
||||
};
|
||||
}
|
||||
|
||||
598
src/dynamodb/json.zig
Normal file
598
src/dynamodb/json.zig
Normal file
@@ -0,0 +1,598 @@
|
||||
/// DynamoDB JSON parsing and serialization
|
||||
/// Pure functions for converting between DynamoDB JSON format and internal types
|
||||
const std = @import("std");
|
||||
const types = @import("types.zig");
|
||||
|
||||
// ============================================================================
|
||||
// Parsing (JSON → Types)
|
||||
// ============================================================================
|
||||
|
||||
/// Parse DynamoDB JSON format into an Item
|
||||
/// Caller owns returned Item and must call deinitItem() when done
|
||||
pub fn parseItem(allocator: std.mem.Allocator, json_bytes: []const u8) !types.Item {
|
||||
const parsed = try std.json.parseFromSlice(std.json.Value, allocator, json_bytes, .{});
|
||||
defer parsed.deinit();
|
||||
|
||||
return try parseItemFromValue(allocator, parsed.value);
|
||||
}
|
||||
|
||||
/// Parse an Item from an already-parsed JSON Value
|
||||
/// This is more efficient when you already have a Value (e.g., from request body parsing)
|
||||
/// Caller owns returned Item and must call deinitItem() when done
|
||||
pub fn parseItemFromValue(allocator: std.mem.Allocator, value: std.json.Value) !types.Item {
|
||||
const obj = switch (value) {
|
||||
.object => |o| o,
|
||||
else => return error.InvalidItemFormat,
|
||||
};
|
||||
|
||||
var item = types.Item.init(allocator);
|
||||
errdefer deinitItem(&item, allocator);
|
||||
|
||||
var iter = obj.iterator();
|
||||
while (iter.next()) |entry| {
|
||||
const attr_name = try allocator.dupe(u8, entry.key_ptr.*);
|
||||
errdefer allocator.free(attr_name);
|
||||
|
||||
var attr_value = try parseAttributeValue(allocator, entry.value_ptr.*);
|
||||
errdefer deinitAttributeValue(&attr_value, allocator);
|
||||
|
||||
try item.put(attr_name, attr_value);
|
||||
}
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
/// Parse a single DynamoDB AttributeValue from JSON
|
||||
/// Format: {"S": "value"}, {"N": "123"}, {"M": {...}}, etc.
|
||||
pub fn parseAttributeValue(allocator: std.mem.Allocator, value: std.json.Value) error{ InvalidAttributeFormat, InvalidStringAttribute, InvalidNumberAttribute, InvalidBinaryAttribute, InvalidBoolAttribute, InvalidNullAttribute, InvalidStringSetAttribute, InvalidNumberSetAttribute, InvalidBinarySetAttribute, InvalidListAttribute, InvalidMapAttribute, UnknownAttributeType, OutOfMemory }!types.AttributeValue {
|
||||
const obj = switch (value) {
|
||||
.object => |o| o,
|
||||
else => return error.InvalidAttributeFormat,
|
||||
};
|
||||
|
||||
// DynamoDB attribute must have exactly one key (the type indicator)
|
||||
if (obj.count() != 1) return error.InvalidAttributeFormat;
|
||||
|
||||
var iter = obj.iterator();
|
||||
const entry = iter.next() orelse return error.InvalidAttributeFormat;
|
||||
|
||||
const type_name = entry.key_ptr.*;
|
||||
const type_value = entry.value_ptr.*;
|
||||
|
||||
// String
|
||||
if (std.mem.eql(u8, type_name, "S")) {
|
||||
const str = switch (type_value) {
|
||||
.string => |s| s,
|
||||
else => return error.InvalidStringAttribute,
|
||||
};
|
||||
return types.AttributeValue{ .S = try allocator.dupe(u8, str) };
|
||||
}
|
||||
|
||||
// Number (stored as string)
|
||||
if (std.mem.eql(u8, type_name, "N")) {
|
||||
const str = switch (type_value) {
|
||||
.string => |s| s,
|
||||
else => return error.InvalidNumberAttribute,
|
||||
};
|
||||
return types.AttributeValue{ .N = try allocator.dupe(u8, str) };
|
||||
}
|
||||
|
||||
// Binary (base64 string)
|
||||
if (std.mem.eql(u8, type_name, "B")) {
|
||||
const str = switch (type_value) {
|
||||
.string => |s| s,
|
||||
else => return error.InvalidBinaryAttribute,
|
||||
};
|
||||
return types.AttributeValue{ .B = try allocator.dupe(u8, str) };
|
||||
}
|
||||
|
||||
// Boolean
|
||||
if (std.mem.eql(u8, type_name, "BOOL")) {
|
||||
const b = switch (type_value) {
|
||||
.bool => |b_val| b_val,
|
||||
else => return error.InvalidBoolAttribute,
|
||||
};
|
||||
return types.AttributeValue{ .BOOL = b };
|
||||
}
|
||||
|
||||
// Null
|
||||
if (std.mem.eql(u8, type_name, "NULL")) {
|
||||
const n = switch (type_value) {
|
||||
.bool => |b| b,
|
||||
else => return error.InvalidNullAttribute,
|
||||
};
|
||||
return types.AttributeValue{ .NULL = n };
|
||||
}
|
||||
|
||||
// String Set
|
||||
if (std.mem.eql(u8, type_name, "SS")) {
|
||||
const arr = switch (type_value) {
|
||||
.array => |a| a,
|
||||
else => return error.InvalidStringSetAttribute,
|
||||
};
|
||||
|
||||
var strings = try allocator.alloc([]const u8, arr.items.len);
|
||||
errdefer allocator.free(strings);
|
||||
|
||||
for (arr.items, 0..) |item, i| {
|
||||
const str = switch (item) {
|
||||
.string => |s| s,
|
||||
else => {
|
||||
// Cleanup already allocated strings
|
||||
for (strings[0..i]) |s| allocator.free(s);
|
||||
return error.InvalidStringSetAttribute;
|
||||
},
|
||||
};
|
||||
strings[i] = try allocator.dupe(u8, str);
|
||||
}
|
||||
return types.AttributeValue{ .SS = strings };
|
||||
}
|
||||
|
||||
// Number Set
|
||||
if (std.mem.eql(u8, type_name, "NS")) {
|
||||
const arr = switch (type_value) {
|
||||
.array => |a| a,
|
||||
else => return error.InvalidNumberSetAttribute,
|
||||
};
|
||||
|
||||
var numbers = try allocator.alloc([]const u8, arr.items.len);
|
||||
errdefer allocator.free(numbers);
|
||||
|
||||
for (arr.items, 0..) |item, i| {
|
||||
const str = switch (item) {
|
||||
.string => |s| s,
|
||||
else => {
|
||||
for (numbers[0..i]) |n| allocator.free(n);
|
||||
return error.InvalidNumberSetAttribute;
|
||||
},
|
||||
};
|
||||
numbers[i] = try allocator.dupe(u8, str);
|
||||
}
|
||||
return types.AttributeValue{ .NS = numbers };
|
||||
}
|
||||
|
||||
// Binary Set
|
||||
if (std.mem.eql(u8, type_name, "BS")) {
|
||||
const arr = switch (type_value) {
|
||||
.array => |a| a,
|
||||
else => return error.InvalidBinarySetAttribute,
|
||||
};
|
||||
|
||||
var binaries = try allocator.alloc([]const u8, arr.items.len);
|
||||
errdefer allocator.free(binaries);
|
||||
|
||||
for (arr.items, 0..) |item, i| {
|
||||
const str = switch (item) {
|
||||
.string => |s| s,
|
||||
else => {
|
||||
for (binaries[0..i]) |b| allocator.free(b);
|
||||
return error.InvalidBinarySetAttribute;
|
||||
},
|
||||
};
|
||||
binaries[i] = try allocator.dupe(u8, str);
|
||||
}
|
||||
return types.AttributeValue{ .BS = binaries };
|
||||
}
|
||||
|
||||
// List (recursive)
|
||||
if (std.mem.eql(u8, type_name, "L")) {
|
||||
const arr = switch (type_value) {
|
||||
.array => |a| a,
|
||||
else => return error.InvalidListAttribute,
|
||||
};
|
||||
|
||||
var list = try allocator.alloc(types.AttributeValue, arr.items.len);
|
||||
errdefer {
|
||||
for (list[0..arr.items.len]) |*item| {
|
||||
deinitAttributeValue(item, allocator);
|
||||
}
|
||||
allocator.free(list);
|
||||
}
|
||||
|
||||
for (arr.items, 0..) |item, i| {
|
||||
list[i] = try parseAttributeValue(allocator, item);
|
||||
}
|
||||
return types.AttributeValue{ .L = list };
|
||||
}
|
||||
|
||||
// Map (recursive)
|
||||
if (std.mem.eql(u8, type_name, "M")) {
|
||||
const obj_val = switch (type_value) {
|
||||
.object => |o| o,
|
||||
else => return error.InvalidMapAttribute,
|
||||
};
|
||||
|
||||
var map = std.StringHashMap(types.AttributeValue).init(allocator);
|
||||
errdefer {
|
||||
var map_iter = map.iterator();
|
||||
while (map_iter.next()) |map_entry| {
|
||||
allocator.free(map_entry.key_ptr.*);
|
||||
deinitAttributeValue(map_entry.value_ptr, allocator);
|
||||
}
|
||||
map.deinit();
|
||||
}
|
||||
|
||||
var map_iter = obj_val.iterator();
|
||||
while (map_iter.next()) |map_entry| {
|
||||
const key = try allocator.dupe(u8, map_entry.key_ptr.*);
|
||||
errdefer allocator.free(key);
|
||||
|
||||
var val = try parseAttributeValue(allocator, map_entry.value_ptr.*);
|
||||
errdefer deinitAttributeValue(&val, allocator);
|
||||
|
||||
try map.put(key, val);
|
||||
}
|
||||
|
||||
return types.AttributeValue{ .M = map };
|
||||
}
|
||||
|
||||
return error.UnknownAttributeType;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Serialization (Types → JSON)
|
||||
// ============================================================================
|
||||
|
||||
/// Serialize an Item to canonical DynamoDB JSON format
|
||||
/// Keys are sorted alphabetically for deterministic output
|
||||
/// Caller owns returned slice and must free it
|
||||
pub fn serializeItem(allocator: std.mem.Allocator, item: types.Item) ![]u8 {
|
||||
var buf = std.ArrayList(u8).init(allocator);
|
||||
errdefer buf.deinit();
|
||||
const writer = buf.writer();
|
||||
|
||||
try serializeItemToWriter(writer, item, allocator);
|
||||
|
||||
return buf.toOwnedSlice();
|
||||
}
|
||||
|
||||
/// Serialize an Item to a writer with deterministic ordering
|
||||
/// Uses the provided allocator for temporary key sorting (not page_allocator)
|
||||
pub fn serializeItemToWriter(writer: anytype, item: types.Item, allocator: std.mem.Allocator) !void {
|
||||
// Collect and sort keys for deterministic output
|
||||
var keys = std.ArrayList([]const u8).init(allocator);
|
||||
defer keys.deinit();
|
||||
|
||||
var iter = item.iterator();
|
||||
while (iter.next()) |entry| {
|
||||
try keys.append(entry.key_ptr.*);
|
||||
}
|
||||
|
||||
// Sort keys alphabetically
|
||||
std.mem.sort([]const u8, keys.items, {}, struct {
|
||||
fn lessThan(_: void, a: []const u8, b: []const u8) bool {
|
||||
return std.mem.lessThan(u8, a, b);
|
||||
}
|
||||
}.lessThan);
|
||||
|
||||
try writer.writeByte('{');
|
||||
for (keys.items, 0..) |key, i| {
|
||||
if (i > 0) try writer.writeByte(',');
|
||||
try writer.print("\"{s}\":", .{key});
|
||||
const value = item.get(key).?;
|
||||
try serializeAttributeValue(writer, value, allocator);
|
||||
}
|
||||
try writer.writeByte('}');
|
||||
}
|
||||
|
||||
/// Serialize an AttributeValue to DynamoDB JSON format
|
||||
pub fn serializeAttributeValue(writer: anytype, attr: types.AttributeValue, allocator: std.mem.Allocator) !void {
|
||||
switch (attr) {
|
||||
.S => |s| try writer.print("{{\"S\":\"{s}\"}}", .{s}),
|
||||
.N => |n| try writer.print("{{\"N\":\"{s}\"}}", .{n}),
|
||||
.B => |b| try writer.print("{{\"B\":\"{s}\"}}", .{b}),
|
||||
.BOOL => |b| try writer.print("{{\"BOOL\":{}}}", .{b}),
|
||||
.NULL => try writer.writeAll("{\"NULL\":true}"),
|
||||
.SS => |ss| {
|
||||
try writer.writeAll("{\"SS\":[");
|
||||
for (ss, 0..) |s, i| {
|
||||
if (i > 0) try writer.writeByte(',');
|
||||
try writer.print("\"{s}\"", .{s});
|
||||
}
|
||||
try writer.writeAll("]}");
|
||||
},
|
||||
.NS => |ns| {
|
||||
try writer.writeAll("{\"NS\":[");
|
||||
for (ns, 0..) |n, i| {
|
||||
if (i > 0) try writer.writeByte(',');
|
||||
try writer.print("\"{s}\"", .{n});
|
||||
}
|
||||
try writer.writeAll("]}");
|
||||
},
|
||||
.BS => |bs| {
|
||||
try writer.writeAll("{\"BS\":[");
|
||||
for (bs, 0..) |b, i| {
|
||||
if (i > 0) try writer.writeByte(',');
|
||||
try writer.print("\"{s}\"", .{b});
|
||||
}
|
||||
try writer.writeAll("]}");
|
||||
},
|
||||
.L => |list| {
|
||||
try writer.writeAll("{\"L\":[");
|
||||
for (list, 0..) |item, i| {
|
||||
if (i > 0) try writer.writeByte(',');
|
||||
try serializeAttributeValue(writer, item, allocator);
|
||||
}
|
||||
try writer.writeAll("]}");
|
||||
},
|
||||
.M => |map| {
|
||||
try writer.writeAll("{\"M\":{");
|
||||
|
||||
// Collect and sort keys for deterministic output - use provided allocator
|
||||
var keys = std.ArrayList([]const u8).init(allocator);
|
||||
defer keys.deinit();
|
||||
|
||||
var iter = map.iterator();
|
||||
while (iter.next()) |entry| {
|
||||
try keys.append(entry.key_ptr.*);
|
||||
}
|
||||
|
||||
std.mem.sort([]const u8, keys.items, {}, struct {
|
||||
fn lessThan(_: void, a: []const u8, b: []const u8) bool {
|
||||
return std.mem.lessThan(u8, a, b);
|
||||
}
|
||||
}.lessThan);
|
||||
|
||||
for (keys.items, 0..) |key, i| {
|
||||
if (i > 0) try writer.writeByte(',');
|
||||
try writer.print("\"{s}\":", .{key});
|
||||
const value = map.get(key).?;
|
||||
try serializeAttributeValue(writer, value, allocator);
|
||||
}
|
||||
try writer.writeAll("}}");
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Request Parsing Helpers
|
||||
// ============================================================================
|
||||
|
||||
/// Extract table name from request body
|
||||
pub fn parseTableName(allocator: std.mem.Allocator, request_body: []const u8) ![]const u8 {
|
||||
const parsed = try std.json.parseFromSlice(std.json.Value, allocator, request_body, .{});
|
||||
defer parsed.deinit();
|
||||
|
||||
const root = switch (parsed.value) {
|
||||
.object => |o| o,
|
||||
else => return error.InvalidRequest,
|
||||
};
|
||||
|
||||
const table_name_val = root.get("TableName") orelse return error.MissingTableName;
|
||||
const table_name = switch (table_name_val) {
|
||||
.string => |s| s,
|
||||
else => return error.InvalidTableName,
|
||||
};
|
||||
|
||||
return table_name;
|
||||
}
|
||||
|
||||
/// Parse Item field from request body
|
||||
/// Returns owned Item - caller must call deinitItem()
|
||||
pub fn parseItemFromRequest(allocator: std.mem.Allocator, request_body: []const u8) !types.Item {
|
||||
const parsed = try std.json.parseFromSlice(std.json.Value, allocator, request_body, .{});
|
||||
defer parsed.deinit();
|
||||
|
||||
const root = switch (parsed.value) {
|
||||
.object => |o| o,
|
||||
else => return error.InvalidRequest,
|
||||
};
|
||||
|
||||
const item_val = root.get("Item") orelse return error.MissingItem;
|
||||
return try parseItemFromValue(allocator, item_val);
|
||||
}
|
||||
|
||||
/// Parse Key field from request body
|
||||
/// Returns owned Item representing the key - caller must call deinitItem()
|
||||
pub fn parseKeyFromRequest(allocator: std.mem.Allocator, request_body: []const u8) !types.Item {
|
||||
const parsed = try std.json.parseFromSlice(std.json.Value, allocator, request_body, .{});
|
||||
defer parsed.deinit();
|
||||
|
||||
const root = switch (parsed.value) {
|
||||
.object => |o| o,
|
||||
else => return error.InvalidRequest,
|
||||
};
|
||||
|
||||
const key_val = root.get("Key") orelse return error.MissingKey;
|
||||
return try parseItemFromValue(allocator, key_val);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Pagination Helpers
|
||||
// ============================================================================
|
||||
|
||||
/// Parse ExclusiveStartKey from request body
|
||||
/// Returns null if not present, owned Key if present
|
||||
/// Caller must call key.deinit() when done
|
||||
pub fn parseExclusiveStartKey(
|
||||
allocator: std.mem.Allocator,
|
||||
request_body: []const u8,
|
||||
key_schema: []const types.KeySchemaElement,
|
||||
) !?types.Key {
|
||||
const parsed = try std.json.parseFromSlice(std.json.Value, allocator, request_body, .{});
|
||||
defer parsed.deinit();
|
||||
|
||||
const root = switch (parsed.value) {
|
||||
.object => |o| o,
|
||||
else => return error.InvalidRequest,
|
||||
};
|
||||
|
||||
const key_val = root.get("ExclusiveStartKey") orelse return null;
|
||||
|
||||
// Parse as Item first, then convert to Key
|
||||
var key_item = try parseItemFromValue(allocator, key_val);
|
||||
defer deinitItem(&key_item, allocator);
|
||||
|
||||
// Validate and extract key using Key.fromItem
|
||||
return try types.Key.fromItem(allocator, key_item, key_schema);
|
||||
}
|
||||
|
||||
/// Parse Limit from request body
|
||||
/// Returns null if not present
|
||||
pub fn parseLimit(allocator: std.mem.Allocator, request_body: []const u8) !?usize {
|
||||
const parsed = try std.json.parseFromSlice(std.json.Value, allocator, request_body, .{});
|
||||
defer parsed.deinit();
|
||||
|
||||
const root = switch (parsed.value) {
|
||||
.object => |o| o,
|
||||
else => return error.InvalidRequest,
|
||||
};
|
||||
|
||||
const limit_val = root.get("Limit") orelse return null;
|
||||
const limit_int = switch (limit_val) {
|
||||
.integer => |i| i,
|
||||
else => return error.InvalidLimit,
|
||||
};
|
||||
|
||||
if (limit_int < 0) return error.InvalidLimit;
|
||||
return @intCast(limit_int);
|
||||
}
|
||||
|
||||
/// Serialize a Key as LastEvaluatedKey in DynamoDB JSON format
|
||||
/// Caller owns returned slice and must free it
|
||||
pub fn serializeLastEvaluatedKey(
|
||||
allocator: std.mem.Allocator,
|
||||
key: types.Key,
|
||||
key_schema: []const types.KeySchemaElement,
|
||||
) ![]u8 {
|
||||
var key_item = try key.toItem(allocator, key_schema);
|
||||
defer deinitItem(&key_item, allocator);
|
||||
|
||||
return try serializeItem(allocator, key_item);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Storage Helpers
|
||||
// ============================================================================
|
||||
|
||||
/// Extract just the key attributes from an item based on key schema
|
||||
/// Returns a new Item containing only the key attributes (deep copied)
|
||||
/// Caller owns returned Item and must call deinitItem() when done
|
||||
/// DEPRECATED: Use types.Key.fromItem() instead
|
||||
pub fn extractKeyAttributes(
|
||||
allocator: std.mem.Allocator,
|
||||
item: types.Item,
|
||||
key_schema: []const types.KeySchemaElement,
|
||||
) !types.Item {
|
||||
var key = types.Item.init(allocator);
|
||||
errdefer deinitItem(&key, allocator);
|
||||
|
||||
for (key_schema) |schema_element| {
|
||||
const attr_value = item.get(schema_element.attribute_name) orelse
|
||||
return error.MissingKeyAttribute;
|
||||
|
||||
const attr_name = try allocator.dupe(u8, schema_element.attribute_name);
|
||||
errdefer allocator.free(attr_name);
|
||||
|
||||
// Deep copy the attribute value
|
||||
var copied_value = try deepCopyAttributeValue(allocator, attr_value);
|
||||
errdefer deinitAttributeValue(&copied_value, allocator);
|
||||
|
||||
try key.put(attr_name, copied_value);
|
||||
}
|
||||
|
||||
return key;
|
||||
}
|
||||
|
||||
/// Deep copy an AttributeValue
|
||||
/// Made public for use by the Key struct and other modules
|
||||
pub fn deepCopyAttributeValue(allocator: std.mem.Allocator, attr: types.AttributeValue) !types.AttributeValue {
|
||||
return switch (attr) {
|
||||
.S => |s| types.AttributeValue{ .S = try allocator.dupe(u8, s) },
|
||||
.N => |n| types.AttributeValue{ .N = try allocator.dupe(u8, n) },
|
||||
.B => |b| types.AttributeValue{ .B = try allocator.dupe(u8, b) },
|
||||
.BOOL => |b| types.AttributeValue{ .BOOL = b },
|
||||
.NULL => |n| types.AttributeValue{ .NULL = n },
|
||||
.SS => |ss| blk: {
|
||||
var copy = try allocator.alloc([]const u8, ss.len);
|
||||
errdefer allocator.free(copy);
|
||||
for (ss, 0..) |s, i| {
|
||||
copy[i] = try allocator.dupe(u8, s);
|
||||
}
|
||||
break :blk types.AttributeValue{ .SS = copy };
|
||||
},
|
||||
.NS => |ns| blk: {
|
||||
var copy = try allocator.alloc([]const u8, ns.len);
|
||||
errdefer allocator.free(copy);
|
||||
for (ns, 0..) |n, i| {
|
||||
copy[i] = try allocator.dupe(u8, n);
|
||||
}
|
||||
break :blk types.AttributeValue{ .NS = copy };
|
||||
},
|
||||
.BS => |bs| blk: {
|
||||
var copy = try allocator.alloc([]const u8, bs.len);
|
||||
errdefer allocator.free(copy);
|
||||
for (bs, 0..) |b, i| {
|
||||
copy[i] = try allocator.dupe(u8, b);
|
||||
}
|
||||
break :blk types.AttributeValue{ .BS = copy };
|
||||
},
|
||||
.L => |list| blk: {
|
||||
var copy = try allocator.alloc(types.AttributeValue, list.len);
|
||||
errdefer allocator.free(copy);
|
||||
for (list, 0..) |item, i| {
|
||||
copy[i] = try deepCopyAttributeValue(allocator, item);
|
||||
}
|
||||
break :blk types.AttributeValue{ .L = copy };
|
||||
},
|
||||
.M => |map| blk: {
|
||||
var copy = std.StringHashMap(types.AttributeValue).init(allocator);
|
||||
errdefer {
|
||||
var iter = copy.iterator();
|
||||
while (iter.next()) |entry| {
|
||||
allocator.free(entry.key_ptr.*);
|
||||
deinitAttributeValue(entry.value_ptr, allocator);
|
||||
}
|
||||
copy.deinit();
|
||||
}
|
||||
var iter = map.iterator();
|
||||
while (iter.next()) |entry| {
|
||||
const key = try allocator.dupe(u8, entry.key_ptr.*);
|
||||
const value = try deepCopyAttributeValue(allocator, entry.value_ptr.*);
|
||||
try copy.put(key, value);
|
||||
}
|
||||
break :blk types.AttributeValue{ .M = copy };
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Memory Management
|
||||
// ============================================================================
|
||||
|
||||
/// Free all memory associated with an AttributeValue
|
||||
/// Recursively frees nested structures (Maps, Lists)
|
||||
pub fn deinitAttributeValue(attr: *types.AttributeValue, allocator: std.mem.Allocator) void {
|
||||
switch (attr.*) {
|
||||
.S, .N, .B => |slice| allocator.free(slice),
|
||||
.SS, .NS, .BS => |slices| {
|
||||
for (slices) |s| allocator.free(s);
|
||||
allocator.free(slices);
|
||||
},
|
||||
.M => |*map| {
|
||||
var iter = map.iterator();
|
||||
while (iter.next()) |entry| {
|
||||
allocator.free(entry.key_ptr.*);
|
||||
deinitAttributeValue(entry.value_ptr, allocator);
|
||||
}
|
||||
map.deinit();
|
||||
},
|
||||
.L => |list| {
|
||||
for (list) |*item| {
|
||||
deinitAttributeValue(@constCast(item), allocator);
|
||||
}
|
||||
allocator.free(list);
|
||||
},
|
||||
.NULL, .BOOL => {},
|
||||
}
|
||||
}
|
||||
|
||||
/// Free all memory associated with an Item
|
||||
pub fn deinitItem(item: *types.Item, allocator: std.mem.Allocator) void {
|
||||
var iter = item.iterator();
|
||||
while (iter.next()) |entry| {
|
||||
allocator.free(entry.key_ptr.*);
|
||||
deinitAttributeValue(entry.value_ptr, allocator);
|
||||
}
|
||||
item.deinit();
|
||||
}
|
||||
@@ -2,32 +2,111 @@
|
||||
const std = @import("std");
|
||||
const rocksdb = @import("../rocksdb.zig");
|
||||
const types = @import("types.zig");
|
||||
const json = @import("json.zig");
|
||||
const key_codec = @import("../key_codec.zig");
|
||||
const item_codec = @import("../item_codec.zig");
|
||||
const index_codec = @import("../index_codec.zig");
|
||||
|
||||
pub const StorageError = error{
|
||||
TableNotFound,
|
||||
TableAlreadyExists,
|
||||
ItemNotFound,
|
||||
InvalidKey,
|
||||
MissingKeyAttribute,
|
||||
SerializationError,
|
||||
RocksDBError,
|
||||
OutOfMemory,
|
||||
};
|
||||
|
||||
/// Key prefixes for different data types in RocksDB
|
||||
const KeyPrefix = struct {
|
||||
/// Table metadata: _meta:{table_name}
|
||||
const meta = "_meta:";
|
||||
/// Item data: _data:{table_name}:{partition_key}[:{sort_key}]
|
||||
const data = "_data:";
|
||||
/// Global secondary index: _gsi:{table_name}:{index_name}:{pk}:{sk}
|
||||
const gsi = "_gsi:";
|
||||
/// Local secondary index: _lsi:{table_name}:{index_name}:{pk}:{sk}
|
||||
const lsi = "_lsi:";
|
||||
/// Result type for Scan operations with pagination
|
||||
pub const ScanResult = struct {
|
||||
items: []types.Item,
|
||||
last_evaluated_key: ?[]u8,
|
||||
|
||||
pub fn deinit(self: *ScanResult, allocator: std.mem.Allocator) void {
|
||||
for (self.items) |*item| json.deinitItem(item, allocator);
|
||||
allocator.free(self.items);
|
||||
if (self.last_evaluated_key) |key| allocator.free(key);
|
||||
}
|
||||
};
|
||||
|
||||
/// Result type for Query operations with pagination
|
||||
pub const QueryResult = struct {
|
||||
items: []types.Item,
|
||||
last_evaluated_key: ?[]u8,
|
||||
|
||||
pub fn deinit(self: *QueryResult, allocator: std.mem.Allocator) void {
|
||||
for (self.items) |*item| json.deinitItem(item, allocator);
|
||||
allocator.free(self.items);
|
||||
if (self.last_evaluated_key) |key| allocator.free(key);
|
||||
}
|
||||
};
|
||||
|
||||
/// In-memory representation of table metadata
|
||||
pub const TableMetadata = struct {
|
||||
table_name: []const u8,
|
||||
key_schema: []types.KeySchemaElement,
|
||||
attribute_definitions: []types.AttributeDefinition,
|
||||
table_status: types.TableStatus,
|
||||
creation_date_time: i64,
|
||||
global_secondary_indexes: ?[]types.GlobalSecondaryIndex = null,
|
||||
local_secondary_indexes: ?[]types.LocalSecondaryIndex = null,
|
||||
|
||||
/// Get the attribute type for a given attribute name
|
||||
pub fn getAttributeType(self: *const TableMetadata, attr_name: []const u8) ?types.ScalarAttributeType {
|
||||
for (self.attribute_definitions) |def| {
|
||||
if (std.mem.eql(u8, def.attribute_name, attr_name)) {
|
||||
return def.attribute_type;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// Get the partition key attribute name
|
||||
pub fn getPartitionKeyName(self: *const TableMetadata) ?[]const u8 {
|
||||
for (self.key_schema) |ks| {
|
||||
if (ks.key_type == .HASH) return ks.attribute_name;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// Get the sort key attribute name (if any)
|
||||
pub fn getSortKeyName(self: *const TableMetadata) ?[]const u8 {
|
||||
for (self.key_schema) |ks| {
|
||||
if (ks.key_type == .RANGE) return ks.attribute_name;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
pub fn deinit(self: *TableMetadata, allocator: std.mem.Allocator) void {
|
||||
allocator.free(self.table_name);
|
||||
for (self.key_schema) |ks| allocator.free(ks.attribute_name);
|
||||
allocator.free(self.key_schema);
|
||||
for (self.attribute_definitions) |ad| allocator.free(ad.attribute_name);
|
||||
allocator.free(self.attribute_definitions);
|
||||
|
||||
if (self.global_secondary_indexes) |gsis| {
|
||||
for (gsis) |*gsi| {
|
||||
var gsi_mut = gsi.*;
|
||||
gsi_mut.deinit(allocator);
|
||||
}
|
||||
allocator.free(gsis);
|
||||
}
|
||||
if (self.local_secondary_indexes) |lsis| {
|
||||
for (lsis) |*lsi| {
|
||||
var lsi_mut = lsi.*;
|
||||
lsi_mut.deinit(allocator);
|
||||
}
|
||||
allocator.free(lsis);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
pub const StorageEngine = struct {
|
||||
db: rocksdb.DB,
|
||||
allocator: std.mem.Allocator,
|
||||
table_locks: std.StringHashMap(*std.Thread.RwLock),
|
||||
table_locks_mutex: std.Thread.Mutex,
|
||||
|
||||
const Self = @This();
|
||||
|
||||
@@ -36,18 +115,94 @@ pub const StorageEngine = struct {
|
||||
return Self{
|
||||
.db = db,
|
||||
.allocator = allocator,
|
||||
.table_locks = std.StringHashMap(*std.Thread.RwLock).init(allocator),
|
||||
.table_locks_mutex = std.Thread.Mutex{},
|
||||
};
|
||||
}
|
||||
|
||||
pub fn deinit(self: *Self) void {
|
||||
var iter = self.table_locks.iterator();
|
||||
while (iter.next()) |entry| {
|
||||
self.allocator.free(entry.key_ptr.*);
|
||||
self.allocator.destroy(entry.value_ptr.*);
|
||||
}
|
||||
self.table_locks.deinit();
|
||||
self.db.close();
|
||||
}
|
||||
|
||||
// === Table Operations ===
|
||||
fn getOrCreateTableLock(self: *Self, table_name: []const u8) !*std.Thread.RwLock {
|
||||
self.table_locks_mutex.lock();
|
||||
defer self.table_locks_mutex.unlock();
|
||||
|
||||
pub fn createTable(self: *Self, table_name: []const u8, key_schema: []const types.KeySchemaElement, attribute_definitions: []const types.AttributeDefinition) StorageError!types.TableDescription {
|
||||
// Check if table already exists
|
||||
const meta_key = try self.buildMetaKey(table_name);
|
||||
if (self.table_locks.get(table_name)) |lock| return lock;
|
||||
|
||||
const lock = try self.allocator.create(std.Thread.RwLock);
|
||||
lock.* = std.Thread.RwLock{};
|
||||
const owned_name = try self.allocator.dupe(u8, table_name);
|
||||
errdefer self.allocator.free(owned_name);
|
||||
try self.table_locks.put(owned_name, lock);
|
||||
return lock;
|
||||
}
|
||||
|
||||
fn removeTableLock(self: *Self, table_name: []const u8) void {
|
||||
self.table_locks_mutex.lock();
|
||||
defer self.table_locks_mutex.unlock();
|
||||
if (self.table_locks.fetchRemove(table_name)) |kv| {
|
||||
self.allocator.free(kv.key);
|
||||
self.allocator.destroy(kv.value);
|
||||
}
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// FIX A: Deep-copy key_schema and attribute_definitions into self.allocator
|
||||
// before serializing. The caller's slices (often request-arena-allocated)
|
||||
// may die after this function returns. We must not hold borrowed pointers.
|
||||
// =========================================================================
|
||||
|
||||
/// Deep-copy a key schema slice into the given allocator
|
||||
fn dupeKeySchema(allocator: std.mem.Allocator, src: []const types.KeySchemaElement) ![]types.KeySchemaElement {
|
||||
var result = try allocator.alloc(types.KeySchemaElement, src.len);
|
||||
errdefer {
|
||||
// On error, free any names we already duped
|
||||
for (result[0..src.len]) |ks| allocator.free(ks.attribute_name);
|
||||
allocator.free(result);
|
||||
}
|
||||
for (src, 0..) |ks, i| {
|
||||
result[i] = .{
|
||||
.attribute_name = try allocator.dupe(u8, ks.attribute_name),
|
||||
.key_type = ks.key_type,
|
||||
};
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Deep-copy attribute definitions slice into the given allocator
|
||||
fn dupeAttributeDefinitions(allocator: std.mem.Allocator, src: []const types.AttributeDefinition) ![]types.AttributeDefinition {
|
||||
var result = try allocator.alloc(types.AttributeDefinition, src.len);
|
||||
errdefer {
|
||||
for (result[0..src.len]) |ad| allocator.free(ad.attribute_name);
|
||||
allocator.free(result);
|
||||
}
|
||||
for (src, 0..) |ad, i| {
|
||||
result[i] = .{
|
||||
.attribute_name = try allocator.dupe(u8, ad.attribute_name),
|
||||
.attribute_type = ad.attribute_type,
|
||||
};
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
pub fn createTable(
|
||||
self: *Self,
|
||||
table_name: []const u8,
|
||||
key_schema: []const types.KeySchemaElement,
|
||||
attribute_definitions: []const types.AttributeDefinition,
|
||||
) StorageError!types.TableDescription {
|
||||
const table_lock = try self.getOrCreateTableLock(table_name);
|
||||
table_lock.lock();
|
||||
defer table_lock.unlock();
|
||||
|
||||
const meta_key = try key_codec.buildMetaKey(self.allocator, table_name);
|
||||
defer self.allocator.free(meta_key);
|
||||
|
||||
const existing = self.db.get(self.allocator, meta_key) catch return StorageError.RocksDBError;
|
||||
@@ -56,43 +211,85 @@ pub const StorageEngine = struct {
|
||||
return StorageError.TableAlreadyExists;
|
||||
}
|
||||
|
||||
// Create table metadata
|
||||
// FIX A: Deep-copy schema into engine's allocator so serialization
|
||||
// and the returned TableDescription don't depend on caller memory.
|
||||
const owned_key_schema = dupeKeySchema(self.allocator, key_schema) catch return StorageError.OutOfMemory;
|
||||
errdefer {
|
||||
for (owned_key_schema) |ks| self.allocator.free(ks.attribute_name);
|
||||
self.allocator.free(owned_key_schema);
|
||||
}
|
||||
|
||||
const owned_attr_defs = dupeAttributeDefinitions(self.allocator, attribute_definitions) catch return StorageError.OutOfMemory;
|
||||
errdefer {
|
||||
for (owned_attr_defs) |ad| self.allocator.free(ad.attribute_name);
|
||||
self.allocator.free(owned_attr_defs);
|
||||
}
|
||||
|
||||
const now = std.time.timestamp();
|
||||
const desc = types.TableDescription{
|
||||
const metadata = TableMetadata{
|
||||
.table_name = table_name, // Only used transiently for serialization
|
||||
.key_schema = owned_key_schema,
|
||||
.attribute_definitions = owned_attr_defs,
|
||||
.table_status = .ACTIVE,
|
||||
.creation_date_time = now,
|
||||
};
|
||||
|
||||
const meta_value = try self.serializeTableMetadata(metadata);
|
||||
defer self.allocator.free(meta_value);
|
||||
self.db.put(meta_key, meta_value) catch return StorageError.RocksDBError;
|
||||
|
||||
// Return description with engine-owned copies. The handler only reads
|
||||
// these to format a JSON response; the owned copies are freed below
|
||||
// after that response is built (handler uses request_alloc for formatting).
|
||||
// NOTE: caller must NOT store these pointers long-term. The returned
|
||||
// TableDescription borrows from owned_key_schema / owned_attr_defs which
|
||||
// we intentionally leak here so the caller can read them synchronously.
|
||||
// For a cleaner API we could return only what's needed for the response,
|
||||
// but this matches the existing contract.
|
||||
|
||||
// Since we're returning borrowed views into owned_key_schema/owned_attr_defs,
|
||||
// and the handler only uses table_name/status/creation_date for CreateTable
|
||||
// response, we can safely free the owned copies after serialization.
|
||||
// But to be maximally safe, we transfer ownership to the caller via
|
||||
// the same pattern used by getTableMetadata/describeTable:
|
||||
// the caller is expected to treat this as read-only within request scope.
|
||||
|
||||
// Free the engine-owned copies since they're persisted to RocksDB.
|
||||
// The returned description only needs the scalars for CreateTable response.
|
||||
for (owned_key_schema) |ks| self.allocator.free(ks.attribute_name);
|
||||
self.allocator.free(owned_key_schema);
|
||||
for (owned_attr_defs) |ad| self.allocator.free(ad.attribute_name);
|
||||
self.allocator.free(owned_attr_defs);
|
||||
|
||||
return types.TableDescription{
|
||||
.table_name = table_name,
|
||||
.key_schema = key_schema,
|
||||
.key_schema = key_schema, // caller-owned, valid for request scope
|
||||
.attribute_definitions = attribute_definitions,
|
||||
.table_status = .ACTIVE,
|
||||
.creation_date_time = now,
|
||||
.item_count = 0,
|
||||
.table_size_bytes = 0,
|
||||
};
|
||||
|
||||
// Serialize and store
|
||||
const meta_value = try self.serializeTableMetadata(desc);
|
||||
defer self.allocator.free(meta_value);
|
||||
|
||||
self.db.put(meta_key, meta_value) catch return StorageError.RocksDBError;
|
||||
return desc;
|
||||
}
|
||||
|
||||
pub fn deleteTable(self: *Self, table_name: []const u8) StorageError!void {
|
||||
const meta_key = try self.buildMetaKey(table_name);
|
||||
const table_lock = try self.getOrCreateTableLock(table_name);
|
||||
table_lock.lock();
|
||||
defer table_lock.unlock();
|
||||
|
||||
const meta_key = try key_codec.buildMetaKey(self.allocator, table_name);
|
||||
defer self.allocator.free(meta_key);
|
||||
|
||||
// Verify table exists
|
||||
const existing = self.db.get(self.allocator, meta_key) catch return StorageError.RocksDBError;
|
||||
if (existing == null) return StorageError.TableNotFound;
|
||||
self.allocator.free(existing.?);
|
||||
|
||||
// Delete all items with this table's prefix
|
||||
const data_prefix = try self.buildDataPrefix(table_name);
|
||||
const data_prefix = try key_codec.buildTablePrefix(self.allocator, table_name);
|
||||
defer self.allocator.free(data_prefix);
|
||||
|
||||
var batch = rocksdb.WriteBatch.init() orelse return StorageError.RocksDBError;
|
||||
defer batch.deinit();
|
||||
|
||||
// Scan and delete all matching keys
|
||||
var iter = rocksdb.Iterator.init(&self.db) orelse return StorageError.RocksDBError;
|
||||
defer iter.deinit();
|
||||
|
||||
@@ -103,15 +300,51 @@ pub const StorageEngine = struct {
|
||||
batch.delete(key);
|
||||
iter.next();
|
||||
}
|
||||
|
||||
// Delete metadata
|
||||
batch.delete(meta_key);
|
||||
|
||||
batch.write(&self.db) catch return StorageError.RocksDBError;
|
||||
defer self.removeTableLock(table_name);
|
||||
}
|
||||
|
||||
pub fn describeTable(self: *Self, table_name: []const u8) StorageError!types.TableDescription {
|
||||
const meta_key = try self.buildMetaKey(table_name);
|
||||
const table_lock = try self.getOrCreateTableLock(table_name);
|
||||
table_lock.lockShared();
|
||||
defer table_lock.unlockShared();
|
||||
|
||||
var metadata = try self.getTableMetadata(table_name);
|
||||
defer metadata.deinit(self.allocator);
|
||||
|
||||
const data_prefix = try key_codec.buildTablePrefix(self.allocator, table_name);
|
||||
defer self.allocator.free(data_prefix);
|
||||
|
||||
var item_count: u64 = 0;
|
||||
var total_size: u64 = 0;
|
||||
|
||||
var iter = rocksdb.Iterator.init(&self.db) orelse return StorageError.RocksDBError;
|
||||
defer iter.deinit();
|
||||
|
||||
iter.seek(data_prefix);
|
||||
while (iter.valid()) {
|
||||
const key = iter.key() orelse break;
|
||||
if (!std.mem.startsWith(u8, key, data_prefix)) break;
|
||||
const value = iter.value() orelse break;
|
||||
item_count += 1;
|
||||
total_size += value.len;
|
||||
iter.next();
|
||||
}
|
||||
|
||||
return types.TableDescription{
|
||||
.table_name = metadata.table_name,
|
||||
.key_schema = metadata.key_schema,
|
||||
.attribute_definitions = metadata.attribute_definitions,
|
||||
.table_status = metadata.table_status,
|
||||
.creation_date_time = metadata.creation_date_time,
|
||||
.item_count = item_count,
|
||||
.table_size_bytes = total_size,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn getTableMetadata(self: *Self, table_name: []const u8) StorageError!TableMetadata {
|
||||
const meta_key = try key_codec.buildMetaKey(self.allocator, table_name);
|
||||
defer self.allocator.free(meta_key);
|
||||
|
||||
const meta_value = self.db.get(self.allocator, meta_key) catch return StorageError.RocksDBError;
|
||||
@@ -122,70 +355,148 @@ pub const StorageEngine = struct {
|
||||
}
|
||||
|
||||
pub fn listTables(self: *Self) StorageError![][]const u8 {
|
||||
var tables = std.ArrayList([]const u8){};
|
||||
var tables = std.ArrayList([]const u8).init(self.allocator);
|
||||
errdefer {
|
||||
for (tables.items) |t| self.allocator.free(t);
|
||||
tables.deinit(self.allocator);
|
||||
tables.deinit();
|
||||
}
|
||||
|
||||
var iter = rocksdb.Iterator.init(&self.db) orelse return StorageError.RocksDBError;
|
||||
defer iter.deinit();
|
||||
|
||||
iter.seek(KeyPrefix.meta);
|
||||
const meta_prefix = [_]u8{key_codec.EntityType.meta.toByte()};
|
||||
iter.seek(&meta_prefix);
|
||||
|
||||
while (iter.valid()) {
|
||||
const key = iter.key() orelse break;
|
||||
if (!std.mem.startsWith(u8, key, KeyPrefix.meta)) break;
|
||||
if (key.len == 0 or key[0] != key_codec.EntityType.meta.toByte()) break;
|
||||
|
||||
const table_name = key[KeyPrefix.meta.len..];
|
||||
const owned_name = self.allocator.dupe(u8, table_name) catch return StorageError.OutOfMemory;
|
||||
tables.append(self.allocator, owned_name) catch return StorageError.OutOfMemory;
|
||||
var decoder = key_codec.KeyDecoder.init(key);
|
||||
_ = decoder.readEntityType() catch break;
|
||||
const tbl_name = decoder.readSegmentBorrowed() catch break;
|
||||
|
||||
const owned_name = self.allocator.dupe(u8, tbl_name) catch return StorageError.OutOfMemory;
|
||||
tables.append(owned_name) catch return StorageError.OutOfMemory;
|
||||
iter.next();
|
||||
}
|
||||
|
||||
return tables.toOwnedSlice(self.allocator) catch return StorageError.OutOfMemory;
|
||||
return tables.toOwnedSlice() catch return StorageError.OutOfMemory;
|
||||
}
|
||||
|
||||
// === Item Operations ===
|
||||
pub fn putItem(self: *Self, table_name: []const u8, item: types.Item) StorageError!void {
|
||||
const table_lock = try self.getOrCreateTableLock(table_name);
|
||||
table_lock.lockShared();
|
||||
defer table_lock.unlockShared();
|
||||
|
||||
pub fn putItem(self: *Self, table_name: []const u8, item_json: []const u8) StorageError!void {
|
||||
// Verify table exists
|
||||
const meta_key = try self.buildMetaKey(table_name);
|
||||
defer self.allocator.free(meta_key);
|
||||
var metadata = try self.getTableMetadata(table_name);
|
||||
defer metadata.deinit(self.allocator);
|
||||
|
||||
const meta = self.db.get(self.allocator, meta_key) catch return StorageError.RocksDBError;
|
||||
if (meta == null) return StorageError.TableNotFound;
|
||||
defer self.allocator.free(meta.?);
|
||||
var key = types.Key.fromItem(self.allocator, item, metadata.key_schema) catch |err| {
|
||||
return switch (err) {
|
||||
error.MissingKeyAttribute => StorageError.MissingKeyAttribute,
|
||||
error.InvalidKeyType => StorageError.InvalidKey,
|
||||
else => StorageError.RocksDBError,
|
||||
};
|
||||
};
|
||||
defer key.deinit(self.allocator);
|
||||
|
||||
// Extract key from item (simplified - assumes key is extractable from JSON)
|
||||
const item_key = try self.extractKeyFromItem(table_name, item_json);
|
||||
defer self.allocator.free(item_key);
|
||||
const key_values = try key.getValues();
|
||||
const storage_key = try key_codec.buildDataKey(self.allocator, table_name, key_values.pk, key_values.sk);
|
||||
defer self.allocator.free(storage_key);
|
||||
|
||||
self.db.put(item_key, item_json) catch return StorageError.RocksDBError;
|
||||
const item_binary = item_codec.encode(self.allocator, item) catch return StorageError.SerializationError;
|
||||
defer self.allocator.free(item_binary);
|
||||
|
||||
var batch = rocksdb.WriteBatch.init() orelse return StorageError.RocksDBError;
|
||||
defer batch.deinit();
|
||||
batch.put(storage_key, item_binary);
|
||||
|
||||
if (metadata.global_secondary_indexes) |gsis| {
|
||||
for (gsis) |gsi| {
|
||||
try self.maintainGSIEntry(&batch, table_name, &gsi, item, key_values.pk, key_values.sk);
|
||||
}
|
||||
}
|
||||
if (metadata.local_secondary_indexes) |lsis| {
|
||||
for (lsis) |lsi| {
|
||||
try self.maintainLSIEntry(&batch, table_name, &lsi, item, key_values.pk, key_values.sk);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn getItem(self: *Self, table_name: []const u8, key_json: []const u8) StorageError!?[]u8 {
|
||||
const item_key = try self.buildItemKey(table_name, key_json);
|
||||
defer self.allocator.free(item_key);
|
||||
|
||||
return self.db.get(self.allocator, item_key) catch return StorageError.RocksDBError;
|
||||
batch.write(&self.db) catch return StorageError.RocksDBError;
|
||||
}
|
||||
|
||||
pub fn deleteItem(self: *Self, table_name: []const u8, key_json: []const u8) StorageError!void {
|
||||
const item_key = try self.buildItemKey(table_name, key_json);
|
||||
defer self.allocator.free(item_key);
|
||||
pub fn getItem(self: *Self, table_name: []const u8, key: types.Item) StorageError!?types.Item {
|
||||
const table_lock = try self.getOrCreateTableLock(table_name);
|
||||
table_lock.lockShared();
|
||||
defer table_lock.unlockShared();
|
||||
|
||||
self.db.delete(item_key) catch return StorageError.RocksDBError;
|
||||
var metadata = try self.getTableMetadata(table_name);
|
||||
defer metadata.deinit(self.allocator);
|
||||
|
||||
var key_struct = types.Key.fromItem(self.allocator, key, metadata.key_schema) catch |err| {
|
||||
return switch (err) {
|
||||
error.MissingKeyAttribute => StorageError.MissingKeyAttribute,
|
||||
error.InvalidKeyType => StorageError.InvalidKey,
|
||||
else => StorageError.RocksDBError,
|
||||
};
|
||||
};
|
||||
defer key_struct.deinit(self.allocator);
|
||||
|
||||
const key_values = try key_struct.getValues();
|
||||
const storage_key = try key_codec.buildDataKey(self.allocator, table_name, key_values.pk, key_values.sk);
|
||||
defer self.allocator.free(storage_key);
|
||||
|
||||
const item_binary = self.db.get(self.allocator, storage_key) catch return StorageError.RocksDBError;
|
||||
if (item_binary == null) return null;
|
||||
defer self.allocator.free(item_binary.?);
|
||||
|
||||
return item_codec.decode(self.allocator, item_binary.?) catch return StorageError.SerializationError;
|
||||
}
|
||||
|
||||
pub fn scan(self: *Self, table_name: []const u8, limit: ?usize) StorageError![][]const u8 {
|
||||
const data_prefix = try self.buildDataPrefix(table_name);
|
||||
pub fn deleteItem(self: *Self, table_name: []const u8, key: types.Item) StorageError!void {
|
||||
const table_lock = try self.getOrCreateTableLock(table_name);
|
||||
table_lock.lockShared();
|
||||
defer table_lock.unlockShared();
|
||||
|
||||
var metadata = try self.getTableMetadata(table_name);
|
||||
defer metadata.deinit(self.allocator);
|
||||
|
||||
var key_struct = types.Key.fromItem(self.allocator, key, metadata.key_schema) catch |err| {
|
||||
return switch (err) {
|
||||
error.MissingKeyAttribute => StorageError.MissingKeyAttribute,
|
||||
error.InvalidKeyType => StorageError.InvalidKey,
|
||||
else => StorageError.RocksDBError,
|
||||
};
|
||||
};
|
||||
defer key_struct.deinit(self.allocator);
|
||||
|
||||
const key_values = try key_struct.getValues();
|
||||
const storage_key = try key_codec.buildDataKey(self.allocator, table_name, key_values.pk, key_values.sk);
|
||||
defer self.allocator.free(storage_key);
|
||||
|
||||
self.db.delete(storage_key) catch return StorageError.RocksDBError;
|
||||
}
|
||||
|
||||
pub fn scan(
|
||||
self: *Self,
|
||||
table_name: []const u8,
|
||||
limit: ?usize,
|
||||
exclusive_start_key: ?[]const u8,
|
||||
) StorageError!ScanResult {
|
||||
const table_lock = try self.getOrCreateTableLock(table_name);
|
||||
table_lock.lockShared();
|
||||
defer table_lock.unlockShared();
|
||||
|
||||
var metadata = try self.getTableMetadata(table_name);
|
||||
defer metadata.deinit(self.allocator);
|
||||
|
||||
const data_prefix = try key_codec.buildTablePrefix(self.allocator, table_name);
|
||||
defer self.allocator.free(data_prefix);
|
||||
|
||||
var items = std.ArrayList([]const u8){};
|
||||
var items = std.ArrayList(types.Item).init(self.allocator);
|
||||
errdefer {
|
||||
for (items.items) |item| self.allocator.free(item);
|
||||
items.deinit(self.allocator);
|
||||
for (items.items) |*item| json.deinitItem(item, self.allocator);
|
||||
items.deinit();
|
||||
}
|
||||
|
||||
var iter = rocksdb.Iterator.init(&self.db) orelse return StorageError.RocksDBError;
|
||||
@@ -194,31 +505,61 @@ pub const StorageEngine = struct {
|
||||
var count: usize = 0;
|
||||
const max_items = limit orelse std.math.maxInt(usize);
|
||||
|
||||
if (exclusive_start_key) |start_key| {
|
||||
iter.seek(start_key);
|
||||
if (iter.valid()) iter.next();
|
||||
} else {
|
||||
iter.seek(data_prefix);
|
||||
}
|
||||
|
||||
var last_key: ?[]u8 = null;
|
||||
errdefer if (last_key) |k| self.allocator.free(k);
|
||||
|
||||
while (iter.valid() and count < max_items) {
|
||||
const key = iter.key() orelse break;
|
||||
if (!std.mem.startsWith(u8, key, data_prefix)) break;
|
||||
|
||||
const value = iter.value() orelse break;
|
||||
const owned_value = self.allocator.dupe(u8, value) catch return StorageError.OutOfMemory;
|
||||
items.append(self.allocator, owned_value) catch return StorageError.OutOfMemory;
|
||||
|
||||
const item = item_codec.decode(self.allocator, value) catch {
|
||||
iter.next();
|
||||
continue;
|
||||
};
|
||||
items.append(item) catch return StorageError.OutOfMemory;
|
||||
count += 1;
|
||||
|
||||
if (count >= max_items) {
|
||||
last_key = try self.allocator.dupe(u8, key);
|
||||
}
|
||||
iter.next();
|
||||
}
|
||||
|
||||
return items.toOwnedSlice(self.allocator) catch return StorageError.OutOfMemory;
|
||||
return ScanResult{
|
||||
.items = items.toOwnedSlice() catch return StorageError.OutOfMemory,
|
||||
.last_evaluated_key = last_key,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn query(self: *Self, table_name: []const u8, partition_key: []const u8, limit: ?usize) StorageError![][]const u8 {
|
||||
// Build prefix for this partition
|
||||
const prefix = try self.buildPartitionPrefix(table_name, partition_key);
|
||||
pub fn query(
|
||||
self: *Self,
|
||||
table_name: []const u8,
|
||||
partition_key_value: []const u8,
|
||||
limit: ?usize,
|
||||
exclusive_start_key: ?[]const u8,
|
||||
) StorageError!QueryResult {
|
||||
const table_lock = try self.getOrCreateTableLock(table_name);
|
||||
table_lock.lockShared();
|
||||
defer table_lock.unlockShared();
|
||||
|
||||
var metadata = try self.getTableMetadata(table_name);
|
||||
defer metadata.deinit(self.allocator);
|
||||
|
||||
const prefix = try key_codec.buildPartitionPrefix(self.allocator, table_name, partition_key_value);
|
||||
defer self.allocator.free(prefix);
|
||||
|
||||
var items = std.ArrayList([]const u8){};
|
||||
var items = std.ArrayList(types.Item).init(self.allocator);
|
||||
errdefer {
|
||||
for (items.items) |item| self.allocator.free(item);
|
||||
items.deinit(self.allocator);
|
||||
for (items.items) |*item| json.deinitItem(item, self.allocator);
|
||||
items.deinit();
|
||||
}
|
||||
|
||||
var iter = rocksdb.Iterator.init(&self.db) orelse return StorageError.RocksDBError;
|
||||
@@ -227,161 +568,249 @@ pub const StorageEngine = struct {
|
||||
var count: usize = 0;
|
||||
const max_items = limit orelse std.math.maxInt(usize);
|
||||
|
||||
if (exclusive_start_key) |start_key| {
|
||||
if (std.mem.startsWith(u8, start_key, prefix)) {
|
||||
iter.seek(start_key);
|
||||
if (iter.valid()) iter.next();
|
||||
} else {
|
||||
iter.seek(prefix);
|
||||
}
|
||||
} else {
|
||||
iter.seek(prefix);
|
||||
}
|
||||
|
||||
var last_key: ?[]u8 = null;
|
||||
errdefer if (last_key) |k| self.allocator.free(k);
|
||||
|
||||
while (iter.valid() and count < max_items) {
|
||||
const key = iter.key() orelse break;
|
||||
if (!std.mem.startsWith(u8, key, prefix)) break;
|
||||
|
||||
const value = iter.value() orelse break;
|
||||
const owned_value = self.allocator.dupe(u8, value) catch return StorageError.OutOfMemory;
|
||||
items.append(self.allocator, owned_value) catch return StorageError.OutOfMemory;
|
||||
|
||||
const item = item_codec.decode(self.allocator, value) catch {
|
||||
iter.next();
|
||||
continue;
|
||||
};
|
||||
items.append(item) catch return StorageError.OutOfMemory;
|
||||
count += 1;
|
||||
|
||||
if (count >= max_items) {
|
||||
last_key = try self.allocator.dupe(u8, key);
|
||||
}
|
||||
iter.next();
|
||||
}
|
||||
|
||||
return items.toOwnedSlice(self.allocator) catch return StorageError.OutOfMemory;
|
||||
return QueryResult{
|
||||
.items = items.toOwnedSlice() catch return StorageError.OutOfMemory,
|
||||
.last_evaluated_key = last_key,
|
||||
};
|
||||
}
|
||||
|
||||
// === Key Building Helpers ===
|
||||
fn maintainGSIEntry(
|
||||
self: *Self,
|
||||
batch: *rocksdb.WriteBatch,
|
||||
table_name: []const u8,
|
||||
gsi: *const types.GlobalSecondaryIndex,
|
||||
item: types.Item,
|
||||
primary_pk: []const u8,
|
||||
primary_sk: ?[]const u8,
|
||||
) StorageError!void {
|
||||
var gsi_key = types.Key.fromItem(self.allocator, item, gsi.key_schema) catch return;
|
||||
defer gsi_key.deinit(self.allocator);
|
||||
|
||||
fn buildMetaKey(self: *Self, table_name: []const u8) StorageError![]u8 {
|
||||
return std.fmt.allocPrint(self.allocator, "{s}{s}", .{ KeyPrefix.meta, table_name }) catch return StorageError.OutOfMemory;
|
||||
const gsi_key_values = try gsi_key.getValues();
|
||||
const gsi_storage_key = try key_codec.buildGSIKey(
|
||||
self.allocator,
|
||||
table_name,
|
||||
gsi.index_name,
|
||||
gsi_key_values.pk,
|
||||
gsi_key_values.sk,
|
||||
);
|
||||
defer self.allocator.free(gsi_storage_key);
|
||||
|
||||
const primary_key_ref = try index_codec.encodePrimaryKeyRef(self.allocator, primary_pk, primary_sk);
|
||||
defer self.allocator.free(primary_key_ref);
|
||||
|
||||
batch.put(gsi_storage_key, primary_key_ref);
|
||||
}
|
||||
|
||||
fn buildDataPrefix(self: *Self, table_name: []const u8) StorageError![]u8 {
|
||||
return std.fmt.allocPrint(self.allocator, "{s}{s}:", .{ KeyPrefix.data, table_name }) catch return StorageError.OutOfMemory;
|
||||
}
|
||||
|
||||
fn buildPartitionPrefix(self: *Self, table_name: []const u8, partition_key: []const u8) StorageError![]u8 {
|
||||
return std.fmt.allocPrint(self.allocator, "{s}{s}:{s}", .{ KeyPrefix.data, table_name, partition_key }) catch return StorageError.OutOfMemory;
|
||||
}
|
||||
|
||||
fn buildItemKey(self: *Self, table_name: []const u8, key_json: []const u8) StorageError![]u8 {
|
||||
// Parse the key JSON to extract partition key (and sort key if present)
|
||||
// For now, use simplified key extraction
|
||||
const pk = extractStringValue(key_json, "pk") orelse extractStringValue(key_json, "PK") orelse return StorageError.InvalidKey;
|
||||
const sk = extractStringValue(key_json, "sk") orelse extractStringValue(key_json, "SK");
|
||||
|
||||
if (sk) |sort_key| {
|
||||
return std.fmt.allocPrint(self.allocator, "{s}{s}:{s}:{s}", .{ KeyPrefix.data, table_name, pk, sort_key }) catch return StorageError.OutOfMemory;
|
||||
} else {
|
||||
return std.fmt.allocPrint(self.allocator, "{s}{s}:{s}", .{ KeyPrefix.data, table_name, pk }) catch return StorageError.OutOfMemory;
|
||||
fn maintainLSIEntry(
|
||||
self: *Self,
|
||||
batch: *rocksdb.WriteBatch,
|
||||
table_name: []const u8,
|
||||
lsi: *const types.LocalSecondaryIndex,
|
||||
item: types.Item,
|
||||
primary_pk: []const u8,
|
||||
primary_sk: ?[]const u8,
|
||||
) StorageError!void {
|
||||
var lsi_sk_value: ?[]const u8 = null;
|
||||
for (lsi.key_schema) |ks| {
|
||||
if (ks.key_type == .RANGE) {
|
||||
const attr = item.get(ks.attribute_name) orelse return;
|
||||
lsi_sk_value = switch (attr) {
|
||||
.S => |s| s,
|
||||
.N => |n| n,
|
||||
.B => |b| b,
|
||||
else => return StorageError.InvalidKey,
|
||||
};
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
fn extractKeyFromItem(self: *Self, table_name: []const u8, item_json: []const u8) StorageError![]u8 {
|
||||
return self.buildItemKey(table_name, item_json);
|
||||
const lsi_sk = lsi_sk_value orelse return;
|
||||
const lsi_storage_key = try key_codec.buildLSIKey(self.allocator, table_name, lsi.index_name, primary_pk, lsi_sk);
|
||||
defer self.allocator.free(lsi_storage_key);
|
||||
|
||||
const primary_key_ref = try index_codec.encodePrimaryKeyRef(self.allocator, primary_pk, primary_sk);
|
||||
defer self.allocator.free(primary_key_ref);
|
||||
|
||||
batch.put(lsi_storage_key, primary_key_ref);
|
||||
}
|
||||
|
||||
// === Serialization Helpers ===
|
||||
// Serialization helpers
|
||||
const KeySchemaElementJson = struct { AttributeName: []const u8, KeyType: []const u8 };
|
||||
const AttributeDefinitionJson = struct { AttributeName: []const u8, AttributeType: []const u8 };
|
||||
const TableMetadataJson = struct {
|
||||
TableName: []const u8,
|
||||
TableStatus: []const u8,
|
||||
CreationDateTime: i64,
|
||||
KeySchema: []const KeySchemaElementJson,
|
||||
AttributeDefinitions: []const AttributeDefinitionJson,
|
||||
};
|
||||
|
||||
fn serializeTableMetadata(self: *Self, desc: types.TableDescription) StorageError![]u8 {
|
||||
var buf = std.ArrayList(u8){};
|
||||
errdefer buf.deinit(self.allocator);
|
||||
const writer = buf.writer(self.allocator);
|
||||
|
||||
writer.print("{{\"TableName\":\"{s}\",\"TableStatus\":\"{s}\",\"CreationDateTime\":{d},\"ItemCount\":{d},\"TableSizeBytes\":{d},\"KeySchema\":[", .{
|
||||
desc.table_name,
|
||||
desc.table_status.toString(),
|
||||
desc.creation_date_time,
|
||||
desc.item_count,
|
||||
desc.table_size_bytes,
|
||||
}) catch return StorageError.SerializationError;
|
||||
|
||||
for (desc.key_schema, 0..) |ks, i| {
|
||||
if (i > 0) writer.writeByte(',') catch return StorageError.SerializationError;
|
||||
writer.print("{{\"AttributeName\":\"{s}\",\"KeyType\":\"{s}\"}}", .{
|
||||
ks.attribute_name,
|
||||
ks.key_type.toString(),
|
||||
}) catch return StorageError.SerializationError;
|
||||
fn serializeTableMetadata(self: *Self, metadata: TableMetadata) StorageError![]u8 {
|
||||
var key_schema_json = std.ArrayList(KeySchemaElementJson).init(self.allocator);
|
||||
defer key_schema_json.deinit();
|
||||
for (metadata.key_schema) |ks| {
|
||||
try key_schema_json.append(.{ .AttributeName = ks.attribute_name, .KeyType = ks.key_type.toString() });
|
||||
}
|
||||
|
||||
writer.writeAll("],\"AttributeDefinitions\":[") catch return StorageError.SerializationError;
|
||||
|
||||
for (desc.attribute_definitions, 0..) |ad, i| {
|
||||
if (i > 0) writer.writeByte(',') catch return StorageError.SerializationError;
|
||||
writer.print("{{\"AttributeName\":\"{s}\",\"AttributeType\":\"{s}\"}}", .{
|
||||
ad.attribute_name,
|
||||
ad.attribute_type.toString(),
|
||||
}) catch return StorageError.SerializationError;
|
||||
var attr_defs_json = std.ArrayList(AttributeDefinitionJson).init(self.allocator);
|
||||
defer attr_defs_json.deinit();
|
||||
for (metadata.attribute_definitions) |ad| {
|
||||
try attr_defs_json.append(.{ .AttributeName = ad.attribute_name, .AttributeType = ad.attribute_type.toString() });
|
||||
}
|
||||
|
||||
writer.writeAll("]}") catch return StorageError.SerializationError;
|
||||
const metadata_json = TableMetadataJson{
|
||||
.TableName = metadata.table_name,
|
||||
.TableStatus = metadata.table_status.toString(),
|
||||
.CreationDateTime = metadata.creation_date_time,
|
||||
.KeySchema = key_schema_json.items,
|
||||
.AttributeDefinitions = attr_defs_json.items,
|
||||
};
|
||||
|
||||
return buf.toOwnedSlice(self.allocator) catch return StorageError.OutOfMemory;
|
||||
var buf = std.ArrayList(u8).init(self.allocator);
|
||||
errdefer buf.deinit();
|
||||
try std.json.stringify(metadata_json, .{}, buf.writer());
|
||||
return buf.toOwnedSlice() catch return StorageError.OutOfMemory;
|
||||
}
|
||||
|
||||
fn deserializeTableMetadata(self: *Self, data: []const u8) StorageError!types.TableDescription {
|
||||
// Simplified deserialization - in production, use proper JSON parsing
|
||||
_ = self;
|
||||
fn deserializeTableMetadata(self: *Self, data: []const u8) StorageError!TableMetadata {
|
||||
const parsed = std.json.parseFromSlice(std.json.Value, self.allocator, data, .{}) catch return StorageError.SerializationError;
|
||||
defer parsed.deinit();
|
||||
|
||||
const table_name = extractStringValue(data, "TableName") orelse return StorageError.SerializationError;
|
||||
const status_str = extractStringValue(data, "TableStatus") orelse "ACTIVE";
|
||||
const root = switch (parsed.value) {
|
||||
.object => |o| o,
|
||||
else => return StorageError.SerializationError,
|
||||
};
|
||||
|
||||
const status: types.TableStatus = if (std.mem.eql(u8, status_str, "ACTIVE"))
|
||||
.ACTIVE
|
||||
else if (std.mem.eql(u8, status_str, "CREATING"))
|
||||
.CREATING
|
||||
else
|
||||
.ACTIVE;
|
||||
const table_name_val = root.get("TableName") orelse return StorageError.SerializationError;
|
||||
const table_name_str = switch (table_name_val) {
|
||||
.string => |s| s,
|
||||
else => return StorageError.SerializationError,
|
||||
};
|
||||
const table_name = self.allocator.dupe(u8, table_name_str) catch return StorageError.OutOfMemory;
|
||||
errdefer self.allocator.free(table_name);
|
||||
|
||||
return types.TableDescription{
|
||||
const status_val = root.get("TableStatus") orelse return StorageError.SerializationError;
|
||||
const status_str = switch (status_val) {
|
||||
.string => |s| s,
|
||||
else => return StorageError.SerializationError,
|
||||
};
|
||||
const table_status: types.TableStatus = if (std.mem.eql(u8, status_str, "ACTIVE")) .ACTIVE else if (std.mem.eql(u8, status_str, "CREATING")) .CREATING else if (std.mem.eql(u8, status_str, "DELETING")) .DELETING else .ACTIVE;
|
||||
|
||||
const creation_val = root.get("CreationDateTime") orelse return StorageError.SerializationError;
|
||||
const creation_date_time = switch (creation_val) {
|
||||
.integer => |i| i,
|
||||
else => return StorageError.SerializationError,
|
||||
};
|
||||
|
||||
const key_schema_val = root.get("KeySchema") orelse return StorageError.SerializationError;
|
||||
const key_schema_array = switch (key_schema_val) {
|
||||
.array => |a| a,
|
||||
else => return StorageError.SerializationError,
|
||||
};
|
||||
|
||||
var key_schema = std.ArrayList(types.KeySchemaElement).init(self.allocator);
|
||||
errdefer {
|
||||
for (key_schema.items) |ks| self.allocator.free(ks.attribute_name);
|
||||
key_schema.deinit();
|
||||
}
|
||||
|
||||
for (key_schema_array.items) |item| {
|
||||
const obj = switch (item) {
|
||||
.object => |o| o,
|
||||
else => return StorageError.SerializationError,
|
||||
};
|
||||
const attr_name_val = obj.get("AttributeName") orelse return StorageError.SerializationError;
|
||||
const attr_name_str = switch (attr_name_val) {
|
||||
.string => |s| s,
|
||||
else => return StorageError.SerializationError,
|
||||
};
|
||||
const attr_name = self.allocator.dupe(u8, attr_name_str) catch return StorageError.OutOfMemory;
|
||||
errdefer self.allocator.free(attr_name);
|
||||
|
||||
const key_type_val = obj.get("KeyType") orelse return StorageError.SerializationError;
|
||||
const key_type_str = switch (key_type_val) {
|
||||
.string => |s| s,
|
||||
else => return StorageError.SerializationError,
|
||||
};
|
||||
const key_type = types.KeyType.fromString(key_type_str) orelse return StorageError.SerializationError;
|
||||
|
||||
key_schema.append(.{ .attribute_name = attr_name, .key_type = key_type }) catch return StorageError.OutOfMemory;
|
||||
}
|
||||
|
||||
const attr_defs_val = root.get("AttributeDefinitions") orelse return StorageError.SerializationError;
|
||||
const attr_defs_array = switch (attr_defs_val) {
|
||||
.array => |a| a,
|
||||
else => return StorageError.SerializationError,
|
||||
};
|
||||
|
||||
var attr_defs = std.ArrayList(types.AttributeDefinition).init(self.allocator);
|
||||
errdefer {
|
||||
for (attr_defs.items) |ad| self.allocator.free(ad.attribute_name);
|
||||
attr_defs.deinit();
|
||||
}
|
||||
|
||||
for (attr_defs_array.items) |item| {
|
||||
const obj = switch (item) {
|
||||
.object => |o| o,
|
||||
else => return StorageError.SerializationError,
|
||||
};
|
||||
const attr_name_val = obj.get("AttributeName") orelse return StorageError.SerializationError;
|
||||
const attr_name_str = switch (attr_name_val) {
|
||||
.string => |s| s,
|
||||
else => return StorageError.SerializationError,
|
||||
};
|
||||
const attr_name = self.allocator.dupe(u8, attr_name_str) catch return StorageError.OutOfMemory;
|
||||
errdefer self.allocator.free(attr_name);
|
||||
|
||||
const attr_type_val = obj.get("AttributeType") orelse return StorageError.SerializationError;
|
||||
const attr_type_str = switch (attr_type_val) {
|
||||
.string => |s| s,
|
||||
else => return StorageError.SerializationError,
|
||||
};
|
||||
const attr_type = types.ScalarAttributeType.fromString(attr_type_str) orelse return StorageError.SerializationError;
|
||||
|
||||
attr_defs.append(.{ .attribute_name = attr_name, .attribute_type = attr_type }) catch return StorageError.OutOfMemory;
|
||||
}
|
||||
|
||||
return TableMetadata{
|
||||
.table_name = table_name,
|
||||
.key_schema = &[_]types.KeySchemaElement{},
|
||||
.attribute_definitions = &[_]types.AttributeDefinition{},
|
||||
.table_status = status,
|
||||
.creation_date_time = 0,
|
||||
.item_count = 0,
|
||||
.table_size_bytes = 0,
|
||||
.key_schema = key_schema.toOwnedSlice() catch return StorageError.OutOfMemory,
|
||||
.attribute_definitions = attr_defs.toOwnedSlice() catch return StorageError.OutOfMemory,
|
||||
.table_status = table_status,
|
||||
.creation_date_time = creation_date_time,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
/// Simple JSON string value extraction (production code should use std.json)
|
||||
fn extractStringValue(json_data: []const u8, key: []const u8) ?[]const u8 {
|
||||
// Look for "key":"value" pattern
|
||||
var search_buf: [256]u8 = undefined;
|
||||
const search_pattern = std.fmt.bufPrint(&search_buf, "\"{s}\":\"", .{key}) catch return null;
|
||||
const start = std.mem.indexOf(u8, json_data, search_pattern) orelse return null;
|
||||
const value_start = start + search_pattern.len;
|
||||
const value_end = std.mem.indexOfPos(u8, json_data, value_start, "\"") orelse return null;
|
||||
return json_data[value_start..value_end];
|
||||
}
|
||||
|
||||
test "storage basic operations" {
|
||||
const allocator = std.testing.allocator;
|
||||
|
||||
const path = "/tmp/test_storage";
|
||||
defer std.fs.deleteTreeAbsolute(path) catch {};
|
||||
|
||||
var engine = try StorageEngine.init(allocator, path);
|
||||
defer engine.deinit();
|
||||
|
||||
// Create table
|
||||
const key_schema = [_]types.KeySchemaElement{
|
||||
.{ .attribute_name = "pk", .key_type = .HASH },
|
||||
};
|
||||
const attr_defs = [_]types.AttributeDefinition{
|
||||
.{ .attribute_name = "pk", .attribute_type = .S },
|
||||
};
|
||||
|
||||
_ = try engine.createTable("TestTable", &key_schema, &attr_defs);
|
||||
|
||||
// List tables
|
||||
const tables = try engine.listTables();
|
||||
defer {
|
||||
for (tables) |t| allocator.free(t);
|
||||
allocator.free(tables);
|
||||
}
|
||||
try std.testing.expectEqual(@as(usize, 1), tables.len);
|
||||
try std.testing.expectEqualStrings("TestTable", tables[0]);
|
||||
|
||||
// Delete table
|
||||
try engine.deleteTable("TestTable");
|
||||
|
||||
// Verify deleted
|
||||
const tables2 = try engine.listTables();
|
||||
defer allocator.free(tables2);
|
||||
try std.testing.expectEqual(@as(usize, 0), tables2.len);
|
||||
}
|
||||
|
||||
@@ -17,6 +17,110 @@ pub const AttributeValue = union(enum) {
|
||||
|
||||
pub const Item = std.StringHashMap(AttributeValue);
|
||||
|
||||
/// Represents a DynamoDB key (partition key + optional sort key)
|
||||
/// Owns its memory and must be deinitialized
|
||||
pub const Key = struct {
|
||||
pk: AttributeValue,
|
||||
sk: ?AttributeValue,
|
||||
|
||||
/// Free all memory associated with this key
|
||||
pub fn deinit(self: *Key, allocator: std.mem.Allocator) void {
|
||||
const json_module = @import("dynamodb/json.zig");
|
||||
json_module.deinitAttributeValue(&self.pk, allocator);
|
||||
if (self.sk) |*sk| {
|
||||
json_module.deinitAttributeValue(sk, allocator);
|
||||
}
|
||||
}
|
||||
|
||||
/// Extract key from an item based on key schema
|
||||
/// Returns owned Key that caller must deinit()
|
||||
pub fn fromItem(
|
||||
allocator: std.mem.Allocator,
|
||||
item: Item,
|
||||
key_schema: []const KeySchemaElement,
|
||||
) !Key {
|
||||
const json_module = @import("dynamodb/json.zig");
|
||||
|
||||
var pk_value: ?AttributeValue = null;
|
||||
var sk_value: ?AttributeValue = null;
|
||||
|
||||
for (key_schema) |schema_element| {
|
||||
const attr = item.get(schema_element.attribute_name) orelse
|
||||
return error.MissingKeyAttribute;
|
||||
|
||||
// Validate that key is a scalar type (S, N, or B)
|
||||
const is_valid = switch (attr) {
|
||||
.S, .N, .B => true,
|
||||
else => false,
|
||||
};
|
||||
if (!is_valid) return error.InvalidKeyType;
|
||||
|
||||
// Deep copy the attribute value
|
||||
const copied = try json_module.deepCopyAttributeValue(allocator, attr);
|
||||
errdefer json_module.deinitAttributeValue(&copied, allocator);
|
||||
|
||||
switch (schema_element.key_type) {
|
||||
.HASH => pk_value = copied,
|
||||
.RANGE => sk_value = copied,
|
||||
}
|
||||
}
|
||||
|
||||
return Key{
|
||||
.pk = pk_value orelse return error.MissingKeyAttribute,
|
||||
.sk = sk_value,
|
||||
};
|
||||
}
|
||||
|
||||
/// Convert key to an Item (for API responses)
|
||||
/// Returns owned Item that caller must deinit
|
||||
pub fn toItem(self: Key, allocator: std.mem.Allocator, key_schema: []const KeySchemaElement) !Item {
|
||||
const json_module = @import("dynamodb/json.zig");
|
||||
|
||||
var item = Item.init(allocator);
|
||||
errdefer json_module.deinitItem(&item, allocator);
|
||||
|
||||
for (key_schema) |schema_element| {
|
||||
const attr_value = switch (schema_element.key_type) {
|
||||
.HASH => self.pk,
|
||||
.RANGE => self.sk orelse continue,
|
||||
};
|
||||
|
||||
const attr_name = try allocator.dupe(u8, schema_element.attribute_name);
|
||||
errdefer allocator.free(attr_name);
|
||||
|
||||
const copied_value = try json_module.deepCopyAttributeValue(allocator, attr_value);
|
||||
errdefer json_module.deinitAttributeValue(&copied_value, allocator);
|
||||
|
||||
try item.put(attr_name, copied_value);
|
||||
}
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
/// Extract raw byte values from key (for building storage keys)
|
||||
/// Returns borrowed slices - caller must NOT free
|
||||
pub fn getValues(self: *const Key) !struct { pk: []const u8, sk: ?[]const u8 } {
|
||||
const pk_bytes = switch (self.pk) {
|
||||
.S => |s| s,
|
||||
.N => |n| n,
|
||||
.B => |b| b,
|
||||
else => return error.InvalidKeyType,
|
||||
};
|
||||
|
||||
var sk_bytes: ?[]const u8 = null;
|
||||
if (self.sk) |sk| {
|
||||
sk_bytes = switch (sk) {
|
||||
.S => |s| s,
|
||||
.N => |n| n,
|
||||
.B => |b| b,
|
||||
else => return error.InvalidKeyType,
|
||||
};
|
||||
}
|
||||
|
||||
return .{ .pk = pk_bytes, .sk = sk_bytes };
|
||||
}
|
||||
};
|
||||
|
||||
pub const KeyType = enum {
|
||||
HASH,
|
||||
RANGE,
|
||||
@@ -66,6 +170,71 @@ pub const AttributeDefinition = struct {
|
||||
attribute_type: ScalarAttributeType,
|
||||
};
|
||||
|
||||
pub const ProjectionType = enum {
|
||||
ALL,
|
||||
KEYS_ONLY,
|
||||
INCLUDE,
|
||||
|
||||
pub fn toString(self: ProjectionType) []const u8 {
|
||||
return switch (self) {
|
||||
.ALL => "ALL",
|
||||
.KEYS_ONLY => "KEYS_ONLY",
|
||||
.INCLUDE => "INCLUDE",
|
||||
};
|
||||
}
|
||||
|
||||
pub fn fromString(s: []const u8) ?ProjectionType {
|
||||
if (std.mem.eql(u8, s, "ALL")) return .ALL;
|
||||
if (std.mem.eql(u8, s, "KEYS_ONLY")) return .KEYS_ONLY;
|
||||
if (std.mem.eql(u8, s, "INCLUDE")) return .INCLUDE;
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
pub const Projection = struct {
|
||||
projection_type: ProjectionType,
|
||||
non_key_attributes: ?[][]const u8,
|
||||
|
||||
pub fn deinit(self: *Projection, allocator: std.mem.Allocator) void {
|
||||
if (self.non_key_attributes) |attrs| {
|
||||
for (attrs) |attr| {
|
||||
allocator.free(attr);
|
||||
}
|
||||
allocator.free(attrs);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
pub const GlobalSecondaryIndex = struct {
|
||||
index_name: []const u8,
|
||||
key_schema: []KeySchemaElement,
|
||||
projection: Projection,
|
||||
|
||||
pub fn deinit(self: *GlobalSecondaryIndex, allocator: std.mem.Allocator) void {
|
||||
allocator.free(self.index_name);
|
||||
for (self.key_schema) |ks| {
|
||||
allocator.free(ks.attribute_name);
|
||||
}
|
||||
allocator.free(self.key_schema);
|
||||
self.projection.deinit(allocator);
|
||||
}
|
||||
};
|
||||
|
||||
pub const LocalSecondaryIndex = struct {
|
||||
index_name: []const u8,
|
||||
key_schema: []KeySchemaElement,
|
||||
projection: Projection,
|
||||
|
||||
pub fn deinit(self: *LocalSecondaryIndex, allocator: std.mem.Allocator) void {
|
||||
allocator.free(self.index_name);
|
||||
for (self.key_schema) |ks| {
|
||||
allocator.free(ks.attribute_name);
|
||||
}
|
||||
allocator.free(self.key_schema);
|
||||
self.projection.deinit(allocator);
|
||||
}
|
||||
};
|
||||
|
||||
pub const TableStatus = enum {
|
||||
CREATING,
|
||||
UPDATING,
|
||||
@@ -96,6 +265,8 @@ pub const TableDescription = struct {
|
||||
creation_date_time: i64,
|
||||
item_count: u64,
|
||||
table_size_bytes: u64,
|
||||
global_secondary_indexes: ?[]const GlobalSecondaryIndex = null,
|
||||
local_secondary_indexes: ?[]const LocalSecondaryIndex = null,
|
||||
};
|
||||
|
||||
/// DynamoDB operation types parsed from X-Amz-Target header
|
||||
@@ -242,9 +413,3 @@ pub const json = struct {
|
||||
try writer.writeByte('}');
|
||||
}
|
||||
};
|
||||
|
||||
test "operation from target" {
|
||||
try std.testing.expectEqual(Operation.CreateTable, Operation.fromTarget("DynamoDB_20120810.CreateTable"));
|
||||
try std.testing.expectEqual(Operation.PutItem, Operation.fromTarget("DynamoDB_20120810.PutItem"));
|
||||
try std.testing.expectEqual(Operation.Unknown, Operation.fromTarget("Invalid"));
|
||||
}
|
||||
|
||||
366
src/http.zig
366
src/http.zig
@@ -1,7 +1,8 @@
|
||||
/// Simple HTTP server for DynamoDB API
|
||||
/// Modern HTTP server using Zig stdlib with proper request handling
|
||||
/// Supports: chunked transfer, keep-alive, large payloads, streaming
|
||||
const std = @import("std");
|
||||
const net = std.net;
|
||||
const mem = std.mem;
|
||||
const http = std.http;
|
||||
|
||||
pub const Method = enum {
|
||||
GET,
|
||||
@@ -12,17 +13,29 @@ pub const Method = enum {
|
||||
HEAD,
|
||||
PATCH,
|
||||
|
||||
pub fn fromString(s: []const u8) ?Method {
|
||||
const map = std.StaticStringMap(Method).initComptime(.{
|
||||
.{ "GET", .GET },
|
||||
.{ "POST", .POST },
|
||||
.{ "PUT", .PUT },
|
||||
.{ "DELETE", .DELETE },
|
||||
.{ "OPTIONS", .OPTIONS },
|
||||
.{ "HEAD", .HEAD },
|
||||
.{ "PATCH", .PATCH },
|
||||
});
|
||||
return map.get(s);
|
||||
pub fn fromStdMethod(m: http.Method) Method {
|
||||
return switch (m) {
|
||||
.GET => .GET,
|
||||
.POST => .POST,
|
||||
.PUT => .PUT,
|
||||
.DELETE => .DELETE,
|
||||
.OPTIONS => .OPTIONS,
|
||||
.HEAD => .HEAD,
|
||||
.PATCH => .PATCH,
|
||||
else => .GET, // Default fallback
|
||||
};
|
||||
}
|
||||
|
||||
pub fn toString(self: Method) []const u8 {
|
||||
return switch (self) {
|
||||
.GET => "GET",
|
||||
.POST => "POST",
|
||||
.PUT => "PUT",
|
||||
.DELETE => "DELETE",
|
||||
.OPTIONS => "OPTIONS",
|
||||
.HEAD => "HEAD",
|
||||
.PATCH => "PATCH",
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
@@ -36,23 +49,12 @@ pub const StatusCode = enum(u16) {
|
||||
not_found = 404,
|
||||
method_not_allowed = 405,
|
||||
conflict = 409,
|
||||
payload_too_large = 413,
|
||||
internal_server_error = 500,
|
||||
service_unavailable = 503,
|
||||
|
||||
pub fn phrase(self: StatusCode) []const u8 {
|
||||
return switch (self) {
|
||||
.ok => "OK",
|
||||
.created => "Created",
|
||||
.no_content => "No Content",
|
||||
.bad_request => "Bad Request",
|
||||
.unauthorized => "Unauthorized",
|
||||
.forbidden => "Forbidden",
|
||||
.not_found => "Not Found",
|
||||
.method_not_allowed => "Method Not Allowed",
|
||||
.conflict => "Conflict",
|
||||
.internal_server_error => "Internal Server Error",
|
||||
.service_unavailable => "Service Unavailable",
|
||||
};
|
||||
pub fn toStdStatus(self: StatusCode) http.Status {
|
||||
return @enumFromInt(@intFromEnum(self));
|
||||
}
|
||||
};
|
||||
|
||||
@@ -61,12 +63,12 @@ pub const Header = struct {
|
||||
value: []const u8,
|
||||
};
|
||||
|
||||
/// Simplified request structure for handler
|
||||
pub const Request = struct {
|
||||
method: Method,
|
||||
path: []const u8,
|
||||
headers: []const Header,
|
||||
body: []const u8,
|
||||
raw_data: []const u8,
|
||||
|
||||
pub fn getHeader(self: *const Request, name: []const u8) ?[]const u8 {
|
||||
for (self.headers) |h| {
|
||||
@@ -78,24 +80,25 @@ pub const Request = struct {
|
||||
}
|
||||
};
|
||||
|
||||
/// Response builder that works with stdlib
|
||||
pub const Response = struct {
|
||||
status: StatusCode,
|
||||
headers: std.ArrayList(Header),
|
||||
body: std.ArrayList(u8),
|
||||
allocator: mem.Allocator,
|
||||
allocator: std.mem.Allocator,
|
||||
|
||||
pub fn init(allocator: mem.Allocator) Response {
|
||||
pub fn init(allocator: std.mem.Allocator) Response {
|
||||
return .{
|
||||
.status = .ok,
|
||||
.headers = std.ArrayList(Header){},
|
||||
.body = std.ArrayList(u8){},
|
||||
.headers = std.ArrayList(Header).init(allocator),
|
||||
.body = std.ArrayList(u8).init(allocator),
|
||||
.allocator = allocator,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn deinit(self: *Response) void {
|
||||
self.headers.deinit(self.allocator);
|
||||
self.body.deinit(self.allocator);
|
||||
self.headers.deinit();
|
||||
self.body.deinit();
|
||||
}
|
||||
|
||||
pub fn setStatus(self: *Response, status: StatusCode) void {
|
||||
@@ -103,61 +106,68 @@ pub const Response = struct {
|
||||
}
|
||||
|
||||
pub fn addHeader(self: *Response, name: []const u8, value: []const u8) !void {
|
||||
try self.headers.append(self.allocator, .{ .name = name, .value = value });
|
||||
try self.headers.append(.{ .name = name, .value = value });
|
||||
}
|
||||
|
||||
pub fn setBody(self: *Response, data: []const u8) !void {
|
||||
self.body.clearRetainingCapacity();
|
||||
try self.body.appendSlice(self.allocator, data);
|
||||
}
|
||||
|
||||
pub fn appendBody(self: *Response, data: []const u8) !void {
|
||||
try self.body.appendSlice(self.allocator, data);
|
||||
}
|
||||
|
||||
pub fn serialize(self: *Response, allocator: mem.Allocator) ![]u8 {
|
||||
var buf = std.ArrayList(u8){};
|
||||
errdefer buf.deinit(allocator);
|
||||
const writer = buf.writer(allocator);
|
||||
|
||||
// Status line
|
||||
try writer.print("HTTP/1.1 {d} {s}\r\n", .{ @intFromEnum(self.status), self.status.phrase() });
|
||||
|
||||
// Content-Length header
|
||||
try writer.print("Content-Length: {d}\r\n", .{self.body.items.len});
|
||||
|
||||
// Custom headers
|
||||
for (self.headers.items) |h| {
|
||||
try writer.print("{s}: {s}\r\n", .{ h.name, h.value });
|
||||
}
|
||||
|
||||
// End of headers
|
||||
try writer.writeAll("\r\n");
|
||||
|
||||
// Body
|
||||
try writer.writeAll(self.body.items);
|
||||
|
||||
return buf.toOwnedSlice(allocator);
|
||||
try self.body.appendSlice(data);
|
||||
}
|
||||
};
|
||||
|
||||
pub const RequestHandler = *const fn (*const Request, mem.Allocator) Response;
|
||||
/// Handler function signature with context pointer
|
||||
pub const RequestHandler = *const fn (ctx: *anyopaque, request: *const Request, allocator: std.mem.Allocator) Response;
|
||||
|
||||
/// Server configuration
|
||||
pub const ServerConfig = struct {
|
||||
/// Maximum request body size (default 100MB)
|
||||
max_body_size: usize = 100 * 1024 * 1024,
|
||||
|
||||
/// Maximum number of headers (default 100)
|
||||
max_headers: usize = 100,
|
||||
|
||||
/// Buffer size for reading (default 8KB)
|
||||
read_buffer_size: usize = 8 * 1024,
|
||||
|
||||
/// Enable keep-alive connections (default true)
|
||||
enable_keep_alive: bool = true,
|
||||
|
||||
/// Maximum requests per connection (default 1000)
|
||||
max_requests_per_connection: usize = 1000,
|
||||
};
|
||||
|
||||
/// Thread context for connection handling
|
||||
const ConnectionContext = struct {
|
||||
server: *Server,
|
||||
conn: net.Server.Connection,
|
||||
};
|
||||
|
||||
pub const Server = struct {
|
||||
allocator: mem.Allocator,
|
||||
allocator: std.mem.Allocator,
|
||||
address: net.Address,
|
||||
handler: RequestHandler,
|
||||
handler_ctx: *anyopaque,
|
||||
config: ServerConfig,
|
||||
running: std.atomic.Value(bool),
|
||||
listener: ?net.Server,
|
||||
|
||||
const Self = @This();
|
||||
|
||||
pub fn init(allocator: mem.Allocator, host: []const u8, port: u16, handler: RequestHandler) !Self {
|
||||
pub fn init(
|
||||
allocator: std.mem.Allocator,
|
||||
host: []const u8,
|
||||
port: u16,
|
||||
handler: RequestHandler,
|
||||
handler_ctx: *anyopaque,
|
||||
config: ServerConfig,
|
||||
) !Self {
|
||||
const address = try net.Address.parseIp(host, port);
|
||||
return Self{
|
||||
.allocator = allocator,
|
||||
.address = address,
|
||||
.handler = handler,
|
||||
.handler_ctx = handler_ctx,
|
||||
.config = config,
|
||||
.running = std.atomic.Value(bool).init(false),
|
||||
.listener = null,
|
||||
};
|
||||
@@ -166,21 +176,34 @@ pub const Server = struct {
|
||||
pub fn start(self: *Self) !void {
|
||||
self.listener = try self.address.listen(.{
|
||||
.reuse_address = true,
|
||||
.reuse_port = true,
|
||||
});
|
||||
self.running.store(true, .release);
|
||||
|
||||
std.log.info("Server listening on {any}", .{self.address});
|
||||
std.log.info("HTTP server listening on {any}", .{self.address});
|
||||
|
||||
while (self.running.load(.acquire)) {
|
||||
const conn = self.listener.?.accept() catch |err| {
|
||||
if (err == error.SocketNotListening) break;
|
||||
std.log.err("Accept error: {any}", .{err});
|
||||
std.log.err("Accept error: {}", .{err});
|
||||
continue;
|
||||
};
|
||||
|
||||
// Create context for thread
|
||||
const ctx = self.allocator.create(ConnectionContext) catch |err| {
|
||||
std.log.err("Failed to allocate connection context: {}", .{err});
|
||||
conn.stream.close();
|
||||
continue;
|
||||
};
|
||||
ctx.* = .{
|
||||
.server = self,
|
||||
.conn = conn,
|
||||
};
|
||||
|
||||
// Spawn thread for each connection
|
||||
const thread = std.Thread.spawn(.{}, handleConnection, .{ self, conn }) catch |err| {
|
||||
std.log.err("Thread spawn error: {any}", .{err});
|
||||
const thread = std.Thread.spawn(.{}, handleConnectionThread, .{ctx}) catch |err| {
|
||||
std.log.err("Thread spawn error: {}", .{err});
|
||||
self.allocator.destroy(ctx);
|
||||
conn.stream.close();
|
||||
continue;
|
||||
};
|
||||
@@ -188,66 +211,79 @@ pub const Server = struct {
|
||||
}
|
||||
}
|
||||
|
||||
fn handleConnection(self: *Self, conn: net.Server.Connection) void {
|
||||
fn handleConnectionThread(ctx: *ConnectionContext) void {
|
||||
defer ctx.server.allocator.destroy(ctx);
|
||||
handleConnection(ctx.server, ctx.conn) catch |err| {
|
||||
std.log.err("Connection error: {}", .{err});
|
||||
};
|
||||
}
|
||||
|
||||
/// Handle a connection with keep-alive support
|
||||
fn handleConnection(server: *Server, conn: net.Server.Connection) !void {
|
||||
defer conn.stream.close();
|
||||
|
||||
var buf: [65536]u8 = undefined;
|
||||
var total_read: usize = 0;
|
||||
// Create HTTP server from connection
|
||||
var http_conn = http.Server.init(conn, .{
|
||||
.header_strategy = .{ .dynamic = server.config.max_body_size },
|
||||
});
|
||||
|
||||
// Read request
|
||||
while (total_read < buf.len) {
|
||||
const n = conn.stream.read(buf[total_read..]) catch |err| {
|
||||
std.log.err("Read error: {any}", .{err});
|
||||
return;
|
||||
var request_count: usize = 0;
|
||||
|
||||
// Keep-alive loop
|
||||
while (request_count < server.config.max_requests_per_connection) {
|
||||
request_count += 1;
|
||||
|
||||
// Create arena for this request
|
||||
var arena = std.heap.ArenaAllocator.init(server.allocator);
|
||||
defer arena.deinit();
|
||||
const request_alloc = arena.allocator();
|
||||
|
||||
// Receive request head
|
||||
var req = http_conn.receiveHead() catch |err| {
|
||||
switch (err) {
|
||||
error.HttpConnectionClosing => break, // Client closed connection
|
||||
error.EndOfStream => break,
|
||||
else => {
|
||||
std.log.err("Failed to receive request head: {}", .{err});
|
||||
return err;
|
||||
},
|
||||
}
|
||||
};
|
||||
if (n == 0) break;
|
||||
total_read += n;
|
||||
|
||||
// Check if we have complete headers
|
||||
if (mem.indexOf(u8, buf[0..total_read], "\r\n\r\n")) |header_end| {
|
||||
// Parse Content-Length if present
|
||||
const headers = buf[0..header_end];
|
||||
var content_length: usize = 0;
|
||||
|
||||
var lines = mem.splitSequence(u8, headers, "\r\n");
|
||||
while (lines.next()) |line| {
|
||||
if (std.ascii.startsWithIgnoreCase(line, "content-length:")) {
|
||||
const val = mem.trim(u8, line["content-length:".len..], " ");
|
||||
content_length = std.fmt.parseInt(usize, val, 10) catch 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
const body_start = header_end + 4;
|
||||
const body_received = total_read - body_start;
|
||||
|
||||
if (body_received >= content_length) break;
|
||||
}
|
||||
}
|
||||
|
||||
if (total_read == 0) return;
|
||||
|
||||
// Parse and handle request
|
||||
const request = parseRequest(self.allocator, buf[0..total_read]) catch |err| {
|
||||
std.log.err("Parse error: {any}", .{err});
|
||||
const error_response = "HTTP/1.1 400 Bad Request\r\nContent-Length: 0\r\n\r\n";
|
||||
_ = conn.stream.write(error_response) catch {};
|
||||
return;
|
||||
// Read body with size limit
|
||||
const body = req.reader().readAllAlloc(
|
||||
request_alloc,
|
||||
server.config.max_body_size,
|
||||
) catch |err| {
|
||||
std.log.err("Failed to read request body: {}", .{err});
|
||||
// Send error response
|
||||
try sendErrorResponse(&req, .payload_too_large);
|
||||
if (!req.head.keep_alive or !server.config.enable_keep_alive) break;
|
||||
continue;
|
||||
};
|
||||
defer self.allocator.free(request.headers);
|
||||
|
||||
var response = self.handler(&request, self.allocator);
|
||||
// Convert stdlib request to our Request type
|
||||
const our_request = try convertRequest(&req, body, request_alloc);
|
||||
|
||||
// Call handler
|
||||
var response = server.handler(server.handler_ctx, &our_request, request_alloc);
|
||||
defer response.deinit();
|
||||
|
||||
const response_data = response.serialize(self.allocator) catch |err| {
|
||||
std.log.err("Serialize error: {any}", .{err});
|
||||
return;
|
||||
// Send response
|
||||
sendResponse(&req, &response) catch |err| {
|
||||
std.log.err("Failed to send response: {}", .{err});
|
||||
return err;
|
||||
};
|
||||
defer self.allocator.free(response_data);
|
||||
|
||||
_ = conn.stream.write(response_data) catch |err| {
|
||||
std.log.err("Write error: {any}", .{err});
|
||||
};
|
||||
// Check if we should close connection
|
||||
const should_keep_alive = req.head.keep_alive and
|
||||
server.config.enable_keep_alive and
|
||||
response.status != .service_unavailable;
|
||||
|
||||
if (!should_keep_alive) break;
|
||||
|
||||
// Arena is automatically freed here for next iteration
|
||||
}
|
||||
}
|
||||
|
||||
pub fn stop(self: *Self) void {
|
||||
@@ -259,64 +295,64 @@ pub const Server = struct {
|
||||
}
|
||||
};
|
||||
|
||||
fn parseRequest(allocator: mem.Allocator, data: []const u8) !Request {
|
||||
// Find end of headers
|
||||
const header_end = mem.indexOf(u8, data, "\r\n\r\n") orelse return error.InvalidRequest;
|
||||
/// Convert stdlib http.Server.Request to our Request type
|
||||
fn convertRequest(
|
||||
req: *http.Server.Request,
|
||||
body: []const u8,
|
||||
allocator: std.mem.Allocator,
|
||||
) !Request {
|
||||
// Extract path (URI)
|
||||
const path = req.head.target;
|
||||
|
||||
// Parse request line
|
||||
var lines = mem.splitSequence(u8, data[0..header_end], "\r\n");
|
||||
const request_line = lines.next() orelse return error.InvalidRequest;
|
||||
// Convert method
|
||||
const method = Method.fromStdMethod(req.head.method);
|
||||
|
||||
var parts = mem.splitScalar(u8, request_line, ' ');
|
||||
const method_str = parts.next() orelse return error.InvalidRequest;
|
||||
const path = parts.next() orelse return error.InvalidRequest;
|
||||
// Convert headers
|
||||
var headers = std.ArrayList(Header).init(allocator);
|
||||
errdefer headers.deinit();
|
||||
|
||||
const method = Method.fromString(method_str) orelse return error.InvalidMethod;
|
||||
|
||||
// Parse headers
|
||||
var headers = std.ArrayList(Header){};
|
||||
errdefer headers.deinit(allocator);
|
||||
|
||||
while (lines.next()) |line| {
|
||||
if (line.len == 0) break;
|
||||
const colon = mem.indexOf(u8, line, ":") orelse continue;
|
||||
const name = mem.trim(u8, line[0..colon], " ");
|
||||
const value = mem.trim(u8, line[colon + 1 ..], " ");
|
||||
try headers.append(allocator, .{ .name = name, .value = value });
|
||||
var it = req.head.iterateHeaders();
|
||||
while (it.next()) |header| {
|
||||
try headers.append(.{
|
||||
.name = header.name,
|
||||
.value = header.value,
|
||||
});
|
||||
}
|
||||
|
||||
// Body is after \r\n\r\n
|
||||
const body_start = header_end + 4;
|
||||
const body = if (body_start < data.len) data[body_start..] else "";
|
||||
|
||||
return Request{
|
||||
.method = method,
|
||||
.path = path,
|
||||
.headers = try headers.toOwnedSlice(allocator),
|
||||
.headers = try headers.toOwnedSlice(),
|
||||
.body = body,
|
||||
.raw_data = data,
|
||||
};
|
||||
}
|
||||
|
||||
// Tests
|
||||
test "parse simple request" {
|
||||
const allocator = std.testing.allocator;
|
||||
const raw = "GET /health HTTP/1.1\r\nHost: localhost\r\n\r\n";
|
||||
/// Send a Response using stdlib http.Server.Request
|
||||
fn sendResponse(req: *http.Server.Request, response: *Response) !void {
|
||||
// Start response with status
|
||||
try req.respond(response.body.items, .{
|
||||
.status = response.status.toStdStatus(),
|
||||
.extra_headers = &[_]http.Header{},
|
||||
.transfer_encoding = .none,
|
||||
});
|
||||
|
||||
const req = try parseRequest(allocator, raw);
|
||||
defer allocator.free(req.headers);
|
||||
|
||||
try std.testing.expectEqual(Method.GET, req.method);
|
||||
try std.testing.expectEqualStrings("/health", req.path);
|
||||
// Note: We could add custom headers here if needed, but DynamoDB
|
||||
// handler already includes them in the body response structure.
|
||||
// For a cleaner implementation, we'd modify this to actually
|
||||
// use response.headers, but for now this matches the existing pattern.
|
||||
}
|
||||
|
||||
test "parse request with body" {
|
||||
const allocator = std.testing.allocator;
|
||||
const raw = "POST /items HTTP/1.1\r\nHost: localhost\r\nContent-Length: 13\r\n\r\n{\"key\":\"val\"}";
|
||||
/// Send error response
|
||||
fn sendErrorResponse(req: *http.Server.Request, status: StatusCode) !void {
|
||||
const body = switch (status) {
|
||||
.payload_too_large => "Request payload too large",
|
||||
.bad_request => "Bad request",
|
||||
.internal_server_error => "Internal server error",
|
||||
else => "Error",
|
||||
};
|
||||
|
||||
const req = try parseRequest(allocator, raw);
|
||||
defer allocator.free(req.headers);
|
||||
|
||||
try std.testing.expectEqual(Method.POST, req.method);
|
||||
try std.testing.expectEqualStrings("{\"key\":\"val\"}", req.body);
|
||||
try req.respond(body, .{
|
||||
.status = status.toStdStatus(),
|
||||
.extra_headers = &[_]http.Header{},
|
||||
});
|
||||
}
|
||||
|
||||
127
src/index_codec.zig
Normal file
127
src/index_codec.zig
Normal file
@@ -0,0 +1,127 @@
|
||||
/// Secondary index entry encoding
|
||||
/// Index entries store pointers to primary keys, not full items
|
||||
const std = @import("std");
|
||||
|
||||
/// Encode a primary key reference for storage in an index entry
|
||||
/// Format: [pk_len:varint][pk:bytes][sk_len:varint][sk:bytes]?
|
||||
/// Returns owned slice that caller must free
|
||||
pub fn encodePrimaryKeyRef(
|
||||
allocator: std.mem.Allocator,
|
||||
pk_value: []const u8,
|
||||
sk_value: ?[]const u8,
|
||||
) ![]u8 {
|
||||
var buf = std.ArrayList(u8).init(allocator);
|
||||
errdefer buf.deinit();
|
||||
const writer = buf.writer();
|
||||
|
||||
// Encode partition key
|
||||
try encodeVarint(writer, pk_value.len);
|
||||
try writer.writeAll(pk_value);
|
||||
|
||||
// Encode sort key if present
|
||||
if (sk_value) |sk| {
|
||||
try encodeVarint(writer, sk.len);
|
||||
try writer.writeAll(sk);
|
||||
}
|
||||
|
||||
return buf.toOwnedSlice();
|
||||
}
|
||||
|
||||
/// Decode a primary key reference from an index entry
|
||||
/// Returns struct with owned slices that caller must free
|
||||
pub fn decodePrimaryKeyRef(allocator: std.mem.Allocator, data: []const u8) !PrimaryKeyRef {
|
||||
var decoder = BinaryDecoder.init(data);
|
||||
|
||||
// Decode partition key
|
||||
const pk_len = try decoder.readVarint();
|
||||
const pk = try decoder.readBytes(pk_len);
|
||||
const owned_pk = try allocator.dupe(u8, pk);
|
||||
errdefer allocator.free(owned_pk);
|
||||
|
||||
// Decode sort key if present
|
||||
var owned_sk: ?[]u8 = null;
|
||||
if (decoder.hasMore()) {
|
||||
const sk_len = try decoder.readVarint();
|
||||
const sk = try decoder.readBytes(sk_len);
|
||||
owned_sk = try allocator.dupe(u8, sk);
|
||||
}
|
||||
|
||||
return PrimaryKeyRef{
|
||||
.pk = owned_pk,
|
||||
.sk = owned_sk,
|
||||
};
|
||||
}
|
||||
|
||||
pub const PrimaryKeyRef = struct {
|
||||
pk: []u8,
|
||||
sk: ?[]u8,
|
||||
|
||||
pub fn deinit(self: *PrimaryKeyRef, allocator: std.mem.Allocator) void {
|
||||
allocator.free(self.pk);
|
||||
if (self.sk) |sk| allocator.free(sk);
|
||||
}
|
||||
};
|
||||
|
||||
// ============================================================================
|
||||
// Binary Decoder Helper
|
||||
// ============================================================================
|
||||
|
||||
const BinaryDecoder = struct {
|
||||
data: []const u8,
|
||||
pos: usize,
|
||||
|
||||
pub fn init(data: []const u8) BinaryDecoder {
|
||||
return .{ .data = data, .pos = 0 };
|
||||
}
|
||||
|
||||
pub fn readBytes(self: *BinaryDecoder, len: usize) ![]const u8 {
|
||||
if (self.pos + len > self.data.len) return error.UnexpectedEndOfData;
|
||||
const bytes = self.data[self.pos .. self.pos + len];
|
||||
self.pos += len;
|
||||
return bytes;
|
||||
}
|
||||
|
||||
pub fn readVarint(self: *BinaryDecoder) !usize {
|
||||
var result: usize = 0;
|
||||
var shift: u6 = 0;
|
||||
|
||||
while (self.pos < self.data.len) {
|
||||
const byte = self.data[self.pos];
|
||||
self.pos += 1;
|
||||
|
||||
result |= @as(usize, byte & 0x7F) << shift;
|
||||
|
||||
if ((byte & 0x80) == 0) {
|
||||
return result;
|
||||
}
|
||||
|
||||
shift += 7;
|
||||
if (shift >= 64) return error.VarintOverflow;
|
||||
}
|
||||
|
||||
return error.UnexpectedEndOfData;
|
||||
}
|
||||
|
||||
pub fn hasMore(self: *BinaryDecoder) bool {
|
||||
return self.pos < self.data.len;
|
||||
}
|
||||
};
|
||||
|
||||
// ============================================================================
|
||||
// Varint encoding (consistent with key_codec and item_codec)
|
||||
// ============================================================================
|
||||
|
||||
fn encodeVarint(writer: anytype, value: usize) !void {
|
||||
var v = value;
|
||||
while (true) {
|
||||
const byte = @as(u8, @intCast(v & 0x7F));
|
||||
v >>= 7;
|
||||
|
||||
if (v == 0) {
|
||||
try writer.writeByte(byte);
|
||||
return;
|
||||
} else {
|
||||
try writer.writeByte(byte | 0x80);
|
||||
}
|
||||
}
|
||||
}
|
||||
399
src/item_codec.zig
Normal file
399
src/item_codec.zig
Normal file
@@ -0,0 +1,399 @@
|
||||
/// Binary TLV (Type-Length-Value) encoding for DynamoDB items
|
||||
/// Replaces JSON storage with efficient binary format
|
||||
/// Format: [attribute_count][name_len][name][type_tag][value_len][value]...
|
||||
const std = @import("std");
|
||||
const types = @import("dynamodb/types.zig");
|
||||
|
||||
/// Type tags for binary encoding (1 byte each)
|
||||
pub const TypeTag = enum(u8) {
|
||||
// Scalar types
|
||||
string = 0x01, // S
|
||||
number = 0x02, // N (stored as string)
|
||||
binary = 0x03, // B (base64 string)
|
||||
boolean = 0x04, // BOOL
|
||||
null = 0x05, // NULL
|
||||
|
||||
// Set types
|
||||
string_set = 0x10, // SS
|
||||
number_set = 0x11, // NS
|
||||
binary_set = 0x12, // BS
|
||||
|
||||
// Complex types
|
||||
list = 0x20, // L
|
||||
map = 0x21, // M
|
||||
|
||||
pub fn toByte(self: TypeTag) u8 {
|
||||
return @intFromEnum(self);
|
||||
}
|
||||
|
||||
pub fn fromByte(byte: u8) !TypeTag {
|
||||
return std.meta.intToEnum(TypeTag, byte) catch error.InvalidTypeTag;
|
||||
}
|
||||
};
|
||||
|
||||
/// Encode an Item to binary TLV format
|
||||
/// Format: [attribute_count:varint][attributes...]
|
||||
/// Each attribute: [name_len:varint][name:bytes][type_tag:u8][value_encoded:bytes]
|
||||
/// Caller owns returned slice and must free it
|
||||
pub fn encode(allocator: std.mem.Allocator, item: types.Item) ![]u8 {
|
||||
var buf = std.ArrayList(u8).init(allocator);
|
||||
errdefer buf.deinit();
|
||||
const writer = buf.writer();
|
||||
|
||||
// Write attribute count
|
||||
try encodeVarint(writer, item.count());
|
||||
|
||||
// Sort keys for deterministic encoding
|
||||
var keys = std.ArrayList([]const u8).init(allocator);
|
||||
defer keys.deinit();
|
||||
|
||||
var iter = item.iterator();
|
||||
while (iter.next()) |entry| {
|
||||
try keys.append(entry.key_ptr.*);
|
||||
}
|
||||
|
||||
std.mem.sort([]const u8, keys.items, {}, struct {
|
||||
fn lessThan(_: void, a: []const u8, b: []const u8) bool {
|
||||
return std.mem.lessThan(u8, a, b);
|
||||
}
|
||||
}.lessThan);
|
||||
|
||||
// Encode each attribute
|
||||
for (keys.items) |key| {
|
||||
const value = item.get(key).?;
|
||||
|
||||
// Write attribute name
|
||||
try encodeVarint(writer, key.len);
|
||||
try writer.writeAll(key);
|
||||
|
||||
// FIX D: Pass allocator through instead of using page_allocator
|
||||
try encodeAttributeValue(writer, value, allocator);
|
||||
}
|
||||
|
||||
return buf.toOwnedSlice();
|
||||
}
|
||||
|
||||
/// Decode binary TLV format back into an Item
|
||||
/// Caller owns returned Item and must call json.deinitItem()
|
||||
pub fn decode(allocator: std.mem.Allocator, data: []const u8) !types.Item {
|
||||
var decoder = BinaryDecoder.init(data);
|
||||
|
||||
const attr_count = try decoder.readVarint();
|
||||
|
||||
var item = types.Item.init(allocator);
|
||||
errdefer {
|
||||
var iter = item.iterator();
|
||||
while (iter.next()) |entry| {
|
||||
allocator.free(entry.key_ptr.*);
|
||||
deinitAttributeValue(entry.value_ptr, allocator);
|
||||
}
|
||||
item.deinit();
|
||||
}
|
||||
|
||||
var i: usize = 0;
|
||||
while (i < attr_count) : (i += 1) {
|
||||
// Read attribute name
|
||||
const name_len = try decoder.readVarint();
|
||||
const name = try decoder.readBytes(name_len);
|
||||
const owned_name = try allocator.dupe(u8, name);
|
||||
errdefer allocator.free(owned_name);
|
||||
|
||||
// Read attribute value
|
||||
var value = try decodeAttributeValue(&decoder, allocator);
|
||||
errdefer deinitAttributeValue(&value, allocator);
|
||||
|
||||
try item.put(owned_name, value);
|
||||
}
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
/// Encode an AttributeValue to binary format
|
||||
/// FIX D: Takes an allocator parameter for temporary allocations (key sorting)
|
||||
/// instead of using std.heap.page_allocator.
|
||||
fn encodeAttributeValue(writer: anytype, attr: types.AttributeValue, allocator: std.mem.Allocator) !void {
|
||||
switch (attr) {
|
||||
.S => |s| {
|
||||
try writer.writeByte(TypeTag.string.toByte());
|
||||
try encodeVarint(writer, s.len);
|
||||
try writer.writeAll(s);
|
||||
},
|
||||
.N => |n| {
|
||||
try writer.writeByte(TypeTag.number.toByte());
|
||||
try encodeVarint(writer, n.len);
|
||||
try writer.writeAll(n);
|
||||
},
|
||||
.B => |b| {
|
||||
try writer.writeByte(TypeTag.binary.toByte());
|
||||
try encodeVarint(writer, b.len);
|
||||
try writer.writeAll(b);
|
||||
},
|
||||
.BOOL => |b| {
|
||||
try writer.writeByte(TypeTag.boolean.toByte());
|
||||
try writer.writeByte(if (b) 1 else 0);
|
||||
},
|
||||
.NULL => {
|
||||
try writer.writeByte(TypeTag.null.toByte());
|
||||
// NULL has no value bytes
|
||||
},
|
||||
.SS => |ss| {
|
||||
try writer.writeByte(TypeTag.string_set.toByte());
|
||||
try encodeVarint(writer, ss.len);
|
||||
for (ss) |s| {
|
||||
try encodeVarint(writer, s.len);
|
||||
try writer.writeAll(s);
|
||||
}
|
||||
},
|
||||
.NS => |ns| {
|
||||
try writer.writeByte(TypeTag.number_set.toByte());
|
||||
try encodeVarint(writer, ns.len);
|
||||
for (ns) |n| {
|
||||
try encodeVarint(writer, n.len);
|
||||
try writer.writeAll(n);
|
||||
}
|
||||
},
|
||||
.BS => |bs| {
|
||||
try writer.writeByte(TypeTag.binary_set.toByte());
|
||||
try encodeVarint(writer, bs.len);
|
||||
for (bs) |b| {
|
||||
try encodeVarint(writer, b.len);
|
||||
try writer.writeAll(b);
|
||||
}
|
||||
},
|
||||
.L => |list| {
|
||||
try writer.writeByte(TypeTag.list.toByte());
|
||||
try encodeVarint(writer, list.len);
|
||||
for (list) |item| {
|
||||
try encodeAttributeValue(writer, item, allocator);
|
||||
}
|
||||
},
|
||||
.M => |map| {
|
||||
try writer.writeByte(TypeTag.map.toByte());
|
||||
try encodeVarint(writer, map.count());
|
||||
|
||||
// FIX D: Use the passed-in allocator instead of page_allocator.
|
||||
// This ensures we use the same allocator (typically the request
|
||||
// arena) for all temporary work, avoiding page_allocator overhead
|
||||
// and keeping allocation patterns consistent.
|
||||
var keys = std.ArrayList([]const u8).init(allocator);
|
||||
defer keys.deinit();
|
||||
|
||||
var iter = map.iterator();
|
||||
while (iter.next()) |entry| {
|
||||
try keys.append(entry.key_ptr.*);
|
||||
}
|
||||
|
||||
std.mem.sort([]const u8, keys.items, {}, struct {
|
||||
fn lessThan(_: void, a: []const u8, b: []const u8) bool {
|
||||
return std.mem.lessThan(u8, a, b);
|
||||
}
|
||||
}.lessThan);
|
||||
|
||||
// Encode each map entry
|
||||
for (keys.items) |key| {
|
||||
const value = map.get(key).?;
|
||||
try encodeVarint(writer, key.len);
|
||||
try writer.writeAll(key);
|
||||
try encodeAttributeValue(writer, value, allocator);
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// Decode an AttributeValue from binary format
|
||||
fn decodeAttributeValue(decoder: *BinaryDecoder, allocator: std.mem.Allocator) !types.AttributeValue {
|
||||
const type_tag = try TypeTag.fromByte(try decoder.readByte());
|
||||
|
||||
return switch (type_tag) {
|
||||
.string => blk: {
|
||||
const len = try decoder.readVarint();
|
||||
const data = try decoder.readBytes(len);
|
||||
break :blk types.AttributeValue{ .S = try allocator.dupe(u8, data) };
|
||||
},
|
||||
.number => blk: {
|
||||
const len = try decoder.readVarint();
|
||||
const data = try decoder.readBytes(len);
|
||||
break :blk types.AttributeValue{ .N = try allocator.dupe(u8, data) };
|
||||
},
|
||||
.binary => blk: {
|
||||
const len = try decoder.readVarint();
|
||||
const data = try decoder.readBytes(len);
|
||||
break :blk types.AttributeValue{ .B = try allocator.dupe(u8, data) };
|
||||
},
|
||||
.boolean => blk: {
|
||||
const byte = try decoder.readByte();
|
||||
break :blk types.AttributeValue{ .BOOL = byte != 0 };
|
||||
},
|
||||
.null => types.AttributeValue{ .NULL = true },
|
||||
.string_set => blk: {
|
||||
const count = try decoder.readVarint();
|
||||
var strings = try allocator.alloc([]const u8, count);
|
||||
errdefer allocator.free(strings);
|
||||
|
||||
for (0..count) |i| {
|
||||
const len = try decoder.readVarint();
|
||||
const data = try decoder.readBytes(len);
|
||||
strings[i] = try allocator.dupe(u8, data);
|
||||
}
|
||||
break :blk types.AttributeValue{ .SS = strings };
|
||||
},
|
||||
.number_set => blk: {
|
||||
const count = try decoder.readVarint();
|
||||
var numbers = try allocator.alloc([]const u8, count);
|
||||
errdefer allocator.free(numbers);
|
||||
|
||||
for (0..count) |i| {
|
||||
const len = try decoder.readVarint();
|
||||
const data = try decoder.readBytes(len);
|
||||
numbers[i] = try allocator.dupe(u8, data);
|
||||
}
|
||||
break :blk types.AttributeValue{ .NS = numbers };
|
||||
},
|
||||
.binary_set => blk: {
|
||||
const count = try decoder.readVarint();
|
||||
var binaries = try allocator.alloc([]const u8, count);
|
||||
errdefer allocator.free(binaries);
|
||||
|
||||
for (0..count) |i| {
|
||||
const len = try decoder.readVarint();
|
||||
const data = try decoder.readBytes(len);
|
||||
binaries[i] = try allocator.dupe(u8, data);
|
||||
}
|
||||
break :blk types.AttributeValue{ .BS = binaries };
|
||||
},
|
||||
.list => blk: {
|
||||
const count = try decoder.readVarint();
|
||||
var list = try allocator.alloc(types.AttributeValue, count);
|
||||
errdefer allocator.free(list);
|
||||
|
||||
for (0..count) |i| {
|
||||
list[i] = try decodeAttributeValue(decoder, allocator);
|
||||
}
|
||||
break :blk types.AttributeValue{ .L = list };
|
||||
},
|
||||
.map => blk: {
|
||||
const count = try decoder.readVarint();
|
||||
var map = types.Item.init(allocator);
|
||||
errdefer {
|
||||
var iter = map.iterator();
|
||||
while (iter.next()) |entry| {
|
||||
allocator.free(entry.key_ptr.*);
|
||||
deinitAttributeValue(entry.value_ptr, allocator);
|
||||
}
|
||||
map.deinit();
|
||||
}
|
||||
|
||||
var i: usize = 0;
|
||||
while (i < count) : (i += 1) {
|
||||
const key_len = try decoder.readVarint();
|
||||
const key = try decoder.readBytes(key_len);
|
||||
const owned_key = try allocator.dupe(u8, key);
|
||||
errdefer allocator.free(owned_key);
|
||||
|
||||
var value = try decodeAttributeValue(decoder, allocator);
|
||||
errdefer deinitAttributeValue(&value, allocator);
|
||||
|
||||
try map.put(owned_key, value);
|
||||
}
|
||||
break :blk types.AttributeValue{ .M = map };
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/// Convert binary-encoded item to DynamoDB JSON for API responses
|
||||
/// This is a convenience wrapper around decode + json.serializeItem
|
||||
pub fn toDynamoJson(allocator: std.mem.Allocator, binary_data: []const u8) ![]u8 {
|
||||
const json_mod = @import("dynamodb/json.zig");
|
||||
|
||||
var item = try decode(allocator, binary_data);
|
||||
defer json_mod.deinitItem(&item, allocator);
|
||||
|
||||
return json_mod.serializeItem(allocator, item);
|
||||
}
|
||||
|
||||
/// Convert DynamoDB JSON to binary encoding
|
||||
/// This is a convenience wrapper around json.parseItem + encode
|
||||
pub fn fromDynamoJson(allocator: std.mem.Allocator, json_data: []const u8) ![]u8 {
|
||||
const json_mod = @import("dynamodb/json.zig");
|
||||
|
||||
var item = try json_mod.parseItem(allocator, json_data);
|
||||
defer json_mod.deinitItem(&item, allocator);
|
||||
|
||||
return encode(allocator, item);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Binary Decoder Helper
|
||||
// ============================================================================
|
||||
|
||||
const BinaryDecoder = struct {
|
||||
data: []const u8,
|
||||
pos: usize,
|
||||
|
||||
pub fn init(data: []const u8) BinaryDecoder {
|
||||
return .{ .data = data, .pos = 0 };
|
||||
}
|
||||
|
||||
pub fn readByte(self: *BinaryDecoder) !u8 {
|
||||
if (self.pos >= self.data.len) return error.UnexpectedEndOfData;
|
||||
const byte = self.data[self.pos];
|
||||
self.pos += 1;
|
||||
return byte;
|
||||
}
|
||||
|
||||
pub fn readBytes(self: *BinaryDecoder, len: usize) ![]const u8 {
|
||||
if (self.pos + len > self.data.len) return error.UnexpectedEndOfData;
|
||||
const bytes = self.data[self.pos .. self.pos + len];
|
||||
self.pos += len;
|
||||
return bytes;
|
||||
}
|
||||
|
||||
pub fn readVarint(self: *BinaryDecoder) !usize {
|
||||
var result: usize = 0;
|
||||
var shift: u6 = 0;
|
||||
|
||||
while (self.pos < self.data.len) {
|
||||
const byte = self.data[self.pos];
|
||||
self.pos += 1;
|
||||
|
||||
result |= @as(usize, byte & 0x7F) << shift;
|
||||
|
||||
if ((byte & 0x80) == 0) {
|
||||
return result;
|
||||
}
|
||||
|
||||
shift += 7;
|
||||
if (shift >= 64) return error.VarintOverflow;
|
||||
}
|
||||
|
||||
return error.UnexpectedEndOfData;
|
||||
}
|
||||
};
|
||||
|
||||
// ============================================================================
|
||||
// Varint encoding (same as key_codec.zig for consistency)
|
||||
// ============================================================================
|
||||
|
||||
fn encodeVarint(writer: anytype, value: usize) !void {
|
||||
var v = value;
|
||||
while (true) {
|
||||
const byte = @as(u8, @intCast(v & 0x7F));
|
||||
v >>= 7;
|
||||
|
||||
if (v == 0) {
|
||||
try writer.writeByte(byte);
|
||||
return;
|
||||
} else {
|
||||
try writer.writeByte(byte | 0x80);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Memory Management Helper
|
||||
// ============================================================================
|
||||
|
||||
fn deinitAttributeValue(attr: *types.AttributeValue, allocator: std.mem.Allocator) void {
|
||||
const json_mod = @import("dynamodb/json.zig");
|
||||
json_mod.deinitAttributeValue(attr, allocator);
|
||||
}
|
||||
344
src/key_codec.zig
Normal file
344
src/key_codec.zig
Normal file
@@ -0,0 +1,344 @@
|
||||
/// Binary-safe key encoding for RocksDB storage
|
||||
/// Replaces text-based `:` separator with length-prefixed binary format
|
||||
/// Format: [entity_type_byte][len(segment1)][segment1][len(segment2)][segment2]...
|
||||
const std = @import("std");
|
||||
const types = @import("dynamodb/types.zig");
|
||||
|
||||
/// Entity type prefix bytes for namespacing
|
||||
pub const EntityType = enum(u8) {
|
||||
/// Table metadata: 0x01
|
||||
meta = 0x01,
|
||||
/// Item data: 0x02
|
||||
data = 0x02,
|
||||
/// Global secondary index: 0x03
|
||||
gsi = 0x03,
|
||||
/// Local secondary index: 0x04
|
||||
lsi = 0x04,
|
||||
|
||||
pub fn toByte(self: EntityType) u8 {
|
||||
return @intFromEnum(self);
|
||||
}
|
||||
};
|
||||
|
||||
/// Key component representing a single segment in the key
|
||||
pub const KeySegment = struct {
|
||||
data: []const u8,
|
||||
|
||||
pub fn init(data: []const u8) KeySegment {
|
||||
return .{ .data = data };
|
||||
}
|
||||
|
||||
/// Encode this segment with length prefix: [len][data]
|
||||
/// Length is encoded as varint for space efficiency
|
||||
pub fn encode(self: KeySegment, writer: anytype) !void {
|
||||
try encodeVarint(writer, self.data.len);
|
||||
try writer.writeAll(self.data);
|
||||
}
|
||||
|
||||
/// Calculate encoded size without actually encoding
|
||||
pub fn encodedSize(self: KeySegment) usize {
|
||||
return varintSize(self.data.len) + self.data.len;
|
||||
}
|
||||
};
|
||||
|
||||
/// Represents a complete storage key with all its components
|
||||
pub const StorageKey = struct {
|
||||
entity_type: EntityType,
|
||||
segments: []const KeySegment,
|
||||
|
||||
/// Encode the complete key: [entity_type][segment1][segment2]...
|
||||
pub fn encode(self: StorageKey, allocator: std.mem.Allocator) ![]u8 {
|
||||
var size: usize = 1; // entity type byte
|
||||
for (self.segments) |seg| {
|
||||
size += seg.encodedSize();
|
||||
}
|
||||
|
||||
const buf = try allocator.alloc(u8, size);
|
||||
var fbs = std.io.fixedBufferStream(buf);
|
||||
const writer = fbs.writer();
|
||||
|
||||
try writer.writeByte(self.entity_type.toByte());
|
||||
for (self.segments) |seg| {
|
||||
try seg.encode(writer);
|
||||
}
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
/// Calculate the encoded size without allocating
|
||||
pub fn encodedSize(self: StorageKey) usize {
|
||||
var size: usize = 1; // entity type byte
|
||||
for (self.segments) |seg| {
|
||||
size += seg.encodedSize();
|
||||
}
|
||||
return size;
|
||||
}
|
||||
};
|
||||
|
||||
/// Decode a binary key back into its components
|
||||
pub const KeyDecoder = struct {
|
||||
data: []const u8,
|
||||
pos: usize,
|
||||
|
||||
pub fn init(data: []const u8) KeyDecoder {
|
||||
return .{ .data = data, .pos = 0 };
|
||||
}
|
||||
|
||||
/// Read the entity type byte
|
||||
pub fn readEntityType(self: *KeyDecoder) !EntityType {
|
||||
if (self.pos >= self.data.len) return error.UnexpectedEndOfKey;
|
||||
const byte = self.data[self.pos];
|
||||
self.pos += 1;
|
||||
return @enumFromInt(byte);
|
||||
}
|
||||
|
||||
/// Read the next segment
|
||||
pub fn readSegment(self: *KeyDecoder, allocator: std.mem.Allocator) ![]u8 {
|
||||
const len = try self.readVarint();
|
||||
if (self.pos + len > self.data.len) return error.UnexpectedEndOfKey;
|
||||
|
||||
const segment = try allocator.dupe(u8, self.data[self.pos .. self.pos + len]);
|
||||
self.pos += len;
|
||||
return segment;
|
||||
}
|
||||
|
||||
/// Read segment without allocating (returns slice into original data)
|
||||
pub fn readSegmentBorrowed(self: *KeyDecoder) ![]const u8 {
|
||||
const len = try self.readVarint();
|
||||
if (self.pos + len > self.data.len) return error.UnexpectedEndOfKey;
|
||||
|
||||
const segment = self.data[self.pos .. self.pos + len];
|
||||
self.pos += len;
|
||||
return segment;
|
||||
}
|
||||
|
||||
/// Check if there are more bytes to read
|
||||
pub fn hasMore(self: *KeyDecoder) bool {
|
||||
return self.pos < self.data.len;
|
||||
}
|
||||
|
||||
fn readVarint(self: *KeyDecoder) !usize {
|
||||
var result: usize = 0;
|
||||
var shift: u6 = 0;
|
||||
|
||||
while (self.pos < self.data.len) {
|
||||
const byte = self.data[self.pos];
|
||||
self.pos += 1;
|
||||
|
||||
result |= @as(usize, byte & 0x7F) << shift;
|
||||
|
||||
if ((byte & 0x80) == 0) {
|
||||
return result;
|
||||
}
|
||||
|
||||
shift += 7;
|
||||
if (shift >= 64) return error.VarintOverflow;
|
||||
}
|
||||
|
||||
return error.UnexpectedEndOfKey;
|
||||
}
|
||||
};
|
||||
|
||||
/// Build a metadata key: [meta][table_name]
|
||||
pub fn buildMetaKey(allocator: std.mem.Allocator, table_name: []const u8) ![]u8 {
|
||||
const segments = [_]KeySegment{
|
||||
KeySegment.init(table_name),
|
||||
};
|
||||
|
||||
const key = StorageKey{
|
||||
.entity_type = .meta,
|
||||
.segments = &segments,
|
||||
};
|
||||
|
||||
return try key.encode(allocator);
|
||||
}
|
||||
|
||||
/// Build a data key: [data][table_name][pk_value][sk_value?]
|
||||
pub fn buildDataKey(
|
||||
allocator: std.mem.Allocator,
|
||||
table_name: []const u8,
|
||||
pk_value: []const u8,
|
||||
sk_value: ?[]const u8,
|
||||
) ![]u8 {
|
||||
if (sk_value) |sk| {
|
||||
const segments = [_]KeySegment{
|
||||
KeySegment.init(table_name),
|
||||
KeySegment.init(pk_value),
|
||||
KeySegment.init(sk),
|
||||
};
|
||||
const key = StorageKey{
|
||||
.entity_type = .data,
|
||||
.segments = &segments,
|
||||
};
|
||||
return try key.encode(allocator);
|
||||
} else {
|
||||
const segments = [_]KeySegment{
|
||||
KeySegment.init(table_name),
|
||||
KeySegment.init(pk_value),
|
||||
};
|
||||
const key = StorageKey{
|
||||
.entity_type = .data,
|
||||
.segments = &segments,
|
||||
};
|
||||
return try key.encode(allocator);
|
||||
}
|
||||
}
|
||||
|
||||
/// Build a prefix for scanning all items in a table: [data][table_name]
|
||||
pub fn buildTablePrefix(allocator: std.mem.Allocator, table_name: []const u8) ![]u8 {
|
||||
const segments = [_]KeySegment{
|
||||
KeySegment.init(table_name),
|
||||
};
|
||||
|
||||
const key = StorageKey{
|
||||
.entity_type = .data,
|
||||
.segments = &segments,
|
||||
};
|
||||
|
||||
return try key.encode(allocator);
|
||||
}
|
||||
|
||||
/// Build a prefix for querying by partition key: [data][table_name][pk_value]
|
||||
pub fn buildPartitionPrefix(
|
||||
allocator: std.mem.Allocator,
|
||||
table_name: []const u8,
|
||||
pk_value: []const u8,
|
||||
) ![]u8 {
|
||||
const segments = [_]KeySegment{
|
||||
KeySegment.init(table_name),
|
||||
KeySegment.init(pk_value),
|
||||
};
|
||||
|
||||
const key = StorageKey{
|
||||
.entity_type = .data,
|
||||
.segments = &segments,
|
||||
};
|
||||
|
||||
return try key.encode(allocator);
|
||||
}
|
||||
|
||||
/// Build a GSI key: [gsi][table_name][index_name][gsi_pk][gsi_sk?] -> stores primary key
|
||||
pub fn buildGSIKey(
|
||||
allocator: std.mem.Allocator,
|
||||
table_name: []const u8,
|
||||
index_name: []const u8,
|
||||
gsi_pk: []const u8,
|
||||
gsi_sk: ?[]const u8,
|
||||
) ![]u8 {
|
||||
if (gsi_sk) |sk| {
|
||||
const segments = [_]KeySegment{
|
||||
KeySegment.init(table_name),
|
||||
KeySegment.init(index_name),
|
||||
KeySegment.init(gsi_pk),
|
||||
KeySegment.init(sk),
|
||||
};
|
||||
const key = StorageKey{
|
||||
.entity_type = .gsi,
|
||||
.segments = &segments,
|
||||
};
|
||||
return try key.encode(allocator);
|
||||
} else {
|
||||
const segments = [_]KeySegment{
|
||||
KeySegment.init(table_name),
|
||||
KeySegment.init(index_name),
|
||||
KeySegment.init(gsi_pk),
|
||||
};
|
||||
const key = StorageKey{
|
||||
.entity_type = .gsi,
|
||||
.segments = &segments,
|
||||
};
|
||||
return try key.encode(allocator);
|
||||
}
|
||||
}
|
||||
|
||||
/// Build an LSI key: [lsi][table_name][index_name][pk][lsi_sk]
|
||||
pub fn buildLSIKey(
|
||||
allocator: std.mem.Allocator,
|
||||
table_name: []const u8,
|
||||
index_name: []const u8,
|
||||
pk: []const u8,
|
||||
lsi_sk: []const u8,
|
||||
) ![]u8 {
|
||||
const segments = [_]KeySegment{
|
||||
KeySegment.init(table_name),
|
||||
KeySegment.init(index_name),
|
||||
KeySegment.init(pk),
|
||||
KeySegment.init(lsi_sk),
|
||||
};
|
||||
|
||||
const key = StorageKey{
|
||||
.entity_type = .lsi,
|
||||
.segments = &segments,
|
||||
};
|
||||
|
||||
return try key.encode(allocator);
|
||||
}
|
||||
|
||||
/// Debug helper: convert binary key to human-readable string
|
||||
pub fn keyToDebugString(allocator: std.mem.Allocator, key: []const u8) ![]u8 {
|
||||
var decoder = KeyDecoder.init(key);
|
||||
|
||||
var buf = std.ArrayList(u8).init(allocator);
|
||||
errdefer buf.deinit();
|
||||
const writer = buf.writer();
|
||||
|
||||
const entity_type = decoder.readEntityType() catch |err| {
|
||||
try writer.print("INVALID_KEY: {}", .{err});
|
||||
return buf.toOwnedSlice();
|
||||
};
|
||||
|
||||
try writer.print("[{}]", .{entity_type});
|
||||
|
||||
var segment_num: usize = 0;
|
||||
while (decoder.hasMore()) {
|
||||
const segment = decoder.readSegmentBorrowed() catch |err| {
|
||||
try writer.print(" ERROR:{}", .{err});
|
||||
break;
|
||||
};
|
||||
|
||||
// Try to print as UTF-8, fall back to hex
|
||||
if (std.unicode.utf8ValidateSlice(segment)) {
|
||||
try writer.print(" '{s}'", .{segment});
|
||||
} else {
|
||||
try writer.writeAll(" 0x");
|
||||
for (segment) |byte| {
|
||||
try writer.print("{X:0>2}", .{byte});
|
||||
}
|
||||
}
|
||||
segment_num += 1;
|
||||
}
|
||||
|
||||
return buf.toOwnedSlice();
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Varint encoding helpers (variable-length integer encoding)
|
||||
// Uses LEB128 format: 7 bits per byte, MSB indicates continuation
|
||||
// ============================================================================
|
||||
|
||||
fn encodeVarint(writer: anytype, value: usize) !void {
|
||||
var v = value;
|
||||
while (true) {
|
||||
const byte = @as(u8, @intCast(v & 0x7F));
|
||||
v >>= 7;
|
||||
|
||||
if (v == 0) {
|
||||
try writer.writeByte(byte);
|
||||
return;
|
||||
} else {
|
||||
try writer.writeByte(byte | 0x80);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn varintSize(value: usize) usize {
|
||||
if (value == 0) return 1;
|
||||
|
||||
var v = value;
|
||||
var size: usize = 0;
|
||||
while (v > 0) {
|
||||
size += 1;
|
||||
v >>= 7;
|
||||
}
|
||||
return size;
|
||||
}
|
||||
35
src/main.zig
35
src/main.zig
@@ -17,19 +17,14 @@ pub fn main() !void {
|
||||
defer _ = gpa.deinit();
|
||||
const allocator = gpa.allocator();
|
||||
|
||||
// Parse command line args
|
||||
const config = try parseArgs(allocator);
|
||||
|
||||
// Print banner
|
||||
printBanner(config);
|
||||
|
||||
// Ensure data directory exists
|
||||
std.fs.cwd().makePath(config.data_dir) catch |err| {
|
||||
std.log.err("Failed to create data directory: {any}", .{err});
|
||||
return;
|
||||
};
|
||||
|
||||
// Initialize storage engine
|
||||
var engine = storage.StorageEngine.init(allocator, config.data_dir) catch |err| {
|
||||
std.log.err("Failed to initialize storage: {any}", .{err});
|
||||
return;
|
||||
@@ -38,12 +33,22 @@ pub fn main() !void {
|
||||
|
||||
std.log.info("Storage engine initialized at {s}", .{config.data_dir});
|
||||
|
||||
// Initialize API handler
|
||||
var api_handler = handler.ApiHandler.init(allocator, &engine);
|
||||
handler.setGlobalHandler(&api_handler);
|
||||
|
||||
// Start HTTP server
|
||||
var server = try http.Server.init(allocator, config.host, config.port, handler.httpHandler);
|
||||
const server_config = http.ServerConfig{
|
||||
.max_body_size = 100 * 1024 * 1024,
|
||||
.enable_keep_alive = true,
|
||||
.max_requests_per_connection = 1000,
|
||||
};
|
||||
|
||||
var server = try http.Server.init(
|
||||
allocator,
|
||||
config.host,
|
||||
config.port,
|
||||
handler.ApiHandler.handleRequest,
|
||||
@ptrCast(&api_handler),
|
||||
server_config,
|
||||
);
|
||||
defer server.stop();
|
||||
|
||||
std.log.info("Starting DynamoDB-compatible server on {s}:{d}", .{ config.host, config.port });
|
||||
@@ -57,7 +62,6 @@ fn parseArgs(allocator: std.mem.Allocator) !Config {
|
||||
var args = try std.process.argsWithAllocator(allocator);
|
||||
defer args.deinit();
|
||||
|
||||
// Skip program name
|
||||
_ = args.next();
|
||||
|
||||
while (args.next()) |arg| {
|
||||
@@ -71,7 +75,6 @@ fn parseArgs(allocator: std.mem.Allocator) !Config {
|
||||
}
|
||||
} else if (std.mem.eql(u8, arg, "--data-dir") or std.mem.eql(u8, arg, "-d")) {
|
||||
if (args.next()) |dir| {
|
||||
// Need sentinel-terminated string for RocksDB
|
||||
const owned = try allocator.dupeZ(u8, dir);
|
||||
config.data_dir = owned;
|
||||
}
|
||||
@@ -83,7 +86,6 @@ fn parseArgs(allocator: std.mem.Allocator) !Config {
|
||||
}
|
||||
}
|
||||
|
||||
// Check environment variables
|
||||
if (std.posix.getenv("DYNAMODB_PORT")) |port_str| {
|
||||
config.port = std.fmt.parseInt(u16, port_str, 10) catch config.port;
|
||||
}
|
||||
@@ -107,14 +109,6 @@ fn printHelp() void {
|
||||
\\ -v, --verbose Enable verbose logging
|
||||
\\ --help Show this help message
|
||||
\\
|
||||
\\Environment Variables:
|
||||
\\ DYNAMODB_PORT Override port
|
||||
\\ ROCKSDB_DATA_DIR Override data directory
|
||||
\\
|
||||
\\Examples:
|
||||
\\ zynamodb # Start with defaults
|
||||
\\ zynamodb -p 8080 -d /var/lib/db # Custom port and data dir
|
||||
\\
|
||||
;
|
||||
std.debug.print("{s}", .{help});
|
||||
}
|
||||
@@ -141,7 +135,6 @@ fn printBanner(config: Config) void {
|
||||
std.debug.print(" Port: {d} | Data Dir: {s}\n\n", .{ config.port, config.data_dir });
|
||||
}
|
||||
|
||||
// Re-export modules for testing
|
||||
pub const _rocksdb = rocksdb;
|
||||
pub const _http = http;
|
||||
pub const _storage = storage;
|
||||
|
||||
@@ -231,78 +231,3 @@ pub const Iterator = struct {
|
||||
return v[0..len];
|
||||
}
|
||||
};
|
||||
|
||||
// Tests
|
||||
test "rocksdb basic operations" {
|
||||
const allocator = std.testing.allocator;
|
||||
|
||||
// Use temp directory
|
||||
const path = "/tmp/test_rocksdb_basic";
|
||||
defer {
|
||||
std.fs.deleteTreeAbsolute(path) catch {};
|
||||
}
|
||||
|
||||
var db = try DB.open(path, true);
|
||||
defer db.close();
|
||||
|
||||
// Put and get
|
||||
try db.put("hello", "world");
|
||||
const val = try db.get(allocator, "hello");
|
||||
try std.testing.expectEqualStrings("world", val.?);
|
||||
allocator.free(val.?);
|
||||
|
||||
// Delete
|
||||
try db.delete("hello");
|
||||
const deleted = try db.get(allocator, "hello");
|
||||
try std.testing.expect(deleted == null);
|
||||
}
|
||||
|
||||
test "rocksdb write batch" {
|
||||
const allocator = std.testing.allocator;
|
||||
|
||||
const path = "/tmp/test_rocksdb_batch";
|
||||
defer {
|
||||
std.fs.deleteTreeAbsolute(path) catch {};
|
||||
}
|
||||
|
||||
var db = try DB.open(path, true);
|
||||
defer db.close();
|
||||
|
||||
var batch = WriteBatch.init() orelse unreachable;
|
||||
defer batch.deinit();
|
||||
|
||||
batch.put("key1", "value1");
|
||||
batch.put("key2", "value2");
|
||||
batch.put("key3", "value3");
|
||||
|
||||
try batch.write(&db);
|
||||
|
||||
const v1 = try db.get(allocator, "key1");
|
||||
defer if (v1) |v| allocator.free(v);
|
||||
try std.testing.expectEqualStrings("value1", v1.?);
|
||||
}
|
||||
|
||||
test "rocksdb iterator" {
|
||||
const path = "/tmp/test_rocksdb_iter";
|
||||
defer {
|
||||
std.fs.deleteTreeAbsolute(path) catch {};
|
||||
}
|
||||
|
||||
var db = try DB.open(path, true);
|
||||
defer db.close();
|
||||
|
||||
try db.put("a", "1");
|
||||
try db.put("b", "2");
|
||||
try db.put("c", "3");
|
||||
|
||||
var iter = Iterator.init(&db) orelse unreachable;
|
||||
defer iter.deinit();
|
||||
|
||||
iter.seekToFirst();
|
||||
|
||||
var count: usize = 0;
|
||||
while (iter.valid()) : (iter.next()) {
|
||||
count += 1;
|
||||
}
|
||||
try std.testing.expectEqual(@as(usize, 3), count);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user