Skip to content
Permalink
Browse files
Fix file descriptor leak (#116)
  • Loading branch information
corgiboygsj committed Oct 14, 2021
1 parent 7a49330 commit 5f06b65162b7ec0eb71c806d616cea70bb49cfbb
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
@@ -73,7 +73,6 @@ public void write(KvEntry entry) throws IOException {
long entrySize = this.segmentBuilder.sizeOfEntry(entry);
long segmentSize = this.segmentBuilder.dataLength();
if ((entrySize + segmentSize) > this.maxEntriesBytes) {
this.segmentBuilder.finish();
// Create new hgkvFile.
this.segmentBuilder = nextSegmentBuilder(this.config, this.dir,
++this.segmentId);
@@ -95,10 +94,12 @@ public void close() throws IOException {
this.finish();
}

private static HgkvFileBuilder nextSegmentBuilder(Config config,
HgkvDir dir,
int segmentId)
throws IOException {
private HgkvFileBuilder nextSegmentBuilder(Config config, HgkvDir dir,
int segmentId)
throws IOException {
if (this.segmentBuilder != null) {
this.segmentBuilder.finish();
}
String fileName = StringUtils.join(HgkvDirImpl.FILE_NAME_PREFIX,
String.valueOf(segmentId),
HgkvDirImpl.FILE_EXTEND_NAME);
@@ -36,6 +36,7 @@ public class HgkvFileBuilderImpl implements HgkvFileBuilder {
// Max entries size of a block
private final long maxDataBlockSize;

private final HgkvFile file;
private final RandomAccessOutput output;
private final BlockBuilder dataBlockBuilder;
private final IndexBlockBuilder indexBlockBuilder;
@@ -51,8 +52,8 @@ public class HgkvFileBuilderImpl implements HgkvFileBuilder {

public HgkvFileBuilderImpl(Config config, String path) throws IOException {
this.maxDataBlockSize = config.get(ComputerOptions.HGKV_DATABLOCK_SIZE);
HgkvFile hgkvFile = HgkvFileImpl.create(path);
this.output = hgkvFile.output();
this.file = HgkvFileImpl.create(path);
this.output = this.file.output();
this.dataBlockBuilder = new DataBlockBuilderImpl(this.output);
this.indexBlockBuilder = new IndexBlockBuilderImpl(this.output);
this.buildFinished = false;
@@ -94,6 +95,7 @@ public void finish() throws IOException {
this.writeIndexBlock();
this.writeFooter();
this.output.close();
this.file.close();
this.buildFinished = true;
}

@@ -58,6 +58,7 @@ public EntryIterator iterator() throws IOException {

private static class EntryIter implements EntryIterator {

private final HgkvFile file;
private final RandomAccessInput input;
private final RandomAccessInput userAccessInput;
private long numEntries;
@@ -67,8 +68,9 @@ private static class EntryIter implements EntryIterator {
public EntryIter(HgkvFile hgkvFile, boolean useInlinePointer,
boolean withSubKv)
throws IOException {
this.numEntries = hgkvFile.numEntries();
File file = new File(hgkvFile.path());
this.file = hgkvFile;
this.numEntries = this.file.numEntries();
File file = new File(this.file.path());
this.input = IOFactory.createFileInput(file);
this.userAccessInput = this.input.duplicate();
this.useInlinePointer = useInlinePointer;
@@ -95,6 +97,7 @@ public KvEntry next() {

@Override
public void close() throws IOException {
this.file.close();
this.input.close();
this.userAccessInput.close();
}

0 comments on commit 5f06b65

Please sign in to comment.