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

Method remove_document

nodedb/src/engine/sparse/inverted/indexing.rs:164–247  ·  view source on GitHub ↗

Remove a document from the inverted index.

(
        &self,
        tid: TenantId,
        collection: &str,
        surrogate: Surrogate,
    )

Source from the content-addressed store, hash-verified

162
163 /// Remove a document from the inverted index.
164 pub fn remove_document(
165 &self,
166 tid: TenantId,
167 collection: &str,
168 surrogate: Surrogate,
169 ) -> crate::Result<()> {
170 let t = tid.as_u64();
171
172 let db = self.inner.backend().db();
173 let write_txn = db.begin_write().map_err(|e| inverted_err("write txn", e))?;
174 {
175 let mut postings_table = write_txn
176 .open_table(POSTINGS)
177 .map_err(|e| inverted_err("open postings", e))?;
178
179 let terms: Vec<String> = postings_table
180 .range((t, collection, "")..=(t, collection, "\u{10ffff}"))
181 .map_err(|e| inverted_err("range", e))?
182 .filter_map(|r| r.ok().map(|(k, _)| k.value().2.to_string()))
183 .collect();
184
185 let mut updates: Vec<(String, Option<Vec<u8>>)> = Vec::new();
186 for term in &terms {
187 if let Ok(Some(val)) = postings_table.get((t, collection, term.as_str())) {
188 let mut list: Vec<Posting> =
189 zerompk::from_msgpack(val.value()).unwrap_or_default();
190 let before = list.len();
191 list.retain(|p| p.doc_id != surrogate);
192 if list.len() != before {
193 if list.is_empty() {
194 updates.push((term.clone(), None));
195 } else {
196 let bytes = zerompk::to_msgpack_vec(&list).unwrap_or_default();
197 updates.push((term.clone(), Some(bytes)));
198 }
199 }
200 }
201 }
202
203 for (term, new_val) in &updates {
204 match new_val {
205 None => {
206 postings_table
207 .remove((t, collection, term.as_str()))
208 .map_err(|e| inverted_err("remove posting", e))?;
209 }
210 Some(bytes) => {
211 postings_table
212 .insert((t, collection, term.as_str()), bytes.as_slice())
213 .map_err(|e| inverted_err("update posting", e))?;
214 }
215 }
216 }
217
218 let mut lengths = write_txn
219 .open_table(DOC_LENGTHS)
220 .map_err(|e| inverted_err("open doc_lengths", e))?;
221

Callers 1

remove_documentFunction · 0.45

Calls 15

inverted_errFunction · 0.85
begin_writeMethod · 0.80
collectMethod · 0.80
to_stringMethod · 0.80
as_u64Method · 0.45
dbMethod · 0.45
backendMethod · 0.45
rangeMethod · 0.45
okMethod · 0.45
getMethod · 0.45
as_strMethod · 0.45
lenMethod · 0.45

Tested by 1

remove_documentFunction · 0.36