Skip to main content

lance_table/transaction/
builder.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright The Lance Authors
3
4//! The transaction itself: an operation plus the version it was based on.
5
6use crate::transaction::Operation;
7use lance_core::deepsize::DeepSizeOf;
8use std::collections::HashMap;
9use std::sync::Arc;
10use uuid::Uuid;
11
12/// A change to a dataset that can be retried
13///
14/// This contains enough information to be able to build the next manifest,
15/// given the current manifest.
16#[derive(Debug, Clone, DeepSizeOf, PartialEq)]
17pub struct Transaction {
18    /// The version of the table this transaction is based off of. If this is
19    /// the first transaction, this should be 0.
20    pub read_version: u64,
21    pub uuid: String,
22    pub operation: Operation,
23    pub tag: Option<String>,
24    pub transaction_properties: Option<Arc<HashMap<String, String>>>,
25}
26
27/// Add TransactionBuilder for flexibly setting option without using `mut`
28pub struct TransactionBuilder {
29    read_version: u64,
30    // uuid is optional for builder since it can autogenerate
31    uuid: Option<String>,
32    operation: Operation,
33    tag: Option<String>,
34    transaction_properties: Option<Arc<HashMap<String, String>>>,
35}
36
37impl TransactionBuilder {
38    pub fn new(read_version: u64, operation: Operation) -> Self {
39        Self {
40            read_version,
41            uuid: None,
42            operation,
43            tag: None,
44            transaction_properties: None,
45        }
46    }
47
48    pub fn uuid(mut self, uuid: String) -> Self {
49        self.uuid = Some(uuid);
50        self
51    }
52
53    pub fn tag(mut self, tag: Option<String>) -> Self {
54        self.tag = tag;
55        self
56    }
57
58    pub fn transaction_properties(
59        mut self,
60        transaction_properties: Option<Arc<HashMap<String, String>>>,
61    ) -> Self {
62        self.transaction_properties = transaction_properties;
63        self
64    }
65
66    pub fn build(self) -> Transaction {
67        let uuid = self
68            .uuid
69            .unwrap_or_else(|| Uuid::new_v4().hyphenated().to_string());
70        Transaction {
71            read_version: self.read_version,
72            uuid,
73            operation: self.operation,
74            tag: self.tag,
75            transaction_properties: self.transaction_properties,
76        }
77    }
78}
79
80impl Transaction {
81    pub fn new_from_version(read_version: u64, operation: Operation) -> Self {
82        TransactionBuilder::new(read_version, operation).build()
83    }
84
85    pub fn new(read_version: u64, operation: Operation, tag: Option<String>) -> Self {
86        TransactionBuilder::new(read_version, operation)
87            .tag(tag)
88            .build()
89    }
90}