7 namespace org { namespace ldk { namespace structs {
11 /** An implementation of KVStore */
12 public interface KVStoreInterface {
13 /**Returns the data stored for the given `primary_namespace`, `secondary_namespace`, and
16 * Returns an [`ErrorKind::NotFound`] if the given `key` could not be found in the given
17 * `primary_namespace` and `secondary_namespace`.
19 * [`ErrorKind::NotFound`]: io::ErrorKind::NotFound
21 Result_CVec_u8ZIOErrorZ read(string primary_namespace, string secondary_namespace, string key);
22 /**Persists the given data under the given `key`.
24 * Will create the given `primary_namespace` and `secondary_namespace` if not already present
27 Result_NoneIOErrorZ write(string primary_namespace, string secondary_namespace, string key, byte[] buf);
28 /**Removes any data that had previously been persisted under the given `key`.
30 * If the `lazy` flag is set to `true`, the backend implementation might choose to lazily
31 * remove the given `key` at some point in time after the method returns, e.g., as part of an
32 * eventual batch deletion of multiple keys. As a consequence, subsequent calls to
33 * [`KVStore::list`] might include the removed key until the changes are actually persisted.
35 * Note that while setting the `lazy` flag reduces the I/O burden of multiple subsequent
36 * `remove` calls, it also influences the atomicity guarantees as lazy `remove`s could
37 * potentially get lost on crash after the method returns. Therefore, this flag should only be
38 * set for `remove` operations that can be safely replayed at a later time.
40 * Returns successfully if no data will be stored for the given `primary_namespace`,
41 * `secondary_namespace`, and `key`, independently of whether it was present before its
44 Result_NoneIOErrorZ remove(string primary_namespace, string secondary_namespace, string key, bool lazy);
45 /**Returns a list of keys that are stored under the given `secondary_namespace` in
46 * `primary_namespace`.
48 * Returns the keys in arbitrary order, so users requiring a particular order need to sort the
49 * returned keys. Returns an empty list if `primary_namespace` or `secondary_namespace` is unknown.
51 Result_CVec_StrZIOErrorZ list(string primary_namespace, string secondary_namespace);
55 * Provides an interface that allows storage and retrieval of persisted values that are associated
58 * In order to avoid collisions the key space is segmented based on the given `primary_namespace`s
59 * and `secondary_namespace`s. Implementations of this trait are free to handle them in different
60 * ways, as long as per-namespace key uniqueness is asserted.
62 * Keys and namespaces are required to be valid ASCII strings in the range of
63 * [`KVSTORE_NAMESPACE_KEY_ALPHABET`] and no longer than [`KVSTORE_NAMESPACE_KEY_MAX_LEN`]. Empty
64 * primary namespaces and secondary namespaces (`\"\"`) are assumed to be a valid, however, if
65 * `primary_namespace` is empty, `secondary_namespace` is required to be empty, too. This means
66 * that concerns should always be separated by primary namespace first, before secondary
67 * namespaces are used. While the number of primary namespaces will be relatively small and is
68 * determined at compile time, there may be many secondary namespaces per primary namespace. Note
69 * that per-namespace uniqueness needs to also hold for keys *and* namespaces in any given
70 * namespace, i.e., conflicts between keys and equally named
71 * primary namespaces/secondary namespaces must be avoided.
73 * Note:** Users migrating custom persistence backends from the pre-v0.0.117 `KVStorePersister`
74 * interface can use a concatenation of `[{primary_namespace}/[{secondary_namespace}/]]{key}` to
75 * recover a `key` compatible with the data model previously assumed by `KVStorePersister::persist`.
77 public class KVStore : CommonBase {
78 internal bindings.LDKKVStore bindings_instance;
79 internal long instance_idx;
81 internal KVStore(object _dummy, long ptr) : base(ptr) { bindings_instance = null; }
83 if (ptr != 0) { bindings.KVStore_free(ptr); }
86 private class LDKKVStoreHolder { internal KVStore held; }
87 private class LDKKVStoreImpl : bindings.LDKKVStore {
88 internal LDKKVStoreImpl(KVStoreInterface arg, LDKKVStoreHolder impl_holder) { this.arg = arg; this.impl_holder = impl_holder; }
89 private KVStoreInterface arg;
90 private LDKKVStoreHolder impl_holder;
91 public long read(long _primary_namespace, long _secondary_namespace, long _key) {
92 string _primary_namespace_conv = InternalUtils.decodeString(_primary_namespace);
93 string _secondary_namespace_conv = InternalUtils.decodeString(_secondary_namespace);
94 string _key_conv = InternalUtils.decodeString(_key);
95 Result_CVec_u8ZIOErrorZ ret = arg.read(_primary_namespace_conv, _secondary_namespace_conv, _key_conv);
97 long result = ret == null ? 0 : ret.clone_ptr();
100 public long write(long _primary_namespace, long _secondary_namespace, long _key, long _buf) {
101 string _primary_namespace_conv = InternalUtils.decodeString(_primary_namespace);
102 string _secondary_namespace_conv = InternalUtils.decodeString(_secondary_namespace);
103 string _key_conv = InternalUtils.decodeString(_key);
104 byte[] _buf_conv = InternalUtils.decodeUint8Array(_buf);
105 Result_NoneIOErrorZ ret = arg.write(_primary_namespace_conv, _secondary_namespace_conv, _key_conv, _buf_conv);
107 long result = ret == null ? 0 : ret.clone_ptr();
110 public long remove(long _primary_namespace, long _secondary_namespace, long _key, bool _lazy) {
111 string _primary_namespace_conv = InternalUtils.decodeString(_primary_namespace);
112 string _secondary_namespace_conv = InternalUtils.decodeString(_secondary_namespace);
113 string _key_conv = InternalUtils.decodeString(_key);
114 Result_NoneIOErrorZ ret = arg.remove(_primary_namespace_conv, _secondary_namespace_conv, _key_conv, _lazy);
116 long result = ret == null ? 0 : ret.clone_ptr();
119 public long list(long _primary_namespace, long _secondary_namespace) {
120 string _primary_namespace_conv = InternalUtils.decodeString(_primary_namespace);
121 string _secondary_namespace_conv = InternalUtils.decodeString(_secondary_namespace);
122 Result_CVec_StrZIOErrorZ ret = arg.list(_primary_namespace_conv, _secondary_namespace_conv);
124 long result = ret == null ? 0 : ret.clone_ptr();
129 /** Creates a new instance of KVStore from a given implementation */
130 public static KVStore new_impl(KVStoreInterface arg) {
131 LDKKVStoreHolder impl_holder = new LDKKVStoreHolder();
132 LDKKVStoreImpl impl = new LDKKVStoreImpl(arg, impl_holder);
133 long[] ptr_idx = bindings.LDKKVStore_new(impl);
135 impl_holder.held = new KVStore(null, ptr_idx[0]);
136 impl_holder.held.instance_idx = ptr_idx[1];
137 impl_holder.held.bindings_instance = impl;
138 return impl_holder.held;
142 * Returns the data stored for the given `primary_namespace`, `secondary_namespace`, and
145 * Returns an [`ErrorKind::NotFound`] if the given `key` could not be found in the given
146 * `primary_namespace` and `secondary_namespace`.
148 * [`ErrorKind::NotFound`]: io::ErrorKind::NotFound
150 public Result_CVec_u8ZIOErrorZ read(string primary_namespace, string secondary_namespace, string key) {
151 long ret = bindings.KVStore_read(this.ptr, InternalUtils.encodeString(primary_namespace), InternalUtils.encodeString(secondary_namespace), InternalUtils.encodeString(key));
153 GC.KeepAlive(primary_namespace);
154 GC.KeepAlive(secondary_namespace);
156 if (ret >= 0 && ret <= 4096) { return null; }
157 Result_CVec_u8ZIOErrorZ ret_hu_conv = Result_CVec_u8ZIOErrorZ.constr_from_ptr(ret);
162 * Persists the given data under the given `key`.
164 * Will create the given `primary_namespace` and `secondary_namespace` if not already present
167 public Result_NoneIOErrorZ write(string primary_namespace, string secondary_namespace, string key, byte[] buf) {
168 long ret = bindings.KVStore_write(this.ptr, InternalUtils.encodeString(primary_namespace), InternalUtils.encodeString(secondary_namespace), InternalUtils.encodeString(key), InternalUtils.encodeUint8Array(buf));
170 GC.KeepAlive(primary_namespace);
171 GC.KeepAlive(secondary_namespace);
174 if (ret >= 0 && ret <= 4096) { return null; }
175 Result_NoneIOErrorZ ret_hu_conv = Result_NoneIOErrorZ.constr_from_ptr(ret);
180 * Removes any data that had previously been persisted under the given `key`.
182 * If the `lazy` flag is set to `true`, the backend implementation might choose to lazily
183 * remove the given `key` at some point in time after the method returns, e.g., as part of an
184 * eventual batch deletion of multiple keys. As a consequence, subsequent calls to
185 * [`KVStore::list`] might include the removed key until the changes are actually persisted.
187 * Note that while setting the `lazy` flag reduces the I/O burden of multiple subsequent
188 * `remove` calls, it also influences the atomicity guarantees as lazy `remove`s could
189 * potentially get lost on crash after the method returns. Therefore, this flag should only be
190 * set for `remove` operations that can be safely replayed at a later time.
192 * Returns successfully if no data will be stored for the given `primary_namespace`,
193 * `secondary_namespace`, and `key`, independently of whether it was present before its
196 public Result_NoneIOErrorZ remove(string primary_namespace, string secondary_namespace, string key, bool lazy) {
197 long ret = bindings.KVStore_remove(this.ptr, InternalUtils.encodeString(primary_namespace), InternalUtils.encodeString(secondary_namespace), InternalUtils.encodeString(key), lazy);
199 GC.KeepAlive(primary_namespace);
200 GC.KeepAlive(secondary_namespace);
203 if (ret >= 0 && ret <= 4096) { return null; }
204 Result_NoneIOErrorZ ret_hu_conv = Result_NoneIOErrorZ.constr_from_ptr(ret);
209 * Returns a list of keys that are stored under the given `secondary_namespace` in
210 * `primary_namespace`.
212 * Returns the keys in arbitrary order, so users requiring a particular order need to sort the
213 * returned keys. Returns an empty list if `primary_namespace` or `secondary_namespace` is unknown.
215 public Result_CVec_StrZIOErrorZ list(string primary_namespace, string secondary_namespace) {
216 long ret = bindings.KVStore_list(this.ptr, InternalUtils.encodeString(primary_namespace), InternalUtils.encodeString(secondary_namespace));
218 GC.KeepAlive(primary_namespace);
219 GC.KeepAlive(secondary_namespace);
220 if (ret >= 0 && ret <= 4096) { return null; }
221 Result_CVec_StrZIOErrorZ ret_hu_conv = Result_CVec_StrZIOErrorZ.constr_from_ptr(ret);