use of com.orientechnologies.orient.core.db.ODatabaseDocumentInternal 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.ODatabaseDocumentInternal in project orientdb by orientechnologies.
the class OCommandExecutorSQLSelect method execParallelWithPool.
private boolean execParallelWithPool(final ORecordIteratorClusters iTarget, final ODatabaseDocumentTx db) {
final int[] clusterIds = iTarget.getClusterIds();
// CREATE ONE THREAD PER CLUSTER
final int jobNumbers = clusterIds.length;
final List<Future<?>> jobs = new ArrayList<Future<?>>();
OLogManager.instance().debug(this, "Executing parallel query with strategy executors. clusterIds=%d, jobs=%d", clusterIds.length, jobNumbers);
final boolean[] results = new boolean[jobNumbers];
final OCommandContext[] contexts = new OCommandContext[jobNumbers];
final RuntimeException[] exceptions = new RuntimeException[jobNumbers];
parallelRunning = true;
final AtomicInteger runningJobs = new AtomicInteger(jobNumbers);
for (int i = 0; i < jobNumbers; ++i) {
final int current = i;
final Runnable job = new Runnable() {
@Override
public void run() {
try {
ODatabaseDocumentInternal localDatabase = null;
try {
exceptions[current] = null;
results[current] = true;
final OCommandContext threadContext = context.copy();
contexts[current] = threadContext;
localDatabase = db.copy();
localDatabase.activateOnCurrentThread();
// CREATE A SNAPSHOT TO AVOID DEADLOCKS
db.getMetadata().getSchema().makeSnapshot();
scanClusterWithIterator(localDatabase, threadContext, clusterIds[current], current, results);
} catch (RuntimeException t) {
exceptions[current] = t;
} finally {
runningJobs.decrementAndGet();
resultQueue.offer(PARALLEL_END_EXECUTION_THREAD);
if (localDatabase != null)
localDatabase.close();
}
} catch (Exception e) {
if (exceptions[current] == null) {
exceptions[current] = new RuntimeException(e);
}
e.printStackTrace();
}
}
};
jobs.add(Orient.instance().submit(job));
}
final int maxQueueSize = OGlobalConfiguration.QUERY_PARALLEL_RESULT_QUEUE_SIZE.getValueAsInteger() - 1;
boolean cancelQuery = false;
boolean tipProvided = false;
while (runningJobs.get() > 0 || !resultQueue.isEmpty()) {
try {
final AsyncResult result = resultQueue.take();
final int qSize = resultQueue.size();
if (!tipProvided && qSize >= maxQueueSize) {
OLogManager.instance().debug(this, "Parallel query '%s' has result queue full (size=%d), this could reduce concurrency level. Consider increasing queue size with setting: %s=<size>", parserText, maxQueueSize + 1, OGlobalConfiguration.QUERY_PARALLEL_RESULT_QUEUE_SIZE.getKey());
tipProvided = true;
}
if (OExecutionThreadLocal.isInterruptCurrentOperation())
throw new InterruptedException("Operation has been interrupted");
if (result != PARALLEL_END_EXECUTION_THREAD) {
if (!handleResult(result.record, result.context)) {
// STOP EXECUTORS
parallelRunning = false;
break;
}
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
cancelQuery = true;
break;
}
}
parallelRunning = false;
if (cancelQuery) {
// CANCEL ALL THE RUNNING JOBS
for (int i = 0; i < jobs.size(); ++i) {
jobs.get(i).cancel(true);
}
} else {
// JOIN ALL THE JOBS
for (int i = 0; i < jobs.size(); ++i) {
try {
jobs.get(i).get();
context.merge(contexts[i]);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
break;
} catch (final ExecutionException e) {
OLogManager.instance().error(this, "Error on executing parallel query", e);
throw OException.wrapException(new OCommandExecutionException("Error on executing parallel query"), e);
}
}
}
// CHECK FOR ANY EXCEPTION
for (int i = 0; i < jobNumbers; ++i) if (exceptions[i] != null)
throw exceptions[i];
for (int i = 0; i < jobNumbers; ++i) {
if (!results[i])
return false;
}
return true;
}
use of com.orientechnologies.orient.core.db.ODatabaseDocumentInternal in project orientdb by orientechnologies.
the class OCommandExecutorSQLSelect method executeSearchRecord.
protected boolean executeSearchRecord(final OIdentifiable id, final OCommandContext iContext, boolean callHooks) {
if (id == null)
return false;
final ORID identity = id.getIdentity();
if (uniqueResult != null) {
if (uniqueResult.containsKey(identity))
return true;
if (identity.isValid())
uniqueResult.put(identity, identity);
}
if (!checkInterruption())
return false;
final LOCKING_STRATEGY contextLockingStrategy = iContext.getVariable("$locking") != null ? (LOCKING_STRATEGY) iContext.getVariable("$locking") : null;
final LOCKING_STRATEGY localLockingStrategy = contextLockingStrategy != null ? contextLockingStrategy : lockingStrategy;
if (localLockingStrategy != null && !(localLockingStrategy == LOCKING_STRATEGY.DEFAULT || localLockingStrategy == LOCKING_STRATEGY.NONE || localLockingStrategy == LOCKING_STRATEGY.EXCLUSIVE_LOCK || localLockingStrategy == LOCKING_STRATEGY.SHARED_LOCK))
throw new IllegalStateException("Unsupported locking strategy " + localLockingStrategy);
if (localLockingStrategy == LOCKING_STRATEGY.SHARED_LOCK) {
id.lock(false);
if (id instanceof ORecord) {
final ORecord record = (ORecord) id;
record.reload(null, true, false);
}
} else if (localLockingStrategy == LOCKING_STRATEGY.EXCLUSIVE_LOCK) {
id.lock(true);
if (id instanceof ORecord) {
final ORecord record = (ORecord) id;
record.reload(null, true, false);
}
}
ORecord record = null;
try {
if (!(id instanceof ORecord)) {
record = getDatabase().load(id.getIdentity(), null, !isUseCache());
if (id instanceof OContextualRecordId && ((OContextualRecordId) id).getContext() != null) {
Map<String, Object> ridContext = ((OContextualRecordId) id).getContext();
for (String key : ridContext.keySet()) {
context.setVariable(key, ridContext.get(key));
}
}
} else {
record = (ORecord) id;
}
iContext.updateMetric("recordReads", +1);
if (record == null)
// SKIP IT
return true;
if (ORecordInternal.getRecordType(record) != ODocument.RECORD_TYPE && checkSkipBlob())
// SKIP binary records in case of projection.
return true;
iContext.updateMetric("documentReads", +1);
iContext.setVariable("current", record);
if (filter(record, iContext)) {
if (callHooks) {
((ODatabaseDocumentInternal) getDatabase()).callbackHooks(ORecordHook.TYPE.BEFORE_READ, record);
((ODatabaseDocumentInternal) getDatabase()).callbackHooks(ORecordHook.TYPE.AFTER_READ, record);
}
if (parallel) {
try {
applyGroupBy(record, iContext);
resultQueue.put(new AsyncResult(record, iContext));
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return false;
}
tmpQueueOffer.incrementAndGet();
} else {
applyGroupBy(record, iContext);
if (!handleResult(record, iContext)) {
// LIMIT REACHED
return false;
}
}
}
} finally {
if (localLockingStrategy != null && record != null && record.isLocked()) {
// CONTEXT LOCK: lock must be released (no matter if filtered or not)
if (localLockingStrategy == LOCKING_STRATEGY.EXCLUSIVE_LOCK || localLockingStrategy == LOCKING_STRATEGY.SHARED_LOCK) {
record.unlock();
}
}
}
return true;
}
use of com.orientechnologies.orient.core.db.ODatabaseDocumentInternal in project orientdb by orientechnologies.
the class OCommandExecutorSQLSetAware method extractClassFromTarget.
protected OClass extractClassFromTarget(String iTarget) {
// CLASS
if (!iTarget.toUpperCase(Locale.ENGLISH).startsWith(OCommandExecutorSQLAbstract.CLUSTER_PREFIX) && !iTarget.startsWith(OCommandExecutorSQLAbstract.INDEX_PREFIX)) {
if (iTarget.toUpperCase(Locale.ENGLISH).startsWith(OCommandExecutorSQLAbstract.CLASS_PREFIX))
// REMOVE CLASS PREFIX
iTarget = iTarget.substring(OCommandExecutorSQLAbstract.CLASS_PREFIX.length());
if (iTarget.charAt(0) == ORID.PREFIX)
return getDatabase().getMetadata().getSchema().getClassByClusterId(new ORecordId(iTarget).getClusterId());
return getDatabase().getMetadata().getSchema().getClass(iTarget);
}
// CLUSTER
if (iTarget.toUpperCase(Locale.ENGLISH).startsWith(OCommandExecutorSQLAbstract.CLUSTER_PREFIX)) {
String clusterName = iTarget.substring(OCommandExecutorSQLAbstract.CLUSTER_PREFIX.length()).trim();
ODatabaseDocumentInternal db = getDatabase();
if (clusterName.startsWith("[") && clusterName.endsWith("]")) {
String[] clusterNames = clusterName.substring(1, clusterName.length() - 1).split(",");
OClass candidateClass = null;
for (String cName : clusterNames) {
OCluster aCluster = db.getStorage().getClusterByName(cName.trim());
if (aCluster == null) {
return null;
}
OClass aClass = db.getMetadata().getSchema().getClassByClusterId(aCluster.getId());
if (aClass == null) {
return null;
}
if (candidateClass == null || candidateClass.equals(aClass) || candidateClass.isSubClassOf(aClass)) {
candidateClass = aClass;
} else if (!candidateClass.isSuperClassOf(aClass)) {
return null;
}
}
return candidateClass;
} else {
OCluster cluster = db.getStorage().getClusterByName(clusterName);
if (cluster != null) {
return db.getMetadata().getSchema().getClassByClusterId(cluster.getId());
}
}
}
return null;
}
use of com.orientechnologies.orient.core.db.ODatabaseDocumentInternal in project orientdb by orientechnologies.
the class OCommandExecutorSQLAlterSequence method parse.
@Override
public OCommandExecutorSQLAlterSequence parse(OCommandRequest iRequest) {
final OCommandRequestText textRequest = (OCommandRequestText) iRequest;
String queryText = textRequest.getText();
String originalQuery = queryText;
try {
queryText = preParse(queryText, iRequest);
textRequest.setText(queryText);
init((OCommandRequestText) iRequest);
final ODatabaseDocumentInternal database = getDatabase();
final StringBuilder word = new StringBuilder();
parserRequiredKeyword(KEYWORD_ALTER);
parserRequiredKeyword(KEYWORD_SEQUENCE);
this.sequenceName = parserRequiredWord(false, "Expected <sequence name>");
this.params = new OSequence.CreateParams();
String temp;
while ((temp = parseOptionalWord(true)) != null) {
if (parserIsEnded()) {
break;
}
if (temp.equals(KEYWORD_START)) {
String startAsString = parserRequiredWord(true, "Expected <start value>");
this.params.start = Long.parseLong(startAsString);
} else if (temp.equals(KEYWORD_INCREMENT)) {
String incrementAsString = parserRequiredWord(true, "Expected <increment value>");
this.params.increment = Integer.parseInt(incrementAsString);
} else if (temp.equals(KEYWORD_CACHE)) {
String cacheAsString = parserRequiredWord(true, "Expected <cache value>");
this.params.cacheSize = Integer.parseInt(cacheAsString);
}
}
} finally {
textRequest.setText(originalQuery);
}
return this;
}
Aggregations