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