Search in sources :

Example 1 with StaticArrayEntry

use of org.janusgraph.diskstorage.util.StaticArrayEntry in project janusgraph by JanusGraph.

the class EdgeSerializer method writeRelation.

public StaticArrayEntry writeRelation(InternalRelation relation, InternalRelationType type, int position, TypeInspector tx) {
    assert type == relation.getType() || (type.getBaseType() != null && type.getBaseType().equals(relation.getType()));
    Direction dir = EdgeDirection.fromPosition(position);
    Preconditions.checkArgument(type.isUnidirected(Direction.BOTH) || type.isUnidirected(dir));
    long typeId = type.longId();
    DirectionID dirID = getDirID(dir, relation.isProperty() ? RelationCategory.PROPERTY : RelationCategory.EDGE);
    DataOutput out = serializer.getDataOutput(DEFAULT_CAPACITY);
    int valuePosition;
    IDHandler.writeRelationType(out, typeId, dirID, type.isInvisibleType());
    Multiplicity multiplicity = type.multiplicity();
    long[] sortKey = type.getSortKey();
    assert !multiplicity.isConstrained() || sortKey.length == 0 : type.name();
    int keyStartPos = out.getPosition();
    if (!multiplicity.isConstrained()) {
        writeInlineTypes(sortKey, relation, out, tx, InlineType.KEY);
    }
    int keyEndPos = out.getPosition();
    long relationId = relation.longId();
    // How multiplicity is handled for edges and properties is slightly different
    if (relation.isEdge()) {
        long otherVertexId = relation.getVertex((position + 1) % 2).longId();
        if (multiplicity.isConstrained()) {
            if (multiplicity.isUnique(dir)) {
                valuePosition = out.getPosition();
                VariableLong.writePositive(out, otherVertexId);
            } else {
                VariableLong.writePositiveBackward(out, otherVertexId);
                valuePosition = out.getPosition();
            }
            VariableLong.writePositive(out, relationId);
        } else {
            VariableLong.writePositiveBackward(out, otherVertexId);
            VariableLong.writePositiveBackward(out, relationId);
            valuePosition = out.getPosition();
        }
    } else {
        assert relation.isProperty();
        Preconditions.checkArgument(relation.isProperty());
        Object value = ((JanusGraphVertexProperty) relation).value();
        Preconditions.checkNotNull(value);
        PropertyKey key = (PropertyKey) type;
        assert key.dataType().isInstance(value);
        if (multiplicity.isConstrained()) {
            if (multiplicity.isUnique(dir)) {
                // Cardinality=SINGLE
                valuePosition = out.getPosition();
                writePropertyValue(out, key, value);
            } else {
                // Cardinality=SET
                writePropertyValue(out, key, value);
                valuePosition = out.getPosition();
            }
            VariableLong.writePositive(out, relationId);
        } else {
            assert multiplicity.getCardinality() == Cardinality.LIST;
            VariableLong.writePositiveBackward(out, relationId);
            valuePosition = out.getPosition();
            writePropertyValue(out, key, value);
        }
    }
    // Write signature
    long[] signature = type.getSignature();
    writeInlineTypes(signature, relation, out, tx, InlineType.SIGNATURE);
    // Write remaining properties
    LongSet writtenTypes = new LongHashSet(sortKey.length + signature.length);
    if (sortKey.length > 0 || signature.length > 0) {
        for (long id : sortKey) writtenTypes.add(id);
        for (long id : signature) writtenTypes.add(id);
    }
    LongArrayList remainingTypes = new LongArrayList(8);
    for (PropertyKey t : relation.getPropertyKeysDirect()) {
        if (!(t instanceof ImplicitKey) && !writtenTypes.contains(t.longId())) {
            remainingTypes.add(t.longId());
        }
    }
    // Sort types before writing to ensure that value is always written the same way
    long[] remaining = remainingTypes.toArray();
    Arrays.sort(remaining);
    for (long tid : remaining) {
        PropertyKey t = tx.getExistingPropertyKey(tid);
        writeInline(out, t, relation.getValueDirect(t), InlineType.NORMAL);
    }
    assert valuePosition > 0;
    return new StaticArrayEntry(type.getSortOrder() == Order.DESC ? out.getStaticBufferFlipBytes(keyStartPos, keyEndPos) : out.getStaticBuffer(), valuePosition);
}
Also used : DataOutput(org.janusgraph.graphdb.database.serialize.DataOutput) LongArrayList(com.carrotsearch.hppc.LongArrayList) LongSet(com.carrotsearch.hppc.LongSet) EdgeDirection(org.janusgraph.graphdb.relations.EdgeDirection) Direction(org.apache.tinkerpop.gremlin.structure.Direction) StaticArrayEntry(org.janusgraph.diskstorage.util.StaticArrayEntry) LongHashSet(com.carrotsearch.hppc.LongHashSet) ImplicitKey(org.janusgraph.graphdb.types.system.ImplicitKey) DirectionID(org.janusgraph.graphdb.database.idhandling.IDHandler.DirectionID)

Example 2 with StaticArrayEntry

use of org.janusgraph.diskstorage.util.StaticArrayEntry in project janusgraph by JanusGraph.

the class IndexSerializer method getIndexEntry.

private Entry getIndexEntry(CompositeIndexType index, RecordEntry[] record, JanusGraphElement element) {
    DataOutput out = serializer.getDataOutput(1 + 8 + 8 * record.length + 4 * 8);
    out.putByte(FIRST_INDEX_COLUMN_BYTE);
    if (index.getCardinality() != Cardinality.SINGLE) {
        VariableLong.writePositive(out, element.longId());
        if (index.getCardinality() != Cardinality.SET) {
            for (RecordEntry re : record) {
                VariableLong.writePositive(out, re.relationId);
            }
        }
    }
    int valuePosition = out.getPosition();
    if (element instanceof JanusGraphVertex) {
        VariableLong.writePositive(out, element.longId());
    } else {
        assert element instanceof JanusGraphRelation;
        RelationIdentifier rid = (RelationIdentifier) element.id();
        long[] longs = rid.getLongRepresentation();
        Preconditions.checkArgument(longs.length == 3 || longs.length == 4);
        for (long aLong : longs) VariableLong.writePositive(out, aLong);
    }
    return new StaticArrayEntry(out.getStaticBuffer(), valuePosition);
}
Also used : DataOutput(org.janusgraph.graphdb.database.serialize.DataOutput) RelationIdentifier(org.janusgraph.graphdb.relations.RelationIdentifier) StaticArrayEntry(org.janusgraph.diskstorage.util.StaticArrayEntry)

Example 3 with StaticArrayEntry

use of org.janusgraph.diskstorage.util.StaticArrayEntry in project janusgraph by JanusGraph.

the class StaticArrayEntryTest method testInversion.

@Test
public void testInversion() {
    WriteBuffer wb = new WriteByteBuffer(20);
    wb.putInt(1).putInt(2).putInt(3).putInt(4);
    Entry entry = new StaticArrayEntry(wb.getStaticBufferFlipBytes(4, 2 * 4), 3 * 4);
    ReadBuffer rb = entry.asReadBuffer();
    assertEquals(1, rb.getInt());
    assertEquals(2, rb.subrange(4, true).getInt());
    assertEquals(~2, rb.getInt());
    assertEquals(3, rb.getInt());
    assertEquals(4, rb.getInt());
    rb.movePositionTo(entry.getValuePosition());
    assertEquals(4, rb.getInt());
}
Also used : StaticArrayEntry(org.janusgraph.diskstorage.util.StaticArrayEntry) WriteByteBuffer(org.janusgraph.diskstorage.util.WriteByteBuffer) StaticArrayEntry(org.janusgraph.diskstorage.util.StaticArrayEntry) Test(org.junit.Test)

Example 4 with StaticArrayEntry

use of org.janusgraph.diskstorage.util.StaticArrayEntry in project janusgraph by JanusGraph.

the class StaticArrayEntryTest method testArrayBuffer.

@Test
public void testArrayBuffer() {
    WriteBuffer wb = new WriteByteBuffer(128);
    wb.putInt(1).putInt(2).putInt(3).putInt(4);
    int valuePos = wb.getPosition();
    wb.putInt(5).putInt(6);
    Entry entry = new StaticArrayEntry(wb.getStaticBuffer(), valuePos);
    assertEquals(4 * 4, entry.getValuePosition());
    assertEquals(6 * 4, entry.length());
    assertTrue(entry.hasValue());
    for (int i = 1; i <= 6; i++) assertEquals(i, entry.getInt((i - 1) * 4));
    ReadBuffer rb = entry.asReadBuffer();
    for (int i = 1; i <= 6; i++) assertEquals(i, rb.getInt());
    assertFalse(rb.hasRemaining());
    assertNull(entry.getCache());
    entry.setCache(cache);
    assertEquals(cache, entry.getCache());
    rb = entry.getColumnAs(StaticBuffer.STATIC_FACTORY).asReadBuffer();
    for (int i = 1; i <= 4; i++) assertEquals(i, rb.getInt());
    assertFalse(rb.hasRemaining());
    rb = entry.getValueAs(StaticBuffer.STATIC_FACTORY).asReadBuffer();
    for (int i = 5; i <= 6; i++) assertEquals(i, rb.getInt());
    assertFalse(rb.hasRemaining());
}
Also used : StaticArrayEntry(org.janusgraph.diskstorage.util.StaticArrayEntry) WriteByteBuffer(org.janusgraph.diskstorage.util.WriteByteBuffer) StaticArrayEntry(org.janusgraph.diskstorage.util.StaticArrayEntry) Test(org.junit.Test)

Example 5 with StaticArrayEntry

use of org.janusgraph.diskstorage.util.StaticArrayEntry in project janusgraph by JanusGraph.

the class HBaseStoreManagerMutationTest method testKCVMutationToPuts.

@Test
public void testKCVMutationToPuts() throws Exception {
    final Map<String, Map<StaticBuffer, KCVMutation>> storeMutationMap = new HashMap<>();
    final Map<StaticBuffer, KCVMutation> rowkeyMutationMap = new HashMap<>();
    final List<Long> expectedColumnsWithTTL = new ArrayList<>();
    final List<Long> expectedColumnsWithoutTTL = new ArrayList<>();
    final List<Long> expectedColumnDelete = new ArrayList<>();
    StaticArrayEntry e = null;
    StaticBuffer rowkey, col, val;
    // 2 rows
    for (int row = 0; row < 2; row++) {
        rowkey = KeyColumnValueStoreUtil.longToByteBuffer(row);
        List<Entry> additions = new ArrayList<>();
        List<StaticBuffer> deletions = new ArrayList<>();
        // 100 columns each row
        int i;
        for (i = 0; i < 100; i++) {
            col = KeyColumnValueStoreUtil.longToByteBuffer(i);
            val = KeyColumnValueStoreUtil.longToByteBuffer(i + 100);
            e = (StaticArrayEntry) StaticArrayEntry.of(col, val);
            // Set half of the columns with TTL, also vary the TTL values
            if (i % 2 == 0) {
                e.setMetaData(EntryMetaData.TTL, i % 10 + 1);
                // Collect the columns with TTL. Only do this for one row
                if (row == 1) {
                    expectedColumnsWithTTL.add((long) i);
                }
                additions.add(e);
            } else {
                // Collect the columns without TTL. Only do this for one row
                if (row == 1) {
                    expectedColumnsWithoutTTL.add((long) i);
                }
                additions.add(e);
            }
        }
        // Add one deletion to the row
        if (row == 1) {
            expectedColumnDelete.add((long) (i - 1));
        }
        deletions.add(e);
        rowkeyMutationMap.put(rowkey, new KCVMutation(additions, deletions));
    }
    storeMutationMap.put("store1", rowkeyMutationMap);
    HBaseStoreManager manager = new HBaseStoreManager(HBaseStorageSetup.getHBaseConfiguration());
    final Map<StaticBuffer, Pair<List<Put>, Delete>> commandsPerRowKey = manager.convertToCommands(storeMutationMap, 0, 0);
    // 2 rows
    Assert.assertEquals(commandsPerRowKey.size(), 2);
    // Verify puts
    final List<Long> putColumnsWithTTL = new ArrayList<>();
    final List<Long> putColumnsWithoutTTL = new ArrayList<>();
    Pair<List<Put>, Delete> commands = commandsPerRowKey.values().iterator().next();
    long colName;
    for (Put p : commands.getFirst()) {
        // In Put, Long.MAX_VALUE means no TTL
        for (Map.Entry<byte[], List<Cell>> me : p.getFamilyCellMap().entrySet()) {
            for (Cell c : me.getValue()) {
                colName = KeyColumnValueStoreUtil.bufferToLong(new StaticArrayBuffer(CellUtil.cloneQualifier(c)));
                if (p.getTTL() < Long.MAX_VALUE) {
                    putColumnsWithTTL.add(colName);
                } else {
                    putColumnsWithoutTTL.add(colName);
                }
            }
        }
    }
    Collections.sort(putColumnsWithoutTTL);
    Collections.sort(putColumnsWithTTL);
    Assert.assertArrayEquals(expectedColumnsWithoutTTL.toArray(), putColumnsWithoutTTL.toArray());
    Assert.assertArrayEquals(expectedColumnsWithTTL.toArray(), putColumnsWithTTL.toArray());
    // Verify deletes
    final List<Long> deleteColumns = new ArrayList<>();
    Delete d = commands.getSecond();
    for (Map.Entry<byte[], List<Cell>> me : d.getFamilyCellMap().entrySet()) {
        for (Cell c : me.getValue()) {
            colName = KeyColumnValueStoreUtil.bufferToLong(new StaticArrayBuffer(CellUtil.cloneQualifier(c)));
            deleteColumns.add(colName);
        }
    }
    Collections.sort(deleteColumns);
    Assert.assertArrayEquals(expectedColumnDelete.toArray(), deleteColumns.toArray());
}
Also used : Delete(org.apache.hadoop.hbase.client.Delete) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) StaticArrayEntry(org.janusgraph.diskstorage.util.StaticArrayEntry) StaticArrayEntry(org.janusgraph.diskstorage.util.StaticArrayEntry) Entry(org.janusgraph.diskstorage.Entry) StaticBuffer(org.janusgraph.diskstorage.StaticBuffer) ArrayList(java.util.ArrayList) List(java.util.List) Cell(org.apache.hadoop.hbase.Cell) Pair(org.apache.hadoop.hbase.util.Pair) StaticArrayBuffer(org.janusgraph.diskstorage.util.StaticArrayBuffer) KCVMutation(org.janusgraph.diskstorage.keycolumnvalue.KCVMutation) Put(org.apache.hadoop.hbase.client.Put) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

StaticArrayEntry (org.janusgraph.diskstorage.util.StaticArrayEntry)7 Test (org.junit.Test)4 WriteByteBuffer (org.janusgraph.diskstorage.util.WriteByteBuffer)3 LongArrayList (com.carrotsearch.hppc.LongArrayList)2 DataOutput (org.janusgraph.graphdb.database.serialize.DataOutput)2 LongHashSet (com.carrotsearch.hppc.LongHashSet)1 LongSet (com.carrotsearch.hppc.LongSet)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Cell (org.apache.hadoop.hbase.Cell)1 Delete (org.apache.hadoop.hbase.client.Delete)1 Put (org.apache.hadoop.hbase.client.Put)1 Pair (org.apache.hadoop.hbase.util.Pair)1 Direction (org.apache.tinkerpop.gremlin.structure.Direction)1 Entry (org.janusgraph.diskstorage.Entry)1 StaticBuffer (org.janusgraph.diskstorage.StaticBuffer)1 IndexEntry (org.janusgraph.diskstorage.indexing.IndexEntry)1