Class ReftableWriter
- java.lang.Object
-
- org.eclipse.jgit.internal.storage.reftable.ReftableWriter
-
public class ReftableWriter extends java.lang.Object
Writes a reftable formatted file.A reftable can be written in a streaming fashion, provided the caller sorts all references. A
ReftableWriter
is single-use, and not thread-safe.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private class
ReftableWriter.IndexBuilder
private static class
ReftableWriter.RefList
private class
ReftableWriter.Section
static class
ReftableWriter.Stats
Statistics about a written reftable.
-
Field Summary
Fields Modifier and Type Field Description private boolean
alignBlocks
private ReftableConfig
config
private BlockWriter
cur
private boolean
indexObjects
private BlockWriter.Entry
lastLog
private BlockWriter.Entry
lastRef
private int
logBlockSize
private ReftableWriter.Section
logs
private int
maxIndexLevels
private long
maxUpdateIndex
private long
minUpdateIndex
private ObjectIdSubclassMap<ReftableWriter.RefList>
obj2ref
private int
objIdLen
private ReftableWriter.Section
objs
private ReftableOutputStream
out
private java.io.OutputStream
outputStream
private int
refBlockSize
private ReftableWriter.Section
refs
private int
restartInterval
private ReftableWriter.Stats
stats
-
Constructor Summary
Constructors Constructor Description ReftableWriter(java.io.OutputStream os)
Initialize a writer with a default configuration.ReftableWriter(ReftableConfig cfg, java.io.OutputStream os)
Initialize a writer with a configuration.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description ReftableWriter
begin()
Begin writing the reftable.private void
beginLog()
void
deleteLog(java.lang.String ref, long updateIndex)
Record deletion of one reflog entry in this reftable.private void
encodeHeader(byte[] hdr)
long
estimateTotalBytes()
Get an estimate of the current size in bytes of the reftableReftableWriter
finish()
Finish writing the reftable by writing its trailer.private void
finishLogSection()
private void
finishRefAndObjSections()
private static long
firstBlockPosition(ReftableWriter.Section s)
ReftableWriter.Stats
getStats()
Get statistics of the last written reftable.private void
indexId(ObjectId id, long blockPos)
private static long
indexPosition(ReftableWriter.Section s)
private void
indexRef(Ref ref, long blockPos)
ReftableWriter
setConfig(ReftableConfig cfg)
Set configuration for the writer.ReftableWriter
setMaxUpdateIndex(long max)
Set the maximum update index for log entries that appear in this reftable.ReftableWriter
setMinUpdateIndex(long min)
Set the minimum update index for log entries that appear in this reftable.private static int
shortestUniqueAbbreviation(java.util.List<ReftableWriter.RefList> in)
private boolean
shouldHaveIndex(ReftableWriter.IndexBuilder idx)
ReftableWriter
sortAndWriteRefs(java.util.Collection<Ref> refsToPack)
Sort a collection of references and write them to the reftable.private static java.util.List<ReftableWriter.RefList>
sortById(ObjectIdSubclassMap<ReftableWriter.RefList> m)
private void
throwIllegalEntry(BlockWriter.Entry last, BlockWriter.Entry now)
private void
writeFileFooter()
private void
writeFileHeader()
void
writeLog(java.lang.String ref, long updateIndex, PersonIdent who, ObjectId oldId, ObjectId newId, java.lang.String message)
Write one reflog entry to the reftable.private void
writeObjBlocks()
void
writeRef(Ref ref)
Write one reference to the reftable.void
writeRef(Ref ref, long updateIndex)
Write one reference to the reftable.
-
-
-
Field Detail
-
config
private ReftableConfig config
-
refBlockSize
private int refBlockSize
-
logBlockSize
private int logBlockSize
-
restartInterval
private int restartInterval
-
maxIndexLevels
private int maxIndexLevels
-
alignBlocks
private boolean alignBlocks
-
indexObjects
private boolean indexObjects
-
minUpdateIndex
private long minUpdateIndex
-
maxUpdateIndex
private long maxUpdateIndex
-
outputStream
private java.io.OutputStream outputStream
-
out
private ReftableOutputStream out
-
obj2ref
private ObjectIdSubclassMap<ReftableWriter.RefList> obj2ref
-
lastRef
private BlockWriter.Entry lastRef
-
lastLog
private BlockWriter.Entry lastLog
-
cur
private BlockWriter cur
-
refs
private ReftableWriter.Section refs
-
objs
private ReftableWriter.Section objs
-
logs
private ReftableWriter.Section logs
-
objIdLen
private int objIdLen
-
stats
private ReftableWriter.Stats stats
-
-
Constructor Detail
-
ReftableWriter
public ReftableWriter(java.io.OutputStream os)
Initialize a writer with a default configuration.- Parameters:
os
- output stream.
-
ReftableWriter
public ReftableWriter(ReftableConfig cfg, java.io.OutputStream os)
Initialize a writer with a configuration.- Parameters:
cfg
- configuration for the writeros
- output stream.
-
-
Method Detail
-
setConfig
public ReftableWriter setConfig(ReftableConfig cfg)
Set configuration for the writer.- Parameters:
cfg
- configuration for the writer.- Returns:
this
-
setMinUpdateIndex
public ReftableWriter setMinUpdateIndex(long min)
Set the minimum update index for log entries that appear in this reftable.- Parameters:
min
- the minimum update index for log entries that appear in this reftable. This should be 1 higher than the prior reftable'smaxUpdateIndex
if this table will be used in a stack.- Returns:
this
-
setMaxUpdateIndex
public ReftableWriter setMaxUpdateIndex(long max)
Set the maximum update index for log entries that appear in this reftable.- Parameters:
max
- the maximum update index for log entries that appear in this reftable. This should be at least 1 higher than the prior reftable'smaxUpdateIndex
if this table will be used in a stack.- Returns:
this
-
begin
public ReftableWriter begin()
Begin writing the reftable. Should be called only once. Call this if a stream was passed to the constructor.- Returns:
this
-
sortAndWriteRefs
public ReftableWriter sortAndWriteRefs(java.util.Collection<Ref> refsToPack) throws java.io.IOException
Sort a collection of references and write them to the reftable. The input refs may not have duplicate names.- Parameters:
refsToPack
- references to sort and write.- Returns:
this
- Throws:
java.io.IOException
- if reftable cannot be written.
-
writeRef
public void writeRef(Ref ref) throws java.io.IOException
Write one reference to the reftable.References must be passed in sorted order.
- Parameters:
ref
- the reference to store.- Throws:
java.io.IOException
- if reftable cannot be written.
-
writeRef
public void writeRef(Ref ref, long updateIndex) throws java.io.IOException
Write one reference to the reftable.References must be passed in sorted order.
- Parameters:
ref
- the reference to store.updateIndex
- the updateIndex that modified this reference. Must be>= minUpdateIndex
for this file.- Throws:
java.io.IOException
- if reftable cannot be written.
-
throwIllegalEntry
private void throwIllegalEntry(BlockWriter.Entry last, BlockWriter.Entry now)
-
indexRef
private void indexRef(Ref ref, long blockPos)
-
indexId
private void indexId(ObjectId id, long blockPos)
-
writeLog
public void writeLog(java.lang.String ref, long updateIndex, PersonIdent who, ObjectId oldId, ObjectId newId, @Nullable java.lang.String message) throws java.io.IOException
Write one reflog entry to the reftable.Reflog entries must be written in reference name and descending
updateIndex
(highest first) order.- Parameters:
ref
- name of the reference.updateIndex
- identifier of the transaction that created the log record. TheupdateIndex
must be unique within the scope ofref
, and must be within the bounds defined byminUpdateIndex <= updateIndex <= maxUpdateIndex
.who
- committer of the reflog entry.oldId
- prior id; passObjectId.zeroId()
for creations.newId
- new id; passObjectId.zeroId()
for deletions.message
- optional message (may be null).- Throws:
java.io.IOException
- if reftable cannot be written.
-
deleteLog
public void deleteLog(java.lang.String ref, long updateIndex) throws java.io.IOException
Record deletion of one reflog entry in this reftable.The deletion can shadow an entry stored in a lower table in the stack. This is useful for
refs/stash
and dropping an entry from its reflog.Deletion must be properly interleaved in sorted updateIndex order with any other logs written by
writeLog(String, long, PersonIdent, ObjectId, ObjectId, String)
.- Parameters:
ref
- the ref to delete (hide) a reflog entry from.updateIndex
- the update index that must be hidden.- Throws:
java.io.IOException
- if reftable cannot be written.
-
beginLog
private void beginLog() throws java.io.IOException
- Throws:
java.io.IOException
-
estimateTotalBytes
public long estimateTotalBytes()
Get an estimate of the current size in bytes of the reftable- Returns:
- an estimate of the current size in bytes of the reftable, if it
was finished right now. Estimate is only accurate if
ReftableConfig.setIndexObjects(boolean)
isfalse
andReftableConfig.setMaxIndexLevels(int)
is1
.
-
finish
public ReftableWriter finish() throws java.io.IOException
Finish writing the reftable by writing its trailer.- Returns:
this
- Throws:
java.io.IOException
- if reftable cannot be written.
-
finishRefAndObjSections
private void finishRefAndObjSections() throws java.io.IOException
- Throws:
java.io.IOException
-
writeObjBlocks
private void writeObjBlocks() throws java.io.IOException
- Throws:
java.io.IOException
-
finishLogSection
private void finishLogSection() throws java.io.IOException
- Throws:
java.io.IOException
-
shouldHaveIndex
private boolean shouldHaveIndex(ReftableWriter.IndexBuilder idx)
-
writeFileHeader
private void writeFileHeader()
-
encodeHeader
private void encodeHeader(byte[] hdr)
-
writeFileFooter
private void writeFileFooter()
-
firstBlockPosition
private static long firstBlockPosition(@Nullable ReftableWriter.Section s)
-
indexPosition
private static long indexPosition(@Nullable ReftableWriter.Section s)
-
getStats
public ReftableWriter.Stats getStats()
Get statistics of the last written reftable.- Returns:
- statistics of the last written reftable.
-
sortById
private static java.util.List<ReftableWriter.RefList> sortById(ObjectIdSubclassMap<ReftableWriter.RefList> m)
-
shortestUniqueAbbreviation
private static int shortestUniqueAbbreviation(java.util.List<ReftableWriter.RefList> in)
-
-