Skip to content

Commit

Permalink
Fully deserialize documents. Fixes #1
Browse files Browse the repository at this point in the history
  • Loading branch information
zbjornson committed Jan 11, 2016
1 parent a08005e commit 7898255
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 17 deletions.
Binary file modified Java/MongoDBLinkUtils.class
Binary file not shown.
40 changes: 26 additions & 14 deletions Java/src/MongoDBLinkUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,36 @@ public static void Iterate(DBCursor cursor) {
if (link.wasInterrupted()) return;

DBObject elem = cursor.next();
Map<String, Object> elemMap = elem.toMap();
link.putFunction("List", elemMap.size());

for (Map.Entry<String, Object> entry : elemMap.entrySet()) {
link.putFunction("Rule", 2);
link.put(entry.getKey());
Object value = entry.getValue();
if (value instanceof org.bson.types.ObjectId) {
link.put(value.toString());
} else if (value instanceof BasicDBList) {
link.put(((BasicDBList) value).toArray());
} else {
link.put(value);
}
Deserialize(link, elem);
}
} catch (Exception e) {
System.out.println("Exception:" + e.toString());
}
}

private static void Deserialize(KernelLink link, DBObject elem) {
try {

Map<String, Object> elemMap = elem.toMap();
link.putFunction("List", elemMap.size());
for (Map.Entry<String, Object> entry : elemMap.entrySet()) {
link.putFunction("Rule", 2);
link.put(entry.getKey());
Object value = entry.getValue();
if (value instanceof org.bson.types.ObjectId) {
link.put(value.toString());
} else if (value instanceof BasicDBList) {
link.put(((BasicDBList) value).toArray());
} else if (value instanceof BasicDBObject) {
Deserialize(link, (BasicDBObject) value);
} else {
link.put(value);
}
}

} catch (Exception e) {
System.out.println("Exception:" + e.toString());
}
}

}
4 changes: 2 additions & 2 deletions MongoDBLink.m
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@

serialize[{}] := JavaNew["com.mongodb.BasicDBObject"]

serialize[_, x : $rulepattern] := JavaBlock@Block[
serialize[_, x : $rulepattern] := Block[
{newdbobj = JavaNew["com.mongodb.BasicDBObject"]},
Map[serialize[newdbobj, #] &, x];
newdbobj
Expand Down Expand Up @@ -322,7 +322,7 @@
iFindDocuments[collection, query, projection, offset, limit]
]

iFindDocuments[collection_Collection, query_, projection_, offset_, limitValue_] := JavaBlock@Block[
iFindDocuments[collection_Collection, query_, projection_, offset_, limitValue_] := (*JavaBlock@*)Block[
{
serializedQuery,
serializedProjection,
Expand Down
11 changes: 10 additions & 1 deletion Tests.wlt
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,15 @@ VerificationTest[
TestID->"494fa114-f280-49de-b9fe-b10ab903ab67"
]

VerificationTest[
collection2 = GetCollection[database, "test2"];
InsertDocument[collection2, {"a" -> {"b" -> {"c" -> 1}, "d" -> {"e" -> 1}}}];

MatchQ[FindDocuments[collection2], {{"_id" -> _, "a" -> {"b" -> {"c" -> 1}, "d" -> {"e" -> 1}}}}],
True,
TestID->"b2a0406c-42f0-465d-aaf8-a7f469922704"
]

EndTestSection[]

BeginTestSection["FindDistinct"]
Expand Down Expand Up @@ -400,6 +409,6 @@ VerificationTest[
TestID->"54141c89-9157-4911-b37a-5e95d34f7d28"
]

Clear[connection, database, collection1];
Clear[connection, database, collection1, collection2];

EndTestSection[]

0 comments on commit 7898255

Please sign in to comment.