Search in sources :

Example 1 with KvEntry

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));
    }
}
Also used : KvEntry(com.baidu.hugegraph.computer.core.store.entry.KvEntry) HgkvDir(com.baidu.hugegraph.computer.core.store.file.hgkvfile.HgkvDir) File(java.io.File) HgkvDirBuilderImpl(com.baidu.hugegraph.computer.core.store.file.hgkvfile.builder.HgkvDirBuilderImpl) Test(org.junit.Test)

Example 2 with KvEntry

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);
    }
}
Also used : KvEntry(com.baidu.hugegraph.computer.core.store.entry.KvEntry) HgkvFileReaderImpl(com.baidu.hugegraph.computer.core.store.file.hgkvfile.reader.HgkvFileReaderImpl) HgkvFile(com.baidu.hugegraph.computer.core.store.file.hgkvfile.HgkvFile) File(java.io.File) Test(org.junit.Test)

Example 3 with KvEntry

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;
}
Also used : BytesOutput(com.baidu.hugegraph.computer.core.io.BytesOutput) BytesInput(com.baidu.hugegraph.computer.core.io.BytesInput) KvEntry(com.baidu.hugegraph.computer.core.store.entry.KvEntry) ArrayList(java.util.ArrayList) KvEntriesInput(com.baidu.hugegraph.computer.core.store.buffer.KvEntriesInput)

Example 4 with KvEntry

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;
    }
}
Also used : KvEntry(com.baidu.hugegraph.computer.core.store.entry.KvEntry) File(java.io.File) HgkvDirBuilderImpl(com.baidu.hugegraph.computer.core.store.file.hgkvfile.builder.HgkvDirBuilderImpl) IOException(java.io.IOException)

Example 5 with KvEntry

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;
}
Also used : HgkvFileBuilderImpl(com.baidu.hugegraph.computer.core.store.file.hgkvfile.builder.HgkvFileBuilderImpl) HgkvFileBuilder(com.baidu.hugegraph.computer.core.store.file.hgkvfile.builder.HgkvFileBuilder) KvEntry(com.baidu.hugegraph.computer.core.store.entry.KvEntry) File(java.io.File) IOException(java.io.IOException)

Aggregations

KvEntry (com.baidu.hugegraph.computer.core.store.entry.KvEntry)38 Test (org.junit.Test)13 BytesId (com.baidu.hugegraph.computer.core.graph.id.BytesId)9 Id (com.baidu.hugegraph.computer.core.graph.id.Id)9 BytesInput (com.baidu.hugegraph.computer.core.io.BytesInput)9 Pointer (com.baidu.hugegraph.computer.core.store.entry.Pointer)9 BytesOutput (com.baidu.hugegraph.computer.core.io.BytesOutput)6 RandomAccessInput (com.baidu.hugegraph.computer.core.io.RandomAccessInput)5 Sorter (com.baidu.hugegraph.computer.core.sort.Sorter)5 ArrayList (java.util.ArrayList)5 IntValueSumCombiner (com.baidu.hugegraph.computer.core.combiner.IntValueSumCombiner)4 PointerCombiner (com.baidu.hugegraph.computer.core.combiner.PointerCombiner)4 IntValue (com.baidu.hugegraph.computer.core.graph.value.IntValue)4 CombineKvOuterSortFlusher (com.baidu.hugegraph.computer.core.sort.flusher.CombineKvOuterSortFlusher)4 DefaultKvEntry (com.baidu.hugegraph.computer.core.store.entry.DefaultKvEntry)4 File (java.io.File)4 IOException (java.io.IOException)4 Properties (com.baidu.hugegraph.computer.core.graph.properties.Properties)3 LongValue (com.baidu.hugegraph.computer.core.graph.value.LongValue)3 OuterSortFlusher (com.baidu.hugegraph.computer.core.sort.flusher.OuterSortFlusher)3