use of com.baidu.hugegraph.computer.core.store.entry.KvEntry in project hugegraph-computer by hugegraph.
the class HgkvDirTest method testHgkvDirBuilder.
@Test
public void testHgkvDirBuilder() throws IOException {
// The data must be ordered
List<Integer> data = ImmutableList.of(2, 3, 2, 1, 5, 2, 5, 9, 6, 2);
List<KvEntry> kvEntries = StoreTestUtil.kvEntriesFromMap(data);
String path = StoreTestUtil.availablePathById("1");
try (KvEntryFileWriter builder = new HgkvDirBuilderImpl(CONFIG, path)) {
for (KvEntry entry : kvEntries) {
builder.write(entry);
}
builder.finish();
// Open the file and determine the footer is as expected
HgkvDir dir = HgkvDirImpl.open(path);
Assert.assertEquals(HgkvFileImpl.MAGIC, dir.magic());
String version = HgkvDirImpl.MAJOR_VERSION + "." + HgkvDirImpl.MINOR_VERSION;
Assert.assertEquals(version, dir.version());
Assert.assertEquals(5, dir.numEntries());
int maxKey = StoreTestUtil.byteArrayToInt(dir.max());
Assert.assertEquals(6, maxKey);
int minKey = StoreTestUtil.byteArrayToInt(dir.min());
Assert.assertEquals(2, minKey);
} finally {
FileUtils.deleteQuietly(new File(path));
}
}
use of com.baidu.hugegraph.computer.core.store.entry.KvEntry in project hugegraph-computer by hugegraph.
the class HgkvFileTest method testHgkvFileReader.
@Test
public void testHgkvFileReader() throws Exception {
// The keys in the data must be ordered
List<Integer> data = testData();
String filePath = StoreTestUtil.availablePathById("1");
File file = StoreTestUtil.mapToHgkvFile(CONFIG, data, filePath);
KvEntryFileReader reader = new HgkvFileReaderImpl(file.getPath(), false);
try (EntryIterator iterator = reader.iterator()) {
int index = 0;
while (iterator.hasNext()) {
KvEntry next = iterator.next();
int key = StoreTestUtil.byteArrayToInt(next.key().bytes());
Assert.assertEquals(data.get(index).intValue(), key);
index += 2;
}
Assert.assertThrows(NoSuchElementException.class, iterator::next);
}
}
use of com.baidu.hugegraph.computer.core.store.entry.KvEntry in project hugegraph-computer by hugegraph.
the class StoreTestUtil method kvEntriesFromMap.
public static List<KvEntry> kvEntriesFromMap(List<Integer> map) throws IOException {
BytesOutput data = IOFactory.createBytesOutput(Constants.SMALL_BUF_SIZE);
Iterator<Integer> iterator = map.iterator();
while (iterator.hasNext()) {
// Write key length
writeData(data, iterator.next());
// Write value length
writeData(data, iterator.next());
}
BytesInput input = IOFactory.createBytesInput(data.buffer(), (int) data.position());
KvEntriesInput iter = new KvEntriesInput(input);
List<KvEntry> entries = new ArrayList<>();
while (iter.hasNext()) {
entries.add(iter.next());
}
iter.close();
return entries;
}
use of com.baidu.hugegraph.computer.core.store.entry.KvEntry in project hugegraph-computer by hugegraph.
the class StoreTestUtil method hgkvDirFromKvMap.
public static void hgkvDirFromKvMap(Config config, List<Integer> map, String path) throws IOException {
File file = new File(path);
try (KvEntryFileWriter builder = new HgkvDirBuilderImpl(config, path)) {
List<KvEntry> entries = StoreTestUtil.kvEntriesFromMap(map);
for (KvEntry entry : entries) {
builder.write(entry);
}
builder.finish();
} catch (Exception e) {
FileUtils.deleteQuietly(file);
throw e;
}
}
use of com.baidu.hugegraph.computer.core.store.entry.KvEntry in project hugegraph-computer by hugegraph.
the class StoreTestUtil method mapToHgkvFile.
public static File mapToHgkvFile(Config config, List<Integer> map, String path) throws IOException {
File file = new File(path);
try (HgkvFileBuilder builder = new HgkvFileBuilderImpl(config, path)) {
List<KvEntry> entries = StoreTestUtil.kvEntriesFromMap(map);
for (KvEntry entry : entries) {
builder.add(entry);
}
builder.finish();
/*
* Some fields are written in a variable-length way,
* so it's not recommended to assert length value.
*/
Assert.assertEquals(19, builder.headerLength());
} catch (Exception e) {
FileUtils.deleteQuietly(file);
throw e;
}
return file;
}
Aggregations