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();
}
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;
}
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);
}
}
}
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());
}
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());
}
Aggregations