1
+ use crate :: RocksDBException ;
1
2
use ext_php_rs:: prelude:: * ;
2
3
use rust_rocksdb:: {
3
4
Options , Transaction , TransactionDB , TransactionDBOptions , TransactionOptions , WriteOptions ,
4
5
} ;
5
6
use std:: sync:: { Arc , Mutex } ;
6
- use crate :: RocksDBException ;
7
7
8
8
#[ php_class]
9
9
pub struct RocksDBTransaction {
@@ -31,8 +31,9 @@ impl RocksDBTransaction {
31
31
opts. set_max_open_files ( 1000 ) ;
32
32
opts. set_log_level ( rust_rocksdb:: LogLevel :: Warn ) ;
33
33
34
- let transaction_db = TransactionDB :: open ( & opts, & txn_db_opts, & path)
35
- . map_err ( |e| ext_php_rs:: exception:: PhpException :: from_class :: < RocksDBException > ( e. to_string ( ) ) ) ?;
34
+ let transaction_db = TransactionDB :: open ( & opts, & txn_db_opts, & path) . map_err ( |e| {
35
+ ext_php_rs:: exception:: PhpException :: from_class :: < RocksDBException > ( e. to_string ( ) )
36
+ } ) ?;
36
37
37
38
let transaction_db = Arc :: new ( transaction_db) ;
38
39
let transaction = create_transaction ( & transaction_db) ;
@@ -58,8 +59,9 @@ impl RocksDBTransaction {
58
59
pub fn commit ( & self ) -> PhpResult < ( ) > {
59
60
let mut txn_guard = self . transaction . lock ( ) . unwrap ( ) ;
60
61
if let Some ( txn) = txn_guard. take ( ) {
61
- txn. commit ( )
62
- . map_err ( |e| ext_php_rs:: exception:: PhpException :: from_class :: < RocksDBException > ( e. to_string ( ) ) ) ?;
62
+ txn. commit ( ) . map_err ( |e| {
63
+ ext_php_rs:: exception:: PhpException :: from_class :: < RocksDBException > ( e. to_string ( ) )
64
+ } ) ?;
63
65
}
64
66
* txn_guard = Some ( create_transaction ( & self . transaction_db ) ) ;
65
67
Ok ( ( ) )
@@ -68,8 +70,9 @@ impl RocksDBTransaction {
68
70
pub fn rollback ( & self ) -> PhpResult < ( ) > {
69
71
let mut txn_guard = self . transaction . lock ( ) . unwrap ( ) ;
70
72
if let Some ( txn) = txn_guard. take ( ) {
71
- txn. rollback ( )
72
- . map_err ( |e| ext_php_rs:: exception:: PhpException :: from_class :: < RocksDBException > ( e. to_string ( ) ) ) ?;
73
+ txn. rollback ( ) . map_err ( |e| {
74
+ ext_php_rs:: exception:: PhpException :: from_class :: < RocksDBException > ( e. to_string ( ) )
75
+ } ) ?;
73
76
}
74
77
* txn_guard = Some ( create_transaction ( & self . transaction_db ) ) ;
75
78
Ok ( ( ) )
@@ -86,8 +89,9 @@ impl RocksDBTransaction {
86
89
pub fn rollback_to_savepoint ( & self ) -> PhpResult < ( ) > {
87
90
let txn_guard = self . transaction . lock ( ) . unwrap ( ) ;
88
91
if let Some ( ref txn) = * txn_guard {
89
- txn. rollback_to_savepoint ( )
90
- . map_err ( |e| ext_php_rs:: exception:: PhpException :: from_class :: < RocksDBException > ( e. to_string ( ) ) ) ?;
92
+ txn. rollback_to_savepoint ( ) . map_err ( |e| {
93
+ ext_php_rs:: exception:: PhpException :: from_class :: < RocksDBException > ( e. to_string ( ) )
94
+ } ) ?;
91
95
}
92
96
Ok ( ( ) )
93
97
}
@@ -102,16 +106,22 @@ impl RocksDBTransaction {
102
106
. cf_handle ( & cf_name)
103
107
. ok_or ( "Column family not found" ) ?;
104
108
txn. put_cf ( & cf, key. as_bytes ( ) , value. as_bytes ( ) )
105
- . map_err ( |e| ext_php_rs:: exception:: PhpException :: from_class :: < RocksDBException > ( e. to_string ( ) ) )
109
+ . map_err ( |e| {
110
+ ext_php_rs:: exception:: PhpException :: from_class :: < RocksDBException > (
111
+ e. to_string ( ) ,
112
+ )
113
+ } )
106
114
}
107
- None => txn
108
- . put ( key. as_bytes ( ) , value. as_bytes ( ) )
109
- . map_err ( |e| ext_php_rs:: exception:: PhpException :: from_class :: < RocksDBException > ( e. to_string ( ) ) ) ,
115
+ None => txn. put ( key. as_bytes ( ) , value. as_bytes ( ) ) . map_err ( |e| {
116
+ ext_php_rs:: exception:: PhpException :: from_class :: < RocksDBException > (
117
+ e. to_string ( ) ,
118
+ )
119
+ } ) ,
110
120
}
111
121
} else {
112
- Err ( ext_php_rs:: exception:: PhpException :: from_class :: < RocksDBException > (
113
- "No active transaction" . to_string ( ) ,
114
- ) )
122
+ Err ( ext_php_rs:: exception:: PhpException :: from_class :: <
123
+ RocksDBException ,
124
+ > ( "No active transaction" . to_string ( ) ) )
115
125
}
116
126
}
117
127
@@ -126,26 +136,32 @@ impl RocksDBTransaction {
126
136
. ok_or ( "Column family not found" ) ?;
127
137
match txn. get_cf ( & cf, key. as_bytes ( ) ) {
128
138
Ok ( Some ( value) ) => Ok ( Some ( String :: from_utf8 ( value) . map_err ( |e| {
129
- ext_php_rs:: exception:: PhpException :: from_class :: < RocksDBException > ( e. to_string ( ) )
130
- } ) ?) ) ,
131
- Ok ( None ) => Ok ( None ) ,
132
- Err ( e) => Err ( ext_php_rs:: exception:: PhpException :: from_class :: < RocksDBException > ( e. to_string ( ) ) ) ,
133
- }
134
- }
135
- None => {
136
- match txn. get ( key. as_bytes ( ) ) {
137
- Ok ( Some ( value) ) => Ok ( Some ( String :: from_utf8 ( value) . map_err ( |e| {
138
- ext_php_rs:: exception:: PhpException :: from_class :: < RocksDBException > ( e. to_string ( ) )
139
+ ext_php_rs:: exception:: PhpException :: from_class :: < RocksDBException > (
140
+ e. to_string ( ) ,
141
+ )
139
142
} ) ?) ) ,
140
143
Ok ( None ) => Ok ( None ) ,
141
- Err ( e) => Err ( ext_php_rs:: exception:: PhpException :: from_class :: < RocksDBException > ( e. to_string ( ) ) ) ,
144
+ Err ( e) => Err ( ext_php_rs:: exception:: PhpException :: from_class :: <
145
+ RocksDBException ,
146
+ > ( e. to_string ( ) ) ) ,
142
147
}
143
148
}
149
+ None => match txn. get ( key. as_bytes ( ) ) {
150
+ Ok ( Some ( value) ) => Ok ( Some ( String :: from_utf8 ( value) . map_err ( |e| {
151
+ ext_php_rs:: exception:: PhpException :: from_class :: < RocksDBException > (
152
+ e. to_string ( ) ,
153
+ )
154
+ } ) ?) ) ,
155
+ Ok ( None ) => Ok ( None ) ,
156
+ Err ( e) => Err ( ext_php_rs:: exception:: PhpException :: from_class :: <
157
+ RocksDBException ,
158
+ > ( e. to_string ( ) ) ) ,
159
+ } ,
144
160
}
145
161
} else {
146
- Err ( ext_php_rs:: exception:: PhpException :: from_class :: < RocksDBException > (
147
- "No active transaction" . to_string ( ) ,
148
- ) )
162
+ Err ( ext_php_rs:: exception:: PhpException :: from_class :: <
163
+ RocksDBException ,
164
+ > ( "No active transaction" . to_string ( ) ) )
149
165
}
150
166
}
151
167
@@ -158,17 +174,22 @@ impl RocksDBTransaction {
158
174
. transaction_db
159
175
. cf_handle ( & cf_name)
160
176
. ok_or ( "Column family not found" ) ?;
161
- txn. delete_cf ( & cf, key. as_bytes ( ) )
162
- . map_err ( |e| ext_php_rs:: exception:: PhpException :: from_class :: < RocksDBException > ( e. to_string ( ) ) )
177
+ txn. delete_cf ( & cf, key. as_bytes ( ) ) . map_err ( |e| {
178
+ ext_php_rs:: exception:: PhpException :: from_class :: < RocksDBException > (
179
+ e. to_string ( ) ,
180
+ )
181
+ } )
163
182
}
164
- None => txn
165
- . delete ( key. as_bytes ( ) )
166
- . map_err ( |e| ext_php_rs:: exception:: PhpException :: from_class :: < RocksDBException > ( e. to_string ( ) ) ) ,
183
+ None => txn. delete ( key. as_bytes ( ) ) . map_err ( |e| {
184
+ ext_php_rs:: exception:: PhpException :: from_class :: < RocksDBException > (
185
+ e. to_string ( ) ,
186
+ )
187
+ } ) ,
167
188
}
168
189
} else {
169
- Err ( ext_php_rs:: exception:: PhpException :: from_class :: < RocksDBException > (
170
- "No active transaction" . to_string ( ) ,
171
- ) )
190
+ Err ( ext_php_rs:: exception:: PhpException :: from_class :: <
191
+ RocksDBException ,
192
+ > ( "No active transaction" . to_string ( ) ) )
172
193
}
173
194
}
174
195
@@ -182,16 +203,22 @@ impl RocksDBTransaction {
182
203
. cf_handle ( & cf_name)
183
204
. ok_or ( "Column family not found" ) ?;
184
205
txn. merge_cf ( & cf, key. as_bytes ( ) , value. as_bytes ( ) )
185
- . map_err ( |e| ext_php_rs:: exception:: PhpException :: from_class :: < RocksDBException > ( e. to_string ( ) ) )
206
+ . map_err ( |e| {
207
+ ext_php_rs:: exception:: PhpException :: from_class :: < RocksDBException > (
208
+ e. to_string ( ) ,
209
+ )
210
+ } )
186
211
}
187
- None => txn
188
- . merge ( key. as_bytes ( ) , value. as_bytes ( ) )
189
- . map_err ( |e| ext_php_rs:: exception:: PhpException :: from_class :: < RocksDBException > ( e. to_string ( ) ) ) ,
212
+ None => txn. merge ( key. as_bytes ( ) , value. as_bytes ( ) ) . map_err ( |e| {
213
+ ext_php_rs:: exception:: PhpException :: from_class :: < RocksDBException > (
214
+ e. to_string ( ) ,
215
+ )
216
+ } ) ,
190
217
}
191
218
} else {
192
- Err ( ext_php_rs:: exception:: PhpException :: from_class :: < RocksDBException > (
193
- "No active transaction" . to_string ( ) ,
194
- ) )
219
+ Err ( ext_php_rs:: exception:: PhpException :: from_class :: <
220
+ RocksDBException ,
221
+ > ( "No active transaction" . to_string ( ) ) )
195
222
}
196
223
}
197
224
}
0 commit comments