Skip to content

Commit d5b8fb6

Browse files
committed
Almost there, it currently only fails the bracket in strings test
1 parent 12c6a87 commit d5b8fb6

2 files changed

Lines changed: 89 additions & 47 deletions

File tree

src/main/java/picoded/dstack/mongodb/MongoDB_DataObjectMap.java

Lines changed: 55 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import picoded.core.conv.ConvertJSON;
1616
import picoded.core.conv.GenericConvert;
1717
import picoded.core.conv.NestedObjectFetch;
18+
import picoded.core.conv.NestedObjectUtil;
1819
import picoded.core.conv.StringEscape;
1920
import picoded.core.struct.query.Query;
2021
import picoded.core.common.ObjectToken;
@@ -23,6 +24,7 @@
2324

2425
// MongoDB imports
2526
import org.bson.Document;
27+
import org.bson.types.Binary;
2628
import com.mongodb.client.*;
2729
import com.mongodb.client.model.Projections;
2830
import com.mongodb.client.model.Filters;
@@ -155,11 +157,43 @@ public Map<String, Object> DataObjectRemoteDataMap_get(String _oid) {
155157
// Get the find result
156158
FindIterable<Document> res = collection.find(Filters.eq("_oid", _oid));
157159

158-
// Export the data without _id
159-
res = res.projection(Projections.excludeId());
160+
// Get the Document object
161+
Document resObj = res.first();
162+
if (resObj == null) {
163+
return null;
164+
}
165+
166+
// The return object
167+
Map<String, Object> ret = new HashMap<>();
168+
169+
// Lets iterate through the object
170+
Set<String> fullKeys = resObj.keySet();
171+
for (String key : fullKeys) {
172+
// Skip the _id reserved col
173+
if (key.equalsIgnoreCase("_id")) {
174+
continue;
175+
}
176+
177+
// Get the value
178+
Object val = resObj.get(key);
179+
180+
// Unwrap the binary type
181+
if (val instanceof Binary) {
182+
val = ((Binary) val).getData();
183+
}
184+
185+
// Populate the ret map
186+
ret.put(key, val);
187+
}
188+
189+
// Neither of this works to resolve the mssqlOuterBracket issue
190+
// Looks like we need to properly reimplement the query support
191+
//
192+
// NestedObjectUtil.repackFullyQualifiedNameKeys(ret);
193+
// NestedObjectUtil.unpackFullyQualifiedNameKeys(ret);
160194

161-
// Return the first document (if any)
162-
return res.first();
195+
// Return the full map
196+
return ret;
163197
}
164198

165199
/**
@@ -180,41 +214,47 @@ public void DataObjectRemoteDataMap_update(String _oid, Map<String, Object> full
180214
Document set_doc = new Document();
181215
Document setOnInsert_doc = new Document();
182216
Document unset_doc = new Document();
183-
217+
184218
// Lets iterate the keys, and decide accordingly
185219
Set<String> fullKeys = fullMap.keySet();
186-
for( String key : fullKeys ) {
220+
for (String key : fullKeys) {
187221
// Get the value
188222
Object value = fullMap.get(key);
189-
223+
190224
// Special _oid handling
191-
if( key.equals("_oid") ) {
225+
if (key.equals("_oid")) {
192226
setOnInsert_doc.append("_oid", _oid);
193227
continue;
194228
}
195-
229+
230+
// Wrap the binary values for the BSON format
231+
// if needed
232+
if (value instanceof byte[]) {
233+
value = new Binary((byte[]) value);
234+
}
235+
196236
// Lets apply the update values
197-
if( updateKeys.contains(key) ) {
237+
if (updateKeys.contains(key)) {
198238
// Handle NULL values unset
199-
if( value == null || value == ObjectToken.NULL ) {
239+
if (value == null || value == ObjectToken.NULL) {
200240
unset_doc.append(key, "");
201241
continue;
202242
}
203-
243+
204244
// Handle values update
205245
set_doc.append(key, value);
206246
continue;
207247
}
208-
248+
209249
// OK - this is not in the update dataset
210250
// meaning we should do a "setOnInsert" if its not null
211-
if( value == null || value == ObjectToken.NULL ) {
251+
if (value == null || value == ObjectToken.NULL) {
212252
// does nothing
213253
} else {
214254
setOnInsert_doc.append(key, value);
215255
}
216256
}
217-
257+
218258
// Generate the "update" doc
219259
Document updateDoc = new Document();
220260
updateDoc.append("$set", set_doc);

src/test/java/picoded/dstack/mongodb/MongoDB_conn_test.java

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,16 @@ public class MongoDB_conn_test {
3535

3636
@Test
3737
public void connectivityTest() {
38-
38+
3939
//-------------------------------------
4040
// Connectivity Setup
4141
//-------------------------------------
42-
42+
4343
// Get the full_url
44-
String full_url = "mongodb://"+DStackTestConfig.MONGODB_HOST()+":"+DStackTestConfig.MONGODB_PORT()+"/testdb"+"?r=majority&w=majority&retryWrites=true&maxPoolSize=50";
45-
44+
String full_url = "mongodb://" + DStackTestConfig.MONGODB_HOST() + ":"
45+
+ DStackTestConfig.MONGODB_PORT() + "/testdb"
46+
+ "?r=majority&w=majority&retryWrites=true&maxPoolSize=50";
47+
4648
// Lets build using the stable API settings
4749
ServerApi serverApi = ServerApi.builder().version(ServerApiVersion.V1).build();
4850
MongoClientSettings settings = MongoClientSettings.builder()
@@ -51,15 +53,15 @@ public void connectivityTest() {
5153
// The mongodb client
5254
MongoClient client = MongoClients.create(settings);
5355
assertNotNull(client);
54-
56+
5557
// The mongodb database
5658
MongoDatabase database = client.getDatabase("testdb");
5759
assertNotNull(database);
58-
60+
5961
// The mongodb collection
6062
MongoCollection<Document> collection = database.getCollection("testcollection");
6163
assertNotNull(collection);
62-
64+
6365
// Unique _oid index setup
6466
IndexOptions opt = new IndexOptions();
6567
opt = opt.unique(true);
@@ -69,7 +71,7 @@ public void connectivityTest() {
6971
//-------------------------------------
7072
// Data cleanup
7173
//-------------------------------------
72-
74+
7375
// Remove any stale data from previous test
7476

7577
// Delete all items
@@ -83,92 +85,92 @@ public void connectivityTest() {
8385
Filters.exists("_oid", false) //
8486
) //
8587
); //
86-
88+
8789
//-------------------------------------
8890
// C : Create the first document
8991
//-------------------------------------
90-
92+
9193
// Generate the document to first "insert"
9294
Document doc = new Document();
9395
doc.put("hello", "world");
9496
doc.put("_oid", "001");
95-
97+
9698
// Lets insert the first document
9799
collection.insertOne(doc);
98-
100+
99101
//-------------------------------------
100102
// R : Read the first document
101103
//-------------------------------------
102-
104+
103105
// Get the find result
104106
FindIterable<Document> findRes = collection.find(Filters.eq("_oid", "001"));
105-
assertNotNull( findRes );
107+
assertNotNull(findRes);
106108

107109
// Export the data without native mongodb '_id'
108110
findRes = findRes.projection(Projections.excludeId());
109111

110112
// Get the document (as a map???)
111113
// in theory its possible, because a Document, is a Map
112-
Map<String,Object> resMap = findRes.first();
113-
assertNotNull( resMap );
114-
114+
Map<String, Object> resMap = findRes.first();
115+
assertNotNull(resMap);
116+
115117
// Lets validate the values
116118
assertEquals("001", resMap.get("_oid"));
117119
assertEquals("world", resMap.get("hello"));
118120
// Ensure the native mongodb '_id' is scrubbed out
119121
assertEquals(null, resMap.get("_id"));
120-
122+
121123
//-------------------------------------
122124
// U : Update the first document
123125
//-------------------------------------
124-
126+
125127
// Generate the details, that needs updating
126128
//
127129
// See: https://www.mongodb.com/docs/manual/reference/operator/update/set/
128130
// for how to use the $set operator
129131
doc = new Document();
130132
doc.append("messsage", "the world is both big and small");
131133
// doc.append("_oid", "001");
132-
134+
133135
Document updateDoc = new Document();
134136
updateDoc.put("$set", doc);
135137

136138
// Lets do a find and update
137-
collection.findOneAndUpdate(Filters.eq("_oid","001"), updateDoc);
138-
139+
collection.findOneAndUpdate(Filters.eq("_oid", "001"), updateDoc);
140+
139141
// Lets validate that the changes were made
140142
//----------------------------------------------
141-
143+
142144
// Get the find result
143145
findRes = collection.find(Filters.eq("_oid", "001"));
144-
assertNotNull( findRes );
146+
assertNotNull(findRes);
145147

146148
// Export the data without native mongodb '_id'
147149
findRes = findRes.projection(Projections.excludeId());
148150

149151
// Get the document (as a map?)
150152
resMap = findRes.first();
151-
assertNotNull( resMap );
152-
153+
assertNotNull(resMap);
154+
153155
// Lets validate the values
154156
assertEquals("001", resMap.get("_oid"));
155157
assertEquals("world", resMap.get("hello"));
156158
assertEquals(null, resMap.get("_id"));
157159
assertEquals("the world is both big and small", resMap.get("messsage"));
158-
160+
159161
//-------------------------------------
160162
// D : Delete the document !
161163
//-------------------------------------
162-
164+
163165
// Delete
164166
DeleteResult delRes = collection.deleteOne(Filters.eq("_oid", "001"));
165167
assertEquals(1, delRes.getDeletedCount());
166-
168+
167169
// And validate if it exists
168170
findRes = collection.find(Filters.eq("_oid", "001"));
169171
findRes = findRes.projection(Projections.excludeId());
170-
assertNull( findRes.first() );
171-
172+
assertNull(findRes.first());
173+
172174
}
173-
175+
174176
}

0 commit comments

Comments
 (0)