more house keeping

This commit is contained in:
2026-02-21 20:50:14 -05:00
parent 47eefd0fe5
commit 5ee3df86f1
5 changed files with 129 additions and 47 deletions

View File

@@ -793,12 +793,20 @@ put_item :: proc(engine: ^Storage_Engine, table_name: string, item: Item) -> Sto
// --- Check if item already exists (need old item for GSI cleanup) ---
old_item: Maybe(Item) = nil
existing_value, existing_err := rocksdb.db_get(&engine.db, storage_key)
if existing_err == .None && existing_value != nil {
if existing_err == .NotFound {
// Item does not exist — nothing to clean up, proceed normally.
} else if existing_err != .None {
// Unexpected RocksDB I/O error — fail closed to avoid orphaned GSI entries.
return .RocksDB_Error
} else if existing_value != nil {
defer delete(existing_value)
decoded_old, decode_ok := decode(existing_value)
if decode_ok {
old_item = decoded_old
if !decode_ok {
// Value exists but is unreadable — fail closed rather than leaving
// stale GSI entries behind after the overwrite.
return .Serialization_Error
}
old_item = decoded_old
}
// Cleanup old_item at the end
defer {
@@ -945,12 +953,21 @@ delete_item :: proc(engine: ^Storage_Engine, table_name: string, key: Item) -> S
// --- Read existing item to know which GSI entries to remove ---
old_item: Maybe(Item) = nil
existing_value, existing_err := rocksdb.db_get(&engine.db, storage_key)
if existing_err == .None && existing_value != nil {
if existing_err == .NotFound {
// Item does not exist — nothing to delete (DynamoDB idempotent delete).
return .None
} else if existing_err != .None {
// Unexpected RocksDB I/O error — fail closed.
return .RocksDB_Error
} else if existing_value != nil {
defer delete(existing_value)
decoded_old, decode_ok := decode(existing_value)
if decode_ok {
old_item = decoded_old
if !decode_ok {
// Value exists but is corrupt — fail closed rather than deleting the
// base item while leaving its GSI entries dangling.
return .Serialization_Error
}
old_item = decoded_old
}
// Cleanup old_item at the end
defer {
@@ -960,7 +977,7 @@ delete_item :: proc(engine: ^Storage_Engine, table_name: string, key: Item) -> S
}
}
// If item doesn't exist, nothing to delete (not an error in DynamoDB)
// If item doesn't exist (existing_value was nil with no error), nothing to delete.
if _, has_old := old_item.?; !has_old {
return .None
}