Skip to content
Permalink
Browse files
fix bug: close file stream ahead at input stage (#118)
* delay close file stream

* tiny improve
  • Loading branch information
corgiboygsj committed Oct 15, 2021
1 parent 614d8a0 commit dec7e2993df09610a43e0dfa034c66530707ce2c
Showing 1 changed file with 22 additions and 23 deletions.
@@ -20,7 +20,8 @@
package com.baidu.hugegraph.computer.core.store.hgkvfile.file.reader;

import java.io.IOException;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;

import com.baidu.hugegraph.computer.core.common.exception.ComputerException;
@@ -67,7 +68,9 @@ public EntryIterator iterator() {

private static class HgkvDirEntryIter implements EntryIterator {

private final Iterator<HgkvFile> segments;
private final List<HgkvFile> segments;
private final List<EntryIterator> segmentsIters;
private int segmentIndex;
private long numEntries;
private EntryIterator kvIter;
private final boolean useInlinePointer;
@@ -76,7 +79,9 @@ private static class HgkvDirEntryIter implements EntryIterator {
public HgkvDirEntryIter(HgkvDir hgkvDir, boolean useInlinePointer,
boolean withSubKv)
throws IOException {
this.segments = hgkvDir.segments().iterator();
this.segments = hgkvDir.segments();
this.segmentsIters = new ArrayList<>();
this.segmentIndex = 0;
this.numEntries = hgkvDir.numEntries();
this.kvIter = null;
this.useInlinePointer = useInlinePointer;
@@ -95,11 +100,7 @@ public KvEntry next() {
}

try {
if (this.kvIter == null) {
this.kvIter = this.nextKeyIter();
}
if (!this.kvIter.hasNext()) {
this.kvIter.close();
if (this.kvIter == null || !this.kvIter.hasNext()) {
this.kvIter = this.nextKeyIter();
}
this.numEntries--;
@@ -111,24 +112,22 @@ public KvEntry next() {

@Override
public void close() throws Exception {
this.kvIter.close();
for (EntryIterator iterator : this.segmentsIters) {
iterator.close();
}
for (HgkvFile segment : this.segments) {
segment.close();
}
}

private EntryIterator nextKeyIter() throws Exception {
EntryIterator iterator;
while (this.segments.hasNext()) {
HgkvFile segment = this.segments.next();
HgkvFileReader reader = new HgkvFileReaderImpl(
segment.path(), this.useInlinePointer,
this.withSubKv);
iterator = reader.iterator();
if (iterator.hasNext()) {
return iterator;
} else {
iterator.close();
}
}
throw new NoSuchElementException();
HgkvFile segment = this.segments.get(this.segmentIndex++);
HgkvFileReader reader = new HgkvFileReaderImpl(
segment.path(), this.useInlinePointer,
this.withSubKv);
EntryIterator iterator = reader.iterator();
this.segmentsIters.add(iterator);
return iterator;
}

private boolean hasNextKey() {

0 comments on commit dec7e29

Please sign in to comment.