Search in sources :

Example 11 with ORecordLazySet

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

the class OCommandExecutorSQLCreateLink method execute.

/**
   * Execute the CREATE LINK.
   */
public Object execute(final Map<Object, Object> iArgs) {
    if (destField == null)
        throw new OCommandExecutionException("Cannot execute the command because it has not been parsed yet");
    final ODatabaseDocumentInternal database = getDatabase();
    if (!(database.getDatabaseOwner() instanceof ODatabaseDocument))
        throw new OCommandSQLParsingException("This command supports only the database type ODatabaseDocumentTx and type '" + database.getClass() + "' was found");
    final ODatabaseDocument db = (ODatabaseDocument) database.getDatabaseOwner();
    final OClass sourceClass = database.getMetadata().getSchema().getClass(sourceClassName);
    if (sourceClass == null)
        throw new OCommandExecutionException("Source class '" + sourceClassName + "' not found");
    final OClass destClass = database.getMetadata().getSchema().getClass(destClassName);
    if (destClass == null)
        throw new OCommandExecutionException("Destination class '" + destClassName + "' not found");
    Object value;
    String cmd = "select from ";
    if (!ODocumentHelper.ATTRIBUTE_RID.equals(destField)) {
        cmd = "select from " + destClassName + " where " + destField + " = ";
    }
    List<ODocument> result;
    ODocument target;
    Object oldValue;
    long total = 0;
    if (linkName == null)
        // NO LINK NAME EXPRESSED: OVERWRITE THE SOURCE FIELD
        linkName = sourceField;
    boolean multipleRelationship;
    if (linkType != null)
        // DETERMINE BASED ON FORCED TYPE
        multipleRelationship = linkType == OType.LINKSET || linkType == OType.LINKLIST;
    else
        multipleRelationship = false;
    long totRecords = db.countClass(sourceClass.getName());
    long currRecord = 0;
    if (progressListener != null)
        progressListener.onBegin(this, totRecords, false);
    database.declareIntent(new OIntentMassiveInsert());
    try {
        // BROWSE ALL THE RECORDS OF THE SOURCE CLASS
        for (ODocument doc : db.browseClass(sourceClass.getName())) {
            value = doc.field(sourceField);
            if (value != null) {
                if (value instanceof ODocument || value instanceof ORID) {
                // ALREADY CONVERTED
                } else if (value instanceof Collection<?>) {
                // TODO
                } else {
                    // SEARCH THE DESTINATION RECORD
                    target = null;
                    if (!ODocumentHelper.ATTRIBUTE_RID.equals(destField) && value instanceof String)
                        if (((String) value).length() == 0)
                            value = null;
                        else
                            value = "'" + value + "'";
                    result = database.<OCommandRequest>command(new OSQLSynchQuery<ODocument>(cmd + value)).execute();
                    if (result == null || result.size() == 0)
                        value = null;
                    else if (result.size() > 1)
                        throw new OCommandExecutionException("Cannot create link because multiple records was found in class '" + destClass.getName() + "' with value " + value + " in field '" + destField + "'");
                    else {
                        target = result.get(0);
                        value = target;
                    }
                    if (target != null && inverse) {
                        // INVERSE RELATIONSHIP
                        oldValue = target.field(linkName);
                        if (oldValue != null) {
                            if (!multipleRelationship)
                                multipleRelationship = true;
                            Collection<ODocument> coll;
                            if (oldValue instanceof Collection) {
                                // ADD IT IN THE EXISTENT COLLECTION
                                coll = (Collection<ODocument>) oldValue;
                                target.setDirty();
                            } else {
                                // CREATE A NEW COLLECTION FOR BOTH
                                coll = new ArrayList<ODocument>(2);
                                target.field(linkName, coll);
                                coll.add((ODocument) oldValue);
                            }
                            coll.add(doc);
                        } else {
                            if (linkType != null)
                                if (linkType == OType.LINKSET) {
                                    value = new ORecordLazySet(target);
                                    ((Set<OIdentifiable>) value).add(doc);
                                } else if (linkType == OType.LINKLIST) {
                                    value = new ORecordLazyList(target);
                                    ((ORecordLazyList) value).add(doc);
                                } else
                                    // IGNORE THE TYPE, SET IT AS LINK
                                    value = doc;
                            else
                                value = doc;
                            target.field(linkName, value);
                        }
                        target.save();
                    } else {
                        // SET THE REFERENCE
                        doc.field(linkName, value);
                        doc.save();
                    }
                    total++;
                }
            }
            if (progressListener != null)
                progressListener.onProgress(this, currRecord, currRecord * 100f / totRecords);
        }
        if (total > 0) {
            if (inverse) {
                // REMOVE THE OLD PROPERTY IF ANY
                OProperty prop = destClass.getProperty(linkName);
                if (prop != null)
                    destClass.dropProperty(linkName);
                if (linkType == null)
                    linkType = multipleRelationship ? OType.LINKSET : OType.LINK;
                // CREATE THE PROPERTY
                destClass.createProperty(linkName, linkType, sourceClass);
            } else {
                // REMOVE THE OLD PROPERTY IF ANY
                OProperty prop = sourceClass.getProperty(linkName);
                if (prop != null)
                    sourceClass.dropProperty(linkName);
                // CREATE THE PROPERTY
                sourceClass.createProperty(linkName, OType.LINK, destClass);
            }
        }
        if (progressListener != null)
            progressListener.onCompletition(this, true);
    } catch (Exception e) {
        if (progressListener != null)
            progressListener.onCompletition(this, false);
        throw OException.wrapException(new OCommandExecutionException("Error on creation of links"), e);
    } finally {
        database.declareIntent(null);
    }
    return total;
}
Also used : ORecordLazyList(com.orientechnologies.orient.core.db.record.ORecordLazyList) OProperty(com.orientechnologies.orient.core.metadata.schema.OProperty) Set(java.util.Set) ORecordLazySet(com.orientechnologies.orient.core.db.record.ORecordLazySet) OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) ArrayList(java.util.ArrayList) ORecordLazySet(com.orientechnologies.orient.core.db.record.ORecordLazySet) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal) OIntentMassiveInsert(com.orientechnologies.orient.core.intent.OIntentMassiveInsert) OException(com.orientechnologies.common.exception.OException) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) Collection(java.util.Collection) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) ORID(com.orientechnologies.orient.core.id.ORID) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 12 with ORecordLazySet

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

the class CollectionOfLinkInNestedDocumentTest method nestedLinkSet.

@Test
public void nestedLinkSet() {
    ODocument doc1 = new ODocument();
    doc1.field("value", "item 1");
    ODocument doc2 = new ODocument();
    doc2.field("value", "item 2");
    ODocument nested = new ODocument();
    ORecordLazySet set = new ORecordLazySet(nested);
    set.add(doc1);
    set.add(doc2);
    nested.field("set", set);
    ODocument base = new ODocument();
    base.field("nested", nested, OType.EMBEDDED);
    OIdentifiable id = db.save(base);
    db.getLocalCache().clear();
    ODocument base1 = db.load(id.getIdentity());
    ODocument nest1 = base1.field("nested");
    assertNotNull(nest1);
    assertTrue(nested.field("set").equals(nest1.field("set")));
}
Also used : ORecordLazySet(com.orientechnologies.orient.core.db.record.ORecordLazySet) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) Test(org.testng.annotations.Test)

Example 13 with ORecordLazySet

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

the class TestOTypeDetection method testOTypeFromValue.

@Test
public void testOTypeFromValue() {
    assertEquals(OType.BOOLEAN, OType.getTypeByValue(true));
    assertEquals(OType.LONG, OType.getTypeByValue(2L));
    assertEquals(OType.INTEGER, OType.getTypeByValue(2));
    assertEquals(OType.SHORT, OType.getTypeByValue((short) 4));
    assertEquals(OType.FLOAT, OType.getTypeByValue(0.5f));
    assertEquals(OType.DOUBLE, OType.getTypeByValue(0.7d));
    assertEquals(OType.BYTE, OType.getTypeByValue((byte) 10));
    assertEquals(OType.STRING, OType.getTypeByValue('a'));
    assertEquals(OType.STRING, OType.getTypeByValue("yaaahooooo"));
    assertEquals(OType.BINARY, OType.getTypeByValue(new byte[] { 0, 1, 2 }));
    assertEquals(OType.DATETIME, OType.getTypeByValue(new Date()));
    assertEquals(OType.DECIMAL, OType.getTypeByValue(new BigDecimal(10)));
    assertEquals(OType.INTEGER, OType.getTypeByValue(new BigInteger("20")));
    assertEquals(OType.LINK, OType.getTypeByValue(new ODocument()));
    assertEquals(OType.LINK, OType.getTypeByValue(new ORecordId()));
    assertEquals(OType.EMBEDDEDLIST, OType.getTypeByValue(new ArrayList<Object>()));
    assertEquals(OType.EMBEDDEDLIST, OType.getTypeByValue(new OTrackedList<Object>(new ODocument())));
    assertEquals(OType.EMBEDDEDSET, OType.getTypeByValue(new HashSet<Object>()));
    assertEquals(OType.EMBEDDEDMAP, OType.getTypeByValue(new HashMap<Object, Object>()));
    assertEquals(OType.LINKSET, OType.getTypeByValue(new ORecordLazySet(new ODocument())));
    assertEquals(OType.LINKLIST, OType.getTypeByValue(new ORecordLazyList(new ODocument())));
    assertEquals(OType.LINKMAP, OType.getTypeByValue(new ORecordLazyMap(new ODocument())));
    assertEquals(OType.LINKBAG, OType.getTypeByValue(new ORidBag()));
    assertEquals(OType.CUSTOM, OType.getTypeByValue(new CustomClass()));
    assertEquals(OType.EMBEDDEDLIST, OType.getTypeByValue(new Object[] {}));
    assertEquals(OType.EMBEDDEDLIST, OType.getTypeByValue(new String[] {}));
    assertEquals(OType.EMBEDDED, OType.getTypeByValue(new DocumentSer()));
    assertEquals(OType.CUSTOM, OType.getTypeByValue(new ClassSerializable()));
}
Also used : ORecordLazyList(com.orientechnologies.orient.core.db.record.ORecordLazyList) HashMap(java.util.HashMap) ORidBag(com.orientechnologies.orient.core.db.record.ridbag.ORidBag) ArrayList(java.util.ArrayList) ORecordLazySet(com.orientechnologies.orient.core.db.record.ORecordLazySet) Date(java.util.Date) BigDecimal(java.math.BigDecimal) ORecordId(com.orientechnologies.orient.core.id.ORecordId) ORecordLazyMap(com.orientechnologies.orient.core.db.record.ORecordLazyMap) OTrackedList(com.orientechnologies.orient.core.db.record.OTrackedList) BigInteger(java.math.BigInteger) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 14 with ORecordLazySet

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

the class OObjectEntitySerializer method multiValueToStream.

@SuppressWarnings("unchecked")
private static Object multiValueToStream(final Object iMultiValue, OType iType, final ODatabaseObject db, final ODocument iRecord) {
    if (iMultiValue == null)
        return null;
    final Collection<Object> sourceValues;
    if (iMultiValue instanceof Collection<?>) {
        sourceValues = (Collection<Object>) iMultiValue;
    } else {
        sourceValues = (Collection<Object>) ((Map<?, ?>) iMultiValue).values();
    }
    if (sourceValues.size() == 0)
        return iMultiValue;
    // TRY TO UNDERSTAND THE COLLECTION TYPE BY ITS CONTENT
    final Object firstValue = sourceValues.iterator().next();
    if (firstValue == null)
        return iMultiValue;
    if (iType == null) {
        // DETERMINE THE RIGHT TYPE BASED ON SOURCE MULTI VALUE OBJECT
        if (OType.isSimpleType(firstValue)) {
            if (iMultiValue instanceof List)
                iType = OType.EMBEDDEDLIST;
            else if (iMultiValue instanceof Set)
                iType = OType.EMBEDDEDSET;
            else
                iType = OType.EMBEDDEDMAP;
        } else {
            if (iMultiValue instanceof List)
                iType = OType.LINKLIST;
            else if (iMultiValue instanceof Set)
                iType = OType.LINKSET;
            else
                iType = OType.LINKMAP;
        }
    }
    Object result = iMultiValue;
    final OType linkedType;
    // CREATE THE RETURN MULTI VALUE OBJECT BASED ON DISCOVERED TYPE
    if (iType.equals(OType.EMBEDDEDSET) || iType.equals(OType.LINKSET)) {
        if (isToSerialize(firstValue.getClass()))
            result = new HashSet<Object>();
        else if ((iRecord != null && iType.equals(OType.EMBEDDEDSET)) || OType.isSimpleType(firstValue))
            result = new OTrackedSet<Object>(iRecord);
        else
            result = new ORecordLazySet(iRecord);
    } else if (iType.equals(OType.EMBEDDEDLIST) || iType.equals(OType.LINKLIST)) {
        if (isToSerialize(firstValue.getClass()))
            result = new ArrayList<Object>();
        else if ((iRecord != null && iType.equals(OType.EMBEDDEDLIST)) || OType.isSimpleType(firstValue))
            result = new OTrackedList<Object>(iRecord);
        else
            result = new ORecordLazyList(iRecord);
    }
    if (iType.equals(OType.LINKLIST) || iType.equals(OType.LINKSET) || iType.equals(OType.LINKMAP))
        linkedType = OType.LINK;
    else if (iType.equals(OType.EMBEDDEDLIST) || iType.equals(OType.EMBEDDEDSET) || iType.equals(OType.EMBEDDEDMAP))
        if (firstValue instanceof List)
            linkedType = OType.EMBEDDEDLIST;
        else if (firstValue instanceof Set)
            linkedType = OType.EMBEDDEDSET;
        else if (firstValue instanceof Map)
            linkedType = OType.EMBEDDEDMAP;
        else
            linkedType = OType.EMBEDDED;
    else
        throw new IllegalArgumentException("Type " + iType + " must be a multi value type (collection or map)");
    if (iMultiValue instanceof Set<?>) {
        for (Object o : sourceValues) {
            ((Set<Object>) result).add(typeToStream(o, linkedType, db, null));
        }
    } else if (iMultiValue instanceof List<?>) {
        for (int i = 0; i < sourceValues.size(); i++) {
            ((List<Object>) result).add(typeToStream(((List<?>) sourceValues).get(i), linkedType, db, null));
        }
    } else {
        if (iMultiValue instanceof OObjectLazyMap<?>) {
            result = ((OObjectLazyMap<?>) iMultiValue).getUnderlying();
        } else {
            if (isToSerialize(firstValue.getClass()))
                result = new HashMap<Object, Object>();
            else if (iRecord != null && iType.equals(OType.EMBEDDEDMAP))
                result = new OTrackedMap<Object>(iRecord);
            else
                result = new ORecordLazyMap(iRecord);
            for (Entry<Object, Object> entry : ((Map<Object, Object>) iMultiValue).entrySet()) {
                ((Map<Object, Object>) result).put(entry.getKey(), typeToStream(entry.getValue(), linkedType, db, null));
            }
        }
    }
    return result;
}
Also used : OObjectLazyMap(com.orientechnologies.orient.object.db.OObjectLazyMap) ORecordLazyList(com.orientechnologies.orient.core.db.record.ORecordLazyList) Set(java.util.Set) ORecordLazySet(com.orientechnologies.orient.core.db.record.ORecordLazySet) HashSet(java.util.HashSet) OTrackedSet(com.orientechnologies.orient.core.db.record.OTrackedSet) OType(com.orientechnologies.orient.core.metadata.schema.OType) ORecordLazySet(com.orientechnologies.orient.core.db.record.ORecordLazySet) ORecordLazyMap(com.orientechnologies.orient.core.db.record.ORecordLazyMap) Entry(java.util.Map.Entry) OTrackedList(com.orientechnologies.orient.core.db.record.OTrackedList) OTrackedMap(com.orientechnologies.orient.core.db.record.OTrackedMap) Collection(java.util.Collection) ProxyObject(javassist.util.proxy.ProxyObject) ODatabaseObject(com.orientechnologies.orient.core.db.object.ODatabaseObject) OSchemaProxyObject(com.orientechnologies.orient.object.metadata.schema.OSchemaProxyObject) List(java.util.List) ORecordLazyList(com.orientechnologies.orient.core.db.record.ORecordLazyList) ArrayList(java.util.ArrayList) OTrackedList(com.orientechnologies.orient.core.db.record.OTrackedList) ORecordLazyMap(com.orientechnologies.orient.core.db.record.ORecordLazyMap) Map(java.util.Map) OTrackedMap(com.orientechnologies.orient.core.db.record.OTrackedMap) HashMap(java.util.HashMap) OObjectLazyMap(com.orientechnologies.orient.object.db.OObjectLazyMap) HashSet(java.util.HashSet)

Aggregations

ORecordLazySet (com.orientechnologies.orient.core.db.record.ORecordLazySet)14 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)8 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)7 ORecordLazyList (com.orientechnologies.orient.core.db.record.ORecordLazyList)7 OTrackedList (com.orientechnologies.orient.core.db.record.OTrackedList)6 ODatabaseObject (com.orientechnologies.orient.core.db.object.ODatabaseObject)5 OTrackedSet (com.orientechnologies.orient.core.db.record.OTrackedSet)5 ArrayList (java.util.ArrayList)5 Set (java.util.Set)5 ORecordLazyMap (com.orientechnologies.orient.core.db.record.ORecordLazyMap)4 ORidBag (com.orientechnologies.orient.core.db.record.ridbag.ORidBag)4 OType (com.orientechnologies.orient.core.metadata.schema.OType)4 Collection (java.util.Collection)4 HashSet (java.util.HashSet)4 Map (java.util.Map)4 Test (org.testng.annotations.Test)4 OTrackedMap (com.orientechnologies.orient.core.db.record.OTrackedMap)3 OSerializationException (com.orientechnologies.orient.core.exception.OSerializationException)3 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)3 ORecord (com.orientechnologies.orient.core.record.ORecord)3