Search in sources :

Example 16 with OIdentifiable

use of com.orientechnologies.orient.core.db.record.OIdentifiable in project orientdb by orientechnologies.

the class ODatabaseImport method importManualIndexes.

private void importManualIndexes() throws IOException, ParseException {
    listener.onMessage("\nImporting manual index entries...");
    ODocument doc = new ODocument();
    OIndexManagerProxy indexManager = database.getMetadata().getIndexManager();
    // FORCE RELOADING
    indexManager.reload();
    int n = 0;
    do {
        jsonReader.readNext(OJSONReader.BEGIN_OBJECT);
        jsonReader.readString(OJSONReader.FIELD_ASSIGNMENT);
        final String indexName = jsonReader.readString(OJSONReader.NEXT_IN_ARRAY);
        if (indexName == null || indexName.length() == 0)
            return;
        listener.onMessage("\n- Index '" + indexName + "'...");
        final OIndex<?> index = database.getMetadata().getIndexManager().getIndex(indexName);
        long tot = 0;
        jsonReader.readNext(OJSONReader.BEGIN_COLLECTION);
        do {
            final String value = jsonReader.readString(OJSONReader.NEXT_IN_ARRAY).trim();
            if (!value.isEmpty() && !indexName.equalsIgnoreCase(EXPORT_IMPORT_MAP_NAME)) {
                doc = (ODocument) ORecordSerializerJSON.INSTANCE.fromString(value, doc, null);
                doc.setLazyLoad(false);
                final OIdentifiable oldRid = doc.<OIdentifiable>field("rid");
                final OIdentifiable newRid;
                if (!doc.<Boolean>field("binary")) {
                    if (exportImportHashTable != null)
                        newRid = exportImportHashTable.get(oldRid);
                    else
                        newRid = oldRid;
                    index.put(doc.field("key"), newRid != null ? newRid.getIdentity() : oldRid.getIdentity());
                } else {
                    ORuntimeKeyIndexDefinition<?> runtimeKeyIndexDefinition = (ORuntimeKeyIndexDefinition<?>) index.getDefinition();
                    OBinarySerializer<?> binarySerializer = runtimeKeyIndexDefinition.getSerializer();
                    if (exportImportHashTable != null)
                        newRid = exportImportHashTable.get(doc.<OIdentifiable>field("rid")).getIdentity();
                    else
                        newRid = doc.<OIdentifiable>field("rid");
                    index.put(binarySerializer.deserialize(doc.<byte[]>field("key"), 0), newRid != null ? newRid : oldRid);
                }
                tot++;
            }
        } while (jsonReader.lastChar() == ',');
        if (index != null) {
            listener.onMessage("OK (" + tot + " entries)");
            n++;
        } else
            listener.onMessage("ERR, the index wasn't found in configuration");
        jsonReader.readNext(OJSONReader.END_OBJECT);
        jsonReader.readNext(OJSONReader.NEXT_IN_ARRAY);
    } while (jsonReader.lastChar() == ',');
    listener.onMessage("\nDone. Imported " + String.format("%,d", n) + " indexes.");
    jsonReader.readNext(OJSONReader.NEXT_IN_OBJECT);
}
Also used : OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 17 with OIdentifiable

use of com.orientechnologies.orient.core.db.record.OIdentifiable in project orientdb by orientechnologies.

the class ODatabaseImport method importRecords.

private long importRecords() throws Exception {
    long total = 0;
    database.getMetadata().getIndexManager().dropIndex(EXPORT_IMPORT_MAP_NAME);
    OIndexFactory factory = OIndexes.getFactory(OClass.INDEX_TYPE.DICTIONARY_HASH_INDEX.toString(), OHashIndexFactory.HASH_INDEX_ALGORITHM);
    exportImportHashTable = (OIndex<OIdentifiable>) database.getMetadata().getIndexManager().createIndex(EXPORT_IMPORT_MAP_NAME, OClass.INDEX_TYPE.DICTIONARY_HASH_INDEX.toString(), new OSimpleKeyIndexDefinition(factory.getLastVersion(), OType.LINK), null, null, null);
    jsonReader.readNext(OJSONReader.BEGIN_COLLECTION);
    long totalRecords = 0;
    listener.onMessage("\n\nImporting records...");
    ORID rid;
    ORID lastRid = new ORecordId();
    final long begin = System.currentTimeMillis();
    long lastLapRecords = 0;
    long last = begin;
    Set<String> involvedClusters = new HashSet<String>();
    while (jsonReader.lastChar() != ']') {
        rid = importRecord();
        if (rid != null) {
            ++lastLapRecords;
            ++totalRecords;
            if (rid.getClusterId() != lastRid.getClusterId() || involvedClusters.isEmpty())
                involvedClusters.add(database.getClusterNameById(rid.getClusterId()));
            final long now = System.currentTimeMillis();
            if (now - last > IMPORT_RECORD_DUMP_LAP_EVERY_MS) {
                final List<String> sortedClusters = new ArrayList<String>(involvedClusters);
                Collections.sort(sortedClusters);
                listener.onMessage(String.format("\n- Imported %,d records into clusters: %s. Total records imported so far: %,d (%,.2f/sec)", lastLapRecords, sortedClusters, totalRecords, (float) lastLapRecords * 1000 / (float) IMPORT_RECORD_DUMP_LAP_EVERY_MS));
                // RESET LAP COUNTERS
                last = now;
                lastLapRecords = 0;
                involvedClusters.clear();
            }
            lastRid = rid;
        }
        record = null;
    }
    if (migrateLinks)
        migrateLinksInImportedDocuments();
    listener.onMessage(String.format("\n\nDone. Imported %,d records in %,.2f secs\n", totalRecords, ((float) (System.currentTimeMillis() - begin)) / 1000));
    jsonReader.readNext(OJSONReader.COMMA_SEPARATOR);
    return total;
}
Also used : ORID(com.orientechnologies.orient.core.id.ORID) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ORecordId(com.orientechnologies.orient.core.id.ORecordId)

Example 18 with OIdentifiable

use of com.orientechnologies.orient.core.db.record.OIdentifiable in project orientdb by orientechnologies.

the class OIndexCursorSingleValue method nextEntry.

@Override
public Map.Entry<Object, OIdentifiable> nextEntry() {
    if (identifiable == null)
        return null;
    final OIdentifiable value = identifiable;
    identifiable = null;
    return new Map.Entry<Object, OIdentifiable>() {

        @Override
        public Object getKey() {
            return key;
        }

        @Override
        public OIdentifiable getValue() {
            return value;
        }

        @Override
        public OIdentifiable setValue(OIdentifiable value) {
            throw new UnsupportedOperationException("setValue");
        }
    };
}
Also used : OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable)

Example 19 with OIdentifiable

use of com.orientechnologies.orient.core.db.record.OIdentifiable in project orientdb by orientechnologies.

the class OIndexCursorSingleValue method next.

@Override
public OIdentifiable next() {
    if (identifiable == null)
        throw new NoSuchElementException();
    final OIdentifiable value = identifiable;
    identifiable = null;
    return value;
}
Also used : OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) NoSuchElementException(java.util.NoSuchElementException)

Example 20 with OIdentifiable

use of com.orientechnologies.orient.core.db.record.OIdentifiable in project orientdb by orientechnologies.

the class OIndexFullText method remove.

/**
   * Splits passed in key on several words and remove records with keys equals to any item of split result and values equals to
   * passed in value.
   *
   * @param key   Key to remove.
   * @param value Value to remove.
   *
   * @return <code>true</code> if at least one record is removed.
   */
@Override
public boolean remove(Object key, final OIdentifiable value) {
    if (key == null)
        return false;
    key = getCollatingValue(key);
    final ODatabase database = getDatabase();
    final boolean txIsActive = database.getTransaction().isActive();
    if (!txIsActive)
        keyLockManager.acquireExclusiveLock(key);
    try {
        final Set<String> words = splitIntoWords(key.toString());
        final OModifiableBoolean removed = new OModifiableBoolean(false);
        for (final String word : words) {
            acquireSharedLock();
            try {
                Set<OIdentifiable> recs;
                while (true) {
                    try {
                        recs = (Set<OIdentifiable>) storage.getIndexValue(indexId, word);
                        break;
                    } catch (OInvalidIndexEngineIdException e) {
                        doReloadIndexEngine();
                    }
                }
                if (recs != null && !recs.isEmpty()) {
                    while (true) {
                        try {
                            storage.updateIndexEntry(indexId, word, new EntityRemover(recs, value, removed));
                            break;
                        } catch (OInvalidIndexEngineIdException e) {
                            doReloadIndexEngine();
                        }
                    }
                }
            } finally {
                releaseSharedLock();
            }
        }
        return removed.getValue();
    } finally {
        if (!txIsActive)
            keyLockManager.releaseExclusiveLock(key);
    }
}
Also used : OInvalidIndexEngineIdException(com.orientechnologies.orient.core.exception.OInvalidIndexEngineIdException) ODatabase(com.orientechnologies.orient.core.db.ODatabase) OModifiableBoolean(com.orientechnologies.common.types.OModifiableBoolean) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable)

Aggregations

OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)536 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)278 ORecordId (com.orientechnologies.orient.core.id.ORecordId)120 Test (org.testng.annotations.Test)104 HashSet (java.util.HashSet)89 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)79 ORidBag (com.orientechnologies.orient.core.db.record.ridbag.ORidBag)70 ORID (com.orientechnologies.orient.core.id.ORID)56 OIndexCursor (com.orientechnologies.orient.core.index.OIndexCursor)47 Test (org.junit.Test)43 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)42 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)41 ArrayList (java.util.ArrayList)39 ORecord (com.orientechnologies.orient.core.record.ORecord)35 Map (java.util.Map)31 ByteBuffer (java.nio.ByteBuffer)28 OrientVertex (com.tinkerpop.blueprints.impls.orient.OrientVertex)26 OIndexTxAwareOneValue (com.orientechnologies.orient.core.index.OIndexTxAwareOneValue)22 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)22 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)21