Search in sources :

Example 81 with ORecordId

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

the class CRUDObjectPhysicalTestSchemaFull method mapObjectsLinkTest.

@Test(dependsOnMethods = "mapEnumAndInternalObjects")
public void mapObjectsLinkTest() {
    JavaComplexTestClass p = database.newInstance(JavaComplexTestClass.class);
    p.setName("Silvester");
    Child c = database.newInstance(Child.class);
    c.setName("John");
    Child c1 = database.newInstance(Child.class);
    c1.setName("Jack");
    Child c2 = database.newInstance(Child.class);
    c2.setName("Bob");
    Child c3 = database.newInstance(Child.class);
    c3.setName("Sam");
    Child c4 = database.newInstance(Child.class);
    c4.setName("Dean");
    p.getList().add(c1);
    p.getList().add(c2);
    p.getList().add(c3);
    p.getList().add(c4);
    p.getChildren().put("first", c);
    p.getEnumList().add(EnumTest.ENUM1);
    p.getEnumList().add(EnumTest.ENUM2);
    p.getEnumSet().add(EnumTest.ENUM1);
    p.getEnumSet().add(EnumTest.ENUM3);
    p.getEnumMap().put("1", EnumTest.ENUM2);
    p.getEnumMap().put("2", EnumTest.ENUM3);
    database.save(p);
    List<Child> cresult = database.query(new OSQLSynchQuery<Child>("select * from Child"));
    Assert.assertTrue(cresult.size() > 0);
    ORID rid = new ORecordId(p.getId());
    database.close();
    database = OObjectDatabasePool.global().acquire(url, "admin", "admin");
    JavaComplexTestClass loaded = database.load(rid);
    Assert.assertEquals(loaded.getList().size(), 4);
    Assert.assertTrue(loaded.getList().get(0) instanceof Child);
    Assert.assertTrue(loaded.getList().get(1) instanceof Child);
    Assert.assertTrue(loaded.getList().get(2) instanceof Child);
    Assert.assertTrue(loaded.getList().get(3) instanceof Child);
    Assert.assertEquals(loaded.getList().get(0).getName(), "Jack");
    Assert.assertEquals(loaded.getList().get(1).getName(), "Bob");
    Assert.assertEquals(loaded.getList().get(2).getName(), "Sam");
    Assert.assertEquals(loaded.getList().get(3).getName(), "Dean");
    Assert.assertEquals(loaded.getEnumList().size(), 2);
    Assert.assertEquals(loaded.getEnumList().get(0), EnumTest.ENUM1);
    Assert.assertEquals(loaded.getEnumList().get(1), EnumTest.ENUM2);
    Assert.assertEquals(loaded.getEnumSet().size(), 2);
    Iterator<EnumTest> it = loaded.getEnumSet().iterator();
    Assert.assertEquals(it.next(), EnumTest.ENUM1);
    Assert.assertEquals(it.next(), EnumTest.ENUM3);
    Assert.assertEquals(loaded.getEnumMap().size(), 2);
    Assert.assertEquals(loaded.getEnumMap().get("1"), EnumTest.ENUM2);
    Assert.assertEquals(loaded.getEnumMap().get("2"), EnumTest.ENUM3);
}
Also used : ORID(com.orientechnologies.orient.core.id.ORID) Child(com.orientechnologies.orient.test.domain.business.Child) ORecordId(com.orientechnologies.orient.core.id.ORecordId) Test(org.testng.annotations.Test)

Example 82 with ORecordId

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

the class CollectionIndexTest method testIndexCollectionUpdateAddItemInTxRollback.

public void testIndexCollectionUpdateAddItemInTxRollback() throws Exception {
    Collector collector = new Collector();
    collector.setStringCollection(new ArrayList<String>(Arrays.asList("spam", "eggs")));
    collector = database.save(collector);
    database.begin();
    Collector loadedCollector = (Collector) database.load(new ORecordId(collector.getId()));
    loadedCollector.getStringCollection().add("cookies");
    loadedCollector = database.save(loadedCollector);
    database.rollback();
    List<ODocument> result = database.command(new OCommandSQL("select key, rid from index:Collector.stringCollection")).execute();
    Assert.assertNotNull(result);
    Assert.assertEquals(result.size(), 2);
    for (ODocument d : result) {
        Assert.assertTrue(d.containsField("key"));
        Assert.assertTrue(d.containsField("rid"));
        if (!d.field("key").equals("spam") && !d.field("key").equals("eggs")) {
            Assert.fail("Unknown key found: " + d.field("key"));
        }
    }
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) Collector(com.orientechnologies.orient.test.domain.whiz.Collector) ORecordId(com.orientechnologies.orient.core.id.ORecordId) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 83 with ORecordId

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

the class RemoteProtocolCommandsTest method testRawCreateWithoutIDTest.

@Test
public void testRawCreateWithoutIDTest() {
    OClass clazz = this.database.getMetadata().getSchema().createClass("RidCreationTestClass");
    OStorage storage = this.database.getStorage();
    ODocument doc = new ODocument("RidCreationTestClass");
    doc.field("test", "test");
    ORecordId bad = new ORecordId(-1, -1);
    OStorageOperationResult<OPhysicalPosition> res = storage.createRecord(bad, doc.toStream(), doc.getVersion(), ODocument.RECORD_TYPE, OPERATION_MODE.SYNCHRONOUS.ordinal(), null);
    // assertTrue(" the cluster is not valid", bad.clusterId >= 0);
    String ids = "";
    for (int aId : clazz.getClusterIds()) ids += aId;
    assertTrue(" returned id:" + bad.getClusterId() + " shoud be one of:" + ids, Arrays.binarySearch(clazz.getClusterIds(), bad.getClusterId()) >= 0);
}
Also used : OClass(com.orientechnologies.orient.core.metadata.schema.OClass) OStorage(com.orientechnologies.orient.core.storage.OStorage) ORecordId(com.orientechnologies.orient.core.id.ORecordId) OPhysicalPosition(com.orientechnologies.orient.core.storage.OPhysicalPosition) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test)

Example 84 with ORecordId

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

the class OSQLEngine method parseRIDTarget.

public Set<OIdentifiable> parseRIDTarget(final ODatabaseDocument database, String iTarget, final OCommandContext iContext, Map<Object, Object> iArgs) {
    final Set<OIdentifiable> ids;
    if (iTarget.startsWith("(")) {
        // SUB-QUERY
        final OSQLSynchQuery<Object> query = new OSQLSynchQuery<Object>(iTarget.substring(1, iTarget.length() - 1));
        query.setContext(iContext);
        final List<OIdentifiable> result = database.query(query, iArgs);
        if (result == null || result.isEmpty())
            ids = Collections.emptySet();
        else {
            ids = new HashSet<OIdentifiable>((int) (result.size() * 1.3));
            for (OIdentifiable aResult : result) ids.add(aResult.getIdentity());
        }
    } else if (iTarget.startsWith("[")) {
        // COLLECTION OF RIDS
        final String[] idsAsStrings = iTarget.substring(1, iTarget.length() - 1).split(",");
        ids = new HashSet<OIdentifiable>((int) (idsAsStrings.length * 1.3));
        for (String idsAsString : idsAsStrings) {
            if (idsAsString.startsWith("$")) {
                Object r = iContext.getVariable(idsAsString);
                if (r instanceof OIdentifiable)
                    ids.add((OIdentifiable) r);
                else
                    OMultiValue.add(ids, r);
            } else
                ids.add(new ORecordId(idsAsString));
        }
    } else {
        // SINGLE RID
        if (iTarget.startsWith("$")) {
            Object r = iContext.getVariable(iTarget);
            if (r instanceof OIdentifiable)
                ids = Collections.<OIdentifiable>singleton((OIdentifiable) r);
            else
                ids = (Set<OIdentifiable>) OMultiValue.add(new HashSet<OIdentifiable>(OMultiValue.getSize(r)), r);
        } else
            ids = Collections.<OIdentifiable>singleton(new ORecordId(iTarget));
    }
    return ids;
}
Also used : OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ORecordId(com.orientechnologies.orient.core.id.ORecordId) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 85 with ORecordId

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

the class OSQLHelper method parseValue.

/**
   * Convert fields from text to real value. Supports: String, RID, Boolean, Float, Integer and NULL.
   * 
   * @param iValue
   *          Value to convert.
   * @return The value converted if recognized, otherwise VALUE_NOT_PARSED
   */
public static Object parseValue(String iValue, final OCommandContext iContext) {
    if (iValue == null)
        return null;
    iValue = iValue.trim();
    Object fieldValue = VALUE_NOT_PARSED;
    if (iValue.startsWith("'") && iValue.endsWith("'") || iValue.startsWith("\"") && iValue.endsWith("\""))
        // STRING
        fieldValue = OStringSerializerHelper.decode(OIOUtils.getStringContent(iValue));
    else if (iValue.charAt(0) == OStringSerializerHelper.LIST_BEGIN && iValue.charAt(iValue.length() - 1) == OStringSerializerHelper.LIST_END) {
        // COLLECTION/ARRAY
        final List<String> items = OStringSerializerHelper.smartSplit(iValue.substring(1, iValue.length() - 1), OStringSerializerHelper.RECORD_SEPARATOR);
        final List<Object> coll = new ArrayList<Object>();
        for (String item : items) {
            coll.add(parseValue(item, iContext));
        }
        fieldValue = coll;
    } else if (iValue.charAt(0) == OStringSerializerHelper.MAP_BEGIN && iValue.charAt(iValue.length() - 1) == OStringSerializerHelper.MAP_END) {
        // MAP
        final List<String> items = OStringSerializerHelper.smartSplit(iValue.substring(1, iValue.length() - 1), OStringSerializerHelper.RECORD_SEPARATOR);
        final Map<Object, Object> map = new HashMap<Object, Object>();
        for (String item : items) {
            final List<String> parts = OStringSerializerHelper.smartSplit(item, OStringSerializerHelper.ENTRY_SEPARATOR);
            if (parts == null || parts.size() != 2)
                throw new OCommandSQLParsingException("Map found but entries are not defined as <key>:<value>");
            Object key = OStringSerializerHelper.decode(parseValue(parts.get(0), iContext).toString());
            Object value = parseValue(parts.get(1), iContext);
            if (VALUE_NOT_PARSED == value) {
                value = new OSQLPredicate(parts.get(1)).evaluate(iContext);
            }
            map.put(key, value);
        }
        if (map.containsKey(ODocumentHelper.ATTRIBUTE_TYPE))
            // IT'S A DOCUMENT
            // TODO: IMPROVE THIS CASE AVOIDING DOUBLE PARSING
            fieldValue = new ODocument().fromJSON(iValue);
        else
            fieldValue = map;
    } else if (iValue.charAt(0) == OStringSerializerHelper.EMBEDDED_BEGIN && iValue.charAt(iValue.length() - 1) == OStringSerializerHelper.EMBEDDED_END) {
        // SUB-COMMAND
        fieldValue = new OCommandSQL(iValue.substring(1, iValue.length() - 1));
        ((OCommandSQL) fieldValue).getContext().setParent(iContext);
    } else if (ORecordId.isA(iValue))
        // RID
        fieldValue = new ORecordId(iValue.trim());
    else {
        if (iValue.equalsIgnoreCase("null"))
            // NULL
            fieldValue = null;
        else if (iValue.equalsIgnoreCase("not null"))
            // NULL
            fieldValue = NOT_NULL;
        else if (iValue.equalsIgnoreCase("defined"))
            // NULL
            fieldValue = DEFINED;
        else if (iValue.equalsIgnoreCase("true"))
            // BOOLEAN, TRUE
            fieldValue = Boolean.TRUE;
        else if (iValue.equalsIgnoreCase("false"))
            // BOOLEAN, FALSE
            fieldValue = Boolean.FALSE;
        else if (iValue.startsWith("date(")) {
            final OSQLFunctionRuntime func = OSQLHelper.getFunction(null, iValue);
            if (func != null) {
                fieldValue = func.execute(null, null, null, iContext);
            }
        } else {
            final Object v = parseStringNumber(iValue);
            if (v != null)
                fieldValue = v;
        }
    }
    return fieldValue;
}
Also used : OSQLFunctionRuntime(com.orientechnologies.orient.core.sql.functions.OSQLFunctionRuntime) HashMap(java.util.HashMap) ORecordId(com.orientechnologies.orient.core.id.ORecordId) OSQLPredicate(com.orientechnologies.orient.core.sql.filter.OSQLPredicate) ArrayList(java.util.ArrayList) List(java.util.List) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

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