use of com.orientechnologies.orient.core.db.record.ridbag.ORidBag in project orientdb by orientechnologies.
the class ODocumentHelper method compareBags.
public static boolean compareBags(ODatabaseDocumentInternal iMyDb, ORidBag myFieldValue, ODatabaseDocumentInternal iOtherDb, ORidBag otherFieldValue, RIDMapper ridMapper) {
final ORidBag myBag = myFieldValue;
final ORidBag otherBag = otherFieldValue;
final int mySize = makeDbCall(iMyDb, new ODbRelatedCall<Integer>() {
public Integer call(ODatabaseDocumentInternal database) {
return myBag.size();
}
});
final int otherSize = makeDbCall(iOtherDb, new ODbRelatedCall<Integer>() {
public Integer call(ODatabaseDocumentInternal database) {
return otherBag.size();
}
});
if (mySize != otherSize)
return false;
boolean oldMyAutoConvert;
boolean oldOtherAutoConvert;
oldMyAutoConvert = myBag.isAutoConvertToRecord();
myBag.setAutoConvertToRecord(false);
oldOtherAutoConvert = otherBag.isAutoConvertToRecord();
otherBag.setAutoConvertToRecord(false);
final ORidBag otherBagCopy = makeDbCall(iOtherDb, new ODbRelatedCall<ORidBag>() {
@Override
public ORidBag call(ODatabaseDocumentInternal database) {
final ORidBag otherRidBag = new ORidBag();
otherRidBag.setAutoConvertToRecord(false);
for (OIdentifiable identifiable : otherBag) otherRidBag.add(identifiable);
return otherRidBag;
}
});
try {
final Iterator<OIdentifiable> myIterator = makeDbCall(iMyDb, new ODbRelatedCall<Iterator<OIdentifiable>>() {
public Iterator<OIdentifiable> call(ODatabaseDocumentInternal database) {
return myBag.iterator();
}
});
while (makeDbCall(iMyDb, new ODbRelatedCall<Boolean>() {
public Boolean call(ODatabaseDocumentInternal database) {
return myIterator.hasNext();
}
})) {
final OIdentifiable myIdentifiable = makeDbCall(iMyDb, new ODbRelatedCall<OIdentifiable>() {
@Override
public OIdentifiable call(ODatabaseDocumentInternal database) {
return myIterator.next();
}
});
final ORID otherRid;
if (ridMapper != null) {
ORID convertedRid = ridMapper.map(myIdentifiable.getIdentity());
if (convertedRid != null)
otherRid = convertedRid;
else
otherRid = myIdentifiable.getIdentity();
} else
otherRid = myIdentifiable.getIdentity();
makeDbCall(iOtherDb, new ODbRelatedCall<Object>() {
@Override
public Object call(ODatabaseDocumentInternal database) {
otherBagCopy.remove(otherRid);
return null;
}
});
}
return makeDbCall(iOtherDb, new ODbRelatedCall<Boolean>() {
@Override
public Boolean call(ODatabaseDocumentInternal database) {
return otherBagCopy.isEmpty();
}
});
} finally {
myBag.setAutoConvertToRecord(oldMyAutoConvert);
otherBag.setAutoConvertToRecord(oldOtherAutoConvert);
}
}
use of com.orientechnologies.orient.core.db.record.ridbag.ORidBag in project orientdb by orientechnologies.
the class OCommandExecutorSQLSelect method applyExpand.
/**
* Extract the content of collections and/or links and put it as result
*/
private void applyExpand() {
if (expandTarget == null) {
return;
}
final long startExpand = System.currentTimeMillis();
try {
if (tempResult == null) {
tempResult = new ArrayList<OIdentifiable>();
if (expandTarget instanceof OSQLFilterItemVariable) {
Object r = ((OSQLFilterItemVariable) expandTarget).getValue(null, null, context);
if (r != null) {
if (r instanceof OIdentifiable) {
((Collection<OIdentifiable>) tempResult).add((OIdentifiable) r);
} else if (r instanceof Iterator || OMultiValue.isMultiValue(r)) {
for (Object o : OMultiValue.getMultiValueIterable(r)) {
((Collection<OIdentifiable>) tempResult).add((OIdentifiable) o);
}
}
}
} else if (expandTarget instanceof OSQLFunctionRuntime && !hasFieldItemParams((OSQLFunctionRuntime) expandTarget)) {
if (((OSQLFunctionRuntime) expandTarget).aggregateResults()) {
throw new OCommandExecutionException("Unsupported operation: aggregate function in expand(" + expandTarget + ")");
} else {
Object r = ((OSQLFunctionRuntime) expandTarget).execute(null, null, null, context);
if (r instanceof OIdentifiable) {
((Collection<OIdentifiable>) tempResult).add((OIdentifiable) r);
} else if (r instanceof Iterator || OMultiValue.isMultiValue(r)) {
int i = 0;
for (Object o : OMultiValue.getMultiValueIterable(r)) {
if ((++i) % 100 == 0 && !checkInterruption()) {
return;
}
((Collection<OIdentifiable>) tempResult).add((OIdentifiable) o);
}
}
}
}
} else {
if (tempResult == null) {
tempResult = new ArrayList<OIdentifiable>();
}
final OMultiCollectionIterator<OIdentifiable> finalResult = new OMultiCollectionIterator<OIdentifiable>();
if (orderedFields == null || orderedFields.size() == 0) {
// expand is applied before sorting, so limiting the result set here would give wrong results
int iteratorLimit = 0;
if (limit < 0) {
iteratorLimit = -1;
} else {
iteratorLimit += limit;
}
finalResult.setLimit(iteratorLimit);
finalResult.setSkip(skip);
}
for (OIdentifiable id : tempResult) {
if (!checkInterruption()) {
return;
}
final Object fieldValue;
if (expandTarget instanceof OSQLFilterItem) {
fieldValue = ((OSQLFilterItem) expandTarget).getValue(id.getRecord(), null, context);
} else if (expandTarget instanceof OSQLFunctionRuntime) {
fieldValue = ((OSQLFunctionRuntime) expandTarget).getResult();
} else {
fieldValue = expandTarget.toString();
}
if (fieldValue != null) {
if (fieldValue instanceof ODocument) {
ArrayList<ODocument> partial = new ArrayList<ODocument>();
partial.add((ODocument) fieldValue);
finalResult.add(partial);
} else if (fieldValue instanceof Collection<?> || fieldValue.getClass().isArray() || fieldValue instanceof Iterator<?> || fieldValue instanceof OIdentifiable || fieldValue instanceof ORidBag) {
finalResult.add(fieldValue);
} else if (fieldValue instanceof Map<?, ?>) {
finalResult.add(((Map<?, OIdentifiable>) fieldValue).values());
}
}
}
tempResult = finalResult;
}
} finally {
context.setVariable("expandElapsed", (System.currentTimeMillis() - startExpand));
}
}
use of com.orientechnologies.orient.core.db.record.ridbag.ORidBag in project orientdb by orientechnologies.
the class OCommandExecutorSQLOptimizeDatabase method optimizeEdges.
private String optimizeEdges() {
final ODatabaseDocumentInternal db = getDatabase();
db.declareIntent(new OIntentMassiveInsert());
try {
long transformed = 0;
if (db.getTransaction().isActive())
db.commit();
db.begin();
try {
final long totalEdges = db.countClass("E");
long browsedEdges = 0;
long lastLapBrowsed = 0;
long lastLapTime = System.currentTimeMillis();
for (ODocument doc : db.browseClass("E")) {
if (Thread.currentThread().isInterrupted())
break;
browsedEdges++;
if (doc != null) {
if (doc.fields() == 2) {
final ORID edgeIdentity = doc.getIdentity();
final ODocument outV = doc.field("out");
final ODocument inV = doc.field("in");
// OUTGOING
final Object outField = outV.field("out_" + doc.getClassName());
if (outField instanceof ORidBag) {
final Iterator<OIdentifiable> it = ((ORidBag) outField).iterator();
while (it.hasNext()) {
OIdentifiable v = it.next();
if (edgeIdentity.equals(v)) {
// REPLACE EDGE RID WITH IN-VERTEX RID
it.remove();
((ORidBag) outField).add(inV.getIdentity());
break;
}
}
}
outV.save();
// INCOMING
final Object inField = inV.field("in_" + doc.getClassName());
if (outField instanceof ORidBag) {
final Iterator<OIdentifiable> it = ((ORidBag) inField).iterator();
while (it.hasNext()) {
OIdentifiable v = it.next();
if (edgeIdentity.equals(v)) {
// REPLACE EDGE RID WITH IN-VERTEX RID
it.remove();
((ORidBag) inField).add(outV.getIdentity());
break;
}
}
}
inV.save();
doc.delete();
if (++transformed % batch == 0) {
db.commit();
db.begin();
}
final long now = System.currentTimeMillis();
if (verbose && (now - lastLapTime > 2000)) {
final long elapsed = now - lastLapTime;
OLogManager.instance().info(this, "Browsed %,d of %,d edges, transformed %,d so far (%,d edges/sec)", browsedEdges, totalEdges, transformed, (((browsedEdges - lastLapBrowsed) * 1000 / elapsed)));
lastLapTime = System.currentTimeMillis();
lastLapBrowsed = browsedEdges;
}
}
}
}
// LAST COMMIT
db.commit();
} finally {
if (db.getTransaction().isActive())
db.rollback();
}
return "Transformed " + transformed + " regular edges in lightweight edges";
} finally {
db.declareIntent(null);
}
}
use of com.orientechnologies.orient.core.db.record.ridbag.ORidBag in project orientdb by orientechnologies.
the class SBTreeBagDeleteTest method testDeleteRidbagTx.
@Test
public void testDeleteRidbagTx() {
ODocument doc = new ODocument();
ORidBag bag = new ORidBag();
int size = OGlobalConfiguration.INDEX_EMBEDDED_TO_SBTREEBONSAI_THRESHOLD.getValueAsInteger() * 2;
for (int i = 0; i < size; i++) bag.add(new ORecordId(10, i));
doc.field("bag", bag);
ORID id = db.save(doc).getIdentity();
bag = doc.field("bag");
OBonsaiCollectionPointer pointer = bag.getPointer();
db.begin();
db.delete(doc);
db.commit();
doc = db.load(id);
assertNull(doc);
((OSBTreeCollectionManagerShared) db.getSbTreeCollectionManager()).clear();
OSBTreeBonsai<OIdentifiable, Integer> tree = db.getSbTreeCollectionManager().loadSBTree(pointer);
assertNull(tree);
}
use of com.orientechnologies.orient.core.db.record.ridbag.ORidBag in project orientdb by orientechnologies.
the class OPropertyRidBagIndexDefinition method createValue.
@Override
public Object createValue(final List<?> params) {
if (!(params.get(0) instanceof ORidBag))
return null;
final ORidBag ridBag = (ORidBag) params.get(0);
final List<Object> values = new ArrayList<Object>();
for (final OIdentifiable item : ridBag) {
values.add(createSingleValue(item));
}
return values;
}
Aggregations