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
use bytes::{Buf, BufMut};
use bytes::{Bytes, BytesMut};

// As mentioned here [1], depending on the context, there are two
// concepts of versionstamp.
//
// At the `fdb_c` client level, the "versionstamp" is 10 bytes,
// consisting of the transaction's commit version (8 bytes) and
// transaction batch order (2 bytes).
//
// In the context of the Tuple layer, the "versionstamp" is 12
// bytes. The user can manually add 2 additional bytes to provide
// application level ordering.
//
// `VERSIONSTAMP_TR_VERSION_LEN` below is the `fdb_c` client level
// versionstamp. When `VERSIONSTAMP_TR_VERSION` is all `\xFF`, it
// means that versionstamp is "incomplete".
//
// [1]: https://apple.github.io/foundationdb/data-modeling.html#versionstamps
const VERSIONSTAMP_TR_VERSION_LEN: usize = 10;
const VERSIONSTAMP_USER_VERSION_LEN: usize = 2;

/// Used to represent values written by versionstamp operations with a
/// [`Tuple`].
///
/// [`Versionstamp`] contains twelve bytes. The first ten bytes are
/// the "transaction" version, and they are usually assigned by the
/// database in such a way that all transactions receive a different
/// version that is consistent with a serialization order of the
/// transactions within the database (One can use the
/// [`get_versionstamp`] method to retrieve this version from a
/// [`Transaction`]). This also implies that the transaction version
/// of newly committed transactions will be monotonically increasing
/// over time. The final two bytes are the "user" version and should
/// be set by the client. This allows the user to use this type to
/// impose a total order of items across multiple transactions in the
/// database in a consistent and conflict-free way.
///
/// All [`Versionstamp`]s can exist in one of two states: "incomplete"
/// and "complete". An "incomplete" [`Versionstamp`] is a [`Versionstamp`]
/// that has not been initialized with a meaningful transaction
/// version. For example, this might be used with a [`Versionstamp`] that
/// one wants to fill in with the current transaction's version
/// information. A "complete" [`Versionstamp`], in contradistinction, is
/// one that *has* been assigned a meaningful transaction version. This
/// is usually the case if one is reading back a Versionstamp from the
/// database.
///
/// Example usage might be to do something like the following:
///
/// ```ignore
/// let tr_version = fdb_database
///     .run(|tr| async move {
///         let t = {
///             let mut tup = Tuple::new();
///             tup.add_string(String::from("prefix"));
///             tup.add_versionstamp(Versionstamp::incomplete(0));
///             tup
///         };
///
///         unsafe {
///             tr.mutate(
///                 MutationType::SetVersionstampedKey,
///                 t.pack_with_versionstamp(Bytes::new())?,
///                 Bytes::new(),
///             );
///         }
///
///         Ok(unsafe { tr.get_versionstamp() })
///     })
///     .await?
///     .get()
///     .await?;
///
/// let vs = fdb_database
///     .run(|tr| async move {
///         let subspace = Subspace::new(Bytes::new()).subspace(&{
///             let mut tup = Tuple::new();
///             tup.add_string("prefix".to_string());
///             tup
///         });
///
///         let subspace_range = subspace.range(&Tuple::new());
///
///         let key = subspace_range
///             .into_stream(&tr, RangeOptions::default())
///             .take(1)
///             .next()
///             .await
///             .unwrap()?
///             .into_key();
///
///         Ok(subspace
///             .unpack(&key.into())?
///             .get_versionstamp_ref(0)?
///             .clone())
///     })
///     .await?;
///
/// assert_eq!(vs, Versionstamp::complete(tr_version, 0));
/// ```
///
/// Here, an incomplete [`Versionstamp`] is packed and written to the
/// database with [`SetVersionstampedKey`] mutation type.
///
/// After committing, we then attempt to read back the same key that
/// we just wrote. Then we verify the invariant that the deserialized
/// [`Versionstamp`] is the same as a complete [`Versionstamp`] value
/// created from the first transaction's version information.
///
/// [`Tuple`]: crate::tuple::Tuple
/// [`get_versionstamp`]: crate::transaction::Transaction::get_versionstamp
/// [`Transaction`]: crate::transaction::Transaction
/// [`SetVersionstampedKey`]: crate::transaction::MutationType::SetVersionstampedKey
#[derive(Clone, Ord, Eq, PartialOrd, PartialEq, Debug)]
pub struct Versionstamp {
    complete: bool,
    tr_version: Bytes,
    user_version: u16,
}

impl Versionstamp {
    /// Creates a complete [`Versionstamp`] instance with the given
    /// transaction and user versions.
    ///
    /// # Panic
    ///
    /// Panics if the length of the transaction version is incorrect.
    pub fn complete(tr_version: Bytes, user_version: u16) -> Versionstamp {
        if tr_version.len() != VERSIONSTAMP_TR_VERSION_LEN {
            panic!("Global version has invalid length {}", tr_version.len());
        }

        let complete = true;

        Versionstamp {
            complete,
            tr_version,
            user_version,
        }
    }

    /// Creates a value of [`Versionstamp`] type based on the given
    /// byte array.
    ///
    /// # Panic
    ///
    /// Panics if the length of the byte array is incorrect.
    pub fn from_bytes(version_bytes: Bytes) -> Versionstamp {
        if version_bytes.len() != VERSIONSTAMP_TR_VERSION_LEN + VERSIONSTAMP_USER_VERSION_LEN {
            panic!(
                "Versionstamp bytes must have length {}",
                VERSIONSTAMP_TR_VERSION_LEN + VERSIONSTAMP_USER_VERSION_LEN
            );
        }

        // If we find any of the bytes to be not `0xFF`, then it means
        // that versionstamp is in complete state.
        let mut complete = false;
        version_bytes[0..VERSIONSTAMP_TR_VERSION_LEN]
            .iter()
            .for_each(|x| {
                if *x != 0xFF {
                    complete = true;
                }
            });

        let tr_version = version_bytes.slice(0..VERSIONSTAMP_TR_VERSION_LEN);
        let user_version = version_bytes.slice(VERSIONSTAMP_TR_VERSION_LEN..).get_u16();

        Versionstamp {
            complete,
            tr_version,
            user_version,
        }
    }

    /// Creates an incomplete [`Versionstamp`] instance with the given
    /// user version.
    pub fn incomplete(user_version: u16) -> Versionstamp {
        let complete = false;
        let tr_version = Bytes::from_static(&b"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"[..]);

        Versionstamp {
            complete,
            tr_version,
            user_version,
        }
    }

    /// Retrieve a byte representation of this [`Versionstamp`].
    pub fn get_bytes(&self) -> Bytes {
        let mut buf =
            BytesMut::with_capacity(VERSIONSTAMP_TR_VERSION_LEN + VERSIONSTAMP_USER_VERSION_LEN);
        buf.put(self.tr_version.clone());

        buf.put_u16(self.user_version);
        buf.into()
    }

    /// Retrieve the portion of this [`Versionstamp`] that is set by
    /// the database.
    pub fn get_transaction_version(&self) -> Bytes {
        self.tr_version.clone()
    }

    /// Retrieve the portion of this [`Versionstamp`] that is set by
    /// the user.
    pub fn get_user_version(&self) -> u16 {
        self.user_version
    }

    /// Whether this [`Versionstamp`]'s transaction version is
    /// meaningful.
    pub fn is_complete(&self) -> bool {
        self.complete
    }
}

#[cfg(test)]
mod tests {
    use bytes::Bytes;

    use super::Versionstamp;

    #[test]
    fn complete() {
        assert!(std::panic::catch_unwind(|| {
            Versionstamp::complete(Bytes::from_static(&b"invalid"[..]), 0)
        })
        .is_err());

        assert_eq!(
            Versionstamp::complete(
                Bytes::from_static(&b"\xAA\xBB\xCC\xDD\xEE\xFF\x00\x01\x02\x03"[..]),
                0
            ),
            Versionstamp {
                complete: true,
                tr_version: Bytes::from_static(&b"\xAA\xBB\xCC\xDD\xEE\xFF\x00\x01\x02\x03"[..]),
                user_version: 0
            }
        );
    }

    #[test]
    fn from_bytes() {
        assert!(std::panic::catch_unwind(|| {
            Versionstamp::from_bytes(Bytes::from_static(&b"invalid"[..]))
        })
        .is_err());

        assert_eq!(
            Versionstamp::from_bytes(Bytes::from_static(
                &b"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x02\x91"[..]
            )),
            Versionstamp::incomplete(657)
        );

        assert_eq!(
            Versionstamp::from_bytes(Bytes::from_static(
                &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x02\x91"[..]
            )),
            Versionstamp::complete(
                Bytes::from_static(&b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A"[..]),
                657
            )
        );
    }

    #[test]
    fn incomplete() {
        assert_eq!(
            Versionstamp::incomplete(657),
            Versionstamp {
                complete: false,
                tr_version: Bytes::from_static(&b"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"[..]),
                user_version: 657
            }
        );
    }

    #[test]
    fn get_bytes() {
        assert_eq!(
            (Versionstamp::complete(
                Bytes::from_static(&b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A"[..]),
                657
            ))
            .get_bytes(),
            Bytes::from_static(&b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x02\x91"[..])
        );
        assert_eq!(
            (Versionstamp::incomplete(657)).get_bytes(),
            Bytes::from_static(&b"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x02\x91"[..])
        );
    }

    #[test]
    fn get_transaction_version() {
        assert_eq!(
            (Versionstamp::complete(
                Bytes::from_static(&b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A"[..]),
                657
            ))
            .get_transaction_version(),
            Bytes::from_static(&b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A"[..])
        );

        assert_eq!(
            (Versionstamp::incomplete(657)).get_transaction_version(),
            Bytes::from_static(&b"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"[..])
        );
    }

    #[test]
    fn get_user_version() {
        assert_eq!(
            (Versionstamp::complete(
                Bytes::from_static(&b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A"[..]),
                657
            ))
            .get_user_version(),
            657
        );

        assert_eq!((Versionstamp::incomplete(657)).get_user_version(), 657);
    }

    #[test]
    fn is_complete() {
        assert!((Versionstamp::complete(
            Bytes::from_static(&b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A"[..]),
            657
        ))
        .is_complete());

        assert!(!(Versionstamp::incomplete(657)).is_complete());
    }
}