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 theSharedDataHandlerinterface.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 SharedDatagetSharedData(String key)Retrieves the shared data associated with the given key from the in-memory store.io.roastedroot.proxywasm.internal.WasmResultsetSharedData(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:
getSharedDatain interfaceSharedDataHandler- Parameters:
key- The key identifying the shared data item.- Returns:
- A
SharedDataobject containing the value and its current CAS value, ornullif 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
casis 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
casis 0 (unconditional update) or ifcasmatches the current CAS value stored for the key. On successful update, the CAS value is incremented.
valuetonulleffectively removes the key if the CAS check passes (or if cas is 0), asHashMapallows null values.- Specified by:
setSharedDatain 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.OKif the update was successful, orWasmResult.CAS_MISMATCHif the CAS check failed.
- If the key does not exist: The operation succeeds only if
-
-