Class SimpleSharedDataHandler

  • All Implemented Interfaces:
    SharedDataHandler

    public class SimpleSharedDataHandler
    extends Object
    implements SharedDataHandler
    A basic, in-memory implementation of the SharedDataHandler 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.

    • Constructor Detail

      • SimpleSharedDataHandler

        public SimpleSharedDataHandler()
        Default constructor.
    • 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 interface SharedDataHandler
        Parameters:
        key - The key identifying the shared data item.
        Returns:
        A SharedData object containing the value and its current CAS value, or null 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 if cas matches the current CAS value stored for the key. On successful update, the CAS value is incremented.
        Setting value to null effectively removes the key if the CAS check passes (or if cas is 0), as HashMap allows null values.
        Specified by:
        setSharedData in interface SharedDataHandler
        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, or WasmResult.CAS_MISMATCH if the CAS check failed.