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();
}
}
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);
}
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);
}
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);
}
}
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);
}
Aggregations