Search in sources :

Example 21 with ODocument

use of com.orientechnologies.orient.core.record.impl.ODocument in project orientdb by orientechnologies.

the class ODatabaseDocumentTx method assignAndCheckCluster.

public int assignAndCheckCluster(ORecord record, String iClusterName) {
    ORecordId rid = (ORecordId) record.getIdentity();
    // if provided a cluster name use it.
    if (rid.getClusterId() <= ORID.CLUSTER_POS_INVALID && iClusterName != null) {
        rid.setClusterId(getClusterIdByName(iClusterName));
        if (rid.getClusterId() == -1)
            throw new IllegalArgumentException("Cluster name '" + iClusterName + "' is not configured");
    }
    OClass schemaClass = null;
    // if cluster id is not set yet try to find it out
    if (rid.getClusterId() <= ORID.CLUSTER_ID_INVALID && storage.isAssigningClusterIds()) {
        if (record instanceof ODocument) {
            schemaClass = ODocumentInternal.getImmutableSchemaClass(((ODocument) record));
            if (schemaClass != null) {
                if (schemaClass.isAbstract())
                    throw new OSchemaException("Document belongs to abstract class " + schemaClass.getName() + " and cannot be saved");
                rid.setClusterId(schemaClass.getClusterForNewInstance((ODocument) record));
            } else
                rid.setClusterId(getDefaultClusterId());
        } else {
            rid.setClusterId(getDefaultClusterId());
            if (record instanceof OBlob && rid.getClusterId() != ORID.CLUSTER_ID_INVALID) {
            // Set<Integer> blobClusters = getMetadata().getSchema().getBlobClusters();
            // if (!blobClusters.contains(rid.clusterId) && rid.clusterId != getDefaultClusterId() && rid.clusterId != 0) {
            // if (iClusterName == null)
            // iClusterName = getClusterNameById(rid.clusterId);
            // throw new IllegalArgumentException(
            // "Cluster name '" + iClusterName + "' (id=" + rid.clusterId + ") is not configured to store blobs, valid are "
            // + blobClusters.toString());
            // }
            }
        }
    } else if (record instanceof ODocument)
        schemaClass = ODocumentInternal.getImmutableSchemaClass(((ODocument) record));
    // If the cluster id was set check is validity
    if (rid.getClusterId() > ORID.CLUSTER_ID_INVALID) {
        if (schemaClass != null) {
            String messageClusterName = getClusterNameById(rid.getClusterId());
            checkRecordClass(schemaClass, messageClusterName, rid);
            if (!schemaClass.hasClusterId(rid.getClusterId())) {
                throw new IllegalArgumentException("Cluster name '" + messageClusterName + "' (id=" + rid.getClusterId() + ") is not configured to store the class '" + schemaClass.getName() + "', valid are " + Arrays.toString(schemaClass.getClusterIds()));
            }
        }
    }
    return rid.getClusterId();
}
Also used : OBlob(com.orientechnologies.orient.core.record.impl.OBlob) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ORecordId(com.orientechnologies.orient.core.id.ORecordId) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 22 with ODocument

use of com.orientechnologies.orient.core.record.impl.ODocument in project orientdb by orientechnologies.

the class ODatabaseDocumentTx method countClass.

/**
   * Returns the number of the records of the class iClassName considering also sub classes if polymorphic is true.
   */
public long countClass(final String iClassName, final boolean iPolymorphic) {
    final OClass cls = getMetadata().getImmutableSchemaSnapshot().getClass(iClassName);
    if (cls == null)
        throw new IllegalArgumentException("Class '" + iClassName + "' not found in database");
    long totalOnDb = cls.count(iPolymorphic);
    long deletedInTx = 0;
    long addedInTx = 0;
    if (getTransaction().isActive())
        for (ORecordOperation op : getTransaction().getAllRecordEntries()) {
            if (op.type == ORecordOperation.DELETED) {
                final ORecord rec = op.getRecord();
                if (rec != null && rec instanceof ODocument) {
                    OClass schemaClass = ((ODocument) rec).getSchemaClass();
                    if (iPolymorphic) {
                        if (schemaClass.isSubClassOf(iClassName))
                            deletedInTx++;
                    } else {
                        if (iClassName.equals(schemaClass.getName()) || iClassName.equals(schemaClass.getShortName()))
                            deletedInTx++;
                    }
                }
            }
            if (op.type == ORecordOperation.CREATED) {
                final ORecord rec = op.getRecord();
                if (rec != null && rec instanceof ODocument) {
                    OClass schemaClass = ((ODocument) rec).getSchemaClass();
                    if (iPolymorphic) {
                        if (schemaClass.isSubClassOf(iClassName))
                            addedInTx++;
                    } else {
                        if (iClassName.equals(schemaClass.getName()) || iClassName.equals(schemaClass.getShortName()))
                            addedInTx++;
                    }
                }
            }
        }
    return (totalOnDb + addedInTx) - deletedInTx;
}
Also used : ORecord(com.orientechnologies.orient.core.record.ORecord) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 23 with ODocument

use of com.orientechnologies.orient.core.record.impl.ODocument in project orientdb by orientechnologies.

the class OFunctionLibraryImpl method load.

public void load() {
    // COPY CALLBACK IN RAM
    final Map<String, OCallable<Object, Map<Object, Object>>> callbacks = new HashMap<String, OCallable<Object, Map<Object, Object>>>();
    for (Map.Entry<String, OFunction> entry : functions.entrySet()) {
        if (entry.getValue().getCallback() != null)
            callbacks.put(entry.getKey(), entry.getValue().getCallback());
    }
    functions.clear();
    // LOAD ALL THE FUNCTIONS IN MEMORY
    final ODatabaseDocument db = ODatabaseRecordThreadLocal.INSTANCE.get();
    if (((OMetadataInternal) db.getMetadata()).getImmutableSchemaSnapshot().existsClass("OFunction")) {
        List<ODocument> result = db.query(new OSQLSynchQuery<ODocument>("select from OFunction order by name"));
        for (ODocument d : result) {
            d.reload();
            final OFunction f = new OFunction(d);
            // RESTORE CALLBACK IF ANY
            f.setCallback(callbacks.get(f.getName()));
            functions.put(d.field("name").toString().toUpperCase(), f);
        }
    }
}
Also used : OCallable(com.orientechnologies.common.util.OCallable) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 24 with ODocument

use of com.orientechnologies.orient.core.record.impl.ODocument in project orientdb by orientechnologies.

the class OFunctionLibraryImpl method dropFunction.

@Override
public synchronized void dropFunction(OFunction function) {
    String name = function.getName();
    ODocument doc = function.getDocument();
    doc.delete();
    functions.remove(name.toUpperCase());
}
Also used : ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 25 with ODocument

use of com.orientechnologies.orient.core.record.impl.ODocument in project orientdb by orientechnologies.

the class OFunctionLibraryImpl method dropFunction.

@Override
public synchronized void dropFunction(String iName) {
    OFunction function = getFunction(iName);
    ODocument doc = function.getDocument();
    doc.delete();
    functions.remove(iName.toUpperCase());
}
Also used : ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Aggregations

ODocument (com.orientechnologies.orient.core.record.impl.ODocument)2200 Test (org.testng.annotations.Test)651 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)426 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)422 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)277 Test (org.junit.Test)267 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)257 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)244 ORID (com.orientechnologies.orient.core.id.ORID)196 ORecordId (com.orientechnologies.orient.core.id.ORecordId)139 OSchema (com.orientechnologies.orient.core.metadata.schema.OSchema)122 ArrayList (java.util.ArrayList)118 ORidBag (com.orientechnologies.orient.core.db.record.ridbag.ORidBag)103 HashMap (java.util.HashMap)103 HashSet (java.util.HashSet)96 ORecord (com.orientechnologies.orient.core.record.ORecord)80 Set (java.util.Set)76 OETLBaseTest (com.orientechnologies.orient.etl.OETLBaseTest)75 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)68 Collection (java.util.Collection)55