Skip to content

Commit e0cc306

Browse files
Initial version of autosharding. CRUD + data migration.
1 parent 1f9f130 commit e0cc306

39 files changed

+2165
-156
lines changed

core/src/main/java/com/orientechnologies/orient/core/db/ODatabaseComplex.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,15 +167,17 @@ public enum OPERATION_MODE {
167167
* Saves an entity specifying the mode. If the entity is not dirty, then the operation will be ignored. For custom entity
168168
* implementations assure to set the entity as dirty. If the cluster does not exist, an error will be thrown.
169169
*
170+
*
170171
* @param iObject
171172
* The entity to save
172173
* @param iMode
173174
* Mode of save: synchronous (default) or asynchronous
175+
* @param iForceCreate
176+
* Flag that indicates that record should be created. If record with current rid already exists, exception is thrown
174177
* @param iCallback
175-
* Callback to call once the save is made
176-
* @return The saved entity.
178+
* Callback to call once the save is made @return The saved entity.
177179
*/
178-
public <RET extends T> RET save(T iObject, OPERATION_MODE iMode, ORecordCallback<? extends Number> iCallback);
180+
public <RET extends T> RET save(T iObject, OPERATION_MODE iMode, boolean iForceCreate, ORecordCallback<? extends Number> iCallback);
179181

180182
/**
181183
* Saves an entity in the specified cluster in synchronous mode. If the entity is not dirty, then the operation will be ignored.
@@ -193,17 +195,20 @@ public enum OPERATION_MODE {
193195
* Saves an entity in the specified cluster specifying the mode. If the entity is not dirty, then the operation will be ignored.
194196
* For custom entity implementations assure to set the entity as dirty. If the cluster does not exist, an error will be thrown.
195197
*
198+
*
196199
* @param iObject
197200
* The entity to save
198201
* @param iClusterName
199202
* Name of the cluster where to save
200203
* @param iMode
201204
* Mode of save: synchronous (default) or asynchronous
205+
* @param iForceCreate
206+
* Flag that indicates that record should be created. If record with current rid already exists, exception is thrown
202207
* @param iCallback
203-
* Callback to call once the save is made
204-
* @return The saved entity.
208+
* Callback to call once the save is made @return The saved entity.
205209
*/
206-
public <RET extends T> RET save(T iObject, String iClusterName, OPERATION_MODE iMode, ORecordCallback<? extends Number> iCallback);
210+
public <RET extends T> RET save(T iObject, String iClusterName, OPERATION_MODE iMode, boolean iForceCreate,
211+
ORecordCallback<? extends Number> iCallback);
207212

208213
/**
209214
* Deletes an entity from the database in synchronous mode.

core/src/main/java/com/orientechnologies/orient/core/db/ODatabaseRecordWrapperAbstract.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,13 +238,13 @@ public <RET extends ORecordInternal<?>> RET save(final ORecordInternal<?> iRecor
238238
}
239239

240240
public <RET extends ORecordInternal<?>> RET save(final ORecordInternal<?> iRecord, final OPERATION_MODE iMode,
241-
final ORecordCallback<? extends Number> iCallback) {
242-
return (RET) underlying.save(iRecord, iMode, iCallback);
241+
boolean iForceCreate, final ORecordCallback<? extends Number> iCallback) {
242+
return (RET) underlying.save(iRecord, iMode, iForceCreate, iCallback);
243243
}
244244

245245
public <RET extends ORecordInternal<?>> RET save(final ORecordInternal<?> iRecord, final String iClusterName,
246-
final OPERATION_MODE iMode, final ORecordCallback<? extends Number> iCallback) {
247-
return (RET) underlying.save(iRecord, iClusterName, iMode, iCallback);
246+
final OPERATION_MODE iMode, boolean iForceCreate, final ORecordCallback<? extends Number> iCallback) {
247+
return (RET) underlying.save(iRecord, iClusterName, iMode, iForceCreate, iCallback);
248248
}
249249

250250
public void setInternal(final ATTRIBUTES attribute, final Object iValue) {

core/src/main/java/com/orientechnologies/orient/core/db/document/ODatabaseDocumentTx.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ public ORecordIteratorCluster<ODocument> browseCluster(final String iClusterName
213213
*/
214214
@Override
215215
public <RET extends ORecordInternal<?>> RET save(final ORecordInternal<?> iRecord) {
216-
return (RET) save(iRecord, OPERATION_MODE.SYNCHRONOUS, null);
216+
return (RET) save(iRecord, OPERATION_MODE.SYNCHRONOUS, false, null);
217217
}
218218

219219
/**
@@ -228,8 +228,11 @@ public <RET extends ORecordInternal<?>> RET save(final ORecordInternal<?> iRecor
228228
* constraints declared in the schema if any (can work also in schema-less mode). To validate the document the
229229
* {@link ODocument#validate()} is called.
230230
*
231+
*
231232
* @param iRecord
232233
* Record to save.
234+
* @param iForceCreate
235+
* Flag that indicates that record should be created. If record with current rid already exists, exception is thrown
233236
* @return The Database instance itself giving a "fluent interface". Useful to call multiple methods in chain.
234237
* @throws OConcurrentModificationException
235238
* if the version of the document is different by the version contained in the database.
@@ -239,16 +242,16 @@ public <RET extends ORecordInternal<?>> RET save(final ORecordInternal<?> iRecor
239242
*/
240243
@Override
241244
public <RET extends ORecordInternal<?>> RET save(final ORecordInternal<?> iRecord, final OPERATION_MODE iMode,
242-
final ORecordCallback<? extends Number> iCallback) {
245+
boolean iForceCreate, final ORecordCallback<? extends Number> iCallback) {
243246
if (!(iRecord instanceof ODocument))
244-
return (RET) super.save(iRecord, iMode, iCallback);
247+
return (RET) super.save(iRecord, iMode, iForceCreate, iCallback);
245248

246249
ODocument doc = (ODocument) iRecord;
247250
doc.validate();
248251
doc.convertAllMultiValuesToTrackedVersions();
249252

250253
try {
251-
if (doc.getIdentity().isNew()) {
254+
if (iForceCreate || doc.getIdentity().isNew()) {
252255
// NEW RECORD
253256
if (doc.getClassName() != null)
254257
checkSecurity(ODatabaseSecurityResources.CLASS, ORole.PERMISSION_CREATE, doc.getClassName());
@@ -257,15 +260,15 @@ public <RET extends ORecordInternal<?>> RET save(final ORecordInternal<?> iRecor
257260
// CLASS FOUND: FORCE THE STORING IN THE CLUSTER CONFIGURED
258261
String clusterName = getClusterNameById(doc.getSchemaClass().getDefaultClusterId());
259262

260-
return (RET) super.save(doc, clusterName, iMode, iCallback);
263+
return (RET) super.save(doc, clusterName, iMode, iForceCreate, iCallback);
261264
}
262265
} else {
263266
// UPDATE: CHECK ACCESS ON SCHEMA CLASS NAME (IF ANY)
264267
if (doc.getClassName() != null)
265268
checkSecurity(ODatabaseSecurityResources.CLASS, ORole.PERMISSION_UPDATE, doc.getClassName());
266269
}
267270

268-
doc = super.save(doc, iMode, iCallback);
271+
doc = super.save(doc, iMode, iForceCreate, iCallback);
269272

270273
} catch (OException e) {
271274
// PASS THROUGH
@@ -302,7 +305,7 @@ public <RET extends ORecordInternal<?>> RET save(final ORecordInternal<?> iRecor
302305
*/
303306
@Override
304307
public <RET extends ORecordInternal<?>> RET save(final ORecordInternal<?> iRecord, final String iClusterName) {
305-
return (RET) save(iRecord, iClusterName, OPERATION_MODE.SYNCHRONOUS, null);
308+
return (RET) save(iRecord, iClusterName, OPERATION_MODE.SYNCHRONOUS, false, null);
306309
}
307310

308311
/**
@@ -323,6 +326,8 @@ public <RET extends ORecordInternal<?>> RET save(final ORecordInternal<?> iRecor
323326
* Cluster name where to save the record
324327
* @param iMode
325328
* Mode of save: synchronous (default) or asynchronous
329+
* @param iForceCreate
330+
* Flag that indicates that record should be created. If record with current rid already exists, exception is thrown
326331
* @return The Database instance itself giving a "fluent interface". Useful to call multiple methods in chain.
327332
* @throws OConcurrentModificationException
328333
* if the version of the document is different by the version contained in the database.
@@ -332,13 +337,13 @@ public <RET extends ORecordInternal<?>> RET save(final ORecordInternal<?> iRecor
332337
*/
333338
@Override
334339
public <RET extends ORecordInternal<?>> RET save(final ORecordInternal<?> iRecord, String iClusterName,
335-
final OPERATION_MODE iMode, final ORecordCallback<? extends Number> iCallback) {
340+
final OPERATION_MODE iMode, boolean iForceCreate, final ORecordCallback<? extends Number> iCallback) {
336341
if (!(iRecord instanceof ODocument))
337-
return (RET) super.save(iRecord, iClusterName, iMode, iCallback);
342+
return (RET) super.save(iRecord, iClusterName, iMode, iForceCreate, iCallback);
338343

339344
ODocument doc = (ODocument) iRecord;
340345

341-
if (!doc.getIdentity().isValid()) {
346+
if (iForceCreate || !doc.getIdentity().isValid()) {
342347
if (doc.getClassName() != null)
343348
checkSecurity(ODatabaseSecurityResources.CLASS, ORole.PERMISSION_CREATE, doc.getClassName());
344349

@@ -375,7 +380,7 @@ public <RET extends ORecordInternal<?>> RET save(final ORecordInternal<?> iRecor
375380
doc.validate();
376381
doc.convertAllMultiValuesToTrackedVersions();
377382

378-
doc = super.save(doc, iClusterName, iMode, iCallback);
383+
doc = super.save(doc, iClusterName, iMode, iForceCreate, iCallback);
379384
return (RET) doc;
380385
}
381386

core/src/main/java/com/orientechnologies/orient/core/db/raw/ODatabaseRaw.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,12 +237,12 @@ public ORawBuffer read(final ORecordId iRid, final String iFetchPlan, final bool
237237
}
238238

239239
public int save(final int iDataSegmentId, final ORecordId iRid, final byte[] iContent, final int iVersion,
240-
final byte iRecordType, final int iMode, final ORecordCallback<? extends Number> iCallBack) {
240+
final byte iRecordType, final int iMode, boolean iForceCreate, final ORecordCallback<? extends Number> iCallBack) {
241241
// CHECK IF RECORD TYPE IS SUPPORTED
242242
Orient.instance().getRecordFactoryManager().getRecordTypeClass(iRecordType);
243243

244244
try {
245-
if (iRid.clusterPosition < 0) {
245+
if (iForceCreate || iRid.clusterPosition < 0) {
246246
// CREATE
247247
return storage
248248
.createRecord(iDataSegmentId, iRid, iContent, iVersion, iRecordType, iMode, (ORecordCallback<Long>) iCallBack).recordVersion;

core/src/main/java/com/orientechnologies/orient/core/db/record/ODatabaseRecordAbstract.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -273,35 +273,39 @@ public <RET extends ORecordInternal<?>> RET load(final ORID iRecordId, final Str
273273
*/
274274
public <RET extends ORecordInternal<?>> RET save(final ORecordInternal<?> iContent) {
275275
return (RET) executeSaveRecord(iContent, null, iContent.getVersion(), iContent.getRecordType(), true,
276-
OPERATION_MODE.SYNCHRONOUS, null);
276+
OPERATION_MODE.SYNCHRONOUS, false, null);
277277
}
278278

279279
/**
280280
* Updates the record without checking the version.
281281
*
282+
* @param iForceCreate
283+
* Flag that indicates that record should be created. If record with current rid already exists, exception is thrown
282284
* @param iCallback
283285
*/
284286
public <RET extends ORecordInternal<?>> RET save(final ORecordInternal<?> iContent, final OPERATION_MODE iMode,
285-
final ORecordCallback<? extends Number> iCallback) {
286-
return (RET) executeSaveRecord(iContent, null, iContent.getVersion(), iContent.getRecordType(), true, iMode, iCallback);
287+
boolean iForceCreate, final ORecordCallback<? extends Number> iCallback) {
288+
return (RET) executeSaveRecord(iContent, null, iContent.getVersion(), iContent.getRecordType(), true, iMode, iForceCreate,
289+
iCallback);
287290
}
288291

289292
/**
290293
* Updates the record in the requested cluster without checking the version.
291294
*/
292295
public <RET extends ORecordInternal<?>> RET save(final ORecordInternal<?> iContent, final String iClusterName) {
293296
return (RET) executeSaveRecord(iContent, iClusterName, iContent.getVersion(), iContent.getRecordType(), true,
294-
OPERATION_MODE.SYNCHRONOUS, null);
297+
OPERATION_MODE.SYNCHRONOUS, false, null);
295298
}
296299

297300
/**
298301
* Updates the record in the requested cluster without checking the version.
299302
*
300-
* @param iCallbac
303+
* @param iForceCreate
301304
*/
302305
public <RET extends ORecordInternal<?>> RET save(final ORecordInternal<?> iContent, final String iClusterName,
303-
final OPERATION_MODE iMode, final ORecordCallback<? extends Number> iCallback) {
304-
return (RET) executeSaveRecord(iContent, iClusterName, iContent.getVersion(), iContent.getRecordType(), true, iMode, iCallback);
306+
final OPERATION_MODE iMode, boolean iForceCreate, final ORecordCallback<? extends Number> iCallback) {
307+
return (RET) executeSaveRecord(iContent, iClusterName, iContent.getVersion(), iContent.getRecordType(), true, iMode,
308+
iForceCreate, iCallback);
305309
}
306310

307311
/**
@@ -595,7 +599,7 @@ record = iRecord;
595599
}
596600

597601
public <RET extends ORecordInternal<?>> RET executeSaveRecord(final ORecordInternal<?> iRecord, String iClusterName,
598-
final int iVersion, final byte iRecordType, final boolean iCallTriggers, final OPERATION_MODE iMode,
602+
final int iVersion, final byte iRecordType, final boolean iCallTriggers, final OPERATION_MODE iMode, boolean iForceCreate,
599603
final ORecordCallback<? extends Number> iCallback) {
600604
checkOpeness();
601605

@@ -611,15 +615,15 @@ public <RET extends ORecordInternal<?>> RET executeSaveRecord(final ORecordInter
611615
setCurrentDatabaseinThreadLocal();
612616

613617
try {
614-
final boolean wasNew = rid.isNew();
618+
final boolean wasNew = iForceCreate || rid.isNew();
615619
if (wasNew && rid.clusterId == -1 && iClusterName != null)
616620
// ASSIGN THE CLUSTER ID
617621
rid.clusterId = getClusterIdByName(iClusterName);
618622

619623
// STREAM.LENGTH == 0 -> RECORD IN STACK: WILL BE SAVED AFTER
620624
byte[] stream = iRecord.toStream();
621625

622-
final boolean isNew = rid.isNew();
626+
final boolean isNew = iForceCreate || rid.isNew();
623627
if (isNew)
624628
// NOTIFY IDENTITY HAS CHANGED
625629
iRecord.onBeforeIdentityChanged(rid);
@@ -665,7 +669,7 @@ else if (stream == null || stream.length == 0)
665669
try {
666670
// SAVE IT
667671
final int version = underlying.save(dataSegmentId, rid, stream == null ? new byte[0] : stream, realVersion,
668-
iRecord.getRecordType(), iMode.ordinal(), iCallback);
672+
iRecord.getRecordType(), iMode.ordinal(), iForceCreate, iCallback);
669673

670674
if (isNew) {
671675
// UPDATE INFORMATION: CLUSTER ID+POSITION

core/src/main/java/com/orientechnologies/orient/core/db/record/ODatabaseRecordTx.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -198,32 +198,32 @@ public <RET extends ORecordInternal<?>> RET load(final ORID iRecordId, final Str
198198
return (RET) currentTx.loadRecord(iRecordId, null, iFetchPlan);
199199
}
200200

201-
@SuppressWarnings("unchecked")
201+
@SuppressWarnings("unchecked")
202202
@Override
203-
public <RET extends ORecordInternal<?>> RET save(final ORecordInternal<?> iContent, final OPERATION_MODE iMode,
204-
final ORecordCallback<? extends Number> iCallback) {
205-
return (RET) save(iContent, (String) null, iMode, iCallback);
206-
}
203+
public <RET extends ORecordInternal<?>> RET save(final ORecordInternal<?> iContent, final OPERATION_MODE iMode,
204+
boolean iForceCreate, final ORecordCallback<? extends Number> iCallback) {
205+
return (RET) save(iContent, (String) null, iMode, iForceCreate, iCallback);
206+
}
207207

208208
@SuppressWarnings("unchecked")
209-
@Override
210-
public <RET extends ORecordInternal<?>> RET save(final ORecordInternal<?> iContent) {
211-
return (RET) save(iContent, (String) null, OPERATION_MODE.SYNCHRONOUS, null);
212-
}
209+
@Override
210+
public <RET extends ORecordInternal<?>> RET save(final ORecordInternal<?> iContent) {
211+
return (RET) save(iContent, (String) null, OPERATION_MODE.SYNCHRONOUS, false, null);
212+
}
213213

214214
@SuppressWarnings("unchecked")
215-
@Override
216-
public <RET extends ORecordInternal<?>> RET save(final ORecordInternal<?> iContent, final String iClusterName) {
217-
return (RET) save(iContent, iClusterName, OPERATION_MODE.SYNCHRONOUS, null);
218-
}
215+
@Override
216+
public <RET extends ORecordInternal<?>> RET save(final ORecordInternal<?> iContent, final String iClusterName) {
217+
return (RET) save(iContent, iClusterName, OPERATION_MODE.SYNCHRONOUS, false, null);
218+
}
219219

220-
@SuppressWarnings("unchecked")
221-
@Override
222-
public <RET extends ORecordInternal<?>> RET save(final ORecordInternal<?> iContent, final String iClusterName,
223-
final OPERATION_MODE iMode, ORecordCallback<? extends Number> iCallback) {
224-
currentTx.saveRecord(iContent, iClusterName, iMode, iCallback);
225-
return (RET) iContent;
226-
}
220+
@SuppressWarnings("unchecked")
221+
@Override
222+
public <RET extends ORecordInternal<?>> RET save(final ORecordInternal<?> iContent, final String iClusterName,
223+
final OPERATION_MODE iMode, boolean iForceCreate, ORecordCallback<? extends Number> iCallback) {
224+
currentTx.saveRecord(iContent, iClusterName, iMode, iForceCreate, iCallback);
225+
return (RET) iContent;
226+
}
227227

228228
@Override
229229
public ODatabaseRecord delete(final ORecordInternal<?> iRecord) {

core/src/main/java/com/orientechnologies/orient/core/metadata/schema/OSchemaShared.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ public void saveInternal() {
572572
try {
573573

574574
document.setDirty();
575-
super.save();
575+
super.save(OMetadata.CLUSTER_INTERNAL_NAME);
576576

577577
} finally {
578578
lock.releaseExclusiveLock();

core/src/main/java/com/orientechnologies/orient/core/metadata/security/ORole.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
*/
3939
@SuppressWarnings("unchecked")
4040
public class ORole extends ODocumentWrapper {
41-
public static final String ADMIN = "admin";
41+
public static final String ADMIN = "admin";
42+
public static final String CLASS_NAME = "ORole";
4243

4344
public enum ALLOW_MODES {
4445
DENY_ALL_BUT, ALLOW_ALL_BUT
@@ -68,7 +69,7 @@ public ORole() {
6869
}
6970

7071
public ORole(final String iName, final ORole iParent, final ALLOW_MODES iAllowMode) {
71-
super("ORole");
72+
super(CLASS_NAME);
7273
document.field("name", iName);
7374
parentRole = iParent;
7475
document.field("inheritedRole", parentRole != null ? parentRole.getName() : null);

core/src/main/java/com/orientechnologies/orient/core/metadata/security/OUser.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@
3434
* @see ORole
3535
*/
3636
public class OUser extends ODocumentWrapper {
37-
public static final String ADMIN = "admin";
37+
public static final String ADMIN = "admin";
38+
public static final String CLASS_NAME = "OUser";
3839

39-
public enum STATUSES {
40-
SUSPENDED, ACTIVE
41-
}
40+
public enum STATUSES {
41+
SUSPENDED, ACTIVE
42+
}
4243

4344
// AVOID THE INVOCATION OF SETTER
4445
protected Set<ORole> roles = new HashSet<ORole>();
@@ -49,11 +50,11 @@ public enum STATUSES {
4950
public OUser() {
5051
}
5152

52-
public OUser(final String iName) {
53-
super("OUser");
54-
document.field("name", iName);
55-
setAccountStatus(STATUSES.ACTIVE);
56-
}
53+
public OUser(final String iName) {
54+
super(CLASS_NAME);
55+
document.field("name", iName);
56+
setAccountStatus(STATUSES.ACTIVE);
57+
}
5758

5859
public OUser(String iUserName, final String iUserPassword) {
5960
super("OUser");

core/src/main/java/com/orientechnologies/orient/core/record/ORecord.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@ public interface ORecord<T> extends ORecordElement, OIdentifiable, Serializable
174174
*/
175175
public <RET extends ORecord<T>> RET save(String iCluster);
176176

177+
public <RET extends ORecord<T>> RET save(boolean forceCreate);
178+
179+
public <RET extends ORecord<T>> RET save(String iCluster, boolean forceCreate);
180+
177181
/**
178182
* Deletes the record from the database. Behavior depends by the current running transaction if any. If no transaction is running
179183
* then the record is deleted immediately. If an Optimistic transaction is running then the record will be deleted at commit time.

0 commit comments

Comments
 (0)