client-query-cache 0.10.0__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.10.0 → client_query_cache-0.11.0}/PKG-INFO +4 -3
  2. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/README.md +3 -2
  3. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/native/shared-index/Cargo.lock +2 -2
  4. {client_query_cache-0.10.0 → 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.10.0 → 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/keys.rs +618 -0
  9. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/native/shared-index/src/lib.rs +3 -0
  10. client_query_cache-0.11.0/native/shared-index/src/local_cache/shared.rs +725 -0
  11. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/native/shared-index/src/local_cache.rs +285 -58
  12. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/native/shared-index/src/python.rs +62 -9
  13. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/pyproject.toml +17 -13
  14. client_query_cache-0.11.0/rust-toolchain.toml +5 -0
  15. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/_core/canonical.py +29 -5
  16. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/_core/codec.py +11 -9
  17. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/_core/cursor_capture.py +17 -18
  18. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/_core/entries.py +1 -1
  19. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/_core/find_one_reads.py +35 -5
  20. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/_core/keys.py +4 -2
  21. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/_core/lru.py +0 -17
  22. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/_core/manager.py +29 -255
  23. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/_core/native.py +2 -4
  24. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/_core/order_sensitive_keys.py +22 -3
  25. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/_core/read_validation.py +21 -3
  26. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/_shared/adapters.py +39 -13
  27. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/_shared/attachment.py +1 -0
  28. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/_shared/coordinator.py +106 -88
  29. client_query_cache-0.11.0/src/client_query_cache/_shared/shared_index.py +19 -0
  30. client_query_cache-0.11.0/src/client_query_cache/_shared_index.pyi +271 -0
  31. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/asynchronous/collection.py +31 -33
  32. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/asynchronous/cursors.py +3 -5
  33. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/asynchronous/manager.py +11 -9
  34. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/shared.py +1 -0
  35. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/synchronous/collection.py +31 -33
  36. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/synchronous/cursors.py +3 -5
  37. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/synchronous/manager.py +11 -9
  38. client_query_cache-0.10.0/src/client_query_cache/_core/residency.pyi +0 -23
  39. client_query_cache-0.10.0/src/client_query_cache/_shared/shared_index.py +0 -270
  40. client_query_cache-0.10.0/src/client_query_cache/_shared_index.pyi +0 -119
  41. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/LICENSE +0 -0
  42. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/native/shared-index/deny.toml +0 -0
  43. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/native/shared-index/src/clock.rs +0 -0
  44. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/native/shared-index/src/echo/tests.rs +0 -0
  45. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/native/shared-index/src/echo.rs +0 -0
  46. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/native/shared-index/src/frames/tests.rs +0 -0
  47. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/native/shared-index/src/frames.rs +0 -0
  48. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/native/shared-index/src/hash/tests.rs +0 -0
  49. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/native/shared-index/src/hash.rs +0 -0
  50. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/native/shared-index/src/index/support_tests.rs +0 -0
  51. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/native/shared-index/src/index/tests.rs +0 -0
  52. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/native/shared-index/src/key_encoder.rs +0 -0
  53. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/native/shared-index/src/memory/support_tests.rs +0 -0
  54. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/native/shared-index/src/memory/tests.rs +0 -0
  55. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/native/shared-index/src/memory.rs +0 -0
  56. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/native/shared-index/src/relay/tests.rs +0 -0
  57. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/native/shared-index/src/relay.rs +0 -0
  58. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/native/shared-index/src/ring/tests.rs +0 -0
  59. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/native/shared-index/src/ring.rs +0 -0
  60. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/native/shared-index/src/sync.rs +0 -0
  61. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/__init__.py +0 -0
  62. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/_core/__init__.py +0 -0
  63. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/_core/collation.py +0 -0
  64. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/_core/collection_metadata.py +0 -0
  65. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/_core/contracts.pyi +0 -0
  66. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/_core/count_reads.py +0 -0
  67. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/_core/errors.py +0 -0
  68. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/_core/find_reads.py +0 -0
  69. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/_core/identity_reads.py +0 -0
  70. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/_core/lifecycle.py +0 -0
  71. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/_core/locking.py +0 -0
  72. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/_core/namespace.py +0 -0
  73. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/_core/namespace_metadata.py +0 -0
  74. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/_core/projection.py +0 -0
  75. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/_core/query_filters.py +0 -0
  76. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/_core/read_classification.py +0 -0
  77. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/_core/snapshots.py +0 -0
  78. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/_core/stream_activation.py +0 -0
  79. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/_core/stream_cost.py +0 -0
  80. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/_core/stream_events.py +0 -0
  81. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/_core/stream_health.py +0 -0
  82. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/_core/stream_options.py +0 -0
  83. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/_core/stream_startup.py +0 -0
  84. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/_core/traversal.py +0 -0
  85. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/_core/unique_keys.py +0 -0
  86. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/_shared/__init__.py +0 -0
  87. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/_shared/wire.py +0 -0
  88. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/_types.py +0 -0
  89. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/asynchronous/__init__.py +0 -0
  90. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/asynchronous/database.py +0 -0
  91. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/asynchronous/shared.py +0 -0
  92. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/asynchronous/streams.py +0 -0
  93. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/otel.py +0 -0
  94. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/py.typed +0 -0
  95. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/synchronous/__init__.py +0 -0
  96. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/synchronous/database.py +0 -0
  97. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/synchronous/shared.py +0 -0
  98. {client_query_cache-0.10.0 → client_query_cache-0.11.0}/src/client_query_cache/synchronous/streams.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: client-query-cache
3
- Version: 0.10.0
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();