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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
use std::borrow::Borrow;
use std::collections::HashMap;
use std::fmt::{self, Display, Formatter};
use std::time::SystemTime;

use super::errors::{BkError, ErrorKind};

/// Ledger id.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct LedgerId(pub(crate) i64);

impl TryFrom<i64> for LedgerId {
    type Error = BkError;

    fn try_from(i: i64) -> Result<LedgerId, BkError> {
        if i < 0 {
            return Err(BkError::new(ErrorKind::InvalidLedgerId));
        }
        Ok(LedgerId(i))
    }
}

impl From<LedgerId> for i64 {
    fn from(ledger_id: LedgerId) -> Self {
        ledger_id.0
    }
}

impl PartialEq<i64> for LedgerId {
    fn eq(&self, other: &i64) -> bool {
        self.0.eq(other)
    }
}

impl Display for LedgerId {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::result::Result<(), fmt::Error> {
        Display::fmt(&self.0, f)
    }
}

/// Ledger length.
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct LedgerLength(i64);

impl LedgerLength {
    pub const ZERO: LedgerLength = LedgerLength(0);
}

impl From<i64> for LedgerLength {
    fn from(i: i64) -> LedgerLength {
        LedgerLength(i)
    }
}

impl From<LedgerLength> for i64 {
    fn from(ledger_length: LedgerLength) -> i64 {
        ledger_length.0
    }
}

impl From<usize> for LedgerLength {
    fn from(u: usize) -> LedgerLength {
        LedgerLength(u as i64)
    }
}

impl Display for LedgerLength {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::result::Result<(), fmt::Error> {
        Display::fmt(&self.0, f)
    }
}

impl std::ops::Sub<i64> for LedgerLength {
    type Output = Self;

    fn sub(self, rhs: i64) -> LedgerLength {
        LedgerLength(self.0 - rhs)
    }
}

impl std::ops::Add<i64> for LedgerLength {
    type Output = Self;

    fn add(self, rhs: i64) -> LedgerLength {
        LedgerLength(self.0 + rhs)
    }
}

impl std::ops::SubAssign<i64> for LedgerLength {
    fn sub_assign(&mut self, rhs: i64) {
        self.0 -= rhs;
    }
}

impl std::ops::AddAssign<i64> for LedgerLength {
    fn add_assign(&mut self, rhs: i64) {
        self.0 += rhs;
    }
}

impl std::ops::Sub<usize> for LedgerLength {
    type Output = Self;

    fn sub(self, rhs: usize) -> LedgerLength {
        LedgerLength(self.0 - rhs as i64)
    }
}

impl std::ops::Add<usize> for LedgerLength {
    type Output = Self;

    fn add(self, rhs: usize) -> LedgerLength {
        LedgerLength(self.0 + rhs as i64)
    }
}

impl std::ops::SubAssign<usize> for LedgerLength {
    fn sub_assign(&mut self, rhs: usize) {
        self.0 -= rhs as i64;
    }
}

impl std::ops::AddAssign<usize> for LedgerLength {
    fn add_assign(&mut self, rhs: usize) {
        self.0 += rhs as i64;
    }
}

/// Ledger entry id.
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct EntryId(pub(crate) i64);

impl Display for EntryId {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::result::Result<(), fmt::Error> {
        Display::fmt(&self.0, f)
    }
}

impl std::cmp::PartialEq<i64> for EntryId {
    fn eq(&self, other: &i64) -> bool {
        self.0.eq(other)
    }
}

impl std::cmp::PartialOrd<i64> for EntryId {
    fn partial_cmp(&self, other: &i64) -> Option<std::cmp::Ordering> {
        self.0.partial_cmp(other)
    }
}

impl std::ops::Sub for EntryId {
    type Output = i64;

    fn sub(self, rhs: EntryId) -> i64 {
        self.0 - rhs.0
    }
}

impl std::ops::Sub<i64> for EntryId {
    type Output = Self;

    fn sub(self, rhs: i64) -> EntryId {
        EntryId(self.0 - rhs)
    }
}

impl std::ops::Add<i64> for EntryId {
    type Output = Self;

    fn add(self, rhs: i64) -> EntryId {
        EntryId(self.0 + rhs)
    }
}

impl std::ops::SubAssign<i64> for EntryId {
    fn sub_assign(&mut self, rhs: i64) {
        self.0 -= rhs;
    }
}

impl std::ops::AddAssign<i64> for EntryId {
    fn add_assign(&mut self, rhs: i64) {
        self.0 += rhs;
    }
}

impl EntryId {
    /// Well-known invalid entry id.
    pub const INVALID: EntryId = EntryId(-1);
    /// First valid entry id.
    pub const MIN: EntryId = EntryId(0);

    /// Returns whether entry id is valid.
    pub const fn is_valid(&self) -> bool {
        self.0 >= 0
    }

    /// Constructs entry id from i64.
    ///
    /// # Safety
    /// Returned entry id is invalid if given i64 is negative.
    pub const unsafe fn unchecked_from_i64(i: i64) -> EntryId {
        EntryId(i)
    }
}

impl TryFrom<i64> for EntryId {
    type Error = BkError;

    fn try_from(i: i64) -> Result<EntryId, BkError> {
        if i < 0 {
            return Err(BkError::new(ErrorKind::InvalidEntryId));
        }
        Ok(EntryId(i))
    }
}

impl From<EntryId> for i64 {
    fn from(entry_id: EntryId) -> i64 {
        entry_id.0
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct LedgerEnsemble {
    pub(crate) first_entry_id: EntryId,
    pub(crate) bookies: Vec<BookieId>,
}

/// Ledger metadata.
#[derive(Clone, Debug)]
pub struct LedgerMetadata {
    pub ledger_id: LedgerId,
    pub length: LedgerLength,
    pub last_entry_id: EntryId,
    pub state: LedgerState,
    pub password: Vec<u8>,
    pub ensemble_size: u32,
    pub write_quorum_size: u32,
    pub ack_quorum_size: u32,
    pub ensembles: Vec<LedgerEnsemble>,
    pub digest_type: DigestType,
    pub creation_time: Option<SystemTime>,
    pub creator_token: i64,
    pub custom_metadata: HashMap<String, Vec<u8>>,
    pub format_version: i32,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub enum LedgerState {
    Open,
    InRecovery,
    Closed,
}

/// DigestType specifies digest method in ledger entry writting.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)]
#[non_exhaustive]
pub enum DigestType {
    CRC32,
    MAC,
    CRC32C,
    DUMMY,
}

pub struct EnsembleIterator<'a> {
    next: usize,
    ensembles: &'a [LedgerEnsemble],
}

impl<'a> Iterator for EnsembleIterator<'a> {
    type Item = (EntryId, &'a [BookieId], EntryId);

    fn next(&mut self) -> Option<Self::Item> {
        if self.next >= self.ensembles.len() {
            return None;
        }
        let ensemble = &self.ensembles[self.next];
        self.next += 1;
        let next_ensemble_entry_id =
            if self.next >= self.ensembles.len() { EntryId::INVALID } else { self.ensembles[self.next].first_entry_id };
        Some((ensemble.first_entry_id, &ensemble.bookies, next_ensemble_entry_id))
    }
}

impl LedgerMetadata {
    /// Returns ensemble for given entry id and next entry id that will have a different ensemble.
    pub fn ensemble_at(&self, entry_id: EntryId) -> (EntryId, &[BookieId], EntryId) {
        assert!(entry_id >= EntryId::MIN);
        assert!(!self.ensembles.is_empty());
        assert!(self.ensembles[0].first_entry_id == EntryId::MIN);
        let i = match self.ensembles.binary_search_by_key(&entry_id, |e| e.first_entry_id) {
            Ok(i) => i,
            Err(i) => i - 1,
        };
        if i + 1 == self.ensembles.len() {
            (self.ensembles[i].first_entry_id, &self.ensembles[i].bookies, EntryId::INVALID)
        } else {
            (self.ensembles[i].first_entry_id, &self.ensembles[i].bookies, self.ensembles[i + 1].first_entry_id)
        }
    }

    pub fn ensemble_iter(&self, entry_id: EntryId) -> EnsembleIterator<'_> {
        assert!(entry_id >= EntryId::MIN);
        assert!(!self.ensembles.is_empty());
        assert!(self.ensembles[0].first_entry_id == EntryId::MIN);
        let i = match self.ensembles.binary_search_by_key(&entry_id, |e| e.first_entry_id) {
            Ok(i) => i,
            Err(i) => i - 1,
        };
        EnsembleIterator { next: i, ensembles: &self.ensembles }
    }

    pub fn last_ensemble(&self) -> &LedgerEnsemble {
        &self.ensembles[self.ensembles.len() - 1]
    }

    pub fn closed(&self) -> bool {
        self.state == LedgerState::Closed
    }
}

pub(crate) trait HasLedgerMetadata {
    fn metadata(&self) -> &LedgerMetadata;

    fn ensemble_at(&self, entry_id: EntryId) -> (EntryId, &[BookieId], EntryId) {
        return self.metadata().ensemble_at(entry_id);
    }

    fn ensemble_iter(&self, entry_id: EntryId) -> EnsembleIterator<'_> {
        return self.metadata().ensemble_iter(entry_id);
    }

    fn last_ensemble(&self) -> &LedgerEnsemble {
        return self.metadata().last_ensemble();
    }

    fn closed(&self) -> bool {
        return self.metadata().closed();
    }
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct BookieId(compact_str::CompactString);

impl BookieId {
    pub fn new(s: &str) -> BookieId {
        BookieId(s.into())
    }

    pub fn as_str(&self) -> &str {
        self.0.as_str()
    }
}

impl Display for BookieId {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::result::Result<(), fmt::Error> {
        Display::fmt(&self.0.as_str(), f)
    }
}

impl std::ops::Deref for BookieId {
    type Target = str;

    fn deref(&self) -> &str {
        self.as_str()
    }
}

impl AsRef<str> for BookieId {
    fn as_ref(&self) -> &str {
        self.0.as_str()
    }
}

impl Borrow<str> for BookieId {
    fn borrow(&self) -> &str {
        self.0.as_str()
    }
}