4
4
// You may not use this file except in accordance with one or both of these
5
5
// licenses.
6
6
7
- //! This module contains a simple key-value store trait KVStorePersister that
7
+ //! This module contains a simple key-value store trait [`KVStore`] that
8
8
//! allows one to implement the persistence for [`ChannelManager`], [`NetworkGraph`],
9
9
//! and [`ChannelMonitor`] all in one place.
10
10
11
11
use core:: ops:: Deref ;
12
12
use bitcoin:: hashes:: hex:: ToHex ;
13
13
use crate :: io;
14
+ use crate :: prelude:: { Vec , String } ;
14
15
use crate :: routing:: scoring:: WriteableScore ;
15
16
16
17
use crate :: chain;
@@ -22,7 +23,94 @@ use crate::chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate};
22
23
use crate :: ln:: channelmanager:: ChannelManager ;
23
24
use crate :: routing:: router:: Router ;
24
25
use crate :: routing:: gossip:: NetworkGraph ;
25
- use super :: { logger:: Logger , ser:: Writeable } ;
26
+ use crate :: util:: logger:: Logger ;
27
+ use crate :: util:: ser:: Writeable ;
28
+
29
+ /// The alphabet of characters allowed for namespaces and keys.
30
+ pub const KVSTORE_NAMESPACE_KEY_ALPHABET : & str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-" ;
31
+
32
+ /// The maximum number of characters namespaces and keys may have.
33
+ pub const KVSTORE_NAMESPACE_KEY_MAX_LEN : usize = 120 ;
34
+
35
+ /// The namespace under which the [`ChannelManager`] will be persisted.
36
+ pub const CHANNEL_MANAGER_PERSISTENCE_NAMESPACE : & str = "" ;
37
+ /// The sub-namespace under which the [`ChannelManager`] will be persisted.
38
+ pub const CHANNEL_MANAGER_PERSISTENCE_SUB_NAMESPACE : & str = "" ;
39
+ /// The key under which the [`ChannelManager`] will be persisted.
40
+ pub const CHANNEL_MANAGER_PERSISTENCE_KEY : & str = "manager" ;
41
+
42
+ /// The namespace under which [`ChannelMonitor`]s will be persisted.
43
+ pub const CHANNEL_MONITOR_PERSISTENCE_NAMESPACE : & str = "monitors" ;
44
+ /// The sub-namespace under which [`ChannelMonitor`]s will be persisted.
45
+ pub const CHANNEL_MONITOR_PERSISTENCE_SUB_NAMESPACE : & str = "" ;
46
+
47
+ /// The namespace under which the [`NetworkGraph`] will be persisted.
48
+ pub const NETWORK_GRAPH_PERSISTENCE_NAMESPACE : & str = "" ;
49
+ /// The sub-namespace under which the [`NetworkGraph`] will be persisted.
50
+ pub const NETWORK_GRAPH_PERSISTENCE_SUB_NAMESPACE : & str = "" ;
51
+ /// The key under which the [`NetworkGraph`] will be persisted.
52
+ pub const NETWORK_GRAPH_PERSISTENCE_KEY : & str = "network_graph" ;
53
+
54
+ /// The namespace under which the [`WriteableScore`] will be persisted.
55
+ pub const SCORER_PERSISTENCE_NAMESPACE : & str = "" ;
56
+ /// The sub-namespace under which the [`WriteableScore`] will be persisted.
57
+ pub const SCORER_PERSISTENCE_SUB_NAMESPACE : & str = "" ;
58
+ /// The key under which the [`WriteableScore`] will be persisted.
59
+ pub const SCORER_PERSISTENCE_KEY : & str = "scorer" ;
60
+
61
+ /// Provides an interface that allows storage and retrieval of persisted values that are associated
62
+ /// with given keys.
63
+ ///
64
+ /// In order to avoid collisions the key space is segmented based on the given `namespace`s and
65
+ /// `sub_namespace`s. Implementations of this trait are free to handle them in different ways, as
66
+ /// long as per-namespace key uniqueness is asserted.
67
+ ///
68
+ /// Keys and namespaces are required to be valid ASCII strings in the range of
69
+ /// [`KVSTORE_NAMESPACE_KEY_ALPHABET`] and no longer than [`KVSTORE_NAMESPACE_KEY_MAX_LEN`]. Empty
70
+ /// namespaces and sub-namespaces (`""`) are assumed to be a valid, however, if `namespace` is
71
+ /// empty, `sub_namespace` is required to be empty, too. This means that concerns should always be
72
+ /// separated by namespace first, before sub-namespaces are used. While the number of namespaces
73
+ /// will be relatively small and is determined at compile time, there may be many sub-namespaces
74
+ /// per namespace. Note that per-namespace uniqueness needs to also hold for keys *and*
75
+ /// namespaces/sub-namespaces in any given namespace/sub-namespace, i.e., conflicts between keys
76
+ /// and equally named namespaces/sub-namespaces must be avoided.
77
+ ///
78
+ /// **Note:** Users migrating custom persistence backends from the pre-v0.0.117 `KVStorePersister`
79
+ /// interface can use a concatenation of `[{namespace}/[{sub_namespace}/]]{key}` to recover a `key` compatible with the
80
+ /// data model previously assumed by `KVStorePersister::persist`.
81
+ pub trait KVStore {
82
+ /// Returns the data stored for the given `namespace`, `sub_namespace`, and `key`.
83
+ ///
84
+ /// Returns an [`ErrorKind::NotFound`] if the given `key` could not be found in the given
85
+ /// `namespace` and `sub_namespace`.
86
+ ///
87
+ /// [`ErrorKind::NotFound`]: io::ErrorKind::NotFound
88
+ fn read ( & self , namespace : & str , sub_namespace : & str , key : & str ) -> io:: Result < Vec < u8 > > ;
89
+ /// Persists the given data under the given `key`.
90
+ ///
91
+ /// Will create the given `namespace` and `sub_namespace` if not already present in the store.
92
+ fn write ( & self , namespace : & str , sub_namespace : & str , key : & str , buf : & [ u8 ] ) -> io:: Result < ( ) > ;
93
+ /// Removes any data that had previously been persisted under the given `key`.
94
+ ///
95
+ /// If the `lazy` flag is set to `true`, the backend implementation might choose to lazily
96
+ /// remove the given `key` at some point in time after the method returns, e.g., as part of an
97
+ /// eventual batch deletion of multiple keys. As a consequence, subsequent calls to
98
+ /// [`KVStore::list`] might include the removed key until the changes are actually persisted.
99
+ ///
100
+ /// Note that while setting the `lazy` flag reduces the I/O burden of multiple subsequent
101
+ /// `remove` calls, it also influences the atomicity guarantees as lazy `remove`s could
102
+ /// potentially get lost on crash after the method returns. Therefore, this flag should only be
103
+ /// set for `remove` operations that can be safely replayed at a later time.
104
+ ///
105
+ /// Returns successfully if no data will be stored for the given `namespace`, `sub_namespace`, and
106
+ /// `key`, independently of whether it was present before its invokation or not.
107
+ fn remove ( & self , namespace : & str , sub_namespace : & str , key : & str , lazy : bool ) -> io:: Result < ( ) > ;
108
+ /// Returns a list of keys that are stored under the given `sub_namespace` in `namespace`.
109
+ ///
110
+ /// Returns the keys in arbitrary order, so users requiring a particular order need to sort the
111
+ /// returned keys. Returns an empty list if `namespace` or `sub_namespace` is unknown.
112
+ fn list ( & self , namespace : & str , sub_namespace : & str ) -> io:: Result < Vec < String > > ;
113
+ }
26
114
27
115
/// Trait for a key-value store for persisting some writeable object at some key
28
116
/// Implementing `KVStorePersister` provides auto-implementations for [`Persister`]
0 commit comments