use of com.thinkaurelius.titan.graphdb.database.serialize.DataOutput in project titan by thinkaurelius.
the class EdgeSerializer method writeRelation.
public StaticArrayEntry writeRelation(InternalRelation relation, InternalRelationType type, int position, TypeInspector tx) {
assert type == relation.getType() || type.getBaseType().equals(relation.getType());
Direction dir = EdgeDirection.fromPosition(position);
Preconditions.checkArgument(type.isUnidirected(Direction.BOTH) || type.isUnidirected(dir));
long typeid = type.longId();
DirectionID dirID = getDirID(dir, relation.isProperty() ? RelationCategory.PROPERTY : RelationCategory.EDGE);
DataOutput out = serializer.getDataOutput(DEFAULT_CAPACITY);
int valuePosition;
IDHandler.writeRelationType(out, typeid, dirID, type.isInvisibleType());
Multiplicity multiplicity = type.multiplicity();
long[] sortKey = type.getSortKey();
assert !multiplicity.isConstrained() || sortKey.length == 0 : type.name();
int keyStartPos = out.getPosition();
if (!multiplicity.isConstrained()) {
writeInlineTypes(sortKey, relation, out, tx, InlineType.KEY);
}
int keyEndPos = out.getPosition();
long relationId = relation.longId();
//How multiplicity is handled for edges and properties is slightly different
if (relation.isEdge()) {
long otherVertexId = relation.getVertex((position + 1) % 2).longId();
if (multiplicity.isConstrained()) {
if (multiplicity.isUnique(dir)) {
valuePosition = out.getPosition();
VariableLong.writePositive(out, otherVertexId);
} else {
VariableLong.writePositiveBackward(out, otherVertexId);
valuePosition = out.getPosition();
}
VariableLong.writePositive(out, relationId);
} else {
VariableLong.writePositiveBackward(out, otherVertexId);
VariableLong.writePositiveBackward(out, relationId);
valuePosition = out.getPosition();
}
} else {
assert relation.isProperty();
Preconditions.checkArgument(relation.isProperty());
Object value = ((TitanVertexProperty) relation).value();
Preconditions.checkNotNull(value);
PropertyKey key = (PropertyKey) type;
assert key.dataType().isInstance(value);
if (multiplicity.isConstrained()) {
if (multiplicity.isUnique(dir)) {
//Cardinality=SINGLE
valuePosition = out.getPosition();
writePropertyValue(out, key, value);
} else {
//Cardinality=SET
writePropertyValue(out, key, value);
valuePosition = out.getPosition();
}
VariableLong.writePositive(out, relationId);
} else {
assert multiplicity.getCardinality() == Cardinality.LIST;
VariableLong.writePositiveBackward(out, relationId);
valuePosition = out.getPosition();
writePropertyValue(out, key, value);
}
}
//Write signature
long[] signature = type.getSignature();
writeInlineTypes(signature, relation, out, tx, InlineType.SIGNATURE);
//Write remaining properties
LongSet writtenTypes = new LongHashSet(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 (PropertyKey t : relation.getPropertyKeysDirect()) {
if (!(t instanceof ImplicitKey) && !writtenTypes.contains(t.longId())) {
remainingTypes.add(t.longId());
}
}
//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) {
PropertyKey t = tx.getExistingPropertyKey(tid);
writeInline(out, t, relation.getValueDirect(t), InlineType.NORMAL);
}
assert valuePosition > 0;
StaticArrayEntry entry = new StaticArrayEntry(type.getSortOrder() == Order.DESC ? out.getStaticBufferFlipBytes(keyStartPos, keyEndPos) : out.getStaticBuffer(), valuePosition);
return entry;
}
use of com.thinkaurelius.titan.graphdb.database.serialize.DataOutput in project titan by thinkaurelius.
the class KCVSConfiguration method object2StaticBuffer.
private <O> StaticBuffer object2StaticBuffer(final O value) {
if (value == null)
throw Graph.Variables.Exceptions.variableValueCanNotBeNull();
if (!serializer.validDataType(value.getClass()))
throw Graph.Variables.Exceptions.dataTypeOfVariableValueNotSupported(value);
DataOutput out = serializer.getDataOutput(128);
out.writeClassAndObject(value);
return out.getStaticBuffer();
}
use of com.thinkaurelius.titan.graphdb.database.serialize.DataOutput in project titan by thinkaurelius.
the class KCVSLog method writeMessage.
private Entry writeMessage(KCVSMessage msg) {
StaticBuffer content = msg.getContent();
DataOutput out = manager.serializer.getDataOutput(8 + 8 + manager.senderId.length() + 2 + content.length());
Instant rawTimestamp = msg.getTimestamp();
Preconditions.checkArgument(rawTimestamp.isAfter(Instant.EPOCH));
out.putLong(times.getTime(rawTimestamp));
out.writeObjectNotNull(manager.senderId);
out.putLong(numMsgCounter.incrementAndGet());
final int valuePos = out.getPosition();
out.putBytes(content);
return new StaticArrayEntry(out.getStaticBuffer(), valuePos);
}
use of com.thinkaurelius.titan.graphdb.database.serialize.DataOutput in project titan by thinkaurelius.
the class TransactionLogHeader method serializeHeader.
private DataOutput serializeHeader(Serializer serializer, int capacity, LogTxStatus status, EnumMap<LogTxMeta, Object> meta) {
Preconditions.checkArgument(status != null && meta != null, "Invalid status or meta");
DataOutput out = serializer.getDataOutput(capacity);
out.putLong(times.getTime(txTimestamp));
VariableLong.writePositive(out, transactionId);
out.writeObjectNotNull(status);
Preconditions.checkArgument(meta.size() < Byte.MAX_VALUE, "Too much meta data: %s", meta.size());
out.putByte(VariableLong.unsignedByte(meta.size()));
for (Map.Entry<LogTxMeta, Object> metaentry : meta.entrySet()) {
assert metaentry.getValue() != null;
out.putByte(VariableLong.unsignedByte(metaentry.getKey().ordinal()));
out.writeObjectNotNull(metaentry.getValue());
}
return out;
}
use of com.thinkaurelius.titan.graphdb.database.serialize.DataOutput in project titan by thinkaurelius.
the class KCVSLog method getLogKey.
private StaticBuffer getLogKey(final int partitionId, final int bucketId, final int timeslice) {
Preconditions.checkArgument(partitionId >= 0 && partitionId < (1 << manager.partitionBitWidth));
Preconditions.checkArgument(bucketId >= 0 && bucketId < numBuckets);
DataOutput o = manager.serializer.getDataOutput(3 * 4);
//Offset to put significant bits in front
o.putInt((partitionId << (32 - manager.partitionBitWidth)));
o.putInt(bucketId);
o.putInt(timeslice);
return o.getStaticBuffer();
}
Aggregations