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

Method getset

nodedb/src/engine/kv/engine_atomic.rs:241–292  ·  view source on GitHub ↗

Atomic get-and-set: sets new value, returns old value. If key didn't exist, returns `None`. Preserves existing TTL.

(
        &mut self,
        tenant_id: u64,
        collection: &str,
        key: &[u8],
        new_value: &[u8],
        now_ms: u64,
    )

Source from the content-addressed store, hash-verified

239 /// If key didn't exist, returns `None`.
240 /// Preserves existing TTL.
241 pub fn getset(
242 &mut self,
243 tenant_id: u64,
244 collection: &str,
245 key: &[u8],
246 new_value: &[u8],
247 now_ms: u64,
248 ) -> Option<Vec<u8>> {
249 let tkey = table_key(tenant_id, collection);
250 let table = self.ensure_table(tkey, tenant_id, collection);
251 let old = table.get(key, now_ms).map(|v| v.to_vec());
252
253 // For typed KV: update the first non-key string field in the map.
254 let write_bytes = if let Some(ref cur) = old
255 && let Ok(nodedb_types::Value::Object(mut map)) = nodedb_types::value_from_msgpack(cur)
256 && map.len() > 1
257 {
258 let new_str = String::from_utf8_lossy(new_value).to_string();
259 let mut updated = false;
260 for (k, v) in map.iter_mut() {
261 if k == "key" {
262 continue;
263 }
264 if matches!(v, nodedb_types::Value::String(_)) {
265 *v = nodedb_types::Value::String(new_str.clone());
266 updated = true;
267 break;
268 }
269 }
270 if updated {
271 nodedb_types::value_to_msgpack(&nodedb_types::Value::Object(map))
272 .unwrap_or_else(|_| new_value.to_vec())
273 } else {
274 new_value.to_vec()
275 }
276 } else {
277 new_value.to_vec()
278 };
279
280 // GetSet preserves existing TTL (ttl_ms = 0).
281 self.atomic_put(
282 tenant_id,
283 collection,
284 tkey,
285 key,
286 &write_bytes,
287 0,
288 now_ms,
289 old.is_none(),
290 );
291 old
292 }
293
294 /// Ensure a hash table exists for (tenant, collection), creating if needed.
295 /// Returns a mutable reference to the table.

Callers 3

execute_kv_getsetMethod · 0.80
getset_new_keyFunction · 0.80
getset_existing_keyFunction · 0.80

Calls 11

table_keyFunction · 0.85
value_from_msgpackFunction · 0.85
value_to_msgpackFunction · 0.85
ensure_tableMethod · 0.80
to_stringMethod · 0.80
iter_mutMethod · 0.80
atomic_putMethod · 0.80
getMethod · 0.45
to_vecMethod · 0.45
lenMethod · 0.45
cloneMethod · 0.45

Tested by 2

getset_new_keyFunction · 0.64
getset_existing_keyFunction · 0.64