Debian Codegraph @

ogr/ogrsf_frmts/mongodbv3/ogrmongodbv3driver.cpp

raw
1 /******************************************************************************
2 *
3 * Project: MongoDB Translator
4 * Purpose: Implements OGRMongoDBDriver.
5 * Author: Even Rouault, even.rouault at spatialys.com
6 *
7 ******************************************************************************
8 * Copyright (c) 2014-2019, Even Rouault <even dot rouault at spatialys dot com>
9 *
10 * SPDX-License-Identifier: MIT
11 ****************************************************************************/
12
13 #include "mongocxxv3_headers.h"
14 #include "ogrmongodbv3drivercore.h"
15
16 #include "cpl_time.h"
17 #include "gdal_priv.h"
18 #include "ogrsf_frmts.h"
19 #include "ogr_p.h"
20
21 #include <limits>
22 #include <map>
23 #include <memory>
24 #include <mutex>
25 #include <vector>
26
27 extern "C" void CPL_DLL RegisterOGRMongoDBv3();
28
29 // g++ -Wall -Wextra -std=c++11 -fPIC -Iport -Igcore -Iogr -Iogr/ogrsf_frmts
30 // -I$HOME/install-mongocxx-3.4.0/include/bsoncxx/v_noabi
31 // -I$HOME/install-mongocxx-3.4.0/include/mongocxx/v_noabi
32 // ogr/ogrsf_frmts/mongodbv3/ogrmongodbv3driver.cpp -shared -o ogr_MongoDBv3.so
33 // -L$HOME/install-mongocxx-3.4.0/lib -lmongocxx -lgdal
34
35 using bsoncxx::builder::basic::kvp;
36
37 static mongocxx::instance *g_pInst = nullptr;
38 static bool g_bCanInstantiateMongo = true;
39
40 namespace
41 {
42 typedef struct _IntOrMap IntOrMap;
43
44 struct _IntOrMap
45 {
46 // cppcheck-suppress unusedStructMember
47 int bIsMap;
48
49 union
50 {
51 // cppcheck-suppress unusedStructMember
52 int nField;
53 // cppcheck-suppress unusedStructMember
54 std::map<CPLString, IntOrMap *> *poMap;
55 // cppcheck-suppress unusedStructMember
56 } u;
57 };
58 } // namespace
59
60 class OGRMongoDBv3Layer;
61
62 class OGRMongoDBv3Dataset final : public GDALDataset
63 {
64 friend class OGRMongoDBv3Layer;
65
66 mongocxx::client m_oConn{};
67 CPLString m_osDatabase{};
68 std::vector<std::unique_ptr<OGRMongoDBv3Layer>> m_apoLayers{};
69 bool m_bFlattenNestedAttributes = false;
70 int m_nBatchSize = 0;
71 int m_nFeatureCountToEstablishFeatureDefn = 0;
72 bool m_bJSonField = false;
73 CPLString m_osFID{};
74 bool m_bUseOGRMetadata = true;
75 bool m_bBulkInsert = true;
76
77 void CreateLayers(mongocxx::database &db);
78
79 public:
80 OGRMongoDBv3Dataset() = default;
81
82 int GetLayerCount() const override
83 {
84 return static_cast<int>(m_apoLayers.size());
85 }
86
87 const OGRLayer *GetLayer(int) const override;
88 OGRLayer *GetLayerByName(const char *pszLayerName) override;
89
90 OGRLayer *ExecuteSQL(const char *pszSQLCommand,
91 OGRGeometry *poSpatialFilter,
92 const char *pszDialect) override;
93 void ReleaseResultSet(OGRLayer *poLayer) override;
94
95 OGRLayer *ICreateLayer(const char *pszName,
96 const OGRGeomFieldDefn *poGeomFieldDefn,
97 CSLConstList papszOptions) override;
98 OGRErr DeleteLayer(int iLayer) override;
99 int TestCapability(const char *pszCap) const override;
100
101 bool Open(GDALOpenInfo *poOpenInfo);
102 };
103
104 class OGRMongoDBv3Layer final : public OGRLayer
105 {
106 friend class OGRMongoDBv3Dataset;
107
108 OGRMongoDBv3Dataset *m_poDS = nullptr;
109 OGRFeatureDefn *m_poFeatureDefn = nullptr;
110 bool m_bHasEstablishedFeatureDefn = false;
111 mongocxx::database m_oDb{};
112 mongocxx::collection m_oColl{};
113 CPLString m_osFID{};
114 bsoncxx::document::value m_oQueryAttr{
115 bsoncxx::builder::basic::make_document()};
116 bsoncxx::document::value m_oQuerySpat{
117 bsoncxx::builder::basic::make_document()};
118 bool m_bLayerMetadataUpdatable = false;
119 bool m_bUpdateLayerMetadata = false;
120 bool m_bDotAsNestedField = true;
121 bool m_bIgnoreSourceID = false;
122 bool m_bCreateSpatialIndex = true;
123 GIntBig m_nIndex = 0;
124 GIntBig m_nNextFID = 0;
125 std::unique_ptr<mongocxx::cursor> m_poCursor{};
126 std::unique_ptr<mongocxx::cursor::iterator> m_poIterator{};
127
128 std::vector<std::vector<CPLString>> m_aaosFieldPaths{};
129
130 std::vector<std::vector<CPLString>> m_aaosGeomFieldPaths{};
131 std::vector<CPLString> m_aosGeomIndexes{};
132 std::vector<std::unique_ptr<OGRCoordinateTransformation>> m_apoCT{};
133
134 std::map<CPLString, CPLString> CollectGeomIndices();
135 bool ReadOGRMetadata(std::map<CPLString, CPLString> &oMapIndices);
136 void EstablishFeatureDefn();
137 void WriteOGRMetadata();
138 void AddOrUpdateField(const char *pszAttrName,
139 const bsoncxx::document::element &elt,
140 char chNestedAttributeSeparator,
141 std::vector<CPLString> &aosPaths,
142 std::map<CPLString, CPLString> &oMapIndices);
143 std::unique_ptr<OGRFeature> Translate(const bsoncxx::document::view &doc);
144 bsoncxx::document::value BuildQuery();
145
146 OGRErr PrepareForUpdateOrUpsert(const OGRFeature *poFeature);
147 bsoncxx::document::value
148 BuildIDMatchFilter(bsoncxx::document::view view,
149 const OGRFeature *poFeature) const;
150
151 void SerializeField(bsoncxx::builder::basic::document &b,
152 OGRFeature *poFeature, int iField,
153 const char *pszJSonField);
154 void SerializeGeometry(bsoncxx::builder::basic::document &b,
155 OGRGeometry *poGeom, int iField,
156 const char *pszJSonField);
157 void SerializeRecursive(bsoncxx::builder::basic::document &b,
158 OGRFeature *poFeature,
159 std::map<CPLString, IntOrMap *> &aoMap);
160 static void InsertInMap(IntOrMap *rootMap,
161 std::map<std::vector<CPLString>, IntOrMap *> &aoMap,
162 const std::vector<CPLString> &aosFieldPathFull,
163 int nField);
164 bsoncxx::document::value BuildBSONObjFromFeature(
165 OGRFeature *poFeature, bool bUpdate, int nUpdatedFieldsCount,
166 const int *panUpdatedFieldsIdx, int nUpdatedGeomFieldsCount,
167 const int *panUpdatedGeomFieldsIdx);
168 std::vector<bsoncxx::document::value> m_aoDocsToInsert{};
169
170 public:
171 OGRMongoDBv3Layer(OGRMongoDBv3Dataset *poDS, const std::string &osDbName,
172 const std::string &osCollection);
173 ~OGRMongoDBv3Layer() override;
174
175 void ResetReading() override;
176 const char *GetFIDColumn() const override;
177 OGRFeature *GetNextFeature() override;
178 OGRFeature *GetFeature(GIntBig nFID) override;
179 OGRErr DeleteFeature(GIntBig nFID) override;
180 GIntBig GetFeatureCount(int bForce) override;
181 OGRErr SetAttributeFilter(const char *pszFilter) override;
182
183 OGRErr ISetSpatialFilter(int iGeomField,
184 const OGRGeometry *poGeom) override;
185 int TestCapability(const char *pszCap) const override;
186 const OGRFeatureDefn *GetLayerDefn() const override;
187 OGRErr CreateField(const OGRFieldDefn *poFieldIn, int) override;
188 OGRErr CreateGeomField(const OGRGeomFieldDefn *poFieldIn, int) override;
189 OGRErr ICreateFeature(OGRFeature *poFeature) override;
190 OGRErr ISetFeature(OGRFeature *poFeature) override;
191 OGRErr IUpsertFeature(OGRFeature *poFeature) override;
192 OGRErr IUpdateFeature(OGRFeature *poFeature, int nUpdatedFieldsCount,
193 const int *panUpdatedFieldsIdx,
194 int nUpdatedGeomFieldsCount,
195 const int *panUpdatedGeomFieldsIdx,
196 bool bUpdateStyleString) override;
197
198 OGRErr SyncToDisk() override;
199
200 GDALDataset *GetDataset() override
201 {
202 return m_poDS;
203 }
204 };
205
206 /************************************************************************/
207 /* OGRMongoDBv3Layer() */
208 /************************************************************************/
209
210 OGRMongoDBv3Layer::OGRMongoDBv3Layer(OGRMongoDBv3Dataset *poDS,
211 const std::string &osDbName,
212 const std::string &osCollection)
213 : m_poDS(poDS)
214 {
215 CPLString osLayerName;
216 m_oDb = m_poDS->m_oConn[osDbName];
217 m_oColl = m_oDb[osCollection];
218 if (m_poDS->m_osDatabase == osDbName)
219 osLayerName = osCollection;
220 else
221 osLayerName = osDbName + "." + osCollection;
222 m_poFeatureDefn = new OGRFeatureDefn(osLayerName);
223 m_poFeatureDefn->Reference();
224 m_poFeatureDefn->SetGeomType(wkbNone);
225 SetDescription(m_poFeatureDefn->GetName());
226
227 OGRFieldDefn oFieldDefn("_id", OFTString);
228 std::vector<CPLString> aosPath;
229 aosPath.push_back("_id");
230 m_aaosFieldPaths.push_back(aosPath);
231 m_poFeatureDefn->AddFieldDefn(&oFieldDefn);
232 }
233
234 /************************************************************************/
235 /* ~OGRMongoDBv3Layer() */
236 /************************************************************************/
237
238 OGRMongoDBv3Layer::~OGRMongoDBv3Layer()
239 {
240 OGRMongoDBv3Layer::SyncToDisk();
241
242 if (m_poFeatureDefn)
243 m_poFeatureDefn->Release();
244 }
245
246 /************************************************************************/
247 /* WriteOGRMetadata() */
248 /************************************************************************/
249
250 void OGRMongoDBv3Layer::WriteOGRMetadata()
251 {
252 // CPLDebug("MongoDBv3", "WriteOGRMetadata(%s)",
253 // m_poFeatureDefn->GetName());
254 if (!m_bUpdateLayerMetadata)
255 return;
256 m_bUpdateLayerMetadata = false;
257
258 try
259 {
260 bsoncxx::builder::basic::document b;
261
262 b.append(kvp("layer", m_oColl.name()));
263
264 if (!m_osFID.empty())
265 {
266 b.append(kvp("fid", m_osFID));
267 }
268
269 bsoncxx::builder::basic::array fields;
270
271 CPLAssert(static_cast<int>(m_aaosFieldPaths.size()) ==
272 m_poFeatureDefn->GetFieldCount());
273 for (int i = 1; i < m_poFeatureDefn->GetFieldCount(); i++)
274 {
275 OGRFieldDefn *poFieldDefn = m_poFeatureDefn->GetFieldDefn(i);
276 const char *pszFieldName = poFieldDefn->GetNameRef();
277 if (EQUAL(pszFieldName, "_json"))
278 continue;
279 bsoncxx::builder::basic::array path;
280 for (int j = 0; j < static_cast<int>(m_aaosFieldPaths[i].size());
281 j++)
282 path.append(m_aaosFieldPaths[i][j]);
283 bsoncxx::builder::basic::document rec;
284 rec.append(kvp("name", pszFieldName));
285 OGRFieldType eType = poFieldDefn->GetType();
286 rec.append(kvp("type", OGR_GetFieldTypeName(eType)));
287 if (eType == OFTInteger && poFieldDefn->GetSubType() == OFSTBoolean)
288 {
289 rec.append(kvp("subtype", "Boolean"));
290 }
291 rec.append(kvp("path", path.extract()));
292 fields.append(rec.extract());
293 }
294 b.append(kvp("fields", fields.extract()));
295
296 bsoncxx::builder::basic::array geomfields;
297 CPLAssert(static_cast<int>(m_aaosGeomFieldPaths.size()) ==
298 m_poFeatureDefn->GetGeomFieldCount());
299 for (int i = 0; i < m_poFeatureDefn->GetGeomFieldCount(); i++)
300 {
301 OGRGeomFieldDefn *poGeomFieldDefn =
302 m_poFeatureDefn->GetGeomFieldDefn(i);
303 const char *pszFieldName = poGeomFieldDefn->GetNameRef();
304 bsoncxx::builder::basic::array path;
305 for (int j = 0;
306 j < static_cast<int>(m_aaosGeomFieldPaths[i].size()); j++)
307 path.append(m_aaosGeomFieldPaths[i][j]);
308 const char *pszGeomType =
309 OGRToOGCGeomType(poGeomFieldDefn->GetType());
310 bsoncxx::builder::basic::document rec;
311 rec.append(kvp("name", pszFieldName));
312 rec.append(kvp("type", pszGeomType));
313 rec.append(kvp("path", path.extract()));
314 geomfields.append(rec.extract());
315 }
316 b.append(kvp("geomfields", geomfields.extract()));
317
318 {
319 bsoncxx::builder::basic::document filter{};
320 filter.append(kvp("layer", m_oColl.name()));
321 m_oDb["_ogr_metadata"].find_one_and_delete(filter.extract());
322 }
323
324 m_oDb["_ogr_metadata"].insert_one(b.extract());
325 }
326 catch (const std::exception &ex)
327 {
328 CPLError(CE_Failure, CPLE_AppDefined, "%s: %s", "WriteOGRMetadata()",
329 ex.what());
330 }
331 }
332
333 /************************************************************************/
334 /* SyncToDisk() */
335 /************************************************************************/
336
337 OGRErr OGRMongoDBv3Layer::SyncToDisk()
338 {
339 try
340 {
341 if (!m_aoDocsToInsert.empty())
342 {
343 m_oColl.insert_many(m_aoDocsToInsert.begin(),
344 m_aoDocsToInsert.end());
345 m_aoDocsToInsert.clear();
346 }
347 }
348 catch (const std::exception &ex)
349 {
350 m_aoDocsToInsert.clear();
351 CPLError(CE_Failure, CPLE_AppDefined, "%s: %s", "CreateFeature()",
352 ex.what());
353 return OGRERR_FAILURE;
354 }
355
356 WriteOGRMetadata();
357
358 return OGRERR_NONE;
359 }
360
361 /************************************************************************/
362 /* ResetReading() */
363 /************************************************************************/
364
365 void OGRMongoDBv3Layer::ResetReading()
366 {
367 m_poCursor.reset();
368 m_poIterator.reset();
369 m_nIndex = 0;
370 }
371
372 /************************************************************************/
373 /* GetLayerDefn() */
374 /************************************************************************/
375
376 const OGRFeatureDefn *OGRMongoDBv3Layer::GetLayerDefn() const
377 {
378 if (!m_bHasEstablishedFeatureDefn)
379 const_cast<OGRMongoDBv3Layer *>(this)->EstablishFeatureDefn();
380
381 return m_poFeatureDefn;
382 }
383
384 /************************************************************************/
385 /* OGRMongoDBv3GetFieldTypeFromBSON() */
386 /************************************************************************/
387
388 static OGRFieldType
389 OGRMongoDBv3GetFieldTypeFromBSON(const bsoncxx::document::element &elt,
390 OGRFieldSubType &eSubType)
391 {
392 eSubType = OFSTNone;
393
394 auto eBSONType = elt.type();
395 if (eBSONType == bsoncxx::type::k_bool)
396 {
397 eSubType = OFSTBoolean;
398 return OFTInteger;
399 }
400 else if (eBSONType == bsoncxx::type::k_double)
401 return OFTReal;
402 else if (eBSONType == bsoncxx::type::k_int32)
403 return OFTInteger;
404 else if (eBSONType == bsoncxx::type::k_int64)
405 return OFTInteger64;
406 #if BSONCXX_VERSION_MAJOR >= 4
407 else if (eBSONType == bsoncxx::type::k_string)
408 #else
409 else if (eBSONType == bsoncxx::type::k_utf8)
410 #endif
411 return OFTString;
412 else if (eBSONType == bsoncxx::type::k_array)
413 {
414 auto arrayView = elt.get_array().value;
415 if (arrayView.empty())
416 return OFTStringList; /* we don't know, so let's assume it is a
417 string list */
418 OGRFieldType eType = OFTIntegerList;
419 bool bOnlyBoolean = true;
420 for (auto &&subElt : arrayView)
421 {
422 eBSONType = subElt.type();
423
424 bOnlyBoolean &= (eBSONType == bsoncxx::type::k_bool);
425 if (eBSONType == bsoncxx::type::k_double)
426 eType = OFTRealList;
427 else if (eType == OFTIntegerList &&
428 eBSONType == bsoncxx::type::k_int64)
429 eType = OFTInteger64List;
430 else if (eBSONType != bsoncxx::type::k_int32 &&
431 eBSONType != bsoncxx::type::k_int64 &&
432 eBSONType != bsoncxx::type::k_bool)
433 return OFTStringList;
434 }
435 if (bOnlyBoolean)
436 eSubType = OFSTBoolean;
437 return eType;
438 }
439 else if (eBSONType == bsoncxx::type::k_date)
440 return OFTDateTime;
441 else if (eBSONType == bsoncxx::type::k_binary)
442 return OFTBinary;
443 else
444 return OFTString; /* null, object */
445 }
446
447 /************************************************************************/
448 /* get_string() */
449 /************************************************************************/
450
451 static std::string get_string(const bsoncxx::document::element &elt)
452 {
453 #if BSONCXX_VERSION_MAJOR >= 4
454 CPLAssert(elt.type() == bsoncxx::type::k_string);
455 #else
456 CPLAssert(elt.type() == bsoncxx::type::k_utf8);
457 #endif
458 #if BSONCXX_VERSION_MAJOR > 3 || BSONCXX_VERSION_MINOR >= 7
459 return std::string(elt.get_string().value);
460 #else
461 return std::string(elt.get_utf8().value);
462 #endif
463 }
464
465 static std::string get_string(const bsoncxx::array::element &elt)
466 {
467 #if BSONCXX_VERSION_MAJOR >= 4
468 CPLAssert(elt.type() == bsoncxx::type::k_string);
469 #else
470 CPLAssert(elt.type() == bsoncxx::type::k_utf8);
471 #endif
472 #if BSONCXX_VERSION_MAJOR > 3 || BSONCXX_VERSION_MINOR >= 7
473 return std::string(elt.get_string().value);
474 #else
475 return std::string(elt.get_utf8().value);
476 #endif
477 }
478
479 /************************************************************************/
480 /* AddOrUpdateField() */
481 /************************************************************************/
482
483 void OGRMongoDBv3Layer::AddOrUpdateField(
484 const char *pszAttrName, const bsoncxx::document::element &elt,
485 char chNestedAttributeSeparator, std::vector<CPLString> &aosPaths,
486 std::map<CPLString, CPLString> &oMapIndices)
487 {
488 const auto eBSONType = elt.type();
489 if (eBSONType == bsoncxx::type::k_null ||
490 eBSONType == bsoncxx::type::k_undefined ||
491 eBSONType == bsoncxx::type::k_minkey ||
492 eBSONType == bsoncxx::type::k_maxkey)
493 return;
494
495 if (eBSONType == bsoncxx::type::k_document)
496 {
497 bsoncxx::document::view doc{elt.get_document()};
498 auto eltType = doc["type"];
499 #if BSONCXX_VERSION_MAJOR >= 4
500 if (eltType && eltType.type() == bsoncxx::type::k_string)
501 #else
502 if (eltType && eltType.type() == bsoncxx::type::k_utf8)
503 #endif
504 {
505 OGRwkbGeometryType eGeomType =
506 OGRFromOGCGeomType(get_string(eltType).c_str());
507 if (eGeomType != wkbUnknown)
508 {
509 int nIndex = m_poFeatureDefn->GetGeomFieldIndex(pszAttrName);
510 if (nIndex < 0)
511 {
512 OGRGeomFieldDefn fldDefn(pszAttrName, eGeomType);
513 OGRSpatialReference *poSRS = new OGRSpatialReference();
514 poSRS->SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
515 poSRS->SetFromUserInput(SRS_WKT_WGS84_LAT_LONG);
516 fldDefn.SetSpatialRef(poSRS);
517 poSRS->Release();
518 m_poFeatureDefn->AddGeomFieldDefn(&fldDefn);
519
520 aosPaths.push_back(std::string(elt.key()));
521 m_aaosGeomFieldPaths.push_back(aosPaths);
522 if (oMapIndices.find(pszAttrName) == oMapIndices.end())
523 m_aosGeomIndexes.push_back(oMapIndices[pszAttrName]);
524 else
525 m_aosGeomIndexes.push_back("none");
526 m_apoCT.push_back(nullptr);
527 }
528 else
529 {
530 OGRGeomFieldDefn *poFDefn =
531 m_poFeatureDefn->GetGeomFieldDefn(nIndex);
532 if (poFDefn->GetType() != eGeomType)
533 poFDefn->SetType(wkbUnknown);
534 }
535 }
536 }
537 else if (m_poDS->m_bFlattenNestedAttributes)
538 {
539 if (m_poFeatureDefn->GetGeomFieldIndex(pszAttrName) >= 0)
540 return;
541 aosPaths.push_back(std::string(elt.key()));
542 for (auto &&subElt : doc)
543 {
544 char szSeparator[2];
545 szSeparator[0] = chNestedAttributeSeparator;
546 szSeparator[1] = 0;
547 CPLString osAttrName(pszAttrName);
548 osAttrName += szSeparator;
549 osAttrName += std::string(subElt.key());
550
551 std::vector<CPLString> aosNewPaths(aosPaths);
552 AddOrUpdateField(osAttrName, subElt, chNestedAttributeSeparator,
553 aosNewPaths, oMapIndices);
554 }
555 return;
556 }
557 }
558 else if (eBSONType == bsoncxx::type::k_array)
559 {
560 if (m_poFeatureDefn->GetGeomFieldIndex(pszAttrName) >= 0)
561 return;
562 if (oMapIndices.find(pszAttrName) != oMapIndices.end() &&
563 oMapIndices[pszAttrName] == "2d")
564 {
565 OGRGeomFieldDefn fldDefn(pszAttrName, wkbPoint);
566 OGRSpatialReference *poSRS = new OGRSpatialReference();
567 poSRS->SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
568 poSRS->SetFromUserInput(SRS_WKT_WGS84_LAT_LONG);
569 fldDefn.SetSpatialRef(poSRS);
570 poSRS->Release();
571 m_poFeatureDefn->AddGeomFieldDefn(&fldDefn);
572
573 aosPaths.push_back(std::string(elt.key()));
574 m_aaosGeomFieldPaths.push_back(aosPaths);
575 m_aosGeomIndexes.push_back("2d");
576 m_apoCT.push_back(nullptr);
577 }
578 }
579
580 if (m_poFeatureDefn->GetGeomFieldIndex(pszAttrName) >= 0)
581 return;
582
583 OGRFieldSubType eNewSubType;
584 OGRFieldType eNewType = OGRMongoDBv3GetFieldTypeFromBSON(elt, eNewSubType);
585
586 int nIndex = m_poFeatureDefn->GetFieldIndex(pszAttrName);
587 if (nIndex < 0)
588 {
589 OGRFieldDefn fldDefn(pszAttrName, eNewType);
590 fldDefn.SetSubType(eNewSubType);
591 if (eNewSubType == OFSTBoolean)
592 fldDefn.SetWidth(1);
593 m_poFeatureDefn->AddFieldDefn(&fldDefn);
594
595 aosPaths.push_back(std::string(elt.key()));
596 m_aaosFieldPaths.push_back(aosPaths);
597 }
598 else
599 {
600 OGRFieldDefn *poFDefn = m_poFeatureDefn->GetFieldDefn(nIndex);
601 OGRUpdateFieldType(poFDefn, eNewType, eNewSubType);
602 }
603 }
604
605 /************************************************************************/
606 /* CollectGeomIndices() */
607 /************************************************************************/
608
609 std::map<CPLString, CPLString> OGRMongoDBv3Layer::CollectGeomIndices()
610 {
611 std::map<CPLString, CPLString> oMapIndices;
612 try
613 {
614 auto cursor = m_oColl.list_indexes();
615 for (auto &&l_index : cursor)
616 {
617 // std::string s(bsoncxx::to_json(l_index));
618 // CPLDebug("MongoDBv3", "%s", s.c_str());
619 auto key = l_index["key"];
620 if (key && key.type() == bsoncxx::type::k_document)
621 {
622 bsoncxx::document::view keyDoc{key.get_document()};
623 for (auto &&field : keyDoc)
624 {
625 #if BSONCXX_VERSION_MAJOR >= 4
626 if (field.type() == bsoncxx::type::k_string)
627 #else
628 if (field.type() == bsoncxx::type::k_utf8)
629 #endif
630 {
631 const std::string v(get_string(field));
632 if (v == "2d" || v == "2dsphere")
633 {
634 std::string idxColName(field.key());
635 // CPLDebug("MongoDBv3", "Index %s for %s of %s",
636 // v.c_str(),
637 // idxColName.c_str(),
638 // m_poFeatureDefn->GetName());
639 oMapIndices[idxColName] = v;
640 }
641 }
642 }
643 }
644 }
645 }
646 catch (const std::exception &ex)
647 {
648 CPLDebug("MongoDBv3", "Error when listing indices: %s", ex.what());
649 }
650 return oMapIndices;
651 }
652
653 /************************************************************************/
654 /* ReadOGRMetadata() */
655 /************************************************************************/
656
657 bool OGRMongoDBv3Layer::ReadOGRMetadata(
658 std::map<CPLString, CPLString> &oMapIndices)
659 {
660 try
661 {
662 bsoncxx::builder::basic::document filter{};
663 filter.append(kvp("layer", m_oColl.name()));
664 auto docOpt = m_oDb["_ogr_metadata"].find_one(filter.extract());
665 if (docOpt)
666 {
667 auto doc = docOpt->view();
668 auto fid = doc["fid"];
669 #if BSONCXX_VERSION_MAJOR >= 4
670 if (fid && fid.type() == bsoncxx::type::k_string)
671 #else
672 if (fid && fid.type() == bsoncxx::type::k_utf8)
673 #endif
674 m_osFID = get_string(fid);
675
676 auto fields = doc["fields"];
677 if (fields && fields.type() == bsoncxx::type::k_array)
678 {
679 auto arrayView(fields.get_array().value);
680 for (const auto &elt : arrayView)
681 {
682 if (elt.type() == bsoncxx::type::k_document)
683 {
684 auto obj2_doc(elt.get_document());
685 auto obj2(obj2_doc.view());
686 auto name = obj2["name"];
687 auto type = obj2["type"];
688 auto subtype = obj2["subtype"];
689 auto path = obj2["path"];
690 #if BSONCXX_VERSION_MAJOR >= 4
691 if (name && name.type() == bsoncxx::type::k_string &&
692 type && type.type() == bsoncxx::type::k_string &&
693 #else
694 if (name && name.type() == bsoncxx::type::k_utf8 &&
695 type && type.type() == bsoncxx::type::k_utf8 &&
696 #endif
697 path && path.type() == bsoncxx::type::k_array)
698 {
699 if (get_string(name) == "_id")
700 continue;
701 OGRFieldType eType(OFTString);
702 for (int j = 0; j <= OFTMaxType; j++)
703 {
704 if (EQUAL(OGR_GetFieldTypeName(
705 static_cast<OGRFieldType>(j)),
706 get_string(type).c_str()))
707 {
708 eType = static_cast<OGRFieldType>(j);
709 break;
710 }
711 }
712
713 std::vector<CPLString> aosPaths;
714 auto oPathArray(path.get_array().value);
715 bool ok = true;
716 for (const auto &eltPath : oPathArray)
717 {
718 #if BSONCXX_VERSION_MAJOR >= 4
719 if (eltPath.type() != bsoncxx::type::k_string)
720 #else
721 if (eltPath.type() != bsoncxx::type::k_utf8)
722 #endif
723 {
724 ok = false;
725 break;
726 }
727 aosPaths.push_back(get_string(eltPath));
728 }
729 if (!ok)
730 continue;
731
732 OGRFieldDefn oFieldDefn(get_string(name).c_str(),
733 eType);
734 if (subtype &&
735 #if BSONCXX_VERSION_MAJOR >= 4
736 subtype.type() == bsoncxx::type::k_string &&
737 #else
738 subtype.type() == bsoncxx::type::k_utf8 &&
739 #endif
740 get_string(subtype) == "Boolean")
741 {
742 // cppcheck-suppress danglingTemporaryLifetime
743 oFieldDefn.SetSubType(OFSTBoolean);
744 }
745 // cppcheck-suppress danglingTemporaryLifetime
746 m_poFeatureDefn->AddFieldDefn(&oFieldDefn);
747
748 m_aaosFieldPaths.push_back(aosPaths);
749 }
750 }
751 }
752 }
753
754 auto geomfields = doc["geomfields"];
755 if (geomfields && geomfields.type() == bsoncxx::type::k_array)
756 {
757 auto arrayView(geomfields.get_array().value);
758 for (const auto &elt : arrayView)
759 {
760 if (elt.type() == bsoncxx::type::k_document)
761 {
762 auto obj2_doc(elt.get_document());
763 auto obj2(obj2_doc.view());
764 auto name = obj2["name"];
765 auto type = obj2["type"];
766 auto path = obj2["path"];
767 #if BSONCXX_VERSION_MAJOR >= 4
768 if (name && name.type() == bsoncxx::type::k_string &&
769 type && type.type() == bsoncxx::type::k_string &&
770 #else
771 if (name && name.type() == bsoncxx::type::k_utf8 &&
772 type && type.type() == bsoncxx::type::k_utf8 &&
773 #endif
774 path && path.type() == bsoncxx::type::k_array)
775 {
776
777 std::vector<CPLString> aosPaths;
778 auto oPathArray(path.get_array().value);
779 bool ok = true;
780 for (const auto &eltPath : oPathArray)
781 {
782 #if BSONCXX_VERSION_MAJOR >= 4
783 if (eltPath.type() != bsoncxx::type::k_string)
784 #else
785 if (eltPath.type() != bsoncxx::type::k_utf8)
786 #endif
787 {
788 ok = false;
789 break;
790 }
791 aosPaths.push_back(get_string(eltPath));
792 }
793 if (!ok)
794 continue;
795
796 OGRwkbGeometryType eType(
797 OGRFromOGCGeomType(get_string(type).c_str()));
798 OGRGeomFieldDefn oFieldDefn(
799 get_string(name).c_str(), eType);
800 OGRSpatialReference *poSRS =
801 new OGRSpatialReference();
802 poSRS->SetAxisMappingStrategy(
803 OAMS_TRADITIONAL_GIS_ORDER);
804 poSRS->SetFromUserInput(SRS_WKT_WGS84_LAT_LONG);
805 // cppcheck-suppress danglingTemporaryLifetime
806 oFieldDefn.SetSpatialRef(poSRS);
807 poSRS->Release();
808 // cppcheck-suppress danglingTemporaryLifetime
809 m_poFeatureDefn->AddGeomFieldDefn(&oFieldDefn);
810
811 m_aaosGeomFieldPaths.push_back(aosPaths);
812 if (oMapIndices.find(oFieldDefn.GetNameRef()) !=
813 oMapIndices.end())
814 m_aosGeomIndexes.push_back(
815 oMapIndices[oFieldDefn.GetNameRef()]);
816 else
817 m_aosGeomIndexes.push_back("none");
818 // CPLDebug("MongoDBv3", "Layer %s:
819 // m_aosGeomIndexes[%d] = %s",
820 // m_poFeatureDefn->GetName(),
821 // m_poFeatureDefn->GetGeomFieldCount()-1,
822 // m_aosGeomIndexes[m_poFeatureDefn->GetGeomFieldCount()-1].c_str());
823 m_apoCT.push_back(nullptr);
824 }
825 }
826 }
827 }
828
829 m_bLayerMetadataUpdatable = true;
830 return true;
831 }
832 }
833 catch (const std::exception &ex)
834 {
835 CPLError(CE_Warning, CPLE_AppDefined, "%s: %s", "ReadOGRMetadata()",
836 ex.what());
837 }
838 return false;
839 }
840
841 /************************************************************************/
842 /* EstablishFeatureDefn() */
843 /************************************************************************/
844
845 void OGRMongoDBv3Layer::EstablishFeatureDefn()
846 {
847 if (m_bHasEstablishedFeatureDefn)
848 return;
849
850 std::map<CPLString, CPLString> oMapIndices(CollectGeomIndices());
851
852 int nCount = m_poDS->m_nFeatureCountToEstablishFeatureDefn;
853 if (m_poDS->m_bUseOGRMetadata)
854 {
855 if (ReadOGRMetadata(oMapIndices))
856 nCount = 0;
857 }
858
859 if (nCount != 0)
860 {
861 try
862 {
863 mongocxx::options::find options;
864 if (nCount > 0)
865 {
866 options.limit(nCount);
867 }
868 if (m_poDS->m_nBatchSize > 0)
869 {
870 options.batch_size(m_poDS->m_nBatchSize);
871 }
872
873 auto cursor = m_oColl.find({}, options);
874 for (auto &&doc : cursor)
875 {
876 // std::string s(bsoncxx::to_json(doc));
877 // CPLDebug("MongoDBv3", "%s", s.c_str());
878 for (auto &&field : doc)
879 {
880 std::vector<CPLString> aosPaths;
881 std::string osKey(field.key());
882 if (osKey == m_poDS->m_osFID)
883 {
884 m_osFID = osKey;
885 }
886 else
887 {
888 AddOrUpdateField(osKey.c_str(), field, '.', aosPaths,
889 oMapIndices);
890 }
891 }
892 }
893 }
894 catch (const std::exception &ex)
895 {
896 CPLError(CE_Failure, CPLE_AppDefined, "%s: %s",
897 "EstablishFeatureDefn()", ex.what());
898 }
899 }
900
901 if (m_poDS->m_bJSonField)
902 {
903 OGRFieldDefn fldDefn("_json", OFTString);
904 m_poFeatureDefn->AddFieldDefn(&fldDefn);
905 std::vector<CPLString> aosPaths;
906 m_aaosFieldPaths.push_back(aosPaths);
907 }
908
909 m_bHasEstablishedFeatureDefn = true;
910 }
911
912 /************************************************************************/
913 /* GetFIDColumn() */
914 /************************************************************************/
915
916 const char *OGRMongoDBv3Layer::GetFIDColumn() const
917 {
918 if (!m_bHasEstablishedFeatureDefn)
919 const_cast<OGRMongoDBv3Layer *>(this)->EstablishFeatureDefn();
920 return m_osFID.c_str();
921 }
922
923 /************************************************************************/
924 /* BuildQuery() */
925 /************************************************************************/
926
927 bsoncxx::document::value OGRMongoDBv3Layer::BuildQuery()
928 {
929 bsoncxx::builder::basic::document b{};
930 auto queryAttrView(m_oQueryAttr.view());
931 for (const auto &field : queryAttrView)
932 {
933 b.append(kvp(field.key(), field.get_value()));
934 }
935 auto querySpatView(m_oQuerySpat.view());
936 for (const auto &field : querySpatView)
937 {
938 b.append(kvp(field.key(), field.get_value()));
939 }
940 return b.extract();
941 }
942
943 /************************************************************************/
944 /* GetFeatureCount() */
945 /************************************************************************/
946
947 GIntBig OGRMongoDBv3Layer::GetFeatureCount(int bForce)
948 {
949 if (m_poAttrQuery != nullptr ||
950 (m_poFilterGeom != nullptr && !TestCapability(OLCFastSpatialFilter)))
951 {
952 return OGRLayer::GetFeatureCount(bForce);
953 }
954
955 if (!m_bHasEstablishedFeatureDefn)
956 EstablishFeatureDefn();
957 SyncToDisk();
958
959 try
960 {
961 return static_cast<GIntBig>(m_oColl.count_documents(BuildQuery()));
962 }
963 catch (const std::exception &ex)
964 {
965 CPLError(CE_Warning, CPLE_AppDefined, "%s: %s", "GetFeatureCount()",
966 ex.what());
967 return OGRLayer::GetFeatureCount(bForce);
968 }
969 }
970
971 /************************************************************************/
972 /* Stringify() */
973 /************************************************************************/
974
975 #if BSONCXX_VERSION_MAJOR > 3 || BSONCXX_VERSION_MINOR >= 6
976 static CPLString Stringify(const bsoncxx::types::bson_value::view &val)
977 #else
978 static CPLString Stringify(const bsoncxx::types::value &val)
979 #endif
980 {
981 const auto eBSONType = val.type();
982 #if BSONCXX_VERSION_MAJOR >= 4
983 if (eBSONType == bsoncxx::type::k_string)
984 #else
985 if (eBSONType == bsoncxx::type::k_utf8)
986 #endif
987 {
988 #if BSONCXX_VERSION_MAJOR > 3 || BSONCXX_VERSION_MINOR >= 7
989 return std::string(val.get_string().value);
990 #else
991 return std::string(val.get_utf8().value);
992 #endif
993 }
994 else if (eBSONType == bsoncxx::type::k_int32)
995 return CPLSPrintf("%d", val.get_int32().value);
996 else if (eBSONType == bsoncxx::type::k_int64)
997 return CPLSPrintf(CPL_FRMT_GIB,
998 static_cast<GIntBig>(val.get_int64().value));
999 else if (eBSONType == bsoncxx::type::k_double)
1000 return CPLSPrintf("%.16g", val.get_double().value);
1001 else if (eBSONType == bsoncxx::type::k_oid)
1002 return val.get_oid().value.to_string();
1003 else if (eBSONType == bsoncxx::type::k_bool)
1004 return CPLSPrintf("%d", val.get_bool().value);
1005 else if (eBSONType == bsoncxx::type::k_date)
1006 {
1007 GIntBig secsandmillis = static_cast<GIntBig>(val.get_date().to_int64());
1008 struct tm tm;
1009 GIntBig secs = secsandmillis / 1000;
1010 int millis = static_cast<int>(secsandmillis % 1000);
1011 if (millis < 0)
1012 {
1013 secs--;
1014 millis += 1000;
1015 }
1016 CPLUnixTimeToYMDHMS(secs, &tm);
1017 return CPLSPrintf("%04d-%02d-%02dT%02d:%02d:%02d.%03dZ",
1018 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
1019 tm.tm_hour, tm.tm_min, tm.tm_sec, millis);
1020 }
1021 else if (eBSONType == bsoncxx::type::k_document)
1022 {
1023 return CPLString(bsoncxx::to_json(val.get_document().value));
1024 }
1025 else
1026 {
1027 // This looks like a hack, but to_json() only works on documents
1028 // so we have to wrap our value as a fake { "v": val } document...
1029 bsoncxx::builder::basic::document b{};
1030 b.append(kvp("v", val));
1031 CPLString ret(bsoncxx::to_json(b.extract()));
1032 CPLAssert(ret.find("{ \"v\" : ") == 0);
1033 CPLAssert(ret.substr(ret.size() - 2) == " }");
1034 ret = ret.substr(strlen("{ \"v\" : "),
1035 ret.size() - strlen("{ \"v\" : ") - 2);
1036 return ret;
1037 }
1038 }
1039
1040 /************************************************************************/
1041 /* OGRMongoDBV3ReaderSetField() */
1042 /************************************************************************/
1043
1044 static void OGRMongoDBV3ReaderSetField(OGRFeature *poFeature,
1045 const char *pszAttrName,
1046 const bsoncxx::document::element &elt,
1047 bool bFlattenNestedAttributes,
1048 char chNestedAttributeSeparator)
1049 {
1050 int nGeomFieldIndex;
1051 auto eBSONType = elt.type();
1052
1053 if (eBSONType == bsoncxx::type::k_document &&
1054 (nGeomFieldIndex = poFeature->GetGeomFieldIndex(pszAttrName)) >= 0)
1055 {
1056 CPLPushErrorHandler(CPLQuietErrorHandler);
1057 OGRGeometry *poGeom = OGRGeometry::FromHandle(
1058 OGR_G_CreateGeometryFromJson(Stringify(elt.get_value())));
1059 CPLPopErrorHandler();
1060 if (poGeom != nullptr)
1061 {
1062 poGeom->assignSpatialReference(
1063 poFeature->GetDefnRef()
1064 ->GetGeomFieldDefn(nGeomFieldIndex)
1065 ->GetSpatialRef());
1066 poFeature->SetGeomFieldDirectly(nGeomFieldIndex, poGeom);
1067 }
1068 return;
1069 }
1070 else if (eBSONType == bsoncxx::type::k_array &&
1071 (nGeomFieldIndex = poFeature->GetGeomFieldIndex(pszAttrName)) >= 0)
1072 {
1073 auto arrayView = elt.get_array().value;
1074 const unsigned nSize = static_cast<unsigned>(
1075 std::distance(arrayView.begin(), arrayView.end()));
1076 if (nSize == 2)
1077 {
1078 auto x = arrayView[0];
1079 auto y = arrayView[1];
1080 if (x.type() == bsoncxx::type::k_double &&
1081 y.type() == bsoncxx::type::k_double)
1082 {
1083 OGRGeometry *poGeom =
1084 new OGRPoint(x.get_double().value, y.get_double().value);
1085 poGeom->assignSpatialReference(
1086 poFeature->GetDefnRef()
1087 ->GetGeomFieldDefn(nGeomFieldIndex)
1088 ->GetSpatialRef());
1089 poFeature->SetGeomFieldDirectly(nGeomFieldIndex, poGeom);
1090 }
1091 }
1092 return;
1093 }
1094
1095 if (bFlattenNestedAttributes && eBSONType == bsoncxx::type::k_document)
1096 {
1097 auto doc(elt.get_document().value);
1098 for (auto &&field : doc)
1099 {
1100 CPLString osAttrName(pszAttrName);
1101 osAttrName += chNestedAttributeSeparator;
1102 std::string keyName(field.key());
1103 osAttrName += keyName;
1104 OGRMongoDBV3ReaderSetField(poFeature, osAttrName, field,
1105 bFlattenNestedAttributes,
1106 chNestedAttributeSeparator);
1107 }
1108 return;
1109 }
1110
1111 int nField = poFeature->GetFieldIndex(pszAttrName);
1112 if (nField < 0)
1113 return;
1114 const OGRFieldDefn *poFieldDefn = poFeature->GetFieldDefnRef(nField);
1115 CPLAssert(nullptr != poFieldDefn);
1116 OGRFieldType eType = poFieldDefn->GetType();
1117 if (eBSONType == bsoncxx::type::k_null)
1118 poFeature->SetFieldNull(nField);
1119 else if (eBSONType == bsoncxx::type::k_int32)
1120 poFeature->SetField(nField, elt.get_int32().value);
1121 else if (eBSONType == bsoncxx::type::k_int64)
1122 poFeature->SetField(nField,
1123 static_cast<GIntBig>(elt.get_int64().value));
1124 else if (eBSONType == bsoncxx::type::k_double)
1125 poFeature->SetField(nField, elt.get_double().value);
1126 else if (eBSONType == bsoncxx::type::k_minkey && eType == OFTReal)
1127 poFeature->SetField(nField, -std::numeric_limits<double>::infinity());
1128 else if (eBSONType == bsoncxx::type::k_maxkey && eType == OFTReal)
1129 poFeature->SetField(nField, std::numeric_limits<double>::infinity());
1130 else if (eBSONType == bsoncxx::type::k_minkey && eType == OFTInteger)
1131 poFeature->SetField(nField, INT_MIN);
1132 else if (eBSONType == bsoncxx::type::k_maxkey && eType == OFTInteger)
1133 poFeature->SetField(nField, INT_MAX);
1134 else if (eBSONType == bsoncxx::type::k_minkey && eType == OFTInteger64)
1135 poFeature->SetField(nField, std::numeric_limits<GIntBig>::min());
1136 else if (eBSONType == bsoncxx::type::k_maxkey && eType == OFTInteger64)
1137 poFeature->SetField(nField, std::numeric_limits<GIntBig>::max());
1138 else if (eBSONType == bsoncxx::type::k_array)
1139 {
1140 auto arrayView = elt.get_array().value;
1141 const unsigned nSize = static_cast<unsigned>(
1142 std::distance(arrayView.begin(), arrayView.end()));
1143 if (eType == OFTStringList)
1144 {
1145 char **papszValues =
1146 static_cast<char **>(CPLCalloc(nSize + 1, sizeof(char *)));
1147 unsigned int i = 0;
1148 for (auto &&subElt : arrayView)
1149 {
1150 papszValues[i] = CPLStrdup(Stringify(subElt.get_value()));
1151 ++i;
1152 }
1153 poFeature->SetField(nField, papszValues);
1154 CSLDestroy(papszValues);
1155 }
1156 else if (eType == OFTRealList)
1157 {
1158 double *padfValues =
1159 static_cast<double *>(CPLMalloc(nSize * sizeof(double)));
1160 unsigned int i = 0;
1161 for (auto &&subElt : arrayView)
1162 {
1163 eBSONType = subElt.type();
1164 if (eBSONType == bsoncxx::type::k_int32)
1165 padfValues[i] = subElt.get_int32().value;
1166 else if (eBSONType == bsoncxx::type::k_int64)
1167 padfValues[i] =
1168 static_cast<double>(subElt.get_int64().value);
1169 else if (eBSONType == bsoncxx::type::k_double)
1170 padfValues[i] = subElt.get_double().value;
1171 else if (eBSONType == bsoncxx::type::k_minkey)
1172 padfValues[i] = -std::numeric_limits<double>::infinity();
1173 else if (eBSONType == bsoncxx::type::k_maxkey)
1174 padfValues[i] = std::numeric_limits<double>::infinity();
1175 else
1176 padfValues[i] = CPLAtof(Stringify(subElt.get_value()));
1177 ++i;
1178 }
1179 poFeature->SetField(nField, nSize, padfValues);
1180 CPLFree(padfValues);
1181 }
1182 else if (eType == OFTIntegerList)
1183 {
1184 int *panValues = static_cast<int *>(CPLMalloc(nSize * sizeof(int)));
1185 unsigned int i = 0;
1186 for (auto &&subElt : arrayView)
1187 {
1188 eBSONType = subElt.type();
1189 if (eBSONType == bsoncxx::type::k_int32)
1190 panValues[i] = subElt.get_int32().value;
1191 else if (eBSONType == bsoncxx::type::k_int64)
1192 {
1193 GIntBig nVal = subElt.get_int64().value;
1194 if (nVal < INT_MIN)
1195 panValues[i] = INT_MIN;
1196 else if (nVal > INT_MAX)
1197 panValues[i] = INT_MAX;
1198 else
1199 panValues[i] = static_cast<int>(nVal);
1200 }
1201 else if (eBSONType == bsoncxx::type::k_double)
1202 {
1203 double dfVal = subElt.get_double().value;
1204 if (dfVal < INT_MIN)
1205 panValues[i] = INT_MIN;
1206 else if (dfVal > INT_MAX)
1207 panValues[i] = INT_MAX;
1208 else
1209 panValues[i] = static_cast<int>(dfVal);
1210 }
1211 else if (eBSONType == bsoncxx::type::k_minkey)
1212 panValues[i] = INT_MIN;
1213 else if (eBSONType == bsoncxx::type::k_maxkey)
1214 panValues[i] = INT_MAX;
1215 else
1216 panValues[i] = atoi(Stringify(subElt.get_value()));
1217 ++i;
1218 }
1219 poFeature->SetField(nField, nSize, panValues);
1220 CPLFree(panValues);
1221 }
1222 else if (eType == OFTInteger64List)
1223 {
1224 GIntBig *panValues =
1225 static_cast<GIntBig *>(CPLMalloc(nSize * sizeof(GIntBig)));
1226 unsigned int i = 0;
1227 for (auto &&subElt : arrayView)
1228 {
1229 eBSONType = subElt.type();
1230 if (eBSONType == bsoncxx::type::k_int32)
1231 panValues[i] = subElt.get_int32().value;
1232 else if (eBSONType == bsoncxx::type::k_int64)
1233 panValues[i] = subElt.get_int64().value;
1234 else if (eBSONType == bsoncxx::type::k_double)
1235 {
1236 double dfVal = subElt.get_double().value;
1237 if (dfVal < std::numeric_limits<GIntBig>::min())
1238 panValues[i] = std::numeric_limits<GIntBig>::min();
1239 else if (dfVal > static_cast<double>(
1240 std::numeric_limits<GIntBig>::max()))
1241 panValues[i] = std::numeric_limits<GIntBig>::max();
1242 else
1243 panValues[i] = static_cast<GIntBig>(dfVal);
1244 }
1245 else if (eBSONType == bsoncxx::type::k_minkey)
1246 panValues[i] = std::numeric_limits<GIntBig>::min();
1247 else if (eBSONType == bsoncxx::type::k_maxkey)
1248 panValues[i] = std::numeric_limits<GIntBig>::max();
1249 else
1250 panValues[i] = CPLAtoGIntBig(Stringify(subElt.get_value()));
1251 ++i;
1252 }
1253 poFeature->SetField(nField, nSize, panValues);
1254 CPLFree(panValues);
1255 }
1256 }
1257 #if BSONCXX_VERSION_MAJOR >= 4
1258 else if (eBSONType == bsoncxx::type::k_string)
1259 #else
1260 else if (eBSONType == bsoncxx::type::k_utf8)
1261 #endif
1262 {
1263 poFeature->SetField(nField, get_string(elt).c_str());
1264 }
1265 else if (eBSONType == bsoncxx::type::k_oid)
1266 poFeature->SetField(nField, elt.get_oid().value.to_string().c_str());
1267 else if (eBSONType == bsoncxx::type::k_bool)
1268 poFeature->SetField(nField, elt.get_bool().value);
1269 else if (eBSONType == bsoncxx::type::k_binary)
1270 {
1271 const auto v(elt.get_binary());
1272 int len = static_cast<int>(v.size);
1273 const GByte *pabyData = v.bytes;
1274 poFeature->SetField(nField, len, pabyData);
1275 }
1276 else
1277 poFeature->SetField(nField, Stringify(elt.get_value()));
1278 }
1279
1280 /************************************************************************/
1281 /* Translate() */
1282 /************************************************************************/
1283
1284 std::unique_ptr<OGRFeature>
1285 OGRMongoDBv3Layer::Translate(const bsoncxx::document::view &doc)
1286 {
1287 auto poFeature = std::make_unique<OGRFeature>(m_poFeatureDefn);
1288 for (auto &&field : doc)
1289 {
1290 std::string fieldName(field.key());
1291 if (!m_poDS->m_osFID.empty() && EQUAL(m_osFID, fieldName.c_str()))
1292 {
1293 const auto eBSONType = field.type();
1294 if (eBSONType == bsoncxx::type::k_int32)
1295 {
1296 poFeature->SetFID(field.get_int32().value);
1297 }
1298 else if (eBSONType == bsoncxx::type::k_int64)
1299 {
1300 poFeature->SetFID(field.get_int64().value);
1301 }
1302 else if (eBSONType == bsoncxx::type::k_double)
1303 {
1304 double dfV = field.get_double().value;
1305 if (dfV >= static_cast<double>(
1306 std::numeric_limits<GIntBig>::min()) &&
1307 dfV <= static_cast<double>(
1308 std::numeric_limits<GIntBig>::max()))
1309 {
1310 auto nV = static_cast<GIntBig>(dfV);
1311 if (static_cast<double>(nV) == dfV)
1312 {
1313 poFeature->SetFID(nV);
1314 }
1315 }
1316 }
1317 }
1318 else
1319 {
1320 OGRMongoDBV3ReaderSetField(poFeature.get(), fieldName.c_str(),
1321 field,
1322 m_poDS->m_bFlattenNestedAttributes, '.');
1323 }
1324
1325 if (m_poDS->m_bJSonField)
1326 {
1327 poFeature->SetField("_json", bsoncxx::to_json(doc).c_str());
1328 }
1329 }
1330 return poFeature;
1331 }
1332
1333 /************************************************************************/
1334 /* GetNextFeature() */
1335 /************************************************************************/
1336
1337 OGRFeature *OGRMongoDBv3Layer::GetNextFeature()
1338 {
1339 if (!m_bHasEstablishedFeatureDefn)
1340 EstablishFeatureDefn();
1341 if (!m_aoDocsToInsert.empty())
1342 SyncToDisk();
1343
1344 try
1345 {
1346 if (!m_poCursor)
1347 {
1348 mongocxx::options::find options;
1349 if (m_poDS->m_nBatchSize > 0)
1350 {
1351 options.batch_size(m_poDS->m_nBatchSize);
1352 }
1353 m_poCursor.reset(
1354 new mongocxx::cursor(m_oColl.find(BuildQuery(), options)));
1355 m_poIterator.reset(
1356 new mongocxx::cursor::iterator(m_poCursor->begin()));
1357 }
1358 if (!m_poCursor || !m_poIterator)
1359 return nullptr;
1360
1361 while (*m_poIterator != m_poCursor->end())
1362 {
1363 auto poFeature(Translate(**m_poIterator));
1364 if (poFeature->GetFID() < 0)
1365 poFeature->SetFID(++m_nIndex);
1366
1367 (*m_poIterator)++;
1368
1369 if ((m_poFilterGeom == nullptr ||
1370 FilterGeometry(poFeature->GetGeometryRef())) &&
1371 (m_poAttrQuery == nullptr ||
1372 m_poAttrQuery->Evaluate(poFeature.get())))
1373 {
1374 return poFeature.release();
1375 }
1376 }
1377 }
1378 catch (const std::exception &ex)
1379 {
1380 CPLError(CE_Failure, CPLE_AppDefined, "%s: %s", "GetNextFeature()",
1381 ex.what());
1382 }
1383 return nullptr;
1384 }
1385
1386 /************************************************************************/
1387 /* GetFeature() */
1388 /************************************************************************/
1389
1390 OGRFeature *OGRMongoDBv3Layer::GetFeature(GIntBig nFID)
1391 {
1392 if (!m_bHasEstablishedFeatureDefn)
1393 EstablishFeatureDefn();
1394 if (!m_aoDocsToInsert.empty())
1395 SyncToDisk();
1396
1397 if (m_osFID.empty())
1398 {
1399 auto oQueryAttrBak = m_oQueryAttr;
1400 auto oQuerySpatBak = m_oQuerySpat;
1401 m_oQueryAttr = bsoncxx::builder::basic::make_document();
1402 m_oQuerySpat = bsoncxx::builder::basic::make_document();
1403 OGRFeature *poFeature = OGRLayer::GetFeature(nFID);
1404 m_oQueryAttr = oQueryAttrBak;
1405 m_oQuerySpat = oQuerySpatBak;
1406 return poFeature;
1407 }
1408
1409 try
1410 {
1411 bsoncxx::builder::basic::document b{};
1412 b.append(kvp(std::string(m_osFID), static_cast<int64_t>(nFID)));
1413 auto obj = m_oColl.find_one(b.extract());
1414 if (!obj)
1415 return nullptr;
1416
1417 auto poFeature = Translate(obj->view());
1418 poFeature->SetFID(nFID);
1419 return poFeature.release();
1420 }
1421 catch (const std::exception &ex)
1422 {
1423 CPLError(CE_Failure, CPLE_AppDefined, "%s: %s", "GetFeature()",
1424 ex.what());
1425 return nullptr;
1426 }
1427 }
1428
1429 /************************************************************************/
1430 /* DeleteFeature() */
1431 /************************************************************************/
1432
1433 OGRErr OGRMongoDBv3Layer::DeleteFeature(GIntBig nFID)
1434 {
1435 if (m_poDS->GetAccess() != GA_Update)
1436 {
1437 CPLError(CE_Failure, CPLE_AppDefined,
1438 "Dataset opened in read-only mode");
1439 return OGRERR_FAILURE;
1440 }
1441
1442 if (!m_bHasEstablishedFeatureDefn)
1443 EstablishFeatureDefn();
1444 if (!m_aoDocsToInsert.empty())
1445 SyncToDisk();
1446 if (m_osFID.empty())
1447 return OGRERR_FAILURE;
1448
1449 try
1450 {
1451 bsoncxx::builder::basic::document b{};
1452 b.append(kvp(std::string(m_osFID), static_cast<int64_t>(nFID)));
1453 auto obj = m_oColl.find_one_and_delete(b.extract());
1454 // if( obj )
1455 //{
1456 // std::string s(bsoncxx::to_json(obj->view()));
1457 // CPLDebug("MongoDBv3", "%s", s.c_str());
1458 // }
1459 return obj ? OGRERR_NONE : OGRERR_NON_EXISTING_FEATURE;
1460 }
1461 catch (const std::exception &ex)
1462 {
1463 CPLError(CE_Failure, CPLE_AppDefined, "%s: %s", "DeleteFeature()",
1464 ex.what());
1465 return OGRERR_FAILURE;
1466 }
1467 }
1468
1469 /************************************************************************/
1470 /* CreateField() */
1471 /************************************************************************/
1472
1473 OGRErr OGRMongoDBv3Layer::CreateField(const OGRFieldDefn *poFieldIn, int)
1474
1475 {
1476 if (m_poDS->GetAccess() != GA_Update)
1477 {
1478 CPLError(CE_Failure, CPLE_AppDefined,
1479 "Dataset opened in read-only mode");
1480 return OGRERR_FAILURE;
1481 }
1482
1483 const char *pszFieldName = poFieldIn->GetNameRef();
1484 if (m_poFeatureDefn->GetFieldIndex(pszFieldName) >= 0)
1485 {
1486 if (!EQUAL(pszFieldName, "_id") && !EQUAL(pszFieldName, "_json"))
1487 {
1488 CPLError(
1489 CE_Failure, CPLE_AppDefined,
1490 "CreateField() called with an already existing field name: %s",
1491 pszFieldName);
1492 }
1493 return OGRERR_FAILURE;
1494 }
1495
1496 m_poFeatureDefn->AddFieldDefn(poFieldIn);
1497
1498 std::vector<CPLString> aosPaths;
1499 if (m_bDotAsNestedField)
1500 {
1501 char **papszTokens = CSLTokenizeString2(pszFieldName, ".", 0);
1502 for (int i = 0; papszTokens[i]; i++)
1503 aosPaths.push_back(papszTokens[i]);
1504 CSLDestroy(papszTokens);
1505 }
1506 else
1507 aosPaths.push_back(pszFieldName);
1508 m_aaosFieldPaths.push_back(aosPaths);
1509
1510 m_bUpdateLayerMetadata = m_bLayerMetadataUpdatable;
1511
1512 return OGRERR_NONE;
1513 }
1514
1515 /************************************************************************/
1516 /* CreateGeomField() */
1517 /************************************************************************/
1518
1519 OGRErr OGRMongoDBv3Layer::CreateGeomField(const OGRGeomFieldDefn *poFieldIn,
1520 int)
1521
1522 {
1523 if (m_poDS->GetAccess() != GA_Update)
1524 {
1525 CPLError(CE_Failure, CPLE_AppDefined,
1526 "Dataset opened in read-only mode");
1527 return OGRERR_FAILURE;
1528 }
1529
1530 if (m_poFeatureDefn->GetGeomFieldIndex(poFieldIn->GetNameRef()) >= 0)
1531 {
1532 CPLError(
1533 CE_Failure, CPLE_AppDefined,
1534 "CreateGeomField() called with an already existing field name: %s",
1535 poFieldIn->GetNameRef());
1536 return OGRERR_FAILURE;
1537 }
1538
1539 OGRGeomFieldDefn oFieldDefn(poFieldIn);
1540 if (EQUAL(oFieldDefn.GetNameRef(), ""))
1541 oFieldDefn.SetName("geometry");
1542
1543 m_poFeatureDefn->AddGeomFieldDefn(&oFieldDefn);
1544
1545 std::vector<CPLString> aosPaths;
1546 if (m_bDotAsNestedField)
1547 {
1548 char **papszTokens =
1549 CSLTokenizeString2(oFieldDefn.GetNameRef(), ".", 0);
1550 for (int i = 0; papszTokens[i]; i++)
1551 aosPaths.push_back(papszTokens[i]);
1552 CSLDestroy(papszTokens);
1553 }
1554 else
1555 aosPaths.push_back(oFieldDefn.GetNameRef());
1556 m_aaosGeomFieldPaths.push_back(aosPaths);
1557 m_aosGeomIndexes.push_back("none");
1558
1559 std::unique_ptr<OGRCoordinateTransformation> poCT;
1560 if (oFieldDefn.GetSpatialRef() != nullptr)
1561 {
1562 OGRSpatialReference oSRS_WGS84;
1563 oSRS_WGS84.SetFromUserInput(SRS_WKT_WGS84_LAT_LONG);
1564 oSRS_WGS84.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
1565 if (!oSRS_WGS84.IsSame(oFieldDefn.GetSpatialRef()))
1566 {
1567 poCT.reset(OGRCreateCoordinateTransformation(
1568 oFieldDefn.GetSpatialRef(), &oSRS_WGS84));
1569 if (poCT.get() == nullptr)
1570 {
1571 CPLError(CE_Warning, CPLE_AppDefined,
1572 "On-the-fly reprojection to WGS84 long/lat would be "
1573 "needed, but instantiation of transformer failed");
1574 }
1575 }
1576 }
1577 m_apoCT.push_back(std::move(poCT));
1578
1579 if (m_bCreateSpatialIndex)
1580 {
1581 // CPLDebug("MongoDBv3", "Create spatial index for %s of %s",
1582 // poFieldIn->GetNameRef(), m_poFeatureDefn->GetName());
1583 try
1584 {
1585 const char *pszIndexType;
1586 if (wkbFlatten(poFieldIn->GetType()) != wkbPoint)
1587 pszIndexType = "2dsphere";
1588 else
1589 pszIndexType = CPLGetConfigOption("OGR_MONGODB_SPAT_INDEX_TYPE",
1590 "2dsphere");
1591
1592 bsoncxx::builder::basic::document b{};
1593 b.append(kvp(std::string(oFieldDefn.GetNameRef()),
1594 std::string(pszIndexType)));
1595 m_oColl.create_index(b.extract());
1596
1597 m_aosGeomIndexes.back() = pszIndexType;
1598 }
1599 catch (const std::exception &ex)
1600 {
1601 CPLError(CE_Failure, CPLE_AppDefined, "%s: %s", "Index creation",
1602 ex.what());
1603 }
1604 }
1605
1606 m_bUpdateLayerMetadata = m_bLayerMetadataUpdatable;
1607
1608 return OGRERR_NONE;
1609 }
1610
1611 /************************************************************************/
1612 /* SerializeField() */
1613 /************************************************************************/
1614
1615 void OGRMongoDBv3Layer::SerializeField(bsoncxx::builder::basic::document &b,
1616 OGRFeature *poFeature, int iField,
1617 const char *pszJSonField)
1618 {
1619 OGRFieldType eType = m_poFeatureDefn->GetFieldDefn(iField)->GetType();
1620 std::string osFieldName(pszJSonField);
1621 if (poFeature->IsFieldNull(iField))
1622 {
1623 b.append(kvp(osFieldName, bsoncxx::types::b_null{}));
1624 }
1625 else if (eType == OFTInteger)
1626 {
1627 if (m_poFeatureDefn->GetFieldDefn(iField)->GetSubType() == OFSTBoolean)
1628 b.append(kvp(osFieldName,
1629 CPL_TO_BOOL(poFeature->GetFieldAsInteger(iField))));
1630 else
1631 b.append(kvp(osFieldName, poFeature->GetFieldAsInteger(iField)));
1632 }
1633 else if (eType == OFTInteger64)
1634 b.append(kvp(osFieldName, static_cast<int64_t>(
1635 poFeature->GetFieldAsInteger64(iField))));
1636 else if (eType == OFTReal)
1637 b.append(kvp(osFieldName, poFeature->GetFieldAsDouble(iField)));
1638 else if (eType == OFTString)
1639 b.append(kvp(osFieldName, poFeature->GetFieldAsString(iField)));
1640 else if (eType == OFTStringList)
1641 {
1642 char **papszValues = poFeature->GetFieldAsStringList(iField);
1643 bsoncxx::builder::basic::array arrayBuilder;
1644 for (int i = 0; papszValues[i]; i++)
1645 arrayBuilder.append(papszValues[i]);
1646 b.append(kvp(osFieldName, arrayBuilder.extract()));
1647 }
1648 else if (eType == OFTIntegerList)
1649 {
1650 int nSize;
1651 const int *panValues = poFeature->GetFieldAsIntegerList(iField, &nSize);
1652 bsoncxx::builder::basic::array arrayBuilder;
1653 for (int i = 0; i < nSize; i++)
1654 arrayBuilder.append(panValues[i]);
1655 b.append(kvp(osFieldName, arrayBuilder.extract()));
1656 }
1657 else if (eType == OFTInteger64List)
1658 {
1659 int nSize;
1660 const GIntBig *panValues =
1661 poFeature->GetFieldAsInteger64List(iField, &nSize);
1662 bsoncxx::builder::basic::array arrayBuilder;
1663 for (int i = 0; i < nSize; i++)
1664 arrayBuilder.append(static_cast<int64_t>(panValues[i]));
1665 b.append(kvp(osFieldName, arrayBuilder.extract()));
1666 }
1667 else if (eType == OFTRealList)
1668 {
1669 int nSize;
1670 const double *padfValues =
1671 poFeature->GetFieldAsDoubleList(iField, &nSize);
1672 bsoncxx::builder::basic::array arrayBuilder;
1673 for (int i = 0; i < nSize; i++)
1674 arrayBuilder.append(padfValues[i]);
1675 b.append(kvp(osFieldName, arrayBuilder.extract()));
1676 }
1677 else if (eType == OFTBinary)
1678 {
1679 int nSize;
1680 const GByte *pabyData = poFeature->GetFieldAsBinary(iField, &nSize);
1681 bsoncxx::types::b_binary bin;
1682 bin.sub_type = bsoncxx::binary_sub_type::k_binary;
1683 bin.size = nSize;
1684 bin.bytes = pabyData;
1685 b.append(kvp(osFieldName, bin));
1686 }
1687 else if (eType == OFTDate || eType == OFTDateTime || eType == OFTTime)
1688 {
1689 struct tm tm;
1690 int nYear, nMonth, nDay, nHour, nMinute, nTZ;
1691 float fSecond;
1692 poFeature->GetFieldAsDateTime(iField, &nYear, &nMonth, &nDay, &nHour,
1693 &nMinute, &fSecond, &nTZ);
1694 tm.tm_year = nYear - 1900;
1695 tm.tm_mon = nMonth - 1;
1696 tm.tm_mday = nDay;
1697 tm.tm_hour = nHour;
1698 tm.tm_min = nMinute;
1699 tm.tm_sec = static_cast<int>(fSecond);
1700 GIntBig millis = 1000 * CPLYMDHMSToUnixTime(&tm) +
1701 static_cast<GIntBig>(1000 * fmod(fSecond, 1));
1702 b.append(kvp(osFieldName,
1703 bsoncxx::types::b_date(
1704 static_cast<std::chrono::milliseconds>(millis))));
1705 }
1706 }
1707
1708 /************************************************************************/
1709 /* SerializeGeometry() */
1710 /************************************************************************/
1711
1712 void OGRMongoDBv3Layer::SerializeGeometry(bsoncxx::builder::basic::document &b,
1713 OGRGeometry *poGeom, int iField,
1714 const char *pszJSonField)
1715 {
1716 std::string osFieldName(pszJSonField);
1717 if (m_aosGeomIndexes[iField] == "2d" &&
1718 wkbFlatten(poGeom->getGeometryType()) == wkbPoint)
1719 {
1720 bsoncxx::builder::basic::array arrayBuilder;
1721 OGRPoint *poPoint = poGeom->toPoint();
1722 arrayBuilder.append(poPoint->getX());
1723 arrayBuilder.append(poPoint->getY());
1724 b.append(kvp(osFieldName, arrayBuilder.extract()));
1725 }
1726 else
1727 {
1728 char *pszJSon = OGR_G_ExportToJson(OGRGeometry::ToHandle(poGeom));
1729 if (pszJSon)
1730 {
1731 // CPLDebug("MongoDBv3", "%s", pszJSon);
1732 auto obj(bsoncxx::from_json(pszJSon));
1733 b.append(kvp(osFieldName, obj));
1734 }
1735 CPLFree(pszJSon);
1736 }
1737 }
1738
1739 /************************************************************************/
1740 /* SerializeRecursive() */
1741 /************************************************************************/
1742
1743 void OGRMongoDBv3Layer::SerializeRecursive(
1744 bsoncxx::builder::basic::document &b, OGRFeature *poFeature,
1745 std::map<CPLString, IntOrMap *> &aoMap)
1746 {
1747 std::map<CPLString, IntOrMap *>::iterator oIter = aoMap.begin();
1748 for (; oIter != aoMap.end(); ++oIter)
1749 {
1750 IntOrMap *intOrMap = oIter->second;
1751 if (intOrMap->bIsMap)
1752 {
1753 bsoncxx::builder::basic::document subB;
1754 SerializeRecursive(subB, poFeature, *(intOrMap->u.poMap));
1755 b.append(kvp(std::string(oIter->first), subB.extract()));
1756 delete intOrMap->u.poMap;
1757 }
1758 else
1759 {
1760 int i = intOrMap->u.nField;
1761 if (i >= 0)
1762 {
1763 SerializeField(b, poFeature, i, oIter->first.c_str());
1764 }
1765 else
1766 {
1767 i = -i - 1;
1768 OGRGeometry *poGeom = poFeature->GetGeomFieldRef(i);
1769 SerializeGeometry(b, poGeom, i, oIter->first.c_str());
1770 }
1771 }
1772 delete intOrMap;
1773 }
1774 }
1775
1776 /************************************************************************/
1777 /* InsertInMap() */
1778 /************************************************************************/
1779
1780 void OGRMongoDBv3Layer::InsertInMap(
1781 IntOrMap *rootMap, std::map<std::vector<CPLString>, IntOrMap *> &aoMap,
1782 const std::vector<CPLString> &aosFieldPathFull, int nField)
1783 {
1784 std::vector<CPLString> aosFieldPath;
1785 std::vector<CPLString> aosFieldPathPrev;
1786 for (int j = 0; j < static_cast<int>(aosFieldPathFull.size()) - 1; j++)
1787 {
1788 aosFieldPath.push_back(aosFieldPathFull[j]);
1789 if (aoMap.find(aosFieldPath) == aoMap.end())
1790 {
1791 IntOrMap *intOrMap = new IntOrMap;
1792 intOrMap->bIsMap = TRUE;
1793 intOrMap->u.poMap = new std::map<CPLString, IntOrMap *>;
1794 aoMap[aosFieldPath] = intOrMap;
1795 }
1796 if (j > 0)
1797 {
1798 std::map<CPLString, IntOrMap *> *poPrevMap =
1799 aoMap[aosFieldPathPrev]->u.poMap;
1800 (*poPrevMap)[aosFieldPathFull[j]] = aoMap[aosFieldPath];
1801 }
1802 else
1803 (*(rootMap->u.poMap))[aosFieldPathFull[j]] = aoMap[aosFieldPath];
1804 aosFieldPathPrev.push_back(aosFieldPathFull[j]);
1805 }
1806 IntOrMap *intOrMap = new IntOrMap;
1807 intOrMap->bIsMap = FALSE;
1808 intOrMap->u.nField = nField;
1809 std::map<CPLString, IntOrMap *> *poPrevMap =
1810 aoMap[aosFieldPathPrev]->u.poMap;
1811 const CPLString &osLastComponent(aosFieldPathFull.back());
1812 CPLAssert((*poPrevMap).find(osLastComponent) == (*poPrevMap).end());
1813 (*(poPrevMap))[osLastComponent] = intOrMap;
1814 }
1815
1816 /************************************************************************/
1817 /* IsInList() */
1818 /************************************************************************/
1819
1820 static bool IsInList(int nVal, int nCount, const int *panList)
1821 {
1822 for (int i = 0; i < nCount; ++i)
1823 {
1824 if (panList[i] == nVal)
1825 return true;
1826 }
1827 return false;
1828 }
1829
1830 /************************************************************************/
1831 /* BuildBSONObjFromFeature() */
1832 /************************************************************************/
1833
1834 bsoncxx::document::value OGRMongoDBv3Layer::BuildBSONObjFromFeature(
1835 OGRFeature *poFeature, bool bUpdate, int nUpdatedFieldsCount,
1836 const int *panUpdatedFieldsIdx, int nUpdatedGeomFieldsCount,
1837 const int *panUpdatedGeomFieldsIdx)
1838 {
1839 bsoncxx::builder::basic::document b{};
1840
1841 int nJSonFieldIndex = m_poFeatureDefn->GetFieldIndex("_json");
1842 if (nJSonFieldIndex >= 0 &&
1843 poFeature->IsFieldSetAndNotNull(nJSonFieldIndex) &&
1844 (nUpdatedFieldsCount < 0 ||
1845 IsInList(nJSonFieldIndex, nUpdatedFieldsCount, panUpdatedFieldsIdx)))
1846 {
1847 CPLString osJSon(poFeature->GetFieldAsString(nJSonFieldIndex));
1848
1849 auto obj(bsoncxx::from_json(osJSon));
1850 auto obj_view(obj.view());
1851 if ((m_bIgnoreSourceID || !obj_view["_id"]) && !bUpdate)
1852 {
1853 bsoncxx::oid generated;
1854 b.append(kvp("_id", generated));
1855 poFeature->SetField(0, generated.to_string().c_str());
1856 }
1857 for (const auto &field : obj_view)
1858 {
1859 b.append(kvp(field.key(), field.get_value()));
1860 }
1861 return b.extract();
1862 }
1863
1864 if (poFeature->GetFID() >= 0 && !m_osFID.empty())
1865 {
1866 b.append(kvp(std::string(m_osFID),
1867 static_cast<int64_t>(poFeature->GetFID())));
1868 }
1869
1870 CPLAssert(static_cast<int>(m_aaosFieldPaths.size()) ==
1871 m_poFeatureDefn->GetFieldCount());
1872
1873 if (!poFeature->IsFieldSetAndNotNull(0) || (!bUpdate && m_bIgnoreSourceID))
1874 {
1875 bsoncxx::oid generated;
1876 b.append(kvp("_id", generated));
1877 poFeature->SetField(0, generated.to_string().c_str());
1878 }
1879 else
1880 b.append(kvp("_id", bsoncxx::oid(poFeature->GetFieldAsString(0))));
1881
1882 IntOrMap *rootMap = new IntOrMap;
1883 rootMap->bIsMap = TRUE;
1884 rootMap->u.poMap = new std::map<CPLString, IntOrMap *>;
1885 std::map<std::vector<CPLString>, IntOrMap *> aoMap;
1886
1887 {
1888 const int nStart = nUpdatedFieldsCount >= 0 ? 0 : 1;
1889 const int nStop = nUpdatedFieldsCount >= 0
1890 ? nUpdatedFieldsCount
1891 : m_poFeatureDefn->GetFieldCount();
1892 for (int i = nStart; i < nStop; i++)
1893 {
1894 const int iField =
1895 nUpdatedFieldsCount >= 0 ? panUpdatedFieldsIdx[i] : i;
1896 if (iField == 0)
1897 continue;
1898 if (!poFeature->IsFieldSet(iField))
1899 continue;
1900
1901 if (m_aaosFieldPaths[iField].size() > 1)
1902 {
1903 InsertInMap(rootMap, aoMap, m_aaosFieldPaths[iField], iField);
1904 }
1905 else
1906 {
1907 const char *pszFieldName =
1908 m_poFeatureDefn->GetFieldDefn(iField)->GetNameRef();
1909 SerializeField(b, poFeature, iField, pszFieldName);
1910 }
1911 }
1912 }
1913
1914 CPLAssert(static_cast<int>(m_aaosGeomFieldPaths.size()) ==
1915 m_poFeatureDefn->GetGeomFieldCount());
1916 CPLAssert(static_cast<int>(m_apoCT.size()) ==
1917 m_poFeatureDefn->GetGeomFieldCount());
1918 const int nStop = nUpdatedGeomFieldsCount >= 0
1919 ? nUpdatedGeomFieldsCount
1920 : m_poFeatureDefn->GetGeomFieldCount();
1921 for (int i = 0; i < nStop; i++)
1922 {
1923 const int iField =
1924 nUpdatedGeomFieldsCount >= 0 ? panUpdatedGeomFieldsIdx[i] : i;
1925 OGRGeometry *poGeom = poFeature->GetGeomFieldRef(iField);
1926 if (poGeom == nullptr)
1927 continue;
1928 if (!bUpdate && m_apoCT[iField] != nullptr)
1929 poGeom->transform(m_apoCT[i].get());
1930
1931 if (m_aaosGeomFieldPaths[iField].size() > 1)
1932 {
1933 InsertInMap(rootMap, aoMap, m_aaosGeomFieldPaths[i], -iField - 1);
1934 }
1935 else
1936 {
1937 const char *pszFieldName =
1938 m_poFeatureDefn->GetGeomFieldDefn(iField)->GetNameRef();
1939 SerializeGeometry(b, poGeom, iField, pszFieldName);
1940 }
1941 }
1942
1943 SerializeRecursive(b, poFeature, *(rootMap->u.poMap));
1944 delete rootMap->u.poMap;
1945 delete rootMap;
1946
1947 return b.extract();
1948 }
1949
1950 /************************************************************************/
1951 /* ICreateFeature() */
1952 /************************************************************************/
1953
1954 OGRErr OGRMongoDBv3Layer::ICreateFeature(OGRFeature *poFeature)
1955 {
1956 if (m_poDS->GetAccess() != GA_Update)
1957 {
1958 CPLError(CE_Failure, CPLE_AppDefined,
1959 "Dataset opened in read-only mode");
1960 return OGRERR_FAILURE;
1961 }
1962
1963 if (!m_bHasEstablishedFeatureDefn)
1964 EstablishFeatureDefn();
1965
1966 try
1967 {
1968 if (poFeature->GetFID() < 0)
1969 {
1970 if (m_nNextFID == 0)
1971 m_nNextFID = GetFeatureCount(false);
1972 poFeature->SetFID(++m_nNextFID);
1973 }
1974
1975 auto bsonObj(BuildBSONObjFromFeature(poFeature, false, -1, nullptr, -1,
1976 nullptr));
1977
1978 if (m_poDS->m_bBulkInsert)
1979 {
1980 constexpr size_t knMAX_DOCS_IN_BULK = 1000;
1981 if (m_aoDocsToInsert.size() == knMAX_DOCS_IN_BULK)
1982 SyncToDisk();
1983 m_aoDocsToInsert.emplace_back(std::move(bsonObj));
1984 }
1985 else
1986 {
1987 // std::string s(bsoncxx::to_json(bsonObj));
1988 // CPLDebug("MongoDBv3", "%s", s.c_str());
1989 m_oColl.insert_one(std::move(bsonObj));
1990 }
1991
1992 return OGRERR_NONE;
1993 }
1994 catch (const std::exception &ex)
1995 {
1996 CPLError(CE_Failure, CPLE_AppDefined, "%s: %s", "CreateFeature()",
1997 ex.what());
1998 return OGRERR_FAILURE;
1999 }
2000 }
2001
2002 /************************************************************************/
2003 /* ISetFeature() */
2004 /************************************************************************/
2005
2006 OGRErr OGRMongoDBv3Layer::ISetFeature(OGRFeature *poFeature)
2007 {
2008 const OGRErr err = PrepareForUpdateOrUpsert(poFeature);
2009 if (err != OGRERR_NONE)
2010 {
2011 return err;
2012 }
2013
2014 try
2015 {
2016 auto bsonObj(
2017 BuildBSONObjFromFeature(poFeature, true, -1, nullptr, -1, nullptr));
2018 auto view(bsonObj.view());
2019 auto filter(BuildIDMatchFilter(view, poFeature));
2020 auto ret =
2021 m_oColl.find_one_and_replace(std::move(filter), std::move(bsonObj));
2022 return ret ? OGRERR_NONE : OGRERR_NON_EXISTING_FEATURE;
2023 }
2024 catch (const std::exception &ex)
2025 {
2026 CPLError(CE_Failure, CPLE_AppDefined, "%s: %s", "SetFeature()",
2027 ex.what());
2028 return OGRERR_FAILURE;
2029 }
2030 }
2031
2032 /************************************************************************/
2033 /* PrepareForUpdateOrUpsert() */
2034 /************************************************************************/
2035
2036 OGRErr OGRMongoDBv3Layer::PrepareForUpdateOrUpsert(const OGRFeature *poFeature)
2037 {
2038 if (m_poDS->GetAccess() != GA_Update)
2039 {
2040 CPLError(CE_Failure, CPLE_AppDefined,
2041 "Dataset opened in read-only mode");
2042 return OGRERR_FAILURE;
2043 }
2044
2045 if (!m_bHasEstablishedFeatureDefn)
2046 EstablishFeatureDefn();
2047 if (!m_aoDocsToInsert.empty())
2048 SyncToDisk();
2049
2050 if (!poFeature->IsFieldSetAndNotNull(0))
2051 {
2052 CPLError(CE_Failure, CPLE_AppDefined, "_id field not set");
2053 return OGRERR_FAILURE;
2054 }
2055 return OGRERR_NONE;
2056 }
2057
2058 /************************************************************************/
2059 /* BuildIDMatchFilter() */
2060 /************************************************************************/
2061
2062 bsoncxx::document::value
2063 OGRMongoDBv3Layer::BuildIDMatchFilter(bsoncxx::document::view view,
2064 const OGRFeature *poFeature) const
2065 {
2066 bsoncxx::builder::basic::document filterBuilder{};
2067 filterBuilder.append(kvp("_id", view["_id"].get_oid().value));
2068 if (!m_osFID.empty())
2069 filterBuilder.append(kvp(std::string(m_osFID),
2070 static_cast<int64_t>(poFeature->GetFID())));
2071
2072 return filterBuilder.extract();
2073 }
2074
2075 /************************************************************************/
2076 /* IUpsertFeature() */
2077 /************************************************************************/
2078
2079 OGRErr OGRMongoDBv3Layer::IUpsertFeature(OGRFeature *poFeature)
2080 {
2081 if (!TestCapability(OLCUpsertFeature))
2082 return OGRERR_FAILURE;
2083
2084 const OGRErr err = PrepareForUpdateOrUpsert(poFeature);
2085 if (err != OGRERR_NONE)
2086 {
2087 return err;
2088 }
2089
2090 try
2091 {
2092 auto bsonObj(
2093 BuildBSONObjFromFeature(poFeature, true, -1, nullptr, -1, nullptr));
2094 auto view(bsonObj.view());
2095 auto filter(BuildIDMatchFilter(view, poFeature));
2096 m_oColl.find_one_and_update(
2097 std::move(filter), std::move(bsonObj),
2098 mongocxx::options::find_one_and_update().upsert(true));
2099 return OGRERR_NONE;
2100 }
2101 catch (const std::exception &ex)
2102 {
2103 CPLError(CE_Failure, CPLE_AppDefined, "%s: %s", "UpsertFeature()",
2104 ex.what());
2105 return OGRERR_FAILURE;
2106 }
2107 }
2108
2109 /************************************************************************/
2110 /* IUpdateFeature() */
2111 /************************************************************************/
2112
2113 OGRErr OGRMongoDBv3Layer::IUpdateFeature(OGRFeature *poFeature,
2114 int nUpdatedFieldsCount,
2115 const int *panUpdatedFieldsIdx,
2116 int nUpdatedGeomFieldsCount,
2117 const int *panUpdatedGeomFieldsIdx,
2118 bool /* bUpdateStyleString */)
2119 {
2120 if (!TestCapability(OLCUpdateFeature))
2121 return OGRERR_FAILURE;
2122
2123 const OGRErr err = PrepareForUpdateOrUpsert(poFeature);
2124 if (err != OGRERR_NONE)
2125 {
2126 return err;
2127 }
2128
2129 try
2130 {
2131 auto bsonObj(BuildBSONObjFromFeature(
2132 poFeature, true, nUpdatedFieldsCount, panUpdatedFieldsIdx,
2133 nUpdatedGeomFieldsCount, panUpdatedGeomFieldsIdx));
2134 auto view(bsonObj.view());
2135 auto filter(BuildIDMatchFilter(view, poFeature));
2136 auto ret =
2137 m_oColl.find_one_and_update(std::move(filter), std::move(bsonObj));
2138 return ret ? OGRERR_NONE : OGRERR_NON_EXISTING_FEATURE;
2139 }
2140 catch (const std::exception &ex)
2141 {
2142 CPLError(CE_Failure, CPLE_AppDefined, "%s: %s", "UpdateFeature()",
2143 ex.what());
2144 return OGRERR_FAILURE;
2145 }
2146 }
2147
2148 /************************************************************************/
2149 /* TestCapability() */
2150 /************************************************************************/
2151
2152 int OGRMongoDBv3Layer::TestCapability(const char *pszCap) const
2153 {
2154 if (EQUAL(pszCap, OLCStringsAsUTF8))
2155 {
2156 return true;
2157 }
2158 if (EQUAL(pszCap, OLCRandomRead))
2159 {
2160 return const_cast<OGRMongoDBv3Layer *>(this)->GetFIDColumn()[0] != 0;
2161 }
2162 if (EQUAL(pszCap, OLCFastSpatialFilter))
2163 {
2164 const int nGeomFieldCount = GetLayerDefn()->GetGeomFieldCount();
2165 for (int i = 0; i < nGeomFieldCount; i++)
2166 {
2167 if (m_aosGeomIndexes[i] == "none")
2168 {
2169 return false;
2170 }
2171 }
2172 return true;
2173 }
2174 if (EQUAL(pszCap, OLCCreateField) || EQUAL(pszCap, OLCCreateGeomField) ||
2175 EQUAL(pszCap, OLCUpsertFeature) || EQUAL(pszCap, OLCSequentialWrite) ||
2176 EQUAL(pszCap, OLCRandomWrite) || EQUAL(pszCap, OLCUpdateFeature))
2177 {
2178 return m_poDS->GetAccess() == GA_Update;
2179 }
2180 else if (EQUAL(pszCap, OLCDeleteFeature))
2181 {
2182 return m_poDS->GetAccess() == GA_Update &&
2183 const_cast<OGRMongoDBv3Layer *>(this)->GetFIDColumn()[0] != 0;
2184 }
2185
2186 return false;
2187 }
2188
2189 /************************************************************************/
2190 /* SetAttributeFilter() */
2191 /************************************************************************/
2192
2193 OGRErr OGRMongoDBv3Layer::SetAttributeFilter(const char *pszFilter)
2194 {
2195 m_oQueryAttr = bsoncxx::builder::basic::make_document();
2196
2197 if (pszFilter != nullptr && pszFilter[0] == '{')
2198 {
2199 OGRLayer::SetAttributeFilter(nullptr);
2200 try
2201 {
2202 m_oQueryAttr = bsoncxx::from_json(pszFilter);
2203 return OGRERR_NONE;
2204 }
2205 catch (const std::exception &ex)
2206 {
2207 CPLError(CE_Failure, CPLE_AppDefined, "%s: %s",
2208 "SetAttributeFilter()", ex.what());
2209 return OGRERR_FAILURE;
2210 }
2211 }
2212 return OGRLayer::SetAttributeFilter(pszFilter);
2213 }
2214
2215 /************************************************************************/
2216 /* ISetSpatialFilter() */
2217 /************************************************************************/
2218
2219 OGRErr OGRMongoDBv3Layer::ISetSpatialFilter(int iGeomField,
2220 const OGRGeometry *poGeomIn)
2221
2222 {
2223 m_iGeomFieldFilter = iGeomField;
2224
2225 m_oQuerySpat = bsoncxx::builder::basic::make_document();
2226 if (InstallFilter(poGeomIn) && poGeomIn)
2227 {
2228 OGREnvelope sEnvelope;
2229 poGeomIn->getEnvelope(&sEnvelope);
2230 if (sEnvelope.MaxX == sEnvelope.MinX)
2231 sEnvelope.MaxX += 1e-10;
2232 if (sEnvelope.MaxY == sEnvelope.MinY)
2233 sEnvelope.MaxY += 1e-10;
2234
2235 if (sEnvelope.MinX < -180)
2236 sEnvelope.MinX = -180;
2237 if (sEnvelope.MinY < -90)
2238 sEnvelope.MinY = -90;
2239 if (sEnvelope.MaxX > 180)
2240 sEnvelope.MaxX = 180;
2241 if (sEnvelope.MaxY > 90)
2242 sEnvelope.MaxY = 90;
2243 if (sEnvelope.MinX == -180 && sEnvelope.MinY == -90 &&
2244 sEnvelope.MaxX == 180 && sEnvelope.MaxY == 90)
2245 {
2246 return OGRERR_NONE;
2247 }
2248
2249 try
2250 {
2251 if (m_aosGeomIndexes[m_iGeomFieldFilter] == "2dsphere")
2252 {
2253 m_oQuerySpat = bsoncxx::from_json(CPLSPrintf(
2254 "{ \"%s\" : { \"$geoIntersects\" : "
2255 "{ \"$geometry\" : { \"type\" : \"Polygon\" , "
2256 "\"coordinates\" : [["
2257 "[%.16g,%.16g],[%.16g,%.16g],[%.16g,%.16g],[%.16g,%.16g],[%"
2258 ".16g,%.16g]]] } } } }",
2259 m_poFeatureDefn->GetGeomFieldDefn(iGeomField)->GetNameRef(),
2260 sEnvelope.MinX, sEnvelope.MinY, sEnvelope.MaxX,
2261 sEnvelope.MinY, sEnvelope.MaxX, sEnvelope.MaxY,
2262 sEnvelope.MinX, sEnvelope.MaxY, sEnvelope.MinX,
2263 sEnvelope.MinY));
2264 }
2265 else if (m_aosGeomIndexes[m_iGeomFieldFilter] == "2d")
2266 {
2267 m_oQuerySpat = bsoncxx::from_json(CPLSPrintf(
2268 "{ \"%s\" : { \"$geoWithin\" : "
2269 "{ \"$box\" : [ [ %.16g , %.16g ] , [ %.16g , %.16g ] ] } "
2270 "} }",
2271 m_poFeatureDefn->GetGeomFieldDefn(iGeomField)->GetNameRef(),
2272 sEnvelope.MinX, sEnvelope.MinY, sEnvelope.MaxX,
2273 sEnvelope.MaxY));
2274 }
2275 }
2276 catch (const std::exception &ex)
2277 {
2278 CPLError(CE_Failure, CPLE_AppDefined, "%s: %s",
2279 "SetSpatialFilter()", ex.what());
2280 return OGRERR_FAILURE;
2281 }
2282 }
2283 return OGRERR_NONE;
2284 }
2285
2286 /************************************************************************/
2287 /* GetLayer() */
2288 /************************************************************************/
2289
2290 const OGRLayer *OGRMongoDBv3Dataset::GetLayer(int nIndex) const
2291 {
2292 if (nIndex < 0 || nIndex >= GetLayerCount())
2293 return nullptr;
2294 return m_apoLayers[nIndex].get();
2295 }
2296
2297 /************************************************************************/
2298 /* GetLayerByName() */
2299 /************************************************************************/
2300
2301 OGRLayer *OGRMongoDBv3Dataset::GetLayerByName(const char *pszLayerName)
2302 {
2303 OGRLayer *poLayer = GDALDataset::GetLayerByName(pszLayerName);
2304 if (poLayer != nullptr)
2305 return poLayer;
2306
2307 for (const auto &l_poLayer : m_apoLayers)
2308 {
2309 l_poLayer->SyncToDisk();
2310 }
2311
2312 CPLString osDatabase;
2313 if (m_osDatabase.empty())
2314 {
2315 const char *pszDot = strchr(pszLayerName, '.');
2316 if (pszDot == nullptr)
2317 return nullptr;
2318 osDatabase = pszLayerName;
2319 osDatabase.resize(pszDot - pszLayerName);
2320 pszLayerName = pszDot + 1;
2321 }
2322 else
2323 osDatabase = m_osDatabase;
2324
2325 for (int i = 0; i < 2; i++)
2326 {
2327 try
2328 {
2329 auto db = m_oConn.database(osDatabase);
2330 auto aosCollections = db.list_collection_names();
2331 for (const auto &osCollection : aosCollections)
2332 {
2333 if (EQUAL(osCollection.c_str(), pszLayerName))
2334 {
2335 m_apoLayers.emplace_back(new OGRMongoDBv3Layer(
2336 this, osDatabase, osCollection.c_str()));
2337 return m_apoLayers.back().get();
2338 }
2339 }
2340 }
2341 catch (const std::exception &ex)
2342 {
2343 CPLError(CE_Failure, CPLE_AppDefined, "Command failed: %s",
2344 ex.what());
2345 }
2346 if (i == 0)
2347 {
2348 if (m_osDatabase.empty())
2349 break;
2350 const char *pszDot = strchr(pszLayerName, '.');
2351 if (pszDot == nullptr)
2352 break;
2353 osDatabase = pszLayerName;
2354 osDatabase.resize(pszDot - pszLayerName);
2355 pszLayerName = pszDot + 1;
2356 }
2357 }
2358
2359 return nullptr;
2360 }
2361
2362 /************************************************************************/
2363 /* CreateLayers() */
2364 /************************************************************************/
2365
2366 void OGRMongoDBv3Dataset::CreateLayers(mongocxx::database &db)
2367 {
2368 std::string dbName(db.name());
2369 auto aosCollections = db.list_collection_names();
2370 for (const auto &osCollection : aosCollections)
2371 {
2372 if (osCollection != "_ogr_metadata")
2373 {
2374 m_apoLayers.emplace_back(
2375 new OGRMongoDBv3Layer(this, dbName.c_str(), osCollection));
2376 }
2377 }
2378 }
2379
2380 /************************************************************************/
2381 /* Open() */
2382 /************************************************************************/
2383
2384 bool OGRMongoDBv3Dataset::Open(GDALOpenInfo *poOpenInfo)
2385 {
2386 eAccess = poOpenInfo->eAccess;
2387
2388 const char *pszHost =
2389 CSLFetchNameValueDef(poOpenInfo->papszOpenOptions, "HOST", "localhost");
2390 const char *pszPort =
2391 CSLFetchNameValueDef(poOpenInfo->papszOpenOptions, "PORT", "27017");
2392 const char *pszURI = CSLFetchNameValue(poOpenInfo->papszOpenOptions, "URI");
2393 if (pszURI == nullptr)
2394 {
2395 if (STARTS_WITH_CI(poOpenInfo->pszFilename, "mongodbv3:"))
2396 pszURI = poOpenInfo->pszFilename + strlen("mongodbv3:");
2397 else if (STARTS_WITH_CI(poOpenInfo->pszFilename, "mongodb:") ||
2398 STARTS_WITH_CI(poOpenInfo->pszFilename, "mongodb+srv:"))
2399 pszURI = poOpenInfo->pszFilename;
2400 }
2401 const char *pszUser =
2402 CSLFetchNameValue(poOpenInfo->papszOpenOptions, "USER");
2403 const char *pszPassword =
2404 CSLFetchNameValue(poOpenInfo->papszOpenOptions, "PASSWORD");
2405 if ((pszUser != nullptr && pszPassword == nullptr) ||
2406 (pszUser == nullptr && pszPassword != nullptr))
2407 {
2408 CPLError(CE_Failure, CPLE_AppDefined,
2409 "USER and PASSWORD open options must be both specified.");
2410 return false;
2411 }
2412
2413 try
2414 {
2415 mongocxx::uri uri;
2416 if (pszURI && pszURI[0])
2417 {
2418 uri = mongocxx::uri(pszURI);
2419 }
2420 else
2421 {
2422 CPLString osURI("mongodb://");
2423 if (pszUser && pszPassword)
2424 {
2425 osURI += pszUser;
2426 osURI += ':';
2427 osURI += pszPassword;
2428 osURI += '@';
2429 }
2430 osURI += pszHost;
2431 osURI += ':';
2432 osURI += pszPort;
2433 uri = mongocxx::uri(osURI);
2434 }
2435 m_osDatabase = uri.database();
2436 if (m_osDatabase.empty())
2437 {
2438 m_osDatabase = CSLFetchNameValueDef(poOpenInfo->papszOpenOptions,
2439 "DBNAME", "");
2440 }
2441
2442 CPLString osPEMKeyFile = CSLFetchNameValueDef(
2443 poOpenInfo->papszOpenOptions, "SSL_PEM_KEY_FILE", "");
2444 CPLString osPEMKeyPassword = CSLFetchNameValueDef(
2445 poOpenInfo->papszOpenOptions, "SSL_PEM_KEY_PASSWORD", "");
2446 CPLString osCAFile = CSLFetchNameValueDef(poOpenInfo->papszOpenOptions,
2447 "SSL_CA_FILE", "");
2448 CPLString osCRLFile = CSLFetchNameValueDef(poOpenInfo->papszOpenOptions,
2449 "SSL_CRL_FILE", "");
2450 bool bAllowInvalidCertificates =
2451 CPLFetchBool(poOpenInfo->papszOpenOptions,
2452 "SSL_ALLOW_INVALID_CERTIFICATES", false);
2453
2454 mongocxx::options::client client_options;
2455 if (!osPEMKeyFile.empty() || !osPEMKeyPassword.empty() ||
2456 !osCAFile.empty() || !osCRLFile.empty() ||
2457 bAllowInvalidCertificates)
2458 {
2459 #if BSONCXX_VERSION_MAJOR > 3 || BSONCXX_VERSION_MINOR >= 6
2460 mongocxx::options::tls tls_options;
2461 #else
2462 mongocxx::options::ssl tls_options;
2463 #endif
2464 if (!osPEMKeyFile.empty())
2465 tls_options.pem_file(osPEMKeyFile);
2466 if (!osPEMKeyPassword.empty())
2467 tls_options.pem_password(osPEMKeyPassword);
2468 if (!osCAFile.empty())
2469 tls_options.ca_file(osCAFile);
2470 if (!osCRLFile.empty())
2471 tls_options.crl_file(osCRLFile);
2472 tls_options.allow_invalid_certificates(bAllowInvalidCertificates);
2473 #if BSONCXX_VERSION_MAJOR > 3 || BSONCXX_VERSION_MINOR >= 6
2474 client_options.tls_opts(tls_options);
2475 #else
2476 client_options.ssl_opts(tls_options);
2477 #endif
2478 }
2479
2480 m_oConn = mongocxx::client(uri, client_options);
2481
2482 try
2483 {
2484 auto db =
2485 m_oConn[m_osDatabase.empty() ? "admin" : m_osDatabase.c_str()];
2486 auto ret =
2487 db.run_command(bsoncxx::from_json("{ \"buildInfo\" : 1 }"));
2488 std::string s(bsoncxx::to_json(ret.view()));
2489 CPLDebug("MongoDBv3", "%s", s.c_str());
2490 }
2491 catch (const std::exception &ex)
2492 {
2493 CPLDebug("MongoDBv3", "buildInfo(): %s", ex.what());
2494 }
2495
2496 if (m_osDatabase.empty())
2497 {
2498 auto dbs = m_oConn.list_databases();
2499 for (const auto &dbBson : dbs)
2500 {
2501 std::string dbName(get_string(dbBson["name"]));
2502 if (dbName == "admin" || dbName == "config" ||
2503 dbName == "local")
2504 {
2505 continue;
2506 }
2507 CPLDebug("MongoDBv3", "Iterating over database %s",
2508 dbName.c_str());
2509 auto db = m_oConn.database(dbName);
2510 CreateLayers(db);
2511 }
2512 }
2513 else
2514 {
2515 auto db = m_oConn.database(m_osDatabase);
2516 CreateLayers(db);
2517 }
2518 }
2519 catch (const std::exception &ex)
2520 {
2521 CPLError(CE_Failure, CPLE_AppDefined, "%s", ex.what());
2522 return false;
2523 }
2524
2525 m_nBatchSize = atoi(
2526 CSLFetchNameValueDef(poOpenInfo->papszOpenOptions, "BATCH_SIZE", "0"));
2527 m_nFeatureCountToEstablishFeatureDefn = atoi(
2528 CSLFetchNameValueDef(poOpenInfo->papszOpenOptions,
2529 "FEATURE_COUNT_TO_ESTABLISH_FEATURE_DEFN", "100"));
2530 m_bJSonField =
2531 CPLFetchBool(poOpenInfo->papszOpenOptions, "JSON_FIELD", false);
2532 m_bFlattenNestedAttributes = CPLFetchBool(
2533 poOpenInfo->papszOpenOptions, "FLATTEN_NESTED_ATTRIBUTES", true);
2534 m_osFID =
2535 CSLFetchNameValueDef(poOpenInfo->papszOpenOptions, "FID", "ogc_fid");
2536 m_bUseOGRMetadata =
2537 CPLFetchBool(poOpenInfo->papszOpenOptions, "USE_OGR_METADATA", true);
2538 m_bBulkInsert =
2539 CPLFetchBool(poOpenInfo->papszOpenOptions, "BULK_INSERT", true);
2540
2541 return true;
2542 }
2543
2544 /************************************************************************/
2545 /* ICreateLayer() */
2546 /************************************************************************/
2547
2548 OGRLayer *
2549 OGRMongoDBv3Dataset::ICreateLayer(const char *pszName,
2550 const OGRGeomFieldDefn *poGeomFieldDefn,
2551 CSLConstList papszOptions)
2552 {
2553 if (m_osDatabase.empty())
2554 {
2555 CPLError(CE_Failure, CPLE_AppDefined,
2556 "Cannot create layer/collection when dataset opened without "
2557 "explicit database");
2558 return nullptr;
2559 }
2560
2561 if (eAccess != GA_Update)
2562 {
2563 CPLError(CE_Failure, CPLE_AppDefined,
2564 "Dataset opened in read-only mode");
2565 return nullptr;
2566 }
2567
2568 for (int i = 0; i < static_cast<int>(m_apoLayers.size()); i++)
2569 {
2570 if (EQUAL(m_apoLayers[i]->GetName(), pszName))
2571 {
2572 if (CSLFetchNameValue(papszOptions, "OVERWRITE") != nullptr &&
2573 !EQUAL(CSLFetchNameValue(papszOptions, "OVERWRITE"), "NO"))
2574 {
2575 DeleteLayer(i);
2576 }
2577 else
2578 {
2579 CPLError(CE_Failure, CPLE_AppDefined,
2580 "Layer %s already exists, CreateLayer failed.\n"
2581 "Use the layer creation option OVERWRITE=YES to "
2582 "replace it.",
2583 pszName);
2584 return nullptr;
2585 }
2586 }
2587 }
2588
2589 try
2590 {
2591 m_oConn[m_osDatabase].create_collection(std::string(pszName));
2592 }
2593 catch (const std::exception &ex)
2594 {
2595 CPLError(CE_Failure, CPLE_AppDefined, "%s", ex.what());
2596 return nullptr;
2597 }
2598
2599 m_apoLayers.emplace_back(
2600 new OGRMongoDBv3Layer(this, m_osDatabase, pszName));
2601 auto poLayer = m_apoLayers.back().get();
2602
2603 poLayer->m_osFID = CSLFetchNameValueDef(papszOptions, "FID", "ogc_fid");
2604 poLayer->m_bLayerMetadataUpdatable =
2605 CPLFetchBool(papszOptions, "WRITE_OGR_METADATA", true);
2606 poLayer->m_bUpdateLayerMetadata = poLayer->m_bLayerMetadataUpdatable;
2607 poLayer->m_bDotAsNestedField =
2608 CPLFetchBool(papszOptions, "DOT_AS_NESTED_FIELD", true);
2609 poLayer->m_bIgnoreSourceID =
2610 CPLFetchBool(papszOptions, "IGNORE_SOURCE_ID", false);
2611 poLayer->m_bCreateSpatialIndex =
2612 CPLFetchBool(papszOptions, "SPATIAL_INDEX", true);
2613
2614 if (poGeomFieldDefn && poGeomFieldDefn->GetType() != wkbNone)
2615 {
2616 const char *pszGeometryName =
2617 CSLFetchNameValueDef(papszOptions, "GEOMETRY_NAME", "geometry");
2618 OGRGeomFieldDefn oFieldDefn(poGeomFieldDefn);
2619 oFieldDefn.SetName(pszGeometryName);
2620 poLayer->CreateGeomField(&oFieldDefn, FALSE);
2621 }
2622
2623 return poLayer;
2624 }
2625
2626 /************************************************************************/
2627 /* DeleteLayer() */
2628 /************************************************************************/
2629
2630 OGRErr OGRMongoDBv3Dataset::DeleteLayer(int iLayer)
2631
2632 {
2633 if (eAccess != GA_Update)
2634 {
2635 CPLError(CE_Failure, CPLE_AppDefined,
2636 "Dataset opened in read-only mode");
2637 return OGRERR_FAILURE;
2638 }
2639
2640 if (iLayer < 0 || iLayer >= static_cast<int>(m_apoLayers.size()))
2641 return OGRERR_FAILURE;
2642
2643 /* -------------------------------------------------------------------- */
2644 /* Blow away our OGR structures related to the layer. This is */
2645 /* pretty dangerous if anything has a reference to this layer! */
2646 /* -------------------------------------------------------------------- */
2647 CPLString osLayerName = m_apoLayers[iLayer]->GetName();
2648 CPLDebug("MongoDB", "DeleteLayer(%s)", osLayerName.c_str());
2649
2650 try
2651 {
2652 {
2653 bsoncxx::builder::basic::document b{};
2654 std::string colName(m_apoLayers[iLayer]->m_oColl.name());
2655 b.append(kvp("layer", colName));
2656 m_apoLayers[iLayer]->m_oDb["_ogr_metadata"].find_one_and_delete(
2657 b.extract());
2658 }
2659
2660 m_apoLayers[iLayer]->m_oColl.drop();
2661
2662 m_apoLayers.erase(m_apoLayers.begin() + iLayer);
2663
2664 return OGRERR_NONE;
2665 }
2666 catch (const std::exception &ex)
2667 {
2668 CPLError(CE_Failure, CPLE_AppDefined, "%s: %s", "DeleteLayer()",
2669 ex.what());
2670 return OGRERR_FAILURE;
2671 }
2672 }
2673
2674 /************************************************************************/
2675 /* TestCapability() */
2676 /************************************************************************/
2677
2678 int OGRMongoDBv3Dataset::TestCapability(const char *pszCap) const
2679
2680 {
2681 if (EQUAL(pszCap, ODsCCreateLayer) || EQUAL(pszCap, ODsCDeleteLayer) ||
2682 EQUAL(pszCap, ODsCCreateGeomFieldAfterCreateLayer))
2683 return eAccess == GA_Update;
2684 else
2685 return FALSE;
2686 }
2687
2688 /************************************************************************/
2689 /* OGRMongoDBv3SingleFeatureLayer */
2690 /************************************************************************/
2691
2692 class OGRMongoDBv3SingleFeatureLayer final : public OGRLayer
2693 {
2694 OGRFeatureDefn *m_poFeatureDefn;
2695 CPLString osVal;
2696 int iNextShapeId;
2697
2698 public:
2699 explicit OGRMongoDBv3SingleFeatureLayer(const char *pszVal);
2700
2701 ~OGRMongoDBv3SingleFeatureLayer() override
2702 {
2703 m_poFeatureDefn->Release();
2704 }
2705
2706 void ResetReading() override
2707 {
2708 iNextShapeId = 0;
2709 }
2710
2711 OGRFeature *GetNextFeature() override;
2712
2713 const OGRFeatureDefn *GetLayerDefn() const override
2714 {
2715 return m_poFeatureDefn;
2716 }
2717
2718 int TestCapability(const char *) const override
2719 {
2720 return FALSE;
2721 }
2722 };
2723
2724 /************************************************************************/
2725 /* OGRMongoDBv3SingleFeatureLayer() */
2726 /************************************************************************/
2727
2728 OGRMongoDBv3SingleFeatureLayer::OGRMongoDBv3SingleFeatureLayer(
2729 const char *pszVal)
2730 {
2731 m_poFeatureDefn = new OGRFeatureDefn("RESULT");
2732 m_poFeatureDefn->Reference();
2733 OGRFieldDefn oField("_json", OFTString);
2734 m_poFeatureDefn->AddFieldDefn(&oField);
2735
2736 iNextShapeId = 0;
2737 osVal = pszVal;
2738 }
2739
2740 /************************************************************************/
2741 /* GetNextFeature() */
2742 /************************************************************************/
2743
2744 OGRFeature *OGRMongoDBv3SingleFeatureLayer::GetNextFeature()
2745 {
2746 if (iNextShapeId != 0)
2747 return nullptr;
2748
2749 OGRFeature *poFeature = new OGRFeature(m_poFeatureDefn);
2750 poFeature->SetField(0, osVal);
2751 poFeature->SetFID(iNextShapeId++);
2752 return poFeature;
2753 }
2754
2755 /************************************************************************/
2756 /* ExecuteSQL() */
2757 /************************************************************************/
2758
2759 OGRLayer *OGRMongoDBv3Dataset::ExecuteSQL(const char *pszSQLCommand,
2760 OGRGeometry *poSpatialFilter,
2761 const char *pszDialect)
2762 {
2763 for (const auto &poLayer : m_apoLayers)
2764 {
2765 poLayer->SyncToDisk();
2766 }
2767
2768 /* -------------------------------------------------------------------- */
2769 /* Special case DELLAYER: command. */
2770 /* -------------------------------------------------------------------- */
2771 if (STARTS_WITH_CI(pszSQLCommand, "DELLAYER:"))
2772 {
2773 const char *pszLayerName = pszSQLCommand + 9;
2774
2775 while (*pszLayerName == ' ')
2776 pszLayerName++;
2777
2778 for (int iLayer = 0; iLayer < static_cast<int>(m_apoLayers.size());
2779 iLayer++)
2780 {
2781 if (EQUAL(m_apoLayers[iLayer]->GetName(), pszLayerName))
2782 {
2783 DeleteLayer(iLayer);
2784 break;
2785 }
2786 }
2787 return nullptr;
2788 }
2789
2790 /* -------------------------------------------------------------------- */
2791 /* Special case WRITE_OGR_METADATA command. */
2792 /* -------------------------------------------------------------------- */
2793 if (STARTS_WITH_CI(pszSQLCommand, "WRITE_OGR_METADATA "))
2794 {
2795 if (eAccess != GA_Update)
2796 {
2797 CPLError(CE_Failure, CPLE_AppDefined,
2798 "Dataset opened in read-only mode");
2799 return nullptr;
2800 }
2801 const char *pszLayerName =
2802 pszSQLCommand + strlen("WRITE_OGR_METADATA ");
2803 auto poLayer =
2804 static_cast<OGRMongoDBv3Layer *>(GetLayerByName(pszLayerName));
2805 if (poLayer == nullptr)
2806 {
2807 CPLError(CE_Failure, CPLE_AppDefined, "Layer %s not found",
2808 pszLayerName);
2809 return nullptr;
2810 }
2811 poLayer->GetLayerDefn(); // force schema discovery
2812 poLayer->m_bLayerMetadataUpdatable = true;
2813 poLayer->m_bUpdateLayerMetadata = true;
2814 poLayer->SyncToDisk();
2815
2816 return nullptr;
2817 }
2818
2819 if (pszDialect != nullptr && EQUAL(pszDialect, "MONGODB"))
2820 {
2821 if (m_osDatabase.empty())
2822 {
2823 CPLError(CE_Failure, CPLE_AppDefined,
2824 "Cannot run ExecuteSQL() when dataset opened without "
2825 "explicit database");
2826 return nullptr;
2827 }
2828 try
2829 {
2830 auto ret = m_oConn[m_osDatabase].run_command(
2831 bsoncxx::from_json(pszSQLCommand));
2832 return new OGRMongoDBv3SingleFeatureLayer(
2833 bsoncxx::to_json(ret).c_str());
2834 }
2835 catch (const std::exception &ex)
2836 {
2837 CPLError(CE_Failure, CPLE_AppDefined, "Command failed: %s",
2838 ex.what());
2839 return nullptr;
2840 }
2841 }
2842 else
2843 {
2844 return GDALDataset::ExecuteSQL(pszSQLCommand, poSpatialFilter,
2845 pszDialect);
2846 }
2847 }
2848
2849 /************************************************************************/
2850 /* ReleaseResultSet() */
2851 /************************************************************************/
2852
2853 void OGRMongoDBv3Dataset::ReleaseResultSet(OGRLayer *poLayer)
2854 {
2855 delete poLayer;
2856 }
2857
2858 /************************************************************************/
2859 /* OGRMongoDBv3DriverOpen() */
2860 /************************************************************************/
2861
2862 static GDALDataset *OGRMongoDBv3DriverOpen(GDALOpenInfo *poOpenInfo)
2863
2864 {
2865 if (!OGRMongoDBv3DriverIdentify(poOpenInfo))
2866 return nullptr;
2867
2868 {
2869 static std::mutex oMutex;
2870 std::lock_guard<std::mutex> oLock(oMutex);
2871 if (g_pInst == nullptr)
2872 {
2873 if (!g_bCanInstantiateMongo)
2874 {
2875 CPLError(CE_Failure, CPLE_AppDefined,
2876 "MongoDB client has been previously shut down and "
2877 "can no longer be reinitialized");
2878 return nullptr;
2879 }
2880
2881 #ifdef use_logger
2882 class logger final : public mongocxx::logger
2883 {
2884 public:
2885 explicit logger()
2886 {
2887 }
2888
2889 void
2890 operator()(mongocxx::log_level level,
2891 bsoncxx::stdx::string_view /*domain*/,
2892 bsoncxx::stdx::string_view message) noexcept override
2893 {
2894 if (level >= mongocxx::log_level::k_trace)
2895 return;
2896 std::string tmp(message);
2897 CPLDebug("MongoDBv3", "%s", tmp.c_str());
2898 }
2899 };
2900 #endif
2901 g_pInst = new mongocxx::instance(
2902 #ifdef use_logger
2903 bsoncxx::stdx::make_unique<logger>()
2904 #endif
2905 );
2906 }
2907 }
2908
2909 auto poDS = new OGRMongoDBv3Dataset();
2910 if (!poDS->Open(poOpenInfo))
2911 {
2912 delete poDS;
2913 poDS = nullptr;
2914 }
2915
2916 return poDS;
2917 }
2918
2919 /************************************************************************/
2920 /* OGRMongoDBv3DriverUnload() */
2921 /************************************************************************/
2922
2923 static void OGRMongoDBv3DriverUnload(GDALDriver *)
2924 {
2925 if (g_pInst != nullptr)
2926 {
2927 delete g_pInst;
2928 g_pInst = nullptr;
2929 g_bCanInstantiateMongo = false;
2930 }
2931 }
2932
2933 /************************************************************************/
2934 /* RegisterOGRMongoDBv3() */
2935 /************************************************************************/
2936
2937 void RegisterOGRMongoDBv3()
2938 {
2939 if (GDALGetDriverByName(DRIVER_NAME) != nullptr)
2940 return;
2941
2942 GDALDriver *poDriver = new GDALDriver();
2943 OGRMongoDBv3DriverSetCommonMetadata(poDriver);
2944
2945 poDriver->pfnOpen = OGRMongoDBv3DriverOpen;
2946 poDriver->pfnUnloadDriver = OGRMongoDBv3DriverUnload;
2947
2948 GetGDALDriverManager()->RegisterDriver(poDriver);
2949 }