client-query-cache 0.9.1__tar.gz → 0.11.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (98) hide show
  1. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/PKG-INFO +4 -3
  2. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/README.md +3 -2
  3. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/native/shared-index/Cargo.lock +2 -2
  4. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/native/shared-index/Cargo.toml +0 -1
  5. client_query_cache-0.11.0/native/shared-index/src/array/tests.rs +137 -0
  6. client_query_cache-0.11.0/native/shared-index/src/array.rs +117 -0
  7. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/native/shared-index/src/index.rs +8 -11
  8. client_query_cache-0.11.0/native/shared-index/src/key_encoder.rs +224 -0
  9. client_query_cache-0.11.0/native/shared-index/src/keys.rs +618 -0
  10. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/native/shared-index/src/lib.rs +7 -0
  11. client_query_cache-0.11.0/native/shared-index/src/local_cache/shared.rs +725 -0
  12. client_query_cache-0.11.0/native/shared-index/src/local_cache.rs +3099 -0
  13. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/native/shared-index/src/python.rs +65 -8
  14. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/pyproject.toml +20 -13
  15. client_query_cache-0.11.0/rust-toolchain.toml +5 -0
  16. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/src/client_query_cache/_core/canonical.py +29 -5
  17. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/src/client_query_cache/_core/codec.py +11 -9
  18. client_query_cache-0.11.0/src/client_query_cache/_core/contracts.pyi +209 -0
  19. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/src/client_query_cache/_core/cursor_capture.py +20 -21
  20. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/src/client_query_cache/_core/entries.py +5 -1
  21. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/src/client_query_cache/_core/find_one_reads.py +35 -5
  22. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/src/client_query_cache/_core/keys.py +4 -2
  23. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/src/client_query_cache/_core/lru.py +0 -17
  24. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/src/client_query_cache/_core/manager.py +57 -267
  25. client_query_cache-0.11.0/src/client_query_cache/_core/native.py +119 -0
  26. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/src/client_query_cache/_core/order_sensitive_keys.py +22 -3
  27. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/src/client_query_cache/_core/read_validation.py +21 -3
  28. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/src/client_query_cache/_core/stream_events.py +1 -1
  29. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/src/client_query_cache/_shared/adapters.py +59 -37
  30. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/src/client_query_cache/_shared/attachment.py +1 -0
  31. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/src/client_query_cache/_shared/coordinator.py +106 -88
  32. client_query_cache-0.11.0/src/client_query_cache/_shared/shared_index.py +19 -0
  33. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/src/client_query_cache/_shared/wire.py +49 -2
  34. client_query_cache-0.11.0/src/client_query_cache/_shared_index.pyi +271 -0
  35. {client_query_cache-0.9.1/src/client_query_cache/synchronous → client_query_cache-0.11.0/src/client_query_cache/asynchronous}/__init__.py +2 -1
  36. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/src/client_query_cache/asynchronous/collection.py +31 -33
  37. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/src/client_query_cache/asynchronous/cursors.py +3 -5
  38. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/src/client_query_cache/asynchronous/manager.py +12 -10
  39. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/src/client_query_cache/asynchronous/streams.py +1 -1
  40. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/src/client_query_cache/shared.py +6 -2
  41. {client_query_cache-0.9.1/src/client_query_cache/asynchronous → client_query_cache-0.11.0/src/client_query_cache/synchronous}/__init__.py +2 -1
  42. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/src/client_query_cache/synchronous/collection.py +31 -33
  43. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/src/client_query_cache/synchronous/cursors.py +3 -5
  44. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/src/client_query_cache/synchronous/manager.py +12 -10
  45. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/src/client_query_cache/synchronous/streams.py +1 -1
  46. client_query_cache-0.9.1/src/client_query_cache/_core/residency.py +0 -35
  47. client_query_cache-0.9.1/src/client_query_cache/_shared/shared_index.py +0 -266
  48. client_query_cache-0.9.1/src/client_query_cache/_shared_index.pyi +0 -98
  49. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/LICENSE +0 -0
  50. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/native/shared-index/deny.toml +0 -0
  51. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/native/shared-index/src/clock.rs +0 -0
  52. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/native/shared-index/src/echo/tests.rs +0 -0
  53. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/native/shared-index/src/echo.rs +0 -0
  54. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/native/shared-index/src/frames/tests.rs +0 -0
  55. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/native/shared-index/src/frames.rs +0 -0
  56. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/native/shared-index/src/hash/tests.rs +0 -0
  57. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/native/shared-index/src/hash.rs +0 -0
  58. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/native/shared-index/src/index/support_tests.rs +0 -0
  59. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/native/shared-index/src/index/tests.rs +0 -0
  60. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/native/shared-index/src/memory/support_tests.rs +0 -0
  61. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/native/shared-index/src/memory/tests.rs +0 -0
  62. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/native/shared-index/src/memory.rs +0 -0
  63. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/native/shared-index/src/relay/tests.rs +0 -0
  64. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/native/shared-index/src/relay.rs +0 -0
  65. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/native/shared-index/src/ring/tests.rs +0 -0
  66. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/native/shared-index/src/ring.rs +0 -0
  67. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/native/shared-index/src/sync.rs +0 -0
  68. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/src/client_query_cache/__init__.py +0 -0
  69. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/src/client_query_cache/_core/__init__.py +0 -0
  70. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/src/client_query_cache/_core/collation.py +0 -0
  71. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/src/client_query_cache/_core/collection_metadata.py +0 -0
  72. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/src/client_query_cache/_core/count_reads.py +0 -0
  73. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/src/client_query_cache/_core/errors.py +0 -0
  74. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/src/client_query_cache/_core/find_reads.py +0 -0
  75. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/src/client_query_cache/_core/identity_reads.py +0 -0
  76. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/src/client_query_cache/_core/lifecycle.py +0 -0
  77. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/src/client_query_cache/_core/locking.py +0 -0
  78. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/src/client_query_cache/_core/namespace.py +0 -0
  79. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/src/client_query_cache/_core/namespace_metadata.py +0 -0
  80. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/src/client_query_cache/_core/projection.py +0 -0
  81. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/src/client_query_cache/_core/query_filters.py +0 -0
  82. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/src/client_query_cache/_core/read_classification.py +0 -0
  83. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/src/client_query_cache/_core/snapshots.py +0 -0
  84. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/src/client_query_cache/_core/stream_activation.py +0 -0
  85. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/src/client_query_cache/_core/stream_cost.py +0 -0
  86. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/src/client_query_cache/_core/stream_health.py +0 -0
  87. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/src/client_query_cache/_core/stream_options.py +0 -0
  88. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/src/client_query_cache/_core/stream_startup.py +0 -0
  89. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/src/client_query_cache/_core/traversal.py +0 -0
  90. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/src/client_query_cache/_core/unique_keys.py +0 -0
  91. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/src/client_query_cache/_shared/__init__.py +0 -0
  92. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/src/client_query_cache/_types.py +0 -0
  93. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/src/client_query_cache/asynchronous/database.py +0 -0
  94. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/src/client_query_cache/asynchronous/shared.py +0 -0
  95. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/src/client_query_cache/otel.py +0 -0
  96. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/src/client_query_cache/py.typed +0 -0
  97. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/src/client_query_cache/synchronous/database.py +0 -0
  98. {client_query_cache-0.9.1 → client_query_cache-0.11.0}/src/client_query_cache/synchronous/shared.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: client-query-cache
3
- Version: 0.9.1
3
+ Version: 0.11.0
4
4
  Classifier: Framework :: AsyncIO
5
5
  Classifier: Operating System :: POSIX :: Linux
6
6
  Classifier: Programming Language :: Python :: Implementation :: CPython
@@ -56,9 +56,10 @@ flowchart TB
56
56
 
57
57
  [Benchmarks](https://alessio-locatelli.github.io/client-query-cache/benchmarks/shared-cache/) compare direct reads, independent caches and shared caching. Sharing avoids duplicated values and streams, but its owner process and cache misses have costs. Rust does not guarantee a faster application or lower total memory for every workload.
58
58
 
59
- ![Illustrative process-local read latency on a logarithmic scale: direct local MongoDB read, about 120 microseconds; direct Atlas M0 read, 79.4 milliseconds; cached reads in either deployment, about 61 microseconds.](docs/user/assets/benchmark-latency-light.svg)
59
+ ![Illustrative read latency on a logarithmic scale: direct local MongoDB read, 103 microseconds; direct Atlas M0 read, 65.5 milliseconds; cached read in either deployment, 8.01 microseconds.](docs/user/assets/benchmark-latency-light.svg#gh-light-mode-only)
60
+ ![Illustrative read latency on a logarithmic scale: direct local MongoDB read, 103 microseconds; direct Atlas M0 read, 65.5 milliseconds; cached read in either deployment, 8.01 microseconds.](docs/user/assets/benchmark-latency-dark.svg#gh-dark-mode-only)
60
61
 
61
- The graphic shows [process-local measurements from local MongoDB and Atlas deployments](https://alessio-locatelli.github.io/client-query-cache/benchmarks/#illustrative-read-latency); it is separate from the shared-cache comparison above.
62
+ The graphic shows [process-local measurements from local and remote MongoDB deployments](https://alessio-locatelli.github.io/client-query-cache/benchmarks/#illustrative-read-latency); it is separate from the shared-cache comparison above.
62
63
 
63
64
  ## Quick start
64
65
 
@@ -31,9 +31,10 @@ flowchart TB
31
31
 
32
32
  [Benchmarks](https://alessio-locatelli.github.io/client-query-cache/benchmarks/shared-cache/) compare direct reads, independent caches and shared caching. Sharing avoids duplicated values and streams, but its owner process and cache misses have costs. Rust does not guarantee a faster application or lower total memory for every workload.
33
33
 
34
- ![Illustrative process-local read latency on a logarithmic scale: direct local MongoDB read, about 120 microseconds; direct Atlas M0 read, 79.4 milliseconds; cached reads in either deployment, about 61 microseconds.](docs/user/assets/benchmark-latency-light.svg)
34
+ ![Illustrative read latency on a logarithmic scale: direct local MongoDB read, 103 microseconds; direct Atlas M0 read, 65.5 milliseconds; cached read in either deployment, 8.01 microseconds.](docs/user/assets/benchmark-latency-light.svg#gh-light-mode-only)
35
+ ![Illustrative read latency on a logarithmic scale: direct local MongoDB read, 103 microseconds; direct Atlas M0 read, 65.5 milliseconds; cached read in either deployment, 8.01 microseconds.](docs/user/assets/benchmark-latency-dark.svg#gh-dark-mode-only)
35
36
 
36
- The graphic shows [process-local measurements from local MongoDB and Atlas deployments](https://alessio-locatelli.github.io/client-query-cache/benchmarks/#illustrative-read-latency); it is separate from the shared-cache comparison above.
37
+ The graphic shows [process-local measurements from local and remote MongoDB deployments](https://alessio-locatelli.github.io/client-query-cache/benchmarks/#illustrative-read-latency); it is separate from the shared-cache comparison above.
37
38
 
38
39
  ## Quick start
39
40
 
@@ -71,9 +71,9 @@ checksum = "20870f649af7073d53e38067b2a84312175d56ea15217e1b15bc83506ec50afb"
71
71
 
72
72
  [[package]]
73
73
  name = "libc"
74
- version = "0.2.190"
74
+ version = "0.2.191"
75
75
  source = "registry+https://github.com/rust-lang/crates.io-index"
76
- checksum = "ce5d3ddc6d3fa000eb1536d85e147bfe31aacaba692ed6a876f95cb7c855be78"
76
+ checksum = "65cd87c9a6d60536580b316ad985726104730a5151c99845f2490428ef91240a"
77
77
 
78
78
  [[package]]
79
79
  name = "log"
@@ -5,7 +5,6 @@ description = "Shared index for client-query-cache worker caches."
5
5
  repository = "https://github.com/alessio-locatelli/client-query-cache"
6
6
  documentation = "https://alessio-locatelli.github.io/client-query-cache/"
7
7
  edition = "2024"
8
- rust-version = "1.85"
9
8
  license = "Apache-2.0"
10
9
  # Cargo 1.95 publish field: https://doc.rust-lang.org/cargo/reference/manifest.html#the-publish-field
11
10
  # The default is publishing to any registry allowed. We override it because the native crate ships only inside the Python distribution.
@@ -0,0 +1,137 @@
1
+ use super::*;
2
+
3
+ const EMPTY_DOCUMENT: [u8; 5] = [5, 0, 0, 0, 0];
4
+
5
+ fn envelope(document: &[u8]) -> Vec<u8> {
6
+ let total = i32::try_from(document.len() + 8).unwrap();
7
+ let mut bytes = total.to_le_bytes().to_vec();
8
+ bytes.extend_from_slice(&DOCUMENT_ELEMENT);
9
+ bytes.extend_from_slice(document);
10
+ bytes.push(0);
11
+ bytes
12
+ }
13
+
14
+ fn document(field: u8) -> Vec<u8> {
15
+ vec![12, 0, 0, 0, 0x08, field, 0, 1, 0, 0, 0, 0]
16
+ }
17
+
18
+ #[test]
19
+ fn an_empty_array_is_a_complete_envelope() {
20
+ let mut builder = ArrayBuilder::default();
21
+ assert_eq!(
22
+ builder.finish(),
23
+ [13, 0, 0, 0, 0x04, b'v', 0, 5, 0, 0, 0, 0, 0]
24
+ );
25
+ }
26
+
27
+ #[test]
28
+ fn documents_are_framed_with_decimal_indexes() {
29
+ let mut builder = ArrayBuilder::default();
30
+ assert_eq!(builder.append(&envelope(&EMPTY_DOCUMENT), 5), Ok(true));
31
+ assert_eq!(builder.retained(), 5);
32
+ assert_eq!(
33
+ builder.finish(),
34
+ [
35
+ 21, 0, 0, 0, 0x04, b'v', 0, 13, 0, 0, 0, 0x03, b'0', 0, 5, 0, 0, 0, 0, 0, 0
36
+ ]
37
+ );
38
+ assert_eq!(builder.retained(), 0);
39
+ }
40
+
41
+ #[test]
42
+ fn index_width_grows_at_each_power_of_ten() {
43
+ let mut builder = ArrayBuilder::default();
44
+ for _ in 0..101 {
45
+ assert_eq!(
46
+ builder.append(&envelope(&EMPTY_DOCUMENT), usize::MAX),
47
+ Ok(true)
48
+ );
49
+ }
50
+ let bytes = builder.finish();
51
+ let element = |key: &[u8]| {
52
+ let mut element = vec![0x03];
53
+ element.extend_from_slice(key);
54
+ element.push(0);
55
+ element.extend_from_slice(&EMPTY_DOCUMENT);
56
+ element
57
+ };
58
+ let expected: Vec<u8> = (0..101)
59
+ .flat_map(|index: u32| element(index.to_string().as_bytes()))
60
+ .collect();
61
+ assert_eq!(&bytes[PREFIX..bytes.len() - TERMINATORS], expected);
62
+ assert_eq!(length(&bytes), Some(bytes.len()));
63
+ assert_eq!(length(&bytes[7..]), Some(bytes.len() - 8));
64
+ }
65
+
66
+ #[test]
67
+ fn a_document_beyond_the_retained_limit_is_refused_without_change() {
68
+ let mut builder = ArrayBuilder::default();
69
+ let first = document(b'a');
70
+ assert_eq!(builder.append(&envelope(&first), 12), Ok(true));
71
+ assert_eq!(builder.append(&envelope(&document(b'b')), 23), Ok(false));
72
+ assert_eq!(builder.retained(), 12);
73
+ let bytes = builder.finish();
74
+ assert_eq!(&bytes[PREFIX + 3..bytes.len() - TERMINATORS], first);
75
+ }
76
+
77
+ #[test]
78
+ fn an_exact_retained_limit_is_admitted() {
79
+ let mut builder = ArrayBuilder::default();
80
+ assert_eq!(builder.append(&envelope(&document(b'a')), 12), Ok(true));
81
+ assert_eq!(builder.append(&envelope(&document(b'b')), 24), Ok(true));
82
+ assert_eq!(builder.retained(), 24);
83
+ }
84
+
85
+ #[test]
86
+ fn malformed_envelopes_are_rejected() {
87
+ let valid = envelope(&document(b'a'));
88
+ let mut wrong_type = valid.clone();
89
+ wrong_type[4] = 0x04;
90
+ let mut wrong_name = valid.clone();
91
+ wrong_name[5] = b'w';
92
+ let mut wrong_length = valid.clone();
93
+ wrong_length[0] += 1;
94
+ let mut wrong_inner_length = valid.clone();
95
+ wrong_inner_length[7] += 1;
96
+ let mut unterminated = valid.clone();
97
+ unterminated[valid.len() - 2] = 1;
98
+ let mut negative = valid.clone();
99
+ negative[3] = 0x80;
100
+ for malformed in [
101
+ valid[..12].to_vec(),
102
+ wrong_type,
103
+ wrong_name,
104
+ wrong_length,
105
+ wrong_inner_length,
106
+ unterminated,
107
+ negative,
108
+ [valid.as_slice(), &[1]].concat(),
109
+ ] {
110
+ let mut builder = ArrayBuilder::default();
111
+ assert_eq!(
112
+ builder.append(&malformed, usize::MAX),
113
+ Err(AppendError::Malformed)
114
+ );
115
+ assert_eq!(builder.retained(), 0);
116
+ }
117
+ }
118
+
119
+ #[test]
120
+ fn growth_beyond_the_bson_limit_overflows() {
121
+ assert_eq!(grown(MAXIMUM_BYTES - 10, 1, 5), Some(MAXIMUM_BYTES));
122
+ assert_eq!(grown(MAXIMUM_BYTES - 10, 1, 6), None);
123
+ assert_eq!(grown(usize::MAX - 1, 1, 0), None);
124
+ }
125
+
126
+ #[test]
127
+ fn clearing_releases_the_buffer() {
128
+ let mut builder = ArrayBuilder::default();
129
+ assert_eq!(
130
+ builder.append(&envelope(&document(b'a')), usize::MAX),
131
+ Ok(true)
132
+ );
133
+ builder.clear();
134
+ assert_eq!(builder.retained(), 0);
135
+ assert_eq!(builder.bytes.capacity(), 0);
136
+ assert_eq!(builder.finish().len(), MINIMUM_ENVELOPE);
137
+ }
@@ -0,0 +1,117 @@
1
+ const ARRAY_ELEMENT: [u8; 3] = [0x04, b'v', 0];
2
+ const DOCUMENT_ELEMENT: [u8; 3] = [0x03, b'v', 0];
3
+ const PREFIX: usize = 11;
4
+ const TERMINATORS: usize = 2;
5
+ const MINIMUM_ENVELOPE: usize = 13;
6
+ const MAXIMUM_BYTES: usize = i32::MAX as usize;
7
+
8
+ #[derive(Debug, PartialEq, Eq)]
9
+ pub(crate) enum AppendError {
10
+ Malformed,
11
+ Overflow,
12
+ }
13
+
14
+ #[derive(Default)]
15
+ pub(crate) struct ArrayBuilder {
16
+ bytes: Vec<u8>,
17
+ count: u64,
18
+ retained: usize,
19
+ }
20
+
21
+ fn length(bytes: &[u8]) -> Option<usize> {
22
+ usize::try_from(i32::from_le_bytes(bytes.get(..4)?.try_into().ok()?)).ok()
23
+ }
24
+
25
+ fn embedded(envelope: &[u8]) -> Option<&[u8]> {
26
+ if envelope.len() < MINIMUM_ENVELOPE
27
+ || envelope[4..7] != DOCUMENT_ELEMENT
28
+ || envelope.last() != Some(&0)
29
+ || length(envelope)? != envelope.len()
30
+ {
31
+ return None;
32
+ }
33
+ let document = &envelope[7..envelope.len() - 1];
34
+ (length(document)? == document.len() && document.last() == Some(&0)).then_some(document)
35
+ }
36
+
37
+ fn decimal(value: u64, digits: &mut [u8; 20]) -> &[u8] {
38
+ let mut start = digits.len();
39
+ let mut remaining = value;
40
+ loop {
41
+ start -= 1;
42
+ digits[start] = b'0' + (remaining % 10) as u8;
43
+ remaining /= 10;
44
+ if remaining == 0 {
45
+ return &digits[start..];
46
+ }
47
+ }
48
+ }
49
+
50
+ fn grown(current: usize, key: usize, document: usize) -> Option<usize> {
51
+ [1, key, 1, document, TERMINATORS]
52
+ .into_iter()
53
+ .try_fold(current, usize::checked_add)
54
+ .filter(|total| *total <= MAXIMUM_BYTES)
55
+ }
56
+
57
+ fn header(bytes: &mut Vec<u8>) {
58
+ bytes.extend_from_slice(&[0; 4]);
59
+ bytes.extend_from_slice(&ARRAY_ELEMENT);
60
+ bytes.extend_from_slice(&[0; 4]);
61
+ }
62
+
63
+ impl ArrayBuilder {
64
+ pub(crate) fn retained(&self) -> usize {
65
+ self.retained
66
+ }
67
+
68
+ pub(crate) fn append(&mut self, envelope: &[u8], limit: usize) -> Result<bool, AppendError> {
69
+ let document = embedded(envelope).ok_or(AppendError::Malformed)?;
70
+ let Some(retained) = self
71
+ .retained
72
+ .checked_add(document.len())
73
+ .filter(|retained| *retained <= limit)
74
+ else {
75
+ return Ok(false);
76
+ };
77
+ let mut digits = [0; 20];
78
+ let key = decimal(self.count, &mut digits);
79
+ let current = if self.bytes.is_empty() {
80
+ PREFIX
81
+ } else {
82
+ self.bytes.len()
83
+ };
84
+ let total = grown(current, key.len(), document.len()).ok_or(AppendError::Overflow)?;
85
+ if self.bytes.is_empty() {
86
+ self.bytes.reserve(total);
87
+ header(&mut self.bytes);
88
+ }
89
+ self.bytes.push(0x03);
90
+ self.bytes.extend_from_slice(key);
91
+ self.bytes.push(0);
92
+ self.bytes.extend_from_slice(document);
93
+ self.count += 1;
94
+ self.retained = retained;
95
+ Ok(true)
96
+ }
97
+
98
+ pub(crate) fn finish(&mut self) -> Vec<u8> {
99
+ let mut bytes = std::mem::take(&mut self.bytes);
100
+ self.clear();
101
+ if bytes.is_empty() {
102
+ header(&mut bytes);
103
+ }
104
+ bytes.extend_from_slice(&[0; TERMINATORS]);
105
+ let total = i32::try_from(bytes.len()).expect("checked BSON envelope length");
106
+ bytes[..4].copy_from_slice(&total.to_le_bytes());
107
+ bytes[7..PREFIX].copy_from_slice(&(total - 8).to_le_bytes());
108
+ bytes
109
+ }
110
+
111
+ pub(crate) fn clear(&mut self) {
112
+ *self = Self::default();
113
+ }
114
+ }
115
+
116
+ #[cfg(all(test, not(loom)))]
117
+ mod tests;
@@ -198,11 +198,10 @@ fn word_of(bytes: &[u8]) -> u64 {
198
198
 
199
199
  fn copy_words(source: &[AtomicU64], destination: &mut [u8]) {
200
200
  let complete_words = destination.len() / WORD_BYTES;
201
- let mut chunks = destination.chunks_exact_mut(WORD_BYTES);
202
- for (chunk, word) in chunks.by_ref().zip(source) {
201
+ let (chunks, remainder) = destination.as_chunks_mut::<WORD_BYTES>();
202
+ for (chunk, word) in chunks.iter_mut().zip(source) {
203
203
  chunk.copy_from_slice(&word.load(Ordering::Relaxed).to_ne_bytes());
204
204
  }
205
- let remainder = chunks.into_remainder();
206
205
  if !remainder.is_empty() {
207
206
  let bytes = source[complete_words].load(Ordering::Relaxed).to_ne_bytes();
208
207
  remainder.copy_from_slice(&bytes[..remainder.len()]);
@@ -844,11 +843,10 @@ impl<M: Words> Writer<M> {
844
843
  if Some(next) != original_next {
845
844
  self.allocations[Allocation::index(next)].previous = Some(Allocation::id(index));
846
845
  }
847
- if previous != original_previous {
848
- if let Some(previous) = previous {
849
- self.allocations[Allocation::index(previous)].next =
850
- Some(Allocation::id(index));
851
- }
846
+ if previous != original_previous
847
+ && let Some(previous) = previous
848
+ {
849
+ self.allocations[Allocation::index(previous)].next = Some(Allocation::id(index));
852
850
  }
853
851
  self.insert_free(index);
854
852
  } else {
@@ -879,14 +877,13 @@ impl<M: Words> Writer<M> {
879
877
  used = 0;
880
878
  }
881
879
  }
882
- let mut chunks = part.chunks_exact(WORD_BYTES);
883
- for chunk in chunks.by_ref() {
880
+ let (chunks, remainder) = part.as_chunks::<WORD_BYTES>();
881
+ for chunk in chunks {
884
882
  self.index
885
883
  .arena(offset)
886
884
  .store(word_of(chunk), Ordering::Relaxed);
887
885
  offset += 1;
888
886
  }
889
- let remainder = chunks.remainder();
890
887
  if !remainder.is_empty() {
891
888
  pending[..remainder.len()].copy_from_slice(remainder);
892
889
  used = remainder.len();
@@ -0,0 +1,224 @@
1
+ //! The shared key envelope follows https://bsonspec.org/spec.html. Specialized
2
+ //! scalar representations stay with the Python wire codec and PyMongo's encoder.
3
+ use pyo3::exceptions::{PyOverflowError, PyValueError};
4
+ use pyo3::prelude::*;
5
+ use pyo3::types::{PyBool, PyBytes, PyDict, PyFloat, PyInt, PyString, PyTuple};
6
+
7
+ type FindKeys<'py> = (Bound<'py, PyBytes>, Bound<'py, PyBytes>);
8
+
9
+ struct Recursion;
10
+
11
+ impl Recursion {
12
+ fn enter(py: Python<'_>) -> PyResult<Self> {
13
+ // CPython's recursion guard covers calls crossing the extension boundary.
14
+ if unsafe { pyo3::ffi::Py_EnterRecursiveCall(c" while encoding a cache key".as_ptr()) } != 0
15
+ {
16
+ return Err(PyErr::fetch(py));
17
+ }
18
+ Ok(Self)
19
+ }
20
+ }
21
+
22
+ impl Drop for Recursion {
23
+ fn drop(&mut self) {
24
+ unsafe { pyo3::ffi::Py_LeaveRecursiveCall() };
25
+ }
26
+ }
27
+
28
+ fn encoded_value(encoded: &[u8]) -> PyResult<(u8, &[u8])> {
29
+ match encoded {
30
+ [a, b, c, d, kind, b'k', 0, payload @ .., 0]
31
+ if usize::try_from(i32::from_le_bytes([*a, *b, *c, *d])).ok()
32
+ == Some(encoded.len()) =>
33
+ {
34
+ Ok((*kind, payload))
35
+ }
36
+ _ => Err(PyValueError::new_err("invalid encoded key envelope")),
37
+ }
38
+ }
39
+
40
+ fn length(value: usize) -> PyResult<[u8; 4]> {
41
+ i32::try_from(value)
42
+ .map(i32::to_le_bytes)
43
+ .map_err(|_| PyOverflowError::new_err("cache key exceeds BSON length"))
44
+ }
45
+
46
+ fn start(buffer: &mut Vec<u8>) -> usize {
47
+ let offset = buffer.len();
48
+ buffer.extend_from_slice(&[0; 4]);
49
+ offset
50
+ }
51
+
52
+ fn finish(buffer: &mut Vec<u8>, offset: usize) -> PyResult<()> {
53
+ buffer.push(0);
54
+ let size = length(buffer.len() - offset)?;
55
+ buffer[offset..offset + 4].copy_from_slice(&size);
56
+ Ok(())
57
+ }
58
+
59
+ fn header(buffer: &mut Vec<u8>, kind: u8, name: &str) {
60
+ buffer.push(kind);
61
+ buffer.extend_from_slice(name.as_bytes());
62
+ buffer.push(0);
63
+ }
64
+
65
+ fn string(buffer: &mut Vec<u8>, name: &str, value: &str) -> PyResult<()> {
66
+ header(buffer, 2, name);
67
+ buffer.extend_from_slice(&length(value.len() + 1)?);
68
+ buffer.extend_from_slice(value.as_bytes());
69
+ buffer.push(0);
70
+ Ok(())
71
+ }
72
+
73
+ fn tagged(buffer: &mut Vec<u8>, name: &str, tag: &str) -> PyResult<usize> {
74
+ header(buffer, 3, name);
75
+ let offset = start(buffer);
76
+ string(buffer, "t", tag)?;
77
+ Ok(offset)
78
+ }
79
+
80
+ fn tuple(buffer: &mut Vec<u8>, name: &str) -> PyResult<(usize, usize)> {
81
+ let document = tagged(buffer, name, "tuple")?;
82
+ header(buffer, 4, "p");
83
+ Ok((document, start(buffer)))
84
+ }
85
+
86
+ fn finish_tuple(buffer: &mut Vec<u8>, offsets: (usize, usize)) -> PyResult<()> {
87
+ finish(buffer, offsets.1)?;
88
+ finish(buffer, offsets.0)
89
+ }
90
+
91
+ #[pyclass(frozen)]
92
+ pub(crate) struct FindKeyEncoder {
93
+ tags: Vec<(usize, String)>,
94
+ }
95
+
96
+ impl FindKeyEncoder {
97
+ fn node(
98
+ &self,
99
+ buffer: &mut Vec<u8>,
100
+ name: &str,
101
+ value: &Bound<'_, PyAny>,
102
+ scalar_encoder: &Bound<'_, PyAny>,
103
+ ) -> PyResult<()> {
104
+ if let Ok(items) = value.cast::<PyTuple>() {
105
+ let _recursion = Recursion::enter(value.py())?;
106
+ let offsets = tuple(buffer, name)?;
107
+ for (index, item) in items.iter().enumerate() {
108
+ self.node(buffer, &index.to_string(), &item, scalar_encoder)?;
109
+ }
110
+ return finish_tuple(buffer, offsets);
111
+ }
112
+ if value.is_none() {
113
+ header(buffer, 10, name);
114
+ } else if value.is_exact_instance_of::<PyBool>() {
115
+ header(buffer, 8, name);
116
+ buffer.push(u8::from(value.extract::<bool>()?));
117
+ } else if value.is_exact_instance_of::<PyInt>() {
118
+ if let Ok(integer) = value.extract::<i64>() {
119
+ let offset = tagged(buffer, name, "int")?;
120
+ header(buffer, 18, "p");
121
+ buffer.extend_from_slice(&integer.to_le_bytes());
122
+ finish(buffer, offset)?;
123
+ } else {
124
+ scalar(buffer, name, value, scalar_encoder)?;
125
+ }
126
+ } else if value.is_exact_instance_of::<PyString>() {
127
+ string(buffer, name, value.cast::<PyString>()?.to_str()?)?;
128
+ } else if value.is_exact_instance_of::<PyBytes>() {
129
+ let bytes = value.cast::<PyBytes>()?.as_bytes();
130
+ header(buffer, 5, name);
131
+ buffer.extend_from_slice(&length(bytes.len())?);
132
+ buffer.push(0);
133
+ buffer.extend_from_slice(bytes);
134
+ } else if value.is_exact_instance_of::<PyFloat>() {
135
+ header(buffer, 1, name);
136
+ buffer.extend_from_slice(&value.extract::<f64>()?.to_le_bytes());
137
+ } else if let Some((_, tag)) = self
138
+ .tags
139
+ .iter()
140
+ .find(|(identity, _)| *identity == value.as_ptr() as usize)
141
+ {
142
+ let offset = tagged(buffer, name, "tag")?;
143
+ string(buffer, "p", tag)?;
144
+ finish(buffer, offset)?;
145
+ } else {
146
+ scalar(buffer, name, value, scalar_encoder)?;
147
+ }
148
+ Ok(())
149
+ }
150
+ }
151
+
152
+ fn scalar(
153
+ buffer: &mut Vec<u8>,
154
+ name: &str,
155
+ value: &Bound<'_, PyAny>,
156
+ scalar_encoder: &Bound<'_, PyAny>,
157
+ ) -> PyResult<()> {
158
+ let encoded = scalar_encoder.call1((value,))?;
159
+ let bytes = encoded.cast::<PyBytes>()?.as_bytes();
160
+ // The existing encoder returns one BSON element named "k". Reuse its
161
+ // complete value, including BSON subtype and specialized tagged nodes.
162
+ let (kind, payload) = encoded_value(bytes)?;
163
+ header(buffer, kind, name);
164
+ buffer.extend_from_slice(payload);
165
+ Ok(())
166
+ }
167
+
168
+ impl FindKeyEncoder {
169
+ fn new(tags: &Bound<'_, PyDict>) -> PyResult<Self> {
170
+ let tags = tags
171
+ .iter()
172
+ .map(|(identity, name)| Ok((identity.extract::<usize>()?, name.extract::<String>()?)))
173
+ .collect::<PyResult<_>>()?;
174
+ Ok(Self { tags })
175
+ }
176
+ }
177
+
178
+ #[pyfunction]
179
+ pub(crate) fn find_key_encoder(tags: &Bound<'_, PyDict>) -> PyResult<FindKeyEncoder> {
180
+ FindKeyEncoder::new(tags)
181
+ }
182
+
183
+ #[pymethods]
184
+ impl FindKeyEncoder {
185
+ fn encode<'py>(
186
+ &self,
187
+ py: Python<'py>,
188
+ family: &Bound<'py, PyAny>,
189
+ limit: &Bound<'py, PyAny>,
190
+ scalar_encoder: &Bound<'py, PyAny>,
191
+ ) -> PyResult<FindKeys<'py>> {
192
+ let mut family_bytes = Vec::with_capacity(256);
193
+ let root = start(&mut family_bytes);
194
+ self.node(&mut family_bytes, "k", family, scalar_encoder)?;
195
+ finish(&mut family_bytes, root)?;
196
+
197
+ let exact = self.wrap(py, &family_bytes, limit, scalar_encoder)?;
198
+ Ok((PyBytes::new(py, &family_bytes), exact))
199
+ }
200
+
201
+ fn wrap<'py>(
202
+ &self,
203
+ py: Python<'py>,
204
+ family_bytes: &[u8],
205
+ limit: &Bound<'py, PyAny>,
206
+ scalar_encoder: &Bound<'py, PyAny>,
207
+ ) -> PyResult<Bound<'py, PyBytes>> {
208
+ let mut exact = Vec::with_capacity(family_bytes.len() + 128);
209
+ let root = start(&mut exact);
210
+ let outer = tuple(&mut exact, "k")?;
211
+ let tag = tagged(&mut exact, "0", "tag")?;
212
+ string(&mut exact, "p", "canonical-seq")?;
213
+ finish(&mut exact, tag)?;
214
+ let pair = tuple(&mut exact, "1")?;
215
+ let (kind, payload) = encoded_value(family_bytes)?;
216
+ header(&mut exact, kind, "0");
217
+ exact.extend_from_slice(payload);
218
+ self.node(&mut exact, "1", limit, scalar_encoder)?;
219
+ finish_tuple(&mut exact, pair)?;
220
+ finish_tuple(&mut exact, outer)?;
221
+ finish(&mut exact, root)?;
222
+ Ok(PyBytes::new(py, &exact))
223
+ }
224
+ }