1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright 2023 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under The General Public License (GPL), version 3.
// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

use crate::record_store::{ClientRecordStore, NodeRecordStore};
use libp2p::kad::{
    store::{RecordStore, Result},
    KBucketDistance as Distance, ProviderRecord, Record, RecordKey,
};
use sn_protocol::NetworkAddress;
use sn_transfers::NanoTokens;
use std::{borrow::Cow, collections::HashSet};

pub enum UnifiedRecordStore {
    Client(ClientRecordStore),
    Node(NodeRecordStore),
}

impl RecordStore for UnifiedRecordStore {
    type RecordsIter<'a> = std::vec::IntoIter<Cow<'a, Record>>;
    type ProvidedIter<'a> = std::vec::IntoIter<Cow<'a, ProviderRecord>>;

    fn get(&self, k: &RecordKey) -> Option<std::borrow::Cow<'_, Record>> {
        match self {
            Self::Client(store) => store.get(k),
            Self::Node(store) => store.get(k),
        }
    }

    fn put(&mut self, r: Record) -> Result<()> {
        match self {
            Self::Client(store) => store.put(r),
            Self::Node(store) => store.put(r),
        }
    }

    fn remove(&mut self, k: &RecordKey) {
        match self {
            Self::Client(store) => store.remove(k),
            Self::Node(store) => store.remove(k),
        }
    }

    fn records(&self) -> Self::RecordsIter<'_> {
        match self {
            Self::Client(store) => store.records(),
            Self::Node(store) => store.records(),
        }
    }

    fn add_provider(&mut self, record: ProviderRecord) -> Result<()> {
        match self {
            Self::Client(store) => store.add_provider(record),
            Self::Node(store) => store.add_provider(record),
        }
    }

    fn providers(&self, key: &RecordKey) -> Vec<ProviderRecord> {
        match self {
            Self::Client(store) => store.providers(key),
            Self::Node(store) => store.providers(key),
        }
    }

    fn provided(&self) -> Self::ProvidedIter<'_> {
        match self {
            Self::Client(store) => store.provided(),
            Self::Node(store) => store.provided(),
        }
    }

    fn remove_provider(&mut self, k: &RecordKey, p: &libp2p::PeerId) {
        match self {
            Self::Client(store) => store.remove_provider(k, p),
            Self::Node(store) => store.remove_provider(k, p),
        }
    }
}

impl UnifiedRecordStore {
    pub(crate) fn contains(&self, key: &RecordKey) -> bool {
        match self {
            Self::Client(store) => store.contains(key),
            Self::Node(store) => store.contains(key),
        }
    }

    pub(crate) fn record_addresses(&self) -> HashSet<NetworkAddress> {
        match self {
            Self::Client(store) => store.record_addresses(),
            Self::Node(store) => store.record_addresses(),
        }
    }

    #[allow(clippy::mutable_key_type)]
    pub(crate) fn record_addresses_ref(&self) -> &HashSet<RecordKey> {
        match self {
            Self::Client(store) => store.record_addresses_ref(),
            Self::Node(store) => store.record_addresses_ref(),
        }
    }

    pub(crate) fn put_verified(&mut self, r: Record) -> Result<()> {
        match self {
            Self::Client(store) => store.put_verified(r),
            Self::Node(store) => store.put_verified(r),
        }
    }

    pub(crate) fn store_cost(&self) -> NanoTokens {
        match self {
            Self::Client(_) => {
                warn!("Calling store cost calculation at Client. This should not happen");
                NanoTokens::zero()
            }
            Self::Node(store) => store.store_cost(),
        }
    }

    pub(crate) fn set_distance_range(&mut self, distance_range: Distance) {
        match self {
            Self::Client(store) => store.set_distance_range(distance_range),
            Self::Node(store) => store.set_distance_range(distance_range),
        }
    }
}