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

Method put

nodedb/src/engine/kv/engine_write.rs:22–120  ·  view source on GitHub ↗
(
        &mut self,
        tenant_id: u64,
        collection: &str,
        key: &[u8],
        value: &[u8],
        ttl_ms: u64,
        now_ms: u64,
        surrogate: Surrogate,
    )

Source from the content-addressed store, hash-verified

20 /// one — existing entries preserve their bound surrogate either way.
21 #[allow(clippy::too_many_arguments)]
22 pub fn put(
23 &mut self,
24 tenant_id: u64,
25 collection: &str,
26 key: &[u8],
27 value: &[u8],
28 ttl_ms: u64,
29 now_ms: u64,
30 surrogate: Surrogate,
31 ) -> Option<Vec<u8>> {
32 let expire_at = if ttl_ms > 0 {
33 now_ms + ttl_ms
34 } else {
35 NO_EXPIRY
36 };
37
38 let tkey = table_key(tenant_id, collection);
39
40 // Single-pass: check indexes + get old entry meta in one HashMap lookup.
41 let has_indexes = self.indexes.get(&tkey).is_some_and(|idx| !idx.is_empty());
42 let old_expire = self
43 .tables
44 .get(&tkey)
45 .and_then(|t| t.get_entry_meta(key))
46 .and_then(|m| {
47 if m.has_ttl {
48 Some(m.expire_at_ms)
49 } else {
50 None
51 }
52 });
53
54 // Cancel old expiry (before mutating the table).
55 if let Some(old_ms) = old_expire {
56 let composite = expiry_key(tenant_id, collection, key);
57 self.expiry.cancel(&composite, old_ms);
58 }
59
60 // Insert/update. Use get_mut (no clone) for existing tables,
61 // entry (clones tkey) only for first-time table creation.
62 let table = if let Some(t) = self.tables.get_mut(&tkey) {
63 t
64 } else {
65 self.hash_to_tenant.entry(tkey).or_insert(tenant_id);
66 self.hash_to_collection
67 .entry(tkey)
68 .or_insert_with(|| collection.to_string());
69 self.tables.entry(tkey).or_insert_with(|| {
70 KvHashTable::new(
71 self.default_capacity,
72 self.load_factor_threshold,
73 self.rehash_batch_size,
74 self.inline_threshold,
75 )
76 })
77 };
78 let old = table.put(key, value, expire_at, surrogate);
79

Callers 15

scan_basicFunction · 0.45
scan_with_count_limitFunction · 0.45
scan_skips_expiredFunction · 0.45
atomic_putMethod · 0.45
incr_overflowFunction · 0.45
incr_type_mismatchFunction · 0.45
cas_successFunction · 0.45
cas_failureFunction · 0.45
getset_existing_keyFunction · 0.45
batch_putMethod · 0.45

Calls 15

table_keyFunction · 0.85
expiry_keyFunction · 0.85
get_entry_metaMethod · 0.80
entryMethod · 0.80
to_stringMethod · 0.80
collectMethod · 0.80
getMethod · 0.45
is_emptyMethod · 0.45
cancelMethod · 0.45
get_mutMethod · 0.45
insertMethod · 0.45

Tested by 15

scan_basicFunction · 0.36
scan_with_count_limitFunction · 0.36
scan_skips_expiredFunction · 0.36
incr_overflowFunction · 0.36
incr_type_mismatchFunction · 0.36
cas_successFunction · 0.36
cas_failureFunction · 0.36
getset_existing_keyFunction · 0.36
basic_get_put_deleteFunction · 0.36
ttl_expiry_via_tickFunction · 0.36