Class FileBlockStore

java.lang.Object
ai.badmonkey.agentspaces.peering.blocks.FileBlockStore
All Implemented Interfaces:
BlockStore

public final class FileBlockStore extends Object implements BlockStore
A file-backed block store (LB-B-05): one file per CID under a directory, written through a temporary name and an atomic move, read back on start so a restart serves what it held and a live entry's block survives the process. The last-served time is the file's modification time, which a read refreshes, so eviction order survives too.
  • Constructor Details

    • FileBlockStore

      public FileBlockStore(Path dir) throws IOException
      Opens or creates a store, reading back every block it holds.
      Parameters:
      dir - the directory
      Throws:
      IOException - when the directory cannot be created or listed
  • Method Details

    • dir

      public Path dir()
      The directory.
    • get

      public Optional<byte[]> get(String cid, long nowMillis)
      Description copied from interface: BlockStore
      Reads a block and marks it served.
      Specified by:
      get in interface BlockStore
      Parameters:
      cid - the CID
      nowMillis - the clock
      Returns:
      the bytes, when held
    • put

      public boolean put(String cid, byte[] bytes, long nowMillis)
      Description copied from interface: BlockStore
      Stores a block unless it is already held, in which case it is marked served.
      Specified by:
      put in interface BlockStore
      Parameters:
      cid - the CID
      bytes - the bytes; the store keeps its own copy
      nowMillis - the clock
      Returns:
      whether the block was new to the store
    • remove

      public long remove(String cid)
      Description copied from interface: BlockStore
      Drops a block.
      Specified by:
      remove in interface BlockStore
      Parameters:
      cid - the CID
      Returns:
      the bytes freed, or -1 when the block was not held
    • cids

      public Set<String> cids()
      Description copied from interface: BlockStore
      The CIDs held.
      Specified by:
      cids in interface BlockStore
    • lastAccessMillis

      public long lastAccessMillis(String cid)
      Description copied from interface: BlockStore
      When a block was last served or stored, or -1 when not held.
      Specified by:
      lastAccessMillis in interface BlockStore
    • size

      public int size()
      Description copied from interface: BlockStore
      How many blocks are held.
      Specified by:
      size in interface BlockStore
    • bytesUsed

      public long bytesUsed()
      Description copied from interface: BlockStore
      The bytes held.
      Specified by:
      bytesUsed in interface BlockStore
    • kind

      public String kind()
      Description copied from interface: BlockStore
      The store kind: memory or file.
      Specified by:
      kind in interface BlockStore