Search in sources :

Example 21 with ORecordDuplicatedException

use of com.orientechnologies.orient.core.storage.ORecordDuplicatedException in project orientdb by orientechnologies.

the class OOrientDBLoader method load.

@Override
public void load(OETLPipeline pipeline, final Object input, OCommandContext context) {
    if (input == null)
        return;
    if (dbAutoCreateProperties) {
        autoCreateProperties(pipeline, input);
    }
    if (tx && dbType == DOCUMENT) {
        final ODatabaseDocument documentDatabase = pipeline.getDocumentDatabase();
        if (!documentDatabase.getTransaction().isActive()) {
            documentDatabase.begin();
            documentDatabase.getTransaction().setUsingLog(txUseLog);
        }
    }
    if (input instanceof OrientVertex) {
        final OrientVertex v = (OrientVertex) input;
        try {
            v.save(clusterName);
        } catch (ORecordDuplicatedException e) {
            if (skipDuplicates) {
            } else {
                throw e;
            }
        } finally {
        }
    } else if (input instanceof ODocument) {
        final ODocument doc = (ODocument) input;
        if (className != null) {
            doc.setClassName(className);
        }
        if (clusterName != null) {
            doc.save(clusterName);
        } else {
            doc.save();
        }
    }
    progress.incrementAndGet();
    // DO BATCH COMMIT
    if (batchCommitSize > 0 && batchCounter.get() > batchCommitSize) {
        if (dbType == DOCUMENT) {
            final ODatabaseDocument documentDatabase = pipeline.getDocumentDatabase();
            log(DEBUG, "committing batch");
            documentDatabase.commit();
            documentDatabase.begin();
            documentDatabase.getTransaction().setUsingLog(txUseLog);
        } else {
            log(DEBUG, "committing batch");
            pipeline.getGraphDatabase().commit();
        }
        batchCounter.set(0);
    } else {
        batchCounter.incrementAndGet();
    }
}
Also used : ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) ORecordDuplicatedException(com.orientechnologies.orient.core.storage.ORecordDuplicatedException) OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 22 with ORecordDuplicatedException

use of com.orientechnologies.orient.core.storage.ORecordDuplicatedException in project orientdb by orientechnologies.

the class OEdgeTransformer method createEdge.

private List<OrientEdge> createEdge(final OrientVertex vertex, final Object joinCurrentValue, Object result) {
    log(OETLProcessor.LOG_LEVELS.DEBUG, "joinCurrentValue=%s, lookupResult=%s", joinCurrentValue, result);
    if (result == null) {
        // APPLY THE STRATEGY DEFINED IN unresolvedLinkAction
        switch(unresolvedLinkAction) {
            case CREATE:
                // Don't try to create a Vertex with a null value
                if (joinCurrentValue != null) {
                    if (lookup != null) {
                        final String[] lookupParts = lookup.split("\\.");
                        final OrientVertex linkedV = pipeline.getGraphDatabase().addTemporaryVertex(lookupParts[0]);
                        linkedV.setProperty(lookupParts[1], joinCurrentValue);
                        if (targetVertexFields != null) {
                            for (String f : targetVertexFields.fieldNames()) linkedV.setProperty(f, resolve(targetVertexFields.field(f)));
                        }
                        linkedV.save();
                        log(OETLProcessor.LOG_LEVELS.DEBUG, "created new vertex=%s", linkedV.getRecord());
                        result = linkedV.getIdentity();
                    } else {
                        throw new OConfigurationException("Cannot create linked document because target class is unknown. Use 'lookup' field");
                    }
                }
                break;
            case ERROR:
                processor.getStats().incrementErrors();
                log(OETLProcessor.LOG_LEVELS.ERROR, "%s: ERROR Cannot resolve join for value '%s'", getName(), joinCurrentValue);
                break;
            case WARNING:
                processor.getStats().incrementWarnings();
                log(OETLProcessor.LOG_LEVELS.INFO, "%s: WARN Cannot resolve join for value '%s'", getName(), joinCurrentValue);
                break;
            case SKIP:
                return null;
            case HALT:
                throw new OETLProcessHaltedException("Cannot resolve join for value '" + joinCurrentValue + "'");
            case NOTHING:
            default:
                return null;
        }
    }
    if (result != null) {
        final List<OrientEdge> edges;
        if (OMultiValue.isMultiValue(result)) {
            final int size = OMultiValue.getSize(result);
            if (size == 0)
                // NO EDGES
                return null;
            edges = new ArrayList<OrientEdge>(size);
        } else
            edges = new ArrayList<OrientEdge>(1);
        for (Object o : OMultiValue.getMultiValueIterable(result)) {
            OIdentifiable oid = (OIdentifiable) o;
            final OrientVertex targetVertex = pipeline.getGraphDatabase().getVertex(oid);
            try {
                // CREATE THE EDGE
                final OrientEdge edge;
                if (directionOut)
                    edge = (OrientEdge) vertex.addEdge(edgeClass, targetVertex);
                else
                    edge = (OrientEdge) targetVertex.addEdge(edgeClass, vertex);
                if (edgeFields != null) {
                    for (String f : edgeFields.fieldNames()) edge.setProperty(f, resolve(edgeFields.field(f)));
                }
                edges.add(edge);
                log(OETLProcessor.LOG_LEVELS.DEBUG, "created new edge=%s", edge);
            } catch (ORecordDuplicatedException e) {
                if (skipDuplicates) {
                    log(OETLProcessor.LOG_LEVELS.DEBUG, "skipped creation of new edge because already exists");
                    continue;
                } else {
                    log(OETLProcessor.LOG_LEVELS.ERROR, "error on creation of new edge because it already exists (skipDuplicates=false)");
                    throw e;
                }
            }
        }
        return edges;
    }
    // NO EDGES
    return null;
}
Also used : ArrayList(java.util.ArrayList) OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) OrientEdge(com.tinkerpop.blueprints.impls.orient.OrientEdge) OETLProcessHaltedException(com.orientechnologies.orient.etl.OETLProcessHaltedException) OConfigurationException(com.orientechnologies.orient.core.exception.OConfigurationException) ORecordDuplicatedException(com.orientechnologies.orient.core.storage.ORecordDuplicatedException)

Example 23 with ORecordDuplicatedException

use of com.orientechnologies.orient.core.storage.ORecordDuplicatedException in project orientdb by orientechnologies.

the class IndexTest method testTransactionUniqueIndexTestOne.

public void testTransactionUniqueIndexTestOne() {
    ODatabaseDocumentTx db = new ODatabaseDocumentTx(database.getURL());
    db.open("admin", "admin");
    if (!db.getMetadata().getSchema().existsClass("TransactionUniqueIndexTest")) {
        final OClass termClass = db.getMetadata().getSchema().createClass("TransactionUniqueIndexTest", 1, null);
        termClass.createProperty("label", OType.STRING);
        termClass.createIndex("idxTransactionUniqueIndexTest", INDEX_TYPE.UNIQUE.toString(), null, new ODocument().fields("ignoreNullValues", true), new String[] { "label" });
        db.getMetadata().getSchema().save();
    }
    ODocument docOne = new ODocument("TransactionUniqueIndexTest");
    docOne.field("label", "A");
    docOne.save();
    final List<ODocument> resultBeforeCommit = db.query(new OSQLSynchQuery<ODocument>("select from index:idxTransactionUniqueIndexTest"));
    Assert.assertEquals(resultBeforeCommit.size(), 1);
    db.begin();
    try {
        ODocument docTwo = new ODocument("TransactionUniqueIndexTest");
        docTwo.field("label", "A");
        docTwo.save();
        db.commit();
        Assert.fail();
    } catch (ORecordDuplicatedException oie) {
    }
    final List<ODocument> resultAfterCommit = db.query(new OSQLSynchQuery<ODocument>("select from index:idxTransactionUniqueIndexTest"));
    Assert.assertEquals(resultAfterCommit.size(), 1);
}
Also used : ORecordDuplicatedException(com.orientechnologies.orient.core.storage.ORecordDuplicatedException) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 24 with ORecordDuplicatedException

use of com.orientechnologies.orient.core.storage.ORecordDuplicatedException in project orientdb by orientechnologies.

the class OClassIndexManager method onRecordAfterUpdate.

@Override
public void onRecordAfterUpdate(ODocument iDocument) {
    iDocument = checkForLoading(iDocument);
    final OClass cls = ODocumentInternal.getImmutableSchemaClass(iDocument);
    if (cls == null)
        return;
    final Collection<OIndex<?>> indexes = cls.getIndexes();
    if (!indexes.isEmpty()) {
        final Set<String> dirtyFields = new HashSet<String>(Arrays.asList(iDocument.getDirtyFields()));
        if (!dirtyFields.isEmpty()) {
            for (final OIndex<?> index : indexes) {
                try {
                    processIndexUpdate(iDocument, dirtyFields, index);
                } catch (ORecordDuplicatedException ex) {
                    iDocument.undo();
                    iDocument.setDirty();
                    database.save(iDocument);
                    throw ex;
                }
            }
        }
    }
}
Also used : ORecordDuplicatedException(com.orientechnologies.orient.core.storage.ORecordDuplicatedException) OClass(com.orientechnologies.orient.core.metadata.schema.OClass)

Example 25 with ORecordDuplicatedException

use of com.orientechnologies.orient.core.storage.ORecordDuplicatedException in project orientdb by orientechnologies.

the class OIndexTxAwareOneValue method checkEntry.

@Override
public ODocument checkEntry(final OIdentifiable iRecord, Object iKey) {
    iKey = getCollatingValue(iKey);
    // CHECK IF ALREADY EXISTS IN TX
    if (!database.getTransaction().isActive()) {
        final OIdentifiable previousRecord = get(iKey);
        if (previousRecord != null && !previousRecord.equals(iRecord)) {
            final ODocument metadata = getMetadata();
            Boolean mergeKeys = false;
            if (metadata != null) {
                mergeKeys = metadata.field(OIndex.MERGE_KEYS);
            }
            final boolean mergeSameKey = mergeKeys != null && mergeKeys;
            if (mergeSameKey) {
                return (ODocument) previousRecord.getRecord();
            } else
                throw new ORecordDuplicatedException(String.format("Cannot index record %s: found duplicated key '%s' in index '%s' previously assigned to the record %s", iRecord, iKey, getName(), previousRecord), getName(), previousRecord.getIdentity());
        }
        return super.checkEntry(iRecord, iKey);
    }
    return null;
}
Also used : ORecordDuplicatedException(com.orientechnologies.orient.core.storage.ORecordDuplicatedException) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Aggregations

ORecordDuplicatedException (com.orientechnologies.orient.core.storage.ORecordDuplicatedException)33 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)19 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)17 Test (org.junit.Test)10 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)8 OCommandScript (com.orientechnologies.orient.core.command.script.OCommandScript)6 ORecordNotFoundException (com.orientechnologies.orient.core.exception.ORecordNotFoundException)5 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)5 Vertex (com.tinkerpop.blueprints.Vertex)5 ONeedRetryException (com.orientechnologies.common.concur.ONeedRetryException)4 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)4 OrientGraph (com.tinkerpop.blueprints.impls.orient.OrientGraph)4 OrientVertex (com.tinkerpop.blueprints.impls.orient.OrientVertex)4 ArrayList (java.util.ArrayList)4 Test (org.testng.annotations.Test)4 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)3 DatabaseAbstractTest (com.orientechnologies.DatabaseAbstractTest)2 OTransactionException (com.orientechnologies.orient.core.exception.OTransactionException)2 ORecordId (com.orientechnologies.orient.core.id.ORecordId)2 OrientTest (com.orientechnologies.orient.test.database.base.OrientTest)2