use of com.orientechnologies.orient.core.db.ODatabaseDocumentInternal 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.ODatabaseDocumentInternal in project orientdb by orientechnologies.
the class OCommandExecutorSQLDeleteEdge method parse.
@SuppressWarnings("unchecked")
public OCommandExecutorSQLDeleteEdge parse(final OCommandRequest iRequest) {
final OCommandRequestText textRequest = (OCommandRequestText) iRequest;
String queryText = textRequest.getText();
String originalQuery = queryText;
try {
// System.out.println("NEW PARSER FROM: " + queryText);
queryText = preParse(queryText, iRequest);
// System.out.println("NEW PARSER TO: " + queryText);
textRequest.setText(queryText);
init((OCommandRequestText) iRequest);
parserRequiredKeyword("DELETE");
parserRequiredKeyword("EDGE");
OClass clazz = null;
String where = null;
String temp = parseOptionalWord(true);
String originalTemp = null;
int limit = -1;
if (temp != null && !parserIsEnded())
originalTemp = parserText.substring(parserGetPreviousPosition(), parserGetCurrentPosition()).trim();
final OModifiableBoolean shutdownFlag = new OModifiableBoolean();
ODatabaseDocumentInternal curDb = ODatabaseRecordThreadLocal.INSTANCE.get();
final OrientGraph graph = OGraphCommandExecutorSQLFactory.getGraph(false, shutdownFlag);
try {
while (temp != null) {
if (temp.equals("FROM")) {
fromExpr = parserRequiredWord(false, "Syntax error", " =><,\r\n");
if (rids != null)
throwSyntaxErrorException("FROM '" + fromExpr + "' is not allowed when specify a RIDs (" + rids + ")");
} else if (temp.equals("TO")) {
toExpr = parserRequiredWord(false, "Syntax error", " =><,\r\n");
if (rids != null)
throwSyntaxErrorException("TO '" + toExpr + "' is not allowed when specify a RID (" + rids + ")");
} else if (temp.startsWith("#")) {
rids = new ArrayList<ORecordId>();
rids.add(new ORecordId(temp));
if (fromExpr != null || toExpr != null)
throwSyntaxErrorException("Specifying the RID " + rids + " is not allowed with FROM/TO");
} else if (temp.startsWith("[") && temp.endsWith("]")) {
temp = temp.substring(1, temp.length() - 1);
rids = new ArrayList<ORecordId>();
for (String rid : temp.split(",")) {
rid = rid.trim();
if (!rid.startsWith("#")) {
throwSyntaxErrorException("Not a valid RID: " + rid);
}
rids.add(new ORecordId(rid));
}
} else if (temp.equals(KEYWORD_WHERE)) {
if (clazz == null)
// ASSIGN DEFAULT CLASS
clazz = graph.getEdgeType(OrientEdgeType.CLASS_NAME);
where = parserGetCurrentPosition() > -1 ? " " + parserText.substring(parserGetCurrentPosition()) : "";
if (this.preParsedStatement != null) {
StringBuilder builder = new StringBuilder();
((ODeleteEdgeStatement) this.preParsedStatement).getWhereClause().toString(parameters, builder);
where = builder.toString();
}
compiledFilter = OSQLEngine.getInstance().parseCondition(where, getContext(), KEYWORD_WHERE);
break;
} else if (temp.equals(KEYWORD_BATCH)) {
temp = parserNextWord(true);
if (temp != null)
batch = Integer.parseInt(temp);
} else if (temp.equals(KEYWORD_LIMIT)) {
temp = parserNextWord(true);
if (temp != null)
limit = Integer.parseInt(temp);
} else if (temp.length() > 0) {
// GET/CHECK CLASS NAME
label = originalTemp;
clazz = graph.getEdgeType(temp);
if (clazz == null)
throw new OCommandSQLParsingException("Class '" + temp + "' was not found");
}
temp = parseOptionalWord(true);
if (parserIsEnded())
break;
}
if (where == null)
if (limit > -1) {
where = " LIMIT " + limit;
} else {
where = "";
}
else
where = " WHERE " + where;
if (fromExpr == null && toExpr == null && rids == null)
if (clazz == null)
// DELETE ALL THE EDGES
query = graph.getRawGraph().command(new OSQLAsynchQuery<ODocument>("select from E" + where, this));
else
// DELETE EDGES OF CLASS X
query = graph.getRawGraph().command(new OSQLAsynchQuery<ODocument>("select from " + clazz.getName() + where, this));
return this;
} finally {
if (shutdownFlag.getValue())
graph.shutdown(false, false);
ODatabaseRecordThreadLocal.INSTANCE.set(curDb);
}
} finally {
textRequest.setText(originalQuery);
}
}
use of com.orientechnologies.orient.core.db.ODatabaseDocumentInternal in project orientdb by orientechnologies.
the class OLogManager method log.
public void log(final Object iRequester, final Level iLevel, String iMessage, final Throwable iException, final Object... iAdditionalArgs) {
if (iMessage != null) {
try {
final ODatabaseDocumentInternal db = ODatabaseRecordThreadLocal.INSTANCE != null ? ODatabaseRecordThreadLocal.INSTANCE.getIfDefined() : null;
if (db != null && db.getStorage() != null && db.getStorage() instanceof OAbstractPaginatedStorage) {
final String dbName = db.getStorage().getName();
if (dbName != null)
iMessage = "$ANSI{green {db=" + dbName + "}} " + iMessage;
}
} catch (Throwable e) {
}
final String requesterName;
if (iRequester instanceof Class<?>) {
requesterName = ((Class<?>) iRequester).getName();
} else if (iRequester != null) {
requesterName = iRequester.getClass().getName();
} else {
requesterName = DEFAULT_LOG;
}
Logger log = loggersCache.get(requesterName);
if (log == null) {
log = Logger.getLogger(requesterName);
if (log != null) {
Logger oldLogger = loggersCache.putIfAbsent(requesterName, log);
if (oldLogger != null)
log = oldLogger;
}
}
if (log == null) {
// USE SYSERR
try {
System.err.println(String.format(iMessage, iAdditionalArgs));
} catch (Exception e) {
OLogManager.instance().warn(this, "Error on formatting message", e);
}
} else if (log.isLoggable(iLevel)) {
// USE THE LOG
try {
final String msg = String.format(iMessage, iAdditionalArgs);
if (iException != null)
log.log(iLevel, msg, iException);
else
log.log(iLevel, msg);
} catch (Exception e) {
System.err.print(String.format("Error on formatting message '%s'. Exception: %s", iMessage, e.toString()));
}
}
}
}
use of com.orientechnologies.orient.core.db.ODatabaseDocumentInternal in project orientdb by orientechnologies.
the class OAbstractPaginatedStorage method executeCommand.
public Object executeCommand(final OCommandRequestText iCommand, final OCommandExecutor executor) {
try {
if (iCommand.isIdempotent() && !executor.isIdempotent())
throw new OCommandExecutionException("Cannot execute non idempotent command");
long beginTime = Orient.instance().getProfiler().startChrono();
try {
ODatabaseDocumentInternal db = ODatabaseRecordThreadLocal.INSTANCE.get();
// CALL BEFORE COMMAND
Iterable<ODatabaseListener> listeners = db.getListeners();
for (ODatabaseListener oDatabaseListener : listeners) {
oDatabaseListener.onBeforeCommand(iCommand, executor);
}
boolean foundInCache = false;
Object result = null;
if (iCommand.isCacheableResult() && executor.isCacheable() && iCommand.getParameters() == null) {
// TRY WITH COMMAND CACHE
result = db.getMetadata().getCommandCache().get(db.getUser(), iCommand.getText(), iCommand.getLimit());
if (result != null) {
foundInCache = true;
if (iCommand.getResultListener() != null) {
// INVOKE THE LISTENER IF ANY
if (result instanceof Collection) {
for (Object o : (Collection) result) iCommand.getResultListener().result(o);
} else
iCommand.getResultListener().result(result);
// RESET THE RESULT TO AVOID TO SEND IT TWICE
result = null;
}
}
}
if (!foundInCache) {
// EXECUTE THE COMMAND
result = executor.execute(iCommand.getParameters());
if (result != null && iCommand.isCacheableResult() && executor.isCacheable() && (iCommand.getParameters() == null || iCommand.getParameters().isEmpty()))
// CACHE THE COMMAND RESULT
db.getMetadata().getCommandCache().put(db.getUser(), iCommand.getText(), result, iCommand.getLimit(), executor.getInvolvedClusters(), System.currentTimeMillis() - beginTime);
}
// CALL AFTER COMMAND
for (ODatabaseListener oDatabaseListener : listeners) {
oDatabaseListener.onAfterCommand(iCommand, executor, result);
}
return result;
} catch (OException e) {
// PASS THROUGH
throw e;
} catch (Exception e) {
throw OException.wrapException(new OCommandExecutionException("Error on execution of command: " + iCommand), e);
} finally {
if (Orient.instance().getProfiler().isRecording()) {
final ODatabaseDocumentInternal db = ODatabaseRecordThreadLocal.INSTANCE.getIfDefined();
if (db != null) {
final OSecurityUser user = db.getUser();
final String userString = user != null ? user.toString() : null;
Orient.instance().getProfiler().stopChrono("db." + ODatabaseRecordThreadLocal.INSTANCE.get().getName() + ".command." + iCommand.toString(), "Command executed against the database", beginTime, "db.*.command.*", null, userString);
}
}
}
} catch (RuntimeException e) {
throw logAndPrepareForRethrow(e);
} catch (Error e) {
throw logAndPrepareForRethrow(e);
} catch (Throwable t) {
throw logAndPrepareForRethrow(t);
}
}
use of com.orientechnologies.orient.core.db.ODatabaseDocumentInternal in project orientdb by orientechnologies.
the class OAbstractPaginatedStorage method commit.
public List<ORecordOperation> commit(final OTransaction clientTx, Runnable callback) {
try {
checkOpeness();
checkLowDiskSpaceFullCheckpointRequestsAndBackgroundDataFlushExceptions();
txBegun.incrementAndGet();
final ODatabaseDocumentInternal databaseRecord = (ODatabaseDocumentInternal) clientTx.getDatabase();
final OIndexManagerProxy indexManager = databaseRecord.getMetadata().getIndexManager();
final TreeMap<String, OTransactionIndexChanges> indexesToCommit = getSortedIndexEntries(clientTx);
final Map<ORecordOperation, Integer> clusterOverrides = new IdentityHashMap<ORecordOperation, Integer>();
databaseRecord.getMetadata().makeThreadLocalSchemaSnapshot();
if (OLogManager.instance().isDebugEnabled())
OLogManager.instance().debug(this, "Committing transaction %d on database '%s' (items=%d thread=%d)...", clientTx.getId(), databaseRecord.getName(), clientTx.getEntryCount(), Thread.currentThread().getId());
final Iterable<ORecordOperation> entries = (Iterable<ORecordOperation>) clientTx.getAllRecordEntries();
final TreeMap<Integer, OCluster> clustersToLock = new TreeMap<Integer, OCluster>();
final Set<ORecordOperation> newRecords = new TreeSet<ORecordOperation>(new Comparator<ORecordOperation>() {
@Override
public int compare(final ORecordOperation o1, final ORecordOperation o2) {
return o1.getRecord().getIdentity().compareTo(o2.getRecord().getIdentity());
}
});
for (ORecordOperation txEntry : entries) {
if (txEntry.type == ORecordOperation.CREATED || txEntry.type == ORecordOperation.RECYCLED || txEntry.type == ORecordOperation.UPDATED) {
final ORecord record = txEntry.getRecord();
if (record instanceof ODocument)
((ODocument) record).validate();
}
if (txEntry.type == ORecordOperation.UPDATED || txEntry.type == ORecordOperation.DELETED) {
final int clusterId = txEntry.getRecord().getIdentity().getClusterId();
clustersToLock.put(clusterId, getClusterById(clusterId));
} else if (txEntry.type == ORecordOperation.CREATED || txEntry.type == ORecordOperation.RECYCLED) {
newRecords.add(txEntry);
final ORecord record = txEntry.getRecord();
final ORID rid = record.getIdentity();
int clusterId = rid.getClusterId();
if (record.isDirty() && clusterId == ORID.CLUSTER_ID_INVALID && record instanceof ODocument) {
// TRY TO FIX CLUSTER ID TO THE DEFAULT CLUSTER ID DEFINED IN SCHEMA CLASS
final OImmutableClass class_ = ODocumentInternal.getImmutableSchemaClass(((ODocument) record));
if (class_ != null) {
clusterId = class_.getClusterForNewInstance((ODocument) record);
clusterOverrides.put(txEntry, clusterId);
}
}
clustersToLock.put(clusterId, getClusterById(clusterId));
}
}
final List<ORecordOperation> result = new ArrayList<ORecordOperation>();
final List<Lock[]> indexKeyLockList = new ArrayList<Lock[]>(indexesToCommit.size());
stateLock.acquireReadLock();
try {
try {
try {
checkOpeness();
lockIndexKeys(indexManager, indexesToCommit, indexKeyLockList);
makeStorageDirty();
startStorageTx(clientTx);
lockClusters(clustersToLock);
lockRidBags(clustersToLock, indexesToCommit);
lockIndexes(indexesToCommit);
Map<ORecordOperation, OPhysicalPosition> positions = new IdentityHashMap<ORecordOperation, OPhysicalPosition>();
for (ORecordOperation txEntry : newRecords) {
ORecord rec = txEntry.getRecord();
ORecordId rid = (ORecordId) rec.getIdentity().copy();
ORecordId oldRID = rid.copy();
final Integer clusterOverride = clusterOverrides.get(txEntry);
final int clusterId = clusterOverride == null ? rid.getClusterId() : clusterOverride;
final OCluster cluster = getClusterById(clusterId);
final OPaginatedCluster.RECORD_STATUS recordStatus = rid.getClusterPosition() > -1 ? ((OPaginatedCluster) cluster).getRecordStatus(rid.getClusterPosition()) : OPaginatedCluster.RECORD_STATUS.NOT_EXISTENT;
OPhysicalPosition ppos = new OPhysicalPosition(rid.getClusterPosition());
if (recordStatus == OPaginatedCluster.RECORD_STATUS.NOT_EXISTENT) {
ppos = cluster.allocatePosition(ORecordInternal.getRecordType(rec));
if (rid.getClusterPosition() > -1) {
// RECORD HAVING A HIGHER CLUSTER POSITION
while (rid.getClusterPosition() > ppos.clusterPosition) {
ppos = cluster.allocatePosition(ORecordInternal.getRecordType(rec));
}
if (rid.getClusterPosition() != ppos.clusterPosition)
throw new OConcurrentCreateException(rid, new ORecordId(rid.getClusterId(), ppos.clusterPosition));
}
} else if (recordStatus == OPaginatedCluster.RECORD_STATUS.REMOVED) {
// RECYCLE THE RID AND OVERWRITE IT WITH THE NEW CONTENT
final ORecord record = txEntry.getRecord();
record.setDirty();
recyclePosition(rid, record.toStream(), record.getVersion(), ORecordInternal.getRecordType(record));
}
positions.put(txEntry, ppos);
rid.setClusterId(cluster.getId());
rid.setClusterPosition(ppos.clusterPosition);
if (!oldRID.equals(rid))
clientTx.updateIdentityAfterCommit(oldRID, rid);
}
for (ORecordOperation txEntry : entries) {
commitEntry(txEntry, positions.get(txEntry));
result.add(txEntry);
}
commitIndexes(indexesToCommit);
endStorageTx();
OTransactionAbstract.updateCacheFromEntries(clientTx, entries, true);
txCommit.incrementAndGet();
} catch (IOException ioe) {
makeRollback(clientTx, ioe);
} catch (RuntimeException e) {
makeRollback(clientTx, e);
} finally {
unlockIndexKeys(indexesToCommit, indexKeyLockList);
transaction.set(null);
}
} finally {
databaseRecord.getMetadata().clearThreadLocalSchemaSnapshot();
}
} finally {
stateLock.releaseReadLock();
}
if (OLogManager.instance().isDebugEnabled())
OLogManager.instance().debug(this, "Committed transaction %d on database '%s' (result=%s thread=%d)", clientTx.getId(), databaseRecord.getName(), result, Thread.currentThread().getId());
return result;
} catch (RuntimeException e) {
throw logAndPrepareForRethrow(e);
} catch (Error e) {
throw logAndPrepareForRethrow(e);
} catch (Throwable t) {
throw logAndPrepareForRethrow(t);
}
}
Aggregations