Search in sources :

Example 11 with ORecordId

use of com.orientechnologies.orient.core.id.ORecordId in project orientdb by orientechnologies.

the class ODatabaseImport method importClusters.

private long importClusters() throws ParseException, IOException {
    listener.onMessage("\nImporting clusters...");
    long total = 0;
    jsonReader.readNext(OJSONReader.BEGIN_COLLECTION);
    boolean recreateManualIndex = false;
    if (exporterVersion <= 4) {
        removeDefaultClusters();
        recreateManualIndex = true;
    }
    final Set<String> indexesToRebuild = new HashSet<String>();
    @SuppressWarnings("unused") ORecordId rid = null;
    while (jsonReader.lastChar() != ']') {
        jsonReader.readNext(OJSONReader.BEGIN_OBJECT);
        String name = jsonReader.readNext(OJSONReader.FIELD_ASSIGNMENT).checkContent("\"name\"").readString(OJSONReader.COMMA_SEPARATOR);
        if (name.length() == 0)
            name = null;
        name = OClassImpl.decodeClassName(name);
        if (name != null)
            // CHECK IF THE CLUSTER IS INCLUDED
            if (includeClusters != null) {
                if (!includeClusters.contains(name)) {
                    jsonReader.readNext(OJSONReader.NEXT_IN_ARRAY);
                    continue;
                }
            } else if (excludeClusters != null) {
                if (excludeClusters.contains(name)) {
                    jsonReader.readNext(OJSONReader.NEXT_IN_ARRAY);
                    continue;
                }
            }
        int id;
        if (exporterVersion < 9) {
            id = jsonReader.readNext(OJSONReader.FIELD_ASSIGNMENT).checkContent("\"id\"").readInteger(OJSONReader.COMMA_SEPARATOR);
            String type = jsonReader.readNext(OJSONReader.FIELD_ASSIGNMENT).checkContent("\"type\"").readString(OJSONReader.NEXT_IN_OBJECT);
        } else
            id = jsonReader.readNext(OJSONReader.FIELD_ASSIGNMENT).checkContent("\"id\"").readInteger(OJSONReader.NEXT_IN_OBJECT);
        String type;
        if (jsonReader.lastChar() == ',')
            type = jsonReader.readNext(OJSONReader.FIELD_ASSIGNMENT).checkContent("\"type\"").readString(OJSONReader.NEXT_IN_OBJECT);
        else
            type = "PHYSICAL";
        if (jsonReader.lastChar() == ',') {
            rid = new ORecordId(jsonReader.readNext(OJSONReader.FIELD_ASSIGNMENT).checkContent("\"rid\"").readString(OJSONReader.NEXT_IN_OBJECT));
        } else
            rid = null;
        listener.onMessage("\n- Creating cluster " + (name != null ? "'" + name + "'" : "NULL") + "...");
        int clusterId = name != null ? database.getClusterIdByName(name) : -1;
        if (clusterId == -1) {
            // CREATE IT
            if (!preserveClusterIDs)
                clusterId = database.addCluster(name);
            else {
                clusterId = database.addCluster(name, id, null);
                assert clusterId == id;
            }
        }
        if (clusterId != id) {
            if (!preserveClusterIDs) {
                if (database.countClusterElements(clusterId - 1) == 0) {
                    listener.onMessage("Found previous version: migrating old clusters...");
                    database.dropCluster(name, true);
                    database.addCluster("temp_" + clusterId, null);
                    clusterId = database.addCluster(name);
                } else
                    throw new OConfigurationException("Imported cluster '" + name + "' has id=" + clusterId + " different from the original: " + id + ". To continue the import drop the cluster '" + database.getClusterNameById(clusterId - 1) + "' that has " + database.countClusterElements(clusterId - 1) + " records");
            } else {
                database.dropCluster(clusterId, false);
                database.addCluster(name, id, null);
            }
        }
        if (name != null && !(name.equalsIgnoreCase(OMetadataDefault.CLUSTER_MANUAL_INDEX_NAME) || name.equalsIgnoreCase(OMetadataDefault.CLUSTER_INTERNAL_NAME) || name.equalsIgnoreCase(OMetadataDefault.CLUSTER_INDEX_NAME))) {
            if (!merge)
                database.command(new OCommandSQL("truncate cluster `" + name + "`")).execute();
            for (OIndex existingIndex : database.getMetadata().getIndexManager().getIndexes()) {
                if (existingIndex.getClusters().contains(name)) {
                    indexesToRebuild.add(existingIndex.getName());
                }
            }
        }
        listener.onMessage("OK, assigned id=" + clusterId);
        total++;
        jsonReader.readNext(OJSONReader.NEXT_IN_ARRAY);
    }
    jsonReader.readNext(OJSONReader.COMMA_SEPARATOR);
    listener.onMessage("\nRebuilding indexes of truncated clusters ...");
    for (final String indexName : indexesToRebuild) database.getMetadata().getIndexManager().getIndex(indexName).rebuild(new OProgressListener() {

        private long last = 0;

        @Override
        public void onBegin(Object iTask, long iTotal, Object metadata) {
            listener.onMessage("\n- Cluster content was updated: rebuilding index '" + indexName + "'...");
        }

        @Override
        public boolean onProgress(Object iTask, long iCounter, float iPercent) {
            final long now = System.currentTimeMillis();
            if (last == 0)
                last = now;
            else if (now - last > 1000) {
                listener.onMessage(String.format("\nIndex '%s' is rebuilding (%.2f/100)", indexName, iPercent));
                last = now;
            }
            return true;
        }

        @Override
        public void onCompletition(Object iTask, boolean iSucceed) {
            listener.onMessage(" Index " + indexName + " was successfully rebuilt.");
        }
    });
    listener.onMessage("\nDone " + indexesToRebuild.size() + " indexes were rebuilt.");
    if (recreateManualIndex) {
        database.addCluster(OMetadataDefault.CLUSTER_MANUAL_INDEX_NAME);
        database.getMetadata().getIndexManager().create();
        listener.onMessage("\nManual index cluster was recreated.");
    }
    listener.onMessage("\nDone. Imported " + total + " clusters");
    if (database.load(new ORecordId(database.getStorage().getConfiguration().indexMgrRecordId)) == null) {
        ODocument indexDocument = new ODocument();
        indexDocument.save(OMetadataDefault.CLUSTER_INTERNAL_NAME);
        database.getStorage().getConfiguration().indexMgrRecordId = indexDocument.getIdentity().toString();
        database.getStorage().getConfiguration().update();
    }
    return total;
}
Also used : ORecordId(com.orientechnologies.orient.core.id.ORecordId) OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) OProgressListener(com.orientechnologies.common.listener.OProgressListener) OConfigurationException(com.orientechnologies.orient.core.exception.OConfigurationException) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 12 with ORecordId

use of com.orientechnologies.orient.core.id.ORecordId in project orientdb by orientechnologies.

the class OStorageRemote method createRecord.

public OStorageOperationResult<OPhysicalPosition> createRecord(final ORecordId iRid, final byte[] iContent, final int iRecordVersion, final byte iRecordType, final int iMode, final ORecordCallback<Long> iCallback) {
    final OSBTreeCollectionManager collectionManager = ODatabaseRecordThreadLocal.INSTANCE.get().getSbTreeCollectionManager();
    ORecordCallback<OPhysicalPosition> realCallback = null;
    if (iCallback != null) {
        realCallback = new ORecordCallback<OPhysicalPosition>() {

            @Override
            public void call(ORecordId iRID, OPhysicalPosition iParameter) {
                iCallback.call(iRID, iParameter.clusterPosition);
            }
        };
    }
    final ORecordId idCopy = iRid.copy();
    // The Upper layer require to return this also if it not really receive response from the network
    final OPhysicalPosition ppos = new OPhysicalPosition(iRecordType);
    asyncNetworkOperation(new OStorageRemoteOperationWrite() {

        @Override
        public void execute(final OChannelBinaryAsynchClient network, final OStorageRemoteSession session, int mode) throws IOException {
            try {
                beginRequest(network, OChannelBinaryProtocol.REQUEST_RECORD_CREATE, session);
                network.writeShort((short) iRid.getClusterId());
                network.writeBytes(iContent);
                network.writeByte(iRecordType);
                network.writeByte((byte) mode);
            } finally {
                endRequest(network);
            }
        }
    }, new OStorageRemoteOperationRead<OPhysicalPosition>() {

        @Override
        public OPhysicalPosition execute(OChannelBinaryAsynchClient network, OStorageRemoteSession session) throws IOException {
            // SYNCHRONOUS
            try {
                beginResponse(network, session);
                // FIRST READ THE ENTIRE RESPONSE
                short clusterId = network.readShort();
                final long clPos = network.readLong();
                final int recVer = network.readVersion();
                final Map<OBonsaiCollectionPointer, OPair<Long, Long>> collectionChanges = readCollectionChanges(network);
                // APPLY CHANGES
                ppos.clusterPosition = clPos;
                ppos.recordVersion = recVer;
                // THIS IS A COMPATIBILITY FIX TO AVOID TO FILL THE CLUSTER ID IN CASE OF ASYNC
                if (iMode == 0) {
                    iRid.setClusterId(clusterId);
                    iRid.setClusterPosition(ppos.clusterPosition);
                }
                idCopy.setClusterId(clusterId);
                idCopy.setClusterPosition(ppos.clusterPosition);
                updateCollection(collectionChanges, collectionManager);
                return ppos;
            } finally {
                endResponse(network);
            }
        }
    }, iMode, idCopy, realCallback, "Error on create record in cluster " + iRid.getClusterId());
    return new OStorageOperationResult<OPhysicalPosition>(ppos);
}
Also used : OIOException(com.orientechnologies.common.io.OIOException) IOException(java.io.IOException) OChannelBinaryAsynchClient(com.orientechnologies.orient.client.binary.OChannelBinaryAsynchClient) ORecordId(com.orientechnologies.orient.core.id.ORecordId) OSBTreeCollectionManager(com.orientechnologies.orient.core.db.record.ridbag.sbtree.OSBTreeCollectionManager)

Example 13 with ORecordId

use of com.orientechnologies.orient.core.id.ORecordId in project orientdb by orientechnologies.

the class OStorageRemoteAsynchEventListener method onRequest.

public void onRequest(final byte iRequestCode, final Object obj) {
    //Using get status to avoid to check the session.
    if (storage.getStatus() == STATUS.CLOSED)
        return;
    if (iRequestCode == OChannelBinaryProtocol.REQUEST_PUSH_DISTRIB_CONFIG) {
        storage.updateClusterConfiguration(null, (byte[]) obj);
        if (OLogManager.instance().isDebugEnabled()) {
            synchronized (storage.getClusterConfiguration()) {
                OLogManager.instance().debug(this, "Received new cluster configuration: %s", storage.getClusterConfiguration().toJSON("prettyPrint"));
            }
        }
    } else if (iRequestCode == OChannelBinaryProtocol.REQUEST_PUSH_LIVE_QUERY) {
        byte[] bytes = (byte[]) obj;
        DataInputStream dis = new DataInputStream(new ByteArrayInputStream(bytes));
        Integer id = null;
        try {
            byte what = dis.readByte();
            if (what == 'r') {
                byte op = dis.readByte();
                id = dis.readInt();
                final ORecord record = Orient.instance().getRecordFactoryManager().newInstance(dis.readByte());
                final int version = readVersion(dis);
                final ORecordId rid = readRID(dis);
                final byte[] content = readBytes(dis);
                ORecordInternal.fill(record, rid, version, content, false);
                OLiveResultListener listener = liveQueryListeners.get(id);
                if (listener != null) {
                    listener.onLiveResult(id, new ORecordOperation(record, op));
                } else {
                    OLogManager.instance().warn(this, "Receiving invalid LiveQuery token: " + id);
                }
            } else if (what == 'u') {
                id = dis.readInt();
                OLiveResultListener listener = liveQueryListeners.get(id);
                listener.onUnsubscribe(id);
            }
        } catch (IOException e) {
            if (id != null) {
                OLiveResultListener listener = liveQueryListeners.get(id);
                if (listener != null) {
                    listener.onError(id);
                }
            }
            e.printStackTrace();
        }
    }
    byte op;
}
Also used : ORecordOperation(com.orientechnologies.orient.core.db.record.ORecordOperation) ByteArrayInputStream(java.io.ByteArrayInputStream) ORecord(com.orientechnologies.orient.core.record.ORecord) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) OLiveResultListener(com.orientechnologies.orient.core.sql.query.OLiveResultListener) ORecordId(com.orientechnologies.orient.core.id.ORecordId)

Example 14 with ORecordId

use of com.orientechnologies.orient.core.id.ORecordId in project orientdb by orientechnologies.

the class OStorageRemoteAsyncOperationTest method testSyncCall.

@Test
public void testSyncCall() {
    final CallStatus status = new CallStatus();
    storage.asyncNetworkOperation(new OStorageRemoteOperationWrite() {

        @Override
        public void execute(OChannelBinaryAsynchClient network, OStorageRemoteSession session, int mode) throws IOException {
            assertNull(status.status);
            status.status = "write";
        }
    }, new OStorageRemoteOperationRead<Object>() {

        @Override
        public Object execute(OChannelBinaryAsynchClient network, OStorageRemoteSession session) throws IOException {
            assertEquals(status.status, "write");
            status.status = "read";
            return null;
        }
    }, 0, new ORecordId(-1, -1), null, "");
    assertEquals(status.status, "read");
}
Also used : IOException(java.io.IOException) OChannelBinaryAsynchClient(com.orientechnologies.orient.client.binary.OChannelBinaryAsynchClient) ORecordId(com.orientechnologies.orient.core.id.ORecordId) Test(org.junit.Test)

Example 15 with ORecordId

use of com.orientechnologies.orient.core.id.ORecordId in project orientdb by orientechnologies.

the class OStorageRemoteAsyncOperationTest method testAsyncRead.

@Test
public void testAsyncRead() throws InterruptedException {
    final CallStatus status = new CallStatus();
    final CountDownLatch callBackWait = new CountDownLatch(1);
    final CountDownLatch readDone = new CountDownLatch(1);
    final CountDownLatch callBackDone = new CountDownLatch(1);
    final Object res = new Object();
    storage.asyncNetworkOperation(new OStorageRemoteOperationWrite() {

        @Override
        public void execute(OChannelBinaryAsynchClient network, OStorageRemoteSession session, int mode) throws IOException {
            assertNull(status.status);
            status.status = "write";
        }
    }, new OStorageRemoteOperationRead<Object>() {

        @Override
        public Object execute(OChannelBinaryAsynchClient network, OStorageRemoteSession session) throws IOException {
            try {
                if (callBackWait.await(10, TimeUnit.MILLISECONDS))
                    readDone.countDown();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return res;
        }
    }, 1, new ORecordId(-1, -1), new ORecordCallback<Object>() {

        @Override
        public void call(ORecordId iRID, Object iParameter) {
            callBackDone.countDown();
        }
    }, "");
    // SBLCK THE CALLBAC THAT SHOULD BE IN ANOTHER THREAD
    callBackWait.countDown();
    boolean called = readDone.await(10, TimeUnit.MILLISECONDS);
    if (!called)
        fail("Read not called");
    called = callBackDone.await(10, TimeUnit.MILLISECONDS);
    if (!called)
        fail("Callback not called");
}
Also used : IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) OChannelBinaryAsynchClient(com.orientechnologies.orient.client.binary.OChannelBinaryAsynchClient) ORecordId(com.orientechnologies.orient.core.id.ORecordId) Test(org.junit.Test)

Aggregations

ORecordId (com.orientechnologies.orient.core.id.ORecordId)431 Test (org.testng.annotations.Test)153 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)139 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)120 ORID (com.orientechnologies.orient.core.id.ORID)71 HashSet (java.util.HashSet)63 OIndexCursor (com.orientechnologies.orient.core.index.OIndexCursor)42 ORidBag (com.orientechnologies.orient.core.db.record.ridbag.ORidBag)37 ORecord (com.orientechnologies.orient.core.record.ORecord)37 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)34 OIndexTxAwareMultiValue (com.orientechnologies.orient.core.index.OIndexTxAwareMultiValue)30 OIndexTxAwareOneValue (com.orientechnologies.orient.core.index.OIndexTxAwareOneValue)30 HashMap (java.util.HashMap)29 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)28 IOException (java.io.IOException)25 Child (com.orientechnologies.orient.test.domain.business.Child)24 OException (com.orientechnologies.common.exception.OException)23 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)23 Map (java.util.Map)22 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)21