/** * 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. */ private static Map<ByteBuffer, Pair<Put, Delete>> convertToCommands( Map<String, Map<ByteBuffer, KCVMutation>> mutations, final long putTimestamp, final long delTimestamp) { Map<ByteBuffer, Pair<Put, Delete>> commandsPerKey = new HashMap<ByteBuffer, Pair<Put, Delete>>(); for (Map.Entry<String, Map<ByteBuffer, KCVMutation>> entry : mutations.entrySet()) { byte[] cfName = entry.getKey().getBytes(); for (Map.Entry<ByteBuffer, KCVMutation> m : entry.getValue().entrySet()) { ByteBuffer key = m.getKey(); KCVMutation mutation = m.getValue(); Pair<Put, Delete> commands = commandsPerKey.get(key); if (commands == null) { commands = new Pair<Put, Delete>(); commandsPerKey.put(key, commands); } if (mutation.hasDeletions()) { if (commands.getSecond() == null) commands.setSecond(new Delete(ByteBufferUtil.getArray(key), delTimestamp, null)); for (ByteBuffer b : mutation.getDeletions()) { commands.getSecond().deleteColumns(cfName, ByteBufferUtil.getArray(b), delTimestamp); } } if (mutation.hasAdditions()) { if (commands.getFirst() == null) commands.setFirst(new Put(ByteBufferUtil.getArray(key), putTimestamp)); for (Entry e : mutation.getAdditions()) { commands .getFirst() .add( cfName, ByteBufferUtil.getArray(e.getColumn()), putTimestamp, ByteBufferUtil.getArray(e.getValue())); } } } } return commandsPerKey; }
private List<Entry> appendResults( ByteBuffer key, ByteBuffer columnPrefix, List<Entry> entries, LimitTracker limit, TransactionHandle txh) { return appendResults( key, columnPrefix, ByteBufferUtil.nextBiggerBuffer(columnPrefix), entries, limit, txh); }
@Override public long[] indexRetrieval(Object key, TitanKey pt, InternalTitanTransaction tx) { Preconditions.checkArgument( pt.isSimple(), "Currently, only simple properties are supported for index retrieval"); Preconditions.checkArgument( pt.hasIndex(), "Cannot retrieve for given property key - it does not have an index"); long[] vertices = null; Preconditions.checkArgument( pt.getDataType().isInstance(key), "Specified object is incompatible with property data type [" + pt.getName() + "]"); for (int readAttempt = 0; readAttempt < maxReadRetryAttempts; readAttempt++) { try { if (pt.isUnique()) { ByteBuffer value = propertyIndex.get(getIndexKey(key), getKeyedIndexColumn(pt), tx.getTxHandle()); if (value != null) { vertices = new long[1]; vertices[0] = VariableLong.readPositive(value); } } else { ByteBuffer startColumn = VariableLong.positiveByteBuffer(pt.getID()); List<Entry> entries = propertyIndex.getSlice( getIndexKey(key), startColumn, ByteBufferUtil.nextBiggerBuffer(startColumn), tx.getTxHandle()); vertices = new long[entries.size()]; int i = 0; for (Entry ent : entries) { vertices[i++] = VariableLong.readPositive(ent.getValue()); } } break; } catch (StorageException e) { if (e instanceof TemporaryStorageException) { if (readAttempt < maxReadRetryAttempts - 1) temporaryStorageException(e); else throw readException(e, maxReadRetryAttempts); } else throw readException(e); } } if (vertices == null) return new long[0]; else return vertices; }
private List<Entry> queryForEntries(AtomicQuery query, TransactionHandle txh) { ByteBuffer key = IDHandler.getKey(query.getVertexID()); List<Entry> entries = null; LimitTracker limit = new LimitTracker(query); boolean dirs[] = getAllowedDirections(query); if (query.hasEdgeTypeCondition()) { TitanType et = query.getTypeCondition(); if (!et.isNew()) { // Result set must be empty if TitanType is new ArrayList<Object> applicableConstraints = null; boolean isRange = false; if (query.hasConstraints()) { assert !et.isSimple(); TypeDefinition def = ((InternalTitanType) et).getDefinition(); String[] keysig = def.getKeySignature(); applicableConstraints = new ArrayList<Object>(keysig.length); Map<String, Object> constraints = query.getConstraints(); for (int i = 0; i < keysig.length; i++) { if (constraints.containsKey(keysig[i])) { Object iv = constraints.get(keysig[i]); applicableConstraints.add(iv); if (iv != null && (iv instanceof AtomicInterval) && ((AtomicInterval) iv).isRange()) { isRange = true; break; } } else break; } if (applicableConstraints.isEmpty()) applicableConstraints = null; } for (int dirID = 0; dirID < 4; dirID++) { if (dirs[dirID]) { if (applicableConstraints != null) { assert !applicableConstraints.isEmpty(); DataOutput start = serializer.getDataOutput(defaultOutputCapacity, true); DataOutput end = null; if (isRange) end = serializer.getDataOutput(defaultOutputCapacity, true); IDHandler.writeEdgeType(start, et.getID(), dirID, idManager); if (isRange) IDHandler.writeEdgeType(end, et.getID(), dirID, idManager); // Write all applicable key constraints for (Object iv : applicableConstraints) { if (iv instanceof AtomicInterval) { AtomicInterval interval = (AtomicInterval) iv; if (interval.isPoint()) { start.writeObject(interval.getStartPoint()); if (isRange) end.writeObject(interval.getStartPoint()); } else { assert isRange; assert interval.isRange(); ByteBuffer startColumn, endColumn; if (interval.getStartPoint() != null) { start.writeObject(interval.getStartPoint()); startColumn = start.getByteBuffer(); if (!interval.startInclusive()) startColumn = ByteBufferUtil.nextBiggerBuffer(startColumn); } else { assert interval.startInclusive(); startColumn = start.getByteBuffer(); } if (interval.getEndPoint() != null) { end.writeObject(interval.getEndPoint()); } else { assert interval.endInclusive(); } endColumn = end.getByteBuffer(); if (interval.endInclusive()) endColumn = ByteBufferUtil.nextBiggerBuffer(endColumn); entries = appendResults(key, startColumn, endColumn, entries, limit, txh); break; // redundant, this must be the last iteration because its a range } } else { assert iv == null || (iv instanceof TitanVertex); long id = 0; if (iv != null) id = ((TitanVertex) iv).getID(); VariableLong.writePositive(start, id); if (isRange) VariableLong.writePositive(end, id); } } if (!isRange) entries = appendResults(key, start.getByteBuffer(), entries, limit, txh); } else { ByteBuffer columnStart = IDHandler.getEdgeType(et.getID(), dirID, idManager); entries = appendResults(key, columnStart, entries, limit, txh); } } } } } else if (query.hasGroupCondition()) { int groupid = query.getGroupCondition().getID(); for (int dirID = 0; dirID < 4; dirID++) { if (dirs[dirID]) { ByteBuffer columnStart = IDHandler.getEdgeTypeGroup(groupid, dirID, idManager); entries = appendResults(key, columnStart, entries, limit, txh); } } } else { int lastDirID = -1; for (int dirID = 0; dirID <= 4; dirID++) { if ((dirID >= 4 || !dirs[dirID]) && lastDirID >= 0) { ByteBuffer columnStart = IDHandler.getEdgeTypeGroup(0, lastDirID, idManager); ByteBuffer columnEnd = IDHandler.getEdgeTypeGroup(idManager.getMaxGroupID() + 1, dirID - 1, idManager); entries = appendResults(key, columnStart, columnEnd, entries, limit, txh); lastDirID = -1; } if (dirID < 4) { if (dirs[dirID] && lastDirID == -1) lastDirID = dirID; } } } if (entries == null) return ImmutableList.of(); else return entries; }