Search in sources :

Example 26 with StaticBuffer

use of org.janusgraph.diskstorage.StaticBuffer in project janusgraph by JanusGraph.

the class ColumnValueStore method mutate.

synchronized void mutate(List<Entry> additions, List<StaticBuffer> deletions, StoreTransaction txh) {
    // Prepare data
    Entry[] add;
    if (!additions.isEmpty()) {
        add = new Entry[additions.size()];
        int pos = 0;
        for (Entry e : additions) {
            add[pos] = e;
            pos++;
        }
        Arrays.sort(add);
    } else
        add = new Entry[0];
    // Filter out deletions that are also added
    Entry[] del;
    if (!deletions.isEmpty()) {
        del = new Entry[deletions.size()];
        int pos = 0;
        for (StaticBuffer deletion : deletions) {
            Entry delEntry = StaticArrayEntry.of(deletion);
            if (Arrays.binarySearch(add, delEntry) >= 0)
                continue;
            del[pos++] = delEntry;
        }
        if (pos < deletions.size())
            del = Arrays.copyOf(del, pos);
        Arrays.sort(del);
    } else
        del = new Entry[0];
    Lock lock = getLock(txh);
    lock.lock();
    try {
        Entry[] oldData = data.array;
        int oldSize = data.size;
        Entry[] newData = new Entry[oldSize + add.length];
        // Merge sort
        int i = 0, indexOld = 0, indexAdd = 0, indexDelete = 0;
        while (indexOld < oldSize) {
            Entry e = oldData[indexOld];
            indexOld++;
            // Compare with additions
            if (indexAdd < add.length) {
                int compare = e.compareTo(add[indexAdd]);
                if (compare >= 0) {
                    e = add[indexAdd];
                    indexAdd++;
                    // Skip duplicates
                    while (indexAdd < add.length && e.equals(add[indexAdd])) indexAdd++;
                }
                if (compare > 0)
                    indexOld--;
            }
            // Compare with deletions
            if (indexDelete < del.length) {
                int compare = e.compareTo(del[indexDelete]);
                if (compare == 0)
                    e = null;
                if (compare >= 0)
                    indexDelete++;
            }
            if (e != null) {
                newData[i] = e;
                i++;
            }
        }
        while (indexAdd < add.length) {
            newData[i] = add[indexAdd];
            i++;
            indexAdd++;
        }
        if (i * 1.0 / newData.length < SIZE_THRESHOLD) {
            // shrink array to free space
            Entry[] tempData = newData;
            newData = new Entry[i];
            System.arraycopy(tempData, 0, newData, 0, i);
        }
        data = new Data(newData, i);
    } finally {
        lock.unlock();
    }
}
Also used : StaticArrayEntry(org.janusgraph.diskstorage.util.StaticArrayEntry) Entry(org.janusgraph.diskstorage.Entry) StaticBuffer(org.janusgraph.diskstorage.StaticBuffer) ReentrantLock(java.util.concurrent.locks.ReentrantLock) NoLock(org.janusgraph.diskstorage.util.NoLock) Lock(java.util.concurrent.locks.Lock)

Example 27 with StaticBuffer

use of org.janusgraph.diskstorage.StaticBuffer in project janusgraph by JanusGraph.

the class OrderedKeyValueStoreManagerAdapter method mutateMany.

@Override
public void mutateMany(Map<String, Map<StaticBuffer, KCVMutation>> mutations, StoreTransaction txh) throws BackendException {
    final Map<String, KVMutation> converted = new HashMap<>(mutations.size());
    for (Map.Entry<String, Map<StaticBuffer, KCVMutation>> storeEntry : mutations.entrySet()) {
        OrderedKeyValueStoreAdapter store = openDatabase(storeEntry.getKey());
        Preconditions.checkNotNull(store);
        KVMutation mut = new KVMutation();
        for (Map.Entry<StaticBuffer, KCVMutation> entry : storeEntry.getValue().entrySet()) {
            StaticBuffer key = entry.getKey();
            KCVMutation mutation = entry.getValue();
            if (mutation.hasAdditions()) {
                for (Entry addition : mutation.getAdditions()) {
                    mut.addition(store.concatenate(key, addition));
                }
            }
            if (mutation.hasDeletions()) {
                for (StaticBuffer del : mutation.getDeletions()) {
                    mut.deletion(store.concatenate(key, del));
                }
            }
        }
        converted.put(storeEntry.getKey(), mut);
    }
    manager.mutateMany(converted, txh);
}
Also used : Entry(org.janusgraph.diskstorage.Entry) HashMap(java.util.HashMap) StaticBuffer(org.janusgraph.diskstorage.StaticBuffer) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) HashMap(java.util.HashMap)

Example 28 with StaticBuffer

use of org.janusgraph.diskstorage.StaticBuffer in project janusgraph by JanusGraph.

the class ConsistentKeyLockerSerializer method fromLockColumn.

public TimestampRid fromLockColumn(StaticBuffer lockKey, TimestampProvider provider) {
    ReadBuffer r = lockKey.asReadBuffer();
    int len = r.length();
    long tsNS = r.getLong();
    len -= 8;
    byte[] curRid = new byte[len];
    for (int i = 0; r.hasRemaining(); i++) {
        curRid[i] = r.getByte();
    }
    StaticBuffer rid = new StaticArrayBuffer(curRid);
    Instant time = provider.getTime(tsNS);
    return new TimestampRid(time, rid);
}
Also used : ReadBuffer(org.janusgraph.diskstorage.ReadBuffer) Instant(java.time.Instant) StaticArrayBuffer(org.janusgraph.diskstorage.util.StaticArrayBuffer) StaticBuffer(org.janusgraph.diskstorage.StaticBuffer)

Example 29 with StaticBuffer

use of org.janusgraph.diskstorage.StaticBuffer in project janusgraph by JanusGraph.

the class TransactionLogHeader method parse.

public static Entry parse(StaticBuffer buffer, Serializer serializer, TimestampProvider times) {
    ReadBuffer read = buffer.asReadBuffer();
    Instant txTimestamp = times.getTime(read.getLong());
    TransactionLogHeader header = new TransactionLogHeader(VariableLong.readPositive(read), txTimestamp, times);
    LogTxStatus status = serializer.readObjectNotNull(read, LogTxStatus.class);
    final EnumMap<LogTxMeta, Object> metadata = new EnumMap<>(LogTxMeta.class);
    int metaSize = VariableLong.unsignedByte(read.getByte());
    for (int i = 0; i < metaSize; i++) {
        LogTxMeta meta = LogTxMeta.values()[VariableLong.unsignedByte(read.getByte())];
        metadata.put(meta, serializer.readObjectNotNull(read, meta.dataType()));
    }
    if (read.hasRemaining()) {
        StaticBuffer content = read.subrange(read.getPosition(), read.length() - read.getPosition());
        return new Entry(header, content, status, metadata);
    } else {
        return new Entry(header, null, status, metadata);
    }
}
Also used : ReadBuffer(org.janusgraph.diskstorage.ReadBuffer) Instant(java.time.Instant) StaticBuffer(org.janusgraph.diskstorage.StaticBuffer)

Example 30 with StaticBuffer

use of org.janusgraph.diskstorage.StaticBuffer in project janusgraph by JanusGraph.

the class KCVSConfiguration method set.

public <O> void set(String key, O value, O expectedValue, final boolean checkExpectedValue) {
    final StaticBuffer column = string2StaticBuffer(key);
    final List<Entry> additions;
    final List<StaticBuffer> deletions;
    if (value != null) {
        // Addition
        additions = new ArrayList<>(1);
        deletions = KeyColumnValueStore.NO_DELETIONS;
        StaticBuffer val = object2StaticBuffer(value);
        additions.add(StaticArrayEntry.of(column, val));
    } else {
        // Deletion
        additions = KeyColumnValueStore.NO_ADDITIONS;
        deletions = Lists.newArrayList(column);
    }
    final StaticBuffer expectedValueBuffer;
    if (checkExpectedValue && expectedValue != null) {
        expectedValueBuffer = object2StaticBuffer(expectedValue);
    } else {
        expectedValueBuffer = null;
    }
    BackendOperation.execute(new BackendOperation.Transactional<Boolean>() {

        @Override
        public Boolean call(StoreTransaction txh) throws BackendException {
            if (checkExpectedValue)
                store.acquireLock(rowKey, column, expectedValueBuffer, txh);
            store.mutate(rowKey, additions, deletions, txh);
            return true;
        }

        @Override
        public String toString() {
            return "setConfiguration";
        }
    }, txProvider, times, maxOperationWaitTime);
}
Also used : StaticArrayEntry(org.janusgraph.diskstorage.util.StaticArrayEntry) Entry(org.janusgraph.diskstorage.Entry) StaticBuffer(org.janusgraph.diskstorage.StaticBuffer) BackendOperation(org.janusgraph.diskstorage.util.BackendOperation) BackendException(org.janusgraph.diskstorage.BackendException)

Aggregations

StaticBuffer (org.janusgraph.diskstorage.StaticBuffer)45 Test (org.junit.Test)17 Entry (org.janusgraph.diskstorage.Entry)14 ArrayList (java.util.ArrayList)10 ReadBuffer (org.janusgraph.diskstorage.ReadBuffer)8 DataOutput (org.janusgraph.graphdb.database.serialize.DataOutput)8 SliceQuery (org.janusgraph.diskstorage.keycolumnvalue.SliceQuery)7 ByteBuffer (java.nio.ByteBuffer)6 List (java.util.List)6 Map (java.util.Map)6 ResultSet (com.datastax.driver.core.ResultSet)5 ImmutableMap (com.google.common.collect.ImmutableMap)5 BackendException (org.janusgraph.diskstorage.BackendException)5 Row (com.datastax.driver.core.Row)4 Tuple (io.vavr.Tuple)4 Array (io.vavr.collection.Array)4 IOException (java.io.IOException)4 Instant (java.time.Instant)4 HashMap (java.util.HashMap)4 Random (java.util.Random)4