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.AutoCloseable
A 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 class
FileReftableStack.CompactionStats
static class
FileReftableStack.ReftableNumbersNotIncreasingException
Thrown if the update indices in the stack are not monotonic(package private) static class
FileReftableStack.Segment
A segment is a consecutive list of reftables of the same approximate size.private static class
FileReftableStack.StackEntry
static interface
FileReftableStack.Writer
Writer 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>
configSupplier
private long
lastNextUpdateIndex
private MergedReftable
mergedReftable
private java.lang.Runnable
onChange
private static long
OVERHEAD
private java.security.SecureRandom
random
private java.io.File
reftableDir
private java.util.List<FileReftableStack.StackEntry>
stack
private java.io.File
stackPath
private FileReftableStack.CompactionStats
stats
-
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 boolean
addReftable(FileReftableStack.Writer w)
Tries to add a new reftable to the stack.private void
autoCompact()
Heuristically tries to compact the stack if the stack has a suitable shape.private static java.util.Optional<FileReftableStack.Segment>
autoCompactCandidate(long[] sizes)
void
close()
(package private) void
compactFully()
private java.io.File
compactLocked(int first, int last)
Write the reftable for the given range into a temp file.(package private) boolean
compactRange(int first, int last)
Compacts a range of the stack, following the file locking protocol documented in the spec.private java.lang.String
filename(long low, long high)
MergedReftable
getMergedReftable()
(package private) FileReftableStack.CompactionStats
getStats()
(package private) boolean
isUpToDate()
(package private) static int
log(long sz)
Calculate an approximate log2.private long
nextUpdateIndex()
private java.util.List<java.lang.String>
readTableNames()
private ReftableConfig
reftableConfig()
(package private) void
reload()
private void
reloadOnce(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.IOException
Creates 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.FileNotFoundException
Reloads 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:
close
in 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.IOException
Write 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.IOException
Compacts 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.IOException
Heuristically 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
-
-