use of com.orientechnologies.orient.core.db.record.ridbag.ORidBag in project orientdb by orientechnologies.
the class OrientVertex method countEdges.
/**
* (Blueprints Extension) Returns the number of edges connected to the current Vertex.
*
* @param iDirection The direction between OUT, IN or BOTH
* @param iLabels Optional labels as Strings to consider
*
* @return A long with the total edges found
*/
public long countEdges(final Direction iDirection, final String... iLabels) {
checkIfAttached();
long counter = 0;
OrientBaseGraph.getEdgeClassNames(getGraph(), iLabels);
OrientBaseGraph.encodeClassNames(iLabels);
if (settings.isUseVertexFieldsForEdgeLabels() || iLabels == null || iLabels.length == 0) {
// VERY FAST
final ODocument doc = getRecord();
for (String fieldName : doc.fieldNames()) {
final OPair<Direction, String> connection = getConnection(iDirection, fieldName, iLabels);
if (connection == null)
// SKIP THIS FIELD
continue;
final Object fieldValue = doc.field(fieldName);
if (fieldValue != null)
if (fieldValue instanceof Collection<?>)
counter += ((Collection<?>) fieldValue).size();
else if (fieldValue instanceof Map<?, ?>)
counter += ((Map<?, ?>) fieldValue).size();
else if (fieldValue instanceof ORidBag) {
counter += ((ORidBag) fieldValue).size();
} else {
counter++;
}
}
} else {
// SLOWER: BROWSE & FILTER
for (Edge e : getEdges(iDirection, iLabels)) if (e != null)
counter++;
}
return counter;
}
use of com.orientechnologies.orient.core.db.record.ridbag.ORidBag in project orientdb by orientechnologies.
the class OrientVertex method remove.
/**
* Removes the current Vertex from the Graph. all the incoming and outgoing edges are automatically removed too.
*/
@Override
public void remove() {
checkClass();
final OrientBaseGraph graph = checkIfAttached();
graph.setCurrentGraphInThreadLocal();
graph.autoStartTransaction();
final ODocument doc = getRecord();
if (doc == null)
throw ExceptionFactory.vertexWithIdDoesNotExist(this.getId());
Map<String, List<ODocument>> treeRidbagEdgesToRemove = new HashMap<String, List<ODocument>>();
if (!graph.getRawGraph().getTransaction().isActive()) {
for (String fieldName : doc.fieldNames()) {
final OPair<Direction, String> connection = getConnection(Direction.BOTH, fieldName);
if (connection == null)
// SKIP THIS FIELD
continue;
Object fv = doc.field(fieldName);
if (fv instanceof ORidBag && !((ORidBag) fv).isEmbedded()) {
List<ODocument> docs = new ArrayList<ODocument>();
for (OIdentifiable id : (ORidBag) fv) docs.add(OrientBaseGraph.getDocument(id, true));
treeRidbagEdgesToRemove.put(fieldName, docs);
}
}
}
// REMOVE THE VERTEX RECORD FIRST TO CATCH CME BEFORE EDGES ARE REMOVED
super.removeRecord();
// REMOVE THE VERTEX FROM MANUAL INDEXES
final Iterator<Index<? extends Element>> it = graph.getIndices().iterator();
if (it.hasNext()) {
final Set<Edge> allEdges = new HashSet<Edge>();
for (Edge e : getEdges(Direction.BOTH)) allEdges.add(e);
while (it.hasNext()) {
final Index<? extends Element> index = it.next();
if (Vertex.class.isAssignableFrom(index.getIndexClass())) {
OrientIndex<OrientVertex> idx = (OrientIndex<OrientVertex>) index;
idx.removeElement(this);
}
if (Edge.class.isAssignableFrom(index.getIndexClass())) {
OrientIndex<OrientEdge> idx = (OrientIndex<OrientEdge>) index;
for (Edge e : allEdges) idx.removeElement((OrientEdge) e);
}
}
}
for (Map.Entry<String, List<ODocument>> entry : treeRidbagEdgesToRemove.entrySet()) {
doc.removeField(entry.getKey());
Iterator<ODocument> iter = entry.getValue().iterator();
while (iter.hasNext()) {
ODocument docEdge = iter.next();
OrientBaseGraph.deleteEdgeIfAny(docEdge, false);
}
}
graph.removeEdgesInternal(this, doc, null, true, settings.isUseVertexFieldsForEdgeLabels(), settings.isAutoScaleEdgeType());
}
use of com.orientechnologies.orient.core.db.record.ridbag.ORidBag in project orientdb by orientechnologies.
the class ORecordSerializerJSON method getValueAsCollection.
private Object getValueAsCollection(ODocument iRecord, String iFieldValue, OType iType, OType iLinkedType, Map<String, Character> iFieldTypes, boolean iNoMap, String iOptions) {
// remove square brackets
iFieldValue = iFieldValue.substring(1, iFieldValue.length() - 1);
if (iType == OType.LINKBAG) {
final ORidBag bag = new ORidBag();
parseCollection(iRecord, iFieldValue, iType, OType.LINK, iFieldTypes, iNoMap, iOptions, new CollectionItemVisitor() {
@Override
public void visitItem(Object item) {
bag.add((OIdentifiable) item);
}
});
return bag;
} else if (iType == OType.LINKSET) {
return getValueAsLinkedCollection(new ORecordLazySet(iRecord), iRecord, iFieldValue, iType, iLinkedType, iFieldTypes, iNoMap, iOptions);
} else if (iType == OType.LINKLIST) {
return getValueAsLinkedCollection(new ORecordLazyList(iRecord), iRecord, iFieldValue, iType, iLinkedType, iFieldTypes, iNoMap, iOptions);
} else if (iType == OType.EMBEDDEDSET) {
return getValueAsEmbeddedCollection(new OTrackedSet<Object>(iRecord), iRecord, iFieldValue, iType, iLinkedType, iFieldTypes, iNoMap, iOptions);
} else {
return getValueAsEmbeddedCollection(new OTrackedList<Object>(iRecord), iRecord, iFieldValue, iType, iLinkedType, iFieldTypes, iNoMap, iOptions);
}
}
use of com.orientechnologies.orient.core.db.record.ridbag.ORidBag in project orientdb by orientechnologies.
the class ORecordSerializerBinaryV0 method deserializeValue.
@Override
public Object deserializeValue(final BytesContainer bytes, final OType type, final ODocument ownerDocument) {
Object value = null;
switch(type) {
case INTEGER:
value = OVarIntSerializer.readAsInteger(bytes);
break;
case LONG:
value = OVarIntSerializer.readAsLong(bytes);
break;
case SHORT:
value = OVarIntSerializer.readAsShort(bytes);
break;
case STRING:
value = readString(bytes);
break;
case DOUBLE:
value = Double.longBitsToDouble(readLong(bytes));
break;
case FLOAT:
value = Float.intBitsToFloat(readInteger(bytes));
break;
case BYTE:
value = readByte(bytes);
break;
case BOOLEAN:
value = readByte(bytes) == 1;
break;
case DATETIME:
value = new Date(OVarIntSerializer.readAsLong(bytes));
break;
case DATE:
long savedTime = OVarIntSerializer.readAsLong(bytes) * MILLISEC_PER_DAY;
savedTime = convertDayToTimezone(TimeZone.getTimeZone("GMT"), ODateHelper.getDatabaseTimeZone(), savedTime);
value = new Date(savedTime);
break;
case EMBEDDED:
value = new ODocument();
deserialize((ODocument) value, bytes);
if (((ODocument) value).containsField(ODocumentSerializable.CLASS_NAME)) {
String className = ((ODocument) value).field(ODocumentSerializable.CLASS_NAME);
try {
Class<?> clazz = Class.forName(className);
ODocumentSerializable newValue = (ODocumentSerializable) clazz.newInstance();
newValue.fromDocument((ODocument) value);
value = newValue;
} catch (Exception e) {
throw new RuntimeException(e);
}
} else
ODocumentInternal.addOwner((ODocument) value, ownerDocument);
break;
case EMBEDDEDSET:
value = readEmbeddedSet(bytes, ownerDocument);
break;
case EMBEDDEDLIST:
value = readEmbeddedList(bytes, ownerDocument);
break;
case LINKSET:
value = readLinkCollection(bytes, new ORecordLazySet(ownerDocument));
break;
case LINKLIST:
value = readLinkCollection(bytes, new ORecordLazyList(ownerDocument));
break;
case BINARY:
value = readBinary(bytes);
break;
case LINK:
value = readOptimizedLink(bytes);
break;
case LINKMAP:
value = readLinkMap(bytes, ownerDocument);
break;
case EMBEDDEDMAP:
value = readEmbeddedMap(bytes, ownerDocument);
break;
case DECIMAL:
value = ODecimalSerializer.INSTANCE.deserialize(bytes.bytes, bytes.offset);
bytes.skip(ODecimalSerializer.INSTANCE.getObjectSize(bytes.bytes, bytes.offset));
break;
case LINKBAG:
ORidBag bag = new ORidBag();
bag.fromStream(bytes);
bag.setOwner(ownerDocument);
value = bag;
break;
case TRANSIENT:
break;
case CUSTOM:
try {
String className = readString(bytes);
Class<?> clazz = Class.forName(className);
OSerializableStream stream = (OSerializableStream) clazz.newInstance();
stream.fromStream(readBinary(bytes));
if (stream instanceof OSerializableWrapper)
value = ((OSerializableWrapper) stream).getSerializable();
else
value = stream;
} catch (Exception e) {
throw new RuntimeException(e);
}
break;
case ANY:
break;
}
return value;
}
use of com.orientechnologies.orient.core.db.record.ridbag.ORidBag in project orientdb by orientechnologies.
the class ORecordSerializerNetworkV0 method deserializeValue.
public Object deserializeValue(BytesContainer bytes, OType type, ODocument document) {
Object value = null;
switch(type) {
case INTEGER:
value = OVarIntSerializer.readAsInteger(bytes);
break;
case LONG:
value = OVarIntSerializer.readAsLong(bytes);
break;
case SHORT:
value = OVarIntSerializer.readAsShort(bytes);
break;
case STRING:
value = readString(bytes);
break;
case DOUBLE:
value = Double.longBitsToDouble(readLong(bytes));
break;
case FLOAT:
value = Float.intBitsToFloat(readInteger(bytes));
break;
case BYTE:
value = readByte(bytes);
break;
case BOOLEAN:
value = readByte(bytes) == 1;
break;
case DATETIME:
value = new Date(OVarIntSerializer.readAsLong(bytes));
break;
case DATE:
long savedTime = OVarIntSerializer.readAsLong(bytes) * MILLISEC_PER_DAY;
savedTime = convertDayToTimezone(TimeZone.getTimeZone("GMT"), ODateHelper.getDatabaseTimeZone(), savedTime);
value = new Date(savedTime);
break;
case EMBEDDED:
value = new ODocument();
deserialize((ODocument) value, bytes);
if (((ODocument) value).containsField(ODocumentSerializable.CLASS_NAME)) {
String className = ((ODocument) value).field(ODocumentSerializable.CLASS_NAME);
try {
Class<?> clazz = Class.forName(className);
ODocumentSerializable newValue = (ODocumentSerializable) clazz.newInstance();
newValue.fromDocument((ODocument) value);
value = newValue;
} catch (Exception e) {
throw new RuntimeException(e);
}
} else
ODocumentInternal.addOwner((ODocument) value, document);
break;
case EMBEDDEDSET:
value = readEmbeddedCollection(bytes, new OTrackedSet<Object>(document), document);
break;
case EMBEDDEDLIST:
value = readEmbeddedCollection(bytes, new OTrackedList<Object>(document), document);
break;
case LINKSET:
value = readLinkCollection(bytes, new ORecordLazySet(document));
break;
case LINKLIST:
value = readLinkCollection(bytes, new ORecordLazyList(document));
break;
case BINARY:
value = readBinary(bytes);
break;
case LINK:
value = readOptimizedLink(bytes);
break;
case LINKMAP:
value = readLinkMap(bytes, document);
break;
case EMBEDDEDMAP:
value = readEmbeddedMap(bytes, document);
break;
case DECIMAL:
value = ODecimalSerializer.INSTANCE.deserialize(bytes.bytes, bytes.offset);
bytes.skip(ODecimalSerializer.INSTANCE.getObjectSize(bytes.bytes, bytes.offset));
break;
case LINKBAG:
ORidBag bag = new ORidBag();
bag.fromStream(bytes);
bag.setOwner(document);
value = bag;
break;
case TRANSIENT:
break;
case CUSTOM:
try {
String className = readString(bytes);
Class<?> clazz = Class.forName(className);
OSerializableStream stream = (OSerializableStream) clazz.newInstance();
stream.fromStream(readBinary(bytes));
if (stream instanceof OSerializableWrapper)
value = ((OSerializableWrapper) stream).getSerializable();
else
value = stream;
} catch (Exception e) {
throw new RuntimeException(e);
}
break;
case ANY:
break;
}
return value;
}
Aggregations