1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
use super::*;
impl EmbeddedStore {
/// Returns an owned copy of the value for `key`.
pub fn get(&self, key: &[u8]) -> Option<Bytes> {
let now_ms = now_millis();
let route = self.route_key(key);
self.get_with_route(route, key, now_ms)
}
/// Single-threaded fused GET + RESP encode. Bypasses the per-shard
/// `RwLock` entirely via `data_ptr()` — saves ~5-10ns per GET in atomic
/// ops.
///
/// # Safety
///
/// The caller must guarantee that no other thread is concurrently accessing
/// any shard of this `EmbeddedStore`. This is true in the `sc=1`
/// multi-direct path where there is exactly one worker thread, but is
/// unsafe in any other configuration.
pub unsafe fn get_blob_string_into_single_threaded(
&self,
key: &[u8],
out: &mut bytes::BytesMut,
) -> bool {
#[cfg(not(feature = "unsafe"))]
{
self.get_blob_string_into(key, out)
}
#[cfg(feature = "unsafe")]
{
let key_hash = hash_key(key);
// SAFETY: forwarded from this function's single-threaded contract.
unsafe { self.get_blob_string_hashed_into_single_threaded(key_hash, key, out) }
}
}
/// Prehashed variant of `get_blob_string_into_single_threaded` for the
/// RESP fast path, where the key bytes have already been validated.
///
/// # Safety
///
/// Same contract as `get_blob_string_into_single_threaded`.
pub unsafe fn get_blob_string_hashed_into_single_threaded(
&self,
key_hash: u64,
key: &[u8],
out: &mut bytes::BytesMut,
) -> bool {
#[cfg(not(feature = "unsafe"))]
{
self.get_blob_string_hashed_into(key_hash, key, out)
}
#[cfg(feature = "unsafe")]
{
if self.shards.len() == 1 {
// SAFETY (per fn contract): no other thread is accessing this shard.
let shard = unsafe { &*self.shards[0].data_ptr() };
if can_skip_session_lookup(key, &shard.session_slots) {
let value = if shard.map.has_no_ttl_entries() {
shard.map.get_ref_hashed_shared_no_ttl(key_hash, key)
} else {
shard.map.get_ref_hashed_shared(key_hash, key, now_millis())
};
if let Some(value) = value {
write_resp_blob_string_into(out, value);
return true;
}
return false;
}
}
let route = self.route_key(key);
// SAFETY (per fn contract): no other thread is accessing this shard.
let shard = unsafe { &*self.shards[route.shard_id].data_ptr() };
// Skip `now_millis()` syscall when no TTL entries exist — the
// expiration filter is a no-op anyway.
let now_ms = if shard.map.has_no_ttl_entries() {
0
} else {
now_millis()
};
if uses_flat_key_storage(self.route_mode, key) {
if let Some(value) = shard.map.get_ref_hashed_shared(route.key_hash, key, now_ms) {
write_resp_blob_string_into(out, value);
return true;
}
return false;
}
// Session-prefixed keys: fall back to the locked path. Bench doesn't
// hit this.
self.get_blob_string_into(key, out)
}
}
/// Single-threaded SET that bypasses the per-shard `RwLock`.
///
/// # Safety
///
/// Same contract as `get_blob_string_into_single_threaded`.
pub unsafe fn set_single_threaded(&self, key: &[u8], value: &[u8], ttl_ms: Option<u64>) {
#[cfg(not(feature = "unsafe"))]
{
self.set(key.to_vec(), value.to_vec(), ttl_ms);
}
#[cfg(feature = "unsafe")]
{
let key_hash = hash_key(key);
// SAFETY: forwarded from this function's single-threaded contract.
unsafe { self.set_single_threaded_hashed(key_hash, key, value, ttl_ms) };
}
}
/// Prehashed variant of `set_single_threaded` for canonical RESP SET.
///
/// # Safety
///
/// Same contract as `set_single_threaded`.
pub unsafe fn set_single_threaded_hashed(
&self,
key_hash: u64,
key: &[u8],
value: &[u8],
ttl_ms: Option<u64>,
) {
#[cfg(not(feature = "unsafe"))]
{
self.set_slice_prehashed(key_hash, key, value, ttl_ms);
}
#[cfg(feature = "unsafe")]
{
if self.shards.len() == 1 {
// SAFETY (per fn contract): no other thread is accessing this shard.
let shard = unsafe { &mut *self.shards[0].data_ptr() };
if ttl_ms.is_none()
&& self.route_mode == EmbeddedRouteMode::FullKey
&& shard.memory_limit_bytes.is_none()
&& shard.eviction_policy == EvictionPolicy::None
{
// SAFETY: forwarded from this function's single-threaded
// contract. The direct 1-shard hot path uses borrowed response
// encoding, so stored value buffers are not cloned while SET
// mutates them.
unsafe { shard.map.set_slice_hashed_no_ttl_hot(key_hash, key, value) };
return;
}
let now_ms = write_now_ms(ttl_ms, shard.memory_limit_bytes);
let expire_at_ms = ttl_ms.map(|ttl| now_ms.saturating_add(ttl));
if let Some(session_prefix) = point_write_session_storage_prefix(key) {
shard
.session_slots
.delete_hashed(&session_prefix, key_hash, key);
}
shard
.map
.set_slice_hashed(key_hash, key, value, expire_at_ms, now_ms);
return;
}
let route = self.route_key(key);
// SAFETY (per fn contract): no other thread is accessing this shard.
let shard = unsafe { &mut *self.shards[route.shard_id].data_ptr() };
let now_ms = write_now_ms(ttl_ms, shard.memory_limit_bytes);
let expire_at_ms = ttl_ms.map(|ttl| now_ms.saturating_add(ttl));
if let Some(session_prefix) = point_write_session_storage_prefix(key) {
shard
.session_slots
.delete_hashed(&session_prefix, route.key_hash, key);
}
shard
.map
.set_slice_hashed(route.key_hash, key, value, expire_at_ms, now_ms);
}
}
/// FCNP single-shard SET path with both hashes supplied by the client.
///
/// # Safety
///
/// Same contract as `set_single_threaded_hashed`; additionally, `key_tag`
/// must equal `hash_key_tag_from_hash(key_hash)`.
#[inline(always)]
pub unsafe fn set_single_threaded_hashed_tagged_no_ttl_hot(
&self,
key_hash: u64,
key_tag: u64,
key: &[u8],
value: &[u8],
) {
#[cfg(not(feature = "unsafe"))]
{
let _ = key_tag;
self.set_slice_prehashed(key_hash, key, value, None);
}
#[cfg(feature = "unsafe")]
{
debug_assert_eq!(self.shards.len(), 1);
debug_assert_eq!(self.route_mode, EmbeddedRouteMode::FullKey);
// SAFETY (per fn contract): no other thread is accessing this shard.
let shard = unsafe { &mut *self.shards[0].data_ptr() };
debug_assert_eq!(shard.memory_limit_bytes, None);
debug_assert_eq!(shard.eviction_policy, EvictionPolicy::None);
// SAFETY: forwarded from this function's single-threaded contract,
// with a caller-provided key tag matching the key bytes.
unsafe {
shard
.map
.set_slice_hashed_tagged_no_ttl_hot(key_hash, key_tag, key, value)
};
}
}
/// Fused GET + RESP-blob-string-encode for the multi-direct hot path.
/// Writes `$<len>\r\n<value>\r\n` directly into `out` under the shard read
/// lock, eliminating the `Vec<u8>` intermediate that the generic `get`
/// path's `.to_vec()` allocates. Returns `true` if the key existed.
pub fn get_blob_string_into(&self, key: &[u8], out: &mut bytes::BytesMut) -> bool {
let route = self.route_key(key);
self.get_blob_string_routed_into(route, key, out)
}
/// Prehashed fused GET + RESP encode. Used by the RESP fast path after it
/// validates the key bytes and computes the hash once.
pub fn get_blob_string_hashed_into(
&self,
key_hash: u64,
key: &[u8],
out: &mut bytes::BytesMut,
) -> bool {
if can_route_with_key_hash(self.route_mode, self.shards.len(), key) {
let route = EmbeddedKeyRoute {
shard_id: self.route_hash(key_hash),
key_hash,
};
return self.get_blob_string_routed_into(route, key, out);
}
self.get_blob_string_into(key, out)
}
fn get_blob_string_routed_into(
&self,
route: EmbeddedKeyRoute,
key: &[u8],
out: &mut bytes::BytesMut,
) -> bool {
use bytes::BufMut;
// Fast path: non-session keys answer from the read lock.
if uses_flat_key_storage(self.route_mode, key) {
let shard = self.shards[route.shard_id].read();
// Skip now_millis when no TTL entries — saves the vDSO call.
let now_ms = if shard.map.has_no_ttl_entries() {
0
} else {
now_millis()
};
if let Some(value) = shard.map.get_ref_hashed_shared(route.key_hash, key, now_ms) {
// Inline RESP blob string encode directly into out.
write_resp_blob_string_into(out, value);
return true;
}
return false;
}
// Session-prefixed keys go through the slow path with a write lock.
let mut shard = self.shards[route.shard_id].write();
if let Some(session_prefix) = derived_session_storage_prefix(key)
&& let Some(value) =
shard
.session_slots
.get_ref_hashed(&session_prefix, route.key_hash, key)
{
out.put_u8(b'$');
let mut buf = itoa::Buffer::new();
out.extend_from_slice(buf.format(value.len()).as_bytes());
out.extend_from_slice(b"\r\n");
out.extend_from_slice(value);
out.extend_from_slice(b"\r\n");
return true;
}
let now_ms_session = now_millis();
if let Some(value) = shard
.map
.get_ref_hashed(route.key_hash, key, now_ms_session)
{
out.put_u8(b'$');
let mut buf = itoa::Buffer::new();
out.extend_from_slice(buf.format(value.len()).as_bytes());
out.extend_from_slice(b"\r\n");
out.extend_from_slice(value);
out.extend_from_slice(b"\r\n");
return true;
}
false
}
/// Prehashed slice SET for the RESP fast path. Avoids rehashing in
/// storage after parsing has already identified the key.
pub fn set_slice_prehashed(
&self,
key_hash: u64,
key: &[u8],
value: &[u8],
ttl_ms: Option<u64>,
) {
if !can_route_with_key_hash(self.route_mode, self.shards.len(), key) {
self.set(key.to_vec(), value.to_vec(), ttl_ms);
return;
}
let route = EmbeddedKeyRoute {
shard_id: self.route_hash(key_hash),
key_hash,
};
if self.objects.has_objects() {
let mut bucket = self.objects.write_bucket(route.shard_id, route.key_hash);
let mut shard = self.shards[route.shard_id].write();
if bucket.delete_any(key) {
self.objects.note_deleted(route.shard_id);
}
let now_ms = write_now_ms(ttl_ms, shard.memory_limit_bytes);
let expire_at_ms = ttl_ms.map(|ttl| now_ms.saturating_add(ttl));
if let Some(session_prefix) = point_write_session_storage_prefix(key) {
shard
.session_slots
.delete_hashed(&session_prefix, route.key_hash, key);
}
shard
.map
.set_slice_hashed(route.key_hash, key, value, expire_at_ms, now_ms);
shard.enforce_memory_limit(now_ms);
return;
}
let mut shard = self.shards[route.shard_id].write();
let now_ms = write_now_ms(ttl_ms, shard.memory_limit_bytes);
let expire_at_ms = ttl_ms.map(|ttl| now_ms.saturating_add(ttl));
if let Some(session_prefix) = point_write_session_storage_prefix(key) {
shard
.session_slots
.delete_hashed(&session_prefix, route.key_hash, key);
}
shard
.map
.set_slice_hashed(route.key_hash, key, value, expire_at_ms, now_ms);
shard.enforce_memory_limit(now_ms);
}
/// Zero-copy `GET` for the multi-direct hot path. Returns the stored
/// `bytes::Bytes` directly (refcount-only clone of `FlatEntry.value`),
/// avoiding the `Vec<u8>` allocation that `get` performs to materialize
/// `Bytes = Vec<u8>` for the public API.
pub fn get_value_bytes(&self, key: &[u8]) -> Option<bytes::Bytes> {
let now_ms = now_millis();
let route = self.route_key(key);
self.get_value_bytes_routed(route, key, now_ms)
}
/// FastCodec GET path. The wire header carries a route hash so the server
/// can usually skip hashing the key again. For session-prefixed keys in
/// session route mode, the route hash is intentionally the session prefix
/// hash, not the full key hash, so we fall back to the generic lookup.
pub fn get_value_bytes_route_hashed(
&self,
route_hash: u64,
key: &[u8],
) -> Option<bytes::Bytes> {
if can_use_route_hash_as_key_hash(self.route_mode, key) {
let route = EmbeddedKeyRoute {
shard_id: self.route_hash(route_hash),
key_hash: route_hash,
};
return self.get_value_bytes_routed(route, key, now_millis());
}
self.get_value_bytes(key)
}
/// FastCodec GET path that calls `write` while the value reference is
/// still protected by the shard read lock. This avoids the refcount bump
/// and drop from returning `bytes::Bytes` on the native GET hot path.
pub fn with_value_bytes_route_hashed<F>(
&self,
route_hash: u64,
key: &[u8],
mut write: F,
) -> bool
where
F: FnMut(&[u8]),
{
if can_use_route_hash_as_key_hash(self.route_mode, key) {
let route = EmbeddedKeyRoute {
shard_id: self.route_hash(route_hash),
key_hash: route_hash,
};
return self.with_value_bytes_routed(route, key, &mut write);
}
if let Some(value) = self.get_value_bytes(key) {
write(value.as_ref());
true
} else {
false
}
}
/// Route-hashed GET path that exposes the stored `Bytes` object while the
/// shard lock is held. Callers can clone the `Bytes` handle for large
/// zero-copy writes, while small responses can still copy from the borrowed
/// value without an extra refcount bump.
pub fn with_shared_value_bytes_route_hashed<F>(
&self,
route_hash: u64,
key: &[u8],
mut write: F,
) -> bool
where
F: FnMut(&bytes::Bytes),
{
if can_use_route_hash_as_key_hash(self.route_mode, key) {
let route = EmbeddedKeyRoute {
shard_id: self.route_hash(route_hash),
key_hash: route_hash,
};
return self.with_shared_value_bytes_routed(route, key, &mut write);
}
if let Some(value) = self.get_value_bytes(key) {
write(&value);
true
} else {
false
}
}
/// Full-key FCNP GET path for clients that provide the key hash, key tag,
/// and key length but omit the key bytes. This is safe for concurrent
/// multi-shard access because the value is exposed only while the shard
/// read lock is held.
pub fn with_shared_value_bytes_full_key_tagged_no_ttl<F>(
&self,
route_hash: u64,
key_tag: u64,
key_len: usize,
mut write: F,
) -> bool
where
F: FnMut(&bytes::Bytes),
{
if self.route_mode != EmbeddedRouteMode::FullKey {
return false;
}
let route = EmbeddedKeyRoute {
shard_id: self.route_hash(route_hash),
key_hash: route_hash,
};
let shard = self.shards[route.shard_id].read();
if !shard.map.has_no_ttl_entries() {
return false;
}
if let Some(value) =
shard
.map
.get_shared_value_bytes_hashed_tagged_no_ttl(route.key_hash, key_tag, key_len)
{
write(value);
true
} else {
false
}
}
/// Native GETEX path for v2. Returns the value and applies the TTL while
/// holding the target shard write lock.
pub fn getex_value_bytes_route_hashed(
&self,
route_hash: Option<u64>,
key: &[u8],
ttl_ms: u64,
) -> Option<bytes::Bytes> {
if let Some(key_hash) = route_hash
&& can_use_route_hash_as_key_hash(self.route_mode, key)
{
let route = EmbeddedKeyRoute {
shard_id: self.route_hash(key_hash),
key_hash,
};
return self.getex_value_bytes_routed(route, key, ttl_ms);
}
let route = self.route_key(key);
self.getex_value_bytes_routed(route, key, ttl_ms)
}
/// Native GETEX path that writes the value while it is borrowed from the
/// shard. This avoids the `Bytes` clone/drop pair in the returning GETEX
/// helper.
pub fn with_getex_value_bytes_route_hashed<F>(
&self,
route_hash: Option<u64>,
key: &[u8],
ttl_ms: u64,
mut write: F,
) -> bool
where
F: FnMut(&[u8]),
{
if let Some(key_hash) = route_hash
&& can_use_route_hash_as_key_hash(self.route_mode, key)
{
let route = EmbeddedKeyRoute {
shard_id: self.route_hash(key_hash),
key_hash,
};
return self.with_getex_value_bytes_routed(route, key, ttl_ms, &mut write);
}
let route = self.route_key(key);
self.with_getex_value_bytes_routed(route, key, ttl_ms, &mut write)
}
/// Single-threaded FastCodec GET path. Avoids shard `RwLock` atomics in
/// the same direct sc=1 setup as the RESP fused single-threaded path.
///
/// # Safety
///
/// The caller must guarantee that no other thread is concurrently accessing
/// any shard of this `EmbeddedStore`.
pub unsafe fn get_value_bytes_route_hashed_single_threaded(
&self,
route_hash: u64,
key: &[u8],
) -> Option<bytes::Bytes> {
#[cfg(not(feature = "unsafe"))]
{
self.get_value_bytes_route_hashed(route_hash, key)
}
#[cfg(feature = "unsafe")]
{
if can_use_route_hash_as_key_hash(self.route_mode, key) {
if self.shards.len() == 1 {
// SAFETY (per fn contract): no other thread is accessing this shard.
let shard = unsafe { &*self.shards[0].data_ptr() };
if can_skip_session_lookup(key, &shard.session_slots) {
let now_ms = if shard.map.has_no_ttl_entries() {
0
} else {
now_millis()
};
return shard.map.get_value_bytes_hashed(route_hash, key, now_ms);
}
}
let route = EmbeddedKeyRoute {
shard_id: self.route_hash(route_hash),
key_hash: route_hash,
};
// SAFETY (per fn contract): no other thread is accessing this shard.
let shard = unsafe { &*self.shards[route.shard_id].data_ptr() };
if uses_flat_key_storage(self.route_mode, key) {
let now_ms = if shard.map.has_no_ttl_entries() {
0
} else {
now_millis()
};
return shard
.map
.get_value_bytes_hashed(route.key_hash, key, now_ms);
}
}
self.get_value_bytes(key)
}
}
/// Single-threaded variant of `with_value_bytes_route_hashed`.
///
/// # Safety
///
/// The caller must guarantee that no other thread is concurrently accessing
/// any shard of this `EmbeddedStore`.
pub unsafe fn with_value_bytes_route_hashed_single_threaded<F>(
&self,
route_hash: u64,
key: &[u8],
write: F,
) -> bool
where
F: FnMut(&[u8]),
{
#[cfg(not(feature = "unsafe"))]
{
self.with_value_bytes_route_hashed(route_hash, key, write)
}
#[cfg(feature = "unsafe")]
{
let mut write = write;
if can_use_route_hash_as_key_hash(self.route_mode, key) {
if self.shards.len() == 1 {
// SAFETY (per fn contract): no other thread is accessing this shard.
let shard = unsafe { &*self.shards[0].data_ptr() };
if can_skip_session_lookup(key, &shard.session_slots) {
let value = if shard.map.has_no_ttl_entries() {
shard.map.get_ref_hashed_shared_no_ttl(route_hash, key)
} else {
shard
.map
.get_ref_hashed_shared(route_hash, key, now_millis())
};
if let Some(value) = value {
write(value);
return true;
}
return false;
}
}
let route = EmbeddedKeyRoute {
shard_id: self.route_hash(route_hash),
key_hash: route_hash,
};
// SAFETY (per fn contract): no other thread is accessing this shard.
let shard = unsafe { &*self.shards[route.shard_id].data_ptr() };
if uses_flat_key_storage(self.route_mode, key) {
let value = if shard.map.has_no_ttl_entries() {
shard.map.get_ref_hashed_shared_no_ttl(route.key_hash, key)
} else {
shard
.map
.get_ref_hashed_shared(route.key_hash, key, now_millis())
};
if let Some(value) = value {
write(value);
return true;
}
return false;
}
}
self.with_value_bytes_route_hashed(route_hash, key, write)
}
}
/// Single-threaded FCNP GET helper that exposes the stored `Bytes` object
/// to the caller. This lets the network path clone only the refcount for
/// large zero-copy writes, while keeping small values on the borrowed
/// inline-copy path.
///
/// # Safety
///
/// The caller must guarantee that no other thread is concurrently accessing
/// any shard of this `EmbeddedStore`.
pub unsafe fn with_shared_value_bytes_route_hashed_single_threaded<F>(
&self,
route_hash: u64,
key: &[u8],
write: F,
) -> bool
where
F: FnMut(&bytes::Bytes),
{
#[cfg(not(feature = "unsafe"))]
{
self.with_shared_value_bytes_route_hashed(route_hash, key, write)
}
#[cfg(feature = "unsafe")]
{
let mut write = write;
if can_use_route_hash_as_key_hash(self.route_mode, key) {
if self.shards.len() == 1 {
// SAFETY (per fn contract): no other thread is accessing this shard.
let shard = unsafe { &*self.shards[0].data_ptr() };
if can_skip_session_lookup(key, &shard.session_slots) {
if shard.map.has_no_ttl_entries() {
return shard.map.with_shared_value_bytes_hashed_no_ttl(
route_hash, key, &mut write,
);
}
return shard.map.with_shared_value_bytes_hashed(
route_hash,
key,
now_millis(),
&mut write,
);
}
}
let route = EmbeddedKeyRoute {
shard_id: self.route_hash(route_hash),
key_hash: route_hash,
};
// SAFETY (per fn contract): no other thread is accessing this shard.
let shard = unsafe { &*self.shards[route.shard_id].data_ptr() };
if uses_flat_key_storage(self.route_mode, key) {
if shard.map.has_no_ttl_entries() {
return shard.map.with_shared_value_bytes_hashed_no_ttl(
route.key_hash,
key,
&mut write,
);
}
return shard.map.with_shared_value_bytes_hashed(
route.key_hash,
key,
now_millis(),
&mut write,
);
}
}
if let Some(value) = self.get_value_bytes(key) {
write(&value);
true
} else {
false
}
}
}
/// 1-shard full-key FCNP GET helper for the hottest native path. This skips
/// route-mode, shard-count, and session-slot checks that the general routed
/// helper must keep.
///
/// # Safety
///
/// The caller must guarantee that this store has exactly one shard, uses
/// `EmbeddedRouteMode::FullKey`, and no other thread is concurrently
/// accessing the shard.
#[inline(always)]
pub unsafe fn with_shared_value_bytes_full_key_single_threaded<F>(
&self,
key_hash: u64,
key: &[u8],
write: F,
) -> bool
where
F: FnMut(&bytes::Bytes),
{
#[cfg(not(feature = "unsafe"))]
{
self.with_shared_value_bytes_route_hashed(key_hash, key, write)
}
#[cfg(feature = "unsafe")]
{
let mut write = write;
debug_assert_eq!(self.shards.len(), 1);
debug_assert_eq!(self.route_mode, EmbeddedRouteMode::FullKey);
// SAFETY (per fn contract): no other thread is accessing this shard.
let shard = unsafe { &*self.shards[0].data_ptr() };
if shard.map.has_no_ttl_entries() {
return shard
.map
.with_shared_value_bytes_hashed_no_ttl(key_hash, key, &mut write);
}
shard
.map
.with_shared_value_bytes_hashed(key_hash, key, now_millis(), &mut write)
}
}
/// 1-shard full-key FCNP GET helper that returns the stored `Bytes`
/// reference directly. This keeps the hottest native GET path out of the
/// closure-based wrapper used by the general routed path.
///
/// # Safety
///
/// The caller must guarantee that this store has exactly one shard, uses
/// `EmbeddedRouteMode::FullKey`, and no other thread is concurrently
/// accessing the shard.
#[inline(always)]
pub unsafe fn get_shared_value_bytes_full_key_single_threaded(
&self,
key_hash: u64,
key: &[u8],
) -> Option<&bytes::Bytes> {
#[cfg(not(feature = "unsafe"))]
{
let _ = (key_hash, key);
None
}
#[cfg(feature = "unsafe")]
{
debug_assert_eq!(self.shards.len(), 1);
debug_assert_eq!(self.route_mode, EmbeddedRouteMode::FullKey);
// SAFETY (per fn contract): no other thread is accessing this shard.
let shard = unsafe { &*self.shards[0].data_ptr() };
if shard.map.has_no_ttl_entries() {
return shard
.map
.get_shared_value_bytes_hashed_no_ttl(key_hash, key);
}
shard
.map
.get_shared_value_bytes_hashed(key_hash, key, now_millis())
}
}
/// 1-shard full-key FCNP GET helper for clients that supply both the key
/// hash and key tag. The native protocol treats that tuple plus key length
/// as the lookup identity, avoiding a key-byte compare in the hottest path.
///
/// # Safety
///
/// The caller must guarantee that this store has exactly one shard, uses
/// `EmbeddedRouteMode::FullKey`, and no other thread is concurrently
/// accessing the shard. `key_tag` must be the tag for the key bytes that
/// produced `key_hash`.
#[inline(always)]
pub unsafe fn get_shared_value_bytes_full_key_tagged_single_threaded(
&self,
key_hash: u64,
key_tag: u64,
key_len: usize,
) -> Option<&bytes::Bytes> {
#[cfg(not(feature = "unsafe"))]
{
let _ = (key_hash, key_tag, key_len);
None
}
#[cfg(feature = "unsafe")]
{
debug_assert_eq!(self.shards.len(), 1);
debug_assert_eq!(self.route_mode, EmbeddedRouteMode::FullKey);
// SAFETY (per fn contract): no other thread is accessing this shard.
let shard = unsafe { &*self.shards[0].data_ptr() };
if shard.map.has_no_ttl_entries() {
return shard
.map
.get_shared_value_bytes_hashed_tagged_no_ttl(key_hash, key_tag, key_len);
}
None
}
}
/// Safe shard-owned full-key GET helper for direct-shard server workers.
///
/// This keeps safe builds on the normal shard `RwLock`, but avoids the
/// generic route/session fallback after the FCNP frame has already proven
/// that the request belongs to `shard_id`.
#[inline(always)]
pub fn with_shared_value_bytes_full_key_owned_shard_no_ttl<F>(
&self,
shard_id: usize,
key_hash: u64,
key: &[u8],
write: F,
) -> Option<bool>
where
F: FnMut(&bytes::Bytes),
{
if self.route_mode != EmbeddedRouteMode::FullKey
|| shard_id >= self.shards.len()
|| self.route_hash(key_hash) != shard_id
{
return None;
}
let shard = self.shards[shard_id].read();
if !shard.map.has_no_ttl_entries() {
return None;
}
let mut write = write;
Some(
shard
.map
.with_shared_value_bytes_hashed_no_ttl(key_hash, key, &mut write),
)
}
/// Shard-owned full-key FCNP GET helper for direct-shard server workers.
///
/// This is the multi-shard counterpart to
/// `get_shared_value_bytes_full_key_tagged_single_threaded`: one server
/// worker owns `shard_id`, the client supplies the full-key hash and tag,
/// and the lookup can skip both the shard lock and the key-byte compare.
///
/// Returns `None` when the hot path is not valid, such as TTL-bearing
/// shards, invalid shard ids, non-full-key routing, or a route mismatch.
/// Otherwise returns `Some(true)` for a hit and `Some(false)` for a miss.
///
/// # Safety
///
/// The caller must guarantee that no other thread is concurrently accessing
/// `shard_id`, that `key_hash` routes to `shard_id`, and that `key_tag`
/// matches the key bytes that produced `key_hash`.
#[inline(always)]
pub unsafe fn with_shared_value_bytes_full_key_tagged_owned_shard_no_ttl<F>(
&self,
shard_id: usize,
key_hash: u64,
key_tag: u64,
key_len: usize,
write: F,
) -> Option<bool>
where
F: FnMut(&bytes::Bytes),
{
#[cfg(not(feature = "unsafe"))]
{
let _ = (shard_id, key_hash, key_tag, key_len, write);
None
}
#[cfg(feature = "unsafe")]
{
if self.route_mode != EmbeddedRouteMode::FullKey
|| shard_id >= self.shards.len()
|| self.route_hash(key_hash) != shard_id
{
return None;
}
let mut write = write;
// SAFETY (per fn contract): this direct worker owns `shard_id`.
let shard = unsafe { &*self.shards[shard_id].data_ptr() };
if !shard.map.has_no_ttl_entries() {
return None;
}
if let Some(value) = shard
.map
.get_shared_value_bytes_hashed_tagged_no_ttl(key_hash, key_tag, key_len)
{
write(value);
Some(true)
} else {
Some(false)
}
}
}
/// Shard-owned session-prefix GET helper for direct-shard server workers.
///
/// Session slabs store borrowed value slices instead of `Bytes`, so this
/// helper writes through a slice callback and avoids the generic fallback's
/// `Bytes::copy_from_slice` materialization. The caller has already
/// validated that `session_prefix` routes to `shard_id`; this helper only
/// checks storage preconditions and performs the lookup.
///
/// # Safety
///
/// When built with the `unsafe` feature, the caller must guarantee no other
/// thread is concurrently accessing `shard_id`. Safe builds keep the shard
/// read lock and therefore only require the route metadata to be valid.
#[inline(always)]
pub unsafe fn with_session_value_slice_owned_shard_no_ttl_prevalidated<F>(
&self,
shard_id: usize,
session_hash: u64,
key_hash: u64,
session_prefix: &[u8],
key: &[u8],
write: F,
) -> Option<bool>
where
F: FnMut(&[u8]),
{
if self.route_mode != EmbeddedRouteMode::SessionPrefix || shard_id >= self.shards.len() {
return None;
}
let mut write = write;
#[cfg(not(feature = "unsafe"))]
{
let shard = self.shards[shard_id].read();
match shard.get_session_ref_hashed_shared_no_ttl_prehashed(
session_hash,
session_prefix,
key_hash,
key,
) {
Some(value) => {
write(value);
Some(true)
}
None => Some(false),
}
}
#[cfg(feature = "unsafe")]
{
// SAFETY (per fn contract): this direct worker owns `shard_id`.
let shard = unsafe { &*self.shards[shard_id].data_ptr() };
match shard.get_session_ref_hashed_shared_no_ttl_prehashed(
session_hash,
session_prefix,
key_hash,
key,
) {
Some(value) => {
write(value);
Some(true)
}
None => Some(false),
}
}
}
/// Safe shard-owned no-TTL session SET helper for direct-shard workers.
///
/// The caller has already validated that `session_prefix` routes to
/// `shard_id`; this helper only enforces storage preconditions and performs
/// the session-slab write.
#[inline(always)]
pub fn set_session_slice_hashed_owned_shard_no_ttl_prevalidated(
&self,
shard_id: usize,
session_hash: u64,
key_hash: u64,
session_prefix: &[u8],
key: &[u8],
value: &[u8],
) -> bool {
if shard_id >= self.shards.len() || self.objects.has_objects() {
return false;
}
let mut shard = self.shards[shard_id].write();
if shard.memory_limit_bytes.is_some() || shard.eviction_policy != EvictionPolicy::None {
return false;
}
shard.set_session_slice_hashed_no_ttl_prehashed(
session_hash,
session_prefix,
key_hash,
key,
value,
);
true
}
/// Shard-owned no-TTL session SET helper for direct-shard workers.
///
/// # Safety
///
/// The caller must guarantee exclusive access to `shard_id` and that the
/// session prefix routes to that shard.
#[inline(always)]
pub unsafe fn set_session_slice_hashed_owned_shard_no_ttl_hot_prevalidated(
&self,
shard_id: usize,
session_hash: u64,
key_hash: u64,
session_prefix: &[u8],
key: &[u8],
value: &[u8],
) -> bool {
#[cfg(not(feature = "unsafe"))]
{
let _ = (shard_id, session_hash, key_hash, session_prefix, key, value);
false
}
#[cfg(feature = "unsafe")]
{
if shard_id >= self.shards.len() || self.objects.has_objects() {
return false;
}
// SAFETY (per fn contract): this direct worker owns `shard_id`.
let shard = unsafe { &mut *self.shards[shard_id].data_ptr() };
if shard.memory_limit_bytes.is_some() || shard.eviction_policy != EvictionPolicy::None {
return false;
}
shard.set_session_slice_hashed_no_ttl_prehashed(
session_hash,
session_prefix,
key_hash,
key,
value,
);
true
}
}
/// Safe shard-owned no-TTL SET helper for direct-shard server workers.
///
/// The helper still takes the shard write lock. It only specializes the
/// routing and no-TTL storage path when the worker-owned shard checks have
/// already succeeded; unsupported cases return `false` so callers can
/// preserve the generic fallback behavior.
#[inline(always)]
pub fn set_slice_hashed_tagged_owned_shard_no_ttl(
&self,
shard_id: usize,
key_hash: u64,
key_tag: u64,
key: &[u8],
value: &[u8],
) -> bool {
if shard_id >= self.shards.len() || self.objects.has_objects() {
return false;
}
let mut shard = self.shards[shard_id].write();
if shard.memory_limit_bytes.is_some() || shard.eviction_policy != EvictionPolicy::None {
return false;
}
match self.route_mode {
EmbeddedRouteMode::FullKey => {
if self.route_hash(key_hash) != shard_id
|| point_write_session_storage_prefix(key).is_some()
{
return false;
}
shard
.map
.set_slice_hashed_tagged_no_ttl_local(key_hash, key_tag, key, value);
}
EmbeddedRouteMode::SessionPrefix => {
let Some(session_prefix) = point_write_session_storage_prefix(key) else {
return false;
};
let route = compute_key_route(self.route_mode, self.shift, key);
if route.shard_id != shard_id || route.key_hash != key_hash {
return false;
}
shard.set_session_slice_hashed_no_ttl(&session_prefix, key_hash, key, value);
}
}
true
}
/// Shard-owned full-key FCNP SET helper for direct-shard server workers.
///
/// # Safety
///
/// The caller must guarantee exclusive access to `shard_id`, that
/// `key_hash` routes to `shard_id`, and that `key_tag` matches `key`.
#[inline(always)]
pub unsafe fn set_slice_hashed_tagged_owned_shard_no_ttl_hot(
&self,
shard_id: usize,
key_hash: u64,
key_tag: u64,
key: &[u8],
value: &[u8],
) -> bool {
#[cfg(not(feature = "unsafe"))]
{
let _ = (shard_id, key_hash, key_tag, key, value);
false
}
#[cfg(feature = "unsafe")]
{
if shard_id >= self.shards.len() {
return false;
}
// SAFETY (per fn contract): this direct worker owns `shard_id`.
let shard = unsafe { &mut *self.shards[shard_id].data_ptr() };
if shard.memory_limit_bytes.is_some() || shard.eviction_policy != EvictionPolicy::None {
return false;
}
match self.route_mode {
EmbeddedRouteMode::FullKey => {
if self.route_hash(key_hash) != shard_id
|| point_write_session_storage_prefix(key).is_some()
{
return false;
}
// SAFETY: forwarded from this function's worker-local ownership contract,
// with a caller-provided key tag matching the key bytes.
unsafe {
shard
.map
.set_slice_hashed_tagged_no_ttl_hot(key_hash, key_tag, key, value)
};
}
EmbeddedRouteMode::SessionPrefix => {
let Some(session_prefix) = point_write_session_storage_prefix(key) else {
return false;
};
let route = compute_key_route(self.route_mode, self.shift, key);
if route.shard_id != shard_id || route.key_hash != key_hash {
return false;
}
shard.set_session_slice_hashed_no_ttl(&session_prefix, key_hash, key, value);
}
}
true
}
}
/// Single-threaded native GETEX path.
///
/// # Safety
///
/// The caller must guarantee that no other thread is concurrently accessing
/// any shard of this `EmbeddedStore`.
pub unsafe fn getex_value_bytes_route_hashed_single_threaded(
&self,
route_hash: Option<u64>,
key: &[u8],
ttl_ms: u64,
) -> Option<bytes::Bytes> {
#[cfg(not(feature = "unsafe"))]
{
self.getex_value_bytes_route_hashed(route_hash, key, ttl_ms)
}
#[cfg(feature = "unsafe")]
{
let now_ms = now_millis();
let expire_at_ms = now_ms.saturating_add(ttl_ms);
if let Some(key_hash) = route_hash
&& can_use_route_hash_as_key_hash(self.route_mode, key)
{
let shard_id = self.route_hash(key_hash);
// SAFETY (per fn contract): no other thread is accessing this shard.
let shard = unsafe { &mut *self.shards[shard_id].data_ptr() };
if uses_flat_key_storage(self.route_mode, key) {
return shard.map.get_value_bytes_hashed_and_expire(
key_hash,
key,
expire_at_ms,
now_ms,
);
}
}
self.getex_value_bytes_route_hashed(route_hash, key, ttl_ms)
}
}
/// Single-threaded borrowed native GETEX path.
///
/// # Safety
///
/// The caller must guarantee that no other thread is concurrently accessing
/// any shard of this `EmbeddedStore`.
pub unsafe fn with_getex_value_bytes_route_hashed_single_threaded<F>(
&self,
route_hash: Option<u64>,
key: &[u8],
ttl_ms: u64,
write: F,
) -> bool
where
F: FnMut(&[u8]),
{
#[cfg(not(feature = "unsafe"))]
{
self.with_getex_value_bytes_route_hashed(route_hash, key, ttl_ms, write)
}
#[cfg(feature = "unsafe")]
{
let mut write = write;
let now_ms = now_millis();
let expire_at_ms = now_ms.saturating_add(ttl_ms);
if let Some(key_hash) = route_hash
&& can_use_route_hash_as_key_hash(self.route_mode, key)
{
let shard_id = self.route_hash(key_hash);
// SAFETY (per fn contract): no other thread is accessing this shard.
let shard = unsafe { &mut *self.shards[shard_id].data_ptr() };
if uses_flat_key_storage(self.route_mode, key) {
return shard.map.with_value_bytes_hashed_and_expire(
key_hash,
key,
expire_at_ms,
now_ms,
&mut write,
);
}
}
self.with_getex_value_bytes_route_hashed(route_hash, key, ttl_ms, write)
}
}
fn get_value_bytes_routed(
&self,
route: EmbeddedKeyRoute,
key: &[u8],
now_ms: u64,
) -> Option<bytes::Bytes> {
if uses_flat_key_storage(self.route_mode, key) {
let shard = self.shards[route.shard_id].read();
return shard
.map
.get_value_bytes_hashed(route.key_hash, key, now_ms);
}
// Session-prefixed keys still go through the legacy path (write lock,
// value copied). Bench workload doesn't use session keys.
let mut shard = self.shards[route.shard_id].write();
if let Some(session_prefix) = derived_session_storage_prefix(key)
&& let Some(value) =
shard
.session_slots
.get_ref_hashed(&session_prefix, route.key_hash, key)
{
return Some(bytes::Bytes::copy_from_slice(value));
}
shard
.map
.get_value_bytes_hashed(route.key_hash, key, now_ms)
}
fn with_value_bytes_routed<F>(&self, route: EmbeddedKeyRoute, key: &[u8], write: &mut F) -> bool
where
F: FnMut(&[u8]),
{
if uses_flat_key_storage(self.route_mode, key) {
let shard = self.shards[route.shard_id].read();
let value = if shard.map.has_no_ttl_entries() {
shard.map.get_ref_hashed_shared_no_ttl(route.key_hash, key)
} else {
shard
.map
.get_ref_hashed_shared(route.key_hash, key, now_millis())
};
if let Some(value) = value {
write(value);
return true;
}
return false;
}
if let Some(value) = self.get_value_bytes_routed(route, key, now_millis()) {
write(value.as_ref());
true
} else {
false
}
}
pub(super) fn with_shared_value_bytes_routed<F>(
&self,
route: EmbeddedKeyRoute,
key: &[u8],
write: &mut F,
) -> bool
where
F: FnMut(&bytes::Bytes),
{
if uses_flat_key_storage(self.route_mode, key) {
let shard = self.shards[route.shard_id].read();
if shard.map.has_no_ttl_entries() {
return shard
.map
.with_shared_value_bytes_hashed_no_ttl(route.key_hash, key, write);
}
return shard.map.with_shared_value_bytes_hashed(
route.key_hash,
key,
now_millis(),
write,
);
}
if let Some(value) = self.get_value_bytes_routed(route, key, now_millis()) {
write(&value);
true
} else {
false
}
}
fn getex_value_bytes_routed(
&self,
route: EmbeddedKeyRoute,
key: &[u8],
ttl_ms: u64,
) -> Option<bytes::Bytes> {
let now_ms = now_millis();
let expire_at_ms = now_ms.saturating_add(ttl_ms);
if uses_flat_key_storage(self.route_mode, key) {
let mut shard = self.shards[route.shard_id].write();
return shard.map.get_value_bytes_hashed_and_expire(
route.key_hash,
key,
expire_at_ms,
now_ms,
);
}
let value = self.get_value_bytes(key);
if value.is_some() {
self.expire(key, expire_at_ms);
}
value
}
fn with_getex_value_bytes_routed<F>(
&self,
route: EmbeddedKeyRoute,
key: &[u8],
ttl_ms: u64,
write: &mut F,
) -> bool
where
F: FnMut(&[u8]),
{
let now_ms = now_millis();
let expire_at_ms = now_ms.saturating_add(ttl_ms);
if uses_flat_key_storage(self.route_mode, key) {
let mut shard = self.shards[route.shard_id].write();
return shard.map.with_value_bytes_hashed_and_expire(
route.key_hash,
key,
expire_at_ms,
now_ms,
write,
);
}
if let Some(value) = self.get_value_bytes(key) {
write(value.as_ref());
self.expire(key, expire_at_ms);
true
} else {
false
}
}
fn get_with_route(&self, route: EmbeddedKeyRoute, key: &[u8], now_ms: u64) -> Option<Bytes> {
// Fast path: non-session keys can answer from the read lock using the
// `&self` shared accessor on `FlatMap`. This is the dominant case for
// workloads that don't use derived session prefixes.
if uses_flat_key_storage(self.route_mode, key) {
let shard = self.shards[route.shard_id].read();
return shard
.map
.get_ref_hashed_shared(route.key_hash, key, now_ms)
.map(<[u8]>::to_vec);
}
// Session-prefixed keys still need the write lock because
// `SessionSlotMap::get_ref_hashed` requires `&mut self`.
let mut shard = self.shards[route.shard_id].write();
if let Some(session_prefix) = derived_session_storage_prefix(key)
&& let Some(value) =
shard
.session_slots
.get_ref_hashed(&session_prefix, route.key_hash, key)
{
return Some(value.to_vec());
}
shard
.map
.get_ref_hashed(route.key_hash, key, now_ms)
.map(<[u8]>::to_vec)
}
/// Returns an owned read view for one key.
///
/// The returned view owns the bytes it exposes, so callers can safely pass
/// the metadata through FFI or buffer-style APIs without tying it to the
/// store's lifetime.
pub fn get_view(&self, key: &[u8]) -> EmbeddedReadView {
let route = self.route_key(key);
self.get_view_routed(route, key, now_millis())
}
#[inline(always)]
pub fn get_prepared_view_no_ttl(&self, prepared: &PreparedPointKey) -> EmbeddedReadView {
let mut shard = self.shards[prepared.route().shard_id].write();
let item = shard
.map
.get_ref_hashed_prepared_no_ttl(
prepared.route().key_hash,
prepared.key(),
prepared.key_tag(),
)
.map(EmbeddedReadSlice::from_slice);
drop(shard);
EmbeddedReadView { item }
}
pub fn get_view_routed_no_ttl(&self, route: EmbeddedKeyRoute, key: &[u8]) -> EmbeddedReadView {
let mut shard = self.shards[route.shard_id].write();
let item = if let Some(session_prefix) = derived_session_storage_prefix(key) {
if shard.session_slots.has_session(&session_prefix) {
shard
.session_slots
.get_ref_hashed(&session_prefix, route.key_hash, key)
.map(EmbeddedReadSlice::from_slice)
} else {
shard
.map
.get_ref_hashed_no_ttl(route.key_hash, key)
.map(EmbeddedReadSlice::from_slice)
}
} else {
shard
.map
.get_ref_hashed_no_ttl(route.key_hash, key)
.map(EmbeddedReadSlice::from_slice)
};
drop(shard);
EmbeddedReadView { item }
}
pub fn get_view_routed(
&self,
route: EmbeddedKeyRoute,
key: &[u8],
now_ms: u64,
) -> EmbeddedReadView {
let mut shard = self.shards[route.shard_id].write();
let item = if let Some(session_prefix) = derived_session_storage_prefix(key) {
if shard.session_slots.has_session(&session_prefix) {
shard
.session_slots
.get_ref_hashed(&session_prefix, route.key_hash, key)
.map(EmbeddedReadSlice::from_slice)
} else {
shard
.map
.get_ref_hashed(route.key_hash, key, now_ms)
.map(EmbeddedReadSlice::from_slice)
}
} else {
shard
.map
.get_ref_hashed(route.key_hash, key, now_ms)
.map(EmbeddedReadSlice::from_slice)
};
drop(shard);
EmbeddedReadView { item }
}
}