Skip to content

Commit 2e27b79

Browse files
committed
Add KVStore interface trait
We upstream the `KVStore` interface trait from LDK Node, which will replace `KVStorePersister` in the coming commits. Besides persistence, `KVStore` implementations will also offer to `list` keys present in a given `namespace` and `read` the stored values.
1 parent 131560e commit 2e27b79

File tree

1 file changed

+53
-1
lines changed

1 file changed

+53
-1
lines changed

lightning/src/util/persist.rs

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use core::ops::Deref;
1212
use bitcoin::hashes::hex::ToHex;
1313
use crate::io;
14+
use crate::prelude::{Vec, String};
1415
use crate::routing::scoring::WriteableScore;
1516

1617
use crate::chain;
@@ -22,7 +23,58 @@ use crate::chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate};
2223
use crate::ln::channelmanager::ChannelManager;
2324
use crate::routing::router::Router;
2425
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 namespace under which the [`ChannelManager`] will be persisted.
30+
pub const CHANNEL_MANAGER_PERSISTENCE_NAMESPACE: &str = "";
31+
/// The key under which the [`ChannelManager`] will be persisted.
32+
pub const CHANNEL_MANAGER_PERSISTENCE_KEY: &str = "manager";
33+
34+
/// The namespace under which [`ChannelMonitor`]s will be persisted.
35+
pub const CHANNEL_MONITOR_PERSISTENCE_NAMESPACE: &str = "monitors";
36+
37+
/// The namespace under which the [`NetworkGraph`] will be persisted.
38+
pub const NETWORK_GRAPH_PERSISTENCE_NAMESPACE: &str = "";
39+
/// The key under which the [`NetworkGraph`] will be persisted.
40+
pub const NETWORK_GRAPH_PERSISTENCE_KEY: &str = "network_graph";
41+
42+
/// The namespace under which the [`WriteableScore`] will be persisted.
43+
pub const SCORER_PERSISTENCE_NAMESPACE: &str = "";
44+
/// The key under which the [`WriteableScore`] will be persisted.
45+
pub const SCORER_PERSISTENCE_KEY: &str = "scorer";
46+
47+
/// Provides an interface that allows to store and retrieve persisted values that are associated
48+
/// with given keys.
49+
///
50+
/// In order to avoid collisions the key space is segmented based on the given `namespace`s.
51+
/// Implementations of this trait are free to handle them in different ways, as long as
52+
/// per-namespace key uniqueness is asserted.
53+
///
54+
/// Keys and namespaces are required to be valid ASCII strings and the empty namespace (`""`) is
55+
/// assumed to be valid namespace.
56+
pub trait KVStore {
57+
/// A reader as returned by [`Self::read`].
58+
type Reader: io::Read;
59+
/// Returns an [`io::Read`] for the given `namespace` and `key` from which [`Readable`]s may be
60+
/// read.
61+
///
62+
/// Returns an [`ErrorKind::NotFound`] if the given `key` could not be found in the given `namespace`.
63+
///
64+
/// [`Readable`]: crate::util::ser::Readable
65+
/// [`ErrorKind::NotFound`]: io::ErrorKind::NotFound
66+
fn read(&self, namespace: &str, key: &str) -> io::Result<Self::Reader>;
67+
/// Persists the given data under the given `key`.
68+
///
69+
/// Will create the given `namespace` if not already present in the store.
70+
fn write(&self, namespace: &str, key: &str, buf: &[u8]) -> io::Result<()>;
71+
/// Removes any data that had previously been persisted under the given `key`.
72+
fn remove(&self, namespace: &str, key: &str) -> io::Result<()>;
73+
/// Returns a list of keys that are stored under the given `namespace`.
74+
///
75+
/// Will return an empty list if the `namespace` is unknown.
76+
fn list(&self, namespace: &str) -> io::Result<Vec<String>>;
77+
}
2678

2779
/// Trait for a key-value store for persisting some writeable object at some key
2880
/// Implementing `KVStorePersister` provides auto-implementations for [`Persister`]

0 commit comments

Comments
 (0)