1
1
use libc:: size_t;
2
2
use positioned_io:: ReadAt ;
3
+ use rusqlite:: TransactionBehavior ;
3
4
4
5
use super :: * ;
5
6
use crate :: c_api:: PossumError :: NoError ;
6
- use crate :: Handle ;
7
7
8
- // This drops the Handle Box. Instead, if this is hard to use correctly from C, it could drop a
9
- // top-level reference count for the box. i.e. If this one goes, there's no way to work with the
10
- // Handle , and when all other outstanding operations on the Handle complete, it will drop the Handle
11
- // for real.
8
+ // This drops the PossumHandle Box. Instead, if this is hard to use correctly from C, it could drop
9
+ // a top-level reference count for the box. i.e. If this one goes, there's no way to work with the
10
+ // PossumHandle , and when all other outstanding operations on the PossumHandle complete, it will
11
+ // drop the PossumHandle for real.
12
12
#[ no_mangle]
13
- pub extern "C" fn possum_drop ( handle : * mut Handle ) {
13
+ pub extern "C" fn possum_drop ( handle : * mut PossumHandle ) {
14
14
drop ( unsafe { Box :: from_raw ( handle) } )
15
15
}
16
16
17
17
#[ no_mangle]
18
18
pub extern "C" fn possum_set_instance_limits (
19
- handle : * mut Handle ,
19
+ handle : * mut PossumHandle ,
20
20
limits : * const PossumLimits ,
21
21
) -> PossumError {
22
22
let handle = unsafe { & mut * handle } ;
23
23
let limits = unsafe { limits. read ( ) } ;
24
24
with_residual ( || {
25
25
handle
26
+ . write ( )
27
+ . unwrap ( )
26
28
. set_instance_limits ( limits. into ( ) )
27
29
. map_err ( Into :: into)
28
30
} )
29
31
}
30
32
31
33
#[ no_mangle]
32
- pub extern "C" fn possum_cleanup_snapshots ( handle : * const Handle ) -> PossumError {
33
- let handle = unsafe { & * handle } ;
34
- with_residual ( || handle. cleanup_snapshots ( ) )
34
+ pub extern "C" fn possum_cleanup_snapshots ( handle : * const PossumHandle ) -> PossumError {
35
+ let handle = unwrap_possum_handle ( handle) ;
36
+ with_residual ( || handle. read ( ) . unwrap ( ) . cleanup_snapshots ( ) )
35
37
}
36
38
37
39
#[ no_mangle]
38
40
pub extern "C" fn possum_single_write_buf (
39
- handle : * mut Handle ,
41
+ handle : * mut PossumHandle ,
40
42
key : PossumBuf ,
41
43
value : PossumBuf ,
42
44
) -> size_t {
43
45
let key_vec = key. as_ref ( ) . to_vec ( ) ;
44
46
let value_slice = value. as_ref ( ) ;
45
47
const ERR_SENTINEL : usize = usize:: MAX ;
46
48
let handle = unsafe { & * handle } ;
47
- match handle. single_write_from ( key_vec, value_slice) {
49
+ match handle
50
+ . read ( )
51
+ . unwrap ( )
52
+ . single_write_from ( key_vec, value_slice)
53
+ {
48
54
Err ( _) => ERR_SENTINEL ,
49
55
Ok ( ( n, _) ) => {
50
56
let n = n. try_into ( ) . unwrap ( ) ;
@@ -55,18 +61,21 @@ pub extern "C" fn possum_single_write_buf(
55
61
}
56
62
57
63
#[ no_mangle]
58
- pub extern "C" fn possum_new_writer ( handle : * mut Handle ) -> * mut PossumWriter {
59
- let handle = unsafe { handle. as_ref ( ) } . unwrap ( ) ;
60
- Box :: into_raw ( Box :: new ( handle. new_writer ( ) . unwrap ( ) ) )
64
+ pub extern "C" fn possum_new_writer ( handle : * mut PossumHandle ) -> * mut PossumWriter {
65
+ let handle = unwrap_possum_handle ( handle) ;
66
+ let writer = BatchWriter :: new ( handle. clone ( ) ) ;
67
+ Box :: into_raw ( Box :: new ( writer) )
61
68
}
62
69
63
70
#[ no_mangle]
64
71
pub extern "C" fn possum_single_stat (
65
- handle : * const Handle ,
72
+ handle : * const PossumHandle ,
66
73
key : PossumBuf ,
67
74
out_stat : * mut PossumStat ,
68
75
) -> bool {
69
76
match unsafe { handle. as_ref ( ) }
77
+ . unwrap ( )
78
+ . read ( )
70
79
. unwrap ( )
71
80
. read_single ( key. as_ref ( ) )
72
81
. unwrap ( )
@@ -82,12 +91,14 @@ pub extern "C" fn possum_single_stat(
82
91
83
92
#[ no_mangle]
84
93
pub extern "C" fn possum_list_items (
85
- handle : * const Handle ,
94
+ handle : * const PossumHandle ,
86
95
prefix : PossumBuf ,
87
96
out_list : * mut * mut PossumItem ,
88
97
out_list_len : * mut size_t ,
89
98
) -> PossumError {
90
99
let items = match unsafe { handle. as_ref ( ) }
100
+ . unwrap ( )
101
+ . read ( )
91
102
. unwrap ( )
92
103
. list_items ( prefix. as_ref ( ) )
93
104
{
@@ -100,13 +111,18 @@ pub extern "C" fn possum_list_items(
100
111
101
112
#[ no_mangle]
102
113
pub extern "C" fn possum_single_read_at (
103
- handle : * const Handle ,
114
+ handle : * const PossumHandle ,
104
115
key : PossumBuf ,
105
116
buf : * mut PossumBuf ,
106
117
offset : u64 ,
107
118
) -> PossumError {
108
119
let rust_key = key. as_ref ( ) ;
109
- let value = match unsafe { handle. as_ref ( ) } . unwrap ( ) . read_single ( rust_key) {
120
+ let value = match unsafe { handle. as_ref ( ) }
121
+ . unwrap ( )
122
+ . read ( )
123
+ . unwrap ( )
124
+ . read_single ( rust_key)
125
+ {
110
126
Ok ( Some ( value) ) => value,
111
127
Ok ( None ) => return PossumError :: NoSuchKey ,
112
128
Err ( err) => return err. into ( ) ,
@@ -124,13 +140,13 @@ pub extern "C" fn possum_single_read_at(
124
140
/// stat is filled if non-null and a delete occurs. NoSuchKey is returned if the key does not exist.
125
141
#[ no_mangle]
126
142
pub extern "C" fn possum_single_delete (
127
- handle : * const Handle ,
143
+ handle : * const PossumHandle ,
128
144
key : PossumBuf ,
129
145
stat : * mut PossumStat ,
130
146
) -> PossumError {
131
147
with_residual ( || {
132
148
let handle = unsafe { & * handle } ;
133
- let value = match handle. single_delete ( key. as_ref ( ) ) {
149
+ let value = match handle. read ( ) . unwrap ( ) . single_delete ( key. as_ref ( ) ) {
134
150
Ok ( None ) => return Err ( crate :: Error :: NoSuchKey ) ,
135
151
Err ( err) => return Err ( err) ,
136
152
Ok ( Some ( value) ) => value,
@@ -144,15 +160,27 @@ pub extern "C" fn possum_single_delete(
144
160
145
161
#[ no_mangle]
146
162
pub extern "C" fn possum_reader_new (
147
- handle : * const Handle ,
163
+ handle : * const PossumHandle ,
148
164
reader : * mut * mut PossumReader ,
149
165
) -> PossumError {
150
- let handle = unsafe { handle . as_ref ( ) } . unwrap ( ) ;
166
+ let handle = unwrap_possum_handle ( handle ) . clone ( ) ;
151
167
let reader = unsafe { reader. as_mut ( ) } . unwrap ( ) ;
152
- let rust_reader = match handle. read ( ) {
168
+ let owned_tx_res = handle. start_transaction (
169
+ // This is copied from Handle::start_writable_transaction_with_behaviour and Handle::read
170
+ // until I make proper abstractions.
171
+ |conn, handle| {
172
+ let rtx = conn. transaction_with_behavior ( TransactionBehavior :: Deferred ) ?;
173
+ Ok ( Transaction :: new ( rtx, handle) )
174
+ } ,
175
+ ) ;
176
+ let owned_tx = match owned_tx_res {
153
177
Ok ( ok) => ok,
154
178
Err ( err) => return err. into ( ) ,
155
179
} ;
180
+ let rust_reader = Reader {
181
+ owned_tx,
182
+ reads : Default :: default ( ) ,
183
+ } ;
156
184
* reader = Box :: into_raw ( Box :: new ( PossumReader {
157
185
rust_reader : Some ( rust_reader) ,
158
186
values : Default :: default ( ) ,
@@ -162,23 +190,31 @@ pub extern "C" fn possum_reader_new(
162
190
163
191
#[ no_mangle]
164
192
pub extern "C" fn possum_handle_move_prefix (
165
- handle : * mut Handle ,
193
+ handle : * mut PossumHandle ,
166
194
from : PossumBuf ,
167
195
to : PossumBuf ,
168
196
) -> PossumError {
169
197
let handle = unsafe { & mut * handle } ;
170
198
with_residual ( || {
171
199
handle
200
+ . read ( )
201
+ . unwrap ( )
172
202
. move_prefix ( from. as_ref ( ) , to. as_ref ( ) )
173
203
. map_err ( Into :: into)
174
204
} )
175
205
}
176
206
177
207
#[ no_mangle]
178
208
pub extern "C" fn possum_handle_delete_prefix (
179
- handle : * mut Handle ,
209
+ handle : * mut PossumHandle ,
180
210
prefix : PossumBuf ,
181
211
) -> PossumError {
182
212
let handle = unsafe { & mut * handle } ;
183
- with_residual ( || handle. delete_prefix ( prefix. as_ref ( ) ) . map_err ( Into :: into) )
213
+ with_residual ( || {
214
+ handle
215
+ . read ( )
216
+ . unwrap ( )
217
+ . delete_prefix ( prefix. as_ref ( ) )
218
+ . map_err ( Into :: into)
219
+ } )
184
220
}
0 commit comments