Skip to content

Commit 78b480a

Browse files
committedJun 8, 2024
add lock
1 parent b8b2ef5 commit 78b480a

File tree

5 files changed

+91
-56
lines changed

5 files changed

+91
-56
lines changed
 

‎.github/workflows/build.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ jobs:
6464
rustup default stable
6565
rustup target add x86_64-unknown-linux-gnu
6666
67+
- name: Install Dependencies
68+
run: |
69+
sudo apt-get update
70+
sudo apt-get install -y libc6-dev
71+
6772
- name: Run Tests
6873
run: |
6974
cargo test --target x86_64-unknown-linux-gnu

‎Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ FROM php:${FROM_PHP}-fpm-${FROM_DISTRO}
99

1010
ENV CARGO_NET_GIT_FETCH_WITH_CLI=true
1111

12-
RUN apt-get update && apt install curl build-essential gcc libclang-dev make openssl libssl-dev git -y
12+
RUN apt-get update && apt install curl build-essential gcc libclang-dev make openssl libssl-dev git libc6-dev -y
1313

1414
RUN curl https://sh.rustup.rs -sSf | bash -s -- -y
1515

‎src/lib.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#![cfg_attr(all(windows, target_arch = "x86_64"), feature(abi_vectorcall))]
22
#![no_main]
33

4-
54
mod backup;
65
mod transaction;
76
mod write_batch;
@@ -11,18 +10,18 @@ use ext_php_rs::error::Error;
1110
use ext_php_rs::prelude::*;
1211
use ext_php_rs::types::{ZendHashTable, Zval};
1312
use ext_php_rs::zend::{ce, ModuleEntry};
13+
use ext_php_rs::{info_table_end, info_table_row, info_table_start};
14+
use fs2::FileExt;
1415
use json_patch::Patch;
1516
use rust_rocksdb::{
1617
ColumnFamilyDescriptor, DBWithThreadMode, MergeOperands, Options, SingleThreaded, DB,
1718
};
1819
use serde_json::{from_value, Value};
1920
use std::collections::HashMap;
20-
use std::time::Duration;
21-
use ext_php_rs::{info_table_end, info_table_row, info_table_start};
2221
use std::fs::File;
2322
use std::path::Path;
2423
use std::thread;
25-
use fs2::FileExt;
24+
use std::time::Duration;
2625

2726
use crate::backup::RocksDBBackup;
2827
use crate::transaction::RocksDBTransaction;
@@ -90,7 +89,8 @@ fn acquire_lock(lock_file: &str) -> Result<File, PhpException> {
9089
}
9190

9291
fn release_lock(file: File) -> PhpResult<()> {
93-
file.unlock().map_err(|e| PhpException::from(e.to_string()))?;
92+
file.unlock()
93+
.map_err(|e| PhpException::from(e.to_string()))?;
9494
Ok(())
9595
}
9696

@@ -142,23 +142,26 @@ impl RocksDB {
142142
Ok(db) => Ok(RocksDB {
143143
db,
144144
lock_handle: Some(lock_handle),
145-
position: None
145+
position: None,
146146
}),
147147
Err(e) => {
148148
let _ = release_lock(lock_handle);
149149
Err(e.to_string().into())
150-
},
150+
}
151151
}
152152
}
153153

154154
#[destructor]
155155
pub fn __destruct(&self) {
156156
if let Some(lock_handle) = &self.lock_handle {
157-
let _ = release_lock(lock_handle.try_clone().expect("Failed to clone file handle"));
157+
let _ = release_lock(
158+
lock_handle
159+
.try_clone()
160+
.expect("Failed to clone file handle"),
161+
);
158162
}
159163
}
160164

161-
162165
pub fn put(&self, key: String, value: String, cf_name: Option<String>) -> PhpResult<()> {
163166
match cf_name {
164167
Some(cf_name) => {

‎src/transaction.rs

Lines changed: 72 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
use crate::RocksDBException;
12
use ext_php_rs::prelude::*;
23
use rust_rocksdb::{
34
Options, Transaction, TransactionDB, TransactionDBOptions, TransactionOptions, WriteOptions,
45
};
56
use std::sync::{Arc, Mutex};
6-
use crate::RocksDBException;
77

88
#[php_class]
99
pub struct RocksDBTransaction {
@@ -31,8 +31,9 @@ impl RocksDBTransaction {
3131
opts.set_max_open_files(1000);
3232
opts.set_log_level(rust_rocksdb::LogLevel::Warn);
3333

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+
})?;
3637

3738
let transaction_db = Arc::new(transaction_db);
3839
let transaction = create_transaction(&transaction_db);
@@ -58,8 +59,9 @@ impl RocksDBTransaction {
5859
pub fn commit(&self) -> PhpResult<()> {
5960
let mut txn_guard = self.transaction.lock().unwrap();
6061
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+
})?;
6365
}
6466
*txn_guard = Some(create_transaction(&self.transaction_db));
6567
Ok(())
@@ -68,8 +70,9 @@ impl RocksDBTransaction {
6870
pub fn rollback(&self) -> PhpResult<()> {
6971
let mut txn_guard = self.transaction.lock().unwrap();
7072
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+
})?;
7376
}
7477
*txn_guard = Some(create_transaction(&self.transaction_db));
7578
Ok(())
@@ -86,8 +89,9 @@ impl RocksDBTransaction {
8689
pub fn rollback_to_savepoint(&self) -> PhpResult<()> {
8790
let txn_guard = self.transaction.lock().unwrap();
8891
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+
})?;
9195
}
9296
Ok(())
9397
}
@@ -102,16 +106,22 @@ impl RocksDBTransaction {
102106
.cf_handle(&cf_name)
103107
.ok_or("Column family not found")?;
104108
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+
})
106114
}
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+
}),
110120
}
111121
} 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()))
115125
}
116126
}
117127

@@ -126,26 +136,32 @@ impl RocksDBTransaction {
126136
.ok_or("Column family not found")?;
127137
match txn.get_cf(&cf, key.as_bytes()) {
128138
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+
)
139142
})?)),
140143
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())),
142147
}
143148
}
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+
},
144160
}
145161
} 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()))
149165
}
150166
}
151167

@@ -158,17 +174,22 @@ impl RocksDBTransaction {
158174
.transaction_db
159175
.cf_handle(&cf_name)
160176
.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+
})
163182
}
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+
}),
167188
}
168189
} 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()))
172193
}
173194
}
174195

@@ -182,16 +203,22 @@ impl RocksDBTransaction {
182203
.cf_handle(&cf_name)
183204
.ok_or("Column family not found")?;
184205
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+
})
186211
}
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+
}),
190217
}
191218
} 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()))
195222
}
196223
}
197224
}

‎src/write_batch.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
use crate::RocksDBException;
12
use ext_php_rs::prelude::*;
23
use rust_rocksdb::{Options, WriteBatchWithTransaction, DB};
34
use std::sync::{Arc, Mutex};
45
use std::time::Duration;
5-
use crate::RocksDBException;
66

77
#[php_class]
88
pub struct RocksDBWriteBatch {

0 commit comments

Comments
 (0)