Skip to content

Commit d159391

Browse files
committed
Cleanup
1 parent 13ae71e commit d159391

File tree

7 files changed

+346
-112
lines changed

7 files changed

+346
-112
lines changed

examples/README.md

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ The configuration files that might interest you are:
1212
- `docker-conf/db-config.json` -> contains the database configuration
1313
- `docker-conf/sync-function.js` -> contains the sync function used by the Sync Gateway
1414

15-
To start both the Sync Gatewawy and Couchbase Server, move to `docker-conf` through a terminal and use:
15+
To start both the Sync Gateway and Couchbase Server, move to `docker-conf` through a terminal and use:
1616

1717
```shell
1818
$ docker-compose up
@@ -44,16 +44,11 @@ $ curl -XPUT -v "http://localhost:4985/my-db/" -H 'Content-Type: application/jso
4444

4545
## Running an example
4646

47-
As of now, there is only one example: `sgw_1_cblite`.
47+
As of now, there is only one example: `ticket_70596`.
4848

4949
It can be run with the following command:
5050
```shell
51-
$ cargo run --features=enterprise --example sgw_1_cblite
51+
$ cargo run --features=enterprise --example ticket_70596
5252
```
5353

54-
What it does:
55-
- Create a cblite database `test1`
56-
- Add a user `great_name` to the Sync Gateway
57-
- Retrieve a session token for the user `great_name` from the Sync Gateway
58-
- Start a continuous push & pull replicator
59-
- Create a document, then wait for 5 seconds for the replication to finish
54+
There are utility functions available to interact with the Sync Gateway or Couchbase Server, feel free to add more if needed.

examples/sgw_1_cblite.rs

Lines changed: 0 additions & 103 deletions
This file was deleted.

examples/ticket_70596.rs

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
mod utils;
2+
3+
use std::path::Path;
4+
use couchbase_lite::*;
5+
use utils::*;
6+
7+
fn main() {
8+
let mut db = Database::open(
9+
"test1",
10+
Some(DatabaseConfiguration {
11+
directory: Path::new("./"),
12+
#[cfg(feature = "enterprise")]
13+
encryption_key: None,
14+
}),
15+
)
16+
.unwrap();
17+
18+
add_or_update_user("great_name", vec!["channel1".into()]);
19+
let session_token = get_session("great_name");
20+
println!("Sync gateway session token: {session_token}");
21+
22+
let mut repl =
23+
setup_replicator(db.clone(), session_token).add_document_listener(Box::new(doc_listener));
24+
25+
repl.start(false);
26+
27+
std::thread::sleep(std::time::Duration::from_secs(3));
28+
29+
// Auto-purge test scenario from support ticket https://support.couchbase.com/hc/en-us/requests/70596?page=1
30+
// Testing if documents pushed to inaccessible channels get auto-purged
31+
create_doc(&mut db, "doc1", "channel1");
32+
create_doc(&mut db, "doc2", "channel2");
33+
34+
std::thread::sleep(std::time::Duration::from_secs(10));
35+
assert!(get_doc(&db, "doc1").is_ok());
36+
assert!(get_doc(&db, "doc2").is_ok()); // This looks buggy
37+
38+
change_channel(&mut db, "doc1", "channel2");
39+
40+
std::thread::sleep(std::time::Duration::from_secs(10));
41+
assert!(get_doc(&db, "doc1").is_err());
42+
43+
repl.stop(None);
44+
}
45+
46+
fn create_doc(db: &mut Database, id: &str, channel: &str) {
47+
let mut doc = Document::new_with_id(id);
48+
doc.set_properties_as_json(
49+
&serde_json::json!({
50+
"channels": channel,
51+
})
52+
.to_string(),
53+
)
54+
.unwrap();
55+
db.save_document(&mut doc).unwrap();
56+
57+
println!(
58+
"Created doc {id} with content: {}",
59+
doc.properties_as_json()
60+
);
61+
}
62+
63+
fn get_doc(db: &Database, id: &str) -> Result<Document> {
64+
db.get_document(id)
65+
}
66+
67+
fn change_channel(db: &mut Database, id: &str, channel: &str) {
68+
let mut doc = get_doc(db, id).unwrap();
69+
let mut prop = doc.mutable_properties();
70+
prop.at("channels").put_string(channel);
71+
let _ = db.save_document(&mut doc);
72+
println!(
73+
"Changed doc {id} with content: {}",
74+
doc.properties_as_json()
75+
);
76+
}
77+
78+
fn setup_replicator(db: Database, session_token: String) -> Replicator {
79+
let repl_conf = ReplicatorConfiguration {
80+
database: Some(db.clone()),
81+
endpoint: Endpoint::new_with_url(SYNC_GW_URL).unwrap(),
82+
replicator_type: ReplicatorType::PushAndPull,
83+
continuous: true,
84+
disable_auto_purge: false,
85+
max_attempts: 3,
86+
max_attempt_wait_time: 1,
87+
heartbeat: 60,
88+
authenticator: None,
89+
proxy: None,
90+
headers: vec![(
91+
"Cookie".to_string(),
92+
format!("SyncGatewaySession={session_token}"),
93+
)]
94+
.into_iter()
95+
.collect(),
96+
pinned_server_certificate: None,
97+
trusted_root_certificates: None,
98+
channels: MutableArray::default(),
99+
document_ids: MutableArray::default(),
100+
collections: None,
101+
accept_parent_domain_cookies: false,
102+
#[cfg(feature = "enterprise")]
103+
accept_only_self_signed_server_certificate: false,
104+
};
105+
let repl_context = ReplicationConfigurationContext::default();
106+
Replicator::new(repl_conf, Box::new(repl_context)).unwrap()
107+
}
108+
109+
fn doc_listener(direction: Direction, documents: Vec<ReplicatedDocument>) {
110+
println!("=== Document(s) replicated ===");
111+
println!("Direction: {direction:?}");
112+
for document in documents {
113+
println!("Document: {document:?}");
114+
}
115+
println!("===");
116+
}

examples/utils/cbs_admin.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
use crate::utils::constants::*;
2+
3+
pub fn purge_doc_from_cbs(doc_id: &str) {
4+
let url = format!("{CBS_URL}/pools/default/buckets/{CBS_BUCKET}/docs/{doc_id}");
5+
let response = reqwest::blocking::Client::new()
6+
.delete(&url)
7+
.basic_auth(CBS_ADMIN_USER, Some(CBS_ADMIN_PWD))
8+
.send();
9+
10+
match response {
11+
Ok(resp) => {
12+
let status = resp.status();
13+
if let Ok(body) = resp.text() {
14+
println!("Purge {doc_id} from CBS: status={status}, body={body}");
15+
} else {
16+
println!("Purge {doc_id} from CBS: status={status}");
17+
}
18+
}
19+
Err(e) => println!("Purge {doc_id} from CBS error: {e}"),
20+
}
21+
}
22+
23+
pub fn compact_cbs_bucket() {
24+
let url = format!("{CBS_URL}/pools/default/buckets/{CBS_BUCKET}/controller/compactBucket");
25+
let response = reqwest::blocking::Client::new()
26+
.post(&url)
27+
.basic_auth(CBS_ADMIN_USER, Some(CBS_ADMIN_PWD))
28+
.send();
29+
30+
match response {
31+
Ok(resp) => {
32+
let status = resp.status();
33+
if let Ok(body) = resp.text() {
34+
println!("Compact CBS bucket: status={status}, body={body}");
35+
} else {
36+
println!("Compact CBS bucket: status={status}");
37+
}
38+
}
39+
Err(e) => println!("Compact CBS bucket error: {e}"),
40+
}
41+
}
42+
43+
pub fn check_doc_in_cbs(doc_id: &str) {
44+
let url = format!("{CBS_URL}:8093/query/service");
45+
let query = format!(
46+
"SELECT META().id, META().deleted FROM `{CBS_BUCKET}` WHERE META().id = '{doc_id}'"
47+
);
48+
let body = serde_json::json!({"statement": query});
49+
50+
let response = reqwest::blocking::Client::new()
51+
.post(&url)
52+
.basic_auth(CBS_ADMIN_USER, Some(CBS_ADMIN_PWD))
53+
.json(&body)
54+
.send();
55+
56+
match response {
57+
Ok(resp) => {
58+
if let Ok(text) = resp.text() {
59+
println!("CBS check for {doc_id}: {text}");
60+
}
61+
}
62+
Err(e) => println!("CBS check error: {e}"),
63+
}
64+
}

examples/utils/constants.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pub const SYNC_GW_URL_ADMIN: &str = "http://localhost:4985/my-db";
2+
pub const SYNC_GW_URL: &str = "ws://localhost:4984/my-db";
3+
pub const CBS_URL: &str = "http://localhost:8091";
4+
pub const CBS_BUCKET: &str = "my-bucket";
5+
pub const CBS_ADMIN_USER: &str = "cb_admin";
6+
pub const CBS_ADMIN_PWD: &str = "cb_admin_pwd";

examples/utils/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
pub mod cbs_admin;
2+
pub mod constants;
3+
pub mod sgw_admin;
4+
5+
// Re-export commonly used functions
6+
pub use constants::*;
7+
pub use sgw_admin::*;
8+
pub use cbs_admin::*;

0 commit comments

Comments
 (0)