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,89 @@ 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 namespace under which the [`ChannelManager`] will be persisted.
33
+ pub const CHANNEL_MANAGER_PERSISTENCE_NAMESPACE : & str = "" ;
34
+ /// The sub-namespace under which the [`ChannelManager`] will be persisted.
35
+ pub const CHANNEL_MANAGER_PERSISTENCE_SUB_NAMESPACE : & str = "" ;
36
+ /// The key under which the [`ChannelManager`] will be persisted.
37
+ pub const CHANNEL_MANAGER_PERSISTENCE_KEY : & str = "manager" ;
38
+
39
+ /// The namespace under which [`ChannelMonitor`]s will be persisted.
40
+ pub const CHANNEL_MONITOR_PERSISTENCE_NAMESPACE : & str = "monitors" ;
41
+ /// The sub-namespace under which [`ChannelMonitor`]s will be persisted.
42
+ pub const CHANNEL_MONITOR_PERSISTENCE_SUB_NAMESPACE : & str = "" ;
43
+
44
+ /// The namespace under which the [`NetworkGraph`] will be persisted.
45
+ pub const NETWORK_GRAPH_PERSISTENCE_NAMESPACE : & str = "" ;
46
+ /// The sub-namespace under which the [`NetworkGraph`] will be persisted.
47
+ pub const NETWORK_GRAPH_PERSISTENCE_SUB_NAMESPACE : & str = "" ;
48
+ /// The key under which the [`NetworkGraph`] will be persisted.
49
+ pub const NETWORK_GRAPH_PERSISTENCE_KEY : & str = "network_graph" ;
50
+
51
+ /// The namespace under which the [`WriteableScore`] will be persisted.
52
+ pub const SCORER_PERSISTENCE_NAMESPACE : & str = "" ;
53
+ /// The sub-namespace under which the [`WriteableScore`] will be persisted.
54
+ pub const SCORER_PERSISTENCE_SUB_NAMESPACE : & str = "" ;
55
+ /// The key under which the [`WriteableScore`] will be persisted.
56
+ pub const SCORER_PERSISTENCE_KEY : & str = "scorer" ;
57
+
58
+ /// Provides an interface that allows to store and retrieve persisted values that are associated
59
+ /// with given keys.
60
+ ///
61
+ /// In order to avoid collisions the key space is segmented based on the given `namespace`s and
62
+ /// `sub_namespace`s. Implementations of this trait are free to handle them in different ways, as
63
+ /// long as per-namespace key uniqueness is asserted.
64
+ ///
65
+ /// Keys and namespaces are required to be valid ASCII strings in the range of
66
+ /// [`KVSTORE_NAMESPACE_KEY_ALPHABET`] and no longer than 120 characters. Empty namespaces and
67
+ /// sub-namespaces (`""`) are assumed to be a valid, however, if `namespace` is empty,
68
+ /// `sub_namespace` is required to be empty, too. This means that concerns should always be separated by
69
+ /// namespace first, before sub-namespaces are used. Note that per-namespace uniqueness needs to
70
+ /// also hold for keys *and* namespaces/sub-namespaces in any given namespace/sub-namespace, i.e.,
71
+ /// conflicts between keys and equally named namespaces/sub-namespaces must be avoided.
72
+ ///
73
+ /// **Note:** Users migrating custom persistence backends from the pre-v0.0.117 `KVStorePersister`
74
+ /// interface can use a concatenation of `[{namespace}/[{sub_namespace}/]]{key}` to recover a `key` compatible with the
75
+ /// data model previously assumed by `KVStorePersister::persist`.
76
+ pub trait KVStore {
77
+ /// Returns the data stored for the given `namespace`, `sub_namespace`, and `key`.
78
+ ///
79
+ /// Returns an [`ErrorKind::NotFound`] if the given `key` could not be found in the given
80
+ /// `namespace` and `sub_namespace`.
81
+ ///
82
+ /// [`ErrorKind::NotFound`]: io::ErrorKind::NotFound
83
+ fn read ( & self , namespace : & str , sub_namespace : & str , key : & str ) -> io:: Result < Vec < u8 > > ;
84
+ /// Persists the given data under the given `key`.
85
+ ///
86
+ /// Will create the given `namespace` and `sub_namespace` if not already present in the store.
87
+ fn write ( & self , namespace : & str , sub_namespace : & str , key : & str , buf : & [ u8 ] ) -> io:: Result < ( ) > ;
88
+ /// Removes any data that had previously been persisted under the given `key`.
89
+ ///
90
+ /// If the `lazy` flag is set to `true`, the backend implementation might choose to lazily
91
+ /// remove the given `key` at some point in time after the method returns, e.g., as part of an
92
+ /// eventual batch deletion of multiple keys. As a consequence, subsequent calls to
93
+ /// [`KVStore::list`] might include the removed key until the changes are actually persisted.
94
+ ///
95
+ /// Note that while setting the `lazy` flag reduces the I/O burden of multiple subsequent
96
+ /// `remove` calls, it also influences the atomicity guarantees as lazy `remove`s could
97
+ /// potentially get lost on crash after the method returns. Therefore, this flag should only be
98
+ /// set for `remove` operations that can be safely replayed at a later time.
99
+ ///
100
+ /// Returns successfully if no data will be stored for the given `namespace`, `sub_namespace`, and
101
+ /// `key`, independently of whether it was present before its invokation or not.
102
+ fn remove ( & self , namespace : & str , sub_namespace : & str , key : & str , lazy : bool ) -> io:: Result < ( ) > ;
103
+ /// Returns a list of keys that are stored under the given `sub_namespace` in `namespace`.
104
+ ///
105
+ /// Returns the keys in arbitrary order, so users requiring a particular order need sort the
106
+ /// returned keys. Returns an empty list if `namespace` or `sub_namespace` is unknown.
107
+ fn list ( & self , namespace : & str , sub_namespace : & str ) -> io:: Result < Vec < String > > ;
108
+ }
26
109
27
110
/// Trait for a key-value store for persisting some writeable object at some key
28
111
/// Implementing `KVStorePersister` provides auto-implementations for [`Persister`]
0 commit comments