use of com.thinkaurelius.titan.diskstorage.StaticBuffer in project titan by thinkaurelius.
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);
EnumMap<LogTxMeta, Object> metadata = new EnumMap<LogTxMeta, Object>(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 com.thinkaurelius.titan.diskstorage.StaticBuffer in project titan by thinkaurelius.
the class IDManagementTest method testDirectionPrefix.
@Test
public void testDirectionPrefix() {
for (RelationType type : RelationType.values()) {
StaticBuffer[] bounds = IDHandler.getBounds(type);
assertEquals(1, bounds[0].length());
assertEquals(1, bounds[1].length());
assertTrue(bounds[0].compareTo(bounds[1]) < 0);
}
}
use of com.thinkaurelius.titan.diskstorage.StaticBuffer in project titan by thinkaurelius.
the class EdgeSerializer method writeRelation.
public Entry writeRelation(InternalRelation relation, int position, StandardTitanTx tx) {
Preconditions.checkArgument(position < relation.getLen());
TitanType type = relation.getType();
long typeid = type.getID();
Direction dir = EdgeDirection.fromPosition(position);
int dirID = getDirID(dir, relation.isProperty() ? RelationType.PROPERTY : RelationType.EDGE);
DataOutput colOut = serializer.getDataOutput(DEFAULT_COLUMN_CAPACITY, true);
IDHandler.writeEdgeType(colOut, typeid, dirID);
InternalType definition = (InternalType) type;
long[] sortKey = definition.getSortKey();
int startPosition = colOut.getPosition();
if (!type.isUnique(dir)) {
writeInlineTypes(sortKey, relation, colOut, tx);
}
int endPosition = colOut.getPosition();
DataOutput writer = colOut;
long vertexIdDiff = 0;
long relationIdDiff = relation.getID() - relation.getVertex(position).getID();
if (relation.isEdge())
vertexIdDiff = relation.getVertex((position + 1) % 2).getID() - relation.getVertex(position).getID();
if (type.isUnique(dir)) {
writer = serializer.getDataOutput(DEFAULT_VALUE_CAPACITY, true);
if (relation.isEdge())
VariableLong.write(writer, vertexIdDiff);
VariableLong.write(writer, relationIdDiff);
} else {
if (relation.isEdge())
VariableLong.writeBackward(writer, vertexIdDiff);
VariableLong.writeBackward(writer, relationIdDiff);
}
if (!type.isUnique(dir)) {
writer = serializer.getDataOutput(DEFAULT_VALUE_CAPACITY, true);
}
if (relation.isProperty()) {
Preconditions.checkArgument(relation.isProperty());
Object value = ((TitanProperty) relation).getValue();
Preconditions.checkNotNull(value);
TitanKey key = (TitanKey) type;
assert key.getDataType().isInstance(value);
if (hasGenericDataType(key)) {
writer.writeClassAndObject(value);
} else {
writer.writeObjectNotNull(value);
}
}
// Write signature & sort key if unique
if (type.isUnique(dir)) {
writeInlineTypes(sortKey, relation, writer, tx);
}
long[] signature = definition.getSignature();
writeInlineTypes(signature, relation, writer, tx);
// Write remaining properties
LongSet writtenTypes = new LongOpenHashSet(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 (TitanType t : relation.getPropertyKeysDirect()) {
if (!writtenTypes.contains(t.getID())) {
remainingTypes.add(t.getID());
}
}
// 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) {
TitanType t = tx.getExistingType(tid);
writeInline(writer, t, relation.getProperty(t), true);
}
StaticBuffer column = ((InternalType) type).getSortOrder() == Order.DESC ? colOut.getStaticBufferFlipBytes(startPosition, endPosition) : colOut.getStaticBuffer();
return new StaticBufferEntry(column, writer.getStaticBuffer());
}
use of com.thinkaurelius.titan.diskstorage.StaticBuffer in project titan by thinkaurelius.
the class KCVSConfiguration method getConfigurationProperty.
/**
* Reads the configuration property for this StoreManager
*
* @param key Key identifying the configuration property
* @return Value stored for the key or null if the configuration property has not (yet) been defined.
* @throws StorageException
*/
public String getConfigurationProperty(final String key) throws StorageException {
StaticBuffer column = string2StaticBuffer(key);
final KeySliceQuery query = new KeySliceQuery(rowKey, column, ByteBufferUtil.nextBiggerBuffer(column), false);
StaticBuffer result = BackendOperation.execute(new Callable<StaticBuffer>() {
@Override
public StaticBuffer call() throws Exception {
StoreTransaction txh = null;
try {
txh = manager.beginTransaction(new StoreTxConfig(ConsistencyLevel.KEY_CONSISTENT));
List<Entry> entries = store.getSlice(query, txh);
if (entries.isEmpty())
return null;
return entries.get(0).getValue();
} finally {
if (txh != null)
txh.commit();
}
}
@Override
public String toString() {
return "getConfiguration";
}
}, maxOperationWaitTime);
if (result == null)
return null;
return staticBuffer2String(result);
}
use of com.thinkaurelius.titan.diskstorage.StaticBuffer in project titan by thinkaurelius.
the class StandardTitanGraph method persist.
private <V extends InternalVertex> List<StaticBuffer> persist(ListMultimap<V, InternalRelation> mutatedEdges, StandardTitanTx tx) throws StorageException {
assert mutatedEdges != null && !mutatedEdges.isEmpty();
Collection<V> vertices = mutatedEdges.keySet();
BackendTransaction mutator = tx.getTxHandle();
List<StaticBuffer> mutatedKeys = new ArrayList<StaticBuffer>(vertices.size());
for (V vertex : vertices) {
Preconditions.checkArgument(vertex.getID() > 0, "Vertex has no id: %s", vertex.getID());
List<InternalRelation> edges = mutatedEdges.get(vertex);
List<Entry> additions = new ArrayList<Entry>(edges.size());
List<StaticBuffer> deletions = new ArrayList<StaticBuffer>(Math.max(10, edges.size() / 10));
for (InternalRelation edge : edges) {
for (int pos = 0; pos < edge.getLen(); pos++) {
if (edge.getVertex(pos).equals(vertex)) {
if (edge.isRemoved()) {
deletions.add(edgeSerializer.writeRelation(edge, pos, tx).getColumn());
} else {
Preconditions.checkArgument(edge.isNew());
additions.add(edgeSerializer.writeRelation(edge, pos, tx));
}
}
}
}
StaticBuffer vertexKey = IDHandler.getKey(vertex.getID());
mutator.mutateEdges(vertexKey, additions, deletions);
if (!vertex.isNew())
mutatedKeys.add(vertexKey);
// Index Updates
for (InternalRelation relation : edges) {
if (relation.getVertex(0).equals(vertex)) {
if (relation.isRemoved()) {
if (relation.isProperty()) {
indexSerializer.removeProperty((TitanProperty) relation, mutator);
} else if (relation.isEdge()) {
indexSerializer.removeEdge(relation, mutator);
}
} else {
Preconditions.checkArgument(relation.isNew());
if (relation.isProperty()) {
indexSerializer.addProperty((TitanProperty) relation, mutator);
} else {
indexSerializer.addEdge(relation, mutator);
}
}
}
}
}
return mutatedKeys;
}
Aggregations