Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
amitayh committed Jun 3, 2024
1 parent 2085bc8 commit fe10730
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 9 deletions.
42 changes: 41 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,10 @@ mod tests {
.with(EntityOperation::on_new().assert("person/name", "Bob")),
);

// [:find ?name
// :where [?name :person/name "Eve"]]
let query_result = sut.query(
Query::new().with(
Query::new().find(Find::variable("?name")).with(
Clause::new()
.with_entity(Pattern::variable("?name"))
.with_attribute(Pattern::ident("person/name"))
Expand All @@ -191,6 +193,8 @@ mod tests {
.with(EntityOperation::on_temp_id("joe").assert("person/name", "Joe")),
);

// [:find ?joe
// :where [?joe :person/name "Joe"]]
let query_result = sut.query(
Query::new().find(Find::variable("?joe")).with(
Clause::new()
Expand Down Expand Up @@ -250,6 +254,10 @@ mod tests {
),
);

// [:find ?release-name
// :where [?artist :artist/name "John Lenon"]
// [?release :release/artist ?artist]
// [?release :release/name ?release-name]]
let query_result = sut.query(
Query::new()
.find(Find::variable("?release-name"))
Expand Down Expand Up @@ -299,6 +307,8 @@ mod tests {
.with(EntityOperation::on_id(joe_id).assert("person/email", "[email protected]")),
);

// [:find ?email
// :where [?joe_id :person/email ?email]]
let query_result = sut.query(
Query::new().find(Find::variable("?email")).with(
Clause::new()
Expand Down Expand Up @@ -334,6 +344,8 @@ mod tests {
.with(EntityOperation::on_id(joe_id).assert("person/likes", "Ice cream")),
);

// [:find ?likes
// :where [?joe_id :person/likes ?likes]]
let query_result = sut.query(
Query::new().find(Find::variable("?likes")).with(
Clause::new()
Expand Down Expand Up @@ -375,6 +387,8 @@ mod tests {
),
);

// [:find ?likes
// :where [?joe_id :person/likes ?likes]]
let query_result = sut.query_at_snapshot(
first_tx_result.tx_id,
Query::new().find(Find::variable("?likes")).with(
Expand All @@ -400,6 +414,9 @@ mod tests {
Transaction::new().with(EntityOperation::on_new().assert("person/name", "Joe")),
);

// [:find ?tx ?tx_time
// :where [_ :person/name "Joe" ?tx]
// [?tx ?tx_time_id ?tx_time]]
let query_result = sut.query(
Query::new()
.find(Find::variable("?tx"))
Expand Down Expand Up @@ -437,6 +454,8 @@ mod tests {
);

// Find all datoms belonging to transaction
// [:find ?e ?a ?v
// :where [?e ?a ?v ?tx_id]]
let query_result = sut.query(
Query::new()
.find(Find::variable("?e"))
Expand Down Expand Up @@ -475,6 +494,8 @@ mod tests {
Transaction::new().with(EntityOperation::on_new().assert("person/name", "John")),
);

// [:find (count)
// : where [?person :person/name]]
let query_result = sut.query(
Query::new().find(Find::count()).with(
Clause::new()
Expand All @@ -496,6 +517,9 @@ mod tests {
// Insert data
sut.transact(create_beatles());

// [:find ?born (count)
// :where [?person :person/born ?born]
// [?person :person/name ?name]]
let query = Query::new()
.find(Find::variable("?born"))
.find(Find::count())
Expand Down Expand Up @@ -531,6 +555,9 @@ mod tests {
// Insert data
sut.transact(create_beatles());

// [:find (sum ?born) ?born
// :where [?person :person/born ?born]
// [?person :person/name ?name]]
let query = Query::new()
.find(Find::sum("?born"))
.find(Find::variable("?born"))
Expand Down Expand Up @@ -594,6 +621,9 @@ mod tests {
),
);

// [:find ?name (count-distinct ?likes)
// :where [?person :person/name ?name]
// [?person :person/likes ?likes]]
let query = Query::new()
.find(Find::variable("?name"))
.find(Find::count_distinct("?likes"))
Expand Down Expand Up @@ -629,6 +659,8 @@ mod tests {
// Insert data
sut.transact(create_beatles());

// [:find ?name
// :where [?person :person/born ?born]]
let query_result = sut
.try_query(
Query::new().find(Find::variable("?name")).with(
Expand All @@ -653,6 +685,8 @@ mod tests {
// Insert data
sut.transact(create_beatles());

// [:find ?name (count)
// :where [?person :person/born ?born]]
let query_result = sut.try_query(
Query::new()
.find(Find::variable("?name"))
Expand All @@ -678,6 +712,10 @@ mod tests {
// Insert data
sut.transact(create_beatles());

// [:find ?name
// :where [?person :person/born ?born]
// [?person :person/name ?name]
// [(> ?born 1940)]]
let query_result = sut.query(
Query::new()
.find(Find::variable("?name"))
Expand Down Expand Up @@ -722,6 +760,8 @@ mod tests {
);

let joe_id = tx_result.temp_ids["joe"];
// [:find ?likes
// :where [?joe_id :person/likes ?likes]]
let query = Query::new().find(Find::variable("?likes")).with(
Clause::new()
.with_entity(Pattern::Constant(joe_id))
Expand Down
14 changes: 7 additions & 7 deletions src/query/assignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ impl PartialAssignment {
/// assignment.assign("bar", Value::U64(2));
/// assignment.assign("baz", Value::U64(3));
///
/// assert_eq!(Value::U64(1), assignment.assigned["foo"]);
/// assert_eq!(Value::U64(2), assignment.assigned["bar"]);
/// assert_eq!(Value::U64(3), assignment.assigned["baz"]);
/// assert_eq!(Some(&Value::U64(1)), assignment.get("foo"));
/// assert_eq!(Some(&Value::U64(2)), assignment.get("bar"));
/// assert_eq!(Some(&Value::U64(3)), assignment.get("baz"));
/// ```
pub fn from_clauses(clauses: &[Clause]) -> Self {
Self::new(
Expand Down Expand Up @@ -117,10 +117,10 @@ impl PartialAssignment {
/// let datom = Datom::add(entity, attribute, value, tx);
/// let updated = assignment.update_with(&clause, datom);
///
/// assert_eq!(Value::Ref(entity), updated.assigned["?entity"]);
/// assert_eq!(Value::Ref(attribute), updated.assigned["?attribute"]);
/// assert_eq!(Value::U64(value), updated.assigned["?value"]);
/// assert_eq!(Value::Ref(tx), updated.assigned["?tx"]);
/// assert_eq!(Some(&Value::Ref(entity)), updated.get("?entity"));
/// assert_eq!(Some(&Value::Ref(attribute)), updated.get("?attribute"));
/// assert_eq!(Some(&Value::U64(value)), updated.get("?value"));
/// assert_eq!(Some(&Value::Ref(tx)), updated.get("?tx"));
/// ```
pub fn update_with(&self, clause: &Clause, datom: Datom) -> Self {
let mut assignment = self.clone();
Expand Down
1 change: 0 additions & 1 deletion src/storage/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ impl<'a> WriteStorage for DiskStorage<'a, ReadWrite> {
latest_entity_id = latest_entity_id.max(datom.entity);
}
batch.put_cf(system, KEY_LATEST_ENTITY_ID, latest_entity_id.to_be_bytes());
//batch.put_cf(system, "entity_id", latest_entity_id.to_be_bytes());
self.db.write(batch)?;
Ok(())
}
Expand Down

0 comments on commit fe10730

Please sign in to comment.