forked from ApolloCrawler/gauc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcouchbase.rs
69 lines (53 loc) · 2.1 KB
/
couchbase.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
extern crate clap;
extern crate gauc;
extern crate libc;
use gauc::couchbase::*;
use std::ffi::CStr;
use std::ffi::CString;
fn main() {
let connstr = CString::new("couchbase://localhost/default").unwrap();
let mut cropts = CreateSt::new();
cropts.v3.connstr = connstr.as_ptr();
let mut instance: Instance = Instance::new();
unsafe {
let res = lcb_create(&mut instance as *mut Instance, &cropts as *const CreateSt);
println!("Create Res: {:?}", res);
let res = lcb_connect(instance);
println!("Connect Res: {:?}", res);
let res = lcb_wait(instance);
println!("Connect Wait Res: {:?}", res);
let res = lcb_get_bootstrap_status(instance);
println!(
"Bootstrap Status: {:?} \"{}\"",
res,
CStr::from_ptr(lcb_strerror(instance, res)).to_str().unwrap() // description
);
lcb_install_callback3(instance, CallbackType::Get, op_callback);
let key = "foo";
let ckey = CString::new(key).unwrap();
let mut gcmd = cmd::Get::default();
gcmd.key._type = KvBufferType::Copy;
gcmd.key.contig.bytes = ckey.as_ptr() as *const libc::c_void;
gcmd.key.contig.nbytes = key.len() as u64;
let res = lcb_get3(instance, std::ptr::null(), &gcmd as *const cmd::Get);
println!("Get Res: {:?}", res);
let res = lcb_wait(instance);
println!("Get Wait Res: {:?}", res);
lcb_destroy(instance);
}
}
unsafe extern "C" fn op_callback(_instance: Instance, cbtype: CallbackType, resp: *const response::Base) {
match cbtype {
CallbackType::Get => {
println!("> Get Callback!");
let gresp = resp as *const response::GetInternal;
println!(">> CAS: {}", (*gresp).cas);
if !(*gresp).value.is_null() {
let res = CString::from_raw((*gresp).value as *mut i8);
let length = (*gresp).nvalue as usize;
println!(">> Content: {}", &res.into_string().unwrap()[..length]);
}
},
_ => panic!("! Unknown Callback...")
};
}