use of com.orientechnologies.orient.core.id.ORecordId in project orientdb by orientechnologies.
the class ODatabaseDocumentTx method executeSaveEmptyRecord.
public <RET extends ORecord> RET executeSaveEmptyRecord(ORecord record, String clusterName) {
ORecordId rid = (ORecordId) record.getIdentity();
assert rid.isNew();
ORecordInternal.onBeforeIdentityChanged(record);
int id = assignAndCheckCluster(record, clusterName);
clusterName = getClusterNameById(id);
checkSecurity(ORule.ResourceGeneric.CLUSTER, ORole.PERMISSION_CREATE, clusterName);
byte[] content = getSerializer().writeClassOnly(record);
final OStorageOperationResult<OPhysicalPosition> ppos = storage.createRecord(rid, content, record.getVersion(), recordType, OPERATION_MODE.SYNCHRONOUS.ordinal(), null);
ORecordInternal.setVersion(record, ppos.getResult().recordVersion);
((ORecordId) record.getIdentity()).copyFrom(rid);
ORecordInternal.onAfterIdentityChanged(record);
return (RET) record;
}
use of com.orientechnologies.orient.core.id.ORecordId 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.id.ORecordId in project orientdb by orientechnologies.
the class OIndexManagerRemote method createIndex.
public OIndex<?> createIndex(final String iName, final String iType, final OIndexDefinition iIndexDefinition, final int[] iClusterIdsToIndex, final OProgressListener progressListener, ODocument metadata, String engine) {
String createIndexDDL;
if (iIndexDefinition != null)
createIndexDDL = iIndexDefinition.toCreateIndexDDL(iName, iType, engine);
else
createIndexDDL = new OSimpleKeyIndexDefinition().toCreateIndexDDL(iName, iType, engine);
if (metadata != null)
createIndexDDL += " " + OCommandExecutorSQLCreateIndex.KEYWORD_METADATA + " " + metadata.toJSON();
acquireExclusiveLock();
try {
if (progressListener != null)
progressListener.onBegin(this, 0, false);
getDatabase().command(new OCommandSQL(createIndexDDL)).execute();
ORecordInternal.setIdentity(document, new ORecordId(ODatabaseRecordThreadLocal.INSTANCE.get().getStorage().getConfiguration().indexMgrRecordId));
if (progressListener != null)
progressListener.onCompletition(this, true);
reload();
final Locale locale = getServerLocale();
return preProcessBeforeReturn(indexes.get(iName.toLowerCase(locale)));
} finally {
releaseExclusiveLock();
}
}
use of com.orientechnologies.orient.core.id.ORecordId in project orientdb by orientechnologies.
the class ODatabaseImport method importRecords.
private long importRecords() throws Exception {
long total = 0;
database.getMetadata().getIndexManager().dropIndex(EXPORT_IMPORT_MAP_NAME);
OIndexFactory factory = OIndexes.getFactory(OClass.INDEX_TYPE.DICTIONARY_HASH_INDEX.toString(), OHashIndexFactory.HASH_INDEX_ALGORITHM);
exportImportHashTable = (OIndex<OIdentifiable>) database.getMetadata().getIndexManager().createIndex(EXPORT_IMPORT_MAP_NAME, OClass.INDEX_TYPE.DICTIONARY_HASH_INDEX.toString(), new OSimpleKeyIndexDefinition(factory.getLastVersion(), OType.LINK), null, null, null);
jsonReader.readNext(OJSONReader.BEGIN_COLLECTION);
long totalRecords = 0;
listener.onMessage("\n\nImporting records...");
ORID rid;
ORID lastRid = new ORecordId();
final long begin = System.currentTimeMillis();
long lastLapRecords = 0;
long last = begin;
Set<String> involvedClusters = new HashSet<String>();
while (jsonReader.lastChar() != ']') {
rid = importRecord();
if (rid != null) {
++lastLapRecords;
++totalRecords;
if (rid.getClusterId() != lastRid.getClusterId() || involvedClusters.isEmpty())
involvedClusters.add(database.getClusterNameById(rid.getClusterId()));
final long now = System.currentTimeMillis();
if (now - last > IMPORT_RECORD_DUMP_LAP_EVERY_MS) {
final List<String> sortedClusters = new ArrayList<String>(involvedClusters);
Collections.sort(sortedClusters);
listener.onMessage(String.format("\n- Imported %,d records into clusters: %s. Total records imported so far: %,d (%,.2f/sec)", lastLapRecords, sortedClusters, totalRecords, (float) lastLapRecords * 1000 / (float) IMPORT_RECORD_DUMP_LAP_EVERY_MS));
// RESET LAP COUNTERS
last = now;
lastLapRecords = 0;
involvedClusters.clear();
}
lastRid = rid;
}
record = null;
}
if (migrateLinks)
migrateLinksInImportedDocuments();
listener.onMessage(String.format("\n\nDone. Imported %,d records in %,.2f secs\n", totalRecords, ((float) (System.currentTimeMillis() - begin)) / 1000));
jsonReader.readNext(OJSONReader.COMMA_SEPARATOR);
return total;
}
use of com.orientechnologies.orient.core.id.ORecordId in project orientdb by orientechnologies.
the class ODatabaseImport method importInfo.
private void importInfo() throws IOException, ParseException {
listener.onMessage("\nImporting database info...");
jsonReader.readNext(OJSONReader.BEGIN_OBJECT);
while (jsonReader.lastChar() != '}') {
final String fieldName = jsonReader.readString(OJSONReader.FIELD_ASSIGNMENT);
if (fieldName.equals("exporter-version"))
exporterVersion = jsonReader.readInteger(OJSONReader.NEXT_IN_OBJECT);
else if (fieldName.equals("schemaRecordId"))
schemaRecordId = new ORecordId(jsonReader.readString(OJSONReader.NEXT_IN_OBJECT));
else if (fieldName.equals("indexMgrRecordId"))
indexMgrRecordId = new ORecordId(jsonReader.readString(OJSONReader.NEXT_IN_OBJECT));
else
jsonReader.readNext(OJSONReader.NEXT_IN_OBJECT);
}
jsonReader.readNext(OJSONReader.COMMA_SEPARATOR);
if (schemaRecordId == null)
schemaRecordId = new ORecordId(database.getStorage().getConfiguration().schemaRecordId);
if (indexMgrRecordId == null)
indexMgrRecordId = new ORecordId(database.getStorage().getConfiguration().indexMgrRecordId);
listener.onMessage("OK");
}
Aggregations