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
// Copyright The Pit Project Owners. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Please see https://github.com/openpitkit and the OWNERS file for details.
//! Generic key-value storage with pluggable synchronization.
//!
//! This module exposes a small, focused storage abstraction around a
//! `HashMap<Key, Value>` whose synchronization scheme is chosen at the
//! type level via a [`LockingPolicyFactory`]. The intended use is the
//! application-bootstrap-time construction of in-memory tables that
//! later serve hot-path lookups.
//!
//! # Concepts
//!
//! * [`StorageBuilder<Factory>`] — the builder, parameterised by a
//! [`LockingPolicyFactory`]. It is owned by the engine builder and
//! exposed through `storage_builder()`; each storage gets its own
//! freshly created [`LockingPolicy`].
//! * [`Storage<Key, Value, LockingPolicy>`] — the actual key-value
//! store. Exposes scoped access methods that run a caller closure
//! while the appropriate locks are held; references handed to the
//! closure are confined to the closure's call. Cannot be cloned.
//!
//! # Built-in policies
//!
//! Three policies cover the common synchronization regimes; the
//! mechanism is deliberately open so that custom regimes can be added
//! by implementing [`LockingPolicy`] and [`LockingPolicyFactory`].
//! Concrete policy types are private to this crate; callers name only
//! the factory types below.
//!
//! * [`NoLocking`] — no synchronization. The resulting storage is
//! `!Send` and `!Sync`. The closure-based access methods make safe
//! misuse via overlapping references impossible at the type level;
//! accidental closure re-entry on the same storage is detected by a
//! debug-only check.
//! * [`IndexLocking`] — one reader-writer lock guards key insertions
//! and removals; per-key value access is unsynchronized. The closure
//! based access methods, plus the debug-only re-entry check, keep
//! single-thread misuse impossible to express in safe code.
//! * [`FullLocking`] — one reader-writer lock guards key insertions
//! and removals, a second reader-writer lock guards every value
//! access. A writer to one value blocks readers of every other
//! value; the trade-off is a small fixed amount of state per storage
//! instead of a per-key allocation.
//!
//! # Example
//!
//! ```
//! use openpit::Engine;
//!
//! let builder = Engine::builder::<(), (), ()>().full_sync();
//! let storage = builder.storage_builder().create_for_bound_key::<&'static str, u64>();
//!
//! // Insert.
//! storage.with_mut("ticks", || 0, |counter, is_new| {
//! assert!(is_new);
//! *counter += 1;
//! });
//!
//! // Read.
//! assert_eq!(storage.with(&"ticks", |v| *v), Some(1));
//!
//! // Increment under a write closure.
//! storage.with_mut("ticks", || 0, |counter, is_new| {
//! assert!(!is_new);
//! *counter += 41;
//! });
//! assert_eq!(storage.with(&"ticks", |v| *v), Some(42));
//!
//! // Remove.
//! assert!(storage.remove(&"ticks"));
//! assert_eq!(storage.with(&"ticks", |v| *v), None);
//! ```
//!
//! # Compile-time vs runtime configuration
//!
//! Synchronization is selected statically through the type parameter
//! `LockingPolicy`. This lets the compiler inline and specialize every
//! operation for a known policy, which is important for the no-op
//! `NoLocking` regime. Runtime selection (e.g. exposing the choice
//! through a future FFI surface) can be layered on top by providing an
//! enum that implements [`LockingPolicy`] and dispatches its methods
//! through a `match`; that layer is not part of this module.
pub use StorageBuilder;
pub use ;
pub use IndexFlag;
pub use ;
pub use ;
pub use ;
pub use Storage;