java.lang.Object
net.i2p.router.networkdb.kademlia.PersistentDataStore.Writer
- Enclosing class:
PersistentDataStore
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Map<Hash, DatabaseEntry> Queue of entries pending disk write.Keys to remove from disk on next write cycle.private booleanFlag to signal the write loop to exit.private final ObjectLock object for coordinating write cycle timing.Constructor Summary
ConstructorsMethod Summary
Modifier and TypeMethodDescriptionprivate List<Map.Entry<Hash, DatabaseEntry>> Drain up to WRITE_LIMIT pending entries from the write queue.voidflush()Signal the write loop to exit.private voidlogWriteStats(int written, long startTime) Log write statistics and reset the timer.voidqueue(Hash key, DatabaseEntry data) Add entry to the disk write queue.voidMark a key for removal from disk on next cycle.private voidDrops queued entries that were replaced.voidrun()private voidSleep until the next write cycle or until flushed.private voidwriteBatch(List<Map.Entry<Hash, DatabaseEntry>> entries) Write a batch of RouterInfo entries to disk under the _dbDir lock.
Field Details
_keys
Queue of entries pending disk write._keysToRemove
_waitLock
Lock object for coordinating write cycle timing._quit
private volatile boolean _quitFlag to signal the write loop to exit.
Constructor Details
Writer
public Writer()Builds the disk writer thread.
Method Details
queue
Add entry to the disk write queue.- Parameters:
key- the hash of the router whose entry is queueddata- the entry to write on the next cycle
remove
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
Drain up to WRITE_LIMIT pending entries from the write queue. Returns the drained entries; caller must write andremoveQueued()them.- Returns:
- the drained entries, at most WRITE_LIMIT of them; empty when nothing is pending
writeBatch
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 0startTime- 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
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.