Class WriterPool
java.lang.Object
org.archive.io.WriterPool
- Direct Known Subclasses:
ARCWriterPool,WARCWriterPool
Pool of Writers.
Abstract. Override and pass in the Constructor a factory that creates
WriterPoolMember implementations.- Author:
- stack
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected BlockingQueue<WriterPoolMember> round-robin queue of available writersprotected intcurrent count of active writers; only read/mutated in synchronized blocksstatic final intDefault maximum active number of files in the pool.static final intMaximum time to wait on a free file before considering making a new one (if not already at max)protected static final intAssumed largest possible value of maxActive; pool will have this maximum capacity, so dynamic changes beyond this number won't work.protected longsystem time when writer was last wanted (because one was not ready in time)protected longsystem time when writer was last 'rolled over' (imminent creation of new file)protected intmaximum number of writers to create at a timeprotected intmaximum ms to wait before considering creation of a writerprotected final AtomicIntegerUsed to generate unique filename sequences.protected final WriterPoolSettingsFile settings. -
Constructor Summary
ConstructorsConstructorDescriptionWriterPool(AtomicInteger serial, WriterPoolSettings settings, int poolMaximumActive, int poolMaximumWait) Constructor -
Method Summary
Modifier and TypeMethodDescriptionCheck out aWriterPoolMember.voidclose()Close allWriterPoolMembers in pool.voiddestroyWriter(WriterPoolMember writer) Discard a previously-used writer, cleanly closing it and leaving it out of the pool.protected Collection<WriterPoolMember> Drains all the active writers fromavailableWriters, blocking to wait for any writers currently in use to become available.voidflush()intintprotected StringReturns the atomic integer used to generate serial numbers for files.voidClose and discard a writer that experienced a potentially-corrupting error.org.json.JSONArrayprotected WriterPoolMemberCreate a new writer instance, if still below maxActive count.protected abstract WriterPoolMembervoidreturnFile(WriterPoolMember writer) Return a writer, for likely reuse unless (1) writer's current file has reached its target size; and (2) there's been no demand for additional writers since the last time a new writer-file was rolled-over.
-
Field Details
-
serialNo
Used to generate unique filename sequences. -
DEFAULT_MAX_ACTIVE
public static final int DEFAULT_MAX_ACTIVEDefault maximum active number of files in the pool.- See Also:
-
LARGEST_MAX_ACTIVE
protected static final int LARGEST_MAX_ACTIVEAssumed largest possible value of maxActive; pool will have this maximum capacity, so dynamic changes beyond this number won't work.- See Also:
-
DEFAULT_MAX_WAIT_FOR_IDLE
public static final int DEFAULT_MAX_WAIT_FOR_IDLEMaximum time to wait on a free file before considering making a new one (if not already at max)- See Also:
-
settings
File settings. Keep in data structure rather than as individual values. -
maxActive
protected int maxActivemaximum number of writers to create at a time -
maxWait
protected int maxWaitmaximum ms to wait before considering creation of a writer -
currentActive
protected int currentActivecurrent count of active writers; only read/mutated in synchronized blocks -
availableWriters
round-robin queue of available writers -
lastWriterNeededTime
protected long lastWriterNeededTimesystem time when writer was last wanted (because one was not ready in time) -
lastWriterRolloverTime
protected long lastWriterRolloverTimesystem time when writer was last 'rolled over' (imminent creation of new file)
-
-
Constructor Details
-
WriterPool
public WriterPool(AtomicInteger serial, WriterPoolSettings settings, int poolMaximumActive, int poolMaximumWait) Constructor- Parameters:
serial- Used to generate unique filename sequencessettings- Settings for this pool.
-
-
Method Details
-
borrowFile
Check out aWriterPoolMember. This method should be followed by a call toreturnFile(WriterPoolMember)orinvalidateFile(WriterPoolMember)else pool starts leaking.- Returns:
- Writer checked out of a pool of files or created
- Throws:
IOException- Problem getting Writer from pool (Converted from Exception to IOException so this pool can live as a good citizen down in depths of ARCSocketFactory).
-
makeNewWriterIfAppropriate
Create a new writer instance, if still below maxActive count. Remember times to help make later decision when writer should be discarded.- Returns:
- WriterPoolMember or null if already at max
-
makeWriter
- Returns:
- new WriterPoolMember of appropriate type
-
destroyWriter
Discard a previously-used writer, cleanly closing it and leaving it out of the pool.- Parameters:
writer-- Throws:
IOException
-
returnFile
Return a writer, for likely reuse unless (1) writer's current file has reached its target size; and (2) there's been no demand for additional writers since the last time a new writer-file was rolled-over. In that case, the possibly-superfluous writer instance is discarded.- Parameters:
writer- Writer to return to the pool.- Throws:
IOException- Problem returning File to pool.
-
invalidateFile
Close and discard a writer that experienced a potentially-corrupting error.- Parameters:
f- writer with problem- Throws:
IOException
-
getNumActive
- Returns:
- Number of
WriterPoolMembers checked out of pool. - Throws:
UnsupportedOperationException
-
getNumIdle
- Returns:
- Number of
WriterPoolMemberinstances still in the pool. - Throws:
UnsupportedOperationException
-
close
public void close()Close allWriterPoolMembers in pool. -
getSettings
- Returns:
- Returns settings.
-
getPoolState
- Returns:
- State of the pool string
-
getSerialNo
Returns the atomic integer used to generate serial numbers for files.- Returns:
- the serial number generator
-
drainAllWriters
Drains all the active writers fromavailableWriters, blocking to wait for any writers currently in use to become available.When finished with writers, call availableWriters.addAll(...) to put them back into the rotation.
- Returns:
- all the active writers
-
flush
public void flush() -
jsonStatus
public org.json.JSONArray jsonStatus() throws org.json.JSONException- Throws:
org.json.JSONException
-