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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
use holochain_core_types::{
    chain_header::ChainHeader,
    crud_status::CrudStatus,
    entry::{entry_type::EntryType, Entry, EntryWithMeta},
    time::Timeout,
};
use holochain_json_api::{error::JsonError, json::*};

use holochain_persistence_api::cas::content::{Address, AddressableContent};
use std::collections::HashMap;

#[derive(Deserialize, Debug, Serialize, DefaultJson, Clone, PartialEq)]
pub enum StatusRequestKind {
    Initial,
    Latest,
    All,
}
impl Default for StatusRequestKind {
    fn default() -> Self {
        StatusRequestKind::Latest
    }
}

/// Structure used to specify what should be returned to a call to get_entry_result()
/// The default is to return the latest entry.
#[derive(Deserialize, Debug, Serialize, DefaultJson, PartialEq, Clone)]
pub struct GetEntryOptions {
    pub status_request: StatusRequestKind,
    pub entry: bool,
    pub headers: bool,
    pub timeout: Timeout,
}

impl Default for GetEntryOptions {
    fn default() -> Self {
        GetEntryOptions {
            status_request: StatusRequestKind::default(),
            entry: true,
            headers: false,
            timeout: Default::default(),
        }
    }
}

impl GetEntryOptions {
    pub fn new(
        status_request: StatusRequestKind,
        entry: bool,
        headers: bool,
        timeout: Timeout,
    ) -> Self {
        GetEntryOptions {
            status_request,
            entry,
            headers,
            timeout,
        }
    }
}

#[derive(Deserialize, Debug, Serialize, DefaultJson)]
pub struct GetEntryArgs {
    pub address: Address,
    pub options: GetEntryOptions,
}

#[derive(Deserialize, Debug, Serialize, DefaultJson, Clone)]
pub struct EntryResultMeta {
    pub address: Address,
    pub entry_type: EntryType,
    pub crud_status: CrudStatus,
}

/// Structure that holds data returned from a get entry request.
/// When the meta is None, we know the entry wasn't found.  This is
/// because at the very least the entry_type and the address will be
/// returned if nothing else was requested in the GetEntryOptions
#[derive(Deserialize, Debug, Serialize, DefaultJson, Clone)]
pub struct GetEntryResultItem {
    pub meta: Option<EntryResultMeta>,
    pub entry: Option<Entry>,
    pub headers: Vec<ChainHeader>, // headers if requested in options
}
impl GetEntryResultItem {
    pub fn new(maybe_entry_with_meta: Option<(&EntryWithMeta, Vec<ChainHeader>)>) -> Self {
        match maybe_entry_with_meta {
            Some((entry_with_meta, headers)) => GetEntryResultItem {
                meta: Some(EntryResultMeta {
                    address: entry_with_meta.entry.address(),
                    entry_type: entry_with_meta.entry.entry_type(),
                    crud_status: entry_with_meta.crud_status,
                }),
                entry: Some(entry_with_meta.entry.clone()),
                headers,
            },
            _ => GetEntryResultItem {
                meta: None,
                entry: None,
                headers: Vec::new(),
            },
        }
    }
}

/// Structure that holds a whole crud status history if the status request
/// in the GetEntryOptions was set to StatusRequestKind::All
#[derive(Deserialize, Debug, Serialize, DefaultJson, Clone, Default)]
pub struct EntryHistory {
    pub items: Vec<GetEntryResultItem>,
    pub crud_links: HashMap<Address, Address>,
}
impl EntryHistory {
    pub fn new() -> Self {
        Default::default()
    }

    pub fn push(&mut self, entry_with_meta: &EntryWithMeta, headers: Vec<ChainHeader>) {
        let address = entry_with_meta.entry.address();
        let item = GetEntryResultItem::new(Some((entry_with_meta, headers)));
        self.items.push(item);
        if let Some(new_address) = entry_with_meta.maybe_link_update_delete.clone() {
            self.crud_links.insert(address, new_address);
        }
    }
}

#[derive(Deserialize, Debug, Serialize, DefaultJson, Clone)]
#[allow(clippy::large_enum_variant)]
pub enum GetEntryResultType {
    Single(GetEntryResultItem),
    All(EntryHistory),
}

#[derive(Deserialize, Debug, Serialize, DefaultJson, Clone)]
pub struct GetEntryResult {
    pub result: GetEntryResultType,
}
impl GetEntryResult {
    pub fn new(
        request_kind: StatusRequestKind,
        maybe_entry_with_meta_and_headers: Option<(&EntryWithMeta, Vec<ChainHeader>)>,
    ) -> Self {
        match request_kind {
            StatusRequestKind::All => {
                let mut entry_result = GetEntryResult {
                    result: GetEntryResultType::All(EntryHistory::new()),
                };
                if let Some((entry_with_meta, headers)) = maybe_entry_with_meta_and_headers {
                    entry_result.push(entry_with_meta, headers);
                }
                entry_result
            }
            _ => GetEntryResult {
                result: GetEntryResultType::Single(GetEntryResultItem::new(
                    maybe_entry_with_meta_and_headers,
                )),
            },
        }
    }
    pub fn found(&self) -> bool {
        match self.result {
            GetEntryResultType::Single(ref item) => item.meta.is_some(),
            GetEntryResultType::All(ref history) => !history.items.is_empty(),
        }
    }

    /// clears the entry result to be equivalent to not found
    pub fn clear(&mut self) {
        match self.result {
            GetEntryResultType::Single(_) => {
                self.result = GetEntryResultType::Single(GetEntryResultItem::new(None))
            }
            GetEntryResultType::All(ref mut history) => history.items.clear(),
        };
    }

    /// adds an item to history, or if Single, writes over the current value of the item
    pub fn push(&mut self, entry_with_meta: &EntryWithMeta, headers: Vec<ChainHeader>) {
        match self.result {
            GetEntryResultType::Single(_) => {
                self.result = GetEntryResultType::Single(GetEntryResultItem::new(Some((
                    entry_with_meta,
                    headers,
                ))))
            }
            GetEntryResultType::All(ref mut history) => history.push(entry_with_meta, headers),
        };
    }

    /// returns the entry searched for.  Note that if the GetEntryOptions did not
    /// include a request for the entry value, this function will return None even if the
    /// entry was found.
    pub fn latest(&self) -> Option<Entry> {
        match self.result {
            GetEntryResultType::Single(ref item) => item.entry.clone(),
            GetEntryResultType::All(ref history) => {
                let last = history.items.last()?;
                last.entry.clone()
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use holochain_core_types::{
        chain_header::test_chain_header,
        entry::{test_entry, test_entry_a, test_entry_b},
    };

    #[test]
    fn test_get_entry_result_found() {
        let result = GetEntryResult::new(StatusRequestKind::Initial, None);
        assert!(!result.found());
        let result = GetEntryResult::new(StatusRequestKind::Latest, None);
        assert!(!result.found());
        let result = GetEntryResult::new(StatusRequestKind::All, None);
        assert!(!result.found());
    }

    #[test]
    fn test_get_entry_single_latest() {
        let mut result = GetEntryResult::new(StatusRequestKind::Initial, None);
        assert_eq!(result.latest(), None);
        result.push(
            &EntryWithMeta {
                entry: test_entry(),
                crud_status: CrudStatus::Live,
                maybe_link_update_delete: None,
            },
            vec![test_chain_header()],
        );
        assert!(result.found());
        assert_eq!(result.latest(), Some(test_entry()));
    }

    #[test]
    fn test_get_entry_all_latest() {
        let mut result = GetEntryResult::new(StatusRequestKind::All, None);
        assert_eq!(result.latest(), None);
        result.push(
            &EntryWithMeta {
                entry: test_entry_a(),
                crud_status: CrudStatus::Modified,
                maybe_link_update_delete: None,
            },
            vec![test_chain_header()],
        );
        result.push(
            &EntryWithMeta {
                entry: test_entry_b(),
                crud_status: CrudStatus::Live,
                maybe_link_update_delete: None,
            },
            vec![test_chain_header()],
        );
        assert!(result.found());
        assert_eq!(result.latest(), Some(test_entry_b()));
    }

    #[test]
    fn test_clear() {
        let mut result = GetEntryResult::new(StatusRequestKind::All, None);
        result.push(
            &EntryWithMeta {
                entry: test_entry(),
                crud_status: CrudStatus::Live,
                maybe_link_update_delete: None,
            },
            vec![test_chain_header()],
        );
        assert!(result.found());
        result.clear();
        assert!(!result.found());

        result = GetEntryResult::new(StatusRequestKind::Initial, None);
        result.push(
            &EntryWithMeta {
                entry: test_entry(),
                crud_status: CrudStatus::Live,
                maybe_link_update_delete: None,
            },
            vec![test_chain_header()],
        );
        assert!(result.found());
        result.clear();
        assert!(!result.found());
    }
}