libp2p_kad/record/store.rs
1// Copyright 2019 Parity Technologies (UK) Ltd.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the "Software"),
5// to deal in the Software without restriction, including without limitation
6// the rights to use, copy, modify, merge, publish, distribute, sublicense,
7// and/or sell copies of the Software, and to permit persons to whom the
8// Software is furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
14// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19// DEALINGS IN THE SOFTWARE.
20
21mod memory;
22
23pub use memory::{MemoryStore, MemoryStoreConfig};
24
25use crate::K_VALUE;
26use super::*;
27use std::borrow::Cow;
28
29/// The result of an operation on a `RecordStore`.
30pub type Result<T> = std::result::Result<T, Error>;
31
32/// The possible errors of a `RecordStore` operation.
33#[derive(Debug)]
34pub enum Error {
35 /// The store is at capacity w.r.t. the total number of stored records.
36 MaxRecords,
37 /// The store is at capacity w.r.t. the total number of stored keys for
38 /// provider records.
39 MaxProvidedKeys,
40 /// The value of a record to be stored is too large.
41 ValueTooLarge,
42}
43
44/// Trait for types implementing a record store.
45///
46/// There are two types of records managed by a `RecordStore`:
47///
48/// 1. Regular (value-)records. These records store an arbitrary value
49/// associated with a key which is distributed to the closest nodes
50/// to the key in the Kademlia DHT as per the standard Kademlia "push-model".
51/// These records are subject to re-replication and re-publication as
52/// per the standard Kademlia protocol.
53///
54/// 2. Provider records. These records associate the ID of a peer with a key
55/// who can supposedly provide the associated value. These records are
56/// mere "pointers" to the data which may be followed by contacting these
57/// providers to obtain the value. These records are specific to the
58/// libp2p Kademlia specification and realise a "pull-model" for distributed
59/// content. Just like a regular record, a provider record is distributed
60/// to the closest nodes to the key.
61///
62pub trait RecordStore<'a> {
63 type RecordsIter: Iterator<Item = Cow<'a, Record>>;
64 type ProvidedIter: Iterator<Item = Cow<'a, ProviderRecord>>;
65
66 /// Gets a record from the store, given its key.
67 fn get(&'a self, k: &Key) -> Option<Cow<'_, Record>>;
68
69 /// Puts a record into the store.
70 fn put(&'a mut self, r: Record) -> Result<()>;
71
72 /// Removes the record with the given key from the store.
73 fn remove(&'a mut self, k: &Key);
74
75 /// Gets an iterator over all (value-) records currently stored.
76 fn records(&'a self) -> Self::RecordsIter;
77
78 /// Adds a provider record to the store.
79 ///
80 /// A record store only needs to store a number of provider records
81 /// for a key corresponding to the replication factor and should
82 /// store those records whose providers are closest to the key.
83 fn add_provider(&'a mut self, record: ProviderRecord) -> Result<()>;
84
85 /// Gets a copy of the stored provider records for the given key.
86 fn providers(&'a self, key: &Key) -> Vec<ProviderRecord>;
87
88 /// Gets an iterator over all stored provider records for which the
89 /// node owning the store is itself the provider.
90 fn provided(&'a self) -> Self::ProvidedIter;
91
92 /// Removes a provider record from the store.
93 fn remove_provider(&'a mut self, k: &Key, p: &PeerId);
94}
95