/** * When just created object is detached to pure pojo it gets temporary id. * Real id is assigned only after transaction commit. This method tracks original * document, intercepts its id change and sets correct id and version into pojo. * So it become safe to use such pojo id outside of transaction. * * @param document original document * @param pojo pojo */ public static void trackIdChange(final ODocument document, final Object pojo) { if (document.getIdentity().isNew()) { ORecordInternal.addIdentityChangeListener(document, new OIdentityChangeListener() { @Override public void onBeforeIdentityChange(final ORecord record) { // not needed } @Override public void onAfterIdentityChange(final ORecord record) { OObjectSerializerHelper.setObjectID(record.getIdentity(), pojo); OObjectSerializerHelper.setObjectVersion(record.getVersion(), pojo); } }); } }
if(doc!=null) if(autoSave) doc.save(); this.orid = doc.getIdentity(); if(orid!=null && orid.isValid()) ORecordInternal.setDirtyManager(savedDocument, null);
ORecordInternal.setIdentity(doc, new ORecordId(-2, getTemporaryRIDCounter(iContext))); result.add(doc); } else { final List<String> nextFields = unwindFields.subList(1, unwindFields.size()); Object fieldValue = doc.field(firstField); if (fieldValue == null || !(fieldValue instanceof Iterable) || fieldValue instanceof ODocument) { result.addAll(unwind(doc, nextFields, iContext)); Iterator iterator = ((Iterable) fieldValue).iterator(); if (!iterator.hasNext()) { ODocument unwindedDoc = new ODocument(); doc.copyTo(unwindedDoc); unwindedDoc.field(firstField, (Object) null); result.addAll(unwind(unwindedDoc, nextFields, iContext));
@Override public boolean add(OIdentifiable e) { if (map.containsKey(e)) return false; if (e == null) map.put(null, null); else if (e instanceof ORecord && e.getIdentity().isNew()) { ORecordInternal.addIdentityChangeListener((ORecord) e, this); ORecordInternal.track(sourceRecord, e); map.put(e, e); } else if (!e.getIdentity().isPersistent()) { ORecordInternal.track(sourceRecord, e); map.put(e, e); } else map.put(e, ENTRY_REMOVAL); setDirty(); fireCollectionChangedEvent( new OMultiValueChangeEvent<OIdentifiable, OIdentifiable>(OMultiValueChangeEvent.OChangeType.ADD, e, e)); return true; }
private void internalTrack(ORecord pointing, OIdentifiable pointed) { if (pointing instanceof ODocument) { if (((ODocument) pointing).isEmbedded()) { ORecordElement ele = pointing.getOwner(); while (!(ele instanceof ODocument) && ele != null && ele.getOwner() != null) ele = ele.getOwner(); if (pointed.getIdentity().isNew()) { if (!(pointed instanceof ODocument) || !((ODocument) pointed).isEmbedded()) { if (references == null) { references = new IdentityHashMap<ODocument, List<OIdentifiable>>(); List<OIdentifiable> point = ORecordInternal.getDirtyManager((ORecord) pointed).getPointed((ORecord) pointed); if (point != null && point.size() > 0) { if (references == null) { ORecordInternal.setDirtyManager((ORecord) pointed, this);
public <RET extends ORecord> RET executeSaveEmptyRecord(ORecord record, String clusterName) { ORecordId rid = (ORecordId) record.getIdentity(); assert rid.isNew(); ORecordInternal.onBeforeIdentityChanged(record); int id = assignAndCheckCluster(record, clusterName); clusterName = getClusterNameById(id); checkSecurity(ORule.ResourceGeneric.CLUSTER, ORole.PERMISSION_CREATE, clusterName); byte[] content = getSerializer().writeClassOnly(record); final OStorageOperationResult<OPhysicalPosition> ppos = getStorage() .createRecord(rid, content, record.getVersion(), recordType, OPERATION_MODE.SYNCHRONOUS.ordinal(), null); ORecordInternal.setVersion(record, ppos.getResult().recordVersion); ((ORecordId) record.getIdentity()).copyFrom(rid); ORecordInternal.onAfterIdentityChanged(record); return (RET) record; }
throw new ORecordNotFoundException(getIdentity(), "The vertex " + getIdentity() + " has been deleted"); final ORID oldIdentity = getIdentity().copy(); final ORecord oldRecord = oldIdentity.getRecord(); if (oldRecord == null) throw new ORecordNotFoundException(getIdentity(), "The vertex " + getIdentity() + " has been deleted"); final ODocument doc = ((ODocument) getRecord()).copy(); oldRecord.delete(); doc.setClassName(iClassName); doc.setDirty(); ORecordInternal.setIdentity(doc, new ORecordId());
iRecord.clear(); fieldTypes = loadFieldTypes(fieldTypes, fieldValueAsString); } else if (fieldName.equals(ODocumentHelper.ATTRIBUTE_TYPE)) { if (iRecord == null || ORecordInternal.getRecordType(iRecord) != fieldValueAsString.charAt(0)) { ORecord localRecord = ODatabaseRecordThreadLocal.instance().get().load(new ORecordId(fieldValueAsString)); if (localRecord != null) iRecord = localRecord; iRecord = new ODocument(); ORecordInternal.setIdentity(iRecord, new ORecordId(fieldValueAsString)); else if (fieldName.equals(ODocumentHelper.ATTRIBUTE_VERSION)) ORecordInternal.setVersion(iRecord, Integer.parseInt(fieldValue)); else if (fieldName.equals(ODocumentHelper.ATTRIBUTE_CLASS)) { continue; iRecord.fromStream(OCommonConst.EMPTY_BYTE_ARRAY); else if (iRecord instanceof OBlob) { if (first != null && first instanceof ORecord && !((ORecord) first).getIdentity().isValid()) type = v instanceof Set<?> ? OType.EMBEDDEDSET : OType.EMBEDDEDLIST; doc.field(fieldName, v, type); continue;
if (ORecordBytes.RECORD_TYPE == ORecordInternal.getRecordType(record)) { for (Entry<String, Object> projection : iProjections.entrySet()) { if ("rid".equalsIgnoreCase(projection.getKey())) { iValue.field(projection.getKey(), record.getIdentity()); inputDocument.copyTo(iValue); else { iValue.field(prjName, (Object) null); continue; if (value instanceof OIdentifiable) { value = ((OIdentifiable) value).getRecord(); if (value != null && !((OIdentifiable) value).getIdentity().isPersistent()) link = false; iValue.field(prjName, iteratorValues, link ? OType.LINKLIST : OType.EMBEDDEDLIST); } else if (projectionValue instanceof ODocument && ((ODocument) projectionValue).getIdentity().getClusterId() < 0) { iValue.field(prjName, projectionValue, OType.EMBEDDED); } else if (projectionValue instanceof Set<?>) { if (value instanceof OIdentifiable) { value = ((OIdentifiable) value).getRecord(); if (value != null && !((OIdentifiable) value).getIdentity().isPersistent()) link = false;
ODocument oDocument = new ODocument(map); iResult.add(oDocument); return oDocument; ODocument doc = new ODocument().fromMap((Map<String, ? extends Object>) current); ORecordInternal.setIdentity(doc, new ORecordId(-2, cursor--)); current = doc; } else if (current instanceof List) { ODocument doc = new ODocument().field("value", current); ORecordInternal.setIdentity(doc, new ORecordId(-2, cursor--)); current = doc;
final ORecord record = recordOperation.getRecord(); if (record instanceof ODocument) { ((ODocument) record).validate(); final int clusterId = recordOperation.getRecord().getIdentity().getClusterId(); clustersToLock.put(clusterId, getClusterById(clusterId)); } else if (recordOperation.type == ORecordOperation.CREATED) { final ORID rid = record.getIdentity(); int clusterId = rid.getClusterId(); if (record.isDirty() && clusterId == ORID.CLUSTER_ID_INVALID && record instanceof ODocument) { final int clusterId = clusterOverride == null ? rid.getClusterId() : clusterOverride; OPhysicalPosition physicalPosition = cluster.allocatePosition(ORecordInternal.getRecordType(rec)); assert atomicOperation.getCounter() == 1; rid.setClusterId(cluster.getId()); if (rid.getClusterPosition() > -1) { while (rid.getClusterPosition() > physicalPosition.clusterPosition) { assert atomicOperation.getCounter() == 1; physicalPosition = cluster.allocatePosition(ORecordInternal.getRecordType(rec)); assert atomicOperation.getCounter() == 1;
updatedRids.put(newRid.copy(), oldRid.copy()); if (!rec.getRecord().getIdentity().equals(newRid)) { ORecordInternal.onBeforeIdentityChanged(rec.getRecord()); final ORecordId recordId = (ORecordId) rec.getRecord().getIdentity(); if (recordId == null) { ORecordInternal.setIdentity(rec.getRecord(), new ORecordId(newRid)); } else { recordId.setClusterPosition(newRid.getClusterPosition()); recordId.setClusterId(newRid.getClusterId()); ORecordInternal.onAfterIdentityChanged(rec.getRecord());
if (record == null || record.getIdentity() == null) { OLogManager.instance().warn(this, "Broken record was detected and will be skipped"); return null; if (schemaImported && record.getIdentity().equals(schemaRecordId)) { if (!includeClusters.contains(database.getClusterNameById(record.getIdentity().getClusterId()))) { return null; if (excludeClusters.contains(database.getClusterNameById(record.getIdentity().getClusterId()))) return null; if (excludeClasses.contains(((ODocument) record).getClassName())) { return null; if (record.getIdentity().getClusterId() == 0 && record.getIdentity().getClusterPosition() == 1) ORecordInternal.setVersion(record, 0); record.setDirty(); ORecordInternal.setIdentity(record, new ORecordId());
final int clusterId = txEntry.getRID().getClusterId(); clustersToLock.put(clusterId, getClusterById(clusterId)); if (!rec.getIdentity().isPersistent()) { if (rec.isDirty()) { final ORecordId rid = (ORecordId) rec.getIdentity().copy(); final ORecordId oldRID = rid.copy(); final OCluster cluster = getClusterById(rid.getClusterId()); final OPhysicalPosition ppos = cluster.allocatePosition(ORecordInternal.getRecordType(rec)); rid.setClusterPosition(ppos.clusterPosition); clientTx.updateIdentityAfterCommit(oldRID, rid); final ORecordId rid = (ORecordId) rec.getIdentity(); final OCluster cluster = getClusterById(rid.getClusterId()); final OPhysicalPosition ppos = cluster.allocatePosition(ORecordInternal.getRecordType(rec)); if (ppos.clusterPosition != rid.getClusterPosition()) { throw new OConcurrentCreateException(rid, new ORecordId(rid.getClusterId(), ppos.clusterPosition));
public OIndex<?> createIndex(final String iName, final String iType, final OIndexDefinition iIndexDefinition, final int[] iClusterIdsToIndex, final OProgressListener progressListener, ODocument metadata, String engine) { String createIndexDDL; if (iIndexDefinition != null) createIndexDDL = iIndexDefinition.toCreateIndexDDL(iName, iType, engine); else createIndexDDL = new OSimpleKeyIndexDefinition().toCreateIndexDDL(iName, iType, engine); if (metadata != null) createIndexDDL += " " + OCommandExecutorSQLCreateIndex.KEYWORD_METADATA + " " + metadata.toJSON(); acquireExclusiveLock(); try { if (progressListener != null) progressListener.onBegin(this, 0, false); getDatabase().command(createIndexDDL).close(); ORecordInternal.setIdentity(document, new ORecordId(getDatabase().getStorage().getConfiguration().getIndexMgrRecordId())); if (progressListener != null) progressListener.onCompletition(this, true); reload(); return preProcessBeforeReturn(getDatabase(), indexes.get(iName)); } catch (OCommandExecutionException x) { throw new OIndexException(x.getMessage()); } finally { releaseExclusiveLock(); } }
last = bytes.offset; bytes.offset = headerCursor; document.field(fieldName, value, type); } else document.field(fieldName, null, (OType[]) null); ORecordInternal.clearSource(document);
RET value = this.rawField(iFieldName); if (!iFieldName.startsWith("@") && _lazyLoad && value instanceof ORID && (((ORID) value).isPersistent() || ((ORID) value) .isNew()) && ODatabaseRecordThreadLocal.instance().isDefined()) { RET newValue = getDatabase().load((ORID) value); if (newValue != null) { unTrack((ORID) value); track((OIdentifiable) newValue); value = newValue; if (this.isTrackingChanges()) { ORecordInternal.setDirtyManager((ORecord) value, this.getDirtyManager());
return iCurrent.getIdentity(); else if (iFieldName.equalsIgnoreCase(ATTRIBUTE_RID_ID)) return iCurrent.getIdentity().getClusterId(); else if (iFieldName.equalsIgnoreCase(ATTRIBUTE_RID_POS)) return iCurrent.getIdentity().getClusterPosition(); else if (iFieldName.equalsIgnoreCase(ATTRIBUTE_VERSION)) return iCurrent.getRecord().getVersion(); else if (iFieldName.equalsIgnoreCase(ATTRIBUTE_CLASS)) return ((ODocument) iCurrent.getRecord()).getClassName(); else if (iFieldName.equalsIgnoreCase(ATTRIBUTE_TYPE)) return Orient.instance().getRecordFactoryManager().getRecordTypeName(ORecordInternal.getRecordType(iCurrent.getRecord())); else if (iFieldName.equalsIgnoreCase(ATTRIBUTE_SIZE)) { final byte[] stream = iCurrent.getRecord().toStream(); return stream != null ? stream.length : 0; } else if (iFieldName.equalsIgnoreCase(ATTRIBUTE_FIELDS)) return ((ODocument) iCurrent.getRecord()).fieldNames(); else if (iFieldName.equalsIgnoreCase(ATTRIBUTE_RAW)) return new String(iCurrent.getRecord().toStream()); return null; doc.checkForFields(iFieldName); ODocumentEntry entry = doc._fields.get(iFieldName); return entry != null ? entry.value : null;
ORecordInternal.setIdentity(iRecord, new ORecordId(current.getClusterId(), current.getClusterPosition())); iRecord = database.load(iRecord, fetchPlan, false, true, lockingStrategy); } else
"" + (char) ORecordInternal.getRecordType(record)); if (settings.attribSameRow) firstAttribute = false; if (settings.includeId && record.getIdentity() != null && record.getIdentity().isValid()) { json.writeAttribute(!firstAttribute ? settings.indentLevel : 1, firstAttribute, ODocumentHelper.ATTRIBUTE_RID, record.getIdentity().toString()); if (settings.attribSameRow) firstAttribute = false; firstAttribute = false; if (settings.includeClazz && record instanceof ODocument && ((ODocument) record).getClassName() != null) { json.writeAttribute(firstAttribute ? settings.indentLevel : 1, firstAttribute, ODocumentHelper.ATTRIBUTE_CLASS, ((ODocument) record).getClassName()); if (settings.attribSameRow) firstAttribute = false;