java.lang.Object
net.i2p.router.networkdb.kademlia.PersistentDataStore.Writer
All Implemented Interfaces:
Flushable, Runnable
Enclosing class:
PersistentDataStore

private class PersistentDataStore.Writerextends Object implements Runnable, Flushable
Thread writing the store changes to disk.
  • Field Details

    • _keys

      private final Map<Hash, DatabaseEntry> _keys
      Queue of entries pending disk write.
    • _keysToRemove

      private final Set<Hash> _keysToRemove
      Keys to remove from disk on next write cycle.
    • _waitLock

      private final Object _waitLock
      Lock object for coordinating write cycle timing.
    • _quit

      private volatile boolean _quit
      Flag to signal the write loop to exit.
  • Constructor Details

    • Writer

      public Writer()
      Builds the disk writer thread.
  • Method Details

    • queue

      public void queue(Hash key, DatabaseEntry data)
      Add entry to the disk write queue.
      Parameters:
      key - the hash of the router whose entry is queued
      data - the entry to write on the next cycle
    • remove

      public void remove(Hash key)
      Mark a key for removal from disk on next cycle.
      Parameters:
      key - the hash of the router whose on-disk file should go
    • removeQueued

      private void removeQueued()
      Drops queued entries that were replaced.
    • drainPending

      private List<Map.Entry<Hash, DatabaseEntry>> drainPending()
      Drain up to WRITE_LIMIT pending entries from the write queue. Returns the drained entries; caller must write and removeQueued() them.
      Returns:
      the drained entries, at most WRITE_LIMIT of them; empty when nothing is pending
    • writeBatch

      private void writeBatch(List<Map.Entry<Hash, DatabaseEntry>> entries)
      Write a batch of RouterInfo entries to disk under the _dbDir lock. Each entry is individually locked to avoid holding the lock across the entire batch.
      Parameters:
      entries - the entries drained from the queue; null data values are skipped
    • logWriteStats

      private void logWriteStats(int written, long startTime)
      Log write statistics and reset the timer.
      Parameters:
      written - the number of RouterInfo files written this cycle; nothing is logged for 0
      startTime - the wall-clock time in ms when the write cycle began
    • waitForNextCycle

      private void waitForNextCycle()
      Sleep until the next write cycle or until flushed.
    • run

      public void run()
      Specified by:
      run in interface Runnable
    • flush

      public void flush()
      Signal the write loop to exit.

      Sets the quit flag and wakes the writer so it leaves waitForNextCycle(). The loop writes the batch it already drained before it re-checks the flag, so entries queued before the call are written; anything queued concurrently with or after it is not.

      Specified by:
      flush in interface Flushable