use of com.orientechnologies.common.util.OPair in project orientdb by orientechnologies.
the class OStorageRemote method readCollectionChanges.
private Map<OBonsaiCollectionPointer, OPair<Long, Long>> readCollectionChanges(final OChannelBinaryAsynchClient network) throws IOException {
final int count = network.readInt();
final Map<OBonsaiCollectionPointer, OPair<Long, Long>> changes = new HashMap<OBonsaiCollectionPointer, OPair<Long, Long>>(count);
for (int i = 0; i < count; i++) {
final long mBitsOfId = network.readLong();
final long lBitsOfId = network.readLong();
final OBonsaiCollectionPointer pointer = OCollectionNetworkSerializer.INSTANCE.readCollectionPointer(network);
changes.put(pointer, new OPair<Long, Long>(mBitsOfId, lBitsOfId));
}
return changes;
}
use of com.orientechnologies.common.util.OPair in project orientdb by orientechnologies.
the class OCommandExecutorSQLInsert method parse.
@SuppressWarnings("unchecked")
public OCommandExecutorSQLInsert 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);
final ODatabaseDocument database = getDatabase();
init((OCommandRequestText) iRequest);
className = null;
newRecords = null;
content = null;
if (parserTextUpperCase.endsWith(KEYWORD_UNSAFE)) {
unsafe = true;
parserText = parserText.substring(0, parserText.length() - KEYWORD_UNSAFE.length() - 1);
parserTextUpperCase = parserTextUpperCase.substring(0, parserTextUpperCase.length() - KEYWORD_UNSAFE.length() - 1);
}
parserRequiredKeyword("INSERT");
parserRequiredKeyword("INTO");
String subjectName = parserRequiredWord(true, "Invalid subject name. Expected cluster, class or index");
if (subjectName.startsWith(OCommandExecutorSQLAbstract.CLUSTER_PREFIX)) {
// CLUSTER
clusterName = subjectName.substring(OCommandExecutorSQLAbstract.CLUSTER_PREFIX.length());
try {
int clusterId = Integer.parseInt(clusterName);
clusterName = database.getClusterNameById(clusterId);
} catch (Exception e) {
//not an integer
}
} else if (subjectName.startsWith(OCommandExecutorSQLAbstract.INDEX_PREFIX))
// INDEX
indexName = subjectName.substring(OCommandExecutorSQLAbstract.INDEX_PREFIX.length());
else {
// CLASS
if (subjectName.startsWith(OCommandExecutorSQLAbstract.CLASS_PREFIX))
subjectName = subjectName.substring(OCommandExecutorSQLAbstract.CLASS_PREFIX.length());
final OClass cls = ((OMetadataInternal) database.getMetadata()).getImmutableSchemaSnapshot().getClass(subjectName);
if (cls == null)
throwParsingException("Class " + subjectName + " not found in database");
if (!unsafe && cls.isSubClassOf("E"))
// FOUND EDGE
throw new OCommandExecutionException("'INSERT' command cannot create Edges. Use 'CREATE EDGE' command instead, or apply the 'UNSAFE' keyword to force it");
className = cls.getName();
clazz = database.getMetadata().getSchema().getClass(className);
if (clazz == null)
throw new OQueryParsingException("Class '" + className + "' was not found");
}
if (clusterName != null && className == null) {
ODatabaseDocumentInternal db = getDatabase();
OCluster cluster = db.getStorage().getClusterByName(clusterName);
if (cluster != null) {
clazz = db.getMetadata().getSchema().getClassByClusterId(cluster.getId());
if (clazz != null) {
className = clazz.getName();
}
}
}
parserSkipWhiteSpaces();
if (parserIsEnded())
throwSyntaxErrorException("Set of fields is missed. Example: (name, surname) or SET name = 'Bill'");
final String temp = parseOptionalWord(true);
if (parserGetLastWord().equalsIgnoreCase("cluster")) {
clusterName = parserRequiredWord(false);
parserSkipWhiteSpaces();
if (parserIsEnded())
throwSyntaxErrorException("Set of fields is missed. Example: (name, surname) or SET name = 'Bill'");
} else
parserGoBack();
newRecords = new ArrayList<Map<String, Object>>();
Boolean sourceClauseProcessed = false;
if (parserGetCurrentChar() == '(') {
parseValues();
parserNextWord(true, " \r\n");
sourceClauseProcessed = true;
} else {
parserNextWord(true, " ,\r\n");
if (parserGetLastWord().equals(KEYWORD_CONTENT)) {
newRecords = null;
parseContent();
sourceClauseProcessed = true;
} else if (parserGetLastWord().equals(KEYWORD_SET)) {
final List<OPair<String, Object>> fields = new ArrayList<OPair<String, Object>>();
parseSetFields(clazz, fields);
newRecords.add(OPair.convertToMap(fields));
sourceClauseProcessed = true;
}
}
if (sourceClauseProcessed)
parserNextWord(true, " \r\n");
// it has to be processed before KEYWORD_FROM in order to not be taken as part of SELECT
if (parserGetLastWord().equals(KEYWORD_RETURN)) {
parseReturn(!sourceClauseProcessed);
parserNextWord(true, " \r\n");
}
if (!sourceClauseProcessed) {
if (parserGetLastWord().equals(KEYWORD_FROM)) {
newRecords = null;
subQuery = new OSQLAsynchQuery<OIdentifiable>(parserText.substring(parserGetCurrentPosition()), this);
}
}
} finally {
textRequest.setText(originalQuery);
}
return this;
}
use of com.orientechnologies.common.util.OPair in project orientdb by orientechnologies.
the class OAtomicOperationsManager method startAtomicOperation.
/**
* Starts atomic operation inside of current thread. If atomic operation has been already started, current atomic operation
* instance will be returned. All durable components have to call this method at the beginning of any data modification
* operation.
* <p>
* <p>In current implementation of atomic operation, each component which is participated in atomic operation is hold under
* exclusive lock till atomic operation will not be completed (committed or rollbacked).
* <p>
* <p>If other thread is going to read data from component it has to acquire read lock inside of atomic operation manager {@link
* #acquireReadLock(ODurableComponent)}, otherwise data consistency will be compromised.
* <p>
* <p>Atomic operation may be delayed if start of atomic operations is prohibited by call of {@link
* #freezeAtomicOperations(Class, String)} method. If mentioned above method is called then execution of current method will be
* stopped till call of {@link #releaseAtomicOperations(long)} method or exception will be thrown. Concrete behaviour depends on
* real values of parameters of {@link #freezeAtomicOperations(Class, String)} method.
*
* @param trackNonTxOperations If this flag set to <code>true</code> then special record {@link ONonTxOperationPerformedWALRecord}
* will be added to WAL in case of atomic operation is started outside of active storage transaction.
* During storage restore procedure this record is monitored and if given record is present then
* rebuild of all indexes is performed.
* @param lockName Name of lock (usually name of component) which is going participate in atomic operation.
*
* @return Instance of active atomic operation.
*/
public OAtomicOperation startAtomicOperation(String lockName, boolean trackNonTxOperations) throws IOException {
OAtomicOperation operation = currentOperation.get();
if (operation != null) {
operation.incrementCounter();
if (trackAtomicOperations) {
final OPair<String, Deque<OPair<String, StackTraceElement[]>>> atomicPair = activeAtomicOperations.get(operation.getOperationUnitId());
if (atomicPair == null) {
throw new IllegalStateException("Atomic operation is not registered in manager");
}
final Deque<OPair<String, StackTraceElement[]>> stack = atomicPair.getValue();
stack.push(new OPair<String, StackTraceElement[]>(lockName, Thread.currentThread().getStackTrace()));
}
if (lockName != null)
acquireExclusiveLockTillOperationComplete(operation, lockName);
return operation;
}
atomicOperationsCount.increment();
while (freezeRequests.get() > 0) {
assert freezeRequests.get() >= 0;
atomicOperationsCount.decrement();
throwFreezeExceptionIfNeeded();
final Thread thread = Thread.currentThread();
addThreadInWaitingList(thread);
if (freezeRequests.get() > 0) {
LockSupport.park(this);
}
atomicOperationsCount.increment();
}
assert freezeRequests.get() >= 0;
final boolean useWal = useWal();
final OOperationUnitId unitId = OOperationUnitId.generateId();
final OLogSequenceNumber lsn = useWal ? writeAheadLog.logAtomicOperationStartRecord(true, unitId) : null;
operation = new OAtomicOperation(lsn, unitId, readCache, writeCache, storage.getId(), performanceStatisticManager);
currentOperation.set(operation);
if (trackAtomicOperations) {
final Thread thread = Thread.currentThread();
final Deque<OPair<String, StackTraceElement[]>> lockStack = new LinkedList<OPair<String, StackTraceElement[]>>();
final OPair<String, StackTraceElement[]> lockPair = new OPair<String, StackTraceElement[]>(lockName, thread.getStackTrace());
lockStack.push(lockPair);
activeAtomicOperations.put(unitId, new OPair<String, Deque<OPair<String, StackTraceElement[]>>>(thread.getName(), lockStack));
}
if (useWal && trackNonTxOperations && storage.getStorageTransaction() == null)
writeAheadLog.log(new ONonTxOperationPerformedWALRecord());
if (lockName != null)
acquireExclusiveLockTillOperationComplete(operation, lockName);
return operation;
}
use of com.orientechnologies.common.util.OPair in project orientdb by orientechnologies.
the class OAtomicOperationsManager method endAtomicOperation.
public OAtomicOperation endAtomicOperation(boolean rollback, Exception exception, String lockName) throws IOException {
final OAtomicOperation operation = currentOperation.get();
assert operation != null;
if (trackAtomicOperations) {
final OPair<String, Deque<OPair<String, StackTraceElement[]>>> atomicPair = activeAtomicOperations.get(operation.getOperationUnitId());
if (atomicPair == null)
throw new IllegalStateException("Atomic operation is not registered inside manager");
final Deque<OPair<String, StackTraceElement[]>> atomicStack = atomicPair.getValue();
final OPair<String, StackTraceElement[]> stackPair = atomicStack.peek();
if (lockName == null) {
if (stackPair.getKey() != null)
throw new IllegalStateException("Lock name stored inside of stack of atomic operation is different from provided lock name");
} else if (!lockName.equals(stackPair.getKey())) {
throw new IllegalStateException("Lock name stored inside of stack of atomic operation is different from provided lock name");
}
atomicStack.poll();
}
if (rollback) {
operation.rollback(exception);
}
if (operation.isRollback() && !rollback) {
final StringWriter writer = new StringWriter();
writer.append("Atomic operation was rolled back by internal component");
if (operation.getRollbackException() != null) {
writer.append(", exception which caused this rollback is :\n");
operation.getRollbackException().printStackTrace(new PrintWriter(writer));
writer.append("\r\n");
}
atomicOperationsCount.decrement();
final ONestedRollbackException nre = new ONestedRollbackException(writer.toString());
throw OException.wrapException(nre, exception);
}
final int counter = operation.getCounter();
assert counter > 0;
if (counter == 1) {
final boolean useWal = useWal();
if (!operation.isRollback())
operation.commitChanges(useWal ? writeAheadLog : null);
if (useWal)
writeAheadLog.logAtomicOperationEndRecord(operation.getOperationUnitId(), rollback, operation.getStartLSN(), operation.getMetadata());
// We have to decrement the counter after the disk operations, otherwise, if they
// fail, we will be unable to rollback the atomic operation later.
operation.decrementCounter();
currentOperation.set(null);
if (trackAtomicOperations) {
activeAtomicOperations.remove(operation.getOperationUnitId());
}
for (String lockObject : operation.lockedObjects()) lockManager.releaseLock(this, lockObject, OOneEntryPerKeyLockManager.LOCK.EXCLUSIVE);
atomicOperationsCount.decrement();
} else
operation.decrementCounter();
return operation;
}
use of com.orientechnologies.common.util.OPair in project orientdb by orientechnologies.
the class OCommandExecutorSQLSetAware method parseSetFields.
protected void parseSetFields(final OClass iClass, final List<OPair<String, Object>> fields) {
String fieldName;
String fieldValue;
while (!parserIsEnded() && (fields.size() == 0 || parserGetLastSeparator() == ',' || parserGetCurrentChar() == ',')) {
fieldName = parserRequiredWord(false, "Field name expected");
if (fieldName.equalsIgnoreCase(KEYWORD_WHERE)) {
parserGoBack();
break;
}
parserNextChars(false, true, "=");
fieldValue = parserRequiredWord(false, "Value expected", " =><,\r\n", true);
// INSERT TRANSFORMED FIELD VALUE
final Object v = convertValue(iClass, fieldName, getFieldValueCountingParameters(fieldValue));
fields.add(new OPair(fieldName, v));
parserSkipWhiteSpaces();
}
if (fields.size() == 0)
throwParsingException("Entries to set <field> = <value> are missed. Example: name = 'Bill', salary = 300.2");
}
Aggregations