forked from lance-format/lance
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommit.rs
More file actions
136 lines (122 loc) · 4.13 KB
/
Copy pathcommit.rs
File metadata and controls
136 lines (122 loc) · 4.13 KB
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
// Copyright 2023 Lance Developers.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::fmt::Debug;
use std::sync::LazyLock;
use lance_table::io::commit::{CommitError, CommitLease, CommitLock};
use snafu::location;
use lance_core::Error;
use pyo3::{exceptions::PyIOError, prelude::*};
static PY_CONFLICT_ERROR: LazyLock<PyResult<Py<PyAny>>> = LazyLock::new(|| {
Python::attach(|py| {
py.import("lance")
.and_then(|lance| lance.getattr("commit"))
.and_then(|commit| commit.getattr("CommitConflictError"))
.map(|err| err.unbind())
})
});
fn handle_error(py_err: PyErr, py: Python) -> CommitError {
let conflict_err_type = match &*PY_CONFLICT_ERROR {
Ok(err) => err.bind(py).get_type(),
Err(import_error) => {
return CommitError::OtherError(Error::Internal {
message: format!("Error importing from pylance {}", import_error),
location: location!(),
})
}
};
if py_err.is_instance(py, &conflict_err_type) {
CommitError::CommitConflict
} else {
CommitError::OtherError(Error::Internal {
message: format!("Error from commit handler: {}", py_err),
location: location!(),
})
}
}
pub struct PyCommitLock {
inner: Py<PyAny>,
}
impl PyCommitLock {
pub fn new(inner: Py<PyAny>) -> Self {
Self { inner }
}
}
impl Debug for PyCommitLock {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let repr = Python::attach(|py| {
self.inner
.call_method0(py, "__repr__")?
.extract::<String>(py)
})
.ok();
f.debug_struct("PyCommitLock")
.field("inner", &repr)
.finish()
}
}
#[async_trait::async_trait]
impl CommitLock for PyCommitLock {
type Lease = PyCommitLease;
async fn lock(&self, version: u64) -> Result<Self::Lease, CommitError> {
let lease = Python::attach(|py| -> Result<_, CommitError> {
let lease = self
.inner
.call1(py, (version,))
.map_err(|err| handle_error(err, py))?;
lease
.call_method0(py, "__enter__")
.map_err(|err| handle_error(err, py))?;
Ok(lease)
})?;
Ok(PyCommitLease { inner: lease })
}
}
pub struct PyCommitLease {
inner: Py<PyAny>,
}
#[async_trait::async_trait]
impl CommitLease for PyCommitLease {
async fn release(&self, success: bool) -> Result<(), CommitError> {
Python::attach(|py| {
if success {
self.inner
.call_method1(py, "__exit__", (py.None(), py.None(), py.None()))
.map_err(|err| handle_error(err, py))
} else {
// If the commit failed, we pass up an exception to the
// context manager.
PyIOError::new_err("commit failed").restore(py);
let args = py
.import("sys")
.unwrap()
.getattr("exc_info")
.unwrap()
.call0()
.unwrap();
self.inner
.call_method1(
py,
"__exit__",
(
args.get_item(0).unwrap(),
args.get_item(1).unwrap(),
args.get_item(2).unwrap(),
),
)
.map_err(|err| handle_error(err, py))
}
})?;
Ok(())
}
}