Skip to content

Commit 123d777

Browse files
committed
feature: initial migration to tokio based network stack
1 parent 58c8c54 commit 123d777

File tree

14 files changed

+568
-46
lines changed

14 files changed

+568
-46
lines changed

Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
[workspace]
2-
members = ["couchbase-lite-core-sys", "couchbase-lite", "chat-demo"]
2+
members = ["couchbase-lite-core-sys", "couchbase-lite", "chat-demo"]
3+
4+
[patch.'crates-io']
5+
http = { git = "https://github.com/Dushistov/http", rev = "3ad3b825edc3a0d9048a6af5ab524f2fc51bde30" }
6+
tungstenite = { git = "https://github.com/Dushistov/tungstenite-rs", rev = "de80b9bced480d407665e46001fc2d7a6874d71e" }

chat-demo/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ edition = "2018"
77
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
88

99
[dependencies]
10-
couchbase-lite = { path = "../couchbase-lite" }
10+
couchbase-lite = { path = "../couchbase-lite", features = ["replication"] }
1111
serde = { version = "1.0.104", features = ["derive"] }
1212
tokio = { version = "0.2.6", features = ["io-std", "io-util"] }
1313
log = "0.4.8"

chat-demo/src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use couchbase_lite::{
2-
fallible_streaming_iterator::FallibleStreamingIterator, use_c4_civet_web_socket_factory,
3-
Database, DatabaseConfig, Document, ReplicatorState,
2+
fallible_streaming_iterator::FallibleStreamingIterator, use_web_sockets, Database,
3+
DatabaseConfig, Document, ReplicatorState,
44
};
55
use log::{error, trace};
66
use serde::{Deserialize, Serialize};
@@ -24,7 +24,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
2424
.unwrap_or_else(|| "ws://192.168.1.132:4984/demo/".to_string());
2525
let token: Option<String> = env::args().nth(3);
2626

27-
use_c4_civet_web_socket_factory();
27+
use_web_sockets(runtime.handle().clone());
2828
let (db_thread, db_exec) = run_db_thread(db_path);
2929
let db_exec_repl = db_exec.clone();
3030
db_exec.spawn(move |db| {

couchbase-lite-core-sys/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use std::{
99
fn main() {
1010
let dst = cmake::Config::new(Path::new("couchbase-lite-core"))
1111
.define("DISABLE_LTO_BUILD", "True")
12+
.define("SANITIZE_FOR_DEBUG_ENABLED", "False")
1213
.build_target("LiteCore")
1314
.build()
1415
.join("build");
@@ -100,7 +101,6 @@ fn main() {
100101
"fleece/FLSlice.h",
101102
"c4Document+Fleece.h",
102103
"fleece/Fleece.h",
103-
"Replicator/CivetWebSocket.hh",
104104
"couch_lite_log_retrans.hpp",
105105
],
106106
&out_dir.join("c4_header.rs"),

couchbase-lite-core-sys/src/lib.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
11
mod c4_header;
22

33
pub use c4_header::*;
4+
5+
use std::os::raw::c_void;
6+
7+
//bindgen can not handle inline functions so
8+
9+
#[inline]
10+
pub unsafe fn c4db_release(db: *mut C4Database) {
11+
c4base_release(db as *mut c_void)
12+
}
13+
14+
#[inline]
15+
#[allow(non_snake_case)]
16+
pub unsafe fn FLSliceResult_Release(s: FLSliceResult) {
17+
_FLBuf_Release(s.buf);
18+
}
19+
20+
#[inline]
21+
pub unsafe fn c4query_release(r: *mut C4Query) {
22+
c4base_release(r as *mut c_void)
23+
}

couchbase-lite/Cargo.toml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ version = "0.2.0"
44
authors = ["Evgeniy A. Dushistov <dushistov@mail.ru>"]
55
edition = "2018"
66

7-
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
[features]
8+
default = ["replication", "tls"]
9+
replication = ["tokio-tungstenite", "http", "tokio", "futures-util", "tokio-tls"]
10+
tls = ["tokio-tungstenite/tls"]
811

912
[dependencies]
1013
couchbase-lite-core-sys = { path = "../couchbase-lite-core-sys" }
@@ -16,7 +19,12 @@ fallible-streaming-iterator = "0.1.9"
1619
bitflags = "1.2.1"
1720
once_cell = "1.2.0"
1821
log = "0.4.8"
22+
tokio-tungstenite = { version = "0.10.1", optional = true }
23+
http = { version = "0.2.1", optional = true }
24+
tokio = { version = "0.2", default-features = false, features = ["sync", "macros"], optional = true }
25+
futures-util = { version = "0.3.5", optional = true }
26+
tokio-tls = { version = "0.3.1", optional = true }
1927

2028
[dev-dependencies]
2129
tempfile = "3.0"
22-
env_logger = "0.7"
30+
env_logger = "0.7"

couchbase-lite/src/document.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{
22
error::{c4error_init, Error},
33
ffi::{
4-
c4db_encodeJSON, c4doc_bodyAsJSON, c4doc_free, c4doc_loadRevisionBody, kDocExists,
4+
c4db_encodeJSON, c4doc_bodyAsJSON, c4doc_loadRevisionBody, c4doc_release, kDocExists,
55
kRevDeleted, C4Document, C4DocumentFlags, C4RevisionFlags,
66
},
77
fl_slice::{fl_slice_to_str_unchecked, AsFlSlice, FlSliceOwner},
@@ -175,6 +175,6 @@ impl C4DocumentOwner {
175175

176176
impl Drop for C4DocumentOwner {
177177
fn drop(&mut self) {
178-
unsafe { c4doc_free(self.0.as_ptr()) };
178+
unsafe { c4doc_release(self.0.as_ptr()) };
179179
}
180180
}

couchbase-lite/src/fl_slice.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,8 @@ pub(crate) unsafe fn fl_slice_to_str_unchecked<'a>(s: FLSlice) -> &'a str {
9696
let bytes: &[u8] = slice::from_raw_parts(s.buf as *const u8, s.size);
9797
str::from_utf8_unchecked(bytes)
9898
}
99+
100+
#[inline]
101+
pub(crate) unsafe fn fl_slice_to_slice<'a>(s: FLSlice) -> &'a [u8] {
102+
slice::from_raw_parts(s.buf as *const u8, s.size)
103+
}

couchbase-lite/src/lib.rs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,21 @@
4040
//! }
4141
//! ```
4242
43+
macro_rules! slice_without_nul {
44+
($c:ident) => {
45+
&$c[0..($c.len() - 1)]
46+
};
47+
}
48+
4349
mod doc_enumerator;
4450
mod document;
4551
mod error;
4652
mod fl_slice;
4753
mod log_reroute;
4854
mod observer;
4955
mod query;
56+
#[cfg(feature = "replication")]
57+
mod repl_transport;
5058
mod replicator;
5159
mod transaction;
5260
mod value;
@@ -60,18 +68,19 @@ pub use crate::{
6068
};
6169
pub use couchbase_lite_core_sys as ffi;
6270
pub use fallible_streaming_iterator;
71+
#[cfg(feature = "replication")]
72+
pub use repl_transport::use_web_sockets;
6373

6474
use crate::{
6575
document::C4DocumentOwner,
6676
error::{c4error_init, Result},
6777
ffi::{
68-
c4db_createIndex, c4db_free, c4db_getDocumentCount, c4db_getIndexes, c4db_open, c4doc_get,
69-
c4socket_registerFactory, kC4ArrayIndex, kC4DB_Create, kC4EncryptionNone, kC4FullTextIndex,
70-
kC4PredictiveIndex, kC4RevisionTrees, kC4SQLiteStorageEngine, kC4ValueIndex,
71-
C4CivetWebSocketFactory, C4Database, C4DatabaseConfig, C4DatabaseFlags,
72-
C4DocumentVersioning, C4EncryptionAlgorithm, C4EncryptionKey, C4IndexOptions, C4String,
73-
FLTrust_kFLTrusted, FLValue, FLValueType, FLValue_AsString, FLValue_FromData,
74-
FLValue_GetType,
78+
c4db_createIndex, c4db_getDocumentCount, c4db_getIndexes, c4db_open, c4db_release,
79+
c4doc_get, kC4ArrayIndex, kC4DB_Create, kC4EncryptionNone, kC4FullTextIndex,
80+
kC4PredictiveIndex, kC4RevisionTrees, kC4SQLiteStorageEngine, kC4ValueIndex, C4Database,
81+
C4DatabaseConfig, C4DatabaseFlags, C4DocumentVersioning, C4EncryptionAlgorithm,
82+
C4EncryptionKey, C4IndexOptions, C4String, FLTrust_kFLTrusted, FLValue, FLValueType,
83+
FLValue_AsString, FLValue_FromData, FLValue_GetType,
7584
},
7685
fl_slice::{fl_slice_to_str_unchecked, AsFlSlice, FlSliceOwner},
7786
log_reroute::DB_LOGGER,
@@ -114,11 +123,6 @@ impl Default for DatabaseConfig {
114123
}
115124
}
116125

117-
/// use embedded web-socket library
118-
pub fn use_c4_civet_web_socket_factory() {
119-
unsafe { c4socket_registerFactory(C4CivetWebSocketFactory) };
120-
}
121-
122126
/// A connection to a couchbase-lite database.
123127
pub struct Database {
124128
inner: DbInner,
@@ -145,7 +149,7 @@ impl Drop for Database {
145149
repl.stop();
146150
}
147151
self.db_observers.clear();
148-
unsafe { c4db_free(self.inner.0.as_ptr()) };
152+
unsafe { c4db_release(self.inner.0.as_ptr()) };
149153
}
150154
}
151155

@@ -272,8 +276,8 @@ impl Database {
272276
where
273277
F: FnMut(ReplicatorState) + Send + 'static,
274278
{
275-
self.db_replicator =
276-
Some(Replicator::new(
279+
let mut db_replicator =
280+
Replicator::new(
277281
self,
278282
url,
279283
token,
@@ -283,7 +287,9 @@ impl Database {
283287
error!("replicator status change: invalid status {}", err);
284288
}
285289
},
286-
)?);
290+
)?;
291+
db_replicator.start();
292+
self.db_replicator = Some(db_replicator);
287293
self.replicator_params = Some(ReplicatorParams {
288294
url: url.into(),
289295
token: token.map(str::to_string),

0 commit comments

Comments
 (0)