Skip to content

Commit

Permalink
Fixed issue #3621
Browse files Browse the repository at this point in the history
  • Loading branch information
lvca committed Sep 23, 2015
1 parent 02ac677 commit d29612b
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 52 deletions.
Expand Up @@ -384,10 +384,10 @@ public int size() {
} else
coll = (OCollection<Object>) iObject;

if (isMultiValue(iToAdd)) {
if (!(iToAdd instanceof Map) && isMultiValue(iToAdd)) {
// COLLECTION - COLLECTION
for (Object o : getMultiValueIterable(iToAdd)) {
if (isMultiValue(o))
if (!(o instanceof Map) && isMultiValue(o))
add(coll, o);
else
coll.add(o);
Expand All @@ -398,7 +398,7 @@ else if (iToAdd != null && iToAdd.getClass().isArray()) {
// ARRAY - COLLECTION
for (int i = 0; i < Array.getLength(iToAdd); ++i) {
Object o = Array.get(iToAdd, i);
if (isMultiValue(o))
if (!(o instanceof Map) && isMultiValue(o))
add(coll, o);
else
coll.add(o);
Expand Down
Expand Up @@ -20,6 +20,8 @@
import com.orientechnologies.common.util.OPair;
import com.orientechnologies.orient.core.config.OGlobalConfiguration;
import com.orientechnologies.orient.core.db.record.OIdentifiable;
import com.orientechnologies.orient.core.id.ORID;
import com.orientechnologies.orient.core.id.ORecordId;
import com.orientechnologies.orient.core.metadata.schema.OClass;
import com.orientechnologies.orient.core.metadata.schema.OProperty;
import com.orientechnologies.orient.core.record.impl.ODocument;
Expand Down Expand Up @@ -85,6 +87,9 @@ protected OClass extractClassFromTarget(String iTarget) {
// REMOVE CLASS PREFIX
iTarget = iTarget.substring(OCommandExecutorSQLAbstract.CLASS_PREFIX.length());

if (iTarget.charAt(0) == ORID.PREFIX)
return getDatabase().getMetadata().getSchema().getClassByClusterId(new ORecordId(iTarget).clusterId);

return getDatabase().getMetadata().getSchema().getClass(iTarget);
}
return null;
Expand All @@ -100,30 +105,20 @@ protected Object convertValue(OClass iClass, String fieldName, Object v) {
switch (p.getType()) {
case EMBEDDED:
// CONVERT MAP IN DOCUMENTS ASSIGNING THE CLASS TAKEN FROM SCHEMA
if (v instanceof Map) {

final ODocument doc = new ODocument();
if (embeddedType != null)
doc.setClassName(embeddedType.getName());

doc.fromMap((Map<String, Object>) v);
v = doc;
}
if (v instanceof Map)
v = createDocumentFromMap(embeddedType, (Map<String, Object>) v);
break;

case EMBEDDEDSET:
// CONVERT MAPS IN DOCUMENTS ASSIGNING THE CLASS TAKEN FROM SCHEMA
if (!(v instanceof Map) && OMultiValue.isMultiValue(v)) {
if (v instanceof Map)
return createDocumentFromMap(embeddedType, (Map<String, Object>) v);
else if (OMultiValue.isMultiValue(v)) {
final Set set = new HashSet();

for (Object o : OMultiValue.getMultiValueIterable(v)) {
if (o instanceof Map) {
final ODocument doc = new ODocument();
if (embeddedType != null)
doc.setClassName(embeddedType.getName());

doc.fromMap((Map<String, Object>) o);

final ODocument doc = createDocumentFromMap(embeddedType, (Map<String, Object>) o);
set.add(doc);
} else if (o instanceof OIdentifiable)
set.add(((OIdentifiable) o).getRecord());
Expand All @@ -137,17 +132,14 @@ protected Object convertValue(OClass iClass, String fieldName, Object v) {

case EMBEDDEDLIST:
// CONVERT MAPS IN DOCUMENTS ASSIGNING THE CLASS TAKEN FROM SCHEMA
if (!(v instanceof Map) && OMultiValue.isMultiValue(v)) {
if (v instanceof Map)
return createDocumentFromMap(embeddedType, (Map<String, Object>) v);
else if (OMultiValue.isMultiValue(v)) {
final List set = new ArrayList();

for (Object o : OMultiValue.getMultiValueIterable(v)) {
if (o instanceof Map) {
final ODocument doc = new ODocument();
if (embeddedType != null)
doc.setClassName(embeddedType.getName());

doc.fromMap((Map<String, Object>) o);

final ODocument doc = createDocumentFromMap(embeddedType, (Map<String, Object>) o);
set.add(doc);
} else if (o instanceof OIdentifiable)
set.add(((OIdentifiable) o).getRecord());
Expand All @@ -166,12 +158,7 @@ protected Object convertValue(OClass iClass, String fieldName, Object v) {

for (Map.Entry<String, Object> entry : ((Map<String, Object>) v).entrySet()) {
if (entry.getValue() instanceof Map) {
final ODocument doc = new ODocument();
if (embeddedType != null)
doc.setClassName(embeddedType.getName());

doc.fromMap((Map<String, Object>) entry.getValue());

final ODocument doc = createDocumentFromMap(embeddedType, (Map<String, Object>) entry.getValue());
map.put(entry.getKey(), doc);
} else if (entry.getValue() instanceof OIdentifiable)
map.put(entry.getKey(), ((OIdentifiable) entry.getValue()).getRecord());
Expand All @@ -188,6 +175,15 @@ protected Object convertValue(OClass iClass, String fieldName, Object v) {
return v;
}

private ODocument createDocumentFromMap(OClass embeddedType, Map<String, Object> o) {
final ODocument doc = new ODocument();
if (embeddedType != null)
doc.setClassName(embeddedType.getName());

doc.fromMap(o);
return doc;
}

@Override
public long getDistributedTimeout() {
return OGlobalConfiguration.DISTRIBUTED_COMMAND_TASK_SYNCH_TIMEOUT.getValueAsLong();
Expand Down
Expand Up @@ -646,7 +646,7 @@ private Object extractValue(ODocument record, OPair<String, Object> entry) {
else if (value instanceof OCommandRequest)
value = ((OCommandRequest) value).execute(record, null, context);

if (value instanceof OIdentifiable)
if (value instanceof OIdentifiable && ((OIdentifiable) value).getIdentity().isPersistent())
// USE ONLY THE RID TO AVOID CONCURRENCY PROBLEM WITH OLD VERSIONS
value = ((OIdentifiable) value).getIdentity();
return value;
Expand Down
Expand Up @@ -419,8 +419,9 @@ public void testAutoConversionOfEmbeddededListNoLinkedClass() {

public void testAutoConversionOfEmbeddededListWithLinkedClass() {
OClass c = database.getMetadata().getSchema().getOrCreateClass("TestConvert");
c.createProperty("embeddedListWithLinkedClass", OType.EMBEDDEDLIST,
database.getMetadata().getSchema().getOrCreateClass("TestConvertLinkedClass"));
if (!c.existsProperty("embeddedListWithLinkedClass"))
c.createProperty("embeddedListWithLinkedClass", OType.EMBEDDEDLIST,
database.getMetadata().getSchema().getOrCreateClass("TestConvertLinkedClass"));

ODocument doc = database
.command(
Expand Down Expand Up @@ -537,14 +538,9 @@ public void testInsertEmbeddedWithRecordAttributes() {
database.getMetadata().getSchema().getOrCreateClass("EmbeddedWithRecordAttributes_Like"));

ODocument doc = database.command(
new OCommandSQL(
"INSERT INTO EmbeddedWithRecordAttributes SET `like` = { \n"
+ " count: 0, \n"
+ " latest: [], \n"
+ " '@type': 'document', \n"
+ " '@class': 'EmbeddedWithRecordAttributes_Like'\n"
+ " } "))
.execute();
new OCommandSQL("INSERT INTO EmbeddedWithRecordAttributes SET `like` = { \n" + " count: 0, \n"
+ " latest: [], \n" + " '@type': 'document', \n" + " '@class': 'EmbeddedWithRecordAttributes_Like'\n"
+ " } ")).execute();

Assert.assertTrue(doc.field("like") instanceof OIdentifiable);
Assert.assertEquals(((ODocument) doc.field("like")).getClassName(), "EmbeddedWithRecordAttributes_Like");
Expand All @@ -557,21 +553,15 @@ public void testInsertEmbeddedWithRecordAttributes2() {
database.getMetadata().getSchema().getOrCreateClass("EmbeddedWithRecordAttributes2_Like"));

ODocument doc = database.command(
new OCommandSQL(
"INSERT INTO EmbeddedWithRecordAttributes2 SET `like` = { \n"
+ " count: 0, \n"
+ " latest: [], \n"
+ " @type: 'document', \n"
+ " @class: 'EmbeddedWithRecordAttributes2_Like'\n"
+ " } "))
.execute();
new OCommandSQL("INSERT INTO EmbeddedWithRecordAttributes2 SET `like` = { \n" + " count: 0, \n"
+ " latest: [], \n" + " @type: 'document', \n" + " @class: 'EmbeddedWithRecordAttributes2_Like'\n"
+ " } ")).execute();

Assert.assertTrue(doc.field("like") instanceof OIdentifiable);
Assert.assertEquals(((ODocument) doc.field("like")).getClassName(), "EmbeddedWithRecordAttributes2_Like");
Assert.assertEquals(((ODocument) doc.field("like")).field("count"), 0);
}


private List<Long> getValidPositions(int clusterId) {
final List<Long> positions = new ArrayList<Long>();

Expand Down
Expand Up @@ -21,6 +21,7 @@
import com.orientechnologies.orient.core.iterator.ORecordIteratorCluster;
import com.orientechnologies.orient.core.metadata.schema.OClass;
import com.orientechnologies.orient.core.metadata.schema.OSchema;
import com.orientechnologies.orient.core.metadata.schema.OType;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.sql.OCommandSQL;
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;
Expand Down Expand Up @@ -559,4 +560,41 @@ public void testMultiplePut() {
Assert.assertTrue(v.field("embmap") instanceof Map);
Assert.assertEquals(((Map) v.field("embmap")).size(), 2);
}

public void testAutoConversionOfEmbeddededListWithLinkedClass() {
OClass c = database.getMetadata().getSchema().getOrCreateClass("TestConvert");
if (!c.existsProperty("embeddedListWithLinkedClass"))
c.createProperty("embeddedListWithLinkedClass", OType.EMBEDDEDLIST,
database.getMetadata().getSchema().getOrCreateClass("TestConvertLinkedClass"));

ODocument doc = database
.command(
new OCommandSQL(
"INSERT INTO TestConvert SET name = 'embeddedListWithLinkedClass', embeddedListWithLinkedClass = [{'line1':'123 Fake Street'}]"))
.execute();

database.command(
new OCommandSQL("UPDATE " + doc.getIdentity() + " ADD embeddedListWithLinkedClass = [{'line1':'123 Fake Street'}]"))
.execute();

doc.reload();

Assert.assertTrue(doc.field("embeddedListWithLinkedClass") instanceof List);
Assert.assertEquals(((Collection) doc.field("embeddedListWithLinkedClass")).size(), 2);

database.command(
new OCommandSQL("UPDATE " + doc.getIdentity() + " ADD embeddedListWithLinkedClass = {'line1':'123 Fake Street'}"))
.execute();

doc.reload();

Assert.assertTrue(doc.field("embeddedListWithLinkedClass") instanceof List);
Assert.assertEquals(((Collection) doc.field("embeddedListWithLinkedClass")).size(), 3);

List addr = doc.field("embeddedListWithLinkedClass");
for (Object o : addr) {
Assert.assertTrue(o instanceof ODocument);
Assert.assertEquals(((ODocument) o).getClassName(), "TestConvertLinkedClass");
}
}
}

0 comments on commit d29612b

Please sign in to comment.