2018-02-14 00:51:21 +00:00
|
|
|
// Copyright 2016 Mozilla
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
|
|
// this file except in compliance with the License. You may obtain a copy of the
|
|
|
|
// License at http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
// Unless required by applicable law or agreed to in writing, software distributed
|
|
|
|
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
|
|
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
|
|
// specific language governing permissions and limitations under the License.
|
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
//extern crate mentat;
|
|
|
|
use mentat::{self, kw, Binding, Entid, HasSchema, Queryable, Schema, Store, TypedValue};
|
|
|
|
use mentat_core::{self, CachedAttributes};
|
2018-02-14 00:51:21 +00:00
|
|
|
|
|
|
|
use std::collections::BTreeSet;
|
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
use mentat_db::cache::SQLiteAttributeCache;
|
2018-02-14 00:51:21 +00:00
|
|
|
|
|
|
|
fn populate_db() -> Store {
|
|
|
|
let mut store = Store::open("").expect("opened");
|
|
|
|
{
|
|
|
|
let mut write = store.begin_transaction().expect("began transaction");
|
2020-01-14 15:46:21 +00:00
|
|
|
let _report = write
|
|
|
|
.transact(
|
|
|
|
r#"[
|
2018-02-14 00:51:21 +00:00
|
|
|
{:db/ident :foo/bar
|
|
|
|
:db/valueType :db.type/long
|
|
|
|
:db/cardinality :db.cardinality/one },
|
|
|
|
{:db/ident :foo/baz
|
|
|
|
:db/valueType :db.type/boolean
|
|
|
|
:db/cardinality :db.cardinality/one },
|
|
|
|
{:db/ident :foo/bap
|
|
|
|
:db/valueType :db.type/string
|
2020-01-14 15:46:21 +00:00
|
|
|
:db/cardinality :db.cardinality/many}]"#,
|
|
|
|
)
|
|
|
|
.expect("transaction expected to succeed");
|
|
|
|
let _report = write
|
|
|
|
.transact(
|
|
|
|
r#"[
|
2018-02-14 00:51:21 +00:00
|
|
|
{:db/ident :item/one
|
|
|
|
:foo/bar 100
|
|
|
|
:foo/baz false
|
|
|
|
:foo/bap ["one","two","buckle my shoe"] },
|
|
|
|
{:db/ident :item/two
|
|
|
|
:foo/bar 200
|
|
|
|
:foo/baz true
|
2020-01-14 15:46:21 +00:00
|
|
|
:foo/bap ["three", "four", "knock at my door"] }]"#,
|
|
|
|
)
|
|
|
|
.expect("transaction expected to succeed");
|
2018-02-14 00:51:21 +00:00
|
|
|
write.commit().expect("committed");
|
|
|
|
}
|
|
|
|
store
|
|
|
|
}
|
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
fn assert_value_present_for_attribute(
|
|
|
|
schema: &Schema,
|
|
|
|
attribute_cache: &mut SQLiteAttributeCache,
|
|
|
|
attribute: Entid,
|
|
|
|
entity: Entid,
|
|
|
|
value: TypedValue,
|
|
|
|
) {
|
2018-02-14 00:51:21 +00:00
|
|
|
let one = attribute_cache.get_value_for_entid(schema, attribute, entity);
|
2020-01-14 15:46:21 +00:00
|
|
|
assert!(attribute_cache
|
|
|
|
.get_values_for_entid(schema, attribute, entity)
|
|
|
|
.is_none());
|
2018-02-14 00:51:21 +00:00
|
|
|
|
|
|
|
assert_eq!(one, Some(&value));
|
|
|
|
}
|
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
fn assert_values_present_for_attribute(
|
|
|
|
schema: &Schema,
|
|
|
|
attribute_cache: &mut SQLiteAttributeCache,
|
|
|
|
attribute: Entid,
|
|
|
|
entity: Entid,
|
|
|
|
values: Vec<TypedValue>,
|
|
|
|
) {
|
|
|
|
assert!(attribute_cache
|
|
|
|
.get_value_for_entid(schema, attribute, entity)
|
|
|
|
.is_none());
|
|
|
|
let actual: BTreeSet<TypedValue> = attribute_cache
|
|
|
|
.get_values_for_entid(schema, attribute, entity)
|
|
|
|
.expect("Non-None")
|
|
|
|
.clone()
|
|
|
|
.into_iter()
|
|
|
|
.collect();
|
2018-02-14 00:51:21 +00:00
|
|
|
let expected: BTreeSet<TypedValue> = values.into_iter().collect();
|
|
|
|
|
|
|
|
assert_eq!(actual, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_add_to_cache() {
|
|
|
|
let mut store = populate_db();
|
|
|
|
let schema = &store.conn().current_schema();
|
|
|
|
let mut attribute_cache = SQLiteAttributeCache::default();
|
|
|
|
let kw = kw!(:foo/bar);
|
2020-01-14 15:46:21 +00:00
|
|
|
let attr: Entid = schema
|
|
|
|
.get_entid(&kw)
|
|
|
|
.expect("Expected entid for attribute")
|
|
|
|
.into();
|
2018-02-14 00:51:21 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
assert!(attribute_cache.value_pairs(schema, attr).is_none());
|
|
|
|
}
|
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
attribute_cache
|
|
|
|
.register(&schema, &store.sqlite_mut(), attr)
|
|
|
|
.expect("No errors on add to cache");
|
2018-02-14 00:51:21 +00:00
|
|
|
{
|
|
|
|
let cached_values = attribute_cache.value_pairs(schema, attr).expect("non-None");
|
|
|
|
assert!(!cached_values.is_empty());
|
2020-01-14 15:46:21 +00:00
|
|
|
let flattened: BTreeSet<TypedValue> =
|
|
|
|
cached_values.values().cloned().filter_map(|x| x).collect();
|
|
|
|
let expected: BTreeSet<TypedValue> = vec![TypedValue::Long(100), TypedValue::Long(200)]
|
|
|
|
.into_iter()
|
|
|
|
.collect();
|
2018-02-14 00:51:21 +00:00
|
|
|
assert_eq!(flattened, expected);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_add_attribute_already_in_cache() {
|
|
|
|
let mut store = populate_db();
|
|
|
|
let schema = store.conn().current_schema();
|
|
|
|
|
|
|
|
let kw = kw!(:foo/bar);
|
2020-01-14 15:46:21 +00:00
|
|
|
let attr: Entid = schema
|
|
|
|
.get_entid(&kw)
|
|
|
|
.expect("Expected entid for attribute")
|
|
|
|
.into();
|
2018-02-14 00:51:21 +00:00
|
|
|
let mut attribute_cache = SQLiteAttributeCache::default();
|
|
|
|
|
|
|
|
let one = schema.get_entid(&kw!(:item/one)).expect("one");
|
|
|
|
let two = schema.get_entid(&kw!(:item/two)).expect("two");
|
2020-01-14 15:46:21 +00:00
|
|
|
attribute_cache
|
2020-08-06 03:03:58 +00:00
|
|
|
.register(&schema, &store.sqlite_mut(), attr)
|
2020-01-14 15:46:21 +00:00
|
|
|
.expect("No errors on add to cache");
|
|
|
|
assert_value_present_for_attribute(
|
|
|
|
&schema,
|
|
|
|
&mut attribute_cache,
|
2020-08-06 03:03:58 +00:00
|
|
|
attr,
|
2020-01-14 15:46:21 +00:00
|
|
|
one.into(),
|
|
|
|
TypedValue::Long(100),
|
|
|
|
);
|
|
|
|
attribute_cache
|
2020-08-06 03:03:58 +00:00
|
|
|
.register(&schema, &store.sqlite_mut(), attr)
|
2020-01-14 15:46:21 +00:00
|
|
|
.expect("No errors on add to cache");
|
|
|
|
assert_value_present_for_attribute(
|
|
|
|
&schema,
|
|
|
|
&mut attribute_cache,
|
2020-08-06 03:03:58 +00:00
|
|
|
attr,
|
2020-01-14 15:46:21 +00:00
|
|
|
two.into(),
|
|
|
|
TypedValue::Long(200),
|
|
|
|
);
|
2018-02-14 00:51:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_remove_from_cache() {
|
|
|
|
let mut store = populate_db();
|
|
|
|
let schema = store.conn().current_schema();
|
|
|
|
|
|
|
|
let kwr = kw!(:foo/bar);
|
2020-01-14 15:46:21 +00:00
|
|
|
let entidr: Entid = schema
|
|
|
|
.get_entid(&kwr)
|
|
|
|
.expect("Expected entid for attribute")
|
|
|
|
.into();
|
2018-02-14 00:51:21 +00:00
|
|
|
let kwz = kw!(:foo/baz);
|
2020-01-14 15:46:21 +00:00
|
|
|
let entidz: Entid = schema
|
|
|
|
.get_entid(&kwz)
|
|
|
|
.expect("Expected entid for attribute")
|
|
|
|
.into();
|
2018-02-14 00:51:21 +00:00
|
|
|
let kwp = kw!(:foo/bap);
|
2020-01-14 15:46:21 +00:00
|
|
|
let entidp: Entid = schema
|
|
|
|
.get_entid(&kwp)
|
|
|
|
.expect("Expected entid for attribute")
|
|
|
|
.into();
|
2018-02-14 00:51:21 +00:00
|
|
|
|
|
|
|
let mut attribute_cache = SQLiteAttributeCache::default();
|
|
|
|
|
|
|
|
let one = schema.get_entid(&kw!(:item/one)).expect("one");
|
|
|
|
let two = schema.get_entid(&kw!(:item/two)).expect("two");
|
2020-01-14 15:46:21 +00:00
|
|
|
assert!(attribute_cache
|
|
|
|
.get_value_for_entid(&schema, entidz, one.into())
|
|
|
|
.is_none());
|
|
|
|
assert!(attribute_cache
|
|
|
|
.get_values_for_entid(&schema, entidz, one.into())
|
|
|
|
.is_none());
|
|
|
|
assert!(attribute_cache
|
|
|
|
.get_value_for_entid(&schema, entidz, two.into())
|
|
|
|
.is_none());
|
|
|
|
assert!(attribute_cache
|
|
|
|
.get_values_for_entid(&schema, entidz, two.into())
|
|
|
|
.is_none());
|
|
|
|
assert!(attribute_cache
|
|
|
|
.get_value_for_entid(&schema, entidp, one.into())
|
|
|
|
.is_none());
|
|
|
|
assert!(attribute_cache
|
|
|
|
.get_values_for_entid(&schema, entidp, one.into())
|
|
|
|
.is_none());
|
|
|
|
|
|
|
|
attribute_cache
|
2020-08-06 03:03:58 +00:00
|
|
|
.register(&schema, &store.sqlite_mut(), entidr)
|
2020-01-14 15:46:21 +00:00
|
|
|
.expect("No errors on add to cache");
|
|
|
|
assert_value_present_for_attribute(
|
|
|
|
&schema,
|
|
|
|
&mut attribute_cache,
|
|
|
|
entidr,
|
|
|
|
one.into(),
|
|
|
|
TypedValue::Long(100),
|
|
|
|
);
|
|
|
|
assert_value_present_for_attribute(
|
|
|
|
&schema,
|
|
|
|
&mut attribute_cache,
|
|
|
|
entidr,
|
|
|
|
two.into(),
|
|
|
|
TypedValue::Long(200),
|
|
|
|
);
|
|
|
|
attribute_cache
|
2020-08-06 03:03:58 +00:00
|
|
|
.register(&schema, &store.sqlite_mut(), entidz)
|
2020-01-14 15:46:21 +00:00
|
|
|
.expect("No errors on add to cache");
|
|
|
|
assert_value_present_for_attribute(
|
|
|
|
&schema,
|
|
|
|
&mut attribute_cache,
|
|
|
|
entidz,
|
|
|
|
one.into(),
|
|
|
|
TypedValue::Boolean(false),
|
|
|
|
);
|
|
|
|
assert_value_present_for_attribute(
|
|
|
|
&schema,
|
|
|
|
&mut attribute_cache,
|
|
|
|
entidz,
|
|
|
|
one.into(),
|
|
|
|
TypedValue::Boolean(false),
|
|
|
|
);
|
|
|
|
attribute_cache
|
2020-08-06 03:03:58 +00:00
|
|
|
.register(&schema, &store.sqlite_mut(), entidp)
|
2020-01-14 15:46:21 +00:00
|
|
|
.expect("No errors on add to cache");
|
|
|
|
assert_values_present_for_attribute(
|
|
|
|
&schema,
|
|
|
|
&mut attribute_cache,
|
|
|
|
entidp,
|
|
|
|
one.into(),
|
|
|
|
vec![
|
|
|
|
TypedValue::typed_string("buckle my shoe"),
|
|
|
|
TypedValue::typed_string("one"),
|
|
|
|
TypedValue::typed_string("two"),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
assert_values_present_for_attribute(
|
|
|
|
&schema,
|
|
|
|
&mut attribute_cache,
|
|
|
|
entidp,
|
|
|
|
two.into(),
|
|
|
|
vec![
|
|
|
|
TypedValue::typed_string("knock at my door"),
|
|
|
|
TypedValue::typed_string("three"),
|
|
|
|
TypedValue::typed_string("four"),
|
|
|
|
],
|
|
|
|
);
|
2018-02-14 00:51:21 +00:00
|
|
|
|
|
|
|
// test that we can remove an item from cache
|
|
|
|
attribute_cache.unregister(entidz);
|
2020-08-06 03:03:58 +00:00
|
|
|
assert!(!attribute_cache.is_attribute_cached_forward(entidz));
|
2020-01-14 15:46:21 +00:00
|
|
|
assert!(attribute_cache
|
|
|
|
.get_value_for_entid(&schema, entidz, one.into())
|
|
|
|
.is_none());
|
|
|
|
assert!(attribute_cache
|
|
|
|
.get_values_for_entid(&schema, entidz, one.into())
|
|
|
|
.is_none());
|
|
|
|
assert!(attribute_cache
|
|
|
|
.get_value_for_entid(&schema, entidz, two.into())
|
|
|
|
.is_none());
|
|
|
|
assert!(attribute_cache
|
|
|
|
.get_values_for_entid(&schema, entidz, two.into())
|
|
|
|
.is_none());
|
2018-02-14 00:51:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_remove_attribute_not_in_cache() {
|
|
|
|
let store = populate_db();
|
|
|
|
let mut attribute_cache = SQLiteAttributeCache::default();
|
|
|
|
|
|
|
|
let schema = store.conn().current_schema();
|
|
|
|
let kw = kw!(:foo/baz);
|
2020-01-14 15:46:21 +00:00
|
|
|
let entid = schema
|
|
|
|
.get_entid(&kw)
|
|
|
|
.expect("Expected entid for attribute")
|
|
|
|
.0;
|
2018-02-14 00:51:21 +00:00
|
|
|
attribute_cache.unregister(entid);
|
|
|
|
assert!(!attribute_cache.is_attribute_cached_forward(entid));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fetch_attribute_value_for_entid() {
|
|
|
|
let mut store = populate_db();
|
|
|
|
let schema = store.conn().current_schema();
|
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
let entities = store
|
|
|
|
.q_once(r#"[:find ?e . :where [?e :foo/bar 100]]"#, None)
|
|
|
|
.expect("Expected query to work")
|
|
|
|
.into_scalar()
|
|
|
|
.expect("expected scalar results");
|
2018-02-14 00:51:21 +00:00
|
|
|
let entid = match entities {
|
2018-04-24 22:08:38 +00:00
|
|
|
Some(Binding::Scalar(TypedValue::Ref(entid))) => entid,
|
2018-02-14 00:51:21 +00:00
|
|
|
x => panic!("expected Some(Ref), got {:?}", x),
|
|
|
|
};
|
|
|
|
|
|
|
|
let kwr = kw!(:foo/bar);
|
2020-01-14 15:46:21 +00:00
|
|
|
let attr_entid = schema
|
|
|
|
.get_entid(&kwr)
|
|
|
|
.expect("Expected entid for attribute")
|
|
|
|
.0;
|
2018-02-14 00:51:21 +00:00
|
|
|
|
|
|
|
let mut attribute_cache = SQLiteAttributeCache::default();
|
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
attribute_cache
|
2020-08-06 03:03:58 +00:00
|
|
|
.register(&schema, &store.sqlite_mut(), attr_entid)
|
2020-01-14 15:46:21 +00:00
|
|
|
.expect("No errors on add to cache");
|
|
|
|
let val = attribute_cache
|
|
|
|
.get_value_for_entid(&schema, attr_entid, entid)
|
|
|
|
.expect("Expected value");
|
2018-02-14 00:51:21 +00:00
|
|
|
assert_eq!(*val, TypedValue::Long(100));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fetch_attribute_values_for_entid() {
|
|
|
|
let mut store = populate_db();
|
|
|
|
let schema = store.conn().current_schema();
|
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
let entities = store
|
|
|
|
.q_once(r#"[:find ?e . :where [?e :foo/bar 100]]"#, None)
|
|
|
|
.expect("Expected query to work")
|
|
|
|
.into_scalar()
|
|
|
|
.expect("expected scalar results");
|
2018-02-14 00:51:21 +00:00
|
|
|
let entid = match entities {
|
2018-04-24 22:08:38 +00:00
|
|
|
Some(Binding::Scalar(TypedValue::Ref(entid))) => entid,
|
2018-02-14 00:51:21 +00:00
|
|
|
x => panic!("expected Some(Ref), got {:?}", x),
|
|
|
|
};
|
|
|
|
|
|
|
|
let kwp = kw!(:foo/bap);
|
2020-01-14 15:46:21 +00:00
|
|
|
let attr_entid = schema
|
|
|
|
.get_entid(&kwp)
|
|
|
|
.expect("Expected entid for attribute")
|
|
|
|
.0;
|
2018-02-14 00:51:21 +00:00
|
|
|
|
|
|
|
let mut attribute_cache = SQLiteAttributeCache::default();
|
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
attribute_cache
|
2020-08-06 03:03:58 +00:00
|
|
|
.register(&schema, &store.sqlite_mut(), attr_entid)
|
2020-01-14 15:46:21 +00:00
|
|
|
.expect("No errors on add to cache");
|
|
|
|
let val = attribute_cache
|
|
|
|
.get_values_for_entid(&schema, attr_entid, entid)
|
|
|
|
.expect("Expected value");
|
|
|
|
assert_eq!(
|
|
|
|
*val,
|
|
|
|
vec![
|
|
|
|
TypedValue::typed_string("buckle my shoe"),
|
|
|
|
TypedValue::typed_string("one"),
|
|
|
|
TypedValue::typed_string("two")
|
|
|
|
]
|
|
|
|
);
|
2018-02-14 00:51:21 +00:00
|
|
|
}
|