Search in sources :

Example 1 with ORecordDuplicatedException

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

the class IndexTest method testTransactionUniqueIndexTestWithDotNameOne.

public void testTransactionUniqueIndexTestWithDotNameOne() {
    ODatabaseDocumentTx db = new ODatabaseDocumentTx(database.getURL());
    db.open("admin", "admin");
    if (!db.getMetadata().getSchema().existsClass("TransactionUniqueIndexWithDotTest")) {
        final OClass termClass = db.getMetadata().getSchema().createClass("TransactionUniqueIndexWithDotTest", 1, null);
        termClass.createProperty("label", OType.STRING).createIndex(INDEX_TYPE.UNIQUE);
        db.getMetadata().getSchema().save();
    }
    ODocument docOne = new ODocument("TransactionUniqueIndexWithDotTest");
    docOne.field("label", "A");
    docOne.save();
    final List<ODocument> resultBeforeCommit = db.query(new OSQLSynchQuery<ODocument>("select from  index:TransactionUniqueIndexWithDotTest.label"));
    Assert.assertEquals(resultBeforeCommit.size(), 1);
    long countClassBefore = db.countClass("TransactionUniqueIndexWithDotTest");
    db.begin();
    try {
        ODocument docTwo = new ODocument("TransactionUniqueIndexWithDotTest");
        docTwo.field("label", "A");
        docTwo.save();
        db.commit();
        Assert.fail();
    } catch (ORecordDuplicatedException oie) {
    }
    Assert.assertEquals(((List<ODocument>) db.command(new OCommandSQL("select from TransactionUniqueIndexWithDotTest")).execute()).size(), countClassBefore);
    final List<ODocument> resultAfterCommit = db.query(new OSQLSynchQuery<ODocument>("select from  index:TransactionUniqueIndexWithDotTest.label"));
    Assert.assertEquals(resultAfterCommit.size(), 1);
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) 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 2 with ORecordDuplicatedException

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

the class IndexTest method testTransactionUniqueIndexTestTwo.

@Test(dependsOnMethods = "testTransactionUniqueIndexTestOne")
public void testTransactionUniqueIndexTestTwo() {
    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();
    }
    final List<ODocument> resultBeforeCommit = db.query(new OSQLSynchQuery<ODocument>("select from index:idxTransactionUniqueIndexTest"));
    Assert.assertEquals(resultBeforeCommit.size(), 1);
    db.begin();
    try {
        ODocument docOne = new ODocument("TransactionUniqueIndexTest");
        docOne.field("label", "B");
        docOne.save();
        ODocument docTwo = new ODocument("TransactionUniqueIndexTest");
        docTwo.field("label", "B");
        docTwo.save();
        db.commit();
        Assert.fail();
    } catch (ORecordDuplicatedException oie) {
        db.rollback();
    }
    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) Test(org.testng.annotations.Test) DatabaseAbstractTest(com.orientechnologies.DatabaseAbstractTest) OrientTest(com.orientechnologies.orient.test.database.base.OrientTest)

Example 3 with ORecordDuplicatedException

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

the class IndexTest method testTransactionUniqueIndexTestWithDotNameTwo.

@Test(dependsOnMethods = "testTransactionUniqueIndexTestWithDotNameOne")
public void testTransactionUniqueIndexTestWithDotNameTwo() {
    ODatabaseDocumentTx db = new ODatabaseDocumentTx(database.getURL());
    db.open("admin", "admin");
    if (!db.getMetadata().getSchema().existsClass("TransactionUniqueIndexWithDotTest")) {
        final OClass termClass = db.getMetadata().getSchema().createClass("TransactionUniqueIndexWithDotTest", 1, null);
        termClass.createProperty("label", OType.STRING).createIndex(INDEX_TYPE.UNIQUE);
        db.getMetadata().getSchema().save();
    }
    final List<ODocument> resultBeforeCommit = db.query(new OSQLSynchQuery<ODocument>("select from index:TransactionUniqueIndexWithDotTest.label"));
    Assert.assertEquals(resultBeforeCommit.size(), 1);
    db.begin();
    try {
        ODocument docOne = new ODocument("TransactionUniqueIndexWithDotTest");
        docOne.field("label", "B");
        docOne.save();
        ODocument docTwo = new ODocument("TransactionUniqueIndexWithDotTest");
        docTwo.field("label", "B");
        docTwo.save();
        db.commit();
        Assert.fail();
    } catch (ORecordDuplicatedException oie) {
        db.rollback();
    }
    final List<ODocument> resultAfterCommit = db.query(new OSQLSynchQuery<ODocument>("select from  index:TransactionUniqueIndexWithDotTest.label"));
    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) Test(org.testng.annotations.Test) DatabaseAbstractTest(com.orientechnologies.DatabaseAbstractTest) OrientTest(com.orientechnologies.orient.test.database.base.OrientTest)

Example 4 with ORecordDuplicatedException

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

the class IndexTest method testDuplicatedIndexOnUnique.

public void testDuplicatedIndexOnUnique() {
    Profile jayMiner = new Profile("Jay", "Jay", "Miner", null);
    database.save(jayMiner);
    Profile jacobMiner = new Profile("Jay", "Jacob", "Miner", null);
    try {
        database.save(jacobMiner);
        // IT SHOULD GIVE ERROR ON DUPLICATED KEY
        Assert.assertTrue(false);
    } catch (ORecordDuplicatedException e) {
        Assert.assertTrue(true);
    }
}
Also used : ORecordDuplicatedException(com.orientechnologies.orient.core.storage.ORecordDuplicatedException) Profile(com.orientechnologies.orient.test.domain.whiz.Profile)

Example 5 with ORecordDuplicatedException

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

the class OIndexOneValue method checkEntry.

@Override
public ODocument checkEntry(final OIdentifiable record, Object key) {
    key = getCollatingValue(key);
    final ODatabase database = getDatabase();
    final boolean txIsActive = database.getTransaction().isActive();
    if (!txIsActive)
        keyLockManager.acquireSharedLock(key);
    try {
        // CHECK IF ALREADY EXIST
        final OIdentifiable indexedRID = get(key);
        if (indexedRID != null && !indexedRID.getIdentity().equals(record.getIdentity())) {
            final Boolean mergeSameKey = metadata != null && (Boolean) metadata.field(OIndex.MERGE_KEYS);
            if (mergeSameKey != null && mergeSameKey)
                return (ODocument) indexedRID.getRecord();
            else
                throw new ORecordDuplicatedException(String.format("Cannot index record %s: found duplicated key '%s' in index '%s' previously assigned to the record %s", record, key, getName(), indexedRID), getName(), indexedRID.getIdentity());
        }
        return null;
    } finally {
        if (!txIsActive)
            keyLockManager.releaseSharedLock(key);
    }
}
Also used : ORecordDuplicatedException(com.orientechnologies.orient.core.storage.ORecordDuplicatedException) ODatabase(com.orientechnologies.orient.core.db.ODatabase) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable)

Aggregations

ORecordDuplicatedException (com.orientechnologies.orient.core.storage.ORecordDuplicatedException)34 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)20 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)18 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 Test (org.testng.annotations.Test)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 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 OSchema (com.orientechnologies.orient.core.metadata.schema.OSchema)2