use of com.thinkaurelius.titan.diskstorage.StaticBuffer in project incubator-atlas by apache.
the class HBaseStoreManager method convertToCommands.
/**
* Convert Titan internal Mutation representation into HBase native commands.
*
* @param mutations Mutations to convert into HBase commands.
* @param putTimestamp The timestamp to use for Put commands.
* @param delTimestamp The timestamp to use for Delete commands.
* @return Commands sorted by key converted from Titan internal representation.
* @throws com.thinkaurelius.titan.diskstorage.PermanentBackendException
*/
private Map<StaticBuffer, Pair<Put, Delete>> convertToCommands(Map<String, Map<StaticBuffer, KCVMutation>> mutations, final long putTimestamp, final long delTimestamp) throws PermanentBackendException {
Map<StaticBuffer, Pair<Put, Delete>> commandsPerKey = new HashMap<>();
for (Map.Entry<String, Map<StaticBuffer, KCVMutation>> entry : mutations.entrySet()) {
String cfString = getCfNameForStoreName(entry.getKey());
byte[] cfName = cfString.getBytes();
for (Map.Entry<StaticBuffer, KCVMutation> m : entry.getValue().entrySet()) {
byte[] key = m.getKey().as(StaticBuffer.ARRAY_FACTORY);
KCVMutation mutation = m.getValue();
Pair<Put, Delete> commands = commandsPerKey.get(m.getKey());
if (commands == null) {
commands = new Pair<>();
commandsPerKey.put(m.getKey(), commands);
}
if (mutation.hasDeletions()) {
if (commands.getSecond() == null) {
Delete d = new Delete(key);
compat.setTimestamp(d, delTimestamp);
commands.setSecond(d);
}
for (StaticBuffer b : mutation.getDeletions()) {
commands.getSecond().deleteColumns(cfName, b.as(StaticBuffer.ARRAY_FACTORY), delTimestamp);
}
}
if (mutation.hasAdditions()) {
if (commands.getFirst() == null) {
Put p = new Put(key, putTimestamp);
commands.setFirst(p);
}
for (Entry e : mutation.getAdditions()) {
commands.getFirst().add(cfName, e.getColumnAs(StaticBuffer.ARRAY_FACTORY), putTimestamp, e.getValueAs(StaticBuffer.ARRAY_FACTORY));
}
}
}
}
return commandsPerKey;
}
use of com.thinkaurelius.titan.diskstorage.StaticBuffer in project titan by thinkaurelius.
the class AstyanaxStoreManager method mutateMany.
@Override
public void mutateMany(Map<String, Map<StaticBuffer, KCVMutation>> batch, StoreTransaction txh) throws BackendException {
MutationBatch m = keyspaceContext.getClient().prepareMutationBatch().withAtomicBatch(atomicBatch).setConsistencyLevel(getTx(txh).getWriteConsistencyLevel().getAstyanax()).withRetryPolicy(retryPolicy.duplicate());
final MaskedTimestamp commitTime = new MaskedTimestamp(txh);
for (Map.Entry<String, Map<StaticBuffer, KCVMutation>> batchentry : batch.entrySet()) {
String storeName = batchentry.getKey();
Preconditions.checkArgument(openStores.containsKey(storeName), "Store cannot be found: " + storeName);
ColumnFamily<ByteBuffer, ByteBuffer> columnFamily = openStores.get(storeName).getColumnFamily();
Map<StaticBuffer, KCVMutation> mutations = batchentry.getValue();
for (Map.Entry<StaticBuffer, KCVMutation> ent : mutations.entrySet()) {
// The CLMs for additions and deletions are separated because
// Astyanax's operation timestamp cannot be set on a per-delete
// or per-addition basis.
KCVMutation titanMutation = ent.getValue();
ByteBuffer key = ent.getKey().asByteBuffer();
if (titanMutation.hasDeletions()) {
ColumnListMutation<ByteBuffer> dels = m.withRow(columnFamily, key);
dels.setTimestamp(commitTime.getDeletionTime(times));
for (StaticBuffer b : titanMutation.getDeletions()) dels.deleteColumn(b.as(StaticBuffer.BB_FACTORY));
}
if (titanMutation.hasAdditions()) {
ColumnListMutation<ByteBuffer> upds = m.withRow(columnFamily, key);
upds.setTimestamp(commitTime.getAdditionTime(times));
for (Entry e : titanMutation.getAdditions()) {
Integer ttl = (Integer) e.getMetaData().get(EntryMetaData.TTL);
if (null != ttl && ttl > 0) {
upds.putColumn(e.getColumnAs(StaticBuffer.BB_FACTORY), e.getValueAs(StaticBuffer.BB_FACTORY), ttl);
} else {
upds.putColumn(e.getColumnAs(StaticBuffer.BB_FACTORY), e.getValueAs(StaticBuffer.BB_FACTORY));
}
}
}
}
}
try {
m.execute();
} catch (ConnectionException e) {
throw new TemporaryBackendException(e);
}
sleepAfterWrite(txh, commitTime);
}
use of com.thinkaurelius.titan.diskstorage.StaticBuffer in project titan by thinkaurelius.
the class CassandraHelper method transformRange.
public static KeyRange transformRange(Token leftKeyExclusive, Token rightKeyInclusive) {
if (!(leftKeyExclusive instanceof BytesToken))
throw new UnsupportedOperationException();
// if left part is BytesToken, right part should be too, otherwise there is no sense in the ring
assert rightKeyInclusive instanceof BytesToken;
// l is exclusive, r is inclusive
BytesToken l = (BytesToken) leftKeyExclusive;
BytesToken r = (BytesToken) rightKeyInclusive;
byte[] leftTokenValue = l.getTokenValue();
byte[] rightTokenValue = r.getTokenValue();
Preconditions.checkArgument(leftTokenValue.length == rightTokenValue.length, "Tokens have unequal length");
int tokenLength = leftTokenValue.length;
byte[][] tokens = new byte[][] { leftTokenValue, rightTokenValue };
byte[][] plusOne = new byte[2][tokenLength];
for (int j = 0; j < 2; j++) {
boolean carry = true;
for (int i = tokenLength - 1; i >= 0; i--) {
byte b = tokens[j][i];
if (carry) {
b++;
carry = false;
}
if (b == 0)
carry = true;
plusOne[j][i] = b;
}
}
StaticBuffer lb = StaticArrayBuffer.of(plusOne[0]);
StaticBuffer rb = StaticArrayBuffer.of(plusOne[1]);
Preconditions.checkArgument(lb.length() == tokenLength, lb.length());
Preconditions.checkArgument(rb.length() == tokenLength, rb.length());
return new KeyRange(lb, rb);
}
use of com.thinkaurelius.titan.diskstorage.StaticBuffer in project titan by thinkaurelius.
the class OrderedKeyValueStoreManagerAdapter method mutateMany.
@Override
public void mutateMany(Map<String, Map<StaticBuffer, KCVMutation>> mutations, StoreTransaction txh) throws BackendException {
Map<String, KVMutation> converted = new HashMap<String, KVMutation>(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 com.thinkaurelius.titan.diskstorage.StaticBuffer in project titan by thinkaurelius.
the class StandardLockCleanerRunnable method runWithExceptions.
private void runWithExceptions() throws BackendException {
StaticBuffer lockKey = serializer.toLockKey(target.getKey(), target.getColumn());
// TODO reduce LOCK_COL_END based on cutoff
List<Entry> locks = store.getSlice(new KeySliceQuery(lockKey, LOCK_COL_START, LOCK_COL_END), tx);
ImmutableList.Builder<StaticBuffer> b = ImmutableList.builder();
for (Entry lc : locks) {
TimestampRid tr = serializer.fromLockColumn(lc.getColumn(), times);
if (tr.getTimestamp().isBefore(cutoff)) {
log.info("Deleting expired lock on {} by rid {} with timestamp {} (before or at cutoff {})", new Object[] { target, tr.getRid(), tr.getTimestamp(), cutoff });
b.add(lc.getColumn());
} else {
log.debug("Ignoring lock on {} by rid {} with timestamp {} (timestamp is after cutoff {})", new Object[] { target, tr.getRid(), tr.getTimestamp(), cutoff });
}
}
List<StaticBuffer> dels = b.build();
if (!dels.isEmpty()) {
store.mutate(lockKey, ImmutableList.<Entry>of(), dels, tx);
log.info("Deleted {} expired locks (before or at cutoff {})", dels.size(), cutoff);
}
}
Aggregations