Package io.roastedroot.proxywasm
Class SimpleSharedDataHandler
- java.lang.Object
-
- io.roastedroot.proxywasm.SimpleSharedDataHandler
-
- All Implemented Interfaces:
SharedDataHandler
public class SimpleSharedDataHandler extends Object implements SharedDataHandler
A basic, in-memory implementation of theSharedDataHandler
interface.This handler manages shared key-value data entirely within the host's memory using a
HashMap
. It supports Compare-And-Swap (CAS) operations for optimistic concurrency control. It is suitable for single-process environments or testing scenarios where data persistence or cross-process sharing is not required.All operations on this handler are synchronized to ensure thread safety within a single JVM.
-
-
Field Summary
-
Fields inherited from interface io.roastedroot.proxywasm.SharedDataHandler
DEFAULT
-
-
Constructor Summary
Constructors Constructor Description SimpleSharedDataHandler()
Default constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description SharedData
getSharedData(String key)
Retrieves the shared data associated with the given key from the in-memory store.io.roastedroot.proxywasm.internal.WasmResult
setSharedData(String key, byte[] value, int cas)
Sets or updates the shared data associated with the given key in the in-memory store, potentially performing a Compare-And-Swap (CAS) check.
-
-
-
Method Detail
-
getSharedData
public SharedData getSharedData(String key) throws WasmException
Retrieves the shared data associated with the given key from the in-memory store.- Specified by:
getSharedData
in interfaceSharedDataHandler
- Parameters:
key
- The key identifying the shared data item.- Returns:
- A
SharedData
object containing the value and its current CAS value, ornull
if the key is not found in the map. - Throws:
WasmException
- (Not currently thrown by this implementation, but part of the interface contract).
-
setSharedData
public io.roastedroot.proxywasm.internal.WasmResult setSharedData(String key, byte[] value, int cas)
Sets or updates the shared data associated with the given key in the in-memory store, potentially performing a Compare-And-Swap (CAS) check.CAS behavior:
- If the key does not exist: The operation succeeds only if
cas
is 0. A new entry is created with the given value and a CAS value of 0 (or 1, depending on interpretation, this implementation uses 0 initially, then increments). - If the key exists: The operation succeeds only if
cas
is 0 (unconditional update) or ifcas
matches the current CAS value stored for the key. On successful update, the CAS value is incremented.
value
tonull
effectively removes the key if the CAS check passes (or if cas is 0), asHashMap
allows null values.- Specified by:
setSharedData
in interfaceSharedDataHandler
- Parameters:
key
- The key identifying the shared data item.value
- The new data value to store (can be null).cas
- The Compare-And-Swap value for conditional update, or 0 for unconditional update.- Returns:
WasmResult.OK
if the update was successful, orWasmResult.CAS_MISMATCH
if the CAS check failed.
- If the key does not exist: The operation succeeds only if
-
-