client-query-cache 0.10.0__tar.gz → 0.12.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.
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/PKG-INFO +5 -4
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/README.md +4 -3
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/native/shared-index/Cargo.lock +9 -2
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/native/shared-index/Cargo.toml +9 -1
- client_query_cache-0.12.0/native/shared-index/src/array/tests.rs +137 -0
- client_query_cache-0.12.0/native/shared-index/src/array.rs +117 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/native/shared-index/src/index.rs +8 -11
- client_query_cache-0.12.0/native/shared-index/src/keys.rs +618 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/native/shared-index/src/lib.rs +3 -0
- client_query_cache-0.12.0/native/shared-index/src/local_cache/shared.rs +727 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/native/shared-index/src/local_cache.rs +1076 -125
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/native/shared-index/src/python.rs +62 -9
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/pyproject.toml +27 -14
- client_query_cache-0.12.0/rust-toolchain.toml +5 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/src/client_query_cache/__init__.py +6 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/src/client_query_cache/_core/canonical.py +29 -5
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/src/client_query_cache/_core/codec.py +11 -9
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/src/client_query_cache/_core/contracts.pyi +10 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/src/client_query_cache/_core/cursor_capture.py +17 -18
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/src/client_query_cache/_core/entries.py +2 -1
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/src/client_query_cache/_core/find_one_reads.py +35 -5
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/src/client_query_cache/_core/identity_reads.py +15 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/src/client_query_cache/_core/keys.py +4 -2
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/src/client_query_cache/_core/locking.py +54 -2
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/src/client_query_cache/_core/lru.py +57 -21
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/src/client_query_cache/_core/manager.py +810 -474
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/src/client_query_cache/_core/namespace.py +12 -8
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/src/client_query_cache/_core/native.py +42 -8
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/src/client_query_cache/_core/order_sensitive_keys.py +22 -3
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/src/client_query_cache/_core/read_validation.py +21 -3
- client_query_cache-0.12.0/src/client_query_cache/_core/retention.py +219 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/src/client_query_cache/_core/snapshots.py +62 -2
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/src/client_query_cache/_shared/adapters.py +41 -14
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/src/client_query_cache/_shared/attachment.py +1 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/src/client_query_cache/_shared/coordinator.py +108 -90
- client_query_cache-0.12.0/src/client_query_cache/_shared/shared_index.py +19 -0
- client_query_cache-0.12.0/src/client_query_cache/_shared_index.pyi +271 -0
- {client_query_cache-0.10.0/src/client_query_cache/synchronous → client_query_cache-0.12.0/src/client_query_cache/asynchronous}/__init__.py +6 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/src/client_query_cache/asynchronous/collection.py +66 -50
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/src/client_query_cache/asynchronous/cursors.py +3 -5
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/src/client_query_cache/asynchronous/manager.py +11 -9
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/src/client_query_cache/shared.py +2 -1
- {client_query_cache-0.10.0/src/client_query_cache/asynchronous → client_query_cache-0.12.0/src/client_query_cache/synchronous}/__init__.py +6 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/src/client_query_cache/synchronous/collection.py +66 -50
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/src/client_query_cache/synchronous/cursors.py +3 -5
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/src/client_query_cache/synchronous/manager.py +11 -9
- client_query_cache-0.10.0/src/client_query_cache/_core/residency.pyi +0 -23
- client_query_cache-0.10.0/src/client_query_cache/_shared/shared_index.py +0 -270
- client_query_cache-0.10.0/src/client_query_cache/_shared_index.pyi +0 -119
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/LICENSE +0 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/native/shared-index/deny.toml +0 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/native/shared-index/src/clock.rs +0 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/native/shared-index/src/echo/tests.rs +0 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/native/shared-index/src/echo.rs +0 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/native/shared-index/src/frames/tests.rs +0 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/native/shared-index/src/frames.rs +0 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/native/shared-index/src/hash/tests.rs +0 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/native/shared-index/src/hash.rs +0 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/native/shared-index/src/index/support_tests.rs +0 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/native/shared-index/src/index/tests.rs +0 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/native/shared-index/src/key_encoder.rs +0 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/native/shared-index/src/memory/support_tests.rs +0 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/native/shared-index/src/memory/tests.rs +0 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/native/shared-index/src/memory.rs +0 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/native/shared-index/src/relay/tests.rs +0 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/native/shared-index/src/relay.rs +0 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/native/shared-index/src/ring/tests.rs +0 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/native/shared-index/src/ring.rs +0 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/native/shared-index/src/sync.rs +0 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/src/client_query_cache/_core/__init__.py +0 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/src/client_query_cache/_core/collation.py +0 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/src/client_query_cache/_core/collection_metadata.py +0 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/src/client_query_cache/_core/count_reads.py +0 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/src/client_query_cache/_core/errors.py +0 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/src/client_query_cache/_core/find_reads.py +0 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/src/client_query_cache/_core/lifecycle.py +0 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/src/client_query_cache/_core/namespace_metadata.py +0 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/src/client_query_cache/_core/projection.py +0 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/src/client_query_cache/_core/query_filters.py +0 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/src/client_query_cache/_core/read_classification.py +0 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/src/client_query_cache/_core/stream_activation.py +0 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/src/client_query_cache/_core/stream_cost.py +0 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/src/client_query_cache/_core/stream_events.py +0 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/src/client_query_cache/_core/stream_health.py +0 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/src/client_query_cache/_core/stream_options.py +0 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/src/client_query_cache/_core/stream_startup.py +0 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/src/client_query_cache/_core/traversal.py +0 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/src/client_query_cache/_core/unique_keys.py +0 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/src/client_query_cache/_shared/__init__.py +0 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/src/client_query_cache/_shared/wire.py +0 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/src/client_query_cache/_types.py +0 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/src/client_query_cache/asynchronous/database.py +0 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/src/client_query_cache/asynchronous/shared.py +0 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/src/client_query_cache/asynchronous/streams.py +0 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/src/client_query_cache/otel.py +0 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/src/client_query_cache/py.typed +0 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/src/client_query_cache/synchronous/database.py +0 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.0}/src/client_query_cache/synchronous/shared.py +0 -0
- {client_query_cache-0.10.0 → client_query_cache-0.12.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.
|
|
3
|
+
Version: 0.12.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
|
-

|
|
60
|
+

|
|
60
61
|
|
|
61
|
-
The graphic shows [process-local measurements from local
|
|
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
|
|
|
@@ -90,7 +91,7 @@ with (
|
|
|
90
91
|
|
|
91
92
|
Check the [platform and MongoDB requirements](https://alessio-locatelli.github.io/client-query-cache/getting-started/installation/#requirements) before installing. [Read eligibility](https://alessio-locatelli.github.io/client-query-cache/reference/api/#bypass-conditions) determines which requests can use the cache.
|
|
92
93
|
|
|
93
|
-
Invalidation is asynchronous: cached reads can
|
|
94
|
+
Eligible cache misses read from the primary with majority read concern; collections configured with another read preference or read concern bypass the cache. Invalidation is asynchronous, with no guaranteed delay: after a write, cached reads can return the preceding value until its change event is processed. There is no time-to-live, entry-age limit or manual invalidation call. Use direct PyMongo reads with suitable consistency controls for read-after-write and current authorization; see [consistency](https://alessio-locatelli.github.io/client-query-cache/usage/consistency/).
|
|
94
95
|
|
|
95
96
|
## Run an application
|
|
96
97
|
|
|
@@ -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
|
-

|
|
35
|
+

|
|
35
36
|
|
|
36
|
-
The graphic shows [process-local measurements from local
|
|
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
|
|
|
@@ -65,7 +66,7 @@ with (
|
|
|
65
66
|
|
|
66
67
|
Check the [platform and MongoDB requirements](https://alessio-locatelli.github.io/client-query-cache/getting-started/installation/#requirements) before installing. [Read eligibility](https://alessio-locatelli.github.io/client-query-cache/reference/api/#bypass-conditions) determines which requests can use the cache.
|
|
67
68
|
|
|
68
|
-
Invalidation is asynchronous: cached reads can
|
|
69
|
+
Eligible cache misses read from the primary with majority read concern; collections configured with another read preference or read concern bypass the cache. Invalidation is asynchronous, with no guaranteed delay: after a write, cached reads can return the preceding value until its change event is processed. There is no time-to-live, entry-age limit or manual invalidation call. Use direct PyMongo reads with suitable consistency controls for read-after-write and current authorization; see [consistency](https://alessio-locatelli.github.io/client-query-cache/usage/consistency/).
|
|
69
70
|
|
|
70
71
|
## Run an application
|
|
71
72
|
|
|
@@ -34,6 +34,7 @@ dependencies = [
|
|
|
34
34
|
"libc",
|
|
35
35
|
"loom",
|
|
36
36
|
"pyo3",
|
|
37
|
+
"rustc-hash",
|
|
37
38
|
]
|
|
38
39
|
|
|
39
40
|
[[package]]
|
|
@@ -71,9 +72,9 @@ checksum = "20870f649af7073d53e38067b2a84312175d56ea15217e1b15bc83506ec50afb"
|
|
|
71
72
|
|
|
72
73
|
[[package]]
|
|
73
74
|
name = "libc"
|
|
74
|
-
version = "0.2.
|
|
75
|
+
version = "0.2.191"
|
|
75
76
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
76
|
-
checksum = "
|
|
77
|
+
checksum = "65cd87c9a6d60536580b316ad985726104730a5151c99845f2490428ef91240a"
|
|
77
78
|
|
|
78
79
|
[[package]]
|
|
79
80
|
name = "log"
|
|
@@ -228,6 +229,12 @@ version = "0.8.11"
|
|
|
228
229
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
229
230
|
checksum = "d6f6ff9a378485b298a5286656da665ba74413d36db0979633275d2e708145d4"
|
|
230
231
|
|
|
232
|
+
[[package]]
|
|
233
|
+
name = "rustc-hash"
|
|
234
|
+
version = "2.1.3"
|
|
235
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
236
|
+
checksum = "6b1e7f9a428571be2dc5bc0505c13fb6bf936822b894ec87abf8a08a4e51742d"
|
|
237
|
+
|
|
231
238
|
[[package]]
|
|
232
239
|
name = "rustversion"
|
|
233
240
|
version = "1.0.23"
|
|
@@ -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.
|
|
@@ -21,6 +20,7 @@ python = ["dep:pyo3"]
|
|
|
21
20
|
[dependencies]
|
|
22
21
|
libc = "0.2.190"
|
|
23
22
|
pyo3 = { version = "0.30.1", features = ["abi3-py314"], optional = true }
|
|
23
|
+
rustc-hash = "2.1.3"
|
|
24
24
|
|
|
25
25
|
[target.'cfg(loom)'.dependencies]
|
|
26
26
|
loom = "0.7.2"
|
|
@@ -29,3 +29,11 @@ loom = "0.7.2"
|
|
|
29
29
|
# Rust 1.95 check-cfg: https://doc.rust-lang.org/rustc/check-cfg/cargo-specifics.html
|
|
30
30
|
# The default is warning on every cfg name Cargo does not declare. We override it because the loom model tests are selected with --cfg loom.
|
|
31
31
|
unexpected_cfgs = { level = "warn", check-cfg = ["cfg(loom)"] }
|
|
32
|
+
|
|
33
|
+
[profile.release]
|
|
34
|
+
# Cargo 1.99 lto: https://doc.rust-lang.org/cargo/reference/profiles.html#lto
|
|
35
|
+
# The default is false, which optimizes across a crate's own code generation units only. We override it because whole-program optimization across the crate and PyO3 shortens the native cache entry points (reports/rust-build-profile/summary.md).
|
|
36
|
+
lto = "fat"
|
|
37
|
+
# Cargo 1.99 codegen-units: https://doc.rust-lang.org/cargo/reference/profiles.html#codegen-units
|
|
38
|
+
# The default is 16. We override it because a single unit lets the optimizer inline across the whole extension.
|
|
39
|
+
codegen-units = 1
|
|
@@ -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
|
|
202
|
-
for (chunk, word) in chunks.
|
|
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
|
-
|
|
849
|
-
|
|
850
|
-
|
|
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
|
|
883
|
-
for chunk in chunks
|
|
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();
|