use of com.baidu.hugegraph.computer.core.store.file.seqfile.ValueFileInput in project hugegraph-computer by hugegraph.
the class ValueFileTest method testSeek.
@Test
public void testSeek() throws IOException {
File dir = createTempDir();
final int bufferSize = 15;
try {
// Test ValueFileInput
try (ValueFileOutput output = new ValueFileOutput(CONFIG, dir, bufferSize)) {
for (int i = 0; i < 50; i++) {
output.writeInt(i);
}
}
try (ValueFileInput input = new ValueFileInput(CONFIG, dir, bufferSize)) {
Long fileLength = Whitebox.getInternalState(input, "fileLength");
Assert.assertEquals(200L, fileLength);
long position;
// Position in buffer
position = 13L;
assertSeek(fileLength, input, position);
// Position not in buffer but in current segment
position = 28L;
assertSeek(fileLength, input, position);
// Position not in buffer and not in current segment
position = 200L;
assertSeek(fileLength, input, position);
position = 3L;
assertSeek(fileLength, input, position);
position = 3L;
assertSeek(fileLength, input, position);
position = 68L;
assertSeek(fileLength, input, position);
position = 0L;
assertSeek(fileLength, input, position);
// Position out of bound
position = 300L;
long finalPosition = position;
Assert.assertThrows(EOFException.class, () -> {
input.seek(finalPosition);
}, e -> {
Assert.assertContains("reach the end of file", e.getMessage());
});
input.seek(16 * Integer.BYTES);
Assert.assertEquals(16, input.readInt());
// Random seek and assert data
Random random = new Random();
for (int i = 0; i < 10; i++) {
int num = random.nextInt(49);
input.seek(num * Integer.BYTES);
Assert.assertEquals(num, input.readInt());
}
}
// Test ValueFileOutput
try (ValueFileOutput output = new ValueFileOutput(CONFIG, dir, bufferSize)) {
// 200, 100, 2, 3, 4.....
for (int i = 0; i < 50; i++) {
// Position in buffer
if (i == 2) {
output.seek(4);
output.writeInt(100);
}
// Position not int buffer
if (i == 10) {
long oldPosition = output.position();
output.seek(0);
output.writeInt(200);
output.seek(oldPosition);
}
output.writeInt(i);
}
Assert.assertThrows(EOFException.class, () -> {
output.seek(500L);
}, e -> {
Assert.assertContains("reach the end of file", e.getMessage());
});
}
try (ValueFileInput input = new ValueFileInput(CONFIG, dir, bufferSize)) {
for (int i = 0; i < 50; i++) {
if (i == 0) {
Assert.assertEquals(200, input.readInt());
continue;
}
if (i == 1) {
Assert.assertEquals(100, input.readInt());
continue;
}
Assert.assertEquals(i, input.readInt());
}
}
} finally {
FileUtils.deleteQuietly(dir);
}
}
use of com.baidu.hugegraph.computer.core.store.file.seqfile.ValueFileInput in project hugegraph-computer by hugegraph.
the class ValueFileTest method testConstructor.
@Test
public void testConstructor() throws IOException {
int bufferCapacity = 13;
File dir = createTempDir();
try {
try (ValueFileOutput output = new ValueFileOutput(CONFIG, dir, bufferCapacity)) {
Assert.assertEquals(0, output.position());
}
Assert.assertThrows(IllegalArgumentException.class, () -> {
new ValueFileOutput(CONFIG, dir, 1);
}, e -> {
Assert.assertContains("bufferCapacity must be >= 8", e.getMessage());
});
try (ValueFileInput input = new ValueFileInput(CONFIG, dir, bufferCapacity)) {
Assert.assertEquals(0, input.position());
}
Assert.assertThrows(IllegalArgumentException.class, () -> {
new ValueFileInput(CONFIG, dir, 1);
}, e -> {
Assert.assertContains("The parameter bufferSize must be >= 8", e.getMessage());
});
Config config = UnitTestBase.updateWithRequiredOptions(ComputerOptions.VALUE_FILE_MAX_SEGMENT_SIZE, String.valueOf(Integer.MAX_VALUE));
try (ValueFileOutput output = new ValueFileOutput(config, dir)) {
Assert.assertEquals(0, output.position());
}
try (ValueFileInput input = new ValueFileInput(config, dir)) {
Assert.assertEquals(0, input.position());
}
} finally {
FileUtils.deleteQuietly(dir);
}
}
use of com.baidu.hugegraph.computer.core.store.file.seqfile.ValueFileInput in project hugegraph-computer by hugegraph.
the class ValueFileTest method testWriteInt.
@Test
public void testWriteInt() throws IOException {
File dir = createTempDir();
final int bufferSize = 10;
try {
try (ValueFileOutput output = new ValueFileOutput(CONFIG, dir, bufferSize)) {
for (int i = 0; i < 50; i++) {
output.writeInt(i);
}
}
try (ValueFileInput input = new ValueFileInput(CONFIG, dir, bufferSize)) {
for (int i = 0; i < 50; i++) {
Assert.assertEquals(i, input.readInt());
}
Assert.assertThrows(IOException.class, input::readInt, e -> {
Assert.assertContains("overflows buffer", e.getMessage());
});
}
} finally {
FileUtils.deleteQuietly(dir);
}
}
use of com.baidu.hugegraph.computer.core.store.file.seqfile.ValueFileInput in project hugegraph-computer by hugegraph.
the class ValueFileTest method testDuplicate.
@Test
public void testDuplicate() throws IOException {
File dir = createTempDir();
final int bufferSize = 15;
try {
try (ValueFileOutput output = new ValueFileOutput(CONFIG, dir, bufferSize)) {
for (int i = 0; i < 50; i++) {
output.writeInt(i);
}
}
try (ValueFileInput input = new ValueFileInput(CONFIG, dir, bufferSize)) {
input.seek(20 * Integer.BYTES);
Assert.assertEquals(20, input.readInt());
UnsafeBytesInput other = input.duplicate();
Assert.assertEquals(21, other.readInt());
Assert.assertEquals(21, input.readInt());
}
} finally {
FileUtils.deleteQuietly(dir);
}
}
use of com.baidu.hugegraph.computer.core.store.file.seqfile.ValueFileInput in project hugegraph-computer by hugegraph.
the class ValueFileTest method testByteArray.
@Test
public void testByteArray() throws IOException {
File dir = createTempDir();
final int bufferSize = 13;
byte[] bytes;
try {
try (ValueFileOutput output = new ValueFileOutput(CONFIG, dir, bufferSize)) {
// The remaining capacity of the buffer can hold
bytes = orderBytesBySize(5);
output.write(bytes);
/*
* The remaining capacity of the buffer can't hold and write
* size is smaller than buffer capacity
*/
bytes = orderBytesBySize(11);
output.write(bytes);
/*
* Write size is greater than buffer capacity and remaining of
* segment can hold write size
*/
bytes = orderBytesBySize(14);
output.write(bytes);
/*
* Write size is greater than buffer capacity and remaining of
* segment can't hold write size
*/
bytes = orderBytesBySize(80);
output.write(bytes);
}
try (ValueFileInput input = new ValueFileInput(CONFIG, dir, bufferSize)) {
assertBytes(5, input.readBytes(5));
assertBytes(11, input.readBytes(11));
assertBytes(14, input.readBytes(14));
assertBytes(80, input.readBytes(80));
}
} finally {
FileUtils.deleteQuietly(dir);
}
}
Aggregations