libp2p_kad/record_priv/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};
24use thiserror::Error;
25
26use super::*;
27use crate::K_VALUE;
28use std::borrow::Cow;
29
30/// The result of an operation on a `RecordStore`.
31pub type Result<T> = std::result::Result<T, Error>;
32
33/// The possible errors of a `RecordStore` operation.
34#[derive(Error, Debug, Clone)]
35pub enum Error {
36 /// The store is at capacity w.r.t. the total number of stored records.
37 #[error("the store cannot contain any more records")]
38 MaxRecords,
39
40 /// The store is at capacity w.r.t. the total number of stored provider records.
41 #[error("the store cannot contain any more provider records")]
42 MaxProvidedKeys,
43
44 /// The store cannot store this value because it is too large.
45 #[error("the value is too large to be stored")]
46 ValueTooLarge,
47}
48
49/// Trait for types implementing a record store.
50///
51/// There are two types of records managed by a `RecordStore`:
52///
53/// 1. Regular (value-)records. These records store an arbitrary value
54/// associated with a key which is distributed to the closest nodes
55/// to the key in the Kademlia DHT as per the standard Kademlia "push-model".
56/// These records are subject to re-replication and re-publication as
57/// per the standard Kademlia protocol.
58///
59/// 2. Provider records. These records associate the ID of a peer with a key
60/// who can supposedly provide the associated value. These records are
61/// mere "pointers" to the data which may be followed by contacting these
62/// providers to obtain the value. These records are specific to the
63/// libp2p Kademlia specification and realise a "pull-model" for distributed
64/// content. Just like a regular record, a provider record is distributed
65/// to the closest nodes to the key.
66///
67pub trait RecordStore {
68 type RecordsIter<'a>: Iterator<Item = Cow<'a, Record>>
69 where
70 Self: 'a;
71 type ProvidedIter<'a>: Iterator<Item = Cow<'a, ProviderRecord>>
72 where
73 Self: 'a;
74
75 /// Gets a record from the store, given its key.
76 fn get(&self, k: &Key) -> Option<Cow<'_, Record>>;
77
78 /// Puts a record into the store.
79 fn put(&mut self, r: Record) -> Result<()>;
80
81 /// Removes the record with the given key from the store.
82 fn remove(&mut self, k: &Key);
83
84 /// Gets an iterator over all (value-) records currently stored.
85 fn records(&self) -> Self::RecordsIter<'_>;
86
87 /// Adds a provider record to the store.
88 ///
89 /// A record store only needs to store a number of provider records
90 /// for a key corresponding to the replication factor and should
91 /// store those records whose providers are closest to the key.
92 fn add_provider(&mut self, record: ProviderRecord) -> Result<()>;
93
94 /// Gets a copy of the stored provider records for the given key.
95 fn providers(&self, key: &Key) -> Vec<ProviderRecord>;
96
97 /// Gets an iterator over all stored provider records for which the
98 /// node owning the store is itself the provider.
99 fn provided(&self) -> Self::ProvidedIter<'_>;
100
101 /// Removes a provider record from the store.
102 fn remove_provider(&mut self, k: &Key, p: &PeerId);
103}