Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions protocols/proven-cache/ffi/zig/src/cache.zig
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ comptime {
if (@intFromEnum(ErrorCode.value_too_large) != gen.ERR_VALUE_TOO_LARGE) @compileError("ABI drift: ErrorCode.value_too_large");
if (@intFromEnum(ErrorCode.cas_conflict) != gen.ERR_CAS_CONFLICT) @compileError("ABI drift: ErrorCode.cas_conflict");

if (@intFromEnum(ErrorCode.not_found) + 1 != gen.STATUS_NOT_FOUND) @compileError("ABI drift: cache_execute not_found status");
if (@intFromEnum(ErrorCode.type_mismatch) + 1 != gen.STATUS_TYPE_MISMATCH) @compileError("ABI drift: cache_execute type_mismatch status");
if (@intFromEnum(ErrorCode.out_of_memory) + 1 != gen.STATUS_OUT_OF_MEMORY) @compileError("ABI drift: cache_execute out_of_memory status");
if (@intFromEnum(ErrorCode.key_too_long) + 1 != gen.STATUS_KEY_TOO_LONG) @compileError("ABI drift: cache_execute key_too_long status");
if (@intFromEnum(ErrorCode.value_too_large) + 1 != gen.STATUS_VALUE_TOO_LARGE) @compileError("ABI drift: cache_execute value_too_large status");
if (@intFromEnum(ErrorCode.cas_conflict) + 1 != gen.STATUS_CAS_CONFLICT) @compileError("ABI drift: cache_execute cas_conflict status");

if (@intFromEnum(ReplicationMode.none) != gen.REPL_NONE) @compileError("ABI drift: ReplicationMode.none");
if (@intFromEnum(ReplicationMode.primary) != gen.REPL_PRIMARY) @compileError("ABI drift: ReplicationMode.primary");
if (@intFromEnum(ReplicationMode.replica) != gen.REPL_REPLICA) @compileError("ABI drift: ReplicationMode.replica");
Expand Down Expand Up @@ -223,8 +230,8 @@ pub export fn cache_execute(slot: c_int, cmd: u8) callconv(.c) u8 {
mutex.lock();
defer mutex.unlock();

const idx = validSlot(slot) orelse return 1; // not_found + 1 as generic error
if (cmd > 12) return 1;
const idx = validSlot(slot) orelse return gen.STATUS_NOT_FOUND; // generic error
if (cmd > 12) return gen.STATUS_NOT_FOUND;

const command: Command = @enumFromInt(cmd);

Expand All @@ -241,13 +248,13 @@ pub export fn cache_execute(slot: c_int, cmd: u8) callconv(.c) u8 {
sessions[idx].key_count >= sessions[idx].max_keys and
sessions[idx].max_keys > 0)
{
return @intFromEnum(ErrorCode.out_of_memory) + 1;
return gen.STATUS_OUT_OF_MEMORY;
}
sessions[idx].key_count += 1;
},
.delete => {
if (sessions[idx].key_count == 0) {
return @intFromEnum(ErrorCode.not_found) + 1;
return gen.STATUS_NOT_FOUND;
}
sessions[idx].key_count -= 1;
},
Expand All @@ -268,17 +275,17 @@ pub export fn cache_execute(slot: c_int, cmd: u8) callconv(.c) u8 {
},
.incr, .decr => {
if (sessions[idx].key_count == 0) {
return @intFromEnum(ErrorCode.not_found) + 1;
return gen.STATUS_NOT_FOUND;
}
},
.append, .prepend => {
if (sessions[idx].key_count == 0) {
return @intFromEnum(ErrorCode.not_found) + 1;
return gen.STATUS_NOT_FOUND;
}
},
.cas => {
if (sessions[idx].key_count == 0) {
return @intFromEnum(ErrorCode.not_found) + 1;
return gen.STATUS_NOT_FOUND;
}
},
}
Expand Down
6 changes: 6 additions & 0 deletions protocols/proven-cache/ffi/zig/src/cache_abi_gen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ pub const ERR_OUT_OF_MEMORY: u8 = 2;
pub const ERR_KEY_TOO_LONG: u8 = 3;
pub const ERR_VALUE_TOO_LARGE: u8 = 4;
pub const ERR_CAS_CONFLICT: u8 = 5;
pub const STATUS_NOT_FOUND: u8 = 1;
pub const STATUS_TYPE_MISMATCH: u8 = 2;
pub const STATUS_OUT_OF_MEMORY: u8 = 3;
pub const STATUS_KEY_TOO_LONG: u8 = 4;
pub const STATUS_VALUE_TOO_LARGE: u8 = 5;
pub const STATUS_CAS_CONFLICT: u8 = 6;
pub const REPL_NONE: u8 = 0;
pub const REPL_PRIMARY: u8 = 1;
pub const REPL_REPLICA: u8 = 2;
Expand Down
6 changes: 6 additions & 0 deletions protocols/proven-cache/src/CacheABI/Emit.idr
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ manifest =
, line "ERR" "KEY_TOO_LONG" (errorCodeToTag KeyTooLong)
, line "ERR" "VALUE_TOO_LARGE" (errorCodeToTag ValueTooLarge)
, line "ERR" "CAS_CONFLICT" (errorCodeToTag CASConflict)
, line "STATUS" "NOT_FOUND" (errorToExecuteStatus NotFound)
, line "STATUS" "TYPE_MISMATCH" (errorToExecuteStatus TypeMismatch)
, line "STATUS" "OUT_OF_MEMORY" (errorToExecuteStatus OutOfMemory)
, line "STATUS" "KEY_TOO_LONG" (errorToExecuteStatus KeyTooLong)
, line "STATUS" "VALUE_TOO_LARGE" (errorToExecuteStatus ValueTooLarge)
, line "STATUS" "CAS_CONFLICT" (errorToExecuteStatus CASConflict)
, line "REPL" "NONE" (replicationModeToTag RNone)
, line "REPL" "PRIMARY" (replicationModeToTag Primary)
, line "REPL" "REPLICA" (replicationModeToTag Replica)
Expand Down
3 changes: 2 additions & 1 deletion protocols/proven-cache/src/CacheABI/Foreign.idr
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ abiVersion = 1
-- +---------------------------+-----------------------------------------------+
-- | cache_execute | (slot: c_int, cmd: u8) -> u8 |
-- | | Execute a command. Returns 0 on success, |
-- | | or an ErrorCode tag on failure. |
-- | | or ErrorCode tag + 1 on failure. |
-- | | See errorToExecuteStatus in CacheABI.Types. |
-- +---------------------------+-----------------------------------------------+
-- | cache_eviction_policy | (slot: c_int) -> u8 (EvictionPolicy tag) |
-- | | Returns the eviction policy for this session. |
Expand Down
25 changes: 25 additions & 0 deletions protocols/proven-cache/src/CacheABI/Types.idr
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,31 @@ errorCodeRoundtrip KeyTooLong = Refl
errorCodeRoundtrip ValueTooLarge = Refl
errorCodeRoundtrip CASConflict = Refl

||| cache_execute reserves zero for success; failures are raw tags plus one.
public export
errorToExecuteStatus : ErrorCode -> Bits8
errorToExecuteStatus e = errorCodeToTag e + 1

||| Decode a failure status. Zero (success) and out-of-range codes are not errors.
public export
executeStatusToError : Bits8 -> Maybe ErrorCode
executeStatusToError 0 = Nothing
executeStatusToError n = tagToErrorCode (n - 1)

public export
executeFailureRoundtrip : (e : ErrorCode) ->
executeStatusToError (errorToExecuteStatus e) = Just e
executeFailureRoundtrip NotFound = Refl
executeFailureRoundtrip TypeMismatch = Refl
executeFailureRoundtrip OutOfMemory = Refl
executeFailureRoundtrip KeyTooLong = Refl
executeFailureRoundtrip ValueTooLarge = Refl
executeFailureRoundtrip CASConflict = Refl

public export
executeSuccessIsNotFailure : executeStatusToError 0 = Nothing
executeSuccessIsNotFailure = Refl

---------------------------------------------------------------------------
-- ReplicationMode (4 constructors, tags 0-3)
---------------------------------------------------------------------------
Expand Down
Loading