MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / insert_spatial_doc

Function insert_spatial_doc

nodedb/src/data/executor/handlers/spatial.rs:418–454  ·  view source on GitHub ↗

Insert a document directly into sparse storage and the R-tree. Returns the hex doc_id.

(
        core: &mut crate::data::executor::core_loop::CoreLoop,
        tid: u64,
        collection: &str,
        field: &str,
        surrogate: Surrogate,
        lng: f64,
        lat: f64,
    

Source from the content-addressed store, hash-verified

416 /// Insert a document directly into sparse storage and the R-tree.
417 /// Returns the hex doc_id.
418 fn insert_spatial_doc(
419 core: &mut crate::data::executor::core_loop::CoreLoop,
420 tid: u64,
421 collection: &str,
422 field: &str,
423 surrogate: Surrogate,
424 lng: f64,
425 lat: f64,
426 ) -> String {
427 let doc_id = crate::engine::document::store::surrogate_to_doc_id(surrogate);
428
429 // Build a minimal msgpack document with a GeoJSON Point field.
430 let geojson = serde_json::json!({
431 field: { "type": "Point", "coordinates": [lng, lat] },
432 "id": &doc_id
433 });
434 let msgpack = nodedb_types::json_to_msgpack(&geojson).unwrap();
435
436 core.sparse.put(tid, collection, &doc_id, &msgpack).unwrap();
437
438 // Manually populate the R-tree and the doc-map.
439 let geom: nodedb_types::geometry::Geometry =
440 serde_json::from_value(serde_json::json!({"type":"Point","coordinates":[lng,lat]}))
441 .unwrap();
442 let bbox = nodedb_types::bbox::geometry_bbox(&geom);
443 let tid_id = TenantId::new(tid);
444 let spatial_key = (tid_id, collection.to_string(), field.to_string());
445 let entry_id = fnv1a_hash(doc_id.as_bytes());
446 let rtree = core.spatial_indexes.entry(spatial_key.clone()).or_default();
447 rtree.insert(RTreeEntry { id: entry_id, bbox });
448 core.spatial_doc_map.insert(
449 (tid_id, collection.to_string(), field.to_string(), entry_id),
450 doc_id.clone(),
451 );
452
453 doc_id
454 }
455
456 /// GeoJSON Point query centred on (0.0, 0.0) for DWithin.
457 fn origin_point() -> nodedb_types::geometry::Geometry {

Callers 1

Calls 10

surrogate_to_doc_idFunction · 0.85
geometry_bboxFunction · 0.85
to_stringMethod · 0.80
entryMethod · 0.80
json_to_msgpackFunction · 0.50
fnv1a_hashFunction · 0.50
putMethod · 0.45
as_bytesMethod · 0.45
cloneMethod · 0.45
insertMethod · 0.45

Tested by 1