Skip to content

Commit 825e893

Browse files
authored
Merge pull request #18 from Dushistov/fix-like-regression
fix LIKE speed regression
2 parents f70df18 + afc4634 commit 825e893

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed
Submodule couchbase-lite-core updated 59 files

couchbase-lite/tests/smoke_tests.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,3 +442,60 @@ fn test_like_offset_limit() {
442442
Ok(query_ret)
443443
}
444444
}
445+
446+
#[test]
447+
fn test_like_performance() {
448+
let _ = env_logger::try_init();
449+
let tmp_dir = tempdir().expect("Can not create tmp directory");
450+
println!("we create tempdir at {}", tmp_dir.path().display());
451+
let db_path = tmp_dir.path().join("a.cblite2");
452+
{
453+
let mut db = Database::open(&db_path, DatabaseConfig::default()).unwrap();
454+
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
455+
#[serde(tag = "type")]
456+
struct Data {
457+
field1: String,
458+
field2: String,
459+
}
460+
461+
const N: usize = 3_000;
462+
let mut trans = db.transaction().unwrap();
463+
for i in 0..N {
464+
let d = Data {
465+
field1: format!("_common_prefix_{}", i),
466+
field2: format!("{}", i + 1),
467+
};
468+
let mut doc = Document::new(&d).unwrap();
469+
trans.save(&mut doc).unwrap();
470+
}
471+
trans.commit().unwrap();
472+
473+
db.create_index("field1", "[[\".field1\"]]", IndexType::ValueIndex, None)
474+
.unwrap();
475+
db.create_index("field2", "[[\".field2\"]]", IndexType::ValueIndex, None)
476+
.unwrap();
477+
478+
for i in 0..N {
479+
let pat = format!("{}", i);
480+
let query = db
481+
.query(&format!(
482+
r#"{{
483+
"WHAT": [["count()"]],
484+
"WHERE": ["OR", ["LIKE", [".field1"], "%{pat}%"],
485+
["LIKE", [".field2"], "%{pat}%"]]}}"#,
486+
pat = pat,
487+
))
488+
.unwrap();
489+
let mut iter = query.run().unwrap();
490+
let mut query_ret = Vec::with_capacity(10);
491+
while let Some(item) = iter.next().unwrap() {
492+
let val = item.get_raw_checked(0).unwrap();
493+
let val = val.as_u64().unwrap();
494+
query_ret.push(val);
495+
}
496+
assert_eq!(1, query_ret.len());
497+
assert!(query_ret[0] > 1);
498+
}
499+
}
500+
tmp_dir.close().expect("Can not close tmp_dir");
501+
}

0 commit comments

Comments
 (0)