Class FileReftableStack
- java.lang.Object
-
- org.eclipse.jgit.internal.storage.file.FileReftableStack
-
- All Implemented Interfaces:
java.lang.AutoCloseable
public class FileReftableStack extends java.lang.Object implements java.lang.AutoCloseableA mutable stack of reftables on local filesystem storage. Not thread-safe. This is an AutoCloseable because this object owns the file handles to the open reftables.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static classFileReftableStack.CompactionStatsstatic classFileReftableStack.ReftableNumbersNotIncreasingExceptionThrown if the update indices in the stack are not monotonic(package private) static classFileReftableStack.SegmentA segment is a consecutive list of reftables of the same approximate size.private static classFileReftableStack.StackEntrystatic interfaceFileReftableStack.WriterWriter is a callable that writes data to a reftable under construction.
-
Field Summary
Fields Modifier and Type Field Description private java.util.function.Supplier<Config>configSupplierprivate longlastNextUpdateIndexprivate MergedReftablemergedReftableprivate java.lang.RunnableonChangeprivate static longOVERHEADprivate java.security.SecureRandomrandomprivate java.io.FilereftableDirprivate java.util.List<FileReftableStack.StackEntry>stackprivate java.io.FilestackPathprivate FileReftableStack.CompactionStatsstats
-
Constructor Summary
Constructors Constructor Description FileReftableStack(java.io.File stackPath, java.io.File reftableDir, java.lang.Runnable onChange, java.util.function.Supplier<Config> configSupplier)Creates a stack corresponding to the list of reftables in the argument
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanaddReftable(FileReftableStack.Writer w)Tries to add a new reftable to the stack.private voidautoCompact()Heuristically tries to compact the stack if the stack has a suitable shape.private static java.util.Optional<FileReftableStack.Segment>autoCompactCandidate(long[] sizes)voidclose()(package private) voidcompactFully()private java.io.FilecompactLocked(int first, int last)Write the reftable for the given range into a temp file.(package private) booleancompactRange(int first, int last)Compacts a range of the stack, following the file locking protocol documented in the spec.private java.lang.Stringfilename(long low, long high)MergedReftablegetMergedReftable()(package private) FileReftableStack.CompactionStatsgetStats()(package private) booleanisUpToDate()(package private) static intlog(long sz)Calculate an approximate log2.private longnextUpdateIndex()private java.util.List<java.lang.String>readTableNames()private ReftableConfigreftableConfig()(package private) voidreload()private voidreloadOnce(java.util.List<java.lang.String> names)Reloads the stack, potentially reusing opened reftableReaders.(package private) static java.util.List<FileReftableStack.Segment>segmentSizes(long[] sizes)private long[]tableSizes()
-
-
-
Field Detail
-
mergedReftable
private MergedReftable mergedReftable
-
stack
private java.util.List<FileReftableStack.StackEntry> stack
-
lastNextUpdateIndex
private long lastNextUpdateIndex
-
stackPath
private final java.io.File stackPath
-
reftableDir
private final java.io.File reftableDir
-
onChange
private final java.lang.Runnable onChange
-
random
private final java.security.SecureRandom random
-
configSupplier
private final java.util.function.Supplier<Config> configSupplier
-
stats
private final FileReftableStack.CompactionStats stats
-
OVERHEAD
private static long OVERHEAD
-
-
Constructor Detail
-
FileReftableStack
public FileReftableStack(java.io.File stackPath, java.io.File reftableDir, @Nullable java.lang.Runnable onChange, java.util.function.Supplier<Config> configSupplier) throws java.io.IOExceptionCreates a stack corresponding to the list of reftables in the argument- Parameters:
stackPath- the filename for the stack.reftableDir- the dir holding the tables.onChange- hook to call if we notice a new writeconfigSupplier- Config supplier- Throws:
java.io.IOException- on I/O problems
-
-
Method Detail
-
getStats
FileReftableStack.CompactionStats getStats()
-
reloadOnce
private void reloadOnce(java.util.List<java.lang.String> names) throws java.io.IOException, java.io.FileNotFoundExceptionReloads the stack, potentially reusing opened reftableReaders.- Parameters:
names- holds the names of the tables to load.- Throws:
java.io.FileNotFoundException- load must be retried.java.io.IOException- on other IO errors.
-
reload
void reload() throws java.io.IOException- Throws:
java.io.IOException
-
getMergedReftable
public MergedReftable getMergedReftable()
- Returns:
- the merged reftable
-
readTableNames
private java.util.List<java.lang.String> readTableNames() throws java.io.IOException- Throws:
java.io.IOException
-
isUpToDate
boolean isUpToDate() throws java.io.IOException- Returns:
- true if the on-disk file corresponds to the in-memory data.
- Throws:
java.io.IOException- on IO problem
-
close
public void close()
- Specified by:
closein interfacejava.lang.AutoCloseable
-
nextUpdateIndex
private long nextUpdateIndex() throws java.io.IOException- Throws:
java.io.IOException
-
filename
private java.lang.String filename(long low, long high)
-
addReftable
public boolean addReftable(FileReftableStack.Writer w) throws java.io.IOException
Tries to add a new reftable to the stack. Returns true if it succeeded, or false if there was a lock failure, due to races with other processes. This is package private so FileReftableDatabase can call into here.- Parameters:
w- writer to write data to a reftable under construction- Returns:
- true if the transaction was successful.
- Throws:
java.io.IOException- on I/O problems
-
reftableConfig
private ReftableConfig reftableConfig()
-
compactLocked
private java.io.File compactLocked(int first, int last) throws java.io.IOExceptionWrite the reftable for the given range into a temp file.- Parameters:
first- index of first stack entry to be writtenlast- index of last stack entry to be written- Returns:
- the file holding the replacement table.
- Throws:
java.io.IOException- on I/O problem
-
compactRange
boolean compactRange(int first, int last) throws java.io.IOExceptionCompacts a range of the stack, following the file locking protocol documented in the spec.- Parameters:
first- index of first stack entry to be considered in compactionlast- index of last stack entry to be considered in compaction- Returns:
- true if a compaction was successfully applied.
- Throws:
java.io.IOException- on I/O problem
-
log
static int log(long sz)
Calculate an approximate log2.- Parameters:
sz-- Returns:
- log2
-
segmentSizes
static java.util.List<FileReftableStack.Segment> segmentSizes(long[] sizes)
-
autoCompactCandidate
private static java.util.Optional<FileReftableStack.Segment> autoCompactCandidate(long[] sizes)
-
autoCompact
private void autoCompact() throws java.io.IOExceptionHeuristically tries to compact the stack if the stack has a suitable shape.- Throws:
java.io.IOException
-
tableSizes
private long[] tableSizes() throws java.io.IOException- Throws:
java.io.IOException
-
compactFully
void compactFully() throws java.io.IOException- Throws:
java.io.IOException
-
-