/** * helper to load relation link document * @param document of relation * @param logError log null error? * @return relation link or null */ private @Nullable ODocument getRelationLink(ODocument document, boolean logError) { Object relationLinkO = document.field("relationLink"); if (relationLinkO == null) { if (logError) logger.error("Could not create relationLink entity while converting relation."); return null; } if (relationLinkO instanceof ODocument) return (ODocument) relationLinkO; else if (relationLinkO instanceof ORecordId) { ODocument doc = repositoryFactory.getDb().load((ORecordId) relationLinkO); if (doc == null) logger.error("Instance vanished from database: " + relationLinkO); return doc; } else { logger.error("Invalid class type: " + relationLinkO.getClass().getName()); return null; } }
/** * Gets record associated with the specified key. * * @param key The key of the record. Cannot be {@literal null}. * @param recordView Type of the record representation. * @return Selector for records in this storage. * @throws ClassCastException Unsupported record view. */ @Override public <R extends Record> Optional<R> getRecord(final Comparable<?> key, final Class<R> recordView) { final PersistentRecord record = getRecord(key, PersistentRecord::new); if (record != null) { record.setDatabase(database); record.setClassName(getDocumentClass().getName()); return database.load(record) == null ? Optional.empty() : Optional.of(record).map(recordView::cast); } else return Optional.empty(); }
/** * find single entity by Orient Id * @param id string representation of orient db, e.g. "#11:1" * @return entity or null */ public T find(@Nullable String id) { // sanity check to return null when looking for null if (id == null || id.isEmpty()) return null; try { initDb(); ODocument document = (ODocument) db.load(new ORecordId(id)); if (document == null) return null; // not correct class? => return null if (!document.getClassName().equals(getModelClassName())) { logger.warn("Entity with id " + id + " is not of type " + getModelClassName()); return null; } // correct class => convert to correct entity return convertToEntity(document); } catch (Exception e) { logger.error("Exception thrown while fetching one entity.", e); } return null; }
@Override public Record get(final RecordId recordId) { checkOpen(); checkNotNull(recordId); OClass oClass = checkOrientClass(recordId.getTypeName()); ODocument document = db.load(recordIdObfuscator.decode(oClass, recordId.getValue())); if (document == null) { return null; } return getRecord(document); }
return null; if (nodeO instanceof ORecordId) nodeO = repositoryFactory.getDb().load((ORecordId) nodeO); if (nodeO == null) { logger.error("Invalid record in direction " + direction + ": " + document.toString());
private void syncIndex(final Map<ORID, EntityAdapter> changes) { IndexBatchRequest batch = new IndexBatchRequest(); try (ODatabaseDocumentTx db = componentDatabase.get().acquire()) { changes.forEach((rid, adapter) -> { ODocument document = db.load(rid); if (document != null) { EntityId componentId = findComponentId(document); if (componentId != null) { batch.update(findRepositoryName(document), componentId); } } else if (adapter instanceof ComponentEntityAdapter) { batch.delete(null, componentId(rid)); } }); } indexRequestProcessor.process(batch); }
private static volatile int schemaVersion = -1; private OPartitionedDatabasePool pool; protected void createPool() { pool = new OPartitionedDatabasePool(getUrl(), getUsername(), getPassword()); } @Override public synchronized ODatabaseDocumentTx openDatabase() { ODatabaseDocumentTx db = pool.acquire(); //DatabaseInfo is a simple class put in a static contect that holds the schema version. DatabaseInfo databaseInfo = CurrentDatabaseInfo.getDatabaseInfo(); ODocument document = db.load((ORID) databaseInfo.getId(), "schemaVersion:0", true); Integer version = document.field("schemaVersion"); if (schemaVersion == -1) { schemaVersion = version; } else if (schemaVersion < version) { db.getMetadata().getSchema().reload(); schemaVersion = version; pool.close(); createPool(); db = pool.acquire(); } return db; }
/** * helper to create or load document from entity * @param entity to create document from * @return created document */ protected ODocument createOrLoadDocument(T entity) { ODocument document; // load from db? if (entity.getId() != null) { initDb(); document = db.load(new ORecordId(entity.getId())); // correct class? if (document != null && !document.getClassName().equals(getModelClassName())) throw new RuntimeException("Record id " + entity.getId() + " should be of type " + getModelClassName() + ", but was of class " + document.getClassName() + "."); } else { // create new entry document = new ODocument(getModelClassName()); } return document; }
@Override public void deleteByRelationType(IRelationType relationType) { // delete source reference pointing to relation type OSQLSynchQuery<ODocument> query = new OSQLSynchQuery<>("select * from Relation where relationType = " + relationType.getId()); List<ODocument> result = db.command(query).execute(); // remove relation and link for (ODocument document : result) { ODocument relationLink = getRelationLink(document, true); if (relationLink != null && db.load(relationLink.getIdentity()) != null) relationLink.delete(); document.delete(); } }
if (referenceField instanceof ODocument) reference = (ODocument) referenceField; else if (referenceField instanceof ORecordId) { reference = repositoryFactory.getDb().load((ORecordId) referenceField); } else { logger.error("Invalid class type: " + referenceField.getClass().getName());
@Override public void deleteByRelation(INode node) { // delete source reference pointing to relation node OSQLSynchQuery<ODocument> query = new OSQLSynchQuery<>("select * from Relation where relationLink.in = " + node.getId() + " OR relationLink.out = " + node.getId()); List<ODocument> result = db.command(query).execute(); // remove relation and link for (ODocument document : result) { ODocument relationLink = getRelationLink(document, true); if (relationLink != null && db.load(relationLink.getIdentity()) != null) relationLink.delete(); document.delete(); } }