Compare commits

..

No commits in common. "master" and "entity-builder" have entirely different histories.

214 changed files with 15683 additions and 27711 deletions

3
.github/FUNDING.yml vendored
View file

@ -1,3 +0,0 @@
liberapay: svartalf
patreon: svartalf
custom: ["https://svartalf.info/donate/", "https://www.buymeacoffee.com/svartalf"]

View file

@ -1,11 +0,0 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for all configuration options:
# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
version: 2
updates:
- package-ecosystem: "cargo" # See documentation for possible values
directory: "/" # Location of package manifests
schedule:
interval: "daily"

View file

@ -1,20 +0,0 @@
name: Security audit
on:
schedule:
- cron: '0 0 1 * *'
push:
paths:
- '**/Cargo.toml'
- '**/Cargo.lock'
pull_request:
jobs:
audit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/audit-check@issue-104
with:
token: ${{ secrets.GITHUB_TOKEN }}

View file

@ -1,13 +0,0 @@
on: [push, pull_request]
name: Clippy (new version test, don't use it!)
jobs:
clippy_check_ng:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: nightly
components: clippy
override: true
- uses: actions-rs/clippy@master

View file

@ -1,16 +0,0 @@
on: [push, pull_request]
name: Clippy check
jobs:
clippy_check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: nightly
components: clippy
override: true
- uses: actions-rs/clippy-check@v1
with:
args: --all-targets --all-features -- -D warnings
token: ${{ secrets.GITHUB_TOKEN }}

View file

@ -1,28 +0,0 @@
# We could use `@actions-rs/cargo` Action ability to automatically install `cross` tool
# in order to compile our application for some unusual targets.
on: [push, pull_request]
name: Cross-compile
jobs:
build:
name: Build
runs-on: ubuntu-latest
strategy:
matrix:
target:
- armv7-unknown-linux-gnueabihf
- powerpc64-unknown-linux-gnu
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
target: ${{ matrix.target }}
override: true
- uses: actions-rs/cargo@v1
with:
use-cross: true
command: build
args: --release --target=${{ matrix.target }}

View file

@ -1,66 +0,0 @@
on: [push, pull_request]
name: Code coverage with grcov
jobs:
grcov:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os:
- ubuntu-latest
- macOS-latest
# - windows-latest
steps:
- uses: actions/checkout@v2
- name: Install toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: nightly
override: true
profile: minimal
- name: Execute tests
uses: actions-rs/cargo@v1
with:
command: test
args: --all
env:
CARGO_INCREMENTAL: 0
RUSTFLAGS: "-Zprofile -Ccodegen-units=1 -Cinline-threshold=0 -Clink-dead-code -Coverflow-checks=off -Cpanic=abort -Zpanic_abort_tests"
# Note that `actions-rs/grcov` Action can install `grcov` too,
# but can't use faster installation methods yet.
# As a temporary experiment `actions-rs/install` Action plugged in here.
# Consider **NOT** to copy that into your workflow,
# but use `actions-rs/grcov` only
- name: Pre-installing grcov
uses: actions-rs/install@v0.1
with:
crate: grcov
use-tool-cache: true
- name: Gather coverage data
id: coverage
uses: actions-rs/grcov@v0.1
with:
coveralls-token: ${{ secrets.COVERALLS_TOKEN }}
- name: Coveralls upload
uses: coverallsapp/github-action@master
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
parallel: true
path-to-lcov: ${{ steps.coverage.outputs.report }}
grcov_finalize:
runs-on: ubuntu-latest
needs: grcov
steps:
- name: Coveralls finalization
uses: coverallsapp/github-action@master
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
parallel-finished: true

View file

@ -1,110 +0,0 @@
# Based on https://github.com/actions-rs/meta/blob/master/recipes/msrv.md
on: [push, pull_request]
name: MSRV
jobs:
check:
name: Check
runs-on: ubuntu-latest
strategy:
matrix:
rust:
- stable
- 1.31.0
steps:
- name: Checkout sources
uses: actions/checkout@v2
- name: Install toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: ${{ matrix.rust }}
override: true
- name: Run cargo check
uses: actions-rs/cargo@v1
continue-on-error: true # WARNING: only for this example, remove it!
with:
command: check
test:
name: Test Suite
runs-on: ubuntu-latest
strategy:
matrix:
rust:
- stable
- 1.31.0
steps:
- name: Checkout sources
uses: actions/checkout@v2
- name: Install toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: ${{ matrix.rust }}
override: true
- name: Run cargo test
uses: actions-rs/cargo@v1
continue-on-error: true # WARNING: only for this example, remove it!
with:
command: test
fmt:
name: Rustfmt
runs-on: ubuntu-latest
strategy:
matrix:
rust:
- stable
- 1.31.0
steps:
- name: Checkout sources
uses: actions/checkout@v2
- name: Install toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: ${{ matrix.rust }}
override: true
- name: Install rustfmt
run: rustup component add rustfmt
- name: Run cargo fmt
uses: actions-rs/cargo@v1
continue-on-error: true # WARNING: only for this example, remove it!
with:
command: fmt
args: --all -- --check
clippy:
name: Clippy
runs-on: ubuntu-latest
strategy:
matrix:
rust:
- stable
- 1.31.0
steps:
- name: Checkout sources
uses: actions/checkout@v2
- name: Install toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: ${{ matrix.rust }}
override: true
- name: Install clippy
run: rustup component add clippy
- name: Run cargo clippy
uses: actions-rs/cargo@v1
continue-on-error: true # WARNING: only for this example, remove it!
with:
command: clippy
args: -- -D warnings

View file

@ -1,78 +0,0 @@
on: [push, pull_request]
name: Nightly lints
jobs:
clippy:
name: Clippy
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v2
- name: Install nightly toolchain with clippy available
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly
override: true
components: clippy
- name: Run cargo clippy
uses: actions-rs/cargo@v1
continue-on-error: true # WARNING: only for this example, remove it!
with:
command: clippy
args: -- -D warnings
rustfmt:
name: Format
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v2
- name: Install nightly toolchain with rustfmt available
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly
override: true
components: rustfmt
- name: Run cargo fmt
uses: actions-rs/cargo@v1
continue-on-error: true # WARNING: only for this example, remove it!
with:
command: fmt
args: --all -- --check
combo:
name: Clippy + rustfmt
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v2
- name: Install nightly toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly
override: true
components: rustfmt, clippy
- name: Run cargo fmt
uses: actions-rs/cargo@v1
continue-on-error: true # WARNING: only for this example, remove it!
with:
command: fmt
args: --all -- --check
- name: Run cargo clippy
uses: actions-rs/cargo@v1
continue-on-error: true # WARNING: only for this example, remove it!
with:
command: clippy
args: -- -D warnings

View file

@ -1,79 +0,0 @@
# Based on https://github.com/actions-rs/meta/blob/master/recipes/quickstart.md
#
# While our "example" application has the platform-specific code,
# for simplicity we are compiling and testing everything on the Ubuntu environment only.
# For multi-OS testing see the `cross.yml` workflow.
on: [push, pull_request]
name: Quickstart
jobs:
check:
name: Check
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v2
- name: Install stable toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- name: Run cargo check
uses: actions-rs/cargo@v1
continue-on-error: true # WARNING: only for this example, remove it!
with:
command: check
test:
name: Test Suite
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v2
- name: Install stable toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- name: Run cargo test
uses: actions-rs/cargo@v1
continue-on-error: true # WARNING: only for this example, remove it!
with:
command: test
lints:
name: Lints
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v2
- name: Install stable toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
components: rustfmt, clippy
- name: Run cargo fmt
uses: actions-rs/cargo@v1
continue-on-error: true # WARNING: only for this example, remove it!
with:
command: fmt
args: --all -- --check
- name: Run cargo clippy
uses: actions-rs/cargo@v1
continue-on-error: true # WARNING: only for this example, remove it!
with:
command: clippy
args: -- -D warnings

6
.gitignore vendored
View file

@ -3,7 +3,7 @@
*.jar *.jar
*jar *jar
*~ *~
**/*.rs.bk *.rs.bk
.s* .s*
.*.sw* .*.sw*
*.rs.bak *.rs.bak
@ -15,8 +15,6 @@
.lein-plugins/ .lein-plugins/
.lein-repl-history .lein-repl-history
.nrepl-port .nrepl-port
.bundle/
docs/vendor/
/.lein-* /.lein-*
/.nrepl-port /.nrepl-port
Cargo.lock Cargo.lock
@ -24,7 +22,7 @@ Cargo.lock
/classes/ /classes/
/node_modules/ /node_modules/
/out/ /out/
/target target/
pom.xml pom.xml
pom.xml.asc pom.xml.asc
/.cljs_node_repl/ /.cljs_node_repl/

View file

@ -1 +0,0 @@
docs/

View file

@ -1,80 +0,0 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
version: 0
allowPullRequests: public
tasks:
####################################################################################################
# Task: Pull requests
####################################################################################################
- provisionerId: '{{ taskcluster.docker.provisionerId }}'
workerType: '{{ taskcluster.docker.workerType }}'
extra:
github:
env: true
events:
- pull_request.opened
- pull_request.edited
- pull_request.synchronize
- pull_request.reopened
- push
scopes:
- "queue:create-task:aws-provisioner-v1/github-worker"
- "queue:scheduler-id:taskcluster-github"
payload:
maxRunTime: 3600
deadline: "{{ '2 hours' | $fromNow }}"
image: 'mozillamobile/android-components:1.4'
command:
- /bin/bash
- '--login'
- '-cx'
- >-
export TERM=dumb
&& git fetch {{ event.head.repo.url }} {{ event.head.repo.branch }}
&& git config advice.detachedHead false
&& git checkout {{event.head.sha}}
&& python automation/taskcluster/decision_task_pull_request.py
features:
taskclusterProxy: true
metadata:
name: Mentat Android SDK - Pull Request
description: Building and testing the Mentat Android SDK - triggered by a pull request.
owner: '{{ event.head.user.email }}'
source: '{{ event.head.repo.url }}'
####################################################################################################
# Task: Release
####################################################################################################
- provisionerId: '{{ taskcluster.docker.provisionerId }}'
workerType: '{{ taskcluster.docker.workerType }}'
extra:
github:
events:
- release
scopes:
- "secrets:get:project/mentat/publish"
payload:
maxRunTime: 3600
deadline: "{{ '2 hours' | $fromNow }}"
image: 'mozillamobile/mentat:1.2'
command:
- /bin/bash
- '--login'
- '-cx'
- >-
export TERM=dumb
&& git fetch origin --tags
&& git config advice.detachedHead false
&& git checkout {{ event.version }}
&& python automation/taskcluster/release/fetch-bintray-api-key.py
&& cd sdks/android/Mentat
&& ./gradlew --no-daemon clean library:assembleRelease
&& VCS_TAG=`git show-ref {{ event.version }}` ./gradlew bintrayUpload --debug -PvcsTag="$VCS_TAG"
features:
taskclusterProxy: true
metadata:
name: Mentat Android SDK - Release ({{ event.version }})
description: Building and publishing release versions.
owner: '{{ event.head.user.email }}'
source: '{{ event.head.repo.url }}'

View file

@ -1,53 +1,11 @@
language: rust language: rust
env:
- CARGO_INCREMENTAL=0
# https://bheisler.github.io/post/efficient-use-of-travis-ci-cache-for-rust/
before_cache:
# Delete loose files in the debug directory
- find ./target/debug -maxdepth 1 -type f -delete
# Delete the test and benchmark executables. Finding these all might take some
# experimentation.
- rm -rf ./target/debug/deps/criterion*
- rm -rf ./target/debug/deps/bench*
# Delete the associated metadata files for those executables
- rm -rf ./target/debug/.fingerprint/criterion*
- rm -rf ./target/debug/.fingerprint/bench*
# Note that all of the above need to be repeated for `release/` instead of
# `debug/` if your build script builds artifacts in release mode.
# This is just more metadata
- rm -f ./target/.rustc_info.json
# Also delete the saved benchmark data from the test benchmarks. If you
# have Criterion.rs benchmarks, you'll probably want to do this as well, or set
# the CRITERION_HOME environment variable to move that data out of the
# `target/` directory.
- rm -rf ./target/criterion
# Also delete cargo's registry index. This is updated on every build, but it's
# way cheaper to re-download than the whole cache is.
- rm -rf "$TRAVIS_HOME/.cargo/registry/index/"
- rm -rf "$TRAVIS_HOME/.cargo/registry/src"
cache:
directories:
- ./target
- $TRAVIS_HOME/.cache/sccache
- $TRAVIS_HOME/.cargo/
- $TRAVIS_HOME/.rustup/
before_script:
- cargo install --force cargo-audit
- cargo generate-lockfile
- rustup component add clippy-preview
script:
- cargo audit
# We use OSX so that we can get a reasonably up to date version of SQLCipher. # We use OSX so that we can get a reasonably up to date version of SQLCipher.
# (The version in Travis's default Ubuntu Trusty is much too old). # (The version in Travis's default Ubuntu Trusty is much too old).
os: osx os: osx
before_install: before_install:
- brew install sqlcipher - brew install sqlcipher --with-fts
rust: rust:
- 1.43.0 - 1.25.0 # Must align with `build/version.rs`.
- 1.44.0
- 1.45.0
- 1.46.0
- 1.47.0
- stable - stable
- beta - beta
- nightly - nightly
@ -55,24 +13,14 @@ matrix:
allow_failures: allow_failures:
- rust: nightly - rust: nightly
fast_finish: true fast_finish: true
jobs:
include:
- stage: "Test iOS"
rust: 1.47.0
script: ./scripts/test-ios.sh
- stage: "Docs"
rust: 1.47.0
script: ./scripts/cargo-doc.sh
script: script:
- cargo build --verbose --all
- cargo clippy --all-targets --all-features -- -D warnings -A clippy::comparison-chain -A clippy::many-single-char-names # Check tests and non-default crate features.
- cargo test --verbose --all - cargo test --verbose --all
- cargo test --features edn/serde_support --verbose --all
# We can't pick individual features out with `cargo test --all` (At the time of this writing, this # We can't pick individual features out with `cargo test --all` (At the time of this writing, this
# works but does the wrong thing because of a bug in cargo, but its fix will be to disallow doing # works but does the wrong thing because of a bug in cargo, but its fix will be to disallow doing
# this all-together, see https://github.com/rust-lang/cargo/issues/5364 for more information). To # this all-together, see https://github.com/rust-lang/cargo/issues/5364 for more information). To
# work around this, we run tests individually for sub-crates that rely on `rusqlite`. # work around this, we run individual tests for each subcrate individually.
- | - |
for crate in "" "db" "db-traits" "ffi" "public-traits" "query-projector" "query-projector-traits" "query-pull" "sql" "tolstoy" "tolstoy-traits" "transaction" "tools/cli"; do for manifest in $(find . -type f -name Cargo.toml); do
cargo test --manifest-path ./$crate/Cargo.toml --verbose --no-default-features --features sqlcipher cargo test --manifest-path $manifest --verbose --no-default-features --features sqlcipher
done done
cache: cargo

View file

@ -1,39 +0,0 @@
# 0.11.1 (2018-08-09)
* sdks/android compiled against:
* Kotlin standard library 1.2.41
* **API changes**: Changed wording of MentatError::ConflictingAttributeDefinitions, MentatError::ExistingVocabularyTooNew, MentatError::UnexpectedCoreSchema.
* [Commits](https://github.com/mozilla/mentat/compare/v0.11.0...v0.11.1)
# 0.11 (2018-07-31)
* sdks/android compiled against:
* Kotlin standard library 1.2.41
* **sdks/android**: `Mentat()` constructor replaced with `open` factory method.
* [Commits](https://github.com/mozilla/mentat/compare/v0.10.0...v0.11.0)
# 0.10 (2018-07-26)
* sdks/android compiled against:
* Kotlin standard library 1.2.41
* **API changes**:
* `store_open{_encrypted}` now accepts an error parameter; corresponding constructors changed to be factory functions.
* [Commits](https://github.com/mozilla/mentat/compare/v0.9.0...v0.10.0)
# 0.9 (2018-07-25)
* sdks/android compiled against:
* Kotlin standard library 1.2.41
* **API changes**:
* Mentat partitions now enforce their integrity, denying entids that aren't already known.
* **sdks/android**: First version published to nalexander's personal bintray repository.
* Various bugfixes and refactorings (see commits below for details)
* [Commits](https://github.com/mozilla/mentat/compare/v0.8.1...v0.9.0)

View file

@ -1,5 +1,4 @@
[package] [package]
edition = "2021"
authors = [ authors = [
"Richard Newman <rnewman@twinql.com>", "Richard Newman <rnewman@twinql.com>",
"Nicholas Alexander <nalexander@mozilla.com>", "Nicholas Alexander <nalexander@mozilla.com>",
@ -11,57 +10,38 @@ authors = [
"Kit Cambridge <kit@yakshaving.ninja>", "Kit Cambridge <kit@yakshaving.ninja>",
"Edouard Oger <eoger@fastmail.com>", "Edouard Oger <eoger@fastmail.com>",
"Thom Chiovoloni <tchiovoloni@mozilla.com>", "Thom Chiovoloni <tchiovoloni@mozilla.com>",
"Gregory Burd <greg@burd.me>",
] ]
name = "mentat" name = "mentat"
version = "0.14.0" version = "0.7.0"
build = "build/version.rs" build = "build/version.rs"
[features] [features]
default = ["bundled_sqlite3", "syncable"] default = ["bundled_sqlite3"]
bundled_sqlite3 = ["rusqlite/bundled"] bundled_sqlite3 = ["rusqlite/bundled"]
sqlcipher = ["rusqlite/sqlcipher", "mentat_db/sqlcipher"] sqlcipher = ["rusqlite/sqlcipher", "mentat_db/sqlcipher"]
syncable = ["mentat_tolstoy", "tolstoy_traits", "mentat_db/syncable"]
[workspace] [workspace]
members = [ members = ["tools/cli", "ffi"]
"tools/cli",
"ffi", "core", "core-traits","db", "db-traits", "edn", "public-traits", "query-algebrizer",
"query-algebrizer-traits", "query-projector", "query-projector-traits","query-pull",
"query-sql", "sql", "sql-traits", "tolstoy-traits", "tolstoy", "transaction"
]
[build-dependencies] [build-dependencies]
rustc_version = "~0.4" rustc_version = "0.2"
[dev-dependencies]
assert_approx_eq = "~1.1"
#[dev-dependencies.cargo-husky]
#version = "1"
#default-features = false # Disable features which are enabled by default
#features = ["run-for-all", "precommit-hook", "run-cargo-fmt", "run-cargo-test", "run-cargo-check", "run-cargo-clippy"]
#cargo audit
#cargo outdated
[dependencies] [dependencies]
chrono = "~0.4" chrono = "0.4"
failure = "~0.1" failure = "0.1.1"
lazy_static = "~1.4" failure_derive = "0.1.1"
time = "0.3.1" lazy_static = "0.2"
log = "~0.4" time = "0.1"
uuid = { version = "~1", features = ["v4", "serde"] } uuid = { version = "0.5", features = ["v4", "serde"] }
[dependencies.rusqlite] [dependencies.rusqlite]
version = "~0.29" version = "0.13"
features = ["limits", "bundled"] # System sqlite might be very old.
features = ["limits"]
[dependencies.edn] [dependencies.edn]
path = "edn" path = "edn"
[dependencies.core_traits]
path = "core-traits"
[dependencies.mentat_core] [dependencies.mentat_core]
path = "core" path = "core"
@ -71,46 +51,26 @@ path = "sql"
[dependencies.mentat_db] [dependencies.mentat_db]
path = "db" path = "db"
[dependencies.db_traits] [dependencies.mentat_query]
path = "db-traits" path = "query"
[dependencies.mentat_query_algebrizer] [dependencies.mentat_query_algebrizer]
path = "query-algebrizer" path = "query-algebrizer"
[dependencies.query_algebrizer_traits]
path = "query-algebrizer-traits"
[dependencies.mentat_query_projector] [dependencies.mentat_query_projector]
path = "query-projector" path = "query-projector"
[dependencies.query_projector_traits]
path = "query-projector-traits"
[dependencies.mentat_query_pull] [dependencies.mentat_query_pull]
path = "query-pull" path = "query-pull"
[dependencies.query_pull_traits]
path = "query-pull-traits"
[dependencies.mentat_query_sql] [dependencies.mentat_query_sql]
path = "query-sql" path = "query-sql"
[dependencies.sql_traits] [dependencies.mentat_query_translator]
path = "sql-traits" path = "query-translator"
[dependencies.public_traits]
path = "public-traits"
[dependencies.mentat_transaction]
path = "transaction"
[dependencies.mentat_tolstoy] [dependencies.mentat_tolstoy]
path = "tolstoy" path = "tolstoy"
optional = true
[dependencies.tolstoy_traits]
path = "tolstoy-traits"
optional = true
[profile.release] [profile.release]
opt-level = 3 opt-level = 3

View file

@ -1,11 +0,0 @@
.PHONY: outdated fix
outdated:
for p in $(dirname $(ls Cargo.toml */Cargo.toml */*/Cargo.toml)); do echo $p; (cd $p; cargo outdated -R); done
fix:
$(for p in $(dirname $(ls Cargo.toml */Cargo.toml */*/Cargo.toml)); do echo $p; (cd $p; cargo fix --allow-dirty --broken-code --edition-idioms); done)
upgrades:
cargo upgrades

29
NOTES
View file

@ -1,29 +0,0 @@
* sqlite -> monetdb-lite-c + fts5 + bayesdb
* fts5 + regex + tre/fuzzy + codesearch/trigram filters, streaming bloom filters https://arxiv.org/abs/2001.03147
* datalog to "goblin relational engine" (gtk)
* branching distributed wal (chain replication) and CRDTs
* alf:fn query language
* datatypes via bit syntax+some code?
* pure lang?
* https://github.com/dahjelle/pouch-datalog
* https://github.com/edn-query-language/eql
* https://github.com/borkdude/jet
* https://github.com/walmartlabs/dyn-edn
* https://github.com/go-edn/edn
* https://github.com/smothers/cause
* https://github.com/oscaro/eq
* https://github.com/clojure-emacs/parseedn
* https://github.com/exoscale/seql
* https://github.com/axboe/liburing
* (EAVtf) - entity attribute value type flags
* distributed, replicated WAL
* https://github.com/mirage/irmin
* What if facts had "confidence" [0-1)?
* entity attribute value type flags
* https://github.com/probcomp/BayesDB
* https://github.com/probcomp/bayeslite
* http://probcomp.csail.mit.edu/software/bayesdb/

View file

@ -1,19 +1,18 @@
# Project Mentat # Project Mentat
[![Build Status](https://travis-ci.org/qpdb/mentat.svg?branch=master)](https://travis-ci.org/qpdb/mentat)
Project Mentat is a persistent, embedded knowledge base. It draws heavily on [DataScript](https://github.com/tonsky/datascript) and [Datomic](http://datomic.com). Project Mentat is a persistent, embedded knowledge base. It draws heavily on [DataScript](https://github.com/tonsky/datascript) and [Datomic](http://datomic.com).
This project was started by Mozilla, but [is no longer being developed or actively maintained by them](https://mail.mozilla.org/pipermail/firefox-dev/2018-September/006780.html). [Their repository](https://github.com/mozilla/mentat) was marked read-only, [this fork](https://github.com/qpdb/mentat) is an attempt to revive and continue that interesting work. We owe the team at Mozilla more than words can express for inspiring us all and for this project in particular. Mentat is implemented in Rust.
*Thank you*. The first version of Project Mentat, named Datomish, [was written in ClojureScript](https://github.com/mozilla/mentat/tree/clojure), targeting both Node (on top of `promise_sqlite`) and Firefox (on top of `Sqlite.jsm`). It also worked in pure Clojure on the JVM on top of `jdbc-sqlite`. The name was changed to avoid confusion with [Datomic](http://datomic.com).
[Documentation](https://docs.rs/mentat) The Rust implementation gives us a smaller compiled output, better performance, more type safety, better tooling, and easier deployment into Firefox and mobile platforms.
--- ---
## Motivation ## Motivation
Mentat is a flexible relational (not key-value, not document-oriented) store that makes it easy to describe, grow, and reuse your domain schema. Mentat is intended to be a flexible relational (not key-value, not document-oriented) store that makes it easy to describe, grow, and reuse your domain schema.
By abstracting away the storage schema, and by exposing change listeners outside the database (not via triggers), we hope to make domain schemas stable, and allow both the data store itself and embedding applications to use better architectures, meeting performance goals in a way that allows future evolution. By abstracting away the storage schema, and by exposing change listeners outside the database (not via triggers), we hope to make domain schemas stable, and allow both the data store itself and embedding applications to use better architectures, meeting performance goals in a way that allows future evolution.
@ -73,11 +72,9 @@ We've observed that data storage is a particular area of difficulty for software
DataScript asks the question: "What if creating a database were as cheap as creating a Hashmap?" DataScript asks the question: "What if creating a database were as cheap as creating a Hashmap?"
Mentat is not interested in that. Instead, it's focused on persistence and performance, with very little interest in immutable databases/databases as values or throwaway use. Mentat is not interested in that. Instead, it's strongly interested in persistence and performance, with very little interest in immutable databases/databases as values or throwaway use.
One might say that Mentat's question is: "What if a database could store arbitrary relations, for arbitrary consumers, without them having to coordinate an up-front storage-level schema?" One might say that Mentat's question is: "What if an SQLite database could store arbitrary relations, for arbitrary consumers, without them having to coordinate an up-front storage-level schema?"
Consider this a practical approach to facts, to knowledge its storage and access, much like SQLite is a practical RDBMS.
(Note that [domain-level schemas are very valuable](http://martinfowler.com/articles/schemaless/).) (Note that [domain-level schemas are very valuable](http://martinfowler.com/articles/schemaless/).)
@ -87,7 +84,7 @@ Some thought has been given to how databases as values — long-term references
Just like DataScript, Mentat speaks Datalog for querying and takes additions and retractions as input to a transaction. Just like DataScript, Mentat speaks Datalog for querying and takes additions and retractions as input to a transaction.
Unlike DataScript, Mentat exposes free-text indexing, thanks to SQLite/FTS. Unlike DataScript, Mentat exposes free-text indexing, thanks to SQLite.
## Comparison to Datomic ## Comparison to Datomic
@ -96,6 +93,8 @@ Datomic is a server-side, enterprise-grade data storage system. Datomic has a be
Many of these design decisions are inapplicable to deployed desktop software; indeed, the use of multiple JVM processes makes Datomic's use in a small desktop app, or a mobile device, prohibitive. Many of these design decisions are inapplicable to deployed desktop software; indeed, the use of multiple JVM processes makes Datomic's use in a small desktop app, or a mobile device, prohibitive.
Mentat was designed for embedding, initially in an experimental Electron app ([Tofino](https://github.com/mozilla/tofino)). It is less concerned with exposing consistent database states outside transaction boundaries, because that's less important here, and dropping some of these requirements allows us to leverage SQLite itself.
## Comparison to SQLite ## Comparison to SQLite
@ -208,7 +207,7 @@ The top-level main crate of Mentat assembles these component crates into somethi
### Syncing ### Syncing
Sync code lives, for [referential reasons](https://engineering.linkedin.com/distributed-systems/log-what-every-software-engineer-should-know-about-real-time-datas-unifying), in a crate named `tolstoy`. This code is a work in progress; current state is a proof-of-concept implementation which largely relies on the internal transactor to make progress in most cases and comes with a basic support for timelines. See [Tolstoy's documentation](https://github.com/mozilla/mentat/tree/master/tolstoy/README.md) for details. Sync code lives, for [referential reasons](https://engineering.linkedin.com/distributed-systems/log-what-every-software-engineer-should-know-about-real-time-datas-unifying), in a crate named `tolstoy`. This code is a work in progress.
### The command-line interface ### The command-line interface

View file

@ -1,36 +0,0 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
FROM mozillamobile/rust-component:buildtools-27.0.3-ndk-r17b-ndk-version-26-rust-stable-rust-beta
MAINTAINER Nick Alexander "nalexander@mozilla.com"
#----------------------------------------------------------------------------------------------------------------------
#-- Project -----------------------------------------------------------------------------------------------------------
#----------------------------------------------------------------------------------------------------------------------
ENV PROJECT_REPOSITORY "https://github.com/mozilla/mentat.git"
RUN git clone $PROJECT_REPOSITORY
WORKDIR /build/mentat
# Temporary.
RUN git fetch origin master && git checkout origin/generic-automation-images && git show-ref HEAD
# Populate dependencies.
RUN ./sdks/android/Mentat/gradlew --no-daemon -p sdks/android/Mentat tasks
# Build Rust.
RUN ./sdks/android/Mentat/gradlew --no-daemon -p sdks/android/Mentat cargoBuild
# Actually build. In the future, we might also lint (to cache additional dependencies).
RUN ./sdks/android/Mentat/gradlew --no-daemon -p sdks/android/Mentat assemble test
#----------------------------------------------------------------------------------------------------------------------
# -- Cleanup ----------------------------------------------------------------------------------------------------------
#----------------------------------------------------------------------------------------------------------------------
# Drop built Rust artifacts.
RUN cargo clean

View file

@ -1,81 +0,0 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
FROM mozillamobile/android-components:1.4
MAINTAINER Nick Alexander "nalexander@mozilla.com"
#----------------------------------------------------------------------------------------------------------------------
#-- Configuration -----------------------------------------------------------------------------------------------------
#----------------------------------------------------------------------------------------------------------------------
ENV ANDROID_NDK_VERSION "r17b"
#----------------------------------------------------------------------------------------------------------------------
#-- System ------------------------------------------------------------------------------------------------------------
#----------------------------------------------------------------------------------------------------------------------
RUN apt-get update -qq
#----------------------------------------------------------------------------------------------------------------------
#-- Android NDK (Android SDK comes from base `android-components` image) ----------------------------------------------
#----------------------------------------------------------------------------------------------------------------------
RUN mkdir -p /build
WORKDIR /build
ENV ANDROID_NDK_HOME /build/android-ndk
RUN curl -L https://dl.google.com/android/repository/android-ndk-${ANDROID_NDK_VERSION}-linux-x86_64.zip > ndk.zip \
&& unzip ndk.zip -d /build \
&& rm ndk.zip \
&& mv /build/android-ndk-${ANDROID_NDK_VERSION} ${ANDROID_NDK_HOME}
ENV ANDROID_NDK_TOOLCHAIN_DIR /build/android-ndk-toolchain
ENV ANDROID_NDK_API_VERSION 26
RUN set -eux; \
python "$ANDROID_NDK_HOME/build/tools/make_standalone_toolchain.py" --arch="arm" --api="$ANDROID_NDK_API_VERSION" --install-dir="$ANDROID_NDK_TOOLCHAIN_DIR/arm-$ANDROID_NDK_API_VERSION" --force; \
python "$ANDROID_NDK_HOME/build/tools/make_standalone_toolchain.py" --arch="arm64" --api="$ANDROID_NDK_API_VERSION" --install-dir="$ANDROID_NDK_TOOLCHAIN_DIR/arm64-$ANDROID_NDK_API_VERSION" --force; \
python "$ANDROID_NDK_HOME/build/tools/make_standalone_toolchain.py" --arch="x86" --api="$ANDROID_NDK_API_VERSION" --install-dir="$ANDROID_NDK_TOOLCHAIN_DIR/x86-$ANDROID_NDK_API_VERSION" --force
#----------------------------------------------------------------------------------------------------------------------
#-- Rust (cribbed from https://github.com/rust-lang-nursery/docker-rust/blob/ced83778ec6fea7f63091a484946f95eac0ee611/1.27.1/stretch/Dockerfile)
#-- Rust is after the Android NDK since Rust rolls forward more frequently. Both stable and beta for advanced consumers.
#----------------------------------------------------------------------------------------------------------------------
ENV RUSTUP_HOME=/usr/local/rustup \
CARGO_HOME=/usr/local/cargo \
PATH=/usr/local/cargo/bin:$PATH \
RUST_VERSION=1.27.1
RUN set -eux; \
rustArch='x86_64-unknown-linux-gnu'; rustupSha256='4d382e77fd6760282912d2d9beec5e260ec919efd3cb9bdb64fe1207e84b9d91'; \
url="https://static.rust-lang.org/rustup/archive/1.12.0/${rustArch}/rustup-init"; \
wget "$url"; \
echo "${rustupSha256} *rustup-init" | sha256sum -c -; \
chmod +x rustup-init; \
./rustup-init -y --no-modify-path --default-toolchain $RUST_VERSION; \
rm rustup-init; \
chmod -R a+w $RUSTUP_HOME $CARGO_HOME; \
rustup --version; \
cargo --version; \
rustc --version; \
rustup target add i686-linux-android; \
rustup target add armv7-linux-androideabi; \
rustup target add aarch64-linux-android
RUN set -eux; \
rustup install beta; \
rustup target add --toolchain beta i686-linux-android; \
rustup target add --toolchain beta armv7-linux-androideabi; \
rustup target add --toolchain beta aarch64-linux-android
#----------------------------------------------------------------------------------------------------------------------
# -- Cleanup ----------------------------------------------------------------------------------------------------------
#----------------------------------------------------------------------------------------------------------------------
WORKDIR /build
RUN apt-get clean

View file

@ -1,122 +0,0 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import datetime
import json
import os
import taskcluster
import re
import subprocess
import sys
"""
Decision task for pull requests
"""
TASK_ID = os.environ.get('TASK_ID')
REPO_URL = os.environ.get('GITHUB_HEAD_REPO_URL')
BRANCH = os.environ.get('GITHUB_HEAD_BRANCH')
COMMIT = os.environ.get('GITHUB_HEAD_SHA')
def fetch_module_names():
process = subprocess.Popen(["./gradlew", "--no-daemon", "printModules"], stdout=subprocess.PIPE,
cwd=os.path.join(os.getcwd(), "sdks", "android", "Mentat"))
(output, err) = process.communicate()
exit_code = process.wait()
if exit_code is not 0:
print "Gradle command returned error:", exit_code
return re.findall('module: (.*)', output, re.M)
def schedule_task(queue, taskId, task):
print "TASK", taskId
print json.dumps(task, indent=4, separators=(',', ': '))
result = queue.createTask(taskId, task)
print "RESULT", taskId
print json.dumps(result, indent=4, separators=(',', ': '))
def create_task(name, description, command):
created = datetime.datetime.now()
expires = taskcluster.fromNow('1 year')
deadline = taskcluster.fromNow('1 day')
return {
"workerType": 'github-worker',
"taskGroupId": TASK_ID,
"expires": taskcluster.stringDate(expires),
"retries": 5,
"created": taskcluster.stringDate(created),
"tags": {},
"priority": "lowest",
"schedulerId": "taskcluster-github",
"deadline": taskcluster.stringDate(deadline),
"dependencies": [ TASK_ID ],
"routes": [],
"scopes": [],
"requires": "all-completed",
"payload": {
"features": {},
"maxRunTime": 7200,
"image": "mozillamobile/mentat:1.2",
"command": [
"/bin/bash",
"--login",
"-cx",
"export TERM=dumb && git fetch %s %s && git config advice.detachedHead false && git checkout %s && cd sdks/android/Mentat && ./gradlew --no-daemon clean %s" % (REPO_URL, BRANCH, COMMIT, command)
],
"artifacts": {},
"deadline": taskcluster.stringDate(deadline)
},
"provisionerId": "aws-provisioner-v1",
"metadata": {
"name": name,
"description": description,
"owner": "nalexander@mozilla.com",
"source": "https://github.com/mozilla/mentat"
}
}
def create_module_task(module):
return create_task(
name='Mentat Android SDK - Module ' + module,
description='Building and testing module ' + module,
command=" ".join(map(lambda x: module + ":" + x, ['assemble', 'test', 'lint'])))
# def create_detekt_task():
# return create_task(
# name='Android Components - detekt',
# description='Running detekt over all modules',
# command='detektCheck')
# def create_ktlint_task():
# return create_task(
# name='Android Components - ktlint',
# description='Running ktlint over all modules',
# command='ktlint')
if __name__ == "__main__":
queue = taskcluster.Queue({ 'baseUrl': 'http://taskcluster/queue/v1' })
modules = fetch_module_names()
if len(modules) == 0:
print "Could not get module names from gradle"
sys.exit(2)
for module in modules:
task = create_module_task(module)
task_id = taskcluster.slugId()
schedule_task(queue, task_id, task)
# schedule_task(queue, taskcluster.slugId(), create_detekt_task())
# schedule_task(queue, taskcluster.slugId(), create_ktlint_task())

View file

@ -1,28 +0,0 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import os
import taskcluster
SECRET_NAME = 'project/mentat/publish'
TASKCLUSTER_BASE_URL = 'http://taskcluster/secrets/v1'
def fetch_publish_secrets(secret_name):
"""Fetch and return secrets from taskcluster's secret service"""
secrets = taskcluster.Secrets({'baseUrl': TASKCLUSTER_BASE_URL})
return secrets.get(secret_name)
def main():
"""Fetch the bintray user and api key from taskcluster's secret service
and save it to local.properties in the project root directory.
"""
data = fetch_publish_secrets(SECRET_NAME)
properties_file_path = os.path.join(os.path.dirname(__file__), '../../../sdks/android/Mentat/local.properties')
with open(properties_file_path, 'w') as properties_file:
properties_file.write("bintray.user=%s\n" % data['secret']['bintray_user'])
properties_file.write("bintray.apikey=%s\n" % data['secret']['bintray_apikey'])
if __name__ == "__main__":
main()

View file

@ -8,25 +8,24 @@
// CONDITIONS OF ANY KIND, either express or implied. See the License for the // CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License. // specific language governing permissions and limitations under the License.
use rustc_version::{version, Version}; extern crate rustc_version;
use std::io::{self, Write}; use std::io::{self, Write};
use std::process::exit; use std::process::exit;
use rustc_version::{
Version,
version,
};
/// MIN_VERSION should be changed when there's a new minimum version of rustc required /// MIN_VERSION should be changed when there's a new minimum version of rustc required
/// to build the project. /// to build the project.
static MIN_VERSION: &str = "1.69.0"; static MIN_VERSION: &'static str = "1.25.0";
fn main() { fn main() {
let ver = version().unwrap(); let ver = version().unwrap();
let min = Version::parse(MIN_VERSION).unwrap(); let min = Version::parse(MIN_VERSION).unwrap();
if ver < min { if ver < min {
writeln!( writeln!(&mut io::stderr(), "Mentat requires rustc {} or higher.", MIN_VERSION).unwrap();
&mut io::stderr(),
"Mentat requires rustc {} or higher, you were using version {}.",
MIN_VERSION,
ver
)
.unwrap();
exit(1); exit(1);
} }
} }

View file

@ -1,23 +0,0 @@
[package]
name = "core_traits"
version = "0.0.2"
workspace = ".."
[lib]
name = "core_traits"
path = "lib.rs"
[dependencies]
chrono = { version = "~0.4", features = ["serde"] }
enum-set = "~0.0.8"
lazy_static = "~1.4"
indexmap = "~1.9"
ordered-float = { version = "~2.8", features = ["serde"] }
uuid = { version = "~1", features = ["v4", "serde"] }
serde = { version = "~1.0", features = ["rc"] }
serde_derive = "~1.0"
bytes = { version = "1.0.1", features = ["serde"] }
[dependencies.edn]
path = "../edn"
features = ["serde_support"]

File diff suppressed because it is too large Load diff

View file

@ -1,18 +1,19 @@
[package] [package]
name = "mentat_core" name = "mentat_core"
version = "0.0.2" version = "0.0.1"
workspace = ".." workspace = ".."
[dependencies] [dependencies]
chrono = { version = "~0.4", features = ["serde"] } chrono = { version = "0.4", features = ["serde"] }
enum-set = "~0.0" enum-set = { git = "https://github.com/rnewman/enum-set" }
failure = "~0.1" failure = "0.1.1"
indexmap = "~1.9" indexmap = "1"
ordered-float = { version = "~2.8", features = ["serde"] } lazy_static = "0.2"
uuid = { version = "~1", features = ["v4", "serde"] } num = "0.1"
ordered-float = { version = "0.5", features = ["serde"] }
[dependencies.core_traits] uuid = { version = "0.5", features = ["v4", "serde"] }
path = "../core-traits" serde = { version = "1.0", features = ["rc"] }
serde_derive = "1.0"
[dependencies.edn] [dependencies.edn]
path = "../edn" path = "../edn"

View file

@ -9,41 +9,31 @@
// specific language governing permissions and limitations under the License. // specific language governing permissions and limitations under the License.
/// Cache traits. /// Cache traits.
use std::collections::BTreeSet;
use core_traits::{Entid, TypedValue}; use std::collections::{
BTreeSet,
};
use crate::Schema; use ::{
Entid,
Schema,
TypedValue,
};
pub trait CachedAttributes { pub trait CachedAttributes {
fn is_attribute_cached_reverse(&self, entid: Entid) -> bool; fn is_attribute_cached_reverse(&self, entid: Entid) -> bool;
fn is_attribute_cached_forward(&self, entid: Entid) -> bool; fn is_attribute_cached_forward(&self, entid: Entid) -> bool;
fn has_cached_attributes(&self) -> bool; fn has_cached_attributes(&self) -> bool;
fn get_values_for_entid( fn get_values_for_entid(&self, schema: &Schema, attribute: Entid, entid: Entid) -> Option<&Vec<TypedValue>>;
&self, fn get_value_for_entid(&self, schema: &Schema, attribute: Entid, entid: Entid) -> Option<&TypedValue>;
schema: &Schema,
attribute: Entid,
entid: Entid,
) -> Option<&Vec<TypedValue>>;
fn get_value_for_entid(
&self,
schema: &Schema,
attribute: Entid,
entid: Entid,
) -> Option<&TypedValue>;
/// Reverse lookup. /// Reverse lookup.
fn get_entid_for_value(&self, attribute: Entid, value: &TypedValue) -> Option<Entid>; fn get_entid_for_value(&self, attribute: Entid, value: &TypedValue) -> Option<Entid>;
fn get_entids_for_value( fn get_entids_for_value(&self, attribute: Entid, value: &TypedValue) -> Option<&BTreeSet<Entid>>;
&self,
attribute: Entid,
value: &TypedValue,
) -> Option<&BTreeSet<Entid>>;
} }
pub trait UpdateableCache<E> { pub trait UpdateableCache<E> {
fn update<I>(&mut self, schema: &Schema, retractions: I, assertions: I) -> Result<(), E> fn update<I>(&mut self, schema: &Schema, retractions: I, assertions: I) -> Result<(), E>
where where I: Iterator<Item=(Entid, Entid, TypedValue)>;
I: Iterator<Item = (Entid, Entid, TypedValue)>;
} }

View file

@ -11,7 +11,7 @@
use std::cell::Cell; use std::cell::Cell;
use std::rc::Rc; use std::rc::Rc;
#[derive(Clone, Default)] #[derive(Clone)]
pub struct RcCounter { pub struct RcCounter {
c: Rc<Cell<usize>>, c: Rc<Cell<usize>>,
} }
@ -19,15 +19,11 @@ pub struct RcCounter {
/// A simple shared counter. /// A simple shared counter.
impl RcCounter { impl RcCounter {
pub fn with_initial(value: usize) -> Self { pub fn with_initial(value: usize) -> Self {
RcCounter { RcCounter { c: Rc::new(Cell::new(value)) }
c: Rc::new(Cell::new(value)),
}
} }
pub fn new() -> Self { pub fn new() -> Self {
RcCounter { RcCounter { c: Rc::new(Cell::new(0)) }
c: Rc::new(Cell::new(0)),
}
} }
/// Return the next value in the sequence. /// Return the next value in the sequence.

View file

@ -14,39 +14,233 @@ extern crate failure;
extern crate indexmap; extern crate indexmap;
extern crate ordered_float; extern crate ordered_float;
extern crate uuid; extern crate uuid;
extern crate serde;
extern crate core_traits; #[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate serde_derive;
extern crate edn; extern crate edn;
use core_traits::{Attribute, Entid, KnownEntid, ValueType}; pub mod values;
mod cache; mod cache;
use std::collections::BTreeMap; use std::collections::{
BTreeMap,
};
pub use uuid::Uuid; pub use uuid::Uuid;
pub use chrono::{ pub use chrono::{
DateTime, DateTime,
Timelike, // For truncation. Timelike, // For truncation.
}; };
pub use edn::parse::parse_query; pub use edn::{
pub use edn::{Cloned, FromMicros, FromRc, Keyword, ToMicros, Utc, ValueRc}; Cloned,
FromMicros,
FromRc,
Keyword,
ToMicros,
Utc,
ValueRc,
};
pub use crate::cache::{CachedAttributes, UpdateableCache}; pub use edn::parse::{
parse_query,
ParseError as EdnParseError,
};
pub use cache::{
CachedAttributes,
UpdateableCache,
};
mod sql_types;
mod tx_report;
/// Core types defining a Mentat knowledge base. /// Core types defining a Mentat knowledge base.
mod types; mod types;
mod tx_report;
mod value_type_set;
mod sql_types;
pub use crate::tx_report::TxReport; pub use tx_report::{
TxReport,
};
pub use crate::types::ValueTypeTag; pub use types::{
Binding,
Entid,
KnownEntid,
StructuredMap,
TypedValue,
ValueType,
ValueTypeTag,
now,
};
pub use crate::sql_types::{SQLTypeAffinity, SQLValueType, SQLValueTypeSet}; pub use value_type_set::{
ValueTypeSet,
};
pub use sql_types::{
SQLTypeAffinity,
SQLValueType,
SQLValueTypeSet,
};
/// Bit flags used in `flags0` column in temporary tables created during search,
/// such as the `search_results`, `inexact_searches` and `exact_searches` tables.
/// When moving to a more concrete table, such as `datoms`, they are expanded out
/// via these flags and put into their own column rather than a bit field.
pub enum AttributeBitFlags {
IndexAVET = 1 << 0,
IndexVAET = 1 << 1,
IndexFulltext = 1 << 2,
UniqueValue = 1 << 3,
}
pub mod attribute {
use TypedValue;
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialOrd, PartialEq)]
pub enum Unique {
Value,
Identity,
}
impl Unique {
// This is easier than rejigging DB_UNIQUE_VALUE to not be EDN.
pub fn into_typed_value(self) -> TypedValue {
match self {
Unique::Value => TypedValue::typed_ns_keyword("db.unique", "value"),
Unique::Identity => TypedValue::typed_ns_keyword("db.unique", "identity"),
}
}
}
}
/// A Mentat schema attribute has a value type and several other flags determining how assertions
/// with the attribute are interpreted.
///
/// TODO: consider packing this into a bitfield or similar.
#[derive(Clone,Debug,Eq,Hash,Ord,PartialOrd,PartialEq)]
pub struct Attribute {
/// The associated value type, i.e., `:db/valueType`?
pub value_type: ValueType,
/// `true` if this attribute is multi-valued, i.e., it is `:db/cardinality
/// :db.cardinality/many`. `false` if this attribute is single-valued (the default), i.e., it
/// is `:db/cardinality :db.cardinality/one`.
pub multival: bool,
/// `None` if this attribute is neither unique-value nor unique-identity.
///
/// `Some(attribute::Unique::Value)` if this attribute is unique-value, i.e., it is `:db/unique
/// :db.unique/value`.
///
/// *Unique-value* means that there is at most one assertion with the attribute and a
/// particular value in the datom store. Unique-value attributes can be used in lookup-refs.
///
/// `Some(attribute::Unique::Identity)` if this attribute is unique-identity, i.e., it is `:db/unique
/// :db.unique/identity`.
///
/// Unique-identity attributes always have value type `Ref`.
///
/// *Unique-identity* means that the attribute is *unique-value* and that they can be used in
/// lookup-refs and will automatically upsert where appropriate.
pub unique: Option<attribute::Unique>,
/// `true` if this attribute is automatically indexed, i.e., it is `:db/indexing true`.
pub index: bool,
/// `true` if this attribute is automatically fulltext indexed, i.e., it is `:db/fulltext true`.
///
/// Fulltext attributes always have string values.
pub fulltext: bool,
/// `true` if this attribute is a component, i.e., it is `:db/isComponent true`.
///
/// Component attributes always have value type `Ref`.
///
/// They are used to compose entities from component sub-entities: they are fetched recursively
/// by pull expressions, and they are automatically recursively deleted where appropriate.
pub component: bool,
/// `true` if this attribute doesn't require history to be kept, i.e., it is `:db/noHistory true`.
pub no_history: bool,
}
impl Attribute {
/// Combine several attribute flags into a bitfield used in temporary search tables.
pub fn flags(&self) -> u8 {
let mut flags: u8 = 0;
if self.index {
flags |= AttributeBitFlags::IndexAVET as u8;
}
if self.value_type == ValueType::Ref {
flags |= AttributeBitFlags::IndexVAET as u8;
}
if self.fulltext {
flags |= AttributeBitFlags::IndexFulltext as u8;
}
if self.unique.is_some() {
flags |= AttributeBitFlags::UniqueValue as u8;
}
flags
}
pub fn to_edn_value(&self, ident: Option<Keyword>) -> edn::Value {
let mut attribute_map: BTreeMap<edn::Value, edn::Value> = BTreeMap::default();
if let Some(ident) = ident {
attribute_map.insert(values::DB_IDENT.clone(), edn::Value::Keyword(ident));
}
attribute_map.insert(values::DB_VALUE_TYPE.clone(), self.value_type.into_edn_value());
attribute_map.insert(values::DB_CARDINALITY.clone(), if self.multival { values::DB_CARDINALITY_MANY.clone() } else { values::DB_CARDINALITY_ONE.clone() });
match self.unique {
Some(attribute::Unique::Value) => { attribute_map.insert(values::DB_UNIQUE.clone(), values::DB_UNIQUE_VALUE.clone()); },
Some(attribute::Unique::Identity) => { attribute_map.insert(values::DB_UNIQUE.clone(), values::DB_UNIQUE_IDENTITY.clone()); },
None => (),
}
if self.index {
attribute_map.insert(values::DB_INDEX.clone(), edn::Value::Boolean(true));
}
if self.fulltext {
attribute_map.insert(values::DB_FULLTEXT.clone(), edn::Value::Boolean(true));
}
if self.component {
attribute_map.insert(values::DB_IS_COMPONENT.clone(), edn::Value::Boolean(true));
}
if self.no_history {
attribute_map.insert(values::DB_NO_HISTORY.clone(), edn::Value::Boolean(true));
}
edn::Value::Map(attribute_map)
}
}
impl Default for Attribute {
fn default() -> Attribute {
Attribute {
// There's no particular reason to favour one value type, so Ref it is.
value_type: ValueType::Ref,
fulltext: false,
index: false,
multival: false,
unique: None,
component: false,
no_history: false,
}
}
}
/// Map `Keyword` idents (`:db/ident`) to positive integer entids (`1`). /// Map `Keyword` idents (`:db/ident`) to positive integer entids (`1`).
pub type IdentMap = BTreeMap<Keyword, Entid>; pub type IdentMap = BTreeMap<Keyword, Entid>;
@ -90,21 +284,15 @@ pub struct Schema {
pub trait HasSchema { pub trait HasSchema {
fn entid_for_type(&self, t: ValueType) -> Option<KnownEntid>; fn entid_for_type(&self, t: ValueType) -> Option<KnownEntid>;
fn get_ident<T>(&self, x: T) -> Option<&Keyword> fn get_ident<T>(&self, x: T) -> Option<&Keyword> where T: Into<Entid>;
where
T: Into<Entid>;
fn get_entid(&self, x: &Keyword) -> Option<KnownEntid>; fn get_entid(&self, x: &Keyword) -> Option<KnownEntid>;
fn attribute_for_entid<T>(&self, x: T) -> Option<&Attribute> fn attribute_for_entid<T>(&self, x: T) -> Option<&Attribute> where T: Into<Entid>;
where
T: Into<Entid>;
// Returns the attribute and the entid named by the provided ident. // Returns the attribute and the entid named by the provided ident.
fn attribute_for_ident(&self, ident: &Keyword) -> Option<(&Attribute, KnownEntid)>; fn attribute_for_ident(&self, ident: &Keyword) -> Option<(&Attribute, KnownEntid)>;
/// Return true if the provided entid identifies an attribute in this schema. /// Return true if the provided entid identifies an attribute in this schema.
fn is_attribute<T>(&self, x: T) -> bool fn is_attribute<T>(&self, x: T) -> bool where T: Into<Entid>;
where
T: Into<Entid>;
/// Return true if the provided ident identifies an attribute in this schema. /// Return true if the provided ident identifies an attribute in this schema.
fn identifies_attribute(&self, x: &Keyword) -> bool; fn identifies_attribute(&self, x: &Keyword) -> bool;
@ -114,37 +302,29 @@ pub trait HasSchema {
impl Schema { impl Schema {
pub fn new(ident_map: IdentMap, entid_map: EntidMap, attribute_map: AttributeMap) -> Schema { pub fn new(ident_map: IdentMap, entid_map: EntidMap, attribute_map: AttributeMap) -> Schema {
let mut s = Schema { let mut s = Schema { ident_map, entid_map, attribute_map, component_attributes: Vec::new() };
ident_map,
entid_map,
attribute_map,
component_attributes: Vec::new(),
};
s.update_component_attributes(); s.update_component_attributes();
s s
} }
/// Returns an symbolic representation of the schema suitable for applying across Mentat stores. /// Returns an symbolic representation of the schema suitable for applying across Mentat stores.
pub fn to_edn_value(&self) -> edn::Value { pub fn to_edn_value(&self) -> edn::Value {
edn::Value::Vector( edn::Value::Vector((&self.attribute_map).iter()
(&self.attribute_map) .map(|(entid, attribute)|
.iter() attribute.to_edn_value(self.get_ident(*entid).cloned()))
.map(|(entid, attribute)| attribute.to_edn_value(self.get_ident(*entid).cloned())) .collect())
.collect(),
)
} }
fn get_raw_entid(&self, x: &Keyword) -> Option<Entid> { fn get_raw_entid(&self, x: &Keyword) -> Option<Entid> {
self.ident_map.get(x).copied() self.ident_map.get(x).map(|x| *x)
} }
pub fn update_component_attributes(&mut self) { pub fn update_component_attributes(&mut self) {
let mut components: Vec<Entid>; let mut components: Vec<Entid>;
components = self components = self.attribute_map
.attribute_map .iter()
.iter() .filter_map(|(k, v)| if v.component { Some(*k) } else { None })
.filter_map(|(k, v)| if v.component { Some(*k) } else { None }) .collect();
.collect();
components.sort_unstable(); components.sort_unstable();
self.component_attributes = components; self.component_attributes = components;
} }
@ -156,10 +336,7 @@ impl HasSchema for Schema {
self.get_entid(&t.into_keyword()) self.get_entid(&t.into_keyword())
} }
fn get_ident<T>(&self, x: T) -> Option<&Keyword> fn get_ident<T>(&self, x: T) -> Option<&Keyword> where T: Into<Entid> {
where
T: Into<Entid>,
{
self.entid_map.get(&x.into()) self.entid_map.get(&x.into())
} }
@ -167,33 +344,25 @@ impl HasSchema for Schema {
self.get_raw_entid(x).map(KnownEntid) self.get_raw_entid(x).map(KnownEntid)
} }
fn attribute_for_entid<T>(&self, x: T) -> Option<&Attribute> fn attribute_for_entid<T>(&self, x: T) -> Option<&Attribute> where T: Into<Entid> {
where
T: Into<Entid>,
{
self.attribute_map.get(&x.into()) self.attribute_map.get(&x.into())
} }
fn attribute_for_ident(&self, ident: &Keyword) -> Option<(&Attribute, KnownEntid)> { fn attribute_for_ident(&self, ident: &Keyword) -> Option<(&Attribute, KnownEntid)> {
self.get_raw_entid(&ident).and_then(|entid| { self.get_raw_entid(&ident)
self.attribute_for_entid(entid) .and_then(|entid| {
.map(|a| (a, KnownEntid(entid))) self.attribute_for_entid(entid).map(|a| (a, KnownEntid(entid)))
}) })
} }
/// Return true if the provided entid identifies an attribute in this schema. /// Return true if the provided entid identifies an attribute in this schema.
fn is_attribute<T>(&self, x: T) -> bool fn is_attribute<T>(&self, x: T) -> bool where T: Into<Entid> {
where
T: Into<Entid>,
{
self.attribute_map.contains_key(&x.into()) self.attribute_map.contains_key(&x.into())
} }
/// Return true if the provided ident identifies an attribute in this schema. /// Return true if the provided ident identifies an attribute in this schema.
fn identifies_attribute(&self, x: &Keyword) -> bool { fn identifies_attribute(&self, x: &Keyword) -> bool {
self.get_raw_entid(x) self.get_raw_entid(x).map(|e| self.is_attribute(e)).unwrap_or(false)
.map(|e| self.is_attribute(e))
.unwrap_or(false)
} }
fn component_attributes(&self) -> &[Entid] { fn component_attributes(&self) -> &[Entid] {
@ -224,7 +393,7 @@ pub mod util;
macro_rules! interpose { macro_rules! interpose {
( $name: pat, $across: expr, $body: block, $inter: block ) => { ( $name: pat, $across: expr, $body: block, $inter: block ) => {
interpose_iter!($name, $across.iter(), $body, $inter) interpose_iter!($name, $across.iter(), $body, $inter)
}; }
} }
/// A helper to bind `name` to values in `across`, running `body` for each value, /// A helper to bind `name` to values in `across`, running `body` for each value,
@ -240,7 +409,7 @@ macro_rules! interpose_iter {
$body; $body;
} }
} }
}; }
} }
#[cfg(test)] #[cfg(test)]
@ -249,8 +418,6 @@ mod test {
use std::str::FromStr; use std::str::FromStr;
use core_traits::{attribute, TypedValue};
fn associate_ident(schema: &mut Schema, i: Keyword, e: Entid) { fn associate_ident(schema: &mut Schema, i: Keyword, e: Entid) {
schema.entid_map.insert(e, i.clone()); schema.entid_map.insert(e, i.clone());
schema.ident_map.insert(i, e); schema.ident_map.insert(i, e);
@ -260,12 +427,58 @@ mod test {
schema.attribute_map.insert(e, a); schema.attribute_map.insert(e, a);
} }
#[test]
fn test_attribute_flags() {
let attr1 = Attribute {
index: true,
value_type: ValueType::Ref,
fulltext: false,
unique: None,
multival: false,
component: false,
no_history: false,
};
assert!(attr1.flags() & AttributeBitFlags::IndexAVET as u8 != 0);
assert!(attr1.flags() & AttributeBitFlags::IndexVAET as u8 != 0);
assert!(attr1.flags() & AttributeBitFlags::IndexFulltext as u8 == 0);
assert!(attr1.flags() & AttributeBitFlags::UniqueValue as u8 == 0);
let attr2 = Attribute {
index: false,
value_type: ValueType::Boolean,
fulltext: true,
unique: Some(attribute::Unique::Value),
multival: false,
component: false,
no_history: false,
};
assert!(attr2.flags() & AttributeBitFlags::IndexAVET as u8 == 0);
assert!(attr2.flags() & AttributeBitFlags::IndexVAET as u8 == 0);
assert!(attr2.flags() & AttributeBitFlags::IndexFulltext as u8 != 0);
assert!(attr2.flags() & AttributeBitFlags::UniqueValue as u8 != 0);
let attr3 = Attribute {
index: false,
value_type: ValueType::Boolean,
fulltext: true,
unique: Some(attribute::Unique::Identity),
multival: false,
component: false,
no_history: false,
};
assert!(attr3.flags() & AttributeBitFlags::IndexAVET as u8 == 0);
assert!(attr3.flags() & AttributeBitFlags::IndexVAET as u8 == 0);
assert!(attr3.flags() & AttributeBitFlags::IndexFulltext as u8 != 0);
assert!(attr3.flags() & AttributeBitFlags::UniqueValue as u8 != 0);
}
#[test] #[test]
fn test_datetime_truncation() { fn test_datetime_truncation() {
let dt: DateTime<Utc> = let dt: DateTime<Utc> = DateTime::from_str("2018-01-11T00:34:09.273457004Z").expect("parsed");
DateTime::from_str("2018-01-11T00:34:09.273457004Z").expect("parsed"); let expected: DateTime<Utc> = DateTime::from_str("2018-01-11T00:34:09.273457Z").expect("parsed");
let expected: DateTime<Utc> =
DateTime::from_str("2018-01-11T00:34:09.273457Z").expect("parsed");
let tv: TypedValue = dt.into(); let tv: TypedValue = dt.into();
if let TypedValue::Instant(roundtripped) = tv { if let TypedValue::Instant(roundtripped) = tv {
@ -333,9 +546,7 @@ mod test {
:db/cardinality :db.cardinality/one :db/cardinality :db.cardinality/one
:db/unique :db.unique/identity :db/unique :db.unique/identity
:db/isComponent true }, ]"#; :db/isComponent true }, ]"#;
let expected_value = edn::parse::value(&expected_output) let expected_value = edn::parse::value(&expected_output).expect("to be able to parse").without_spans();
.expect("to be able to parse")
.without_spans();
assert_eq!(expected_value, value); assert_eq!(expected_value, value);
// let's compare the whole thing again, just to make sure we are not changing anything when we convert to edn. // let's compare the whole thing again, just to make sure we are not changing anything when we convert to edn.

View file

@ -8,11 +8,18 @@
// CONDITIONS OF ANY KIND, either express or implied. See the License for the // CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License. // specific language governing permissions and limitations under the License.
use std::collections::BTreeSet; use std::collections::{
BTreeSet,
};
use core_traits::{ValueType, ValueTypeSet}; use types::{
ValueType,
ValueTypeTag,
};
use crate::types::ValueTypeTag; use value_type_set::{
ValueTypeSet,
};
/// Type safe representation of the possible return values from SQLite's `typeof` /// Type safe representation of the possible return values from SQLite's `typeof`
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialOrd, PartialEq)] #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialOrd, PartialEq)]
@ -41,17 +48,16 @@ pub trait SQLValueType {
impl SQLValueType for ValueType { impl SQLValueType for ValueType {
fn sql_representation(&self) -> (ValueTypeTag, Option<SQLTypeAffinity>) { fn sql_representation(&self) -> (ValueTypeTag, Option<SQLTypeAffinity>) {
match *self { match *self {
ValueType::Ref => (0, None), ValueType::Ref => (0, None),
ValueType::Boolean => (1, None), ValueType::Boolean => (1, None),
ValueType::Instant => (4, None), ValueType::Instant => (4, None),
// SQLite distinguishes integral from decimal types, allowing long and double to share a tag. // SQLite distinguishes integral from decimal types, allowing long and double to share a tag.
ValueType::Long => (5, Some(SQLTypeAffinity::Integer)), ValueType::Long => (5, Some(SQLTypeAffinity::Integer)),
ValueType::Double => (5, Some(SQLTypeAffinity::Real)), ValueType::Double => (5, Some(SQLTypeAffinity::Real)),
ValueType::String => (10, None), ValueType::String => (10, None),
ValueType::Uuid => (11, None), ValueType::Uuid => (11, None),
ValueType::Keyword => (13, None), ValueType::Keyword => (13, None),
ValueType::Bytes => (15, Some(SQLTypeAffinity::Blob)),
} }
} }
@ -62,17 +68,26 @@ impl SQLValueType for ValueType {
/// Returns true if the provided integer is in the SQLite value space of this type. For /// Returns true if the provided integer is in the SQLite value space of this type. For
/// example, `1` is how we encode `true`. /// example, `1` is how we encode `true`.
///
/// ```
/// use mentat_core::{ValueType, SQLValueType};
/// assert!(!ValueType::Instant.accommodates_integer(1493399581314));
/// assert!(!ValueType::Instant.accommodates_integer(1493399581314000));
/// assert!(ValueType::Boolean.accommodates_integer(1));
/// assert!(!ValueType::Boolean.accommodates_integer(-1));
/// assert!(!ValueType::Boolean.accommodates_integer(10));
/// assert!(!ValueType::String.accommodates_integer(10));
/// ```
fn accommodates_integer(&self, int: i64) -> bool { fn accommodates_integer(&self, int: i64) -> bool {
use crate::ValueType::*; use ValueType::*;
match *self { match *self {
Instant => false, // Always use #inst. Instant => false, // Always use #inst.
Long | Double => true, Long | Double => true,
Ref => int >= 0, Ref => int >= 0,
Boolean => (int == 0) || (int == 1), Boolean => (int == 0) || (int == 1),
ValueType::String => false, ValueType::String => false,
Keyword => false, Keyword => false,
Uuid => false, Uuid => false,
Bytes => false,
} }
} }
} }
@ -122,19 +137,3 @@ impl SQLValueTypeSet for ValueTypeSet {
!acc.is_empty() !acc.is_empty()
} }
} }
#[cfg(test)]
mod tests {
use crate::sql_types::SQLValueType;
use core_traits::ValueType;
#[test]
fn test_accommodates_integer() {
assert!(!ValueType::Instant.accommodates_integer(1493399581314));
assert!(!ValueType::Instant.accommodates_integer(1493399581314000));
assert!(ValueType::Boolean.accommodates_integer(1));
assert!(!ValueType::Boolean.accommodates_integer(-1));
assert!(!ValueType::Boolean.accommodates_integer(10));
assert!(!ValueType::String.accommodates_integer(10));
}
}

View file

@ -10,11 +10,15 @@
#![allow(dead_code)] #![allow(dead_code)]
use std::collections::BTreeMap; use std::collections::{
BTreeMap,
};
use core_traits::Entid; use ::{
DateTime,
use crate::{DateTime, Utc}; Entid,
Utc,
};
/// A transaction report summarizes an applied transaction. /// A transaction report summarizes an applied transaction.
#[derive(Clone, Debug, Eq, Hash, Ord, PartialOrd, PartialEq)] #[derive(Clone, Debug, Eq, Hash, Ord, PartialOrd, PartialEq)]

View file

@ -8,4 +8,741 @@
// CONDITIONS OF ANY KIND, either express or implied. See the License for the // CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License. // specific language governing permissions and limitations under the License.
use ::std::ffi::{
CString,
};
use ::std::os::raw::c_char;
use ::std::rc::{
Rc,
};
use ::std::sync::{
Arc,
};
use std::fmt;
use ::enum_set::EnumSet;
use ::ordered_float::OrderedFloat;
use ::uuid::Uuid;
use ::chrono::{
DateTime,
Timelike, // For truncation.
};
use ::indexmap::{
IndexMap,
};
use ::edn::{
self,
Cloned,
FromMicros,
FromRc,
Keyword,
Utc,
ValueRc,
};
use ::edn::entities::{
AttributePlace,
EntidOrIdent,
EntityPlace,
ValuePlace,
TransactableValueMarker,
};
use values;
/// Represents one entid in the entid space.
///
/// Per https://www.sqlite.org/datatype3.html (see also http://stackoverflow.com/a/8499544), SQLite
/// stores signed integers up to 64 bits in size. Since u32 is not appropriate for our use case, we
/// use i64 rather than manually truncating u64 to u63 and casting to i64 throughout the codebase.
pub type Entid = i64;
/// An entid that's either already in the store, or newly allocated to a tempid.
/// TODO: we'd like to link this in some way to the lifetime of a particular PartitionMap.
#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub struct KnownEntid(pub Entid);
impl From<KnownEntid> for Entid {
fn from(k: KnownEntid) -> Entid {
k.0
}
}
impl From<KnownEntid> for TypedValue {
fn from(k: KnownEntid) -> TypedValue {
TypedValue::Ref(k.0)
}
}
impl<V: TransactableValueMarker> Into<EntityPlace<V>> for KnownEntid {
fn into(self) -> EntityPlace<V> {
EntityPlace::Entid(EntidOrIdent::Entid(self.0))
}
}
impl Into<AttributePlace> for KnownEntid {
fn into(self) -> AttributePlace {
AttributePlace::Entid(EntidOrIdent::Entid(self.0))
}
}
impl<V: TransactableValueMarker> Into<ValuePlace<V>> for KnownEntid {
fn into(self) -> ValuePlace<V> {
ValuePlace::Entid(EntidOrIdent::Entid(self.0))
}
}
/// The attribute of each Mentat assertion has a :db/valueType constraining the value to a
/// particular set. Mentat recognizes the following :db/valueType values.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialOrd, PartialEq)]
#[repr(u32)]
pub enum ValueType {
Ref,
Boolean,
Instant,
Long,
Double,
String,
Keyword,
Uuid,
}
pub type ValueTypeTag = i32; pub type ValueTypeTag = i32;
impl ValueType {
pub fn all_enums() -> EnumSet<ValueType> {
// TODO: lazy_static.
let mut s = EnumSet::new();
s.insert(ValueType::Ref);
s.insert(ValueType::Boolean);
s.insert(ValueType::Instant);
s.insert(ValueType::Long);
s.insert(ValueType::Double);
s.insert(ValueType::String);
s.insert(ValueType::Keyword);
s.insert(ValueType::Uuid);
s
}
}
impl ::enum_set::CLike for ValueType {
fn to_u32(&self) -> u32 {
*self as u32
}
unsafe fn from_u32(v: u32) -> ValueType {
::std::mem::transmute(v)
}
}
impl ValueType {
pub fn into_keyword(self) -> Keyword {
Keyword::namespaced("db.type", match self {
ValueType::Ref => "ref",
ValueType::Boolean => "boolean",
ValueType::Instant => "instant",
ValueType::Long => "long",
ValueType::Double => "double",
ValueType::String => "string",
ValueType::Keyword => "keyword",
ValueType::Uuid => "uuid",
})
}
pub fn from_keyword(keyword: &Keyword) -> Option<Self> {
if keyword.namespace() != Some("db.type") {
return None;
}
return match keyword.name() {
"ref" => Some(ValueType::Ref),
"boolean" => Some(ValueType::Boolean),
"instant" => Some(ValueType::Instant),
"long" => Some(ValueType::Long),
"double" => Some(ValueType::Double),
"string" => Some(ValueType::String),
"keyword" => Some(ValueType::Keyword),
"uuid" => Some(ValueType::Uuid),
_ => None,
}
}
pub fn into_typed_value(self) -> TypedValue {
TypedValue::typed_ns_keyword("db.type", match self {
ValueType::Ref => "ref",
ValueType::Boolean => "boolean",
ValueType::Instant => "instant",
ValueType::Long => "long",
ValueType::Double => "double",
ValueType::String => "string",
ValueType::Keyword => "keyword",
ValueType::Uuid => "uuid",
})
}
pub fn into_edn_value(self) -> edn::Value {
match self {
ValueType::Ref => values::DB_TYPE_REF.clone(),
ValueType::Boolean => values::DB_TYPE_BOOLEAN.clone(),
ValueType::Instant => values::DB_TYPE_INSTANT.clone(),
ValueType::Long => values::DB_TYPE_LONG.clone(),
ValueType::Double => values::DB_TYPE_DOUBLE.clone(),
ValueType::String => values::DB_TYPE_STRING.clone(),
ValueType::Keyword => values::DB_TYPE_KEYWORD.clone(),
ValueType::Uuid => values::DB_TYPE_UUID.clone(),
}
}
pub fn is_numeric(&self) -> bool {
match self {
&ValueType::Long | &ValueType::Double => true,
_ => false
}
}
}
impl fmt::Display for ValueType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", match *self {
ValueType::Ref => ":db.type/ref",
ValueType::Boolean => ":db.type/boolean",
ValueType::Instant => ":db.type/instant",
ValueType::Long => ":db.type/long",
ValueType::Double => ":db.type/double",
ValueType::String => ":db.type/string",
ValueType::Keyword => ":db.type/keyword",
ValueType::Uuid => ":db.type/uuid",
})
}
}
/// Represents a value that can be stored in a Mentat store.
// TODO: expand to include :db.type/uri. https://github.com/mozilla/mentat/issues/201
// TODO: JSON data type? https://github.com/mozilla/mentat/issues/31
// TODO: BigInt? Bytes?
#[derive(Clone, Debug, Eq, Hash, Ord, PartialOrd, PartialEq, Serialize, Deserialize)]
pub enum TypedValue {
Ref(Entid),
Boolean(bool),
Long(i64),
Double(OrderedFloat<f64>),
Instant(DateTime<Utc>), // Use `into()` to ensure truncation.
// TODO: &str throughout?
String(ValueRc<String>),
Keyword(ValueRc<Keyword>),
Uuid(Uuid), // It's only 128 bits, so this should be acceptable to clone.
}
/// `TypedValue` is the value type for programmatic use in transaction builders.
impl TransactableValueMarker for TypedValue {}
/// The values bound in a query specification can be:
///
/// * Vecs of structured values, for multi-valued component attributes or nested expressions.
/// * Single structured values, for single-valued component attributes or nested expressions.
/// * Single typed values, for simple attributes.
///
/// The `Binding` enum defines these three options.
///
/// Datomic also supports structured inputs; at present Mentat does not, but this type
/// would also serve that purpose.
///
/// Note that maps are not ordered, and so `Binding` is neither `Ord` nor `PartialOrd`.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Binding {
Scalar(TypedValue),
Vec(ValueRc<Vec<Binding>>),
Map(ValueRc<StructuredMap>),
}
impl<T> From<T> for Binding where T: Into<TypedValue> {
fn from(value: T) -> Self {
Binding::Scalar(value.into())
}
}
impl From<StructuredMap> for Binding {
fn from(value: StructuredMap) -> Self {
Binding::Map(ValueRc::new(value))
}
}
impl From<Vec<Binding>> for Binding {
fn from(value: Vec<Binding>) -> Self {
Binding::Vec(ValueRc::new(value))
}
}
impl Binding {
pub fn val(self) -> Option<TypedValue> {
match self {
Binding::Scalar(v) => Some(v),
_ => None,
}
}
}
/// A pull expression expands a binding into a structure. The returned structure
/// associates attributes named in the input or retrieved from the store with values.
/// This association is a `StructuredMap`.
///
/// Note that 'attributes' in Datomic's case can mean:
/// - Reversed attribute keywords (:artist/_country).
/// - An alias using `:as` (:artist/name :as "Band name").
///
/// We entirely support the former, and partially support the latter -- you can alias
/// using a different keyword only.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct StructuredMap(pub IndexMap<ValueRc<Keyword>, Binding>);
impl StructuredMap {
pub fn insert<N, B>(&mut self, name: N, value: B) where N: Into<ValueRc<Keyword>>, B: Into<Binding> {
self.0.insert(name.into(), value.into());
}
}
impl From<IndexMap<ValueRc<Keyword>, Binding>> for StructuredMap {
fn from(src: IndexMap<ValueRc<Keyword>, Binding>) -> Self {
StructuredMap(src)
}
}
// Mostly for testing.
impl<T> From<Vec<(Keyword, T)>> for StructuredMap where T: Into<Binding> {
fn from(value: Vec<(Keyword, T)>) -> Self {
let mut sm = StructuredMap::default();
for (k, v) in value.into_iter() {
sm.insert(k, v);
}
sm
}
}
impl Binding {
/// Returns true if the provided type is `Some` and matches this value's type, or if the
/// provided type is `None`.
#[inline]
pub fn is_congruent_with<T: Into<Option<ValueType>>>(&self, t: T) -> bool {
t.into().map_or(true, |x| self.matches_type(x))
}
#[inline]
pub fn matches_type(&self, t: ValueType) -> bool {
self.value_type() == Some(t)
}
pub fn value_type(&self) -> Option<ValueType> {
match self {
&Binding::Scalar(ref v) => Some(v.value_type()),
&Binding::Map(_) => None,
&Binding::Vec(_) => None,
}
}
}
impl TypedValue {
/// Returns true if the provided type is `Some` and matches this value's type, or if the
/// provided type is `None`.
#[inline]
pub fn is_congruent_with<T: Into<Option<ValueType>>>(&self, t: T) -> bool {
t.into().map_or(true, |x| self.matches_type(x))
}
#[inline]
pub fn matches_type(&self, t: ValueType) -> bool {
self.value_type() == t
}
pub fn value_type(&self) -> ValueType {
match self {
&TypedValue::Ref(_) => ValueType::Ref,
&TypedValue::Boolean(_) => ValueType::Boolean,
&TypedValue::Long(_) => ValueType::Long,
&TypedValue::Instant(_) => ValueType::Instant,
&TypedValue::Double(_) => ValueType::Double,
&TypedValue::String(_) => ValueType::String,
&TypedValue::Keyword(_) => ValueType::Keyword,
&TypedValue::Uuid(_) => ValueType::Uuid,
}
}
/// Construct a new `TypedValue::Keyword` instance by cloning the provided
/// values and wrapping them in a new `ValueRc`. This is expensive, so this might
/// be best limited to tests.
pub fn typed_ns_keyword(ns: &str, name: &str) -> TypedValue {
Keyword::namespaced(ns, name).into()
}
/// Construct a new `TypedValue::String` instance by cloning the provided
/// value and wrapping it in a new `ValueRc`. This is expensive, so this might
/// be best limited to tests.
pub fn typed_string(s: &str) -> TypedValue {
s.into()
}
pub fn current_instant() -> TypedValue {
Utc::now().into()
}
/// Construct a new `TypedValue::Instant` instance from the provided
/// microsecond timestamp.
pub fn instant(micros: i64) -> TypedValue {
DateTime::<Utc>::from_micros(micros).into()
}
}
trait MicrosecondPrecision {
/// Truncate the provided `DateTime` to microsecond precision.
fn microsecond_precision(self) -> Self;
}
impl MicrosecondPrecision for DateTime<Utc> {
fn microsecond_precision(self) -> DateTime<Utc> {
let nanoseconds = self.nanosecond();
if nanoseconds % 1000 == 0 {
return self;
}
let microseconds = nanoseconds / 1000;
let truncated = microseconds * 1000;
self.with_nanosecond(truncated).expect("valid timestamp")
}
}
/// Return the current time as a UTC `DateTime` instance with microsecond precision.
pub fn now() -> DateTime<Utc> {
Utc::now().microsecond_precision()
}
// We don't do From<i64> or From<Entid> 'cos it's ambiguous.
impl From<bool> for TypedValue {
fn from(value: bool) -> TypedValue {
TypedValue::Boolean(value)
}
}
/// Truncate the provided `DateTime` to microsecond precision, and return the corresponding
/// `TypedValue::Instant`.
impl From<DateTime<Utc>> for TypedValue {
fn from(value: DateTime<Utc>) -> TypedValue {
TypedValue::Instant(value.microsecond_precision())
}
}
impl From<Uuid> for TypedValue {
fn from(value: Uuid) -> TypedValue {
TypedValue::Uuid(value)
}
}
impl<'a> From<&'a str> for TypedValue {
fn from(value: &'a str) -> TypedValue {
TypedValue::String(ValueRc::new(value.to_string()))
}
}
impl From<Arc<String>> for TypedValue {
fn from(value: Arc<String>) -> TypedValue {
TypedValue::String(ValueRc::from_arc(value))
}
}
impl From<Rc<String>> for TypedValue {
fn from(value: Rc<String>) -> TypedValue {
TypedValue::String(ValueRc::from_rc(value))
}
}
impl From<Box<String>> for TypedValue {
fn from(value: Box<String>) -> TypedValue {
TypedValue::String(ValueRc::new(*value))
}
}
impl From<String> for TypedValue {
fn from(value: String) -> TypedValue {
TypedValue::String(ValueRc::new(value))
}
}
impl From<Arc<Keyword>> for TypedValue {
fn from(value: Arc<Keyword>) -> TypedValue {
TypedValue::Keyword(ValueRc::from_arc(value))
}
}
impl From<Rc<Keyword>> for TypedValue {
fn from(value: Rc<Keyword>) -> TypedValue {
TypedValue::Keyword(ValueRc::from_rc(value))
}
}
impl From<Keyword> for TypedValue {
fn from(value: Keyword) -> TypedValue {
TypedValue::Keyword(ValueRc::new(value))
}
}
impl From<u32> for TypedValue {
fn from(value: u32) -> TypedValue {
TypedValue::Long(value as i64)
}
}
impl From<i32> for TypedValue {
fn from(value: i32) -> TypedValue {
TypedValue::Long(value as i64)
}
}
impl From<f64> for TypedValue {
fn from(value: f64) -> TypedValue {
TypedValue::Double(OrderedFloat(value))
}
}
impl TypedValue {
pub fn into_known_entid(self) -> Option<KnownEntid> {
match self {
TypedValue::Ref(v) => Some(KnownEntid(v)),
_ => None,
}
}
pub fn into_entid(self) -> Option<Entid> {
match self {
TypedValue::Ref(v) => Some(v),
_ => None,
}
}
pub fn into_kw(self) -> Option<ValueRc<Keyword>> {
match self {
TypedValue::Keyword(v) => Some(v),
_ => None,
}
}
pub fn into_boolean(self) -> Option<bool> {
match self {
TypedValue::Boolean(v) => Some(v),
_ => None,
}
}
pub fn into_long(self) -> Option<i64> {
match self {
TypedValue::Long(v) => Some(v),
_ => None,
}
}
pub fn into_double(self) -> Option<f64> {
match self {
TypedValue::Double(v) => Some(v.into_inner()),
_ => None,
}
}
pub fn into_instant(self) -> Option<DateTime<Utc>> {
match self {
TypedValue::Instant(v) => Some(v),
_ => None,
}
}
pub fn into_timestamp(self) -> Option<i64> {
match self {
TypedValue::Instant(v) => Some(v.timestamp()),
_ => None,
}
}
pub fn into_string(self) -> Option<ValueRc<String>> {
match self {
TypedValue::String(v) => Some(v),
_ => None,
}
}
pub fn into_c_string(self) -> Option<*mut c_char> {
match self {
TypedValue::String(v) => {
// Get an independent copy of the string.
let s: String = v.cloned();
// Make a CString out of the new bytes.
let c: CString = CString::new(s).expect("String conversion failed!");
// Return a C-owned pointer.
Some(c.into_raw())
},
_ => None,
}
}
pub fn into_kw_c_string(self) -> Option<*mut c_char> {
match self {
TypedValue::Keyword(v) => {
// Get an independent copy of the string.
let s: String = v.to_string();
// Make a CString out of the new bytes.
let c: CString = CString::new(s).expect("String conversion failed!");
// Return a C-owned pointer.
Some(c.into_raw())
},
_ => None,
}
}
pub fn into_uuid_c_string(self) -> Option<*mut c_char> {
match self {
TypedValue::Uuid(v) => {
// Get an independent copy of the string.
let s: String = v.hyphenated().to_string();
// Make a CString out of the new bytes.
let c: CString = CString::new(s).expect("String conversion failed!");
// Return a C-owned pointer.
Some(c.into_raw())
},
_ => None,
}
}
pub fn into_uuid(self) -> Option<Uuid> {
match self {
TypedValue::Uuid(v) => Some(v),
_ => None,
}
}
pub fn into_uuid_string(self) -> Option<String> {
match self {
TypedValue::Uuid(v) => Some(v.hyphenated().to_string()),
_ => None,
}
}
}
impl Binding {
pub fn into_known_entid(self) -> Option<KnownEntid> {
match self {
Binding::Scalar(TypedValue::Ref(v)) => Some(KnownEntid(v)),
_ => None,
}
}
pub fn into_entid(self) -> Option<Entid> {
match self {
Binding::Scalar(TypedValue::Ref(v)) => Some(v),
_ => None,
}
}
pub fn into_kw(self) -> Option<ValueRc<Keyword>> {
match self {
Binding::Scalar(TypedValue::Keyword(v)) => Some(v),
_ => None,
}
}
pub fn into_boolean(self) -> Option<bool> {
match self {
Binding::Scalar(TypedValue::Boolean(v)) => Some(v),
_ => None,
}
}
pub fn into_long(self) -> Option<i64> {
match self {
Binding::Scalar(TypedValue::Long(v)) => Some(v),
_ => None,
}
}
pub fn into_double(self) -> Option<f64> {
match self {
Binding::Scalar(TypedValue::Double(v)) => Some(v.into_inner()),
_ => None,
}
}
pub fn into_instant(self) -> Option<DateTime<Utc>> {
match self {
Binding::Scalar(TypedValue::Instant(v)) => Some(v),
_ => None,
}
}
pub fn into_timestamp(self) -> Option<i64> {
match self {
Binding::Scalar(TypedValue::Instant(v)) => Some(v.timestamp()),
_ => None,
}
}
pub fn into_string(self) -> Option<ValueRc<String>> {
match self {
Binding::Scalar(TypedValue::String(v)) => Some(v),
_ => None,
}
}
pub fn into_uuid(self) -> Option<Uuid> {
match self {
Binding::Scalar(TypedValue::Uuid(v)) => Some(v),
_ => None,
}
}
pub fn into_uuid_string(self) -> Option<String> {
match self {
Binding::Scalar(TypedValue::Uuid(v)) => Some(v.hyphenated().to_string()),
_ => None,
}
}
pub fn into_c_string(self) -> Option<*mut c_char> {
match self {
Binding::Scalar(v) => v.into_c_string(),
_ => None,
}
}
pub fn into_kw_c_string(self) -> Option<*mut c_char> {
match self {
Binding::Scalar(v) => v.into_kw_c_string(),
_ => None,
}
}
pub fn into_uuid_c_string(self) -> Option<*mut c_char> {
match self {
Binding::Scalar(v) => v.into_uuid_c_string(),
_ => None,
}
}
}
#[test]
fn test_typed_value() {
assert!(TypedValue::Boolean(false).is_congruent_with(None));
assert!(TypedValue::Boolean(false).is_congruent_with(ValueType::Boolean));
assert!(!TypedValue::typed_string("foo").is_congruent_with(ValueType::Boolean));
assert!(TypedValue::typed_string("foo").is_congruent_with(ValueType::String));
assert!(TypedValue::typed_string("foo").is_congruent_with(None));
}

View file

@ -67,8 +67,7 @@ pub enum Either<L, R> {
// Cribbed from https://github.com/bluss/either/blob/f793721f3fdeb694f009e731b23a2858286bc0d6/src/lib.rs#L219-L259. // Cribbed from https://github.com/bluss/either/blob/f793721f3fdeb694f009e731b23a2858286bc0d6/src/lib.rs#L219-L259.
impl<L, R> Either<L, R> { impl<L, R> Either<L, R> {
pub fn map_left<F, M>(self, f: F) -> Either<M, R> pub fn map_left<F, M>(self, f: F) -> Either<M, R>
where where F: FnOnce(L) -> M
F: FnOnce(L) -> M,
{ {
use self::Either::*; use self::Either::*;
match self { match self {
@ -78,8 +77,7 @@ impl<L, R> Either<L, R> {
} }
pub fn map_right<F, S>(self, f: F) -> Either<L, S> pub fn map_right<F, S>(self, f: F) -> Either<L, S>
where where F: FnOnce(R) -> S
F: FnOnce(R) -> S,
{ {
use self::Either::*; use self::Either::*;
match self { match self {

View file

@ -8,9 +8,13 @@
// CONDITIONS OF ANY KIND, either express or implied. See the License for the // CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License. // specific language governing permissions and limitations under the License.
use enum_set::EnumSet; use ::enum_set::{
EnumSet,
};
use crate::ValueType; use ::types::{
ValueType,
};
trait EnumSetExtensions<T: ::enum_set::CLike + Clone> { trait EnumSetExtensions<T: ::enum_set::CLike + Clone> {
/// Return a set containing both `x` and `y`. /// Return a set containing both `x` and `y`.
@ -37,6 +41,7 @@ impl<T: ::enum_set::CLike + Clone> EnumSetExtensions<T> for EnumSet<T> {
} }
} }
#[derive(Clone, Copy, Debug, Eq, PartialEq)] #[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ValueTypeSet(pub EnumSet<ValueType>); pub struct ValueTypeSet(pub EnumSet<ValueType>);
@ -92,53 +97,53 @@ impl ValueTypeSet {
self.0.insert(vt) self.0.insert(vt)
} }
pub fn len(self) -> usize { pub fn len(&self) -> usize {
self.0.len() self.0.len()
} }
/// Returns a set containing all the types in this set and `other`. /// Returns a set containing all the types in this set and `other`.
pub fn union(self, other: ValueTypeSet) -> ValueTypeSet { pub fn union(&self, other: &ValueTypeSet) -> ValueTypeSet {
ValueTypeSet(self.0.union(other.0)) ValueTypeSet(self.0.union(other.0))
} }
pub fn intersection(self, other: ValueTypeSet) -> ValueTypeSet { pub fn intersection(&self, other: &ValueTypeSet) -> ValueTypeSet {
ValueTypeSet(self.0.intersection(other.0)) ValueTypeSet(self.0.intersection(other.0))
} }
/// Returns the set difference between `self` and `other`, which is the /// Returns the set difference between `self` and `other`, which is the
/// set of items in `self` that are not in `other`. /// set of items in `self` that are not in `other`.
pub fn difference(self, other: ValueTypeSet) -> ValueTypeSet { pub fn difference(&self, other: &ValueTypeSet) -> ValueTypeSet {
ValueTypeSet(self.0 - other.0) ValueTypeSet(self.0 - other.0)
} }
/// Return an arbitrary type that's part of this set. /// Return an arbitrary type that's part of this set.
/// For a set containing a single type, this will be that type. /// For a set containing a single type, this will be that type.
pub fn exemplar(self) -> Option<ValueType> { pub fn exemplar(&self) -> Option<ValueType> {
self.0.iter().next() self.0.iter().next()
} }
pub fn is_subset(self, other: ValueTypeSet) -> bool { pub fn is_subset(&self, other: &ValueTypeSet) -> bool {
self.0.is_subset(&other.0) self.0.is_subset(&other.0)
} }
/// Returns true if `self` and `other` contain no items in common. /// Returns true if `self` and `other` contain no items in common.
pub fn is_disjoint(self, other: ValueTypeSet) -> bool { pub fn is_disjoint(&self, other: &ValueTypeSet) -> bool {
self.0.is_disjoint(&other.0) self.0.is_disjoint(&other.0)
} }
pub fn contains(self, vt: ValueType) -> bool { pub fn contains(&self, vt: ValueType) -> bool {
self.0.contains(&vt) self.0.contains(&vt)
} }
pub fn is_empty(self) -> bool { pub fn is_empty(&self) -> bool {
self.0.is_empty() self.0.is_empty()
} }
pub fn is_unit(self) -> bool { pub fn is_unit(&self) -> bool {
self.0.len() == 1 self.0.len() == 1
} }
pub fn iter(self) -> ::enum_set::Iter<ValueType> { pub fn iter(&self) -> ::enum_set::Iter<ValueType> {
self.0.iter() self.0.iter()
} }
} }
@ -150,8 +155,8 @@ impl From<ValueType> for ValueTypeSet {
} }
impl ValueTypeSet { impl ValueTypeSet {
pub fn is_only_numeric(self) -> bool { pub fn is_only_numeric(&self) -> bool {
self.is_subset(ValueTypeSet::of_numeric_types()) self.is_subset(&ValueTypeSet::of_numeric_types())
} }
} }

View file

@ -10,20 +10,21 @@
#![allow(dead_code)] #![allow(dead_code)]
use edn::symbols;
/// Literal `Value` instances in the the "db" namespace. /// Literal `Value` instances in the the "db" namespace.
/// ///
/// Used through-out the transactor to match core DB constructs. /// Used through-out the transactor to match core DB constructs.
use edn::types::Value; use edn::types::Value;
use edn::symbols;
/// Declare a lazy static `ident` of type `Value::Keyword` with the given `namespace` and /// Declare a lazy static `ident` of type `Value::Keyword` with the given `namespace` and
/// `name`. /// `name`.
/// ///
/// It may look surprising to declare a new `lazy_static!` block rather than including /// It may look surprising that we declare a new `lazy_static!` block rather than including
/// invocations inside an existing `lazy_static!` block. The latter cannot be done, since macros /// invocations inside an existing `lazy_static!` block. The latter cannot be done, since macros
/// will be expanded outside-in. Looking at the `lazy_static!` source suggests that there is no /// are expanded outside-in. Looking at the `lazy_static!` source suggests that there is no harm in
/// harm in repeating that macro, since internally a multi-`static` block will be expanded into /// repeating that macro, since internally a multi-`static` block is expanded into many
/// many single-`static` blocks. /// single-`static` blocks.
/// ///
/// TODO: take just ":db.part/db" and define DB_PART_DB using "db.part" and "db". /// TODO: take just ":db.part/db" and define DB_PART_DB using "db.part" and "db".
macro_rules! lazy_static_namespaced_keyword_value ( macro_rules! lazy_static_namespaced_keyword_value (
@ -58,7 +59,6 @@ lazy_static_namespaced_keyword_value!(DB_TYPE_REF, "db.type", "ref");
lazy_static_namespaced_keyword_value!(DB_TYPE_STRING, "db.type", "string"); lazy_static_namespaced_keyword_value!(DB_TYPE_STRING, "db.type", "string");
lazy_static_namespaced_keyword_value!(DB_TYPE_URI, "db.type", "uri"); lazy_static_namespaced_keyword_value!(DB_TYPE_URI, "db.type", "uri");
lazy_static_namespaced_keyword_value!(DB_TYPE_UUID, "db.type", "uuid"); lazy_static_namespaced_keyword_value!(DB_TYPE_UUID, "db.type", "uuid");
lazy_static_namespaced_keyword_value!(DB_TYPE_BYTES, "db.type", "bytes");
lazy_static_namespaced_keyword_value!(DB_UNIQUE, "db", "unique"); lazy_static_namespaced_keyword_value!(DB_UNIQUE, "db", "unique");
lazy_static_namespaced_keyword_value!(DB_UNIQUE_IDENTITY, "db.unique", "identity"); lazy_static_namespaced_keyword_value!(DB_UNIQUE_IDENTITY, "db.unique", "identity");
lazy_static_namespaced_keyword_value!(DB_UNIQUE_VALUE, "db.unique", "value"); lazy_static_namespaced_keyword_value!(DB_UNIQUE_VALUE, "db.unique", "value");

View file

@ -1,25 +0,0 @@
[package]
name = "db_traits"
version = "0.0.2"
workspace = ".."
[lib]
name = "db_traits"
path = "lib.rs"
[features]
sqlcipher = ["rusqlite/sqlcipher"]
[dependencies]
failure = "~0.1"
failure_derive = "~0.1"
[dependencies.edn]
path = "../edn"
[dependencies.core_traits]
path = "../core-traits"
[dependencies.rusqlite]
version = "~0.29"
features = ["limits", "bundled"]

View file

@ -1,18 +0,0 @@
// Copyright 2018 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.
extern crate failure;
extern crate failure_derive;
extern crate rusqlite;
extern crate core_traits;
extern crate edn;
pub mod errors;

View file

@ -1,29 +1,27 @@
[package] [package]
name = "mentat_db" name = "mentat_db"
version = "0.0.2" version = "0.0.1"
workspace = ".." workspace = ".."
[features] [features]
default = [] default = []
sqlcipher = ["rusqlite/sqlcipher"] sqlcipher = ["rusqlite/sqlcipher"]
syncable = ["serde", "serde_json", "serde_derive"]
[dependencies] [dependencies]
failure = "~0.1" failure = "0.1.1"
indexmap = "~1.9" failure_derive = "0.1.1"
itertools = "~0.10" indexmap = "1"
lazy_static = "~1.4" itertools = "0.7"
log = "~0.4" lazy_static = "0.2"
ordered-float = "~2.8" log = "0.4"
time = "~0.3" num = "0.1"
petgraph = "~0.6" ordered-float = "0.5"
serde = { version = "~1.0", optional = true } time = "0.1"
serde_json = { version = "~1.0", optional = true } petgraph = "0.4.12"
serde_derive = { version = "~1.0", optional = true }
[dependencies.rusqlite] [dependencies.rusqlite]
version = "~0.29" version = "0.13"
features = ["limits", "bundled"] features = ["limits"]
[dependencies.edn] [dependencies.edn]
path = "../edn" path = "../edn"
@ -31,19 +29,12 @@ path = "../edn"
[dependencies.mentat_core] [dependencies.mentat_core]
path = "../core" path = "../core"
[dependencies.core_traits]
path = "../core-traits"
[dependencies.db_traits]
path = "../db-traits"
[dependencies.mentat_sql] [dependencies.mentat_sql]
path = "../sql" path = "../sql"
# TODO: This should be in dev-dependencies. # Should be dev-dependencies.
[dependencies.tabwriter] [dependencies.tabwriter]
version = "~1.2" version = "1.0.3"
[dev-dependencies] [dev-dependencies]
env_logger = "0.9" env_logger = "0.5"
#tabwriter = { version = "1.2.1" }

View file

@ -24,10 +24,7 @@ pub struct AddRetractAlterSet<K, V> {
pub altered: BTreeMap<K, (V, V)>, pub altered: BTreeMap<K, (V, V)>,
} }
impl<K, V> Default for AddRetractAlterSet<K, V> impl<K, V> Default for AddRetractAlterSet<K, V> where K: Ord {
where
K: Ord,
{
fn default() -> AddRetractAlterSet<K, V> { fn default() -> AddRetractAlterSet<K, V> {
AddRetractAlterSet { AddRetractAlterSet {
asserted: BTreeMap::default(), asserted: BTreeMap::default(),
@ -37,10 +34,7 @@ where
} }
} }
impl<K, V> AddRetractAlterSet<K, V> impl<K, V> AddRetractAlterSet<K, V> where K: Ord {
where
K: Ord,
{
pub fn witness(&mut self, key: K, value: V, added: bool) { pub fn witness(&mut self, key: K, value: V, added: bool) {
if added { if added {
if let Some(retracted_value) = self.retracted.remove(&key) { if let Some(retracted_value) = self.retracted.remove(&key) {
@ -48,10 +42,12 @@ where
} else { } else {
self.asserted.insert(key, value); self.asserted.insert(key, value);
} }
} else if let Some(asserted_value) = self.asserted.remove(&key) {
self.altered.insert(key, (value, asserted_value));
} else { } else {
self.retracted.insert(key, value); if let Some(asserted_value) = self.asserted.remove(&key) {
self.altered.insert(key, (value, asserted_value));
} else {
self.retracted.insert(key, value);
}
} }
} }
} }

View file

@ -10,24 +10,29 @@
#![allow(dead_code)] #![allow(dead_code)]
use crate::db::TypedSQLValue;
use crate::entids;
use db_traits::errors::{DbErrorKind, Result};
use edn; use edn;
use edn::entities::Entity; use errors::{
use edn::symbols; DbErrorKind,
Result,
};
use edn::types::Value; use edn::types::Value;
use edn::symbols;
use core_traits::{values, TypedValue}; use entids;
use db::TypedSQLValue;
use crate::schema::SchemaBuilding; use edn::entities::Entity;
use crate::types::{Partition, PartitionMap}; use mentat_core::{
use mentat_core::{IdentMap, Schema}; IdentMap,
Schema,
TypedValue,
values,
};
use schema::SchemaBuilding;
use types::{Partition, PartitionMap};
/// The first transaction ID applied to the knowledge base. /// The first transaction ID applied to the knowledge base.
/// ///
/// This is the start of the :db.part/tx partition. /// This is the start of the :db.part/tx partition.
pub const TX0: i64 = 0x1000_0000; pub const TX0: i64 = 0x10000000;
/// This is the start of the :db.part/user partition. /// This is the start of the :db.part/user partition.
pub const USER0: i64 = 0x10000; pub const USER0: i64 = 0x10000;
@ -37,118 +42,76 @@ pub const CORE_SCHEMA_VERSION: u32 = 1;
lazy_static! { lazy_static! {
static ref V1_IDENTS: [(symbols::Keyword, i64); 40] = { static ref V1_IDENTS: [(symbols::Keyword, i64); 40] = {
[ [(ns_keyword!("db", "ident"), entids::DB_IDENT),
(ns_keyword!("db", "ident"), entids::DB_IDENT), (ns_keyword!("db.part", "db"), entids::DB_PART_DB),
(ns_keyword!("db.part", "db"), entids::DB_PART_DB), (ns_keyword!("db", "txInstant"), entids::DB_TX_INSTANT),
(ns_keyword!("db", "txInstant"), entids::DB_TX_INSTANT), (ns_keyword!("db.install", "partition"), entids::DB_INSTALL_PARTITION),
( (ns_keyword!("db.install", "valueType"), entids::DB_INSTALL_VALUE_TYPE),
ns_keyword!("db.install", "partition"), (ns_keyword!("db.install", "attribute"), entids::DB_INSTALL_ATTRIBUTE),
entids::DB_INSTALL_PARTITION, (ns_keyword!("db", "valueType"), entids::DB_VALUE_TYPE),
), (ns_keyword!("db", "cardinality"), entids::DB_CARDINALITY),
( (ns_keyword!("db", "unique"), entids::DB_UNIQUE),
ns_keyword!("db.install", "valueType"), (ns_keyword!("db", "isComponent"), entids::DB_IS_COMPONENT),
entids::DB_INSTALL_VALUE_TYPE, (ns_keyword!("db", "index"), entids::DB_INDEX),
), (ns_keyword!("db", "fulltext"), entids::DB_FULLTEXT),
( (ns_keyword!("db", "noHistory"), entids::DB_NO_HISTORY),
ns_keyword!("db.install", "attribute"), (ns_keyword!("db", "add"), entids::DB_ADD),
entids::DB_INSTALL_ATTRIBUTE, (ns_keyword!("db", "retract"), entids::DB_RETRACT),
), (ns_keyword!("db.part", "user"), entids::DB_PART_USER),
(ns_keyword!("db", "valueType"), entids::DB_VALUE_TYPE), (ns_keyword!("db.part", "tx"), entids::DB_PART_TX),
(ns_keyword!("db", "cardinality"), entids::DB_CARDINALITY), (ns_keyword!("db", "excise"), entids::DB_EXCISE),
(ns_keyword!("db", "unique"), entids::DB_UNIQUE), (ns_keyword!("db.excise", "attrs"), entids::DB_EXCISE_ATTRS),
(ns_keyword!("db", "isComponent"), entids::DB_IS_COMPONENT), (ns_keyword!("db.excise", "beforeT"), entids::DB_EXCISE_BEFORE_T),
(ns_keyword!("db", "index"), entids::DB_INDEX), (ns_keyword!("db.excise", "before"), entids::DB_EXCISE_BEFORE),
(ns_keyword!("db", "fulltext"), entids::DB_FULLTEXT), (ns_keyword!("db.alter", "attribute"), entids::DB_ALTER_ATTRIBUTE),
(ns_keyword!("db", "noHistory"), entids::DB_NO_HISTORY), (ns_keyword!("db.type", "ref"), entids::DB_TYPE_REF),
(ns_keyword!("db", "add"), entids::DB_ADD), (ns_keyword!("db.type", "keyword"), entids::DB_TYPE_KEYWORD),
(ns_keyword!("db", "retract"), entids::DB_RETRACT), (ns_keyword!("db.type", "long"), entids::DB_TYPE_LONG),
(ns_keyword!("db.part", "user"), entids::DB_PART_USER), (ns_keyword!("db.type", "double"), entids::DB_TYPE_DOUBLE),
(ns_keyword!("db.part", "tx"), entids::DB_PART_TX), (ns_keyword!("db.type", "string"), entids::DB_TYPE_STRING),
(ns_keyword!("db", "excise"), entids::DB_EXCISE), (ns_keyword!("db.type", "uuid"), entids::DB_TYPE_UUID),
(ns_keyword!("db.excise", "attrs"), entids::DB_EXCISE_ATTRS), (ns_keyword!("db.type", "uri"), entids::DB_TYPE_URI),
( (ns_keyword!("db.type", "boolean"), entids::DB_TYPE_BOOLEAN),
ns_keyword!("db.excise", "beforeT"), (ns_keyword!("db.type", "instant"), entids::DB_TYPE_INSTANT),
entids::DB_EXCISE_BEFORE_T, (ns_keyword!("db.type", "bytes"), entids::DB_TYPE_BYTES),
), (ns_keyword!("db.cardinality", "one"), entids::DB_CARDINALITY_ONE),
(ns_keyword!("db.excise", "before"), entids::DB_EXCISE_BEFORE), (ns_keyword!("db.cardinality", "many"), entids::DB_CARDINALITY_MANY),
( (ns_keyword!("db.unique", "value"), entids::DB_UNIQUE_VALUE),
ns_keyword!("db.alter", "attribute"), (ns_keyword!("db.unique", "identity"), entids::DB_UNIQUE_IDENTITY),
entids::DB_ALTER_ATTRIBUTE, (ns_keyword!("db", "doc"), entids::DB_DOC),
), (ns_keyword!("db.schema", "version"), entids::DB_SCHEMA_VERSION),
(ns_keyword!("db.type", "ref"), entids::DB_TYPE_REF), (ns_keyword!("db.schema", "attribute"), entids::DB_SCHEMA_ATTRIBUTE),
(ns_keyword!("db.type", "keyword"), entids::DB_TYPE_KEYWORD), (ns_keyword!("db.schema", "core"), entids::DB_SCHEMA_CORE),
(ns_keyword!("db.type", "long"), entids::DB_TYPE_LONG),
(ns_keyword!("db.type", "double"), entids::DB_TYPE_DOUBLE),
(ns_keyword!("db.type", "string"), entids::DB_TYPE_STRING),
(ns_keyword!("db.type", "uuid"), entids::DB_TYPE_UUID),
(ns_keyword!("db.type", "uri"), entids::DB_TYPE_URI),
(ns_keyword!("db.type", "boolean"), entids::DB_TYPE_BOOLEAN),
(ns_keyword!("db.type", "instant"), entids::DB_TYPE_INSTANT),
(ns_keyword!("db.type", "bytes"), entids::DB_TYPE_BYTES),
(
ns_keyword!("db.cardinality", "one"),
entids::DB_CARDINALITY_ONE,
),
(
ns_keyword!("db.cardinality", "many"),
entids::DB_CARDINALITY_MANY,
),
(ns_keyword!("db.unique", "value"), entids::DB_UNIQUE_VALUE),
(
ns_keyword!("db.unique", "identity"),
entids::DB_UNIQUE_IDENTITY,
),
(ns_keyword!("db", "doc"), entids::DB_DOC),
(
ns_keyword!("db.schema", "version"),
entids::DB_SCHEMA_VERSION,
),
(
ns_keyword!("db.schema", "attribute"),
entids::DB_SCHEMA_ATTRIBUTE,
),
(ns_keyword!("db.schema", "core"), entids::DB_SCHEMA_CORE),
] ]
}; };
pub static ref V1_PARTS: [(symbols::Keyword, i64, i64, i64, bool); 3] = {
[ static ref V1_PARTS: [(symbols::Keyword, i64, i64); 3] = {
( [(ns_keyword!("db.part", "db"), 0, (1 + V1_IDENTS.len()) as i64),
ns_keyword!("db.part", "db"), (ns_keyword!("db.part", "user"), USER0, USER0),
0, (ns_keyword!("db.part", "tx"), TX0, TX0),
USER0 - 1,
(1 + V1_IDENTS.len()) as i64,
false,
),
(ns_keyword!("db.part", "user"), USER0, TX0 - 1, USER0, true),
(
ns_keyword!("db.part", "tx"),
TX0,
i64::max_value(),
TX0,
false,
),
] ]
}; };
static ref V1_CORE_SCHEMA: [symbols::Keyword; 16] = {
[ static ref V1_CORE_SCHEMA: [(symbols::Keyword); 16] = {
(ns_keyword!("db", "ident")), [(ns_keyword!("db", "ident")),
(ns_keyword!("db.install", "partition")), (ns_keyword!("db.install", "partition")),
(ns_keyword!("db.install", "valueType")), (ns_keyword!("db.install", "valueType")),
(ns_keyword!("db.install", "attribute")), (ns_keyword!("db.install", "attribute")),
(ns_keyword!("db", "txInstant")), (ns_keyword!("db", "txInstant")),
(ns_keyword!("db", "valueType")), (ns_keyword!("db", "valueType")),
(ns_keyword!("db", "cardinality")), (ns_keyword!("db", "cardinality")),
(ns_keyword!("db", "doc")), (ns_keyword!("db", "doc")),
(ns_keyword!("db", "unique")), (ns_keyword!("db", "unique")),
(ns_keyword!("db", "isComponent")), (ns_keyword!("db", "isComponent")),
(ns_keyword!("db", "index")), (ns_keyword!("db", "index")),
(ns_keyword!("db", "fulltext")), (ns_keyword!("db", "fulltext")),
(ns_keyword!("db", "noHistory")), (ns_keyword!("db", "noHistory")),
(ns_keyword!("db.alter", "attribute")), (ns_keyword!("db.alter", "attribute")),
(ns_keyword!("db.schema", "version")), (ns_keyword!("db.schema", "version")),
(ns_keyword!("db.schema", "attribute")), (ns_keyword!("db.schema", "attribute")),
] ]
}; };
static ref V1_SYMBOLIC_SCHEMA: Value = { static ref V1_SYMBOLIC_SCHEMA: Value = {
let s = r#" let s = r#"
{:db/ident {:db/valueType :db.type/keyword {:db/ident {:db/valueType :db.type/keyword
@ -196,9 +159,7 @@ lazy_static! {
:db/cardinality :db.cardinality/many}}"#; :db/cardinality :db.cardinality/many}}"#;
edn::parse::value(s) edn::parse::value(s)
.map(|v| v.without_spans()) .map(|v| v.without_spans())
.map_err(|_| { .map_err(|_| DbErrorKind::BadBootstrapDefinition("Unable to parse V1_SYMBOLIC_SCHEMA".into()))
DbErrorKind::BadBootstrapDefinition("Unable to parse V1_SYMBOLIC_SCHEMA".into())
})
.unwrap() .unwrap()
}; };
} }
@ -206,15 +167,10 @@ lazy_static! {
/// Convert (ident, entid) pairs into [:db/add IDENT :db/ident IDENT] `Value` instances. /// Convert (ident, entid) pairs into [:db/add IDENT :db/ident IDENT] `Value` instances.
fn idents_to_assertions(idents: &[(symbols::Keyword, i64)]) -> Vec<Value> { fn idents_to_assertions(idents: &[(symbols::Keyword, i64)]) -> Vec<Value> {
idents idents
.iter() .into_iter()
.map(|&(ref ident, _)| { .map(|&(ref ident, _)| {
let value = Value::Keyword(ident.clone()); let value = Value::Keyword(ident.clone());
Value::Vector(vec![ Value::Vector(vec![values::DB_ADD.clone(), value.clone(), values::DB_IDENT.clone(), value.clone()])
values::DB_ADD.clone(),
value.clone(),
values::DB_IDENT.clone(),
value,
])
}) })
.collect() .collect()
} }
@ -225,22 +181,18 @@ fn schema_attrs_to_assertions(version: u32, idents: &[symbols::Keyword]) -> Vec<
let schema_attr = Value::Keyword(ns_keyword!("db.schema", "attribute")); let schema_attr = Value::Keyword(ns_keyword!("db.schema", "attribute"));
let schema_version = Value::Keyword(ns_keyword!("db.schema", "version")); let schema_version = Value::Keyword(ns_keyword!("db.schema", "version"));
idents idents
.iter() .into_iter()
.map(|ident| { .map(|ident| {
let value = Value::Keyword(ident.clone()); let value = Value::Keyword(ident.clone());
Value::Vector(vec![ Value::Vector(vec![values::DB_ADD.clone(),
values::DB_ADD.clone(), schema_core.clone(),
schema_core.clone(), schema_attr.clone(),
schema_attr.clone(), value])
value,
])
}) })
.chain(::std::iter::once(Value::Vector(vec![ .chain(::std::iter::once(Value::Vector(vec![values::DB_ADD.clone(),
values::DB_ADD.clone(), schema_core.clone(),
schema_core.clone(), schema_version,
schema_version, Value::Integer(version as i64)])))
Value::Integer(version as i64),
])))
.collect() .collect()
} }
@ -249,10 +201,7 @@ fn schema_attrs_to_assertions(version: u32, idents: &[symbols::Keyword]) -> Vec<
/// ///
/// Such triples are closer to what the transactor will produce when processing attribute /// Such triples are closer to what the transactor will produce when processing attribute
/// assertions. /// assertions.
fn symbolic_schema_to_triples( fn symbolic_schema_to_triples(ident_map: &IdentMap, symbolic_schema: &Value) -> Result<Vec<(symbols::Keyword, symbols::Keyword, TypedValue)>> {
ident_map: &IdentMap,
symbolic_schema: &Value,
) -> Result<Vec<(symbols::Keyword, symbols::Keyword, TypedValue)>> {
// Failure here is a coding error, not a runtime error. // Failure here is a coding error, not a runtime error.
let mut triples: Vec<(symbols::Keyword, symbols::Keyword, TypedValue)> = vec![]; let mut triples: Vec<(symbols::Keyword, symbols::Keyword, TypedValue)> = vec![];
// TODO: Consider `flat_map` and `map` rather than loop. // TODO: Consider `flat_map` and `map` rather than loop.
@ -260,22 +209,16 @@ fn symbolic_schema_to_triples(
Value::Map(ref m) => { Value::Map(ref m) => {
for (ident, mp) in m { for (ident, mp) in m {
let ident = match ident { let ident = match ident {
Value::Keyword(ref ident) => ident, &Value::Keyword(ref ident) => ident,
_ => bail!(DbErrorKind::BadBootstrapDefinition(format!( _ => bail!(DbErrorKind::BadBootstrapDefinition(format!("Expected namespaced keyword for ident but got '{:?}'", ident))),
"Expected namespaced keyword for ident but got '{:?}'",
ident
))),
}; };
match *mp { match *mp {
Value::Map(ref mpp) => { Value::Map(ref mpp) => {
for (attr, value) in mpp { for (attr, value) in mpp {
let attr = match attr { let attr = match attr {
Value::Keyword(ref attr) => attr, &Value::Keyword(ref attr) => attr,
_ => bail!(DbErrorKind::BadBootstrapDefinition(format!( _ => bail!(DbErrorKind::BadBootstrapDefinition(format!("Expected namespaced keyword for attr but got '{:?}'", attr))),
"Expected namespaced keyword for attr but got '{:?}'", };
attr
))),
};
// We have symbolic idents but the transactor handles entids. Ad-hoc // We have symbolic idents but the transactor handles entids. Ad-hoc
// convert right here. This is a fundamental limitation on the // convert right here. This is a fundamental limitation on the
@ -286,27 +229,23 @@ fn symbolic_schema_to_triples(
// bootstrap symbolic schema, or by representing the initial bootstrap // bootstrap symbolic schema, or by representing the initial bootstrap
// schema directly as Rust data. // schema directly as Rust data.
let typed_value = match TypedValue::from_edn_value(value) { let typed_value = match TypedValue::from_edn_value(value) {
Some(TypedValue::Keyword(ref k)) => ident_map Some(TypedValue::Keyword(ref k)) => {
.get(k) ident_map.get(k)
.map(|entid| TypedValue::Ref(*entid)) .map(|entid| TypedValue::Ref(*entid))
.ok_or_else(|| DbErrorKind::UnrecognizedIdent(k.to_string()))?, .ok_or(DbErrorKind::UnrecognizedIdent(k.to_string()))?
},
Some(v) => v, Some(v) => v,
_ => bail!(DbErrorKind::BadBootstrapDefinition(format!( _ => bail!(DbErrorKind::BadBootstrapDefinition(format!("Expected Mentat typed value for value but got '{:?}'", value)))
"Expected Mentat typed value for value but got '{:?}'",
value
))),
}; };
triples.push((ident.clone(), attr.clone(), typed_value)); triples.push((ident.clone(), attr.clone(), typed_value));
} }
} },
_ => bail!(DbErrorKind::BadBootstrapDefinition( _ => bail!(DbErrorKind::BadBootstrapDefinition("Expected {:db/ident {:db/attr value ...} ...}".into()))
"Expected {:db/ident {:db/attr value ...} ...}".into()
)),
} }
} }
} },
_ => bail!(DbErrorKind::BadBootstrapDefinition("Expected {...}".into())), _ => bail!(DbErrorKind::BadBootstrapDefinition("Expected {...}".into()))
} }
Ok(triples) Ok(triples)
} }
@ -321,62 +260,48 @@ fn symbolic_schema_to_assertions(symbolic_schema: &Value) -> Result<Vec<Value>>
match *mp { match *mp {
Value::Map(ref mpp) => { Value::Map(ref mpp) => {
for (attr, value) in mpp { for (attr, value) in mpp {
assertions.push(Value::Vector(vec![ assertions.push(Value::Vector(vec![values::DB_ADD.clone(),
values::DB_ADD.clone(), ident.clone(),
ident.clone(), attr.clone(),
attr.clone(), value.clone()]));
value.clone(),
]));
} }
} },
_ => bail!(DbErrorKind::BadBootstrapDefinition( _ => bail!(DbErrorKind::BadBootstrapDefinition("Expected {:db/ident {:db/attr value ...} ...}".into()))
"Expected {:db/ident {:db/attr value ...} ...}".into()
)),
} }
} }
} },
_ => bail!(DbErrorKind::BadBootstrapDefinition("Expected {...}".into())), _ => bail!(DbErrorKind::BadBootstrapDefinition("Expected {...}".into()))
} }
Ok(assertions) Ok(assertions)
} }
pub(crate) fn bootstrap_partition_map() -> PartitionMap { pub(crate) fn bootstrap_partition_map() -> PartitionMap {
V1_PARTS V1_PARTS.iter()
.iter() .map(|&(ref part, start, index)| (part.to_string(), Partition::new(start, index)))
.map(|&(ref part, start, end, index, allow_excision)| { .collect()
(
part.to_string(),
Partition::new(start, end, index, allow_excision),
)
})
.collect()
} }
pub(crate) fn bootstrap_ident_map() -> IdentMap { pub(crate) fn bootstrap_ident_map() -> IdentMap {
V1_IDENTS V1_IDENTS.iter()
.iter() .map(|&(ref ident, entid)| (ident.clone(), entid))
.map(|&(ref ident, entid)| (ident.clone(), entid)) .collect()
.collect()
} }
pub(crate) fn bootstrap_schema() -> Schema { pub(crate) fn bootstrap_schema() -> Schema {
let ident_map = bootstrap_ident_map(); let ident_map = bootstrap_ident_map();
let bootstrap_triples = let bootstrap_triples = symbolic_schema_to_triples(&ident_map, &V1_SYMBOLIC_SCHEMA).expect("symbolic schema");
symbolic_schema_to_triples(&ident_map, &V1_SYMBOLIC_SCHEMA).expect("symbolic schema");
Schema::from_ident_map_and_triples(ident_map, bootstrap_triples).unwrap() Schema::from_ident_map_and_triples(ident_map, bootstrap_triples).unwrap()
} }
pub(crate) fn bootstrap_entities() -> Vec<Entity<edn::ValueAndSpan>> { pub(crate) fn bootstrap_entities() -> Vec<Entity<edn::ValueAndSpan>> {
let bootstrap_assertions: Value = Value::Vector( let bootstrap_assertions: Value = Value::Vector([
[ symbolic_schema_to_assertions(&V1_SYMBOLIC_SCHEMA).expect("symbolic schema"),
symbolic_schema_to_assertions(&V1_SYMBOLIC_SCHEMA).expect("symbolic schema"), idents_to_assertions(&V1_IDENTS[..]),
idents_to_assertions(&V1_IDENTS[..]), schema_attrs_to_assertions(CORE_SCHEMA_VERSION, V1_CORE_SCHEMA.as_ref()),
schema_attrs_to_assertions(CORE_SCHEMA_VERSION, V1_CORE_SCHEMA.as_ref()), ].concat());
]
.concat(),
);
// Failure here is a coding error (since the inputs are fixed), not a runtime error. // Failure here is a coding error (since the inputs are fixed), not a runtime error.
// TODO: represent these bootstrap entity data errors rather than just panicing. // TODO: represent these bootstrap data errors rather than just panicing.
edn::parse::entities(&bootstrap_assertions.to_string()).expect("bootstrap assertions") let bootstrap_entities: Vec<Entity<edn::ValueAndSpan>> = edn::parse::entities(&bootstrap_assertions.to_string()).expect("bootstrap assertions");
return bootstrap_entities;
} }

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -9,90 +9,45 @@
// specific language governing permissions and limitations under the License. // specific language governing permissions and limitations under the License.
#![allow(dead_code)] #![allow(dead_code)]
#![allow(unused_macros)]
/// Low-level functions for testing. /// Low-level functions for testing.
// Macro to parse a `Borrow<str>` to an `edn::Value` and assert the given `edn::Value` `matches`
// against it.
//
// This is a macro only to give nice line numbers when tests fail.
#[macro_export]
macro_rules! assert_matches {
( $input: expr, $expected: expr ) => {{
// Failure to parse the expected pattern is a coding error, so we unwrap.
let pattern_value = edn::parse::value($expected.borrow())
.expect(format!("to be able to parse expected {}", $expected).as_str())
.without_spans();
let input_value = $input.to_edn();
assert!(
input_value.matches(&pattern_value),
"Expected value:\n{}\nto match pattern:\n{}\n",
input_value.to_pretty(120).unwrap(),
pattern_value.to_pretty(120).unwrap()
);
}};
}
// Transact $input against the given $conn, expecting success or a `Result<TxReport, String>`.
//
// This unwraps safely and makes asserting errors pleasant.
#[macro_export]
macro_rules! assert_transact {
( $conn: expr, $input: expr, $expected: expr ) => {{
trace!("assert_transact: {}", $input);
let result = $conn.transact($input).map_err(|e| e.to_string());
assert_eq!(result, $expected.map_err(|e| e.to_string()));
}};
( $conn: expr, $input: expr ) => {{
trace!("assert_transact: {}", $input);
let result = $conn.transact($input);
assert!(
result.is_ok(),
"Expected Ok(_), got `{}`",
result.unwrap_err()
);
result.unwrap()
}};
}
use std::borrow::Borrow; use std::borrow::Borrow;
use std::collections::BTreeMap; use std::io::{Write};
use std::io::Write;
use itertools::Itertools; use itertools::Itertools;
use rusqlite; use rusqlite;
use rusqlite::types::ToSql; use rusqlite::types::{ToSql};
use rusqlite::TransactionBehavior;
use tabwriter::TabWriter; use tabwriter::TabWriter;
use crate::bootstrap; use bootstrap;
use crate::db::*; use db::TypedSQLValue;
use crate::db::{read_attribute_map, read_ident_map};
use crate::entids;
use db_traits::errors::Result;
use edn; use edn;
use entids;
use core_traits::{Entid, TypedValue, ValueType}; use errors::Result;
use mentat_core::{
use crate::internal_types::TermWithTempIds; HasSchema,
use crate::schema::SchemaBuilding; SQLValueType,
use crate::tx::{transact, transact_terms}; TypedValue,
use crate::types::*; ValueType,
use crate::watcher::NullWatcher; };
use edn::entities::{EntidOrIdent, TempId}; use edn::entities::{
use edn::InternSet; EntidOrIdent,
use mentat_core::{HasSchema, SQLValueType, TxReport}; };
use schema::{
SchemaBuilding,
};
use types::Schema;
/// Represents a *datom* (assertion) in the store. /// Represents a *datom* (assertion) in the store.
#[derive(Clone, Debug, Eq, Hash, Ord, PartialOrd, PartialEq)] #[derive(Clone,Debug,Eq,Hash,Ord,PartialOrd,PartialEq)]
pub struct Datom { pub(crate) struct Datom {
// TODO: generalize this. // TODO: generalize this.
pub e: EntidOrIdent, e: EntidOrIdent,
pub a: EntidOrIdent, a: EntidOrIdent,
pub v: edn::Value, v: edn::Value,
pub tx: i64, tx: i64,
pub added: Option<bool>, added: Option<bool>,
} }
/// Represents a set of datoms (assertions) in the store. /// Represents a set of datoms (assertions) in the store.
@ -100,7 +55,7 @@ pub struct Datom {
/// To make comparision easier, we deterministically order. The ordering is the ascending tuple /// To make comparision easier, we deterministically order. The ordering is the ascending tuple
/// ordering determined by `(e, a, (value_type_tag, v), tx)`, where `value_type_tag` is an internal /// ordering determined by `(e, a, (value_type_tag, v), tx)`, where `value_type_tag` is an internal
/// value that is not exposed but is deterministic. /// value that is not exposed but is deterministic.
pub struct Datoms(pub Vec<Datom>); pub(crate) struct Datoms(pub Vec<Datom>);
/// Represents an ordered sequence of transactions in the store. /// Represents an ordered sequence of transactions in the store.
/// ///
@ -108,16 +63,16 @@ pub struct Datoms(pub Vec<Datom>);
/// ordering determined by `(e, a, (value_type_tag, v), tx, added)`, where `value_type_tag` is an /// ordering determined by `(e, a, (value_type_tag, v), tx, added)`, where `value_type_tag` is an
/// internal value that is not exposed but is deterministic, and `added` is ordered such that /// internal value that is not exposed but is deterministic, and `added` is ordered such that
/// retracted assertions appear before added assertions. /// retracted assertions appear before added assertions.
pub struct Transactions(pub Vec<Datoms>); pub(crate) struct Transactions(pub Vec<Datoms>);
/// Represents the fulltext values in the store. /// Represents the fulltext values in the store.
pub struct FulltextValues(pub Vec<(i64, String)>); pub(crate) struct FulltextValues(pub Vec<(i64, String)>);
impl Datom { impl Datom {
pub fn to_edn(&self) -> edn::Value { pub(crate) fn into_edn(&self) -> edn::Value {
let f = |entid: &EntidOrIdent| -> edn::Value { let f = |entid: &EntidOrIdent| -> edn::Value {
match *entid { match *entid {
EntidOrIdent::Entid(ref y) => edn::Value::Integer(*y), EntidOrIdent::Entid(ref y) => edn::Value::Integer(y.clone()),
EntidOrIdent::Ident(ref y) => edn::Value::Keyword(y.clone()), EntidOrIdent::Ident(ref y) => edn::Value::Keyword(y.clone()),
} }
}; };
@ -133,43 +88,32 @@ impl Datom {
} }
impl Datoms { impl Datoms {
pub fn to_edn(&self) -> edn::Value { pub(crate) fn into_edn(&self) -> edn::Value {
edn::Value::Vector((&self.0).iter().map(|x| x.to_edn()).collect()) edn::Value::Vector((&self.0).into_iter().map(|x| x.into_edn()).collect())
} }
} }
impl Transactions { impl Transactions {
pub fn to_edn(&self) -> edn::Value { pub(crate) fn into_edn(&self) -> edn::Value {
edn::Value::Vector((&self.0).iter().map(|x| x.to_edn()).collect()) edn::Value::Vector((&self.0).into_iter().map(|x| x.into_edn()).collect())
} }
} }
impl FulltextValues { impl FulltextValues {
pub fn to_edn(&self) -> edn::Value { pub(crate) fn into_edn(&self) -> edn::Value {
edn::Value::Vector( edn::Value::Vector((&self.0).into_iter().map(|&(x, ref y)| edn::Value::Vector(vec![edn::Value::Integer(x), edn::Value::Text(y.clone())])).collect())
(&self.0)
.iter()
.map(|&(x, ref y)| {
edn::Value::Vector(vec![edn::Value::Integer(x), edn::Value::Text(y.clone())])
})
.collect(),
)
} }
} }
/// Turn TypedValue::Ref into TypedValue::Keyword when it is possible. /// Turn TypedValue::Ref into TypedValue::Keyword when it is possible.
trait ToIdent { trait ToIdent {
fn map_ident(self, schema: &Schema) -> Self; fn map_ident(self, schema: &Schema) -> Self;
} }
impl ToIdent for TypedValue { impl ToIdent for TypedValue {
fn map_ident(self, schema: &Schema) -> Self { fn map_ident(self, schema: &Schema) -> Self {
if let TypedValue::Ref(e) = self { if let TypedValue::Ref(e) = self {
schema schema.get_ident(e).cloned().map(|i| i.into()).unwrap_or(TypedValue::Ref(e))
.get_ident(e)
.cloned()
.map(|i| i.into())
.unwrap_or(TypedValue::Ref(e))
} else { } else {
self self
} }
@ -177,22 +121,13 @@ impl ToIdent for TypedValue {
} }
/// Convert a numeric entid to an ident `Entid` if possible, otherwise a numeric `Entid`. /// Convert a numeric entid to an ident `Entid` if possible, otherwise a numeric `Entid`.
pub fn to_entid(schema: &Schema, entid: i64) -> EntidOrIdent { fn to_entid(schema: &Schema, entid: i64) -> EntidOrIdent {
schema schema.get_ident(entid).map_or(EntidOrIdent::Entid(entid), |ident| EntidOrIdent::Ident(ident.clone()))
.get_ident(entid)
.map_or(EntidOrIdent::Entid(entid), |ident| {
EntidOrIdent::Ident(ident.clone())
})
} }
// /// Convert a symbolic ident to an ident `Entid` if possible, otherwise a numeric `Entid`.
// pub fn to_ident(schema: &Schema, entid: i64) -> Entid {
// schema.get_ident(entid).map_or(Entid::Entid(entid), |ident| Entid::Ident(ident.clone()))
// }
/// Return the set of datoms in the store, ordered by (e, a, v, tx), but not including any datoms of /// Return the set of datoms in the store, ordered by (e, a, v, tx), but not including any datoms of
/// the form [... :db/txInstant ...]. /// the form [... :db/txInstant ...].
pub fn datoms<S: Borrow<Schema>>(conn: &rusqlite::Connection, schema: &S) -> Result<Datoms> { pub(crate) fn datoms<S: Borrow<Schema>>(conn: &rusqlite::Connection, schema: &S) -> Result<Datoms> {
datoms_after(conn, schema, bootstrap::TX0 - 1) datoms_after(conn, schema, bootstrap::TX0 - 1)
} }
@ -200,49 +135,38 @@ pub fn datoms<S: Borrow<Schema>>(conn: &rusqlite::Connection, schema: &S) -> Res
/// ordered by (e, a, v, tx). /// ordered by (e, a, v, tx).
/// ///
/// The datom set returned does not include any datoms of the form [... :db/txInstant ...]. /// The datom set returned does not include any datoms of the form [... :db/txInstant ...].
pub fn datoms_after<S: Borrow<Schema>>( pub(crate) fn datoms_after<S: Borrow<Schema>>(conn: &rusqlite::Connection, schema: &S, tx: i64) -> Result<Datoms> {
conn: &rusqlite::Connection,
schema: &S,
tx: i64,
) -> Result<Datoms> {
let borrowed_schema = schema.borrow(); let borrowed_schema = schema.borrow();
let mut stmt: rusqlite::Statement = conn.prepare("SELECT e, a, v, value_type_tag, tx FROM datoms WHERE tx > ? ORDER BY e ASC, a ASC, value_type_tag ASC, v ASC, tx ASC")?; let mut stmt: rusqlite::Statement = conn.prepare("SELECT e, a, v, value_type_tag, tx FROM datoms WHERE tx > ? ORDER BY e ASC, a ASC, value_type_tag ASC, v ASC, tx ASC")?;
let r: Result<Vec<_>> = stmt let r: Result<Vec<_>> = stmt.query_and_then(&[&tx], |row| {
.query_and_then(&[&tx], |row| { let e: i64 = row.get_checked(0)?;
let e: i64 = row.get(0)?; let a: i64 = row.get_checked(1)?;
let a: i64 = row.get(1)?;
if a == entids::DB_TX_INSTANT { if a == entids::DB_TX_INSTANT {
return Ok(None); return Ok(None);
} }
let v: rusqlite::types::Value = row.get(2)?; let v: rusqlite::types::Value = row.get_checked(2)?;
let value_type_tag: i32 = row.get(3)?; let value_type_tag: i32 = row.get_checked(3)?;
let attribute = borrowed_schema.require_attribute_for_entid(a)?; let attribute = borrowed_schema.require_attribute_for_entid(a)?;
let value_type_tag = if !attribute.fulltext { let value_type_tag = if !attribute.fulltext { value_type_tag } else { ValueType::Long.value_type_tag() };
value_type_tag
} else {
ValueType::Long.value_type_tag()
};
let typed_value = let typed_value = TypedValue::from_sql_value_pair(v, value_type_tag)?.map_ident(borrowed_schema);
TypedValue::from_sql_value_pair(v, value_type_tag)?.map_ident(borrowed_schema); let (value, _) = typed_value.to_edn_value_pair();
let (value, _) = typed_value.to_edn_value_pair();
let tx: i64 = row.get(4)?; let tx: i64 = row.get_checked(4)?;
Ok(Some(Datom { Ok(Some(Datom {
e: EntidOrIdent::Entid(e), e: EntidOrIdent::Entid(e),
a: to_entid(borrowed_schema, a), a: to_entid(borrowed_schema, a),
v: value, v: value,
tx, tx: tx,
added: None, added: None,
})) }))
})? })?.collect();
.collect();
Ok(Datoms(r?.into_iter().filter_map(|x| x).collect())) Ok(Datoms(r?.into_iter().filter_map(|x| x).collect()))
} }
@ -251,69 +175,50 @@ pub fn datoms_after<S: Borrow<Schema>>(
/// given `tx`, ordered by (tx, e, a, v). /// given `tx`, ordered by (tx, e, a, v).
/// ///
/// Each transaction returned includes the [(transaction-tx) :db/txInstant ...] datom. /// Each transaction returned includes the [(transaction-tx) :db/txInstant ...] datom.
pub fn transactions_after<S: Borrow<Schema>>( pub(crate) fn transactions_after<S: Borrow<Schema>>(conn: &rusqlite::Connection, schema: &S, tx: i64) -> Result<Transactions> {
conn: &rusqlite::Connection,
schema: &S,
tx: i64,
) -> Result<Transactions> {
let borrowed_schema = schema.borrow(); let borrowed_schema = schema.borrow();
let mut stmt: rusqlite::Statement = conn.prepare("SELECT e, a, v, value_type_tag, tx, added FROM transactions WHERE tx > ? ORDER BY tx ASC, e ASC, a ASC, value_type_tag ASC, v ASC, added ASC")?; let mut stmt: rusqlite::Statement = conn.prepare("SELECT e, a, v, value_type_tag, tx, added FROM transactions WHERE tx > ? ORDER BY tx ASC, e ASC, a ASC, value_type_tag ASC, v ASC, added ASC")?;
let r: Result<Vec<_>> = stmt let r: Result<Vec<_>> = stmt.query_and_then(&[&tx], |row| {
.query_and_then(&[&tx], |row| { let e: i64 = row.get_checked(0)?;
let e: i64 = row.get(0)?; let a: i64 = row.get_checked(1)?;
let a: i64 = row.get(1)?;
let v: rusqlite::types::Value = row.get(2)?; let v: rusqlite::types::Value = row.get_checked(2)?;
let value_type_tag: i32 = row.get(3)?; let value_type_tag: i32 = row.get_checked(3)?;
let attribute = borrowed_schema.require_attribute_for_entid(a)?; let attribute = borrowed_schema.require_attribute_for_entid(a)?;
let value_type_tag = if !attribute.fulltext { let value_type_tag = if !attribute.fulltext { value_type_tag } else { ValueType::Long.value_type_tag() };
value_type_tag
} else {
ValueType::Long.value_type_tag()
};
let typed_value = let typed_value = TypedValue::from_sql_value_pair(v, value_type_tag)?.map_ident(borrowed_schema);
TypedValue::from_sql_value_pair(v, value_type_tag)?.map_ident(borrowed_schema); let (value, _) = typed_value.to_edn_value_pair();
let (value, _) = typed_value.to_edn_value_pair();
let tx: i64 = row.get(4)?; let tx: i64 = row.get_checked(4)?;
let added: bool = row.get(5)?; let added: bool = row.get_checked(5)?;
Ok(Datom { Ok(Datom {
e: EntidOrIdent::Entid(e), e: EntidOrIdent::Entid(e),
a: to_entid(borrowed_schema, a), a: to_entid(borrowed_schema, a),
v: value, v: value,
tx, tx: tx,
added: Some(added), added: Some(added),
}) })
})? })?.collect();
.collect();
// Group by tx. // Group by tx.
let r: Vec<Datoms> = r? let r: Vec<Datoms> = r?.into_iter().group_by(|x| x.tx).into_iter().map(|(_key, group)| Datoms(group.collect())).collect();
.into_iter()
.group_by(|x| x.tx)
.into_iter()
.map(|(_key, group)| Datoms(group.collect()))
.collect();
Ok(Transactions(r)) Ok(Transactions(r))
} }
/// Return the set of fulltext values in the store, ordered by rowid. /// Return the set of fulltext values in the store, ordered by rowid.
pub fn fulltext_values(conn: &rusqlite::Connection) -> Result<FulltextValues> { pub(crate) fn fulltext_values(conn: &rusqlite::Connection) -> Result<FulltextValues> {
let mut stmt: rusqlite::Statement = let mut stmt: rusqlite::Statement = conn.prepare("SELECT rowid, text FROM fulltext_values ORDER BY rowid")?;
conn.prepare("SELECT rowid, text FROM fulltext_values ORDER BY rowid")?;
let r: Result<Vec<_>> = stmt let r: Result<Vec<_>> = stmt.query_and_then(&[], |row| {
.query_and_then([], |row| { let rowid: i64 = row.get_checked(0)?;
let rowid: i64 = row.get(0)?; let text: String = row.get_checked(1)?;
let text: String = row.get(1)?; Ok((rowid, text))
Ok((rowid, text)) })?.collect();
})?
.collect();
r.map(FulltextValues) r.map(FulltextValues)
} }
@ -323,222 +228,27 @@ pub fn fulltext_values(conn: &rusqlite::Connection) -> Result<FulltextValues> {
/// ///
/// The query is printed followed by a newline, then the returned columns followed by a newline, and /// The query is printed followed by a newline, then the returned columns followed by a newline, and
/// then the data rows and columns. All columns are aligned. /// then the data rows and columns. All columns are aligned.
pub fn dump_sql_query( pub(crate) fn dump_sql_query(conn: &rusqlite::Connection, sql: &str, params: &[&ToSql]) -> Result<String> {
conn: &rusqlite::Connection,
sql: &str,
params: &[&dyn ToSql],
) -> Result<String> {
let mut stmt: rusqlite::Statement = conn.prepare(sql)?; let mut stmt: rusqlite::Statement = conn.prepare(sql)?;
let mut tw = TabWriter::new(Vec::new()).padding(2); let mut tw = TabWriter::new(Vec::new()).padding(2);
writeln!(&mut tw, "{}", sql).unwrap(); write!(&mut tw, "{}\n", sql).unwrap();
for column_name in stmt.column_names() { for column_name in stmt.column_names() {
write!(&mut tw, "{}\t", column_name).unwrap(); write!(&mut tw, "{}\t", column_name).unwrap();
} }
writeln!(&mut tw).unwrap(); write!(&mut tw, "\n").unwrap();
let r: Result<Vec<_>> = stmt let r: Result<Vec<_>> = stmt.query_and_then(params, |row| {
.query_and_then(params, |row| { for i in 0..row.column_count() {
for i in 0..row.as_ref().column_count() { let value: rusqlite::types::Value = row.get_checked(i)?;
let value: rusqlite::types::Value = row.get(i)?; write!(&mut tw, "{:?}\t", value).unwrap();
write!(&mut tw, "{:?}\t", value).unwrap(); }
} write!(&mut tw, "\n").unwrap();
writeln!(&mut tw).unwrap(); Ok(())
Ok(()) })?.collect();
})?
.collect();
r?; r?;
let dump = String::from_utf8(tw.into_inner().unwrap()).unwrap(); let dump = String::from_utf8(tw.into_inner().unwrap()).unwrap();
Ok(dump) Ok(dump)
} }
// A connection that doesn't try to be clever about possibly sharing its `Schema`. Compare to
// `mentat::Conn`.
pub struct TestConn {
pub sqlite: rusqlite::Connection,
pub partition_map: PartitionMap,
pub schema: Schema,
}
impl TestConn {
fn assert_materialized_views(&self) {
let materialized_ident_map = read_ident_map(&self.sqlite).expect("ident map");
let materialized_attribute_map = read_attribute_map(&self.sqlite).expect("schema map");
let materialized_schema = Schema::from_ident_map_and_attribute_map(
materialized_ident_map,
materialized_attribute_map,
)
.expect("schema");
assert_eq!(materialized_schema, self.schema);
}
pub fn transact<I>(&mut self, transaction: I) -> Result<TxReport>
where
I: Borrow<str>,
{
// Failure to parse the transaction is a coding error, so we unwrap.
let entities = edn::parse::entities(transaction.borrow()).unwrap_or_else(|_| {
panic!("to be able to parse {} into entities", transaction.borrow())
});
let details = {
// The block scopes the borrow of self.sqlite.
// We're about to write, so go straight ahead and get an IMMEDIATE transaction.
let tx = self
.sqlite
.transaction_with_behavior(TransactionBehavior::Immediate)?;
// Applying the transaction can fail, so we don't unwrap.
let details = transact(
&tx,
self.partition_map.clone(),
&self.schema,
&self.schema,
NullWatcher(),
entities,
)?;
tx.commit()?;
details
};
let (report, next_partition_map, next_schema, _watcher) = details;
self.partition_map = next_partition_map;
if let Some(next_schema) = next_schema {
self.schema = next_schema;
}
// Verify that we've updated the materialized views during transacting.
self.assert_materialized_views();
Ok(report)
}
pub fn transact_simple_terms<I>(
&mut self,
terms: I,
tempid_set: InternSet<TempId>,
) -> Result<TxReport>
where
I: IntoIterator<Item = TermWithTempIds>,
{
let details = {
// The block scopes the borrow of self.sqlite.
// We're about to write, so go straight ahead and get an IMMEDIATE transaction.
let tx = self
.sqlite
.transaction_with_behavior(TransactionBehavior::Immediate)?;
// Applying the transaction can fail, so we don't unwrap.
let details = transact_terms(
&tx,
self.partition_map.clone(),
&self.schema,
&self.schema,
NullWatcher(),
terms,
tempid_set,
)?;
tx.commit()?;
details
};
let (report, next_partition_map, next_schema, _watcher) = details;
self.partition_map = next_partition_map;
if let Some(next_schema) = next_schema {
self.schema = next_schema;
}
// Verify that we've updated the materialized views during transacting.
self.assert_materialized_views();
Ok(report)
}
pub fn last_tx_id(&self) -> Entid {
self.partition_map
.get(&":db.part/tx".to_string())
.unwrap()
.next_entid()
- 1
}
pub fn last_transaction(&self) -> Datoms {
transactions_after(&self.sqlite, &self.schema, self.last_tx_id() - 1)
.expect("last_transaction")
.0
.pop()
.unwrap()
}
pub fn transactions(&self) -> Transactions {
transactions_after(&self.sqlite, &self.schema, bootstrap::TX0).expect("transactions")
}
pub fn datoms(&self) -> Datoms {
datoms_after(&self.sqlite, &self.schema, bootstrap::TX0).expect("datoms")
}
pub fn fulltext_values(&self) -> FulltextValues {
fulltext_values(&self.sqlite).expect("fulltext_values")
}
pub fn with_sqlite(mut conn: rusqlite::Connection) -> TestConn {
let db = ensure_current_version(&mut conn).unwrap();
// Does not include :db/txInstant.
let datoms = datoms_after(&conn, &db.schema, 0).unwrap();
assert_eq!(datoms.0.len(), 94);
// Includes :db/txInstant.
let transactions = transactions_after(&conn, &db.schema, 0).unwrap();
assert_eq!(transactions.0.len(), 1);
assert_eq!(transactions.0[0].0.len(), 95);
let mut parts = db.partition_map;
// Add a fake partition to allow tests to do things like
// [:db/add 111 :foo/bar 222]
{
let fake_partition = Partition::new(100, 2000, 1000, true);
parts.insert(":db.part/fake".into(), fake_partition);
}
let test_conn = TestConn {
sqlite: conn,
partition_map: parts,
schema: db.schema,
};
// Verify that we've created the materialized views during bootstrapping.
test_conn.assert_materialized_views();
test_conn
}
pub fn sanitized_partition_map(&mut self) {
self.partition_map.remove(":db.part/fake");
}
}
impl Default for TestConn {
fn default() -> TestConn {
TestConn::with_sqlite(new_connection("").expect("Couldn't open in-memory db"))
}
}
pub struct TempIds(edn::Value);
impl TempIds {
pub fn to_edn(&self) -> edn::Value {
self.0.clone()
}
}
pub fn tempids(report: &TxReport) -> TempIds {
let mut map: BTreeMap<edn::Value, edn::Value> = BTreeMap::default();
for (tempid, &entid) in report.tempids.iter() {
map.insert(edn::Value::Text(tempid.clone()), edn::Value::Integer(entid));
}
TempIds(edn::Value::Map(map))
}

View file

@ -13,7 +13,8 @@
/// Literal `Entid` values in the the "db" namespace. /// Literal `Entid` values in the the "db" namespace.
/// ///
/// Used through-out the transactor to match core DB constructs. /// Used through-out the transactor to match core DB constructs.
use core_traits::Entid;
use types::{Entid};
// Added in SQL schema v1. // Added in SQL schema v1.
pub const DB_IDENT: Entid = 1; pub const DB_IDENT: Entid = 1;
@ -60,35 +61,23 @@ pub const DB_SCHEMA_CORE: Entid = 40;
/// Return `false` if the given attribute will not change the metadata: recognized idents, schema, /// Return `false` if the given attribute will not change the metadata: recognized idents, schema,
/// partitions in the partition map. /// partitions in the partition map.
pub fn might_update_metadata(attribute: Entid) -> bool { pub fn might_update_metadata(attribute: Entid) -> bool {
if attribute >= DB_DOC { if attribute > DB_DOC {
return false; return false
} }
matches!( match attribute {
attribute,
// Idents. // Idents.
DB_IDENT | DB_IDENT |
// Schema. // Schema.
DB_CARDINALITY | DB_CARDINALITY |
DB_DOC |
DB_FULLTEXT | DB_FULLTEXT |
DB_INDEX | DB_INDEX |
DB_IS_COMPONENT | DB_IS_COMPONENT |
DB_UNIQUE | DB_UNIQUE |
DB_VALUE_TYPE DB_VALUE_TYPE =>
) true,
} _ => false,
}
/// Return 'false' if the given attribute might be used to describe a schema attribute.
pub fn is_a_schema_attribute(attribute: Entid) -> bool {
matches!(
attribute,
DB_IDENT
| DB_CARDINALITY
| DB_FULLTEXT
| DB_INDEX
| DB_IS_COMPONENT
| DB_UNIQUE
| DB_VALUE_TYPE
)
} }
lazy_static! { lazy_static! {
@ -100,8 +89,9 @@ lazy_static! {
/// Attributes that are "schema related". These might change the "schema" materialized view. /// Attributes that are "schema related". These might change the "schema" materialized view.
pub static ref SCHEMA_SQL_LIST: String = { pub static ref SCHEMA_SQL_LIST: String = {
format!("({}, {}, {}, {}, {}, {})", format!("({}, {}, {}, {}, {}, {}, {})",
DB_CARDINALITY, DB_CARDINALITY,
DB_DOC,
DB_FULLTEXT, DB_FULLTEXT,
DB_INDEX, DB_INDEX,
DB_IS_COMPONENT, DB_IS_COMPONENT,
@ -111,8 +101,9 @@ lazy_static! {
/// Attributes that are "metadata" related. These might change one of the materialized views. /// Attributes that are "metadata" related. These might change one of the materialized views.
pub static ref METADATA_SQL_LIST: String = { pub static ref METADATA_SQL_LIST: String = {
format!("({}, {}, {}, {}, {}, {}, {})", format!("({}, {}, {}, {}, {}, {}, {}, {})",
DB_CARDINALITY, DB_CARDINALITY,
DB_DOC,
DB_FULLTEXT, DB_FULLTEXT,
DB_IDENT, DB_IDENT,
DB_INDEX, DB_INDEX,

View file

@ -10,15 +10,37 @@
#![allow(dead_code)] #![allow(dead_code)]
use failure::{Backtrace, Context, Fail}; use failure::{
Backtrace,
Context,
Fail,
};
use std::collections::{BTreeMap, BTreeSet}; use std::collections::{
BTreeMap,
BTreeSet,
};
use rusqlite; use rusqlite;
use edn::entities::TempId; use edn::entities::{
TempId,
};
use mentat_core::{
KnownEntid,
};
use types::{
Entid,
TypedValue,
ValueType,
};
use core_traits::{Entid, KnownEntid, TypedValue, ValueType}; #[macro_export]
macro_rules! bail {
($e:expr) => (
return Err($e.into());
)
}
pub type Result<T> = ::std::result::Result<T, DbError>; pub type Result<T> = ::std::result::Result<T, DbError>;
@ -58,46 +80,40 @@ pub enum SchemaConstraintViolation {
/// A transaction tried to assert a datom or datoms with the wrong value `v` type(s). /// A transaction tried to assert a datom or datoms with the wrong value `v` type(s).
TypeDisagreements { TypeDisagreements {
/// The key (`[e a v]`) has an invalid value `v`: it is not of the expected value type. /// The key (`[e a v]`) has an invalid value `v`: it is not of the expected value type.
conflicting_datoms: BTreeMap<(Entid, Entid, TypedValue), ValueType>, conflicting_datoms: BTreeMap<(Entid, Entid, TypedValue), ValueType>
}, },
/// A transaction tried to assert datoms that don't observe the schema's cardinality constraints. /// A transaction tried to assert datoms that don't observe the schema's cardinality constraints.
CardinalityConflicts { conflicts: Vec<CardinalityConflict> }, CardinalityConflicts {
conflicts: Vec<CardinalityConflict>,
},
} }
impl ::std::fmt::Display for SchemaConstraintViolation { impl ::std::fmt::Display for SchemaConstraintViolation {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
use self::SchemaConstraintViolation::*; use self::SchemaConstraintViolation::*;
match self { match self {
ConflictingUpserts { &ConflictingUpserts { ref conflicting_upserts } => {
ref conflicting_upserts,
} => {
writeln!(f, "conflicting upserts:")?; writeln!(f, "conflicting upserts:")?;
for (tempid, entids) in conflicting_upserts { for (tempid, entids) in conflicting_upserts {
writeln!(f, " tempid {:?} upserts to {:?}", tempid, entids)?; writeln!(f, " tempid {:?} upserts to {:?}", tempid, entids)?;
} }
Ok(()) Ok(())
} },
TypeDisagreements { &TypeDisagreements { ref conflicting_datoms } => {
ref conflicting_datoms,
} => {
writeln!(f, "type disagreements:")?; writeln!(f, "type disagreements:")?;
for (ref datom, expected_type) in conflicting_datoms { for (ref datom, expected_type) in conflicting_datoms {
writeln!( writeln!(f, " expected value of type {} but got datom [{} {} {:?}]", expected_type, datom.0, datom.1, datom.2)?;
f,
" expected value of type {} but got datom [{} {} {:?}]",
expected_type, datom.0, datom.1, datom.2
)?;
} }
Ok(()) Ok(())
} },
CardinalityConflicts { ref conflicts } => { &CardinalityConflicts { ref conflicts } => {
writeln!(f, "cardinality conflicts:")?; writeln!(f, "cardinality conflicts:")?;
for conflict in conflicts { for ref conflict in conflicts {
writeln!(f, " {:?}", conflict)?; writeln!(f, " {:?}", conflict)?;
} }
Ok(()) Ok(())
} },
} }
} }
} }
@ -116,12 +132,12 @@ impl ::std::fmt::Display for InputError {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
use self::InputError::*; use self::InputError::*;
match self { match self {
BadDbId => { &BadDbId => {
writeln!(f, ":db/id in map notation must either not be present or be an entid, an ident, or a tempid") writeln!(f, ":db/id in map notation must either not be present or be an entid, an ident, or a tempid")
} },
BadEntityPlace => { &BadEntityPlace => {
writeln!(f, "cannot convert value place into entity place") writeln!(f, "cannot convert value place into entity place")
} },
} }
} }
} }
@ -138,7 +154,7 @@ impl ::std::fmt::Display for DbError {
} }
impl Fail for DbError { impl Fail for DbError {
fn cause(&self) -> Option<&dyn Fail> { fn cause(&self) -> Option<&Fail> {
self.inner.cause() self.inner.cause()
} }
@ -154,38 +170,32 @@ impl DbError {
} }
impl From<DbErrorKind> for DbError { impl From<DbErrorKind> for DbError {
fn from(kind: DbErrorKind) -> Self { fn from(kind: DbErrorKind) -> DbError {
DbError { DbError { inner: Context::new(kind) }
inner: Context::new(kind),
}
} }
} }
impl From<Context<DbErrorKind>> for DbError { impl From<Context<DbErrorKind>> for DbError {
fn from(inner: Context<DbErrorKind>) -> Self { fn from(inner: Context<DbErrorKind>) -> DbError {
DbError { inner } DbError { inner: inner }
} }
} }
impl From<rusqlite::Error> for DbError { impl From<rusqlite::Error> for DbError {
fn from(error: rusqlite::Error) -> Self { fn from(error: rusqlite::Error) -> DbError {
DbError { DbError { inner: Context::new(DbErrorKind::RusqliteError(error.to_string())) }
inner: Context::new(DbErrorKind::RusqliteError(error.to_string())),
}
} }
} }
#[derive(Clone, PartialEq, Debug, Fail)] #[derive(Clone, PartialEq, Debug, Fail)]
pub enum DbErrorKind { pub enum DbErrorKind {
/// We're just not done yet. Recognized a feature that is not yet implemented. /// We're just not done yet. Message that the feature is recognized but not yet
/// implemented.
#[fail(display = "not yet implemented: {}", _0)] #[fail(display = "not yet implemented: {}", _0)]
NotYetImplemented(String), NotYetImplemented(String),
/// We've been given a value that isn't the correct Mentat type. /// We've been given a value that isn't the correct Mentat type.
#[fail( #[fail(display = "value '{}' is not the expected Mentat value type {:?}", _0, _1)]
display = "value '{}' is not the expected Mentat value type {:?}",
_0, _1
)]
BadValuePair(String, ValueType), BadValuePair(String, ValueType),
/// We've got corrupt data in the SQL store: a value and value_type_tag don't line up. /// We've got corrupt data in the SQL store: a value and value_type_tag don't line up.
@ -193,10 +203,11 @@ pub enum DbErrorKind {
#[fail(display = "bad SQL (value_type_tag, value) pair: ({:?}, {:?})", _0, _1)] #[fail(display = "bad SQL (value_type_tag, value) pair: ({:?}, {:?})", _0, _1)]
BadSQLValuePair(rusqlite::types::Value, i32), BadSQLValuePair(rusqlite::types::Value, i32),
/// The SQLite store user_version isn't recognized. This could be an old version of Mentat // /// The SQLite store user_version isn't recognized. This could be an old version of Mentat
/// trying to open a newer version SQLite store; or it could be a corrupt file; or ... // /// trying to open a newer version SQLite store; or it could be a corrupt file; or ...
/// #[fail(display = "bad SQL store user_version: {}", _0)] // #[fail(display = "bad SQL store user_version: {}", _0)]
/// BadSQLiteStoreVersion(i32), // BadSQLiteStoreVersion(i32),
/// A bootstrap definition couldn't be parsed or installed. This is a programmer error, not /// A bootstrap definition couldn't be parsed or installed. This is a programmer error, not
/// a runtime error. /// a runtime error.
#[fail(display = "bad bootstrap definition: {}", _0)] #[fail(display = "bad bootstrap definition: {}", _0)]
@ -211,13 +222,11 @@ pub enum DbErrorKind {
UnrecognizedIdent(String), UnrecognizedIdent(String),
/// An entid->ident mapping failed. /// An entid->ident mapping failed.
#[fail(display = "no ident found for entid: {}", _0)] /// We also use this error if you try to transact an entid that we didn't allocate,
/// in part because we blow the stack in error_chain if we define a new enum!
#[fail(display = "unrecognized or no ident found for entid: {}", _0)]
UnrecognizedEntid(Entid), UnrecognizedEntid(Entid),
/// Tried to transact an entid that isn't allocated.
#[fail(display = "entid not allocated: {}", _0)]
UnallocatedEntid(Entid),
#[fail(display = "unknown attribute for entid: {}", _0)] #[fail(display = "unknown attribute for entid: {}", _0)]
UnknownAttribute(Entid), UnknownAttribute(Entid),
@ -236,9 +245,7 @@ pub enum DbErrorKind {
#[fail(display = "transaction input error: {}", _0)] #[fail(display = "transaction input error: {}", _0)]
InputError(InputError), InputError(InputError),
#[fail( #[fail(display = "Cannot transact a fulltext assertion with a typed value that is not :db/valueType :db.type/string")]
display = "Cannot transact a fulltext assertion with a typed value that is not :db/valueType :db.type/string"
)]
WrongTypeValueForFtsAssertion, WrongTypeValueForFtsAssertion,
// SQL errors. // SQL errors.
@ -284,15 +291,6 @@ pub enum DbErrorKind {
#[fail(display = "Could not update partition map")] #[fail(display = "Could not update partition map")]
FailedToUpdatePartitionMap, FailedToUpdatePartitionMap,
#[fail(display = "Can't operate over mixed timelines")]
TimelinesMixed,
#[fail(display = "Can't move transactions to a non-empty timeline")]
TimelinesMoveToNonEmpty,
#[fail(display = "Supplied an invalid transaction range")]
TimelinesInvalidRange,
// It would be better to capture the underlying `rusqlite::Error`, but that type doesn't // It would be better to capture the underlying `rusqlite::Error`, but that type doesn't
// implement many useful traits, including `Clone`, `Eq`, and `PartialEq`. // implement many useful traits, including `Clone`, `Eq`, and `PartialEq`.
#[fail(display = "SQL error: {}", _0)] #[fail(display = "SQL error: {}", _0)]

View file

@ -12,21 +12,48 @@
//! Types used only within the transactor. These should not be exposed outside of this crate. //! Types used only within the transactor. These should not be exposed outside of this crate.
use std::collections::{BTreeMap, BTreeSet, HashMap}; use std::collections::{
BTreeMap,
BTreeSet,
HashMap,
};
use core_traits::{Attribute, Entid, KnownEntid, TypedValue, ValueType}; use mentat_core::KnownEntid;
use mentat_core::util::Either; use mentat_core::util::Either;
use edn; use edn;
use edn::{
SpannedValue,
ValueAndSpan,
ValueRc,
};
use edn::entities; use edn::entities;
use edn::entities::{EntityPlace, OpType, TempId, TxFunction}; use edn::entities::{
use edn::{SpannedValue, ValueAndSpan, ValueRc}; EntityPlace,
OpType,
TempId,
TxFunction,
};
use crate::schema::SchemaTypeChecking; use errors;
use crate::types::{AVMap, AVPair, Schema, TransactableValue}; use errors::{
use db_traits::errors; DbErrorKind,
use db_traits::errors::{DbErrorKind, Result}; Result,
};
use schema::{
SchemaTypeChecking,
};
use types::{
Attribute,
AVMap,
AVPair,
Entid,
Schema,
TransactableValue,
TypedValue,
ValueType,
};
impl TransactableValue for ValueAndSpan { impl TransactableValue for ValueAndSpan {
fn into_typed_value(self, schema: &Schema, value_type: ValueType) -> Result<TypedValue> { fn into_typed_value(self, schema: &Schema, value_type: ValueType) -> Result<TypedValue> {
@ -44,7 +71,7 @@ impl TransactableValue for ValueAndSpan {
// We only allow namespaced idents. // We only allow namespaced idents.
bail!(DbErrorKind::InputError(errors::InputError::BadEntityPlace)) bail!(DbErrorKind::InputError(errors::InputError::BadEntityPlace))
} }
} },
Text(v) => Ok(EntityPlace::TempId(TempId::External(v).into())), Text(v) => Ok(EntityPlace::TempId(TempId::External(v).into())),
List(ls) => { List(ls) => {
let mut it = ls.iter(); let mut it = ls.iter();
@ -52,37 +79,35 @@ impl TransactableValue for ValueAndSpan {
// Like "(transaction-id)". // Like "(transaction-id)".
(Some(&PlainSymbol(ref op)), None, None, None) => { (Some(&PlainSymbol(ref op)), None, None, None) => {
Ok(EntityPlace::TxFunction(TxFunction { op: op.clone() })) Ok(EntityPlace::TxFunction(TxFunction { op: op.clone() }))
} },
// Like "(lookup-ref)". // Like "(lookup-ref)".
(Some(&PlainSymbol(edn::PlainSymbol(ref s))), Some(a), Some(v), None) (Some(&PlainSymbol(edn::PlainSymbol(ref s))), Some(a), Some(v), None) if s == "lookup-ref" => {
if s == "lookup-ref" =>
{
match a.clone().into_entity_place()? { match a.clone().into_entity_place()? {
EntityPlace::Entid(a) => { EntityPlace::Entid(a) => Ok(EntityPlace::LookupRef(entities::LookupRef { a: entities::AttributePlace::Entid(a), v: v.clone() })),
Ok(EntityPlace::LookupRef(entities::LookupRef { EntityPlace::TempId(_) |
a: entities::AttributePlace::Entid(a), EntityPlace::TxFunction(_) |
v: v.clone(), EntityPlace::LookupRef(_) => bail!(DbErrorKind::InputError(errors::InputError::BadEntityPlace)),
}))
}
EntityPlace::TempId(_)
| EntityPlace::TxFunction(_)
| EntityPlace::LookupRef(_) => {
bail!(DbErrorKind::InputError(errors::InputError::BadEntityPlace))
}
} }
} },
_ => bail!(DbErrorKind::InputError(errors::InputError::BadEntityPlace)), _ => bail!(DbErrorKind::InputError(errors::InputError::BadEntityPlace)),
} }
} },
Nil | Boolean(_) | Instant(_) | BigInteger(_) | Float(_) | Uuid(_) | PlainSymbol(_) Nil |
| NamespacedSymbol(_) | Vector(_) | Set(_) | Map(_) | Bytes(_) => { Boolean(_) |
bail!(DbErrorKind::InputError(errors::InputError::BadEntityPlace)) Instant(_) |
} BigInteger(_) |
Float(_) |
Uuid(_) |
PlainSymbol(_) |
NamespacedSymbol(_) |
Vector(_) |
Set(_) |
Map(_) => bail!(DbErrorKind::InputError(errors::InputError::BadEntityPlace)),
} }
} }
fn as_tempid(&self) -> Option<TempId> { fn as_tempid(&self) -> Option<TempId> {
self.inner.as_text().cloned().map(TempId::External) self.inner.as_text().cloned().map(TempId::External).map(|v| v.into())
} }
} }
@ -97,24 +122,19 @@ impl TransactableValue for TypedValue {
fn into_entity_place(self) -> Result<EntityPlace<Self>> { fn into_entity_place(self) -> Result<EntityPlace<Self>> {
match self { match self {
TypedValue::Ref(x) => Ok(EntityPlace::Entid(entities::EntidOrIdent::Entid(x))), TypedValue::Ref(x) => Ok(EntityPlace::Entid(entities::EntidOrIdent::Entid(x))),
TypedValue::Keyword(x) => Ok(EntityPlace::Entid(entities::EntidOrIdent::Ident( TypedValue::Keyword(x) => Ok(EntityPlace::Entid(entities::EntidOrIdent::Ident((*x).clone()))),
(*x).clone(),
))),
TypedValue::String(x) => Ok(EntityPlace::TempId(TempId::External((*x).clone()).into())), TypedValue::String(x) => Ok(EntityPlace::TempId(TempId::External((*x).clone()).into())),
TypedValue::Boolean(_) TypedValue::Boolean(_) |
| TypedValue::Long(_) TypedValue::Long(_) |
| TypedValue::Double(_) TypedValue::Double(_) |
| TypedValue::Instant(_) TypedValue::Instant(_) |
| TypedValue::Uuid(_) TypedValue::Uuid(_) => bail!(DbErrorKind::InputError(errors::InputError::BadEntityPlace)),
| TypedValue::Bytes(_) => {
bail!(DbErrorKind::InputError(errors::InputError::BadEntityPlace))
}
} }
} }
fn as_tempid(&self) -> Option<TempId> { fn as_tempid(&self) -> Option<TempId> {
match self { match self {
TypedValue::String(ref s) => Some(TempId::External((**s).clone())), &TypedValue::String(ref s) => Some(TempId::External((**s).clone()).into()),
_ => None, _ => None,
} }
} }
@ -138,14 +158,13 @@ pub type LookupRef = ValueRc<AVPair>;
/// Internal representation of an entid on its way to resolution. We either have the simple case (a /// Internal representation of an entid on its way to resolution. We either have the simple case (a
/// numeric entid), a lookup-ref that still needs to be resolved (an atomized [a v] pair), or a temp /// numeric entid), a lookup-ref that still needs to be resolved (an atomized [a v] pair), or a temp
/// ID that needs to be upserted or allocated (an atomized tempid). /// ID that needs to be upserted or allocated (an atomized tempid).
#[derive(Clone, Debug, Eq, Hash, Ord, PartialOrd, PartialEq)] #[derive(Clone,Debug,Eq,Hash,Ord,PartialOrd,PartialEq)]
pub enum LookupRefOrTempId { pub enum LookupRefOrTempId {
LookupRef(LookupRef), LookupRef(LookupRef),
TempId(TempIdHandle), TempId(TempIdHandle)
} }
pub type TermWithTempIdsAndLookupRefs = pub type TermWithTempIdsAndLookupRefs = Term<KnownEntidOr<LookupRefOrTempId>, TypedValueOr<LookupRefOrTempId>>;
Term<KnownEntidOr<LookupRefOrTempId>, TypedValueOr<LookupRefOrTempId>>;
pub type TermWithTempIds = Term<KnownEntidOr<TempIdHandle>, TypedValueOr<TempIdHandle>>; pub type TermWithTempIds = Term<KnownEntidOr<TempIdHandle>, TypedValueOr<TempIdHandle>>;
pub type TermWithoutTempIds = Term<KnownEntid, TypedValue>; pub type TermWithoutTempIds = Term<KnownEntid, TypedValue>;
pub type Population = Vec<TermWithTempIds>; pub type Population = Vec<TermWithTempIds>;
@ -162,14 +181,6 @@ impl TermWithTempIds {
} }
} }
impl TermWithoutTempIds {
pub(crate) fn rewrap<A, B>(self) -> Term<KnownEntidOr<A>, TypedValueOr<B>> {
match self {
Term::AddOrRetract(op, n, a, v) => Term::AddOrRetract(op, Left(n), a, Left(v)),
}
}
}
/// Given a `KnownEntidOr` or a `TypedValueOr`, replace any internal `LookupRef` with the entid from /// Given a `KnownEntidOr` or a `TypedValueOr`, replace any internal `LookupRef` with the entid from
/// the given map. Fail if any `LookupRef` cannot be replaced. /// the given map. Fail if any `LookupRef` cannot be replaced.
/// ///
@ -179,31 +190,16 @@ impl TermWithoutTempIds {
/// The reason for this awkward expression is that we're parameterizing over the _type constructor_ /// The reason for this awkward expression is that we're parameterizing over the _type constructor_
/// (`EntidOr` or `TypedValueOr`), which is not trivial to express in Rust. This only works because /// (`EntidOr` or `TypedValueOr`), which is not trivial to express in Rust. This only works because
/// they're both the same `Result<...>` type with different parameterizations. /// they're both the same `Result<...>` type with different parameterizations.
pub fn replace_lookup_ref<T, U>( pub fn replace_lookup_ref<T, U>(lookup_map: &AVMap, desired_or: Either<T, LookupRefOrTempId>, lift: U) -> errors::Result<Either<T, TempIdHandle>> where U: FnOnce(Entid) -> T {
lookup_map: &AVMap,
desired_or: Either<T, LookupRefOrTempId>,
lift: U,
) -> errors::Result<Either<T, TempIdHandle>>
where
U: FnOnce(Entid) -> T,
{
match desired_or { match desired_or {
Left(desired) => Ok(Left(desired)), // N.b., must unwrap here -- the ::Left types are different! Left(desired) => Ok(Left(desired)), // N.b., must unwrap here -- the ::Left types are different!
Right(other) => { Right(other) => {
match other { match other {
LookupRefOrTempId::TempId(t) => Ok(Right(t)), LookupRefOrTempId::TempId(t) => Ok(Right(t)),
LookupRefOrTempId::LookupRef(av) => lookup_map LookupRefOrTempId::LookupRef(av) => lookup_map.get(&*av)
.get(&*av) .map(|x| lift(*x)).map(Left)
.map(|x| lift(*x))
.map(Left)
// XXX TODO: fix this error kind! // XXX TODO: fix this error kind!
.ok_or_else(|| { .ok_or_else(|| DbErrorKind::UnrecognizedIdent(format!("couldn't lookup [a v]: {:?}", (*av).clone())).into()),
DbErrorKind::UnrecognizedIdent(format!(
"couldn't lookup [a v]: {:?}",
(*av).clone()
))
.into()
}),
} }
} }
} }
@ -217,5 +213,4 @@ pub(crate) struct AddAndRetract {
// A trie-like structure mapping a -> e -> v that prefix compresses and makes uniqueness constraint // A trie-like structure mapping a -> e -> v that prefix compresses and makes uniqueness constraint
// checking more efficient. BTree* for deterministic errors. // checking more efficient. BTree* for deterministic errors.
pub(crate) type AEVTrie<'schema> = pub(crate) type AEVTrie<'schema> = BTreeMap<(Entid, &'schema Attribute), BTreeMap<Entid, AddAndRetract>>;
BTreeMap<(Entid, &'schema Attribute), BTreeMap<Entid, AddAndRetract>>;

View file

@ -9,93 +9,114 @@
// specific language governing permissions and limitations under the License. // specific language governing permissions and limitations under the License.
extern crate failure; extern crate failure;
#[macro_use] extern crate failure_derive;
extern crate indexmap; extern crate indexmap;
extern crate itertools; extern crate itertools;
#[macro_use] #[macro_use] extern crate lazy_static;
extern crate lazy_static; #[macro_use] extern crate log;
#[macro_use]
extern crate log;
#[cfg(feature = "syncable")]
#[macro_use]
extern crate serde_derive;
extern crate num;
extern crate petgraph; extern crate petgraph;
extern crate rusqlite; extern crate rusqlite;
extern crate tabwriter; extern crate tabwriter;
extern crate time; extern crate time;
#[macro_use] #[macro_use] extern crate edn;
extern crate edn; #[macro_use] extern crate mentat_core;
#[macro_use]
extern crate mentat_core;
extern crate db_traits;
#[macro_use]
extern crate core_traits;
extern crate mentat_sql; extern crate mentat_sql;
use std::iter::repeat; use std::iter::repeat;
use itertools::Itertools; use itertools::Itertools;
use db_traits::errors::{DbErrorKind, Result}; pub use errors::{
DbError,
#[macro_use] DbErrorKind,
pub mod debug; Result,
SchemaConstraintViolation,
};
#[macro_use] pub mod errors;
mod add_retract_alter_set; mod add_retract_alter_set;
mod bootstrap;
pub mod cache; pub mod cache;
pub mod db; pub mod db;
mod bootstrap;
pub mod debug;
pub mod entids; pub mod entids;
pub mod internal_types; // pub because we need them for building entities programmatically. pub mod internal_types; // pub because we need them for building entities programmatically.
mod metadata; mod metadata;
mod schema; mod schema;
pub mod timelines; pub mod tx_observer;
mod watcher;
mod tx; mod tx;
mod tx_checking; mod tx_checking;
pub mod tx_observer;
pub mod types; pub mod types;
mod upsert_resolution; mod upsert_resolution;
mod watcher;
// Export these for reference from sync code and tests. // Export these for reference from tests. cfg(test) should work, but doesn't.
pub use crate::bootstrap::{TX0, USER0, V1_PARTS}; // #[cfg(test)]
pub use bootstrap::{
TX0,
USER0,
};
pub static TIMELINE_MAIN: i64 = 0; pub use schema::{
AttributeBuilder,
AttributeValidation,
};
pub use crate::schema::{AttributeBuilder, AttributeValidation}; pub use bootstrap::{
CORE_SCHEMA_VERSION,
pub use crate::bootstrap::CORE_SCHEMA_VERSION; };
use edn::symbols; use edn::symbols;
pub use crate::entids::DB_SCHEMA_CORE; pub use entids::{
DB_SCHEMA_CORE,
};
pub use crate::db::{new_connection, TypedSQLValue}; pub use db::{
TypedSQLValue,
new_connection,
};
#[cfg(feature = "sqlcipher")] #[cfg(feature = "sqlcipher")]
pub use db::{change_encryption_key, new_connection_with_key}; pub use db::{
new_connection_with_key,
change_encryption_key,
};
pub use crate::watcher::TransactWatcher; pub use watcher::{
TransactWatcher,
};
pub use crate::tx::{transact, transact_terms}; pub use tx::{
transact,
transact_terms,
};
pub use crate::tx_observer::{InProgressObserverTransactWatcher, TxObservationService, TxObserver}; pub use tx_observer::{
InProgressObserverTransactWatcher,
TxObservationService,
TxObserver,
};
pub use crate::types::{AttributeSet, Partition, PartitionMap, TransactableValue, DB}; pub use types::{
AttributeSet,
DB,
PartitionMap,
TransactableValue,
};
pub fn to_namespaced_keyword(s: &str) -> Result<symbols::Keyword> { pub fn to_namespaced_keyword(s: &str) -> Result<symbols::Keyword> {
let splits = [':', '/']; let splits = [':', '/'];
let mut i = s.split(&splits[..]); let mut i = s.split(&splits[..]);
let nsk = match (i.next(), i.next(), i.next(), i.next()) { let nsk = match (i.next(), i.next(), i.next(), i.next()) {
(Some(""), Some(namespace), Some(name), None) => { (Some(""), Some(namespace), Some(name), None) => Some(symbols::Keyword::namespaced(namespace, name)),
Some(symbols::Keyword::namespaced(namespace, name))
}
_ => None, _ => None,
}; };
nsk.ok_or_else(|| DbErrorKind::NotYetImplemented(format!("InvalidKeyword: {}", s)).into()) nsk.ok_or(DbErrorKind::NotYetImplemented(format!("InvalidKeyword: {}", s)).into())
} }
/// Prepare an SQL `VALUES` block, like (?, ?, ?), (?, ?, ?). /// Prepare an SQL `VALUES` block, like (?, ?, ?), (?, ?, ?).

View file

@ -26,21 +26,31 @@
use failure::ResultExt; use failure::ResultExt;
use std::collections::btree_map::Entry;
use std::collections::{BTreeMap, BTreeSet}; use std::collections::{BTreeMap, BTreeSet};
use std::collections::btree_map::Entry;
use crate::add_retract_alter_set::AddRetractAlterSet; use add_retract_alter_set::{
use crate::entids; AddRetractAlterSet,
use db_traits::errors::{DbErrorKind, Result}; };
use edn::symbols; use edn::symbols;
use entids;
use errors::{
DbErrorKind,
Result,
};
use mentat_core::{
attribute,
Entid,
Schema,
AttributeMap,
TypedValue,
ValueType,
};
use core_traits::{attribute, Entid, TypedValue, ValueType}; use schema::{
AttributeBuilder,
use mentat_core::{AttributeMap, Schema}; AttributeValidation,
};
use crate::schema::{AttributeBuilder, AttributeValidation};
use crate::types::EAV;
/// An alteration to an attribute. /// An alteration to an attribute.
#[derive(Clone, Debug, Eq, Hash, Ord, PartialOrd, PartialEq)] #[derive(Clone, Debug, Eq, Hash, Ord, PartialOrd, PartialEq)]
@ -82,98 +92,25 @@ pub struct MetadataReport {
impl MetadataReport { impl MetadataReport {
pub fn attributes_did_change(&self) -> bool { pub fn attributes_did_change(&self) -> bool {
!(self.attributes_installed.is_empty() && self.attributes_altered.is_empty()) !(self.attributes_installed.is_empty() &&
self.attributes_altered.is_empty())
} }
} }
/// Update an 'AttributeMap' in place given two sets of ident and attribute retractions, which
/// together contain enough information to reason about a "schema retraction".
///
/// Schema may only be retracted if all of its necessary attributes are being retracted:
/// - :db/ident, :db/valueType, :db/cardinality.
///
/// Note that this is currently incomplete/flawed:
/// - we're allowing optional attributes to not be retracted and dangle afterwards
///
/// Returns a set of attribute retractions which do not involve schema-defining attributes.
fn update_attribute_map_from_schema_retractions(
attribute_map: &mut AttributeMap,
retractions: Vec<EAV>,
ident_retractions: &BTreeMap<Entid, symbols::Keyword>,
) -> Result<Vec<EAV>> {
// Process retractions of schema attributes first. It's allowed to retract a schema attribute
// if all of the schema-defining schema attributes are being retracted.
// A defining set of attributes is :db/ident, :db/valueType, :db/cardinality.
let mut filtered_retractions = vec![];
let mut suspect_retractions = vec![];
// Filter out sets of schema altering retractions.
let mut eas = BTreeMap::new();
for (e, a, v) in retractions.into_iter() {
if entids::is_a_schema_attribute(a) {
eas.entry(e).or_insert_with(Vec::new).push(a);
suspect_retractions.push((e, a, v));
} else {
filtered_retractions.push((e, a, v));
}
}
// TODO (see https://github.com/mozilla/mentat/issues/796).
// Retraction of idents is allowed, but if an ident names a schema attribute, then we should enforce
// retraction of all of the associated schema attributes.
// Unfortunately, our current in-memory schema representation (namely, how we define an Attribute) is not currently
// rich enough: it lacks distinction between presence and absence, and instead assumes default values.
// Currently, in order to do this enforcement correctly, we'd need to inspect 'datoms'.
// Here is an incorrect way to enforce this. It's incorrect because it prevents us from retracting non-"schema naming" idents.
// for retracted_e in ident_retractions.keys() {
// if !eas.contains_key(retracted_e) {
// bail!(DbErrorKind::BadSchemaAssertion(format!("Retracting :db/ident of a schema without retracting its defining attributes is not permitted.")));
// }
// }
for (e, a, v) in suspect_retractions.into_iter() {
let attributes = eas.get(&e).unwrap();
// Found a set of retractions which negate a schema.
if attributes.contains(&entids::DB_CARDINALITY)
&& attributes.contains(&entids::DB_VALUE_TYPE)
{
// Ensure that corresponding :db/ident is also being retracted at the same time.
if ident_retractions.contains_key(&e) {
// Remove attributes corresponding to retracted attribute.
attribute_map.remove(&e);
} else {
bail!(DbErrorKind::BadSchemaAssertion("Retracting defining attributes of a schema without retracting its :db/ident is not permitted.".to_string()));
}
} else {
filtered_retractions.push((e, a, v));
}
}
Ok(filtered_retractions)
}
/// Update a `AttributeMap` in place from the given `[e a typed_value]` triples. /// Update a `AttributeMap` in place from the given `[e a typed_value]` triples.
/// ///
/// This is suitable for producing a `AttributeMap` from the `schema` materialized view, which does not /// This is suitable for producing a `AttributeMap` from the `schema` materialized view, which does not
/// contain install and alter markers. /// contain install and alter markers.
/// ///
/// Returns a report summarizing the mutations that were applied. /// Returns a report summarizing the mutations that were applied.
pub fn update_attribute_map_from_entid_triples( pub fn update_attribute_map_from_entid_triples<A, R>(attribute_map: &mut AttributeMap, assertions: A, retractions: R) -> Result<MetadataReport>
attribute_map: &mut AttributeMap, where A: IntoIterator<Item=(Entid, Entid, TypedValue)>,
assertions: Vec<EAV>, R: IntoIterator<Item=(Entid, Entid, TypedValue)> {
retractions: Vec<EAV>,
) -> Result<MetadataReport> { fn attribute_builder_to_modify(attribute_id: Entid, existing: &AttributeMap) -> AttributeBuilder {
fn attribute_builder_to_modify( existing.get(&attribute_id)
attribute_id: Entid, .map(AttributeBuilder::to_modify_attribute)
existing: &AttributeMap, .unwrap_or_else(AttributeBuilder::default)
) -> AttributeBuilder {
existing
.get(&attribute_id)
.map(AttributeBuilder::modify_attribute)
.unwrap_or_else(AttributeBuilder::default)
} }
// Group mutations by impacted entid. // Group mutations by impacted entid.
@ -181,13 +118,14 @@ pub fn update_attribute_map_from_entid_triples(
// For retractions, we start with an attribute builder that's pre-populated with the existing // For retractions, we start with an attribute builder that's pre-populated with the existing
// attribute values. That allows us to check existing values and unset them. // attribute values. That allows us to check existing values and unset them.
for (entid, attr, ref value) in retractions { for (entid, attr, ref value) in retractions.into_iter() {
let builder = builders let builder = builders.entry(entid).or_insert_with(|| attribute_builder_to_modify(entid, attribute_map));
.entry(entid)
.or_insert_with(|| attribute_builder_to_modify(entid, attribute_map));
match attr { match attr {
// You can only retract :db/unique, :db/isComponent; all others must be altered instead // You can only retract :db/unique, :db/doc, :db/isComponent; all others
// of retracted, or are not allowed to change. // must be altered instead of retracted, or are not allowed to change.
entids::DB_DOC => {
// Nothing to do here; we don't keep docstrings inside `Attribute`s.
},
entids::DB_IS_COMPONENT => { entids::DB_IS_COMPONENT => {
match value { match value {
&TypedValue::Boolean(v) if builder.component == Some(v) => { &TypedValue::Boolean(v) if builder.component == Some(v) => {
@ -198,7 +136,6 @@ pub fn update_attribute_map_from_entid_triples(
}, },
} }
}, },
entids::DB_UNIQUE => { entids::DB_UNIQUE => {
match *value { match *value {
TypedValue::Ref(u) => { TypedValue::Ref(u) => {
@ -217,18 +154,9 @@ pub fn update_attribute_map_from_entid_triples(
_ => bail!(DbErrorKind::BadSchemaAssertion(format!("Expected [:db/retract _ :db/unique :db.unique/_] but got [:db/retract {} :db/unique {:?}]", entid, value))) _ => bail!(DbErrorKind::BadSchemaAssertion(format!("Expected [:db/retract _ :db/unique :db.unique/_] but got [:db/retract {} :db/unique {:?}]", entid, value)))
} }
}, },
_ => {
entids::DB_VALUE_TYPE |
entids::DB_CARDINALITY |
entids::DB_INDEX |
entids::DB_FULLTEXT |
entids::DB_NO_HISTORY => {
bail!(DbErrorKind::BadSchemaAssertion(format!("Retracting attribute {} for entity {} not permitted.", attr, entid))); bail!(DbErrorKind::BadSchemaAssertion(format!("Retracting attribute {} for entity {} not permitted.", attr, entid)));
}, },
_ => {
bail!(DbErrorKind::BadSchemaAssertion(format!("Do not recognize attribute {} for entid {}", attr, entid)))
}
} }
} }
@ -238,6 +166,13 @@ pub fn update_attribute_map_from_entid_triples(
// TODO: improve error messages throughout. // TODO: improve error messages throughout.
match attr { match attr {
entids::DB_DOC => {
match *value {
TypedValue::String(_) => {},
_ => bail!(DbErrorKind::BadSchemaAssertion(format!("Expected [... :db/doc \"string value\"] but got [... :db/doc {:?}] for entid {} and attribute {}", value, entid, attr)))
}
},
entids::DB_VALUE_TYPE => { entids::DB_VALUE_TYPE => {
match *value { match *value {
TypedValue::Ref(entids::DB_TYPE_BOOLEAN) => { builder.value_type(ValueType::Boolean); }, TypedValue::Ref(entids::DB_TYPE_BOOLEAN) => { builder.value_type(ValueType::Boolean); },
@ -248,7 +183,6 @@ pub fn update_attribute_map_from_entid_triples(
TypedValue::Ref(entids::DB_TYPE_REF) => { builder.value_type(ValueType::Ref); }, TypedValue::Ref(entids::DB_TYPE_REF) => { builder.value_type(ValueType::Ref); },
TypedValue::Ref(entids::DB_TYPE_STRING) => { builder.value_type(ValueType::String); }, TypedValue::Ref(entids::DB_TYPE_STRING) => { builder.value_type(ValueType::String); },
TypedValue::Ref(entids::DB_TYPE_UUID) => { builder.value_type(ValueType::Uuid); }, TypedValue::Ref(entids::DB_TYPE_UUID) => { builder.value_type(ValueType::Uuid); },
TypedValue::Ref(entids::DB_TYPE_BYTES) => { builder.value_type(ValueType::Bytes); },
_ => bail!(DbErrorKind::BadSchemaAssertion(format!("Expected [... :db/valueType :db.type/*] but got [... :db/valueType {:?}] for entid {} and attribute {}", value, entid, attr))) _ => bail!(DbErrorKind::BadSchemaAssertion(format!("Expected [... :db/valueType :db.type/*] but got [... :db/valueType {:?}] for entid {} and attribute {}", value, entid, attr)))
} }
}, },
@ -301,7 +235,7 @@ pub fn update_attribute_map_from_entid_triples(
bail!(DbErrorKind::BadSchemaAssertion(format!("Do not recognize attribute {} for entid {}", attr, entid))) bail!(DbErrorKind::BadSchemaAssertion(format!("Do not recognize attribute {} for entid {}", attr, entid)))
} }
} }
} };
let mut attributes_installed: BTreeSet<Entid> = BTreeSet::default(); let mut attributes_installed: BTreeSet<Entid> = BTreeSet::default();
let mut attributes_altered: BTreeMap<Entid, Vec<AttributeAlteration>> = BTreeMap::default(); let mut attributes_altered: BTreeMap<Entid, Vec<AttributeAlteration>> = BTreeMap::default();
@ -310,36 +244,26 @@ pub fn update_attribute_map_from_entid_triples(
match attribute_map.entry(entid) { match attribute_map.entry(entid) {
Entry::Vacant(entry) => { Entry::Vacant(entry) => {
// Validate once… // Validate once…
builder builder.validate_install_attribute().context(DbErrorKind::BadSchemaAssertion(format!("Schema alteration for new attribute with entid {} is not valid", entid)))?;
.validate_install_attribute()
.context(DbErrorKind::BadSchemaAssertion(format!(
"Schema alteration for new attribute with entid {} is not valid",
entid
)))?;
// … and twice, now we have the Attribute. // … and twice, now we have the Attribute.
let a = builder.build(); let a = builder.build();
a.validate(|| entid.to_string())?; a.validate(|| entid.to_string())?;
entry.insert(a); entry.insert(builder.build());
attributes_installed.insert(entid); attributes_installed.insert(entid);
} },
Entry::Occupied(mut entry) => { Entry::Occupied(mut entry) => {
builder builder.validate_alter_attribute().context(DbErrorKind::BadSchemaAssertion(format!("Schema alteration for existing attribute with entid {} is not valid", entid)))?;
.validate_alter_attribute()
.context(DbErrorKind::BadSchemaAssertion(format!(
"Schema alteration for existing attribute with entid {} is not valid",
entid
)))?;
let mutations = builder.mutate(entry.get_mut()); let mutations = builder.mutate(entry.get_mut());
attributes_altered.insert(entid, mutations); attributes_altered.insert(entid, mutations);
} },
} }
} }
Ok(MetadataReport { Ok(MetadataReport {
attributes_installed, attributes_installed: attributes_installed,
attributes_altered, attributes_altered: attributes_altered,
idents_altered: BTreeMap::default(), idents_altered: BTreeMap::default(),
}) })
} }
@ -352,19 +276,14 @@ pub fn update_attribute_map_from_entid_triples(
/// This is suitable for mutating a `Schema` from an applied transaction. /// This is suitable for mutating a `Schema` from an applied transaction.
/// ///
/// Returns a report summarizing the mutations that were applied. /// Returns a report summarizing the mutations that were applied.
pub fn update_schema_from_entid_quadruples<U>( pub fn update_schema_from_entid_quadruples<U>(schema: &mut Schema, assertions: U) -> Result<MetadataReport>
schema: &mut Schema, where U: IntoIterator<Item=(Entid, Entid, TypedValue, bool)> {
assertions: U,
) -> Result<MetadataReport>
where
U: IntoIterator<Item = (Entid, Entid, TypedValue, bool)>,
{
// Group attribute assertions into asserted, retracted, and updated. We assume all our // Group attribute assertions into asserted, retracted, and updated. We assume all our
// attribute assertions are :db/cardinality :db.cardinality/one (so they'll only be added or // attribute assertions are :db/cardinality :db.cardinality/one (so they'll only be added or
// retracted at most once), which means all attribute alterations are simple changes from an old // retracted at most once), which means all attribute alterations are simple changes from an old
// value to a new value. // value to a new value.
let mut attribute_set: AddRetractAlterSet<(Entid, Entid), TypedValue> = let mut attribute_set: AddRetractAlterSet<(Entid, Entid), TypedValue> = AddRetractAlterSet::default();
AddRetractAlterSet::default();
let mut ident_set: AddRetractAlterSet<Entid, symbols::Keyword> = AddRetractAlterSet::default(); let mut ident_set: AddRetractAlterSet<Entid, symbols::Keyword> = AddRetractAlterSet::default();
for (e, a, typed_value, added) in assertions.into_iter() { for (e, a, typed_value, added) in assertions.into_iter() {
@ -372,7 +291,7 @@ where
if a == entids::DB_IDENT { if a == entids::DB_IDENT {
if let TypedValue::Keyword(ref keyword) = typed_value { if let TypedValue::Keyword(ref keyword) = typed_value {
ident_set.witness(e, keyword.as_ref().clone(), added); ident_set.witness(e, keyword.as_ref().clone(), added);
continue; continue
} else { } else {
// Something is terribly wrong: the schema ensures we have a keyword. // Something is terribly wrong: the schema ensures we have a keyword.
unreachable!(); unreachable!();
@ -383,33 +302,13 @@ where
} }
// Collect triples. // Collect triples.
let retracted_triples = attribute_set let retracted_triples = attribute_set.retracted.into_iter().map(|((e, a), typed_value)| (e, a, typed_value));
.retracted let asserted_triples = attribute_set.asserted.into_iter().map(|((e, a), typed_value)| (e, a, typed_value));
.into_iter() let altered_triples = attribute_set.altered.into_iter().map(|((e, a), (_old_value, new_value))| (e, a, new_value));
.map(|((e, a), typed_value)| (e, a, typed_value));
let asserted_triples = attribute_set
.asserted
.into_iter()
.map(|((e, a), typed_value)| (e, a, typed_value));
let altered_triples = attribute_set
.altered
.into_iter()
.map(|((e, a), (_old_value, new_value))| (e, a, new_value));
// First we process retractions which remove schema. let report = update_attribute_map_from_entid_triples(&mut schema.attribute_map,
// This operation consumes our current list of attribute retractions, producing a filtered one. asserted_triples.chain(altered_triples),
let non_schema_retractions = update_attribute_map_from_schema_retractions( retracted_triples)?;
&mut schema.attribute_map,
retracted_triples.collect(),
&ident_set.retracted,
)?;
// Now we process all other retractions.
let report = update_attribute_map_from_entid_triples(
&mut schema.attribute_map,
asserted_triples.chain(altered_triples).collect(),
non_schema_retractions,
)?;
let mut idents_altered: BTreeMap<Entid, IdentAlteration> = BTreeMap::new(); let mut idents_altered: BTreeMap<Entid, IdentAlteration> = BTreeMap::new();
@ -427,25 +326,18 @@ where
idents_altered.insert(entid, IdentAlteration::Ident(new_ident.clone())); idents_altered.insert(entid, IdentAlteration::Ident(new_ident.clone()));
} }
for (entid, ident) in &ident_set.retracted { for (entid, ident) in ident_set.retracted {
schema.entid_map.remove(entid); schema.entid_map.remove(&entid);
schema.ident_map.remove(ident); schema.ident_map.remove(&ident);
idents_altered.insert(*entid, IdentAlteration::Ident(ident.clone())); idents_altered.insert(entid, IdentAlteration::Ident(ident.clone()));
} }
// Component attributes need to change if either: if report.attributes_did_change() {
// - a component attribute changed
// - a schema attribute that was a component was retracted
// These two checks are a rather heavy-handed way of keeping schema's
// component_attributes up-to-date: most of the time we'll rebuild it
// even though it's not necessary (e.g. a schema attribute that's _not_
// a component was removed, or a non-component related attribute changed).
if report.attributes_did_change() || !ident_set.retracted.is_empty() {
schema.update_component_attributes(); schema.update_component_attributes();
} }
Ok(MetadataReport { Ok(MetadataReport {
idents_altered, idents_altered: idents_altered,
..report .. report
}) })
} }

View file

@ -10,57 +10,51 @@
#![allow(dead_code)] #![allow(dead_code)]
use crate::db::TypedSQLValue; use db::TypedSQLValue;
use db_traits::errors::{DbErrorKind, Result};
use edn; use edn;
use errors::{
DbErrorKind,
Result,
};
use edn::symbols; use edn::symbols;
use mentat_core::{
use core_traits::{attribute, Attribute, Entid, KnownEntid, TypedValue, ValueType}; attribute,
Attribute,
use crate::metadata; Entid,
use crate::metadata::AttributeAlteration; EntidMap,
use mentat_core::{AttributeMap, EntidMap, HasSchema, IdentMap, Schema}; HasSchema,
IdentMap,
KnownEntid,
Schema,
AttributeMap,
TypedValue,
ValueType,
};
use metadata;
use metadata::{
AttributeAlteration,
};
pub trait AttributeValidation { pub trait AttributeValidation {
fn validate<F>(&self, ident: F) -> Result<()> fn validate<F>(&self, ident: F) -> Result<()> where F: Fn() -> String;
where
F: Fn() -> String;
} }
impl AttributeValidation for Attribute { impl AttributeValidation for Attribute {
fn validate<F>(&self, ident: F) -> Result<()> fn validate<F>(&self, ident: F) -> Result<()> where F: Fn() -> String {
where
F: Fn() -> String,
{
if self.unique == Some(attribute::Unique::Value) && !self.index { if self.unique == Some(attribute::Unique::Value) && !self.index {
bail!(DbErrorKind::BadSchemaAssertion(format!( bail!(DbErrorKind::BadSchemaAssertion(format!(":db/unique :db/unique_value without :db/index true for entid: {}", ident())))
":db/unique :db/unique_value without :db/index true for entid: {}",
ident()
)))
} }
if self.unique == Some(attribute::Unique::Identity) && !self.index { if self.unique == Some(attribute::Unique::Identity) && !self.index {
bail!(DbErrorKind::BadSchemaAssertion(format!( bail!(DbErrorKind::BadSchemaAssertion(format!(":db/unique :db/unique_identity without :db/index true for entid: {}", ident())))
":db/unique :db/unique_identity without :db/index true for entid: {}",
ident()
)))
} }
if self.fulltext && self.value_type != ValueType::String { if self.fulltext && self.value_type != ValueType::String {
bail!(DbErrorKind::BadSchemaAssertion(format!( bail!(DbErrorKind::BadSchemaAssertion(format!(":db/fulltext true without :db/valueType :db.type/string for entid: {}", ident())))
":db/fulltext true without :db/valueType :db.type/string for entid: {}",
ident()
)))
} }
if self.fulltext && !self.index { if self.fulltext && !self.index {
bail!(DbErrorKind::BadSchemaAssertion(format!( bail!(DbErrorKind::BadSchemaAssertion(format!(":db/fulltext true without :db/index true for entid: {}", ident())))
":db/fulltext true without :db/index true for entid: {}",
ident()
)))
} }
if self.component && self.value_type != ValueType::Ref { if self.component && self.value_type != ValueType::Ref {
bail!(DbErrorKind::BadSchemaAssertion(format!( bail!(DbErrorKind::BadSchemaAssertion(format!(":db/isComponent true without :db/valueType :db.type/ref for entid: {}", ident())))
":db/isComponent true without :db/valueType :db.type/ref for entid: {}",
ident()
)))
} }
// TODO: consider warning if we have :db/index true for :db/valueType :db.type/string, // TODO: consider warning if we have :db/index true for :db/valueType :db.type/string,
// since this may be inefficient. More generally, we should try to drive complex // since this may be inefficient. More generally, we should try to drive complex
@ -73,18 +67,13 @@ impl AttributeValidation for Attribute {
/// Return `Ok(())` if `attribute_map` defines a valid Mentat schema. /// Return `Ok(())` if `attribute_map` defines a valid Mentat schema.
fn validate_attribute_map(entid_map: &EntidMap, attribute_map: &AttributeMap) -> Result<()> { fn validate_attribute_map(entid_map: &EntidMap, attribute_map: &AttributeMap) -> Result<()> {
for (entid, attribute) in attribute_map { for (entid, attribute) in attribute_map {
let ident = || { let ident = || entid_map.get(entid).map(|ident| ident.to_string()).unwrap_or(entid.to_string());
entid_map
.get(entid)
.map(|ident| ident.to_string())
.unwrap_or_else(|| entid.to_string())
};
attribute.validate(ident)?; attribute.validate(ident)?;
} }
Ok(()) Ok(())
} }
#[derive(Clone, Debug, Default, Eq, Hash, Ord, PartialOrd, PartialEq)] #[derive(Clone,Debug,Default,Eq,Hash,Ord,PartialOrd,PartialEq)]
pub struct AttributeBuilder { pub struct AttributeBuilder {
helpful: bool, helpful: bool,
pub value_type: Option<ValueType>, pub value_type: Option<ValueType>,
@ -108,30 +97,30 @@ impl AttributeBuilder {
/// Make a new AttributeBuilder from an existing Attribute. This is important to allow /// Make a new AttributeBuilder from an existing Attribute. This is important to allow
/// retraction. Only attributes that we allow to change are duplicated here. /// retraction. Only attributes that we allow to change are duplicated here.
pub fn modify_attribute(attribute: &Attribute) -> Self { pub fn to_modify_attribute(attribute: &Attribute) -> Self {
let mut ab = AttributeBuilder::default(); let mut ab = AttributeBuilder::default();
ab.multival = Some(attribute.multival); ab.multival = Some(attribute.multival);
ab.unique = Some(attribute.unique); ab.unique = Some(attribute.unique);
ab.component = Some(attribute.component); ab.component = Some(attribute.component);
ab ab
} }
pub fn value_type(&mut self, value_type: ValueType) -> &mut Self { pub fn value_type<'a>(&'a mut self, value_type: ValueType) -> &'a mut Self {
self.value_type = Some(value_type); self.value_type = Some(value_type);
self self
} }
pub fn multival(&mut self, multival: bool) -> &mut Self { pub fn multival<'a>(&'a mut self, multival: bool) -> &'a mut Self {
self.multival = Some(multival); self.multival = Some(multival);
self self
} }
pub fn non_unique(&mut self) -> &mut Self { pub fn non_unique<'a>(&'a mut self) -> &'a mut Self {
self.unique = Some(None); self.unique = Some(None);
self self
} }
pub fn unique(&mut self, unique: attribute::Unique) -> &mut Self { pub fn unique<'a>(&'a mut self, unique: attribute::Unique) -> &'a mut Self {
if self.helpful && unique == attribute::Unique::Identity { if self.helpful && unique == attribute::Unique::Identity {
self.index = Some(true); self.index = Some(true);
} }
@ -139,12 +128,12 @@ impl AttributeBuilder {
self self
} }
pub fn index(&mut self, index: bool) -> &mut Self { pub fn index<'a>(&'a mut self, index: bool) -> &'a mut Self {
self.index = Some(index); self.index = Some(index);
self self
} }
pub fn fulltext(&mut self, fulltext: bool) -> &mut Self { pub fn fulltext<'a>(&'a mut self, fulltext: bool) -> &'a mut Self {
self.fulltext = Some(fulltext); self.fulltext = Some(fulltext);
if self.helpful && fulltext { if self.helpful && fulltext {
self.index = Some(true); self.index = Some(true);
@ -152,35 +141,29 @@ impl AttributeBuilder {
self self
} }
pub fn component(&mut self, component: bool) -> &mut Self { pub fn component<'a>(&'a mut self, component: bool) -> &'a mut Self {
self.component = Some(component); self.component = Some(component);
self self
} }
pub fn no_history(&mut self, no_history: bool) -> &mut Self { pub fn no_history<'a>(&'a mut self, no_history: bool) -> &'a mut Self {
self.no_history = Some(no_history); self.no_history = Some(no_history);
self self
} }
pub fn validate_install_attribute(&self) -> Result<()> { pub fn validate_install_attribute(&self) -> Result<()> {
if self.value_type.is_none() { if self.value_type.is_none() {
bail!(DbErrorKind::BadSchemaAssertion( bail!(DbErrorKind::BadSchemaAssertion("Schema attribute for new attribute does not set :db/valueType".into()));
"Schema attribute for new attribute does not set :db/valueType".into()
));
} }
Ok(()) Ok(())
} }
pub fn validate_alter_attribute(&self) -> Result<()> { pub fn validate_alter_attribute(&self) -> Result<()> {
if self.value_type.is_some() { if self.value_type.is_some() {
bail!(DbErrorKind::BadSchemaAssertion( bail!(DbErrorKind::BadSchemaAssertion("Schema alteration must not set :db/valueType".into()));
"Schema alteration must not set :db/valueType".into()
));
} }
if self.fulltext.is_some() { if self.fulltext.is_some() {
bail!(DbErrorKind::BadSchemaAssertion( bail!(DbErrorKind::BadSchemaAssertion("Schema alteration must not set :db/fulltext".into()));
"Schema alteration must not set :db/fulltext".into()
));
} }
Ok(()) Ok(())
} }
@ -197,7 +180,7 @@ impl AttributeBuilder {
attribute.multival = multival; attribute.multival = multival;
} }
if let Some(ref unique) = self.unique { if let Some(ref unique) = self.unique {
attribute.unique = *unique; attribute.unique = unique.clone();
} }
if let Some(index) = self.index { if let Some(index) = self.index {
attribute.index = index; attribute.index = index;
@ -223,12 +206,14 @@ impl AttributeBuilder {
if let Some(ref unique) = self.unique { if let Some(ref unique) = self.unique {
if *unique != attribute.unique { if *unique != attribute.unique {
attribute.unique = *unique; attribute.unique = unique.clone();
mutations.push(AttributeAlteration::Unique);
}
} else {
if attribute.unique != None {
attribute.unique = None;
mutations.push(AttributeAlteration::Unique); mutations.push(AttributeAlteration::Unique);
} }
} else if attribute.unique != None {
attribute.unique = None;
mutations.push(AttributeAlteration::Unique);
} }
if let Some(index) = self.index { if let Some(index) = self.index {
@ -258,37 +243,27 @@ pub trait SchemaBuilding {
fn require_ident(&self, entid: Entid) -> Result<&symbols::Keyword>; fn require_ident(&self, entid: Entid) -> Result<&symbols::Keyword>;
fn require_entid(&self, ident: &symbols::Keyword) -> Result<KnownEntid>; fn require_entid(&self, ident: &symbols::Keyword) -> Result<KnownEntid>;
fn require_attribute_for_entid(&self, entid: Entid) -> Result<&Attribute>; fn require_attribute_for_entid(&self, entid: Entid) -> Result<&Attribute>;
fn from_ident_map_and_attribute_map( fn from_ident_map_and_attribute_map(ident_map: IdentMap, attribute_map: AttributeMap) -> Result<Schema>;
ident_map: IdentMap,
attribute_map: AttributeMap,
) -> Result<Schema>;
fn from_ident_map_and_triples<U>(ident_map: IdentMap, assertions: U) -> Result<Schema> fn from_ident_map_and_triples<U>(ident_map: IdentMap, assertions: U) -> Result<Schema>
where where U: IntoIterator<Item=(symbols::Keyword, symbols::Keyword, TypedValue)>;
U: IntoIterator<Item = (symbols::Keyword, symbols::Keyword, TypedValue)>;
} }
impl SchemaBuilding for Schema { impl SchemaBuilding for Schema {
fn require_ident(&self, entid: Entid) -> Result<&symbols::Keyword> { fn require_ident(&self, entid: Entid) -> Result<&symbols::Keyword> {
self.get_ident(entid) self.get_ident(entid).ok_or(DbErrorKind::UnrecognizedEntid(entid).into())
.ok_or_else(|| DbErrorKind::UnrecognizedEntid(entid).into())
} }
fn require_entid(&self, ident: &symbols::Keyword) -> Result<KnownEntid> { fn require_entid(&self, ident: &symbols::Keyword) -> Result<KnownEntid> {
self.get_entid(&ident) self.get_entid(&ident).ok_or(DbErrorKind::UnrecognizedIdent(ident.to_string()).into())
.ok_or_else(|| DbErrorKind::UnrecognizedIdent(ident.to_string()).into())
} }
fn require_attribute_for_entid(&self, entid: Entid) -> Result<&Attribute> { fn require_attribute_for_entid(&self, entid: Entid) -> Result<&Attribute> {
self.attribute_for_entid(entid) self.attribute_for_entid(entid).ok_or(DbErrorKind::UnrecognizedEntid(entid).into())
.ok_or_else(|| DbErrorKind::UnrecognizedEntid(entid).into())
} }
/// Create a valid `Schema` from the constituent maps. /// Create a valid `Schema` from the constituent maps.
fn from_ident_map_and_attribute_map( fn from_ident_map_and_attribute_map(ident_map: IdentMap, attribute_map: AttributeMap) -> Result<Schema> {
ident_map: IdentMap, let entid_map: EntidMap = ident_map.iter().map(|(k, v)| (v.clone(), k.clone())).collect();
attribute_map: AttributeMap,
) -> Result<Schema> {
let entid_map: EntidMap = ident_map.iter().map(|(k, v)| (*v, k.clone())).collect();
validate_attribute_map(&entid_map, &attribute_map)?; validate_attribute_map(&entid_map, &attribute_map)?;
Ok(Schema::new(ident_map, entid_map, attribute_map)) Ok(Schema::new(ident_map, entid_map, attribute_map))
@ -296,30 +271,19 @@ impl SchemaBuilding for Schema {
/// Turn vec![(Keyword(:ident), Keyword(:key), TypedValue(:value)), ...] into a Mentat `Schema`. /// Turn vec![(Keyword(:ident), Keyword(:key), TypedValue(:value)), ...] into a Mentat `Schema`.
fn from_ident_map_and_triples<U>(ident_map: IdentMap, assertions: U) -> Result<Schema> fn from_ident_map_and_triples<U>(ident_map: IdentMap, assertions: U) -> Result<Schema>
where where U: IntoIterator<Item=(symbols::Keyword, symbols::Keyword, TypedValue)>{
U: IntoIterator<Item = (symbols::Keyword, symbols::Keyword, TypedValue)>,
{
let entid_assertions: Result<Vec<(Entid, Entid, TypedValue)>> = assertions
.into_iter()
.map(|(symbolic_ident, symbolic_attr, value)| {
let ident: i64 = *ident_map
.get(&symbolic_ident)
.ok_or_else(|| DbErrorKind::UnrecognizedIdent(symbolic_ident.to_string()))?;
let attr: i64 = *ident_map
.get(&symbolic_attr)
.ok_or_else(|| DbErrorKind::UnrecognizedIdent(symbolic_attr.to_string()))?;
Ok((ident, attr, value))
})
.collect();
let mut schema = let entid_assertions: Result<Vec<(Entid, Entid, TypedValue)>> = assertions.into_iter().map(|(symbolic_ident, symbolic_attr, value)| {
Schema::from_ident_map_and_attribute_map(ident_map, AttributeMap::default())?; let ident: i64 = *ident_map.get(&symbolic_ident).ok_or(DbErrorKind::UnrecognizedIdent(symbolic_ident.to_string()))?;
let metadata_report = metadata::update_attribute_map_from_entid_triples( let attr: i64 = *ident_map.get(&symbolic_attr).ok_or(DbErrorKind::UnrecognizedIdent(symbolic_attr.to_string()))?;
&mut schema.attribute_map, Ok((ident, attr, value))
entid_assertions?, }).collect();
// No retractions.
vec![], let mut schema = Schema::from_ident_map_and_attribute_map(ident_map, AttributeMap::default())?;
)?; let metadata_report = metadata::update_attribute_map_from_entid_triples(&mut schema.attribute_map,
entid_assertions?,
// No retractions.
::std::iter::empty())?;
// Rebuild the component attributes list if necessary. // Rebuild the component attributes list if necessary.
if metadata_report.attributes_did_change() { if metadata_report.attributes_did_change() {
@ -334,19 +298,11 @@ pub trait SchemaTypeChecking {
/// ///
/// Either assert that the given value is in the value type's value set, or (in limited cases) /// Either assert that the given value is in the value type's value set, or (in limited cases)
/// coerce the given value into the value type's value set. /// coerce the given value into the value type's value set.
fn to_typed_value( fn to_typed_value(&self, value: &edn::ValueAndSpan, value_type: ValueType) -> Result<TypedValue>;
&self,
value: &edn::ValueAndSpan,
value_type: ValueType,
) -> Result<TypedValue>;
} }
impl SchemaTypeChecking for Schema { impl SchemaTypeChecking for Schema {
fn to_typed_value( fn to_typed_value(&self, value: &edn::ValueAndSpan, value_type: ValueType) -> Result<TypedValue> {
&self,
value: &edn::ValueAndSpan,
value_type: ValueType,
) -> Result<TypedValue> {
// TODO: encapsulate entid-ident-attribute for better error messages, perhaps by including // TODO: encapsulate entid-ident-attribute for better error messages, perhaps by including
// the attribute (rather than just the attribute's value type) into this function or a // the attribute (rather than just the attribute's value type) into this function or a
// wrapper function. // wrapper function.
@ -362,41 +318,42 @@ impl SchemaTypeChecking for Schema {
(ValueType::Uuid, tv @ TypedValue::Uuid(_)) => Ok(tv), (ValueType::Uuid, tv @ TypedValue::Uuid(_)) => Ok(tv),
(ValueType::Instant, tv @ TypedValue::Instant(_)) => Ok(tv), (ValueType::Instant, tv @ TypedValue::Instant(_)) => Ok(tv),
(ValueType::Keyword, tv @ TypedValue::Keyword(_)) => Ok(tv), (ValueType::Keyword, tv @ TypedValue::Keyword(_)) => Ok(tv),
(ValueType::Bytes, tv @ TypedValue::Bytes(_)) => Ok(tv),
// Ref coerces a little: we interpret some things depending on the schema as a Ref. // Ref coerces a little: we interpret some things depending on the schema as a Ref.
(ValueType::Ref, TypedValue::Long(x)) => Ok(TypedValue::Ref(x)), (ValueType::Ref, TypedValue::Long(x)) => Ok(TypedValue::Ref(x)),
(ValueType::Ref, TypedValue::Keyword(ref x)) => { (ValueType::Ref, TypedValue::Keyword(ref x)) => self.require_entid(&x).map(|entid| entid.into()),
self.require_entid(&x).map(|entid| entid.into())
}
// Otherwise, we have a type mismatch. // Otherwise, we have a type mismatch.
// Enumerate all of the types here to allow the compiler to help us. // Enumerate all of the types here to allow the compiler to help us.
// We don't enumerate all `TypedValue` cases, though: that would multiply this // We don't enumerate all `TypedValue` cases, though: that would multiply this
// collection by 8! // collection by 8!
(vt @ ValueType::Boolean, _) (vt @ ValueType::Boolean, _) |
| (vt @ ValueType::Long, _) (vt @ ValueType::Long, _) |
| (vt @ ValueType::Double, _) (vt @ ValueType::Double, _) |
| (vt @ ValueType::String, _) (vt @ ValueType::String, _) |
| (vt @ ValueType::Uuid, _) (vt @ ValueType::Uuid, _) |
| (vt @ ValueType::Instant, _) (vt @ ValueType::Instant, _) |
| (vt @ ValueType::Keyword, _) (vt @ ValueType::Keyword, _) |
| (vt @ ValueType::Bytes, _) (vt @ ValueType::Ref, _)
| (vt @ ValueType::Ref, _) => { => bail!(DbErrorKind::BadValuePair(format!("{}", value), vt)),
bail!(DbErrorKind::BadValuePair(format!("{}", value), vt)) }
}
},
} }
} }
} }
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use self::edn::Keyword;
use super::*; use super::*;
use self::edn::Keyword;
fn add_attribute(schema: &mut Schema,
ident: Keyword,
entid: Entid,
attribute: Attribute) {
fn add_attribute(schema: &mut Schema, ident: Keyword, entid: Entid, attribute: Attribute) {
schema.entid_map.insert(entid, ident.clone()); schema.entid_map.insert(entid, ident.clone());
schema.ident_map.insert(ident, entid); schema.ident_map.insert(ident.clone(), entid);
if attribute.component { if attribute.component {
schema.component_attributes.push(entid); schema.component_attributes.push(entid);
@ -409,80 +366,55 @@ mod test {
fn validate_attribute_map_success() { fn validate_attribute_map_success() {
let mut schema = Schema::default(); let mut schema = Schema::default();
// attribute that is not an index has no uniqueness // attribute that is not an index has no uniqueness
add_attribute( add_attribute(&mut schema, Keyword::namespaced("foo", "bar"), 97, Attribute {
&mut schema, index: false,
Keyword::namespaced("foo", "bar"), value_type: ValueType::Boolean,
97, fulltext: false,
Attribute { unique: None,
index: false, multival: false,
value_type: ValueType::Boolean, component: false,
fulltext: false, no_history: false,
unique: None, });
multival: false,
component: false,
no_history: false,
},
);
// attribute is unique by value and an index // attribute is unique by value and an index
add_attribute( add_attribute(&mut schema, Keyword::namespaced("foo", "baz"), 98, Attribute {
&mut schema, index: true,
Keyword::namespaced("foo", "baz"), value_type: ValueType::Long,
98, fulltext: false,
Attribute { unique: Some(attribute::Unique::Value),
index: true, multival: false,
value_type: ValueType::Long, component: false,
fulltext: false, no_history: false,
unique: Some(attribute::Unique::Value), });
multival: false,
component: false,
no_history: false,
},
);
// attribue is unique by identity and an index // attribue is unique by identity and an index
add_attribute( add_attribute(&mut schema, Keyword::namespaced("foo", "bat"), 99, Attribute {
&mut schema, index: true,
Keyword::namespaced("foo", "bat"), value_type: ValueType::Ref,
99, fulltext: false,
Attribute { unique: Some(attribute::Unique::Identity),
index: true, multival: false,
value_type: ValueType::Ref, component: false,
fulltext: false, no_history: false,
unique: Some(attribute::Unique::Identity), });
multival: false,
component: false,
no_history: false,
},
);
// attribute is a components and a `Ref` // attribute is a components and a `Ref`
add_attribute( add_attribute(&mut schema, Keyword::namespaced("foo", "bak"), 100, Attribute {
&mut schema, index: false,
Keyword::namespaced("foo", "bak"), value_type: ValueType::Ref,
100, fulltext: false,
Attribute { unique: None,
index: false, multival: false,
value_type: ValueType::Ref, component: true,
fulltext: false, no_history: false,
unique: None, });
multival: false,
component: true,
no_history: false,
},
);
// fulltext attribute is a string and an index // fulltext attribute is a string and an index
add_attribute( add_attribute(&mut schema, Keyword::namespaced("foo", "bap"), 101, Attribute {
&mut schema, index: true,
Keyword::namespaced("foo", "bap"), value_type: ValueType::String,
101, fulltext: true,
Attribute { unique: None,
index: true, multival: false,
value_type: ValueType::String, component: false,
fulltext: true, no_history: false,
unique: None, });
multival: false,
component: false,
no_history: false,
},
);
assert!(validate_attribute_map(&schema.entid_map, &schema.attribute_map).is_ok()); assert!(validate_attribute_map(&schema.entid_map, &schema.attribute_map).is_ok());
} }
@ -492,150 +424,88 @@ mod test {
let mut schema = Schema::default(); let mut schema = Schema::default();
// attribute unique by value but not index // attribute unique by value but not index
let ident = Keyword::namespaced("foo", "bar"); let ident = Keyword::namespaced("foo", "bar");
add_attribute( add_attribute(&mut schema, ident , 99, Attribute {
&mut schema, index: false,
ident, value_type: ValueType::Boolean,
99, fulltext: false,
Attribute { unique: Some(attribute::Unique::Value),
index: false, multival: false,
value_type: ValueType::Boolean, component: false,
fulltext: false, no_history: false,
unique: Some(attribute::Unique::Value), });
multival: false,
component: false,
no_history: false,
},
);
let err = validate_attribute_map(&schema.entid_map, &schema.attribute_map) let err = validate_attribute_map(&schema.entid_map, &schema.attribute_map).err().map(|e| e.kind());
.err() assert_eq!(err, Some(DbErrorKind::BadSchemaAssertion(":db/unique :db/unique_value without :db/index true for entid: :foo/bar".into())));
.map(|e| e.kind());
assert_eq!(
err,
Some(DbErrorKind::BadSchemaAssertion(
":db/unique :db/unique_value without :db/index true for entid: :foo/bar".into()
))
);
} }
#[test] #[test]
fn invalid_schema_unique_identity_not_index() { fn invalid_schema_unique_identity_not_index() {
let mut schema = Schema::default(); let mut schema = Schema::default();
// attribute is unique by identity but not index // attribute is unique by identity but not index
add_attribute( add_attribute(&mut schema, Keyword::namespaced("foo", "bar"), 99, Attribute {
&mut schema, index: false,
Keyword::namespaced("foo", "bar"), value_type: ValueType::Long,
99, fulltext: false,
Attribute { unique: Some(attribute::Unique::Identity),
index: false, multival: false,
value_type: ValueType::Long, component: false,
fulltext: false, no_history: false,
unique: Some(attribute::Unique::Identity), });
multival: false,
component: false,
no_history: false,
},
);
let err = validate_attribute_map(&schema.entid_map, &schema.attribute_map) let err = validate_attribute_map(&schema.entid_map, &schema.attribute_map).err().map(|e| e.kind());
.err() assert_eq!(err, Some(DbErrorKind::BadSchemaAssertion(":db/unique :db/unique_identity without :db/index true for entid: :foo/bar".into())));
.map(|e| e.kind());
assert_eq!(
err,
Some(DbErrorKind::BadSchemaAssertion(
":db/unique :db/unique_identity without :db/index true for entid: :foo/bar".into()
))
);
} }
#[test] #[test]
fn invalid_schema_component_not_ref() { fn invalid_schema_component_not_ref() {
let mut schema = Schema::default(); let mut schema = Schema::default();
// attribute that is a component is not a `Ref` // attribute that is a component is not a `Ref`
add_attribute( add_attribute(&mut schema, Keyword::namespaced("foo", "bar"), 99, Attribute {
&mut schema, index: false,
Keyword::namespaced("foo", "bar"), value_type: ValueType::Boolean,
99, fulltext: false,
Attribute { unique: None,
index: false, multival: false,
value_type: ValueType::Boolean, component: true,
fulltext: false, no_history: false,
unique: None, });
multival: false,
component: true,
no_history: false,
},
);
let err = validate_attribute_map(&schema.entid_map, &schema.attribute_map) let err = validate_attribute_map(&schema.entid_map, &schema.attribute_map).err().map(|e| e.kind());
.err() assert_eq!(err, Some(DbErrorKind::BadSchemaAssertion(":db/isComponent true without :db/valueType :db.type/ref for entid: :foo/bar".into())));
.map(|e| e.kind());
assert_eq!(
err,
Some(DbErrorKind::BadSchemaAssertion(
":db/isComponent true without :db/valueType :db.type/ref for entid: :foo/bar"
.into()
))
);
} }
#[test] #[test]
fn invalid_schema_fulltext_not_index() { fn invalid_schema_fulltext_not_index() {
let mut schema = Schema::default(); let mut schema = Schema::default();
// attribute that is fulltext is not an index // attribute that is fulltext is not an index
add_attribute( add_attribute(&mut schema, Keyword::namespaced("foo", "bar"), 99, Attribute {
&mut schema, index: false,
Keyword::namespaced("foo", "bar"), value_type: ValueType::String,
99, fulltext: true,
Attribute { unique: None,
index: false, multival: false,
value_type: ValueType::String, component: false,
fulltext: true, no_history: false,
unique: None, });
multival: false,
component: false,
no_history: false,
},
);
let err = validate_attribute_map(&schema.entid_map, &schema.attribute_map) let err = validate_attribute_map(&schema.entid_map, &schema.attribute_map).err().map(|e| e.kind());
.err() assert_eq!(err, Some(DbErrorKind::BadSchemaAssertion(":db/fulltext true without :db/index true for entid: :foo/bar".into())));
.map(|e| e.kind());
assert_eq!(
err,
Some(DbErrorKind::BadSchemaAssertion(
":db/fulltext true without :db/index true for entid: :foo/bar".into()
))
);
} }
fn invalid_schema_fulltext_index_not_string() { fn invalid_schema_fulltext_index_not_string() {
let mut schema = Schema::default(); let mut schema = Schema::default();
// attribute that is fulltext and not a `String` // attribute that is fulltext and not a `String`
add_attribute( add_attribute(&mut schema, Keyword::namespaced("foo", "bar"), 99, Attribute {
&mut schema, index: true,
Keyword::namespaced("foo", "bar"), value_type: ValueType::Long,
99, fulltext: true,
Attribute { unique: None,
index: true, multival: false,
value_type: ValueType::Long, component: false,
fulltext: true, no_history: false,
unique: None, });
multival: false,
component: false,
no_history: false,
},
);
let err = validate_attribute_map(&schema.entid_map, &schema.attribute_map) let err = validate_attribute_map(&schema.entid_map, &schema.attribute_map).err().map(|e| e.kind());
.err() assert_eq!(err, Some(DbErrorKind::BadSchemaAssertion(":db/fulltext true without :db/valueType :db.type/string for entid: :foo/bar".into())));
.map(|e| e.kind());
assert_eq!(
err,
Some(DbErrorKind::BadSchemaAssertion(
":db/fulltext true without :db/valueType :db.type/string for entid: :foo/bar"
.into()
))
);
} }
} }

View file

@ -1,862 +0,0 @@
// 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.
use std::ops::RangeFrom;
use rusqlite::{self, params_from_iter};
use db_traits::errors::{DbErrorKind, Result};
use core_traits::{Entid, KnownEntid, TypedValue};
use mentat_core::Schema;
use edn::InternSet;
use edn::entities::OpType;
use crate::db;
use crate::db::TypedSQLValue;
use crate::tx::{transact_terms_with_action, TransactorAction};
use crate::types::PartitionMap;
use crate::internal_types::{Term, TermWithoutTempIds};
use crate::watcher::NullWatcher;
/// Collects a supplied tx range into an DESC ordered Vec of valid txs,
/// ensuring they all belong to the same timeline.
fn collect_ordered_txs_to_move(
conn: &rusqlite::Connection,
txs_from: RangeFrom<Entid>,
timeline: Entid,
) -> Result<Vec<Entid>> {
let mut stmt = conn.prepare("SELECT tx, timeline FROM timelined_transactions WHERE tx >= ? AND timeline = ? GROUP BY tx ORDER BY tx DESC")?;
let mut rows = stmt.query_and_then(
&[&txs_from.start, &timeline],
|row: &rusqlite::Row| -> Result<(Entid, Entid)> { Ok((row.get(0)?, row.get(1)?)) },
)?;
let mut txs = vec![];
// TODO do this in SQL instead?
let timeline = match rows.next() {
Some(t) => {
let t = t?;
txs.push(t.0);
t.1
}
None => bail!(DbErrorKind::TimelinesInvalidRange),
};
for t in rows {
let t = t?;
txs.push(t.0);
if t.1 != timeline {
bail!(DbErrorKind::TimelinesMixed);
}
}
Ok(txs)
}
fn move_transactions_to(
conn: &rusqlite::Connection,
tx_ids: &[Entid],
new_timeline: Entid,
) -> Result<()> {
// Move specified transactions over to a specified timeline.
conn.execute(
&format!(
"UPDATE timelined_transactions SET timeline = {} WHERE tx IN {}",
new_timeline,
crate::repeat_values(tx_ids.len(), 1)
),
params_from_iter(tx_ids.iter()),
)?;
Ok(())
}
fn remove_tx_from_datoms(conn: &rusqlite::Connection, tx_id: Entid) -> Result<()> {
conn.execute("DELETE FROM datoms WHERE e = ?", &[&tx_id])?;
Ok(())
}
fn is_timeline_empty(conn: &rusqlite::Connection, timeline: Entid) -> Result<bool> {
let mut stmt = conn.prepare(
"SELECT timeline FROM timelined_transactions WHERE timeline = ? GROUP BY timeline",
)?;
let rows = stmt.query_and_then(&[&timeline], |row| -> Result<i64> { Ok(row.get(0)?) })?;
Ok(rows.count() == 0)
}
/// Get terms for tx_id, reversing them in meaning (swap add & retract).
fn reversed_terms_for(
conn: &rusqlite::Connection,
tx_id: Entid,
) -> Result<Vec<TermWithoutTempIds>> {
let mut stmt = conn.prepare("SELECT e, a, v, value_type_tag, tx, added FROM timelined_transactions WHERE tx = ? AND timeline = ? ORDER BY tx DESC")?;
let rows = stmt.query_and_then(
&[&tx_id, &crate::TIMELINE_MAIN],
|row| -> Result<TermWithoutTempIds> {
let op = if row.get(5)? {
OpType::Retract
} else {
OpType::Add
};
Ok(Term::AddOrRetract(
op,
KnownEntid(row.get(0)?),
row.get(1)?,
TypedValue::from_sql_value_pair(row.get(2)?, row.get(3)?)?,
))
},
)?;
let mut terms = vec![];
for row in rows {
terms.push(row?);
}
Ok(terms)
}
/// Move specified transaction RangeFrom off of main timeline.
pub fn move_from_main_timeline(
conn: &rusqlite::Connection,
schema: &Schema,
partition_map: PartitionMap,
txs_from: RangeFrom<Entid>,
new_timeline: Entid,
) -> Result<(Option<Schema>, PartitionMap)> {
if new_timeline == crate::TIMELINE_MAIN {
bail!(DbErrorKind::NotYetImplemented(
"Can't move transactions to main timeline".to_string()
));
}
// We don't currently ensure that moving transactions onto a non-empty timeline
// will result in sensible end-state for that timeline.
// Let's remove that foot gun by prohibiting moving transactions to a non-empty timeline.
if !is_timeline_empty(conn, new_timeline)? {
bail!(DbErrorKind::TimelinesMoveToNonEmpty);
}
let txs_to_move = collect_ordered_txs_to_move(conn, txs_from, crate::TIMELINE_MAIN)?;
let mut last_schema = None;
for tx_id in &txs_to_move {
let reversed_terms = reversed_terms_for(conn, *tx_id)?;
// Rewind schema and datoms.
let (report, _, new_schema, _) = transact_terms_with_action(
conn,
partition_map.clone(),
schema,
schema,
NullWatcher(),
reversed_terms.into_iter().map(|t| t.rewrap()),
InternSet::new(),
TransactorAction::Materialize,
)?;
// Rewind operation generated a 'tx' and a 'txInstant' assertion, which got
// inserted into the 'datoms' table (due to TransactorAction::Materialize).
// This is problematic. If we transact a few more times, the transactor will
// generate the same 'tx', but with a different 'txInstant'.
// The end result will be a transaction which has a phantom
// retraction of a txInstant, since transactor operates against the state of
// 'datoms', and not against the 'transactions' table.
// A quick workaround is to just remove the bad txInstant datom.
// See test_clashing_tx_instants test case.
remove_tx_from_datoms(conn, report.tx_id)?;
last_schema = new_schema;
}
// Move transactions over to the target timeline.
move_transactions_to(conn, &txs_to_move, new_timeline)?;
Ok((last_schema, db::read_partition_map(conn)?))
}
#[cfg(test)]
mod tests {
use super::*;
use edn;
use std::borrow::Borrow;
use crate::debug::TestConn;
use crate::bootstrap;
// For convenience during testing.
// Real consumers will perform similar operations when appropriate.
fn update_conn(conn: &mut TestConn, schema: &Option<Schema>, pmap: &PartitionMap) {
match schema {
Some(ref s) => conn.schema = s.clone(),
None => (),
};
conn.partition_map = pmap.clone();
}
#[test]
fn test_pop_simple() {
let mut conn = TestConn::default();
conn.sanitized_partition_map();
let t = r#"
[{:db/id :db/doc :db/doc "test"}]
"#;
let partition_map0 = conn.partition_map.clone();
let report1 = assert_transact!(conn, t);
let partition_map1 = conn.partition_map.clone();
let (new_schema, new_partition_map) = move_from_main_timeline(
&conn.sqlite,
&conn.schema,
conn.partition_map.clone(),
conn.last_tx_id()..,
1,
)
.expect("moved single tx");
update_conn(&mut conn, &new_schema, &new_partition_map);
assert_matches!(conn.datoms(), "[]");
assert_matches!(conn.transactions(), "[]");
assert_eq!(new_partition_map, partition_map0);
conn.partition_map = partition_map0;
let report2 = assert_transact!(conn, t);
let partition_map2 = conn.partition_map.clone();
// Ensure that we can't move transactions to a non-empty timeline:
move_from_main_timeline(
&conn.sqlite,
&conn.schema,
conn.partition_map.clone(),
conn.last_tx_id()..,
1,
)
.expect_err("Can't move transactions to a non-empty timeline");
assert_eq!(report1.tx_id, report2.tx_id);
assert_eq!(partition_map1, partition_map2);
assert_matches!(
conn.datoms(),
r#"
[[37 :db/doc "test"]]
"#
);
assert_matches!(
conn.transactions(),
r#"
[[[37 :db/doc "test" ?tx true]
[?tx :db/txInstant ?ms ?tx true]]]
"#
);
}
#[test]
fn test_pop_ident() {
let mut conn = TestConn::default();
conn.sanitized_partition_map();
let t = r#"
[{:db/ident :test/entid :db/doc "test" :db.schema/version 1}]
"#;
let partition_map0 = conn.partition_map.clone();
let schema0 = conn.schema.clone();
let report1 = assert_transact!(conn, t);
let partition_map1 = conn.partition_map.clone();
let schema1 = conn.schema.clone();
let (new_schema, new_partition_map) = move_from_main_timeline(
&conn.sqlite,
&conn.schema,
conn.partition_map.clone(),
conn.last_tx_id()..,
1,
)
.expect("moved single tx");
update_conn(&mut conn, &new_schema, &new_partition_map);
assert_matches!(conn.datoms(), "[]");
assert_matches!(conn.transactions(), "[]");
assert_eq!(conn.partition_map, partition_map0);
assert_eq!(conn.schema, schema0);
let report2 = assert_transact!(conn, t);
assert_eq!(report1.tx_id, report2.tx_id);
assert_eq!(conn.partition_map, partition_map1);
assert_eq!(conn.schema, schema1);
assert_matches!(
conn.datoms(),
r#"
[[?e :db/ident :test/entid]
[?e :db/doc "test"]
[?e :db.schema/version 1]]
"#
);
assert_matches!(
conn.transactions(),
r#"
[[[?e :db/ident :test/entid ?tx true]
[?e :db/doc "test" ?tx true]
[?e :db.schema/version 1 ?tx true]
[?tx :db/txInstant ?ms ?tx true]]]
"#
);
}
#[test]
fn test_clashing_tx_instants() {
let mut conn = TestConn::default();
conn.sanitized_partition_map();
// Transact a basic schema.
assert_transact!(
conn,
r#"
[{:db/ident :person/name :db/valueType :db.type/string :db/cardinality :db.cardinality/one :db/unique :db.unique/identity :db/index true}]
"#
);
// Make an assertion against our schema.
assert_transact!(conn, r#"[{:person/name "Vanya"}]"#);
// Move that assertion away from the main timeline.
let (new_schema, new_partition_map) = move_from_main_timeline(
&conn.sqlite,
&conn.schema,
conn.partition_map.clone(),
conn.last_tx_id()..,
1,
)
.expect("moved single tx");
update_conn(&mut conn, &new_schema, &new_partition_map);
// Assert that our datoms are now just the schema.
assert_matches!(
conn.datoms(),
"
[[?e :db/ident :person/name]
[?e :db/valueType :db.type/string]
[?e :db/cardinality :db.cardinality/one]
[?e :db/unique :db.unique/identity]
[?e :db/index true]]"
);
// Same for transactions.
assert_matches!(
conn.transactions(),
"
[[[?e :db/ident :person/name ?tx true]
[?e :db/valueType :db.type/string ?tx true]
[?e :db/cardinality :db.cardinality/one ?tx true]
[?e :db/unique :db.unique/identity ?tx true]
[?e :db/index true ?tx true]
[?tx :db/txInstant ?ms ?tx true]]]"
);
// Re-assert our initial fact against our schema.
assert_transact!(
conn,
r#"
[[:db/add "tempid" :person/name "Vanya"]]"#
);
// Now, change that fact. This is the "clashing" transaction, if we're
// performing a timeline move using the transactor.
assert_transact!(
conn,
r#"
[[:db/add (lookup-ref :person/name "Vanya") :person/name "Ivan"]]"#
);
// Assert that our datoms are now the schema and the final assertion.
assert_matches!(
conn.datoms(),
r#"
[[?e1 :db/ident :person/name]
[?e1 :db/valueType :db.type/string]
[?e1 :db/cardinality :db.cardinality/one]
[?e1 :db/unique :db.unique/identity]
[?e1 :db/index true]
[?e2 :person/name "Ivan"]]
"#
);
// Assert that we have three correct looking transactions.
// This will fail if we're not cleaning up the 'datoms' table
// after the timeline move.
assert_matches!(
conn.transactions(),
r#"
[[
[?e1 :db/ident :person/name ?tx1 true]
[?e1 :db/valueType :db.type/string ?tx1 true]
[?e1 :db/cardinality :db.cardinality/one ?tx1 true]
[?e1 :db/unique :db.unique/identity ?tx1 true]
[?e1 :db/index true ?tx1 true]
[?tx1 :db/txInstant ?ms1 ?tx1 true]
]
[
[?e2 :person/name "Vanya" ?tx2 true]
[?tx2 :db/txInstant ?ms2 ?tx2 true]
]
[
[?e2 :person/name "Ivan" ?tx3 true]
[?e2 :person/name "Vanya" ?tx3 false]
[?tx3 :db/txInstant ?ms3 ?tx3 true]
]]
"#
);
}
#[test]
fn test_pop_schema() {
let mut conn = TestConn::default();
conn.sanitized_partition_map();
let t = r#"
[{:db/id "e" :db/ident :test/one :db/valueType :db.type/long :db/cardinality :db.cardinality/one}
{:db/id "f" :db/ident :test/many :db/valueType :db.type/long :db/cardinality :db.cardinality/many}]
"#;
let partition_map0 = conn.partition_map.clone();
let schema0 = conn.schema.clone();
let report1 = assert_transact!(conn, t);
let partition_map1 = conn.partition_map.clone();
let schema1 = conn.schema.clone();
let (new_schema, new_partition_map) = move_from_main_timeline(
&conn.sqlite,
&conn.schema,
conn.partition_map.clone(),
report1.tx_id..,
1,
)
.expect("moved single tx");
update_conn(&mut conn, &new_schema, &new_partition_map);
assert_matches!(conn.datoms(), "[]");
assert_matches!(conn.transactions(), "[]");
assert_eq!(conn.partition_map, partition_map0);
assert_eq!(conn.schema, schema0);
let report2 = assert_transact!(conn, t);
let partition_map2 = conn.partition_map.clone();
let schema2 = conn.schema.clone();
assert_eq!(report1.tx_id, report2.tx_id);
assert_eq!(partition_map1, partition_map2);
assert_eq!(schema1, schema2);
assert_matches!(
conn.datoms(),
r#"
[[?e1 :db/ident :test/one]
[?e1 :db/valueType :db.type/long]
[?e1 :db/cardinality :db.cardinality/one]
[?e2 :db/ident :test/many]
[?e2 :db/valueType :db.type/long]
[?e2 :db/cardinality :db.cardinality/many]]
"#
);
assert_matches!(
conn.transactions(),
r#"
[[[?e1 :db/ident :test/one ?tx1 true]
[?e1 :db/valueType :db.type/long ?tx1 true]
[?e1 :db/cardinality :db.cardinality/one ?tx1 true]
[?e2 :db/ident :test/many ?tx1 true]
[?e2 :db/valueType :db.type/long ?tx1 true]
[?e2 :db/cardinality :db.cardinality/many ?tx1 true]
[?tx1 :db/txInstant ?ms ?tx1 true]]]
"#
);
}
#[test]
fn test_pop_schema_all_attributes() {
let mut conn = TestConn::default();
conn.sanitized_partition_map();
let t = r#"
[{
:db/id "e"
:db/ident :test/one
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one
:db/unique :db.unique/value
:db/index true
:db/fulltext true
}]
"#;
let partition_map0 = conn.partition_map.clone();
let schema0 = conn.schema.clone();
let report1 = assert_transact!(conn, t);
let partition_map1 = conn.partition_map.clone();
let schema1 = conn.schema.clone();
let (new_schema, new_partition_map) = move_from_main_timeline(
&conn.sqlite,
&conn.schema,
conn.partition_map.clone(),
report1.tx_id..,
1,
)
.expect("moved single tx");
update_conn(&mut conn, &new_schema, &new_partition_map);
assert_matches!(conn.datoms(), "[]");
assert_matches!(conn.transactions(), "[]");
assert_eq!(conn.partition_map, partition_map0);
assert_eq!(conn.schema, schema0);
let report2 = assert_transact!(conn, t);
let partition_map2 = conn.partition_map.clone();
let schema2 = conn.schema.clone();
assert_eq!(report1.tx_id, report2.tx_id);
assert_eq!(partition_map1, partition_map2);
assert_eq!(schema1, schema2);
assert_matches!(
conn.datoms(),
r#"
[[?e1 :db/ident :test/one]
[?e1 :db/valueType :db.type/string]
[?e1 :db/cardinality :db.cardinality/one]
[?e1 :db/unique :db.unique/value]
[?e1 :db/index true]
[?e1 :db/fulltext true]]
"#
);
assert_matches!(
conn.transactions(),
r#"
[[[?e1 :db/ident :test/one ?tx1 true]
[?e1 :db/valueType :db.type/string ?tx1 true]
[?e1 :db/cardinality :db.cardinality/one ?tx1 true]
[?e1 :db/unique :db.unique/value ?tx1 true]
[?e1 :db/index true ?tx1 true]
[?e1 :db/fulltext true ?tx1 true]
[?tx1 :db/txInstant ?ms ?tx1 true]]]
"#
);
}
#[test]
fn test_pop_schema_all_attributes_component() {
let mut conn = TestConn::default();
conn.sanitized_partition_map();
let t = r#"
[{
:db/id "e"
:db/ident :test/one
:db/valueType :db.type/ref
:db/cardinality :db.cardinality/one
:db/unique :db.unique/value
:db/index true
:db/isComponent true
}]
"#;
let partition_map0 = conn.partition_map.clone();
let schema0 = conn.schema.clone();
let report1 = assert_transact!(conn, t);
let partition_map1 = conn.partition_map.clone();
let schema1 = conn.schema.clone();
let (new_schema, new_partition_map) = move_from_main_timeline(
&conn.sqlite,
&conn.schema,
conn.partition_map.clone(),
report1.tx_id..,
1,
)
.expect("moved single tx");
update_conn(&mut conn, &new_schema, &new_partition_map);
assert_matches!(conn.datoms(), "[]");
assert_matches!(conn.transactions(), "[]");
assert_eq!(conn.partition_map, partition_map0);
// Assert all of schema's components individually, for some guidance in case of failures:
assert_eq!(conn.schema.entid_map, schema0.entid_map);
assert_eq!(conn.schema.ident_map, schema0.ident_map);
assert_eq!(conn.schema.attribute_map, schema0.attribute_map);
assert_eq!(
conn.schema.component_attributes,
schema0.component_attributes
);
// Assert the whole schema, just in case we missed something:
assert_eq!(conn.schema, schema0);
let report2 = assert_transact!(conn, t);
let partition_map2 = conn.partition_map.clone();
let schema2 = conn.schema.clone();
assert_eq!(report1.tx_id, report2.tx_id);
assert_eq!(partition_map1, partition_map2);
assert_eq!(schema1, schema2);
assert_matches!(
conn.datoms(),
r#"
[[?e1 :db/ident :test/one]
[?e1 :db/valueType :db.type/ref]
[?e1 :db/cardinality :db.cardinality/one]
[?e1 :db/unique :db.unique/value]
[?e1 :db/isComponent true]
[?e1 :db/index true]]
"#
);
assert_matches!(
conn.transactions(),
r#"
[[[?e1 :db/ident :test/one ?tx1 true]
[?e1 :db/valueType :db.type/ref ?tx1 true]
[?e1 :db/cardinality :db.cardinality/one ?tx1 true]
[?e1 :db/unique :db.unique/value ?tx1 true]
[?e1 :db/isComponent true ?tx1 true]
[?e1 :db/index true ?tx1 true]
[?tx1 :db/txInstant ?ms ?tx1 true]]]
"#
);
}
#[test]
fn test_pop_in_sequence() {
let mut conn = TestConn::default();
conn.sanitized_partition_map();
let partition_map_after_bootstrap = conn.partition_map.clone();
assert_eq!(
(65536..65538),
conn.partition_map.allocate_entids(":db.part/user", 2)
);
let tx_report0 = assert_transact!(
conn,
r#"[
{:db/id 65536 :db/ident :test/one :db/valueType :db.type/long :db/cardinality :db.cardinality/one :db/unique :db.unique/identity :db/index true}
{:db/id 65537 :db/ident :test/many :db/valueType :db.type/long :db/cardinality :db.cardinality/many}
]"#
);
let first = "[
[65536 :db/ident :test/one]
[65536 :db/valueType :db.type/long]
[65536 :db/cardinality :db.cardinality/one]
[65536 :db/unique :db.unique/identity]
[65536 :db/index true]
[65537 :db/ident :test/many]
[65537 :db/valueType :db.type/long]
[65537 :db/cardinality :db.cardinality/many]
]";
assert_matches!(conn.datoms(), first);
let partition_map0 = conn.partition_map.clone();
assert_eq!(
(65538..65539),
conn.partition_map.allocate_entids(":db.part/user", 1)
);
let tx_report1 = assert_transact!(
conn,
r#"[
[:db/add 65538 :test/one 1]
[:db/add 65538 :test/many 2]
[:db/add 65538 :test/many 3]
]"#
);
let schema1 = conn.schema.clone();
let partition_map1 = conn.partition_map.clone();
assert_matches!(
conn.last_transaction(),
"[[65538 :test/one 1 ?tx true]
[65538 :test/many 2 ?tx true]
[65538 :test/many 3 ?tx true]
[?tx :db/txInstant ?ms ?tx true]]"
);
let second = "[
[65536 :db/ident :test/one]
[65536 :db/valueType :db.type/long]
[65536 :db/cardinality :db.cardinality/one]
[65536 :db/unique :db.unique/identity]
[65536 :db/index true]
[65537 :db/ident :test/many]
[65537 :db/valueType :db.type/long]
[65537 :db/cardinality :db.cardinality/many]
[65538 :test/one 1]
[65538 :test/many 2]
[65538 :test/many 3]
]";
assert_matches!(conn.datoms(), second);
let tx_report2 = assert_transact!(
conn,
r#"[
[:db/add 65538 :test/one 2]
[:db/add 65538 :test/many 2]
[:db/retract 65538 :test/many 3]
[:db/add 65538 :test/many 4]
]"#
);
let schema2 = conn.schema.clone();
assert_matches!(
conn.last_transaction(),
"[[65538 :test/one 1 ?tx false]
[65538 :test/one 2 ?tx true]
[65538 :test/many 3 ?tx false]
[65538 :test/many 4 ?tx true]
[?tx :db/txInstant ?ms ?tx true]]"
);
let third = "[
[65536 :db/ident :test/one]
[65536 :db/valueType :db.type/long]
[65536 :db/cardinality :db.cardinality/one]
[65536 :db/unique :db.unique/identity]
[65536 :db/index true]
[65537 :db/ident :test/many]
[65537 :db/valueType :db.type/long]
[65537 :db/cardinality :db.cardinality/many]
[65538 :test/one 2]
[65538 :test/many 2]
[65538 :test/many 4]
]";
assert_matches!(conn.datoms(), third);
let (new_schema, new_partition_map) = move_from_main_timeline(
&conn.sqlite,
&conn.schema,
conn.partition_map.clone(),
tx_report2.tx_id..,
1,
)
.expect("moved timeline");
update_conn(&mut conn, &new_schema, &new_partition_map);
assert_matches!(conn.datoms(), second);
// Moving didn't change the schema.
assert_eq!(None, new_schema);
assert_eq!(conn.schema, schema2);
// But it did change the partition map.
assert_eq!(conn.partition_map, partition_map1);
let (new_schema, new_partition_map) = move_from_main_timeline(
&conn.sqlite,
&conn.schema,
conn.partition_map.clone(),
tx_report1.tx_id..,
2,
)
.expect("moved timeline");
update_conn(&mut conn, &new_schema, &new_partition_map);
assert_matches!(conn.datoms(), first);
assert_eq!(None, new_schema);
assert_eq!(schema1, conn.schema);
assert_eq!(conn.partition_map, partition_map0);
let (new_schema, new_partition_map) = move_from_main_timeline(
&conn.sqlite,
&conn.schema,
conn.partition_map.clone(),
tx_report0.tx_id..,
3,
)
.expect("moved timeline");
update_conn(&mut conn, &new_schema, &new_partition_map);
assert_eq!(true, new_schema.is_some());
assert_eq!(bootstrap::bootstrap_schema(), conn.schema);
assert_eq!(partition_map_after_bootstrap, conn.partition_map);
assert_matches!(conn.datoms(), "[]");
assert_matches!(conn.transactions(), "[]");
}
#[test]
fn test_move_range() {
let mut conn = TestConn::default();
conn.sanitized_partition_map();
let partition_map_after_bootstrap = conn.partition_map.clone();
assert_eq!(
(65536..65539),
conn.partition_map.allocate_entids(":db.part/user", 3)
);
let tx_report0 = assert_transact!(
conn,
r#"[
{:db/id 65536 :db/ident :test/one :db/valueType :db.type/long :db/cardinality :db.cardinality/one}
{:db/id 65537 :db/ident :test/many :db/valueType :db.type/long :db/cardinality :db.cardinality/many}
]"#
);
assert_transact!(
conn,
r#"[
[:db/add 65538 :test/one 1]
[:db/add 65538 :test/many 2]
[:db/add 65538 :test/many 3]
]"#
);
assert_transact!(
conn,
r#"[
[:db/add 65538 :test/one 2]
[:db/add 65538 :test/many 2]
[:db/retract 65538 :test/many 3]
[:db/add 65538 :test/many 4]
]"#
);
// Remove all of these transactions from the main timeline,
// ensure we get back to a "just bootstrapped" state.
let (new_schema, new_partition_map) = move_from_main_timeline(
&conn.sqlite,
&conn.schema,
conn.partition_map.clone(),
tx_report0.tx_id..,
1,
)
.expect("moved timeline");
update_conn(&mut conn, &new_schema, &new_partition_map);
update_conn(&mut conn, &new_schema, &new_partition_map);
assert_eq!(true, new_schema.is_some());
assert_eq!(bootstrap::bootstrap_schema(), conn.schema);
assert_eq!(partition_map_after_bootstrap, conn.partition_map);
assert_matches!(conn.datoms(), "[]");
assert_matches!(conn.transactions(), "[]");
}
}

File diff suppressed because it is too large Load diff

View file

@ -8,13 +8,24 @@
// CONDITIONS OF ANY KIND, either express or implied. See the License for the // CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License. // specific language governing permissions and limitations under the License.
use std::collections::{BTreeMap, BTreeSet}; use std::collections::{
BTreeSet,
BTreeMap,
};
use core_traits::{Entid, TypedValue, ValueType}; use mentat_core::{
Entid,
TypedValue,
ValueType,
};
use db_traits::errors::CardinalityConflict; use errors::{
CardinalityConflict,
};
use crate::internal_types::AEVTrie; use internal_types::{
AEVTrie,
};
/// Map from found [e a v] to expected type. /// Map from found [e a v] to expected type.
pub(crate) type TypeDisagreements = BTreeMap<(Entid, Entid, TypedValue), ValueType>; pub(crate) type TypeDisagreements = BTreeMap<(Entid, Entid, TypedValue), ValueType>;
@ -52,9 +63,7 @@ pub(crate) fn type_disagreements<'schema>(aev_trie: &AEVTrie<'schema>) -> TypeDi
/// We try to be maximally helpful by yielding every malformed set of datoms, rather than just the /// We try to be maximally helpful by yielding every malformed set of datoms, rather than just the
/// first set, or even the first conflict. In the future, we might change this choice, or allow the /// first set, or even the first conflict. In the future, we might change this choice, or allow the
/// consumer to specify the robustness of the cardinality checking desired. /// consumer to specify the robustness of the cardinality checking desired.
pub(crate) fn cardinality_conflicts<'schema>( pub(crate) fn cardinality_conflicts<'schema>(aev_trie: &AEVTrie<'schema>) -> Vec<CardinalityConflict> {
aev_trie: &AEVTrie<'schema>,
) -> Vec<CardinalityConflict> {
let mut errors = vec![]; let mut errors = vec![];
for (&(a, attribute), evs) in aev_trie { for (&(a, attribute), evs) in aev_trie {

View file

@ -8,51 +8,61 @@
// CONDITIONS OF ANY KIND, either express or implied. See the License for the // CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License. // specific language governing permissions and limitations under the License.
use std::sync::{Arc, Weak}; use std::sync::{
Arc,
Weak,
};
use std::sync::mpsc::{channel, Receiver, RecvError, Sender}; use std::sync::mpsc::{
channel,
Receiver,
RecvError,
Sender,
};
use std::thread; use std::thread;
use indexmap::IndexMap; use indexmap::{
IndexMap,
};
use core_traits::{Entid, TypedValue}; use mentat_core::{
Entid,
Schema,
TypedValue,
};
use mentat_core::Schema; use edn::entities::{
OpType,
};
use edn::entities::OpType; use errors::{
Result,
};
use db_traits::errors::Result; use types::{
AttributeSet,
};
use crate::types::AttributeSet; use watcher::TransactWatcher;
use crate::watcher::TransactWatcher;
pub struct TxObserver { pub struct TxObserver {
#[allow(clippy::type_complexity)] notify_fn: Arc<Box<Fn(&str, IndexMap<&Entid, &AttributeSet>) + Send + Sync>>,
notify_fn: Arc<Box<dyn Fn(&str, IndexMap<&Entid, &AttributeSet>) + Send + Sync>>,
attributes: AttributeSet, attributes: AttributeSet,
} }
impl TxObserver { impl TxObserver {
pub fn new<F>(attributes: AttributeSet, notify_fn: F) -> TxObserver pub fn new<F>(attributes: AttributeSet, notify_fn: F) -> TxObserver where F: Fn(&str, IndexMap<&Entid, &AttributeSet>) + 'static + Send + Sync {
where
F: Fn(&str, IndexMap<&Entid, &AttributeSet>) + 'static + Send + Sync,
{
TxObserver { TxObserver {
notify_fn: Arc::new(Box::new(notify_fn)), notify_fn: Arc::new(Box::new(notify_fn)),
attributes, attributes,
} }
} }
pub fn applicable_reports<'r>( pub fn applicable_reports<'r>(&self, reports: &'r IndexMap<Entid, AttributeSet>) -> IndexMap<&'r Entid, &'r AttributeSet> {
&self, reports.into_iter()
reports: &'r IndexMap<Entid, AttributeSet>, .filter(|&(_txid, attrs)| !self.attributes.is_disjoint(attrs))
) -> IndexMap<&'r Entid, &'r AttributeSet> { .collect()
reports
.into_iter()
.filter(|&(_txid, attrs)| !self.attributes.is_disjoint(attrs))
.collect()
} }
fn notify(&self, key: &str, reports: IndexMap<&Entid, &AttributeSet>) { fn notify(&self, key: &str, reports: IndexMap<&Entid, &AttributeSet>) {
@ -70,10 +80,7 @@ pub struct TxCommand {
} }
impl TxCommand { impl TxCommand {
fn new( fn new(observers: &Arc<IndexMap<String, Arc<TxObserver>>>, reports: IndexMap<Entid, AttributeSet>) -> Self {
observers: &Arc<IndexMap<String, Arc<TxObserver>>>,
reports: IndexMap<Entid, AttributeSet>,
) -> Self {
TxCommand { TxCommand {
reports, reports,
observers: Arc::downgrade(observers), observers: Arc::downgrade(observers),
@ -83,21 +90,20 @@ impl TxCommand {
impl Command for TxCommand { impl Command for TxCommand {
fn execute(&mut self) { fn execute(&mut self) {
if let Some(observers) = self.observers.upgrade() { self.observers.upgrade().map(|observers| {
for (key, observer) in observers.iter() { for (key, observer) in observers.iter() {
let applicable_reports = observer.applicable_reports(&self.reports); let applicable_reports = observer.applicable_reports(&self.reports);
if !applicable_reports.is_empty() { if !applicable_reports.is_empty() {
observer.notify(&key, applicable_reports); observer.notify(&key, applicable_reports);
} }
} }
} });
} }
} }
#[derive(Default)]
pub struct TxObservationService { pub struct TxObservationService {
observers: Arc<IndexMap<String, Arc<TxObserver>>>, observers: Arc<IndexMap<String, Arc<TxObserver>>>,
executor: Option<Sender<Box<dyn Command + Send>>>, executor: Option<Sender<Box<Command + Send>>>,
} }
impl TxObservationService { impl TxObservationService {
@ -109,7 +115,7 @@ impl TxObservationService {
} }
// For testing purposes // For testing purposes
pub fn is_registered(&self, key: &str) -> bool { pub fn is_registered(&self, key: &String) -> bool {
self.observers.contains_key(key) self.observers.contains_key(key)
} }
@ -117,7 +123,7 @@ impl TxObservationService {
Arc::make_mut(&mut self.observers).insert(key, observer); Arc::make_mut(&mut self.observers).insert(key, observer);
} }
pub fn deregister(&mut self, key: &str) { pub fn deregister(&mut self, key: &String) {
Arc::make_mut(&mut self.observers).remove(key); Arc::make_mut(&mut self.observers).remove(key);
} }
@ -132,11 +138,7 @@ impl TxObservationService {
} }
let executor = self.executor.get_or_insert_with(|| { let executor = self.executor.get_or_insert_with(|| {
#[allow(clippy::type_complexity)] let (tx, rx): (Sender<Box<Command + Send>>, Receiver<Box<Command + Send>>) = channel();
let (tx, rx): (
Sender<Box<dyn Command + Send>>,
Receiver<Box<dyn Command + Send>>,
) = channel();
let mut worker = CommandExecutor::new(rx); let mut worker = CommandExecutor::new(rx);
thread::spawn(move || { thread::spawn(move || {
@ -157,7 +159,6 @@ impl Drop for TxObservationService {
} }
} }
#[derive(Default)]
pub struct InProgressObserverTransactWatcher { pub struct InProgressObserverTransactWatcher {
collected_attributes: AttributeSet, collected_attributes: AttributeSet,
pub txes: IndexMap<Entid, AttributeSet>, pub txes: IndexMap<Entid, AttributeSet>,
@ -178,19 +179,21 @@ impl TransactWatcher for InProgressObserverTransactWatcher {
} }
fn done(&mut self, t: &Entid, _schema: &Schema) -> Result<()> { fn done(&mut self, t: &Entid, _schema: &Schema) -> Result<()> {
let collected_attributes = ::std::mem::take(&mut self.collected_attributes); let collected_attributes = ::std::mem::replace(&mut self.collected_attributes, Default::default());
self.txes.insert(*t, collected_attributes); self.txes.insert(*t, collected_attributes);
Ok(()) Ok(())
} }
} }
struct CommandExecutor { struct CommandExecutor {
receiver: Receiver<Box<dyn Command + Send>>, receiver: Receiver<Box<Command + Send>>,
} }
impl CommandExecutor { impl CommandExecutor {
fn new(rx: Receiver<Box<dyn Command + Send>>) -> Self { fn new(rx: Receiver<Box<Command + Send>>) -> Self {
CommandExecutor { receiver: rx } CommandExecutor {
receiver: rx,
}
} }
fn main(&mut self) { fn main(&mut self) {
@ -201,10 +204,12 @@ impl CommandExecutor {
// sync_channel) is disconnected, implying that no further messages will ever be // sync_channel) is disconnected, implying that no further messages will ever be
// received." // received."
// No need to log here. // No need to log here.
return; return
} },
Ok(mut cmd) => cmd.execute(), Ok(mut cmd) => {
cmd.execute()
},
} }
} }
} }

View file

@ -10,110 +10,59 @@
#![allow(dead_code)] #![allow(dead_code)]
use std::collections::{BTreeMap, BTreeSet, HashMap}; use std::collections::HashMap;
use std::iter::FromIterator; use std::collections::{
use std::ops::{Deref, DerefMut, Range}; BTreeMap,
BTreeSet,
};
extern crate mentat_core; extern crate mentat_core;
use core_traits::{Entid, TypedValue, ValueType}; pub use self::mentat_core::{
Attribute,
AttributeBitFlags,
DateTime,
Entid,
Schema,
TypedValue,
Utc,
ValueType,
};
pub use self::mentat_core::{DateTime, Schema, Utc}; use edn::entities::{
EntityPlace,
TempId,
};
use edn::entities::{EntityPlace, TempId}; use errors;
use db_traits::errors;
/// Represents one partition of the entid space. /// Represents one partition of the entid space.
#[derive(Clone, Debug, Eq, Hash, Ord, PartialOrd, PartialEq)] #[derive(Clone,Debug,Eq,Hash,Ord,PartialOrd,PartialEq)]
#[cfg_attr(feature = "syncable", derive(Serialize, Deserialize))]
pub struct Partition { pub struct Partition {
/// The first entid in the partition. /// The first entid in the partition.
pub start: Entid, pub start: i64,
/// Maximum allowed entid in the partition.
pub end: Entid,
/// `true` if entids in the partition can be excised with `:db/excise`.
pub allow_excision: bool,
/// The next entid to be allocated in the partition. /// The next entid to be allocated in the partition.
/// Unless you must use this directly, prefer using provided setter and getter helpers. pub index: i64,
pub(crate) next_entid_to_allocate: Entid,
} }
impl Partition { impl Partition {
pub fn new( pub fn new(start: i64, next: i64) -> Partition {
start: Entid, assert!(start <= next, "A partition represents a monotonic increasing sequence of entids.");
end: Entid, Partition { start: start, index: next }
next_entid_to_allocate: Entid,
allow_excision: bool,
) -> Partition {
assert!(
start <= next_entid_to_allocate && next_entid_to_allocate <= end,
"A partition represents a monotonic increasing sequence of entids."
);
Partition {
start,
end,
next_entid_to_allocate,
allow_excision,
}
} }
pub fn contains_entid(&self, e: Entid) -> bool { pub fn contains_entid(&self, e: i64) -> bool {
(e >= self.start) && (e < self.next_entid_to_allocate) (e >= self.start) && (e < self.index)
}
pub fn allows_entid(&self, e: Entid) -> bool {
(e >= self.start) && (e <= self.end)
}
pub fn next_entid(&self) -> Entid {
self.next_entid_to_allocate
}
pub fn set_next_entid(&mut self, e: Entid) {
assert!(
self.allows_entid(e),
"Partition index must be within its allocated space."
);
self.next_entid_to_allocate = e;
}
pub fn allocate_entids(&mut self, n: usize) -> Range<i64> {
let idx = self.next_entid();
self.set_next_entid(idx + n as i64);
idx..self.next_entid()
} }
} }
/// Map partition names to `Partition` instances. /// Map partition names to `Partition` instances.
#[derive(Clone, Debug, Default, Eq, Hash, Ord, PartialOrd, PartialEq)] pub type PartitionMap = BTreeMap<String, Partition>;
#[cfg_attr(feature = "syncable", derive(Serialize, Deserialize))]
pub struct PartitionMap(BTreeMap<String, Partition>);
impl Deref for PartitionMap {
type Target = BTreeMap<String, Partition>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for PartitionMap {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl FromIterator<(String, Partition)> for PartitionMap {
fn from_iter<T: IntoIterator<Item = (String, Partition)>>(iter: T) -> Self {
PartitionMap(iter.into_iter().collect())
}
}
/// Represents the metadata required to query from, or apply transactions to, a Mentat store. /// Represents the metadata required to query from, or apply transactions to, a Mentat store.
/// ///
/// See https://github.com/mozilla/mentat/wiki/Thoughts:-modeling-db-conn-in-Rust. /// See https://github.com/mozilla/mentat/wiki/Thoughts:-modeling-db-conn-in-Rust.
#[derive(Clone, Debug, Default, Eq, Hash, Ord, PartialOrd, PartialEq)] #[derive(Clone,Debug,Default,Eq,Hash,Ord,PartialOrd,PartialEq)]
pub struct DB { pub struct DB {
/// Map partition name->`Partition`. /// Map partition name->`Partition`.
/// ///
@ -127,8 +76,8 @@ pub struct DB {
impl DB { impl DB {
pub fn new(partition_map: PartitionMap, schema: Schema) -> DB { pub fn new(partition_map: PartitionMap, schema: Schema) -> DB {
DB { DB {
partition_map, partition_map: partition_map,
schema, schema: schema
} }
} }
} }
@ -138,9 +87,6 @@ impl DB {
/// Used to represent lookup-refs and [TEMPID a v] upserts as they are resolved. /// Used to represent lookup-refs and [TEMPID a v] upserts as they are resolved.
pub type AVPair = (Entid, TypedValue); pub type AVPair = (Entid, TypedValue);
/// Used to represent assertions and retractions.
pub(crate) type EAV = (Entid, Entid, TypedValue);
/// Map [a v] pairs to existing entids. /// Map [a v] pairs to existing entids.
/// ///
/// Used to resolve lookup-refs and upserts. /// Used to resolve lookup-refs and upserts.
@ -155,8 +101,7 @@ pub type AttributeSet = BTreeSet<Entid>;
pub trait TransactableValue: Clone { pub trait TransactableValue: Clone {
/// Coerce this value place into the given type. This is where we perform schema-aware /// Coerce this value place into the given type. This is where we perform schema-aware
/// coercion, for example coercing an integral value into a ref where appropriate. /// coercion, for example coercing an integral value into a ref where appropriate.
fn into_typed_value(self, schema: &Schema, value_type: ValueType) fn into_typed_value(self, schema: &Schema, value_type: ValueType) -> errors::Result<TypedValue>;
-> errors::Result<TypedValue>;
/// Make an entity place out of this value place. This is where we limit values in nested maps /// Make an entity place out of this value place. This is where we limit values in nested maps
/// to valid entity places. /// to valid entity places.
@ -164,67 +109,3 @@ pub trait TransactableValue: Clone {
fn as_tempid(&self) -> Option<TempId>; fn as_tempid(&self) -> Option<TempId>;
} }
#[cfg(test)]
mod tests {
use super::Partition;
#[test]
#[should_panic(expected = "A partition represents a monotonic increasing sequence of entids.")]
fn test_partition_limits_sanity1() {
Partition::new(100, 1000, 1001, true);
}
#[test]
#[should_panic(expected = "A partition represents a monotonic increasing sequence of entids.")]
fn test_partition_limits_sanity2() {
Partition::new(100, 1000, 99, true);
}
#[test]
#[should_panic(expected = "Partition index must be within its allocated space.")]
fn test_partition_limits_boundary1() {
let mut part = Partition::new(100, 1000, 100, true);
part.set_next_entid(2000);
}
#[test]
#[should_panic(expected = "Partition index must be within its allocated space.")]
fn test_partition_limits_boundary2() {
let mut part = Partition::new(100, 1000, 100, true);
part.set_next_entid(1001);
}
#[test]
#[should_panic(expected = "Partition index must be within its allocated space.")]
fn test_partition_limits_boundary3() {
let mut part = Partition::new(100, 1000, 100, true);
part.set_next_entid(99);
}
#[test]
#[should_panic(expected = "Partition index must be within its allocated space.")]
fn test_partition_limits_boundary4() {
let mut part = Partition::new(100, 1000, 100, true);
part.set_next_entid(-100);
}
#[test]
#[should_panic(expected = "Partition index must be within its allocated space.")]
fn test_partition_limits_boundary5() {
let mut part = Partition::new(100, 1000, 100, true);
part.allocate_entids(901); // One more than allowed.
}
#[test]
fn test_partition_limits_boundary6() {
let mut part = Partition::new(100, 1000, 100, true);
part.set_next_entid(100); // First entid that's allowed.
part.set_next_entid(101); // Just after first.
assert_eq!(101..111, part.allocate_entids(10));
part.set_next_entid(1000); // Last entid that's allowed.
part.set_next_entid(999); // Just before last.
}
}

View file

@ -13,31 +13,49 @@
//! This module implements the upsert resolution algorithm described at //! This module implements the upsert resolution algorithm described at
//! https://github.com/mozilla/mentat/wiki/Transacting:-upsert-resolution-algorithm. //! https://github.com/mozilla/mentat/wiki/Transacting:-upsert-resolution-algorithm.
use std::collections::{BTreeMap, BTreeSet}; use std::collections::{
BTreeMap,
BTreeSet,
};
use indexmap; use indexmap;
use petgraph::unionfind; use petgraph::unionfind;
use crate::internal_types::{ use errors::{
Population, TempIdHandle, TempIdMap, Term, TermWithTempIds, TermWithoutTempIds, TypedValueOr, DbErrorKind,
Result,
};
use types::{
AVPair,
};
use internal_types::{
Population,
TempIdHandle,
TempIdMap,
Term,
TermWithoutTempIds,
TermWithTempIds,
TypedValueOr,
}; };
use crate::types::AVPair;
use db_traits::errors::{DbErrorKind, Result};
use mentat_core::util::Either::*; use mentat_core::util::Either::*;
use core_traits::{attribute, Attribute, Entid, TypedValue}; use mentat_core::{
attribute,
use crate::schema::SchemaBuilding; Attribute,
Entid,
Schema,
TypedValue,
};
use edn::entities::OpType; use edn::entities::OpType;
use mentat_core::Schema; use schema::SchemaBuilding;
/// A "Simple upsert" that looks like [:db/add TEMPID a v], where a is :db.unique/identity. /// A "Simple upsert" that looks like [:db/add TEMPID a v], where a is :db.unique/identity.
#[derive(Clone, Debug, Eq, Hash, Ord, PartialOrd, PartialEq)] #[derive(Clone,Debug,Eq,Hash,Ord,PartialOrd,PartialEq)]
struct UpsertE(TempIdHandle, Entid, TypedValue); struct UpsertE(TempIdHandle, Entid, TypedValue);
/// A "Complex upsert" that looks like [:db/add TEMPID a OTHERID], where a is :db.unique/identity /// A "Complex upsert" that looks like [:db/add TEMPID a OTHERID], where a is :db.unique/identity
#[derive(Clone, Debug, Eq, Hash, Ord, PartialOrd, PartialEq)] #[derive(Clone,Debug,Eq,Hash,Ord,PartialOrd,PartialEq)]
struct UpsertEV(TempIdHandle, Entid, TempIdHandle); struct UpsertEV(TempIdHandle, Entid, TempIdHandle);
/// A generation collects entities into populations at a single evolutionary step in the upsert /// A generation collects entities into populations at a single evolutionary step in the upsert
@ -46,7 +64,7 @@ struct UpsertEV(TempIdHandle, Entid, TempIdHandle);
/// The upsert resolution process is only concerned with [:db/add ...] entities until the final /// The upsert resolution process is only concerned with [:db/add ...] entities until the final
/// entid allocations. That's why we separate into special simple and complex upsert types /// entid allocations. That's why we separate into special simple and complex upsert types
/// immediately, and then collect the more general term types for final resolution. /// immediately, and then collect the more general term types for final resolution.
#[derive(Clone, Debug, Default, Eq, Hash, Ord, PartialOrd, PartialEq)] #[derive(Clone,Debug,Default,Eq,Hash,Ord,PartialOrd,PartialEq)]
pub(crate) struct Generation { pub(crate) struct Generation {
/// "Simple upserts" that look like [:db/add TEMPID a v], where a is :db.unique/identity. /// "Simple upserts" that look like [:db/add TEMPID a v], where a is :db.unique/identity.
upserts_e: Vec<UpsertE>, upserts_e: Vec<UpsertE>,
@ -69,7 +87,7 @@ pub(crate) struct Generation {
resolved: Vec<TermWithoutTempIds>, resolved: Vec<TermWithoutTempIds>,
} }
#[derive(Clone, Debug, Default, Eq, Hash, Ord, PartialOrd, PartialEq)] #[derive(Clone,Debug,Default,Eq,Hash,Ord,PartialOrd,PartialEq)]
pub(crate) struct FinalPopulations { pub(crate) struct FinalPopulations {
/// Upserts that upserted. /// Upserts that upserted.
pub upserted: Vec<TermWithoutTempIds>, pub upserted: Vec<TermWithoutTempIds>,
@ -84,10 +102,7 @@ pub(crate) struct FinalPopulations {
impl Generation { impl Generation {
/// Split entities into a generation of populations that need to evolve to have their tempids /// Split entities into a generation of populations that need to evolve to have their tempids
/// resolved or allocated, and a population of inert entities that do not reference tempids. /// resolved or allocated, and a population of inert entities that do not reference tempids.
pub(crate) fn from<I>(terms: I, schema: &Schema) -> Result<(Generation, Population)> pub(crate) fn from<I>(terms: I, schema: &Schema) -> Result<(Generation, Population)> where I: IntoIterator<Item=TermWithTempIds> {
where
I: IntoIterator<Item = TermWithTempIds>,
{
let mut generation = Generation::default(); let mut generation = Generation::default();
let mut inert = vec![]; let mut inert = vec![];
@ -102,28 +117,22 @@ impl Generation {
if op == OpType::Add && is_unique(a)? { if op == OpType::Add && is_unique(a)? {
generation.upserts_ev.push(UpsertEV(e, a, v)); generation.upserts_ev.push(UpsertEV(e, a, v));
} else { } else {
generation generation.allocations.push(Term::AddOrRetract(op, Right(e), a, Right(v)));
.allocations
.push(Term::AddOrRetract(op, Right(e), a, Right(v)));
} }
} },
Term::AddOrRetract(op, Right(e), a, Left(v)) => { Term::AddOrRetract(op, Right(e), a, Left(v)) => {
if op == OpType::Add && is_unique(a)? { if op == OpType::Add && is_unique(a)? {
generation.upserts_e.push(UpsertE(e, a, v)); generation.upserts_e.push(UpsertE(e, a, v));
} else { } else {
generation generation.allocations.push(Term::AddOrRetract(op, Right(e), a, Left(v)));
.allocations
.push(Term::AddOrRetract(op, Right(e), a, Left(v)));
} }
} },
Term::AddOrRetract(op, Left(e), a, Right(v)) => { Term::AddOrRetract(op, Left(e), a, Right(v)) => {
generation generation.allocations.push(Term::AddOrRetract(op, Left(e), a, Right(v)));
.allocations },
.push(Term::AddOrRetract(op, Left(e), a, Right(v)));
}
Term::AddOrRetract(op, Left(e), a, Left(v)) => { Term::AddOrRetract(op, Left(e), a, Left(v)) => {
inert.push(Term::AddOrRetract(op, Left(e), a, Left(v))); inert.push(Term::AddOrRetract(op, Left(e), a, Left(v)));
} },
} }
} }
@ -152,10 +161,7 @@ impl Generation {
for UpsertE(t, a, v) in self.upserts_e { for UpsertE(t, a, v) in self.upserts_e {
match temp_id_map.get(&*t) { match temp_id_map.get(&*t) {
Some(&n) => next.upserted.push(Term::AddOrRetract(OpType::Add, n, a, v)), Some(&n) => next.upserted.push(Term::AddOrRetract(OpType::Add, n, a, v)),
None => { None => next.allocations.push(Term::AddOrRetract(OpType::Add, Right(t), a, Left(v))),
next.allocations
.push(Term::AddOrRetract(OpType::Add, Right(t), a, Left(v)))
}
} }
} }
@ -166,13 +172,10 @@ impl Generation {
// could conflict. Moving straight to resolved doesn't give us a chance to // could conflict. Moving straight to resolved doesn't give us a chance to
// search the store for the conflict. // search the store for the conflict.
next.upserts_e.push(UpsertE(t1, a, TypedValue::Ref(n2.0))) next.upserts_e.push(UpsertE(t1, a, TypedValue::Ref(n2.0)))
} },
(None, Some(&n2)) => next.upserts_e.push(UpsertE(t1, a, TypedValue::Ref(n2.0))), (None, Some(&n2)) => next.upserts_e.push(UpsertE(t1, a, TypedValue::Ref(n2.0))),
(Some(&n1), None) => { (Some(&n1), None) => next.allocations.push(Term::AddOrRetract(OpType::Add, Left(n1), a, Right(t2))),
next.allocations (None, None) => next.upserts_ev.push(UpsertEV(t1, a, t2))
.push(Term::AddOrRetract(OpType::Add, Left(n1), a, Right(t2)))
}
(None, None) => next.upserts_ev.push(UpsertEV(t1, a, t2)),
} }
} }
@ -184,40 +187,23 @@ impl Generation {
match term { match term {
Term::AddOrRetract(op, Right(t1), a, Right(t2)) => { Term::AddOrRetract(op, Right(t1), a, Right(t2)) => {
match (temp_id_map.get(&*t1), temp_id_map.get(&*t2)) { match (temp_id_map.get(&*t1), temp_id_map.get(&*t2)) {
(Some(&n1), Some(&n2)) => { (Some(&n1), Some(&n2)) => next.resolved.push(Term::AddOrRetract(op, n1, a, TypedValue::Ref(n2.0))),
next.resolved (None, Some(&n2)) => next.allocations.push(Term::AddOrRetract(op, Right(t1), a, Left(TypedValue::Ref(n2.0)))),
.push(Term::AddOrRetract(op, n1, a, TypedValue::Ref(n2.0))) (Some(&n1), None) => next.allocations.push(Term::AddOrRetract(op, Left(n1), a, Right(t2))),
} (None, None) => next.allocations.push(Term::AddOrRetract(op, Right(t1), a, Right(t2))),
(None, Some(&n2)) => next.allocations.push(Term::AddOrRetract(
op,
Right(t1),
a,
Left(TypedValue::Ref(n2.0)),
)),
(Some(&n1), None) => {
next.allocations
.push(Term::AddOrRetract(op, Left(n1), a, Right(t2)))
}
(None, None) => {
next.allocations
.push(Term::AddOrRetract(op, Right(t1), a, Right(t2)))
}
} }
}
Term::AddOrRetract(op, Right(t), a, Left(v)) => match temp_id_map.get(&*t) {
Some(&n) => next.resolved.push(Term::AddOrRetract(op, n, a, v)),
None => next
.allocations
.push(Term::AddOrRetract(op, Right(t), a, Left(v))),
}, },
Term::AddOrRetract(op, Left(e), a, Right(t)) => match temp_id_map.get(&*t) { Term::AddOrRetract(op, Right(t), a, Left(v)) => {
Some(&n) => { match temp_id_map.get(&*t) {
next.resolved Some(&n) => next.resolved.push(Term::AddOrRetract(op, n, a, v)),
.push(Term::AddOrRetract(op, e, a, TypedValue::Ref(n.0))) None => next.allocations.push(Term::AddOrRetract(op, Right(t), a, Left(v))),
}
},
Term::AddOrRetract(op, Left(e), a, Right(t)) => {
match temp_id_map.get(&*t) {
Some(&n) => next.resolved.push(Term::AddOrRetract(op, e, a, TypedValue::Ref(n.0))),
None => next.allocations.push(Term::AddOrRetract(op, Left(e), a, Right(t))),
} }
None => next
.allocations
.push(Term::AddOrRetract(op, Left(e), a, Right(t))),
}, },
Term::AddOrRetract(_, Left(_), _, Left(_)) => unreachable!(), Term::AddOrRetract(_, Left(_), _, Left(_)) => unreachable!(),
} }
@ -227,7 +213,7 @@ impl Generation {
} }
// Collect id->[a v] pairs that might upsert at this evolutionary step. // Collect id->[a v] pairs that might upsert at this evolutionary step.
pub(crate) fn temp_id_avs(&self) -> Vec<(TempIdHandle, AVPair)> { pub(crate) fn temp_id_avs<'a>(&'a self) -> Vec<(TempIdHandle, AVPair)> {
let mut temp_id_avs: Vec<(TempIdHandle, AVPair)> = vec![]; let mut temp_id_avs: Vec<(TempIdHandle, AVPair)> = vec![];
// TODO: map/collect. // TODO: map/collect.
for &UpsertE(ref t, ref a, ref v) in &self.upserts_e { for &UpsertE(ref t, ref a, ref v) in &self.upserts_e {
@ -243,11 +229,7 @@ impl Generation {
let mut upserts_ev = vec![]; let mut upserts_ev = vec![];
::std::mem::swap(&mut self.upserts_ev, &mut upserts_ev); ::std::mem::swap(&mut self.upserts_ev, &mut upserts_ev);
self.allocations.extend( self.allocations.extend(upserts_ev.into_iter().map(|UpsertEV(t1, a, t2)| Term::AddOrRetract(OpType::Add, Right(t1), a, Right(t2))));
upserts_ev.into_iter().map(|UpsertEV(t1, a, t2)| {
Term::AddOrRetract(OpType::Add, Right(t1), a, Right(t2))
}),
);
Ok(()) Ok(())
} }
@ -256,48 +238,38 @@ impl Generation {
/// ///
/// Some of the tempids may be identified, so we also provide a map from tempid to a dense set /// Some of the tempids may be identified, so we also provide a map from tempid to a dense set
/// of contiguous integer labels. /// of contiguous integer labels.
pub(crate) fn temp_ids_in_allocations( pub(crate) fn temp_ids_in_allocations(&self, schema: &Schema) -> Result<BTreeMap<TempIdHandle, usize>> {
&self,
schema: &Schema,
) -> Result<BTreeMap<TempIdHandle, usize>> {
assert!(self.upserts_e.is_empty(), "All upserts should have been upserted, resolved, or moved to the allocated population!"); assert!(self.upserts_e.is_empty(), "All upserts should have been upserted, resolved, or moved to the allocated population!");
assert!(self.upserts_ev.is_empty(), "All upserts should have been upserted, resolved, or moved to the allocated population!"); assert!(self.upserts_ev.is_empty(), "All upserts should have been upserted, resolved, or moved to the allocated population!");
let mut temp_ids: BTreeSet<TempIdHandle> = BTreeSet::default(); let mut temp_ids: BTreeSet<TempIdHandle> = BTreeSet::default();
let mut tempid_avs: BTreeMap<(Entid, TypedValueOr<TempIdHandle>), Vec<TempIdHandle>> = let mut tempid_avs: BTreeMap<(Entid, TypedValueOr<TempIdHandle>), Vec<TempIdHandle>> = BTreeMap::default();
BTreeMap::default();
for term in self.allocations.iter() { for term in self.allocations.iter() {
match term { match term {
Term::AddOrRetract(OpType::Add, Right(ref t1), a, Right(ref t2)) => { &Term::AddOrRetract(OpType::Add, Right(ref t1), a, Right(ref t2)) => {
temp_ids.insert(t1.clone()); temp_ids.insert(t1.clone());
temp_ids.insert(t2.clone()); temp_ids.insert(t2.clone());
let attribute: &Attribute = schema.require_attribute_for_entid(*a)?; let attribute: &Attribute = schema.require_attribute_for_entid(a)?;
if attribute.unique == Some(attribute::Unique::Identity) { if attribute.unique == Some(attribute::Unique::Identity) {
tempid_avs tempid_avs.entry((a, Right(t2.clone()))).or_insert(vec![]).push(t1.clone());
.entry((*a, Right(t2.clone())))
.or_insert_with(Vec::new)
.push(t1.clone());
} }
} },
Term::AddOrRetract(OpType::Add, Right(ref t), a, ref x @ Left(_)) => { &Term::AddOrRetract(OpType::Add, Right(ref t), a, ref x @ Left(_)) => {
temp_ids.insert(t.clone()); temp_ids.insert(t.clone());
let attribute: &Attribute = schema.require_attribute_for_entid(*a)?; let attribute: &Attribute = schema.require_attribute_for_entid(a)?;
if attribute.unique == Some(attribute::Unique::Identity) { if attribute.unique == Some(attribute::Unique::Identity) {
tempid_avs tempid_avs.entry((a, x.clone())).or_insert(vec![]).push(t.clone());
.entry((*a, x.clone()))
.or_insert_with(Vec::new)
.push(t.clone());
} }
} },
Term::AddOrRetract(OpType::Add, Left(_), _, Right(ref t)) => { &Term::AddOrRetract(OpType::Add, Left(_), _, Right(ref t)) => {
temp_ids.insert(t.clone()); temp_ids.insert(t.clone());
} },
Term::AddOrRetract(OpType::Add, Left(_), _, Left(_)) => unreachable!(), &Term::AddOrRetract(OpType::Add, Left(_), _, Left(_)) => unreachable!(),
Term::AddOrRetract(OpType::Retract, _, _, _) => { &Term::AddOrRetract(OpType::Retract, _, _, _) => {
// [:db/retract ...] entities never allocate entids; they have to resolve due to // [:db/retract ...] entities never allocate entids; they have to resolve due to
// other upserts (or they fail the transaction). // other upserts (or they fail the transaction).
} },
} }
} }
@ -307,23 +279,16 @@ impl Generation {
// The union-find implementation from petgraph operates on contiguous indices, so we need to // The union-find implementation from petgraph operates on contiguous indices, so we need to
// maintain the map from our tempids to indices ourselves. // maintain the map from our tempids to indices ourselves.
let temp_ids: BTreeMap<TempIdHandle, usize> = temp_ids let temp_ids: BTreeMap<TempIdHandle, usize> = temp_ids.into_iter().enumerate().map(|(i, tempid)| (tempid, i)).collect();
.into_iter()
.enumerate()
.map(|(i, tempid)| (tempid, i))
.collect();
debug!( debug!("need to label tempids aggregated using tempid_avs {:?}", tempid_avs);
"need to label tempids aggregated using tempid_avs {:?}",
tempid_avs
);
for vs in tempid_avs.values() { for vs in tempid_avs.values() {
if let Some(&first_index) = vs.first().and_then(|first| temp_ids.get(first)) { vs.first().and_then(|first| temp_ids.get(first)).map(|&first_index| {
for tempid in vs { for tempid in vs {
temp_ids.get(tempid).map(|&i| uf.union(first_index, i)); temp_ids.get(tempid).map(|&i| uf.union(first_index, i));
} }
} });
} }
debug!("union-find aggregation {:?}", uf.clone().into_labeling()); debug!("union-find aggregation {:?}", uf.clone().into_labeling());
@ -340,26 +305,17 @@ impl Generation {
for (tempid, tempid_index) in temp_ids { for (tempid, tempid_index) in temp_ids {
let rep = uf.find_mut(tempid_index); let rep = uf.find_mut(tempid_index);
dense_labels.insert(rep); dense_labels.insert(rep);
dense_labels dense_labels.get_full(&rep).map(|(dense_index, _)| tempid_map.insert(tempid.clone(), dense_index));
.get_full(&rep)
.map(|(dense_index, _)| tempid_map.insert(tempid.clone(), dense_index));
} }
debug!( debug!("labeled tempids using {} labels: {:?}", dense_labels.len(), tempid_map);
"labeled tempids using {} labels: {:?}",
dense_labels.len(),
tempid_map
);
Ok(tempid_map) Ok(tempid_map)
} }
/// After evolution is complete, use the provided allocated entids to segment `self` into /// After evolution is complete, use the provided allocated entids to segment `self` into
/// populations, each with no references to tempids. /// populations, each with no references to tempids.
pub(crate) fn into_final_populations( pub(crate) fn into_final_populations(self, temp_id_map: &TempIdMap) -> Result<FinalPopulations> {
self,
temp_id_map: &TempIdMap,
) -> Result<FinalPopulations> {
assert!(self.upserts_e.is_empty()); assert!(self.upserts_e.is_empty());
assert!(self.upserts_ev.is_empty()); assert!(self.upserts_ev.is_empty());
@ -377,27 +333,21 @@ impl Generation {
(OpType::Add, _, _) => unreachable!(), // This is a coding error -- every tempid in a :db/add entity should resolve or be allocated. (OpType::Add, _, _) => unreachable!(), // This is a coding error -- every tempid in a :db/add entity should resolve or be allocated.
(OpType::Retract, _, _) => bail!(DbErrorKind::NotYetImplemented(format!("[:db/retract ...] entity referenced tempid that did not upsert: one of {}, {}", t1, t2))), (OpType::Retract, _, _) => bail!(DbErrorKind::NotYetImplemented(format!("[:db/retract ...] entity referenced tempid that did not upsert: one of {}, {}", t1, t2))),
} }
} },
Term::AddOrRetract(op, Right(t), a, Left(v)) => { Term::AddOrRetract(op, Right(t), a, Left(v)) => {
match (op, temp_id_map.get(&*t)) { match (op, temp_id_map.get(&*t)) {
(op, Some(&n)) => Term::AddOrRetract(op, n, a, v), (op, Some(&n)) => Term::AddOrRetract(op, n, a, v),
(OpType::Add, _) => unreachable!(), // This is a coding error. (OpType::Add, _) => unreachable!(), // This is a coding error.
(OpType::Retract, _) => bail!(DbErrorKind::NotYetImplemented(format!( (OpType::Retract, _) => bail!(DbErrorKind::NotYetImplemented(format!("[:db/retract ...] entity referenced tempid that did not upsert: {}", t))),
"[:db/retract ...] entity referenced tempid that did not upsert: {}",
t
))),
} }
} },
Term::AddOrRetract(op, Left(e), a, Right(t)) => { Term::AddOrRetract(op, Left(e), a, Right(t)) => {
match (op, temp_id_map.get(&*t)) { match (op, temp_id_map.get(&*t)) {
(op, Some(&n)) => Term::AddOrRetract(op, e, a, TypedValue::Ref(n.0)), (op, Some(&n)) => Term::AddOrRetract(op, e, a, TypedValue::Ref(n.0)),
(OpType::Add, _) => unreachable!(), // This is a coding error. (OpType::Add, _) => unreachable!(), // This is a coding error.
(OpType::Retract, _) => bail!(DbErrorKind::NotYetImplemented(format!( (OpType::Retract, _) => bail!(DbErrorKind::NotYetImplemented(format!("[:db/retract ...] entity referenced tempid that did not upsert: {}", t))),
"[:db/retract ...] entity referenced tempid that did not upsert: {}",
t
))),
} }
} },
Term::AddOrRetract(_, Left(_), _, Left(_)) => unreachable!(), // This is a coding error -- these should not be in allocations. Term::AddOrRetract(_, Left(_), _, Left(_)) => unreachable!(), // This is a coding error -- these should not be in allocations.
}; };
populations.allocated.push(allocated); populations.allocated.push(allocated);

View file

@ -17,13 +17,19 @@
// - When observers are registered we want to flip some flags as writes occur so that we can // - When observers are registered we want to flip some flags as writes occur so that we can
// notifying them outside the transaction. // notifying them outside the transaction.
use core_traits::{Entid, TypedValue}; use mentat_core::{
Entid,
Schema,
TypedValue,
};
use mentat_core::Schema; use edn::entities::{
OpType,
};
use edn::entities::OpType; use errors::{
Result,
use db_traits::errors::Result; };
pub trait TransactWatcher { pub trait TransactWatcher {
fn datom(&mut self, op: OpType, e: Entid, a: Entid, v: &TypedValue); fn datom(&mut self, op: OpType, e: Entid, a: Entid, v: &TypedValue);
@ -38,7 +44,8 @@ pub trait TransactWatcher {
pub struct NullWatcher(); pub struct NullWatcher();
impl TransactWatcher for NullWatcher { impl TransactWatcher for NullWatcher {
fn datom(&mut self, _op: OpType, _e: Entid, _a: Entid, _v: &TypedValue) {} fn datom(&mut self, _op: OpType, _e: Entid, _a: Entid, _v: &TypedValue) {
}
fn done(&mut self, _t: &Entid, _schema: &Schema) -> Result<()> { fn done(&mut self, _t: &Entid, _schema: &Schema) -> Result<()> {
Ok(()) Ok(())

View file

@ -8,8 +8,8 @@
// CONDITIONS OF ANY KIND, either express or implied. See the License for the // CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License. // specific language governing permissions and limitations under the License.
extern crate core_traits;
extern crate edn; extern crate edn;
extern crate mentat_core;
extern crate mentat_db; extern crate mentat_db;
extern crate ordered_float; extern crate ordered_float;
extern crate rusqlite; extern crate rusqlite;
@ -18,105 +18,41 @@ use ordered_float::OrderedFloat;
use edn::symbols; use edn::symbols;
use core_traits::{TypedValue, ValueType}; use mentat_core::{TypedValue, ValueType};
use mentat_db::db::TypedSQLValue; use mentat_db::db::TypedSQLValue;
// It's not possible to test to_sql_value_pair since rusqlite::ToSqlOutput doesn't implement // It's not possible to test to_sql_value_pair since rusqlite::ToSqlOutput doesn't implement
// PartialEq. // PartialEq.
#[test] #[test]
fn test_from_sql_value_pair() { fn test_from_sql_value_pair() {
assert_eq!( assert_eq!(TypedValue::from_sql_value_pair(rusqlite::types::Value::Integer(1234), 0).unwrap(), TypedValue::Ref(1234));
TypedValue::from_sql_value_pair(rusqlite::types::Value::Integer(1234), 0).unwrap(),
TypedValue::Ref(1234)
);
assert_eq!( assert_eq!(TypedValue::from_sql_value_pair(rusqlite::types::Value::Integer(0), 1).unwrap(), TypedValue::Boolean(false));
TypedValue::from_sql_value_pair(rusqlite::types::Value::Integer(0), 1).unwrap(), assert_eq!(TypedValue::from_sql_value_pair(rusqlite::types::Value::Integer(1), 1).unwrap(), TypedValue::Boolean(true));
TypedValue::Boolean(false)
);
assert_eq!(
TypedValue::from_sql_value_pair(rusqlite::types::Value::Integer(1), 1).unwrap(),
TypedValue::Boolean(true)
);
assert_eq!( assert_eq!(TypedValue::from_sql_value_pair(rusqlite::types::Value::Integer(0), 5).unwrap(), TypedValue::Long(0));
TypedValue::from_sql_value_pair(rusqlite::types::Value::Integer(0), 5).unwrap(), assert_eq!(TypedValue::from_sql_value_pair(rusqlite::types::Value::Integer(1234), 5).unwrap(), TypedValue::Long(1234));
TypedValue::Long(0)
);
assert_eq!(
TypedValue::from_sql_value_pair(rusqlite::types::Value::Integer(1234), 5).unwrap(),
TypedValue::Long(1234)
);
assert_eq!( assert_eq!(TypedValue::from_sql_value_pair(rusqlite::types::Value::Real(0.0), 5).unwrap(), TypedValue::Double(OrderedFloat(0.0)));
TypedValue::from_sql_value_pair(rusqlite::types::Value::Real(0.0), 5).unwrap(), assert_eq!(TypedValue::from_sql_value_pair(rusqlite::types::Value::Real(0.5), 5).unwrap(), TypedValue::Double(OrderedFloat(0.5)));
TypedValue::Double(OrderedFloat(0.0))
);
assert_eq!(
TypedValue::from_sql_value_pair(rusqlite::types::Value::Real(0.5), 5).unwrap(),
TypedValue::Double(OrderedFloat(0.5))
);
assert_eq!( assert_eq!(TypedValue::from_sql_value_pair(rusqlite::types::Value::Text(":db/keyword".into()), 10).unwrap(), TypedValue::typed_string(":db/keyword"));
TypedValue::from_sql_value_pair(rusqlite::types::Value::Text(":db/keyword".into()), 10) assert_eq!(TypedValue::from_sql_value_pair(rusqlite::types::Value::Text(":db/keyword".into()), 13).unwrap(), TypedValue::typed_ns_keyword("db", "keyword"));
.unwrap(),
TypedValue::typed_string(":db/keyword")
);
assert_eq!(
TypedValue::from_sql_value_pair(rusqlite::types::Value::Text(":db/keyword".into()), 13)
.unwrap(),
TypedValue::typed_ns_keyword("db", "keyword")
);
assert_eq!(
TypedValue::from_sql_value_pair(rusqlite::types::Value::Blob(vec![1, 2, 3, 42]), 15)
.unwrap(),
TypedValue::Bytes((vec![1, 2, 3, 42]).into())
);
} }
#[test] #[test]
fn test_to_edn_value_pair() { fn test_to_edn_value_pair() {
assert_eq!( assert_eq!(TypedValue::Ref(1234).to_edn_value_pair(), (edn::Value::Integer(1234), ValueType::Ref));
TypedValue::Ref(1234).to_edn_value_pair(),
(edn::Value::Integer(1234), ValueType::Ref)
);
assert_eq!( assert_eq!(TypedValue::Boolean(false).to_edn_value_pair(), (edn::Value::Boolean(false), ValueType::Boolean));
TypedValue::Boolean(false).to_edn_value_pair(), assert_eq!(TypedValue::Boolean(true).to_edn_value_pair(), (edn::Value::Boolean(true), ValueType::Boolean));
(edn::Value::Boolean(false), ValueType::Boolean)
);
assert_eq!(
TypedValue::Boolean(true).to_edn_value_pair(),
(edn::Value::Boolean(true), ValueType::Boolean)
);
assert_eq!( assert_eq!(TypedValue::Long(0).to_edn_value_pair(), (edn::Value::Integer(0), ValueType::Long));
TypedValue::Long(0).to_edn_value_pair(), assert_eq!(TypedValue::Long(1234).to_edn_value_pair(), (edn::Value::Integer(1234), ValueType::Long));
(edn::Value::Integer(0), ValueType::Long)
);
assert_eq!(
TypedValue::Long(1234).to_edn_value_pair(),
(edn::Value::Integer(1234), ValueType::Long)
);
assert_eq!( assert_eq!(TypedValue::Double(OrderedFloat(0.0)).to_edn_value_pair(), (edn::Value::Float(OrderedFloat(0.0)), ValueType::Double));
TypedValue::Double(OrderedFloat(0.0)).to_edn_value_pair(), assert_eq!(TypedValue::Double(OrderedFloat(0.5)).to_edn_value_pair(), (edn::Value::Float(OrderedFloat(0.5)), ValueType::Double));
(edn::Value::Float(OrderedFloat(0.0)), ValueType::Double)
);
assert_eq!(
TypedValue::Double(OrderedFloat(0.5)).to_edn_value_pair(),
(edn::Value::Float(OrderedFloat(0.5)), ValueType::Double)
);
assert_eq!( assert_eq!(TypedValue::typed_string(":db/keyword").to_edn_value_pair(), (edn::Value::Text(":db/keyword".into()), ValueType::String));
TypedValue::typed_string(":db/keyword").to_edn_value_pair(), assert_eq!(TypedValue::typed_ns_keyword("db", "keyword").to_edn_value_pair(), (edn::Value::Keyword(symbols::Keyword::namespaced("db", "keyword")), ValueType::Keyword));
(edn::Value::Text(":db/keyword".into()), ValueType::String)
);
assert_eq!(
TypedValue::typed_ns_keyword("db", "keyword").to_edn_value_pair(),
(
edn::Value::Keyword(symbols::Keyword::namespaced("db", "keyword")),
ValueType::Keyword
)
);
} }

View file

@ -11,7 +11,7 @@ source "https://rubygems.org"
# gem "jekyll", "~> 3.7.3" # gem "jekyll", "~> 3.7.3"
# This is the default theme for new Jekyll sites. You may change this to anything you like. # This is the default theme for new Jekyll sites. You may change this to anything you like.
gem "minima", "~> 2.5.1" gem "minima", "~> 2.0"
# If you want to use GitHub Pages, remove the "gem "jekyll"" above and # If you want to use GitHub Pages, remove the "gem "jekyll"" above and
# uncomment the line below. To upgrade, run `bundle update github-pages`. # uncomment the line below. To upgrade, run `bundle update github-pages`.
@ -19,9 +19,9 @@ gem "minima", "~> 2.5.1"
# If you have any plugins, put them here! # If you have any plugins, put them here!
group :jekyll_plugins do group :jekyll_plugins do
gem "jekyll-feed", "~> 0.15.1" gem "jekyll-feed", "~> 0.9.3"
gem "github-pages", "~> 215" gem "github-pages", "~> 186"
gem "jekyll-commonmark-ghpages", "~> 0.1.6" gem "jekyll-commonmark-ghpages", "~> 0.1.5"
end end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem # Windows does not include zoneinfo files, so bundle the tzinfo-data gem

View file

@ -1,161 +1,148 @@
GEM GEM
remote: https://rubygems.org/ remote: https://rubygems.org/
specs: specs:
activesupport (6.0.4) activesupport (4.2.10)
concurrent-ruby (~> 1.0, >= 1.0.2) i18n (~> 0.7)
i18n (>= 0.7, < 2)
minitest (~> 5.1) minitest (~> 5.1)
thread_safe (~> 0.3, >= 0.3.4)
tzinfo (~> 1.1) tzinfo (~> 1.1)
zeitwerk (~> 2.2, >= 2.2.2) addressable (2.5.2)
addressable (2.8.0) public_suffix (>= 2.0.2, < 4.0)
public_suffix (>= 2.0.2, < 5.0)
coffee-script (2.4.1) coffee-script (2.4.1)
coffee-script-source coffee-script-source
execjs execjs
coffee-script-source (1.11.1) coffee-script-source (1.11.1)
colorator (1.1.0) colorator (1.1.0)
commonmarker (0.17.13) commonmarker (0.17.9)
ruby-enum (~> 0.5) ruby-enum (~> 0.5)
concurrent-ruby (1.1.9) concurrent-ruby (1.0.5)
dnsruby (1.61.7) dnsruby (1.60.2)
simpleidn (~> 0.1) em-websocket (0.5.1)
em-websocket (0.5.2)
eventmachine (>= 0.12.9) eventmachine (>= 0.12.9)
http_parser.rb (~> 0.6.0) http_parser.rb (~> 0.6.0)
ethon (0.14.0) ethon (0.11.0)
ffi (>= 1.15.0) ffi (>= 1.3.0)
eventmachine (1.2.7) eventmachine (1.2.7)
execjs (2.8.1) execjs (2.7.0)
faraday (1.4.3) faraday (0.15.2)
faraday-em_http (~> 1.0)
faraday-em_synchrony (~> 1.0)
faraday-excon (~> 1.1)
faraday-net_http (~> 1.0)
faraday-net_http_persistent (~> 1.1)
multipart-post (>= 1.2, < 3) multipart-post (>= 1.2, < 3)
ruby2_keywords (>= 0.0.4) ffi (1.9.25)
faraday-em_http (1.0.0)
faraday-em_synchrony (1.0.0)
faraday-excon (1.1.0)
faraday-net_http (1.0.1)
faraday-net_http_persistent (1.1.0)
ffi (1.15.3)
forwardable-extended (2.6.0) forwardable-extended (2.6.0)
gemoji (3.0.1) gemoji (3.0.0)
github-pages (215) github-pages (186)
github-pages-health-check (= 1.17.2) activesupport (= 4.2.10)
jekyll (= 3.9.0) github-pages-health-check (= 1.8.1)
jekyll-avatar (= 0.7.0) jekyll (= 3.7.3)
jekyll-avatar (= 0.5.0)
jekyll-coffeescript (= 1.1.1) jekyll-coffeescript (= 1.1.1)
jekyll-commonmark-ghpages (= 0.1.6) jekyll-commonmark-ghpages (= 0.1.5)
jekyll-default-layout (= 0.1.4) jekyll-default-layout (= 0.1.4)
jekyll-feed (= 0.15.1) jekyll-feed (= 0.9.3)
jekyll-gist (= 1.5.0) jekyll-gist (= 1.5.0)
jekyll-github-metadata (= 2.13.0) jekyll-github-metadata (= 2.9.4)
jekyll-mentions (= 1.6.0) jekyll-mentions (= 1.3.0)
jekyll-optional-front-matter (= 0.3.2) jekyll-optional-front-matter (= 0.3.0)
jekyll-paginate (= 1.1.0) jekyll-paginate (= 1.1.0)
jekyll-readme-index (= 0.3.0) jekyll-readme-index (= 0.2.0)
jekyll-redirect-from (= 0.16.0) jekyll-redirect-from (= 0.13.0)
jekyll-relative-links (= 0.6.1) jekyll-relative-links (= 0.5.3)
jekyll-remote-theme (= 0.4.3) jekyll-remote-theme (= 0.3.1)
jekyll-sass-converter (= 1.5.2) jekyll-sass-converter (= 1.5.2)
jekyll-seo-tag (= 2.7.1) jekyll-seo-tag (= 2.4.0)
jekyll-sitemap (= 1.4.0) jekyll-sitemap (= 1.2.0)
jekyll-swiss (= 1.0.0) jekyll-swiss (= 0.4.0)
jekyll-theme-architect (= 0.1.1) jekyll-theme-architect (= 0.1.1)
jekyll-theme-cayman (= 0.1.1) jekyll-theme-cayman (= 0.1.1)
jekyll-theme-dinky (= 0.1.1) jekyll-theme-dinky (= 0.1.1)
jekyll-theme-hacker (= 0.1.2) jekyll-theme-hacker (= 0.1.1)
jekyll-theme-leap-day (= 0.1.1) jekyll-theme-leap-day (= 0.1.1)
jekyll-theme-merlot (= 0.1.1) jekyll-theme-merlot (= 0.1.1)
jekyll-theme-midnight (= 0.1.1) jekyll-theme-midnight (= 0.1.1)
jekyll-theme-minimal (= 0.1.1) jekyll-theme-minimal (= 0.1.1)
jekyll-theme-modernist (= 0.1.1) jekyll-theme-modernist (= 0.1.1)
jekyll-theme-primer (= 0.5.4) jekyll-theme-primer (= 0.5.3)
jekyll-theme-slate (= 0.1.1) jekyll-theme-slate (= 0.1.1)
jekyll-theme-tactile (= 0.1.1) jekyll-theme-tactile (= 0.1.1)
jekyll-theme-time-machine (= 0.1.1) jekyll-theme-time-machine (= 0.1.1)
jekyll-titles-from-headings (= 0.5.3) jekyll-titles-from-headings (= 0.5.1)
jemoji (= 0.12.0) jemoji (= 0.9.0)
kramdown (= 2.3.1) kramdown (= 1.16.2)
kramdown-parser-gfm (= 1.1.0) liquid (= 4.0.0)
liquid (= 4.0.3) listen (= 3.1.5)
mercenary (~> 0.3) mercenary (~> 0.3)
minima (= 2.5.1) minima (= 2.4.1)
nokogiri (>= 1.10.4, < 2.0) nokogiri (>= 1.8.2, < 2.0)
rouge (= 3.26.0) rouge (= 2.2.1)
terminal-table (~> 1.4) terminal-table (~> 1.4)
github-pages-health-check (1.17.2) github-pages-health-check (1.8.1)
addressable (~> 2.3) addressable (~> 2.3)
dnsruby (~> 1.60) dnsruby (~> 1.60)
octokit (~> 4.0) octokit (~> 4.0)
public_suffix (>= 2.0.2, < 5.0) public_suffix (~> 2.0)
typhoeus (~> 1.3) typhoeus (~> 1.3)
html-pipeline (2.14.0) html-pipeline (2.8.0)
activesupport (>= 2) activesupport (>= 2)
nokogiri (>= 1.4) nokogiri (>= 1.4)
http_parser.rb (0.6.0) http_parser.rb (0.6.0)
i18n (0.9.5) i18n (0.9.5)
concurrent-ruby (~> 1.0) concurrent-ruby (~> 1.0)
jekyll (3.9.0) jekyll (3.7.3)
addressable (~> 2.4) addressable (~> 2.4)
colorator (~> 1.0) colorator (~> 1.0)
em-websocket (~> 0.5) em-websocket (~> 0.5)
i18n (~> 0.7) i18n (~> 0.7)
jekyll-sass-converter (~> 1.0) jekyll-sass-converter (~> 1.0)
jekyll-watch (~> 2.0) jekyll-watch (~> 2.0)
kramdown (>= 1.17, < 3) kramdown (~> 1.14)
liquid (~> 4.0) liquid (~> 4.0)
mercenary (~> 0.3.3) mercenary (~> 0.3.3)
pathutil (~> 0.9) pathutil (~> 0.9)
rouge (>= 1.7, < 4) rouge (>= 1.7, < 4)
safe_yaml (~> 1.0) safe_yaml (~> 1.0)
jekyll-avatar (0.7.0) jekyll-avatar (0.5.0)
jekyll (>= 3.0, < 5.0) jekyll (~> 3.0)
jekyll-coffeescript (1.1.1) jekyll-coffeescript (1.1.1)
coffee-script (~> 2.2) coffee-script (~> 2.2)
coffee-script-source (~> 1.11.1) coffee-script-source (~> 1.11.1)
jekyll-commonmark (1.3.1) jekyll-commonmark (1.2.0)
commonmarker (~> 0.14) commonmarker (~> 0.14)
jekyll (>= 3.7, < 5.0) jekyll (>= 3.0, < 4.0)
jekyll-commonmark-ghpages (0.1.6) jekyll-commonmark-ghpages (0.1.5)
commonmarker (~> 0.17.6) commonmarker (~> 0.17.6)
jekyll-commonmark (~> 1.2) jekyll-commonmark (~> 1)
rouge (>= 2.0, < 4.0) rouge (~> 2)
jekyll-default-layout (0.1.4) jekyll-default-layout (0.1.4)
jekyll (~> 3.0) jekyll (~> 3.0)
jekyll-feed (0.15.1) jekyll-feed (0.9.3)
jekyll (>= 3.7, < 5.0) jekyll (~> 3.3)
jekyll-gist (1.5.0) jekyll-gist (1.5.0)
octokit (~> 4.2) octokit (~> 4.2)
jekyll-github-metadata (2.13.0) jekyll-github-metadata (2.9.4)
jekyll (>= 3.4, < 5.0) jekyll (~> 3.1)
octokit (~> 4.0, != 4.4.0) octokit (~> 4.0, != 4.4.0)
jekyll-mentions (1.6.0) jekyll-mentions (1.3.0)
activesupport (~> 4.0)
html-pipeline (~> 2.3) html-pipeline (~> 2.3)
jekyll (>= 3.7, < 5.0) jekyll (~> 3.0)
jekyll-optional-front-matter (0.3.2) jekyll-optional-front-matter (0.3.0)
jekyll (>= 3.0, < 5.0) jekyll (~> 3.0)
jekyll-paginate (1.1.0) jekyll-paginate (1.1.0)
jekyll-readme-index (0.3.0) jekyll-readme-index (0.2.0)
jekyll (>= 3.0, < 5.0) jekyll (~> 3.0)
jekyll-redirect-from (0.16.0) jekyll-redirect-from (0.13.0)
jekyll (>= 3.3, < 5.0) jekyll (~> 3.3)
jekyll-relative-links (0.6.1) jekyll-relative-links (0.5.3)
jekyll (>= 3.3, < 5.0) jekyll (~> 3.3)
jekyll-remote-theme (0.4.3) jekyll-remote-theme (0.3.1)
addressable (~> 2.0) jekyll (~> 3.5)
jekyll (>= 3.5, < 5.0) rubyzip (>= 1.2.1, < 3.0)
jekyll-sass-converter (>= 1.0, <= 3.0.0, != 2.0.0)
rubyzip (>= 1.3.0, < 3.0)
jekyll-sass-converter (1.5.2) jekyll-sass-converter (1.5.2)
sass (~> 3.4) sass (~> 3.4)
jekyll-seo-tag (2.7.1) jekyll-seo-tag (2.4.0)
jekyll (>= 3.8, < 5.0) jekyll (~> 3.3)
jekyll-sitemap (1.4.0) jekyll-sitemap (1.2.0)
jekyll (>= 3.7, < 5.0) jekyll (~> 3.3)
jekyll-swiss (1.0.0) jekyll-swiss (0.4.0)
jekyll-theme-architect (0.1.1) jekyll-theme-architect (0.1.1)
jekyll (~> 3.5) jekyll (~> 3.5)
jekyll-seo-tag (~> 2.0) jekyll-seo-tag (~> 2.0)
@ -165,8 +152,8 @@ GEM
jekyll-theme-dinky (0.1.1) jekyll-theme-dinky (0.1.1)
jekyll (~> 3.5) jekyll (~> 3.5)
jekyll-seo-tag (~> 2.0) jekyll-seo-tag (~> 2.0)
jekyll-theme-hacker (0.1.2) jekyll-theme-hacker (0.1.1)
jekyll (> 3.5, < 5.0) jekyll (~> 3.5)
jekyll-seo-tag (~> 2.0) jekyll-seo-tag (~> 2.0)
jekyll-theme-leap-day (0.1.1) jekyll-theme-leap-day (0.1.1)
jekyll (~> 3.5) jekyll (~> 3.5)
@ -183,8 +170,8 @@ GEM
jekyll-theme-modernist (0.1.1) jekyll-theme-modernist (0.1.1)
jekyll (~> 3.5) jekyll (~> 3.5)
jekyll-seo-tag (~> 2.0) jekyll-seo-tag (~> 2.0)
jekyll-theme-primer (0.5.4) jekyll-theme-primer (0.5.3)
jekyll (> 3.5, < 5.0) jekyll (~> 3.5)
jekyll-github-metadata (~> 2.9) jekyll-github-metadata (~> 2.9)
jekyll-seo-tag (~> 2.0) jekyll-seo-tag (~> 2.0)
jekyll-theme-slate (0.1.1) jekyll-theme-slate (0.1.1)
@ -196,82 +183,71 @@ GEM
jekyll-theme-time-machine (0.1.1) jekyll-theme-time-machine (0.1.1)
jekyll (~> 3.5) jekyll (~> 3.5)
jekyll-seo-tag (~> 2.0) jekyll-seo-tag (~> 2.0)
jekyll-titles-from-headings (0.5.3) jekyll-titles-from-headings (0.5.1)
jekyll (>= 3.3, < 5.0) jekyll (~> 3.3)
jekyll-watch (2.2.1) jekyll-watch (2.0.0)
listen (~> 3.0) listen (~> 3.0)
jemoji (0.12.0) jemoji (0.9.0)
activesupport (~> 4.0, >= 4.2.9)
gemoji (~> 3.0) gemoji (~> 3.0)
html-pipeline (~> 2.2) html-pipeline (~> 2.2)
jekyll (>= 3.0, < 5.0) jekyll (~> 3.0)
kramdown (2.3.1) kramdown (1.16.2)
rexml liquid (4.0.0)
kramdown-parser-gfm (1.1.0) listen (3.1.5)
kramdown (~> 2.0) rb-fsevent (~> 0.9, >= 0.9.4)
liquid (4.0.3) rb-inotify (~> 0.9, >= 0.9.7)
listen (3.5.1) ruby_dep (~> 1.2)
rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10)
mercenary (0.3.6) mercenary (0.3.6)
mini_portile2 (2.6.1) mini_portile2 (2.3.0)
minima (2.5.1) minima (2.4.1)
jekyll (>= 3.5, < 5.0) jekyll (~> 3.5)
jekyll-feed (~> 0.9) jekyll-feed (~> 0.9)
jekyll-seo-tag (~> 2.1) jekyll-seo-tag (~> 2.1)
minitest (5.14.4) minitest (5.11.3)
multipart-post (2.1.1) multipart-post (2.0.0)
nokogiri (1.12.5) nokogiri (1.8.3)
mini_portile2 (~> 2.6.1) mini_portile2 (~> 2.3.0)
racc (~> 1.4) octokit (4.9.0)
octokit (4.21.0)
faraday (>= 0.9)
sawyer (~> 0.8.0, >= 0.5.3) sawyer (~> 0.8.0, >= 0.5.3)
pathutil (0.16.2) pathutil (0.16.1)
forwardable-extended (~> 2.6) forwardable-extended (~> 2.6)
public_suffix (4.0.6) public_suffix (2.0.5)
racc (1.5.2) rb-fsevent (0.10.3)
rb-fsevent (0.11.0) rb-inotify (0.9.10)
rb-inotify (0.10.1) ffi (>= 0.5.0, < 2)
ffi (~> 1.0) rouge (2.2.1)
rexml (3.2.5) ruby-enum (0.7.2)
rouge (3.26.0)
ruby-enum (0.9.0)
i18n i18n
ruby2_keywords (0.0.4) ruby_dep (1.5.0)
rubyzip (2.3.0) rubyzip (1.2.1)
safe_yaml (1.0.5) safe_yaml (1.0.4)
sass (3.7.4) sass (3.5.6)
sass-listen (~> 4.0.0) sass-listen (~> 4.0.0)
sass-listen (4.0.0) sass-listen (4.0.0)
rb-fsevent (~> 0.9, >= 0.9.4) rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7) rb-inotify (~> 0.9, >= 0.9.7)
sawyer (0.8.2) sawyer (0.8.1)
addressable (>= 2.3.5) addressable (>= 2.3.5, < 2.6)
faraday (> 0.8, < 2.0) faraday (~> 0.8, < 1.0)
simpleidn (0.2.1)
unf (~> 0.1.4)
terminal-table (1.8.0) terminal-table (1.8.0)
unicode-display_width (~> 1.1, >= 1.1.1) unicode-display_width (~> 1.1, >= 1.1.1)
thread_safe (0.3.6) thread_safe (0.3.6)
typhoeus (1.4.0) typhoeus (1.3.0)
ethon (>= 0.9.0) ethon (>= 0.9.0)
tzinfo (1.2.9) tzinfo (1.2.5)
thread_safe (~> 0.1) thread_safe (~> 0.1)
unf (0.1.4) unicode-display_width (1.4.0)
unf_ext
unf_ext (0.0.7.7)
unicode-display_width (1.7.0)
zeitwerk (2.4.2)
PLATFORMS PLATFORMS
ruby ruby
DEPENDENCIES DEPENDENCIES
github-pages (~> 215) github-pages (~> 186)
jekyll-commonmark-ghpages (~> 0.1.6) jekyll-commonmark-ghpages (~> 0.1.5)
jekyll-feed (~> 0.15.1) jekyll-feed (~> 0.9.3)
minima (~> 2.5.1) minima (~> 2.0)
tzinfo-data tzinfo-data
BUNDLED WITH BUNDLED WITH
2.2.21 1.16.2

View file

@ -1,177 +0,0 @@
# Introduction
Mentat is a transactional, relational storage system built on top of SQLite. The abstractions it offers allow you to easily tackle some things that are tricky in other storage systems:
- Have multiple components share storage and collaborate.
- Evolve schema.
- Track change over time.
- Synchronize data correctly.
- Store data with rich, checked types.
Mentat offers a programmatic Rust API for managing stores, retrieving data, and _transacting_ new data. It offers a Datalog-based query engine, with queries expressed in EDN, a rich textual data format similar to JSON. And it offers an EDN data format for transacting new data.
This tutorial covers all of these APIs, along with defining vocabulary.
We'll begin by introducing some concepts, and then we'll walk through some examples.
## What does Mentat store?
Depending on your perspective, Mentat looks like a relational store, a graph store, or a tuple store.
Mentat stores relationships between _entities_ and other entities or _values_. An entity is related to other things by an _attribute_.
All entities have an _entity ID_ (abbreviated to _entid_).
Some entities additionally have an identifier called an _ident_, which is a keyword. That looks something like `:bookmark/title`.
A value is a primitive piece of data. Mentat supports the following:
* Strings
* Long integers
* Double-precision floating point numbers
* Millisecond-precision timestamps
* UUIDs
* Booleans
* Keywords (a special kind of string that we use for idents).
There are two special kinds of entities: _attributes_ and _transactions_.
Attributes are themselves entities with a particular set of properties that define their meaning. They have identifiers, so you can refer to them easily. They have a _value type_, which is the type of value Mentat expects to be on the right hand side of the relationship. And they have a _cardinality_ (whether one or many values can exist for a particular entity), whether values are _unique_, a documentation string, and some indexing options.
An attribute looks something like this:
```edn
{:db/ident :bookmark/title
:db/cardinality :db.cardinality/one
:db/valueType :db.type/string
:db/fulltext true
:db/doc "The title of a bookmark."}
```
Transactions are special entities that can be described however you wish. By default they track the timestamp at which they were written.
The relationship between an entity, an attribute, and a value, occurring in a _transaction_ (which is just another kind of entity!) — a tuple of five values — is called a _datom_.
A single datom might look something like this:
```
[:db/add 65536 :bookmark/title "mozilla.org" 268435456]
^ ^ ^ ^ ^
\ Add or retract. | | | |
\ The entity. | | |
\ The attribute. | |
\ The value, a string. |
\ The transaction ID.
```
which is equivalent to saying "in transaction 268435456 we assert that entity 65536 is a bookmark with the title 'mozilla.org'".
When we transact that — which means to add it as a fact to the store — Mentat also describes the transaction itself on our behalf:
```edn
[:db/add 268435456 :db/txInstant "2018-01-25 20:07:04.408358 UTC" 268435456]
```
# A simple app
Let's get started with some Rust code.
First, the imports we'll need. The comments here briefly explain what each thing is.
```rust
// So you can define keywords with neater syntax.
#[macro_use(kw)]
extern crate mentat;
use mentat::{
Store, // A single database connection and in-memory metadata.
}
use mentat::vocabulary::attribute; // Properties of attributes.
```
## Defining a simple vocabulary
All data in Mentat — even the terms we used above, like `:db/cardinality` — are defined in the store itself. So that's where we start. In Rust, we define a _vocabulary definition_, and ask the store to ensure that it exists.
```rust
fn set_up(mut store: Store) -> Result<()> {
// Start a write transaction.
let mut in_progress = store.begin_transaction()?;
// Make sure the core vocabulary exists. This is good practice if a user,
// an external tool, or another component might have touched the file
// since you last wrote it.
in_progress.verify_core_schema()?;
// Make sure our vocabulary is installed, and install if necessary.
// This defines some attributes that we can use to describe people.
in_progress.ensure_vocabulary(&Definition {
name: kw!(:example/people),
version: 1,
attributes: vec![
(kw!(:person/name),
vocabulary::AttributeBuilder::default()
.value_type(ValueType::String)
.multival(true)
.build()),
(kw!(:person/age),
vocabulary::AttributeBuilder::default()
.value_type(ValueType::Long)
.multival(false)
.build()),
(kw!(:person/email),
vocabulary::AttributeBuilder::default()
.value_type(ValueType::String)
.multival(true)
.unique(attribute::Unique::Identity)
.build()),
],
})?;
in_progress.commit()?;
Ok(())
}
```
We open a store and configure its vocabulary like this:
```rust
let path = "/path/to/file.db";
let store = Store::open(path)?;
set_up(store)?;
```
If this code returns successfully, we're good to go.
## Transactions
You'll see in our `set_up` function that we begin and end a transaction, which we call `in_progress`. A read-only transaction is begun via `begin_read`. The resulting objects — `InProgress` and `InProgressRead` support various kinds of read and write operations. Transactions are automatically rolled back when dropped, so remember to call `commit`!
## Adding some data
There are two ways to add data to Mentat: programmatically or textually.
The textual form accepts EDN, a simple relative of JSON that supports richer types and more flexible syntax. You saw this in the introduction. Here's an example:
```rust
in_progress.transact(r#"[
{:person/name "Alice"
:person/age 32
:person/email "alice@example.org"}
]"#)?;
```
You can implicitly _upsert_ data when you have a unique attribute to use:
```rust
// Alice's age is now 33. Note that we don't need to find out an entid,
// nor explicitly INSERT OR REPLACE or UPDATE OR INSERT or similar.
in_progress.transact(r#"[
{:person/age 33
:person/email "alice@example.org"}
]"#)?;
```

View file

@ -7,24 +7,25 @@ workspace = ".."
license = "Apache-2.0" license = "Apache-2.0"
repository = "https://github.com/mozilla/mentat" repository = "https://github.com/mozilla/mentat"
description = "EDN parser for Project Mentat" description = "EDN parser for Project Mentat"
build = "build.rs"
readme = "./README.md" readme = "./README.md"
[dependencies] [dependencies]
chrono = "~0.4" chrono = "0.4"
itertools = "~0.10" itertools = "0.7"
num = "~0.4" num = "0.1"
ordered-float = "~2.8" ordered-float = "0.5"
pretty = "~0.12" pretty = "0.2"
uuid = { version = "~1", features = ["v4", "serde"] } uuid = { version = "0.5", features = ["v4", "serde"] }
serde = { version = "~1.0", optional = true } serde = { version = "1.0", optional = true }
serde_derive = { version = "~1.0", optional = true } serde_derive = { version = "1.0", optional = true }
peg = "~0.8"
bytes = "1.0.1"
hex = "0.4.3"
[dev-dependencies] [dev-dependencies]
serde_test = "~1.0" serde_test = "1.0"
serde_json = "~1.0" serde_json = "1.0"
[features] [features]
serde_support = ["serde", "serde_derive"] serde_support = ["serde", "serde_derive"]
[build-dependencies]
peg = "0.5"

View file

@ -8,8 +8,8 @@
// CONDITIONS OF ANY KIND, either express or implied. See the License for the // CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License. // specific language governing permissions and limitations under the License.
extern crate failure; extern crate peg;
#[macro_use]
extern crate failure_derive;
pub mod errors; fn main() {
peg::cargo_build("src/edn.rustpeg");
}

491
edn/src/edn.rustpeg Normal file
View file

@ -0,0 +1,491 @@
/* -*- comment-start: "//"; -*- */
/* vim: set filetype=rust.rustpeg */
// 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.
use std::collections::{BTreeSet, BTreeMap, LinkedList};
use std::iter::FromIterator;
use std::f64::{NAN, INFINITY, NEG_INFINITY};
use chrono::{
DateTime,
TimeZone,
Utc
};
use num::BigInt;
use ordered_float::OrderedFloat;
use uuid::Uuid;
use entities::*;
use query;
use query::FromValue;
use symbols::*;
use types::{SpannedValue, Span, ValueAndSpan};
// Goal: Be able to parse https://github.com/edn-format/edn
// Also extensible to help parse http://docs.datomic.com/query.html
// Debugging hint: test using `cargo test --features peg/trace -- --nocapture`
// to trace where the parser is failing
// TODO: Support tagged elements
// TODO: Support discard
pub nil -> SpannedValue = "nil" { SpannedValue::Nil }
pub nan -> SpannedValue = "#f" whitespace+ "NaN" { SpannedValue::Float(OrderedFloat(NAN)) }
pub infinity -> SpannedValue = "#f" whitespace+ s:$(sign) "Infinity"
{ SpannedValue::Float(OrderedFloat(if s == "+" { INFINITY } else { NEG_INFINITY })) }
pub boolean -> SpannedValue
= "true" { SpannedValue::Boolean(true) }
/ "false" { SpannedValue::Boolean(false) }
digit = [0-9]
alphanumeric = [0-9a-zA-Z]
octaldigit = [0-7]
validbase = [3][0-6] / [12][0-9] / [2-9]
hex = [0-9a-fA-F]
sign = [+-]
pub raw_bigint -> BigInt = b:$( sign? digit+ ) "N"
{ b.parse::<BigInt>().unwrap() }
pub raw_octalinteger -> i64 = "0" i:$( octaldigit+ )
{ i64::from_str_radix(i, 8).unwrap() }
pub raw_hexinteger -> i64 = "0x" i:$( hex+ )
{ i64::from_str_radix(i, 16).unwrap() }
pub raw_basedinteger -> i64 = b:$( validbase ) "r" i:$( alphanumeric+ )
{ i64::from_str_radix(i, b.parse::<u32>().unwrap()).unwrap() }
pub raw_integer -> i64 = i:$( sign? digit+ ) !("." / ([eE]))
{ i.parse::<i64>().unwrap() }
pub raw_float -> OrderedFloat<f64> = f:$(sign? digit+ ("." digit+)? ([eE] sign? digit+)?)
{ OrderedFloat(f.parse::<f64>().unwrap()) }
pub bigint -> SpannedValue = v:raw_bigint { SpannedValue::BigInteger(v) }
pub octalinteger -> SpannedValue = v:raw_octalinteger { SpannedValue::Integer(v) }
pub hexinteger -> SpannedValue = v:raw_hexinteger { SpannedValue::Integer(v) }
pub basedinteger -> SpannedValue = v:raw_basedinteger { SpannedValue::Integer(v) }
pub integer -> SpannedValue = v:raw_integer { SpannedValue::Integer(v) }
pub float -> SpannedValue = v:raw_float { SpannedValue::Float(v) }
number -> SpannedValue = ( bigint / basedinteger / hexinteger / octalinteger / integer / float )
// TODO: standalone characters: \<char>, \newline, \return, \space and \tab.
string_special_char -> &'input str = "\\" $([\\"ntr])
string_normal_chars -> &'input str = $([^"\\]+)
// This is what we need to do in order to unescape. We can't just match the entire string slice:
// we get a Vec<&str> from rust-peg, where some of the parts might be unescaped special characters,
// and we join it together to form an output string.
// E.g., input = r#"\"foo\\\\bar\""#
// output = [quote, "foo", backslash, "bar", quote]
// result = r#""foo\\bar""#
// For the typical case, string_normal_chars will match multiple, leading to a single-element vec.
pub raw_text -> String = "\"" t:((string_special_char / string_normal_chars)*) "\""
{ t.join(&"").to_string() }
pub text -> SpannedValue
= v:raw_text { SpannedValue::Text(v) }
// RFC 3339 timestamps. #inst "1985-04-12T23:20:50.52Z"
// We accept an arbitrary depth of decimals.
// Note that we discard the timezone information -- all times are translated to UTC.
inst_string -> DateTime<Utc> =
"#inst" whitespace+ "\"" d:$( [0-9]*<4> "-" [0-2][0-9] "-" [0-3][0-9]
"T"
[0-2][0-9] ":" [0-5][0-9] ":" [0-6][0-9]
("." [0-9]+)?
("Z" / (("+" / "-") [0-2][0-9] ":" [0-5][0-9]))
)
"\"" {?
DateTime::parse_from_rfc3339(d)
.map(|t| t.with_timezone(&Utc))
.map_err(|_| "invalid datetime") // Oh, rustpeg.
}
inst_micros -> DateTime<Utc> =
"#instmicros" whitespace+ d:$( digit+ ) {
let micros = d.parse::<i64>().unwrap();
let seconds: i64 = micros / 1000000;
let nanos: u32 = ((micros % 1000000).abs() as u32) * 1000;
Utc.timestamp(seconds, nanos)
}
inst_millis -> DateTime<Utc> =
"#instmillis" whitespace+ d:$( digit+ ) {
let millis = d.parse::<i64>().unwrap();
let seconds: i64 = millis / 1000;
let nanos: u32 = ((millis % 1000).abs() as u32) * 1000000;
Utc.timestamp(seconds, nanos)
}
inst -> SpannedValue = t:(inst_millis / inst_micros / inst_string)
{ SpannedValue::Instant(t) }
uuid_string -> Uuid =
"\"" u:$( [a-f0-9]*<8> "-" [a-f0-9]*<4> "-" [a-f0-9]*<4> "-" [a-f0-9]*<4> "-" [a-f0-9]*<12> ) "\"" {
Uuid::parse_str(u).expect("this is a valid UUID string")
}
pub uuid -> SpannedValue = "#uuid" whitespace+ u:uuid_string
{ SpannedValue::Uuid(u) }
namespace_divider = "."
namespace_separator = "/"
// TODO: Be more picky here
// Keywords follow the rules of symbols, except they can (and must) begin with :
// e.g. :fred or :my/fred. See https://github.com/edn-format/edn#keywords
symbol_char_initial = [a-zA-Z0-9*!_?$%&=<>]
symbol_char_subsequent = [a-zA-Z0-9*!_?$%&=<>-]
symbol_namespace = symbol_char_initial symbol_char_subsequent* (namespace_divider symbol_char_subsequent+)*
symbol_name = ( symbol_char_initial+ symbol_char_subsequent* )
plain_symbol_name = symbol_name / "..." / "."
keyword_prefix = ":"
pub symbol -> SpannedValue =
ns:( sns:$(symbol_namespace) namespace_separator { sns })?
n:$(plain_symbol_name)
{ SpannedValue::from_symbol(ns, n) }
/ #expected("symbol")
pub keyword -> SpannedValue =
keyword_prefix
ns:( sns:$(symbol_namespace) namespace_separator { sns })?
n:$(symbol_name)
{ SpannedValue::from_keyword(ns, n) }
/ #expected("keyword")
pub list -> SpannedValue = "(" __ v:(value)* __ ")"
{ SpannedValue::List(LinkedList::from_iter(v)) }
pub vector -> SpannedValue = "[" __ v:(value)* __ "]"
{ SpannedValue::Vector(v) }
pub set -> SpannedValue = "#{" __ v:(value)* __ "}"
{ SpannedValue::Set(BTreeSet::from_iter(v)) }
pair -> (ValueAndSpan, ValueAndSpan) =
k:(value) v:(value) {
(k, v)
}
pub map -> SpannedValue = "{" __ v:(pair)* __ "}"
{ SpannedValue::Map(BTreeMap::from_iter(v)) }
// It's important that float comes before integer or the parser assumes that
// floats are integers and fails to parse
pub value -> ValueAndSpan =
__ start:#position v:(nil / nan / infinity / boolean / number / inst / uuid / text / keyword / symbol / list / vector / map / set) end:#position __ {
ValueAndSpan {
inner: v,
span: Span::new(start, end)
}
}
/ #expected("value")
atom -> ValueAndSpan
= v:value {? if v.is_atom() { Ok(v) } else { Err("expected atom") } }
// Clojure (and thus EDN) regards commas as whitespace, and thus the two-element vectors [1 2] and
// [1,,,,2] are equivalent, as are the maps {:a 1, :b 2} and {:a 1 :b 2}.
whitespace = #quiet<[ \r\n\t,]>
comment = #quiet<";" [^\r\n]* [\r\n]?>
__ = (whitespace / comment)*
// Transaction entity parser starts here.
pub op -> OpType
= ":db/add" { OpType::Add }
/ ":db/retract" { OpType::Retract }
raw_keyword -> Keyword =
keyword_prefix
ns:( sns:$(symbol_namespace) namespace_separator { sns })?
n:$(symbol_name) {
match ns {
Some(ns) => Keyword::namespaced(ns, n),
None => Keyword::plain(n),
}
}
/ #expected("keyword")
raw_forward_keyword -> Keyword
= v:raw_keyword {? if v.is_forward() { Ok(v) } else { Err("expected :forward or :forward/keyword") } }
raw_backward_keyword -> Keyword
= v:raw_keyword {? if v.is_backward() { Ok(v) } else { Err("expected :_backword or :backward/_keyword") } }
raw_namespaced_keyword -> Keyword
= keyword_prefix ns:$(symbol_namespace) namespace_separator n:$(symbol_name) { Keyword::namespaced(ns, n) }
/ #expected("namespaced keyword")
raw_forward_namespaced_keyword -> Keyword
= v:raw_namespaced_keyword {? if v.is_forward() { Ok(v) } else { Err("expected namespaced :forward/keyword") } }
raw_backward_namespaced_keyword -> Keyword
= v:raw_namespaced_keyword {? if v.is_backward() { Ok(v) } else { Err("expected namespaced :backward/_keyword") } }
entid -> EntidOrIdent
= v:( raw_basedinteger / raw_hexinteger / raw_octalinteger / raw_integer ) { EntidOrIdent::Entid(v) }
/ v:raw_namespaced_keyword { EntidOrIdent::Ident(v) }
/ #expected("entid")
forward_entid -> EntidOrIdent
= v:( raw_basedinteger / raw_hexinteger / raw_octalinteger / raw_integer ) { EntidOrIdent::Entid(v) }
/ v:raw_forward_namespaced_keyword { EntidOrIdent::Ident(v) }
/ #expected("forward entid")
backward_entid -> EntidOrIdent
= v:raw_backward_namespaced_keyword { EntidOrIdent::Ident(v.to_reversed()) }
/ #expected("backward entid")
lookup_ref -> LookupRef<ValueAndSpan>
= "(" __ "lookup-ref" __ a:(entid) __ v:(value) __ ")" { LookupRef { a: AttributePlace::Entid(a), v } }
/ #expected("lookup-ref")
tx_function -> TxFunction
= "(" __ n:$(symbol_name) __ ")" { TxFunction { op: PlainSymbol::plain(n) } }
entity_place -> EntityPlace<ValueAndSpan>
= v:raw_text { EntityPlace::TempId(TempId::External(v).into()) }
/ v:entid { EntityPlace::Entid(v) }
/ v:lookup_ref { EntityPlace::LookupRef(v) }
/ v:tx_function { EntityPlace::TxFunction(v) }
value_place_pair -> (EntidOrIdent, ValuePlace<ValueAndSpan>)
= k:(entid) __ v:(value_place) { (k, v) }
map_notation -> MapNotation<ValueAndSpan>
= "{" __ kvs:(value_place_pair*) __ "}" { kvs.into_iter().collect() }
value_place -> ValuePlace<ValueAndSpan>
= __ v:lookup_ref __ { ValuePlace::LookupRef(v) }
/ __ v:tx_function __ { ValuePlace::TxFunction(v) }
/ __ "[" __ vs:(value_place*) __ "]" __ { ValuePlace::Vector(vs) }
/ __ v:map_notation __ { ValuePlace::MapNotation(v) }
/ __ v:atom __ { ValuePlace::Atom(v) }
pub entity -> Entity<ValueAndSpan>
= __ "[" __ op:(op) __ e:(entity_place) __ a:(forward_entid) __ v:(value_place) __ "]" __ { Entity::AddOrRetract { op, e: e, a: AttributePlace::Entid(a), v: v } }
/ __ "[" __ op:(op) __ e:(value_place) __ a:(backward_entid) __ v:(entity_place) __ "]" __ { Entity::AddOrRetract { op, e: v, a: AttributePlace::Entid(a), v: e } }
/ __ map:map_notation __ { Entity::MapNotation(map) }
/ #expected("entity")
pub entities -> Vec<Entity<ValueAndSpan>>
= __ "[" __ es:(entity*) __ "]" __ { es }
// Query parser starts here.
//
// We expect every rule except the `raw_*` rules to eat whitespace
// (with `__`) at its start and finish. That means that every string
// pattern (say "[") should be bracketed on either side with either a
// whitespace-eating rule or an explicit whitespace eating `__`.
query_function -> query::QueryFunction
= __ n:$(symbol_name) __ {? query::QueryFunction::from_symbol(&PlainSymbol::plain(n)).ok_or("expected query function") }
fn_arg -> query::FnArg
= v:value {? query::FnArg::from_value(&v).ok_or("expected query function argument") }
/ __ "[" args:fn_arg+ "]" __ { query::FnArg::Vector(args) }
find_elem -> query::Element
= __ v:variable __ { query::Element::Variable(v) }
/ __ "(" __ "the" v:variable ")" __ { query::Element::Corresponding(v) }
/ __ "(" __ "pull" var:variable "[" patterns:pull_attribute+ "]" __ ")" __ { query::Element::Pull(query::Pull { var, patterns }) }
/ __ "(" func:query_function args:fn_arg* ")" __ { query::Element::Aggregate(query::Aggregate { func, args }) }
find_spec -> query::FindSpec
= f:find_elem "." __ { query::FindSpec::FindScalar(f) }
/ fs:find_elem+ { query::FindSpec::FindRel(fs) }
/ __ "[" f:find_elem __ "..." __ "]" __ { query::FindSpec::FindColl(f) }
/ __ "[" fs:find_elem+ "]" __ { query::FindSpec::FindTuple(fs) }
pull_attribute -> query::PullAttributeSpec
= __ "*" __ { query::PullAttributeSpec::Wildcard }
/ __ k:raw_forward_namespaced_keyword __ alias:(":as" __ alias:raw_forward_keyword __ { alias })? {
let attribute = query::PullConcreteAttribute::Ident(::std::rc::Rc::new(k));
let alias = alias.map(|alias| ::std::rc::Rc::new(alias));
query::PullAttributeSpec::Attribute(
query::NamedPullAttribute {
attribute,
alias: alias,
})
}
limit -> query::Limit
= __ v:variable __ { query::Limit::Variable(v) }
/ __ n:(raw_octalinteger / raw_hexinteger / raw_basedinteger / raw_integer) __ {?
if n > 0 {
Ok(query::Limit::Fixed(n as u64))
} else {
Err("expected positive integer")
}
}
order -> query::Order
= __ "(" __ "asc" v:variable ")" __ { query::Order(query::Direction::Ascending, v) }
/ __ "(" __ "desc" v:variable ")" __ { query::Order(query::Direction::Descending, v) }
/ v:variable { query::Order(query::Direction::Ascending, v) }
pattern_value_place -> query::PatternValuePlace
= v:value {? query::PatternValuePlace::from_value(&v).ok_or("expected pattern_value_place") }
pattern_non_value_place -> query::PatternNonValuePlace
= v:value {? query::PatternNonValuePlace::from_value(&v).ok_or("expected pattern_non_value_place") }
pattern -> query::WhereClause
= __ "["
src:src_var?
e:pattern_non_value_place
a:pattern_non_value_place
v:pattern_value_place?
tx:pattern_non_value_place?
"]" __
{?
let v = v.unwrap_or(query::PatternValuePlace::Placeholder);
let tx = tx.unwrap_or(query::PatternNonValuePlace::Placeholder);
// Pattern::new takes care of reversal of reversed
// attributes: [?x :foo/_bar ?y] turns into
// [?y :foo/bar ?x].
//
// This is a bit messy: the inner conversion to a Pattern can
// fail if the input is something like
//
// ```edn
// [?x :foo/_reversed 23.4]
// ```
//
// because
//
// ```edn
// [23.4 :foo/reversed ?x]
// ```
//
// is nonsense. That leaves us with a nested optional, which we unwrap here.
query::Pattern::new(src, e, a, v, tx)
.map(query::WhereClause::Pattern)
.ok_or("expected pattern")
}
// TODO: this shouldn't be checked at parse time.
rule_vars -> BTreeSet<query::Variable>
= vs:variable+ {?
let given = vs.len();
let set: BTreeSet<query::Variable> = vs.into_iter().collect();
if given != set.len() {
Err("expected unique variables")
} else {
Ok(set)
}
}
or_pattern_clause -> query::OrWhereClause
= clause:where_clause { query::OrWhereClause::Clause(clause) }
or_and_clause -> query::OrWhereClause
= __ "(" __ "and" clauses:where_clause+ ")" __ { query::OrWhereClause::And(clauses) }
or_where_clause -> query::OrWhereClause
= or_pattern_clause
/ or_and_clause
or_clause -> query::WhereClause
= __ "(" __ "or" clauses:or_where_clause+ ")" __ {
query::WhereClause::OrJoin(query::OrJoin::new(query::UnifyVars::Implicit, clauses))
}
or_join_clause -> query::WhereClause
= __ "(" __ "or-join" __ "[" vars:rule_vars "]" clauses:or_where_clause+ ")" __ {
query::WhereClause::OrJoin(query::OrJoin::new(query::UnifyVars::Explicit(vars), clauses))
}
not_clause -> query::WhereClause
= __ "(" __ "not" clauses:where_clause+ ")" __ {
query::WhereClause::NotJoin(query::NotJoin::new(query::UnifyVars::Implicit, clauses))
}
not_join_clause -> query::WhereClause
= __ "(" __ "not-join" __ "[" vars:rule_vars "]" clauses:where_clause+ ")" __ {
query::WhereClause::NotJoin(query::NotJoin::new(query::UnifyVars::Explicit(vars), clauses))
}
type_annotation -> query::WhereClause
= __ "[" __ "(" __ "type" var:variable __ ty:raw_keyword __ ")" __ "]" __ {
query::WhereClause::TypeAnnotation(
query::TypeAnnotation {
value_type: ty,
variable: var,
})
}
pred -> query::WhereClause
= __ "[" __ "(" func:query_function args:fn_arg* ")" __ "]" __ {
query::WhereClause::Pred(
query::Predicate {
operator: func.0,
args: args,
})
}
pub where_fn -> query::WhereClause
= __ "[" __ "(" func:query_function args:fn_arg* ")" __ binding:binding "]" __ {
query::WhereClause::WhereFn(
query::WhereFn {
operator: func.0,
args: args,
binding,
})
}
where_clause -> query::WhereClause
// Right now we only support patterns and predicates. See #239 for more.
= pattern
/ or_join_clause
/ or_clause
/ not_join_clause
/ not_clause
/ type_annotation
/ pred
/ where_fn
query_part -> query::QueryPart
= __ ":find" fs:find_spec { query::QueryPart::FindSpec(fs) }
/ __ ":in" in_vars:variable+ { query::QueryPart::InVars(in_vars) }
/ __ ":limit" l:limit { query::QueryPart::Limit(l) }
/ __ ":order" os:order+ { query::QueryPart::Order(os) }
/ __ ":where" ws:where_clause+ { query::QueryPart::WhereClauses(ws) }
/ __ ":with" with_vars:variable+ { query::QueryPart::WithVars(with_vars) }
pub parse_query -> query::ParsedQuery
= __ "[" qps:query_part+ "]" __ {? query::ParsedQuery::from_parts(qps) }
variable -> query::Variable
= v:value {? query::Variable::from_value(&v).ok_or("expected variable") }
src_var -> query::SrcVar
= v:value {? query::SrcVar::from_value(&v).ok_or("expected src_var") }
variable_or_placeholder -> query::VariableOrPlaceholder
= v:variable { query::VariableOrPlaceholder::Variable(v) }
/ __ "_" __ { query::VariableOrPlaceholder::Placeholder }
binding -> query::Binding
= __ "[" __ "[" vs:variable_or_placeholder+ "]" __ "]" __ { query::Binding::BindRel(vs) }
/ __ "[" v:variable "..." __ "]" __ { query::Binding::BindColl(v) }
/ __ "[" vs:variable_or_placeholder+ "]" __ { query::Binding::BindTuple(vs) }
/ v:variable { query::Binding::BindScalar(v) }

View file

@ -13,11 +13,18 @@
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::fmt; use std::fmt;
use crate::value_rc::ValueRc; use value_rc::{
ValueRc,
};
use crate::symbols::{Keyword, PlainSymbol}; use symbols::{
Keyword,
PlainSymbol,
};
use crate::types::ValueAndSpan; use types::{
ValueAndSpan,
};
/// `EntityPlace` and `ValuePlace` embed values, either directly (i.e., `ValuePlace::Atom`) or /// `EntityPlace` and `ValuePlace` embed values, either directly (i.e., `ValuePlace::Atom`) or
/// indirectly (i.e., `EntityPlace::LookupRef`). In order to maintain the graph of `Into` and /// indirectly (i.e., `EntityPlace::LookupRef`). In order to maintain the graph of `Into` and
@ -49,8 +56,8 @@ impl TempId {
impl fmt::Display for TempId { impl fmt::Display for TempId {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
match self { match self {
TempId::External(ref s) => write!(f, "{}", s), &TempId::External(ref s) => write!(f, "{}", s),
TempId::Internal(x) => write!(f, "<tempid {}>", x), &TempId::Internal(x) => write!(f, "<tempid {}>", x),
} }
} }
} }
@ -76,8 +83,8 @@ impl From<Keyword> for EntidOrIdent {
impl EntidOrIdent { impl EntidOrIdent {
pub fn unreversed(&self) -> Option<EntidOrIdent> { pub fn unreversed(&self) -> Option<EntidOrIdent> {
match self { match self {
EntidOrIdent::Entid(_) => None, &EntidOrIdent::Entid(_) => None,
EntidOrIdent::Ident(ref a) => a.unreversed().map(EntidOrIdent::Ident), &EntidOrIdent::Ident(ref a) => a.unreversed().map(EntidOrIdent::Ident),
} }
} }
} }

View file

@ -12,9 +12,14 @@
use std::collections::HashSet; use std::collections::HashSet;
use std::hash::Hash; use std::hash::Hash;
use std::ops::{Deref, DerefMut}; use std::ops::{
Deref,
DerefMut,
};
use crate::ValueRc; use ::{
ValueRc,
};
/// An `InternSet` allows to "intern" some potentially large values, maintaining a single value /// An `InternSet` allows to "intern" some potentially large values, maintaining a single value
/// instance owned by the `InternSet` and leaving consumers with lightweight ref-counted handles to /// instance owned by the `InternSet` and leaving consumers with lightweight ref-counted handles to
@ -24,17 +29,11 @@ use crate::ValueRc;
/// ///
/// See https://en.wikipedia.org/wiki/String_interning for discussion. /// See https://en.wikipedia.org/wiki/String_interning for discussion.
#[derive(Clone, Debug, Default, Eq, PartialEq)] #[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct InternSet<T> pub struct InternSet<T> where T: Eq + Hash {
where
T: Eq + Hash,
{
inner: HashSet<ValueRc<T>>, inner: HashSet<ValueRc<T>>,
} }
impl<T> Deref for InternSet<T> impl<T> Deref for InternSet<T> where T: Eq + Hash {
where
T: Eq + Hash,
{
type Target = HashSet<ValueRc<T>>; type Target = HashSet<ValueRc<T>>;
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
@ -42,19 +41,13 @@ where
} }
} }
impl<T> DerefMut for InternSet<T> impl<T> DerefMut for InternSet<T> where T: Eq + Hash {
where
T: Eq + Hash,
{
fn deref_mut(&mut self) -> &mut Self::Target { fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner &mut self.inner
} }
} }
impl<T> InternSet<T> impl<T> InternSet<T> where T: Eq + Hash {
where
T: Eq + Hash,
{
pub fn new() -> InternSet<T> { pub fn new() -> InternSet<T> {
InternSet { InternSet {
inner: HashSet::new(), inner: HashSet::new(),

View file

@ -8,13 +8,10 @@
// CONDITIONS OF ANY KIND, either express or implied. See the License for the // CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License. // specific language governing permissions and limitations under the License.
extern crate bytes;
extern crate chrono; extern crate chrono;
extern crate hex;
extern crate itertools; extern crate itertools;
extern crate num; extern crate num;
extern crate ordered_float; extern crate ordered_float;
extern crate peg;
extern crate pretty; extern crate pretty;
extern crate uuid; extern crate uuid;
@ -27,511 +24,48 @@ extern crate serde_derive;
pub mod entities; pub mod entities;
pub mod intern_set; pub mod intern_set;
pub use crate::intern_set::InternSet; pub use intern_set::{
InternSet,
};
// Intentionally not pub. // Intentionally not pub.
pub mod matcher;
mod namespaceable_name; mod namespaceable_name;
pub mod pretty_print;
pub mod query; pub mod query;
pub mod symbols; pub mod symbols;
pub mod types; pub mod types;
pub mod pretty_print;
pub mod utils; pub mod utils;
pub mod matcher;
pub mod value_rc; pub mod value_rc;
pub use crate::value_rc::{Cloned, FromRc, ValueRc}; pub use value_rc::{
Cloned,
FromRc,
ValueRc,
};
pub mod parse {
include!(concat!(env!("OUT_DIR"), "/edn.rs"));
}
// Re-export the types we use. // Re-export the types we use.
use bytes::Bytes;
pub use chrono::{DateTime, Utc}; pub use chrono::{DateTime, Utc};
use hex::decode;
pub use num::BigInt; pub use num::BigInt;
pub use ordered_float::OrderedFloat; pub use ordered_float::OrderedFloat;
pub use uuid::Uuid; pub use uuid::Uuid;
// Export from our modules. // Export from our modules.
pub use crate::types::{ pub use parse::ParseError;
FromMicros, FromMillis, Span, SpannedValue, ToMicros, ToMillis, Value, ValueAndSpan, pub use uuid::ParseError as UuidParseError;
pub use types::{
FromMicros,
Span,
SpannedValue,
ToMicros,
Value,
ValueAndSpan,
}; };
pub use crate::symbols::{Keyword, NamespacedSymbol, PlainSymbol}; pub use symbols::{
Keyword,
use std::collections::{BTreeMap, BTreeSet, LinkedList}; NamespacedSymbol,
use std::f64::{INFINITY, NAN, NEG_INFINITY}; PlainSymbol,
use std::iter::FromIterator; };
use chrono::TimeZone;
use crate::entities::*;
use crate::query::FromValue;
// Goal: Be able to parse https://github.com/edn-format/edn
// Also extensible to help parse http://docs.datomic.com/query.html
// Debugging hint: test using `cargo test --features peg/trace -- --nocapture`
// to trace where the parser is failing
// TODO: Support tagged elements
// TODO: Support discard
pub type ParseError = peg::error::ParseError<peg::str::LineCol>;
peg::parser!(pub grammar parse() for str {
pub rule nil() -> SpannedValue = "nil" { SpannedValue::Nil }
pub rule nan() -> SpannedValue = "#f" whitespace()+ "NaN" { SpannedValue::Float(OrderedFloat(NAN)) }
pub rule infinity() -> SpannedValue = "#f" whitespace()+ s:$(sign()) "Infinity"
{ SpannedValue::Float(OrderedFloat(if s == "+" { INFINITY } else { NEG_INFINITY })) }
pub rule boolean() -> SpannedValue
= "true" { SpannedValue::Boolean(true) }
/ "false" { SpannedValue::Boolean(false) }
rule digit() = ['0'..='9']
rule alphanumeric() = ['0'..='9' | 'a'..='z' | 'A'..='Z']
rule octaldigit() = ['0'..='7']
rule validbase() = ['3']['0'..='6'] / ['1' | '2']['0'..='9'] / ['2'..='9']
rule hex() = ['0'..='9' | 'a'..='f' | 'A'..='F']
rule sign() = ['+' | '-']
pub rule raw_bigint() -> BigInt = b:$( sign()? digit()+ ) "N"
{ b.parse::<BigInt>().unwrap() }
pub rule raw_octalinteger() -> i64 = "0" i:$( octaldigit()+ )
{ i64::from_str_radix(i, 8).unwrap() }
pub rule raw_hexinteger() -> i64 = "0x" i:$( hex()+ )
{ i64::from_str_radix(i, 16).unwrap() }
pub rule raw_basedinteger() -> i64 = b:$( validbase() ) "r" i:$( alphanumeric()+ )
{ i64::from_str_radix(i, b.parse::<u32>().unwrap()).unwrap() }
pub rule raw_integer() -> i64 = i:$( sign()? digit()+ ) !("." / (['e' | 'E']))
{ i.parse::<i64>().unwrap() }
pub rule raw_float() -> OrderedFloat<f64> = f:$(sign()? digit()+ ("." digit()+)? (['e' | 'E'] sign()? digit()+)?)
{ OrderedFloat(f.parse::<f64>().unwrap()) }
pub rule bigint() -> SpannedValue = v:raw_bigint() { SpannedValue::BigInteger(v) }
pub rule octalinteger() -> SpannedValue = v:raw_octalinteger() { SpannedValue::Integer(v) }
pub rule hexinteger() -> SpannedValue = v:raw_hexinteger() { SpannedValue::Integer(v) }
pub rule basedinteger() -> SpannedValue = v:raw_basedinteger() { SpannedValue::Integer(v) }
pub rule integer() -> SpannedValue = v:raw_integer() { SpannedValue::Integer(v) }
pub rule float() -> SpannedValue = v:raw_float() { SpannedValue::Float(v) }
rule number() -> SpannedValue = ( bigint() / basedinteger() / hexinteger() / octalinteger() / integer() / float() )
// TODO: standalone characters: \<char>, \newline, \return, \space and \tab.
// rule string_standalone_chars() ->
rule string_special_char() -> &'input str = "\\" c:$(['\\' | '"' | 'n' | 't' | 'r']) { c }
rule string_normal_chars() -> &'input str = c:$((!['\"' | '\\'][_])+) { c }
// This is what we need to do in order to unescape. We can't just match the entire string slice:
// we get a Vec<&str> from rust-peg, where some parts might be unescaped special characters and
// we join it together to form an output string.
// E.g., input = r#"\"foo\\\\bar\""#
// output = [quote, "foo", backslash, "bar", quote]
// result = r#""foo\\bar""#
// For the typical case, string_normal_chars will match multiple, leading to a single-element vec.
pub rule raw_text() -> String = "\"" t:((string_special_char() / string_normal_chars())*) "\""
{ t.join("") }
pub rule text() -> SpannedValue
= v:raw_text() { SpannedValue::Text(v) }
// RFC 3339 timestamps. #inst "1985-04-12T23:20:50.52Z"
// We accept an arbitrary depth of decimals.
// TODO: Note that we discard the timezone information -- all times are translated to UTC. Should we?
rule inst_string() -> DateTime<Utc> =
"#inst" whitespace()+ "\"" d:$( ['0'..='9']*<4> "-" ['0'..='2']['0'..='9'] "-" ['0'..='3']['0'..='9']
"T"
['0'..='2']['0'..='9'] ":" ['0'..='5']['0'..='9'] ":" ['0'..='6']['0'..='9']
("." ['0'..='9']+)?
("Z" / (("+" / "-") ['0'..='2']['0'..='9'] ":" ['0'..='5']['0'..='9']))
)
"\"" {?
DateTime::parse_from_rfc3339(d)
.map(|t| t.with_timezone(&Utc))
.map_err(|_| "invalid datetime") // TODO Oh, rustpeg.
}
rule inst_micros() -> DateTime<Utc> =
"#instmicros" whitespace()+ d:$( digit()+ ) {
let micros = d.parse::<i64>().unwrap();
let seconds: i64 = micros / 1_000_000;
let nanos: u32 = ((micros % 1_000_000).unsigned_abs() as u32) * 1000;
Utc.timestamp_opt(seconds, nanos).unwrap()
}
rule inst_millis() -> DateTime<Utc> =
"#instmillis" whitespace()+ d:$( digit()+ ) {
let millis = d.parse::<i64>().unwrap();
let seconds: i64 = millis / 1000;
let nanos: u32 = ((millis % 1000).unsigned_abs() as u32) * 1_000_000;
Utc.timestamp_opt(seconds, nanos).unwrap()
}
rule inst() -> SpannedValue = t:(inst_millis() / inst_micros() / inst_string())
{ SpannedValue::Instant(t) }
rule uuid_string() -> Uuid =
"\"" u:$( ['a'..='f' | '0'..='9']*<8> "-" ['a'..='f' | '0'..='9']*<4> "-" ['a'..='f' | '0'..='9']*<4> "-" ['a'..='f' | '0'..='9']*<4> "-" ['a'..='f' | '0'..='9']*<12> ) "\"" {
Uuid::parse_str(u).expect("this is a valid UUID string")
}
pub rule uuid() -> SpannedValue = "#uuid" whitespace()+ u:uuid_string()
{ SpannedValue::Uuid(u) }
rule byte_buffer() -> Bytes =
u:$( hex()+ ) {
let b = decode(u).expect("this is a valid hex byte string");
Bytes::copy_from_slice(&b)
}
pub rule bytes() -> SpannedValue = "#bytes" whitespace()+ u:byte_buffer()
{ SpannedValue::Bytes(u) }
rule namespace_divider() = "."
rule namespace_separator() = "/"
// TODO: Be more picky here.
// Keywords follow the rules of symbols, except they can (and must) begin with :
// e.g. :fred or :my/fred. See https://github.com/edn-format/edn#keywords
rule symbol_char_initial() = ['a'..='z' | 'A'..='Z' | '0'..='9' | '*' | '!' | '_' | '?' | '$' | '%' | '&' | '=' | '<' | '>']
rule symbol_char_subsequent() = ['+' | 'a'..='z' | 'A'..='Z' | '0'..='9' | '*' | '!' | '_' | '?' | '$' | '%' | '&' | '=' | '<' | '>' | '-']
rule symbol_namespace() = symbol_char_initial() symbol_char_subsequent()* (namespace_divider() symbol_char_subsequent()+)*
rule symbol_name() = ( symbol_char_initial()+ symbol_char_subsequent()* )
rule plain_symbol_name() = symbol_name() / "..." / "."
rule keyword_prefix() = ":"
pub rule symbol() -> SpannedValue =
ns:( sns:$(symbol_namespace()) namespace_separator() { sns })?
n:$(plain_symbol_name())
{ SpannedValue::from_symbol(ns, n) }
/ expected!("symbol")
pub rule keyword() -> SpannedValue =
keyword_prefix()
ns:( sns:$(symbol_namespace()) namespace_separator() { sns })?
n:$(symbol_name())
{ SpannedValue::from_keyword(ns, n) }
/ expected!("keyword")
pub rule list() -> SpannedValue = "(" __ v:(value())* __ ")"
{ SpannedValue::List(LinkedList::from_iter(v)) }
pub rule vector() -> SpannedValue = "[" __ v:(value())* __ "]"
{ SpannedValue::Vector(v) }
pub rule set() -> SpannedValue = "#{" __ v:(value())* __ "}"
{ SpannedValue::Set(BTreeSet::from_iter(v)) }
pub rule pair() -> (ValueAndSpan, ValueAndSpan) =
k:(value()) v:(value()) {
(k, v)
}
pub rule map() -> SpannedValue = "{" __ v:(pair())* __ "}"
{ SpannedValue::Map(BTreeMap::from_iter(v)) }
// Note: It's important that float comes before integer or the parser assumes that floats are integers and fails to parse.
pub rule value() -> ValueAndSpan =
__ start:position!() v:(nil() / nan() / infinity() / boolean() / number() / inst() / uuid() / bytes() / text() / keyword() / symbol() / list() / vector() / map() / set() ) end:position!() __ {
ValueAndSpan {
inner: v,
span: Span::new(start, end)
}
}
/ expected!("value")
rule atom() -> ValueAndSpan
= v:value() {? if v.is_atom() { Ok(v) } else { Err("expected atom") } }
// Clojure (and thus EDN) regards commas as whitespace, and thus the two-element vectors [1 2] and
// [1,,,,2] are equivalent, as are the maps {:a 1, :b 2} and {:a 1 :b 2}.
rule whitespace() = quiet!{[' ' | '\r' | '\n' | '\t' | ',']}
rule comment() = quiet!{";" (!['\r' | '\n'][_])* ['\r' | '\n']?}
rule __() = (whitespace() / comment())*
// Transaction entity parser starts here.
pub rule op() -> OpType
= ":db/add" { OpType::Add }
/ ":db/retract" { OpType::Retract }
rule raw_keyword() -> Keyword =
keyword_prefix()
ns:( sns:$(symbol_namespace()) namespace_separator() { sns })?
n:$(symbol_name()) {
match ns {
Some(ns) => Keyword::namespaced(ns, n),
None => Keyword::plain(n),
}
}
/ expected!("keyword")
rule raw_forward_keyword() -> Keyword
= v:raw_keyword() {? if v.is_forward() { Ok(v) } else { Err("expected :forward or :forward/keyword") } }
rule raw_backward_keyword() -> Keyword
= v:raw_keyword() {? if v.is_backward() { Ok(v) } else { Err("expected :_backward or :backward/_keyword") } }
rule raw_namespaced_keyword() -> Keyword
= keyword_prefix() ns:$(symbol_namespace()) namespace_separator() n:$(symbol_name()) { Keyword::namespaced(ns, n) }
/ expected!("namespaced keyword")
rule raw_forward_namespaced_keyword() -> Keyword
= v:raw_namespaced_keyword() {? if v.is_forward() { Ok(v) } else { Err("expected namespaced :forward/keyword") } }
rule raw_backward_namespaced_keyword() -> Keyword
= v:raw_namespaced_keyword() {? if v.is_backward() { Ok(v) } else { Err("expected namespaced :backward/_keyword") } }
rule entid() -> EntidOrIdent
= v:( raw_basedinteger() / raw_hexinteger() / raw_octalinteger() / raw_integer() ) { EntidOrIdent::Entid(v) }
/ v:raw_namespaced_keyword() { EntidOrIdent::Ident(v) }
/ expected!("entid")
rule forward_entid() -> EntidOrIdent
= v:( raw_basedinteger() / raw_hexinteger() / raw_octalinteger() / raw_integer() ) { EntidOrIdent::Entid(v) }
/ v:raw_forward_namespaced_keyword() { EntidOrIdent::Ident(v) }
/ expected!("forward entid")
rule backward_entid() -> EntidOrIdent
= v:raw_backward_namespaced_keyword() { EntidOrIdent::Ident(v.to_reversed()) }
/ expected!("backward entid")
rule lookup_ref() -> LookupRef<ValueAndSpan>
= "(" __ "lookup-ref" __ a:(entid()) __ v:(value()) __ ")" { LookupRef { a: AttributePlace::Entid(a), v } }
/ expected!("lookup-ref")
rule tx_function() -> TxFunction
= "(" __ n:$(symbol_name()) __ ")" { TxFunction { op: PlainSymbol::plain(n) } }
rule entity_place() -> EntityPlace<ValueAndSpan>
= v:raw_text() { EntityPlace::TempId(TempId::External(v).into()) }
/ v:entid() { EntityPlace::Entid(v) }
/ v:lookup_ref() { EntityPlace::LookupRef(v) }
/ v:tx_function() { EntityPlace::TxFunction(v) }
rule value_place_pair() -> (EntidOrIdent, ValuePlace<ValueAndSpan>)
= k:(entid()) __ v:(value_place()) { (k, v) }
rule map_notation() -> MapNotation<ValueAndSpan>
= "{" __ kvs:(value_place_pair()*) __ "}" { kvs.into_iter().collect() }
rule value_place() -> ValuePlace<ValueAndSpan>
= __ v:lookup_ref() __ { ValuePlace::LookupRef(v) }
/ __ v:tx_function() __ { ValuePlace::TxFunction(v) }
/ __ "[" __ vs:(value_place()*) __ "]" __ { ValuePlace::Vector(vs) }
/ __ v:map_notation() __ { ValuePlace::MapNotation(v) }
/ __ v:atom() __ { ValuePlace::Atom(v) }
pub rule entity() -> Entity<ValueAndSpan>
= __ "[" __ op:(op()) __ e:(entity_place()) __ a:(forward_entid()) __ v:(value_place()) __ "]" __ { Entity::AddOrRetract { op, e, a: AttributePlace::Entid(a), v } }
/ __ "[" __ op:(op()) __ e:(value_place()) __ a:(backward_entid()) __ v:(entity_place()) __ "]" __ { Entity::AddOrRetract { op, e: v, a: AttributePlace::Entid(a), v: e } }
/ __ map:map_notation() __ { Entity::MapNotation(map) }
/ expected!("entity")
pub rule entities() -> Vec<Entity<ValueAndSpan>>
= __ "[" __ es:(entity()*) __ "]" __ { es }
// Query parser starts here.
//
// We expect every rule except the `raw_*` rules to eat whitespace
// (with `__`) at its start and finish. That means that every string
// pattern (say "[") should be bracketed on either side with either a
// whitespace-eating rule or an explicit whitespace eating `__`.
rule query_function() -> query::QueryFunction
= __ n:$(symbol_name()) __ {? query::QueryFunction::from_symbol(&PlainSymbol::plain(n)).ok_or("expected query function") }
rule fn_arg() -> query::FnArg
= v:value() {? query::FnArg::from_value(&v).ok_or("expected query function argument") }
/ __ "[" args:fn_arg()+ "]" __ { query::FnArg::Vector(args) }
rule find_elem() -> query::Element
= __ v:variable() __ { query::Element::Variable(v) }
/ __ "(" __ "the" v:variable() ")" __ { query::Element::Corresponding(v) }
/ __ "(" __ "pull" var:variable() "[" patterns:pull_attribute()+ "]" __ ")" __ { query::Element::Pull(query::Pull { var, patterns }) }
/ __ "(" func:query_function() args:fn_arg()* ")" __ { query::Element::Aggregate(query::Aggregate { func, args }) }
rule find_spec() -> query::FindSpec
= f:find_elem() "." __ { query::FindSpec::FindScalar(f) }
/ fs:find_elem()+ { query::FindSpec::FindRel(fs) }
/ __ "[" f:find_elem() __ "..." __ "]" __ { query::FindSpec::FindColl(f) }
/ __ "[" fs:find_elem()+ "]" __ { query::FindSpec::FindTuple(fs) }
rule pull_attribute() -> query::PullAttributeSpec
= __ "*" __ { query::PullAttributeSpec::Wildcard }
/ __ k:raw_forward_namespaced_keyword() __ alias:(":as" __ alias:raw_forward_keyword() __ { alias })? {
let attribute = query::PullConcreteAttribute::Ident(::std::rc::Rc::new(k));
let alias = alias.map(::std::rc::Rc::new);
query::PullAttributeSpec::Attribute(
query::NamedPullAttribute {
attribute,
alias,
})
}
rule limit() -> query::Limit
= __ v:variable() __ { query::Limit::Variable(v) }
/ __ n:(raw_octalinteger() / raw_hexinteger() / raw_basedinteger() / raw_integer()) __ {?
if n > 0 {
Ok(query::Limit::Fixed(n as u64))
} else {
Err("expected positive integer")
}
}
rule order() -> query::Order
= __ "(" __ "asc" v:variable() ")" __ { query::Order(query::Direction::Ascending, v) }
/ __ "(" __ "desc" v:variable() ")" __ { query::Order(query::Direction::Descending, v) }
/ v:variable() { query::Order(query::Direction::Ascending, v) }
rule pattern_value_place() -> query::PatternValuePlace
= v:value() {? query::PatternValuePlace::from_value(&v).ok_or("expected pattern_value_place") }
rule pattern_non_value_place() -> query::PatternNonValuePlace
= v:value() {? query::PatternNonValuePlace::from_value(&v).ok_or("expected pattern_non_value_place") }
rule pattern() -> query::WhereClause
= __ "["
src:src_var()?
e:pattern_non_value_place()
a:pattern_non_value_place()
v:pattern_value_place()?
tx:pattern_non_value_place()?
"]" __
{?
let v = v.unwrap_or(query::PatternValuePlace::Placeholder);
let tx = tx.unwrap_or(query::PatternNonValuePlace::Placeholder);
// Pattern::new takes care of reversal of reversed
// attributes: [?x :foo/_bar ?y] turns into
// [?y :foo/bar ?x].
//
// This is a bit messy: the inner conversion to a Pattern can
// fail if the input is something like
//
// ```edn
// [?x :foo/_reversed 23.4]
// ```
//
// because
//
// ```edn
// [23.4 :foo/reversed ?x]
// ```
//
// is nonsense. That leaves us with a nested optional, which we unwrap here.
query::Pattern::new(src, e, a, v, tx)
.map(query::WhereClause::Pattern)
.ok_or("expected pattern")
}
// TODO: This shouldn't be checked at parse time.
rule rule_vars() -> BTreeSet<query::Variable>
= vs:variable()+ {?
let given = vs.len();
let set: BTreeSet<query::Variable> = vs.into_iter().collect();
if given != set.len() {
Err("expected unique variables")
} else {
Ok(set)
}
}
rule or_pattern_clause() -> query::OrWhereClause
= clause:where_clause() { query::OrWhereClause::Clause(clause) }
rule or_and_clause() -> query::OrWhereClause
= __ "(" __ "and" clauses:where_clause()+ ")" __ { query::OrWhereClause::And(clauses) }
rule or_where_clause() -> query::OrWhereClause
= or_pattern_clause()
/ or_and_clause()
rule or_clause() -> query::WhereClause
= __ "(" __ "or" clauses:or_where_clause()+ ")" __ {
query::WhereClause::OrJoin(query::OrJoin::new(query::UnifyVars::Implicit, clauses))
}
rule or_join_clause() -> query::WhereClause
= __ "(" __ "or-join" __ "[" vars:rule_vars() "]" clauses:or_where_clause()+ ")" __ {
query::WhereClause::OrJoin(query::OrJoin::new(query::UnifyVars::Explicit(vars), clauses))
}
rule not_clause() -> query::WhereClause
= __ "(" __ "not" clauses:where_clause()+ ")" __ {
query::WhereClause::NotJoin(query::NotJoin::new(query::UnifyVars::Implicit, clauses))
}
rule not_join_clause() -> query::WhereClause
= __ "(" __ "not-join" __ "[" vars:rule_vars() "]" clauses:where_clause()+ ")" __ {
query::WhereClause::NotJoin(query::NotJoin::new(query::UnifyVars::Explicit(vars), clauses))
}
rule type_annotation() -> query::WhereClause
= __ "[" __ "(" __ "type" var:variable() __ ty:raw_keyword() __ ")" __ "]" __ {
query::WhereClause::TypeAnnotation(
query::TypeAnnotation {
value_type: ty,
variable: var,
})
}
rule pred() -> query::WhereClause
= __ "[" __ "(" func:query_function() args:fn_arg()* ")" __ "]" __ {
query::WhereClause::Pred(
query::Predicate {
operator: func.0,
args,
})
}
pub rule where_fn() -> query::WhereClause
= __ "[" __ "(" func:query_function() args:fn_arg()* ")" __ binding:binding() "]" __ {
query::WhereClause::WhereFn(
query::WhereFn {
operator: func.0,
args,
binding,
})
}
rule where_clause() -> query::WhereClause
// Right now we only support patterns and predicates. See #239 for more.
= pattern()
/ or_join_clause()
/ or_clause()
/ not_join_clause()
/ not_clause()
/ type_annotation()
/ pred()
/ where_fn()
rule query_part() -> query::QueryPart
= __ ":find" fs:find_spec() { query::QueryPart::FindSpec(fs) }
/ __ ":in" in_vars:variable()+ { query::QueryPart::InVars(in_vars) }
/ __ ":limit" l:limit() { query::QueryPart::Limit(l) }
/ __ ":order" os:order()+ { query::QueryPart::Order(os) }
/ __ ":where" ws:where_clause()+ { query::QueryPart::WhereClauses(ws) }
/ __ ":with" with_vars:variable()+ { query::QueryPart::WithVars(with_vars) }
pub rule parse_query() -> query::ParsedQuery
= __ "[" qps:query_part()+ "]" __ {? query::ParsedQuery::from_parts(qps) }
rule variable() -> query::Variable
= v:value() {? query::Variable::from_value(&v).ok_or("expected variable") }
rule src_var() -> query::SrcVar
= v:value() {? query::SrcVar::from_value(&v).ok_or("expected src_var") }
rule variable_or_placeholder() -> query::VariableOrPlaceholder
= v:variable() { query::VariableOrPlaceholder::Variable(v) }
/ __ "_" __ { query::VariableOrPlaceholder::Placeholder }
rule binding() -> query::Binding
= __ "[" __ "[" vs:variable_or_placeholder()+ "]" __ "]" __ { query::Binding::BindRel(vs) }
/ __ "[" v:variable() "..." __ "]" __ { query::Binding::BindColl(v) }
/ __ "[" vs:variable_or_placeholder()+ "]" __ { query::Binding::BindTuple(vs) }
/ v:variable() { query::Binding::BindScalar(v) }
});

View file

@ -8,12 +8,12 @@
// CONDITIONS OF ANY KIND, either express or implied. See the License for the // CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License. // specific language governing permissions and limitations under the License.
use itertools::diff_with;
use std::cell::RefCell;
use std::collections::HashMap; use std::collections::HashMap;
use std::cell::RefCell;
use itertools::diff_with;
use crate::symbols; use symbols;
use crate::types::Value; use types::Value;
/// A trait defining pattern matching rules for any given pattern of type `T`. /// A trait defining pattern matching rules for any given pattern of type `T`.
trait PatternMatchingRules<'a, T> { trait PatternMatchingRules<'a, T> {
@ -21,7 +21,7 @@ trait PatternMatchingRules<'a, T> {
fn matches_any(pattern: &T) -> bool; fn matches_any(pattern: &T) -> bool;
/// Return the placeholder name if the given pattern matches a placeholder. /// Return the placeholder name if the given pattern matches a placeholder.
fn matches_placeholder(pattern: &'a T) -> Option<&'a String>; fn matches_placeholder(pattern: &'a T) -> Option<(&'a String)>;
} }
/// A default type implementing `PatternMatchingRules` specialized on /// A default type implementing `PatternMatchingRules` specialized on
@ -34,20 +34,14 @@ impl<'a> PatternMatchingRules<'a, Value> for DefaultPatternMatchingRules {
fn matches_any(pattern: &Value) -> bool { fn matches_any(pattern: &Value) -> bool {
match *pattern { match *pattern {
Value::PlainSymbol(symbols::PlainSymbol(ref s)) => s.starts_with('_'), Value::PlainSymbol(symbols::PlainSymbol(ref s)) => s.starts_with('_'),
_ => false, _ => false
} }
} }
fn matches_placeholder(pattern: &'a Value) -> Option<&'a String> { fn matches_placeholder(pattern: &'a Value) -> Option<(&'a String)> {
match *pattern { match *pattern {
Value::PlainSymbol(symbols::PlainSymbol(ref s)) => { Value::PlainSymbol(symbols::PlainSymbol(ref s)) => if s.starts_with('?') { Some(s) } else { None },
if s.starts_with('?') { _ => None
Some(s)
} else {
None
}
}
_ => None,
} }
} }
} }
@ -58,14 +52,14 @@ impl<'a> PatternMatchingRules<'a, Value> for DefaultPatternMatchingRules {
/// * `[_ _]` matches an arbitrary two-element vector; /// * `[_ _]` matches an arbitrary two-element vector;
/// * `[?x ?x]` matches `[1 1]` and `[#{} #{}]` but not `[1 2]` or `[[] #{}]`; /// * `[?x ?x]` matches `[1 1]` and `[#{} #{}]` but not `[1 2]` or `[[] #{}]`;
struct Matcher<'a> { struct Matcher<'a> {
placeholders: RefCell<HashMap<&'a String, &'a Value>>, placeholders: RefCell<HashMap<&'a String, &'a Value>>
} }
impl<'a> Matcher<'a> { impl<'a> Matcher<'a> {
/// Creates a Matcher instance. /// Creates a Matcher instance.
fn new() -> Matcher<'a> { fn new() -> Matcher<'a> {
Matcher { Matcher {
placeholders: RefCell::default(), placeholders: RefCell::default()
} }
} }
@ -73,9 +67,7 @@ impl<'a> Matcher<'a> {
/// and `pattern`) utilizing a specified pattern matching ruleset `T`. /// and `pattern`) utilizing a specified pattern matching ruleset `T`.
/// Returns true if matching succeeds. /// Returns true if matching succeeds.
fn match_with_rules<T>(value: &'a Value, pattern: &'a Value) -> bool fn match_with_rules<T>(value: &'a Value, pattern: &'a Value) -> bool
where where T: PatternMatchingRules<'a, Value> {
T: PatternMatchingRules<'a, Value>,
{
let matcher = Matcher::new(); let matcher = Matcher::new();
matcher.match_internal::<T>(value, pattern) matcher.match_internal::<T>(value, pattern)
} }
@ -84,10 +76,8 @@ impl<'a> Matcher<'a> {
/// performing pattern matching. Note that the internal `placeholders` cache /// performing pattern matching. Note that the internal `placeholders` cache
/// might not be empty on invocation. /// might not be empty on invocation.
fn match_internal<T>(&self, value: &'a Value, pattern: &'a Value) -> bool fn match_internal<T>(&self, value: &'a Value, pattern: &'a Value) -> bool
where where T: PatternMatchingRules<'a, Value> {
T: PatternMatchingRules<'a, Value>, use Value::*;
{
use crate::Value::*;
if T::matches_any(pattern) { if T::matches_any(pattern) {
true true
@ -96,35 +86,19 @@ impl<'a> Matcher<'a> {
value == *placeholders.entry(symbol).or_insert(value) value == *placeholders.entry(symbol).or_insert(value)
} else { } else {
match (value, pattern) { match (value, pattern) {
(&Vector(ref v), &Vector(ref p)) => { (&Vector(ref v), &Vector(ref p)) =>
diff_with(v, p, |a, b| self.match_internal::<T>(a, b)).is_none() diff_with(v, p, |a, b| self.match_internal::<T>(a, b)).is_none(),
} (&List(ref v), &List(ref p)) =>
(&List(ref v), &List(ref p)) => { diff_with(v, p, |a, b| self.match_internal::<T>(a, b)).is_none(),
diff_with(v, p, |a, b| self.match_internal::<T>(a, b)).is_none() (&Set(ref v), &Set(ref p)) =>
} v.len() == p.len() &&
(&Set(ref v), &Set(ref p)) => { v.iter().all(|a| p.iter().any(|b| self.match_internal::<T>(a, b))) &&
v.len() == p.len() p.iter().all(|b| v.iter().any(|a| self.match_internal::<T>(a, b))),
&& v.iter() (&Map(ref v), &Map(ref p)) =>
.all(|a| p.iter().any(|b| self.match_internal::<T>(a, b))) v.len() == p.len() &&
&& p.iter() v.iter().all(|a| p.iter().any(|b| self.match_internal::<T>(a.0, b.0) && self.match_internal::<T>(a.1, b.1))) &&
.all(|b| v.iter().any(|a| self.match_internal::<T>(a, b))) p.iter().all(|b| v.iter().any(|a| self.match_internal::<T>(a.0, b.0) && self.match_internal::<T>(a.1, b.1))),
} _ => value == pattern
(&Map(ref v), &Map(ref p)) => {
v.len() == p.len()
&& v.iter().all(|a| {
p.iter().any(|b| {
self.match_internal::<T>(a.0, b.0)
&& self.match_internal::<T>(a.1, b.1)
})
})
&& p.iter().all(|b| {
v.iter().any(|a| {
self.match_internal::<T>(a.0, b.0)
&& self.match_internal::<T>(a.1, b.1)
})
})
}
_ => value == pattern,
} }
} }
} }
@ -140,7 +114,7 @@ impl Value {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use crate::parse; use parse;
macro_rules! assert_match { macro_rules! assert_match {
( $pattern:tt, $value:tt, $expected:expr ) => { ( $pattern:tt, $value:tt, $expected:expr ) => {
@ -153,7 +127,7 @@ mod test {
}; };
( $pattern:tt !~ $value:tt ) => { ( $pattern:tt !~ $value:tt ) => {
assert_match!($pattern, $value, false); assert_match!($pattern, $value, false);
}; }
} }
#[test] #[test]

View file

@ -8,14 +8,25 @@
// CONDITIONS OF ANY KIND, either express or implied. See the License for the // CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License. // specific language governing permissions and limitations under the License.
use std::cmp::{Ord, Ordering, PartialOrd}; use std::cmp::{
Ord,
Ordering,
PartialOrd,
};
use std::fmt; use std::fmt;
#[cfg(feature = "serde_support")] #[cfg(feature = "serde_support")]
use serde::de::{self, Deserialize, Deserializer}; use serde::de::{
self,
Deserialize,
Deserializer
};
#[cfg(feature = "serde_support")] #[cfg(feature = "serde_support")]
use serde::ser::{Serialize, Serializer}; use serde::ser::{
Serialize,
Serializer,
};
// Data storage for both NamespaceableKeyword and NamespaceableSymbol. // Data storage for both NamespaceableKeyword and NamespaceableSymbol.
#[derive(Clone, Eq, Hash, PartialEq)] #[derive(Clone, Eq, Hash, PartialEq)]
@ -45,10 +56,7 @@ pub struct NamespaceableName {
impl NamespaceableName { impl NamespaceableName {
#[inline] #[inline]
pub fn plain<T>(name: T) -> Self pub fn plain<T>(name: T) -> Self where T: Into<String> {
where
T: Into<String>,
{
let n = name.into(); let n = name.into();
assert!(!n.is_empty(), "Symbols and keywords cannot be unnamed."); assert!(!n.is_empty(), "Symbols and keywords cannot be unnamed.");
@ -59,21 +67,14 @@ impl NamespaceableName {
} }
#[inline] #[inline]
pub fn namespaced<N, T>(namespace: N, name: T) -> Self pub fn namespaced<N, T>(namespace: N, name: T) -> Self where N: AsRef<str>, T: AsRef<str> {
where
N: AsRef<str>,
T: AsRef<str>,
{
let n = name.as_ref(); let n = name.as_ref();
let ns = namespace.as_ref(); let ns = namespace.as_ref();
// Note: These invariants are not required for safety. That is, if we // Note: These invariants are not required for safety. That is, if we
// decide to allow these we can safely remove them. // decide to allow these we can safely remove them.
assert!(!n.is_empty(), "Symbols and keywords cannot be unnamed."); assert!(!n.is_empty(), "Symbols and keywords cannot be unnamed.");
assert!( assert!(!ns.is_empty(), "Symbols and keywords cannot have an empty non-null namespace.");
!ns.is_empty(),
"Symbols and keywords cannot have an empty non-null namespace."
);
let mut dest = String::with_capacity(n.len() + ns.len()); let mut dest = String::with_capacity(n.len() + ns.len());
@ -85,15 +86,11 @@ impl NamespaceableName {
NamespaceableName { NamespaceableName {
components: dest, components: dest,
boundary, boundary: boundary,
} }
} }
fn new<N, T>(namespace: Option<N>, name: T) -> Self fn new<N, T>(namespace: Option<N>, name: T) -> Self where N: AsRef<str>, T: AsRef<str> {
where
N: AsRef<str>,
T: AsRef<str>,
{
if let Some(ns) = namespace { if let Some(ns) = namespace {
Self::namespaced(ns, name) Self::namespaced(ns, name)
} else { } else {
@ -121,7 +118,7 @@ impl NamespaceableName {
if name.starts_with('_') { if name.starts_with('_') {
Self::new(self.namespace(), &name[1..]) Self::new(self.namespace(), &name[1..])
} else { } else {
Self::new(self.namespace(), format!("_{}", name)) Self::new(self.namespace(), &format!("_{}", name))
} }
} }
@ -144,14 +141,13 @@ impl NamespaceableName {
} }
#[inline] #[inline]
pub fn components(&self) -> (&str, &str) { pub fn components<'a>(&'a self) -> (&'a str, &'a str) {
if self.boundary > 0 { if self.boundary > 0 {
( (&self.components[0..self.boundary],
&self.components[0..self.boundary], &self.components[(self.boundary + 1)..])
&self.components[(self.boundary + 1)..],
)
} else { } else {
(&self.components[0..0], &self.components) (&self.components[0..0],
&self.components)
} }
} }
} }
@ -167,7 +163,7 @@ impl PartialOrd for NamespaceableName {
(_, _) => { (_, _) => {
// Just use a lexicographic ordering. // Just use a lexicographic ordering.
self.components().partial_cmp(&other.components()) self.components().partial_cmp(&other.components())
} },
} }
} }
} }
@ -182,9 +178,9 @@ impl Ord for NamespaceableName {
impl fmt::Debug for NamespaceableName { impl fmt::Debug for NamespaceableName {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("NamespaceableName") fmt.debug_struct("NamespaceableName")
.field("namespace", &self.namespace()) .field("namespace", &self.namespace())
.field("name", &self.name()) .field("name", &self.name())
.finish() .finish()
} }
} }
@ -205,8 +201,8 @@ impl fmt::Display for NamespaceableName {
// friendly and automatic (e.g. `derive`d), and just pass all work off to it in our custom // friendly and automatic (e.g. `derive`d), and just pass all work off to it in our custom
// implementation of Serialize and Deserialize. // implementation of Serialize and Deserialize.
#[cfg(feature = "serde_support")] #[cfg(feature = "serde_support")]
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde_support", serde(rename = "NamespaceableName"))] #[cfg_attr(feature = "serde_support", serde(rename = "NamespaceableName"))]
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
struct SerializedNamespaceableName<'a> { struct SerializedNamespaceableName<'a> {
namespace: Option<&'a str>, namespace: Option<&'a str>,
name: &'a str, name: &'a str,
@ -214,19 +210,14 @@ struct SerializedNamespaceableName<'a> {
#[cfg(feature = "serde_support")] #[cfg(feature = "serde_support")]
impl<'de> Deserialize<'de> for NamespaceableName { impl<'de> Deserialize<'de> for NamespaceableName {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer<'de> {
where
D: Deserializer<'de>,
{
let separated = SerializedNamespaceableName::deserialize(deserializer)?; let separated = SerializedNamespaceableName::deserialize(deserializer)?;
if separated.name.is_empty() { if separated.name.len() == 0 {
return Err(de::Error::custom("Empty name in keyword or symbol")); return Err(de::Error::custom("Empty name in keyword or symbol"));
} }
if let Some(ns) = separated.namespace { if let Some(ns) = separated.namespace {
if ns.is_empty() { if ns.len() == 0 {
Err(de::Error::custom( Err(de::Error::custom("Empty but present namespace in keyword or symbol"))
"Empty but present namespace in keyword or symbol",
))
} else { } else {
Ok(NamespaceableName::namespaced(ns, separated.name)) Ok(NamespaceableName::namespaced(ns, separated.name))
} }
@ -238,10 +229,7 @@ impl<'de> Deserialize<'de> for NamespaceableName {
#[cfg(feature = "serde_support")] #[cfg(feature = "serde_support")]
impl Serialize for NamespaceableName { impl Serialize for NamespaceableName {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer {
where
S: Serializer,
{
let ser = SerializedNamespaceableName { let ser = SerializedNamespaceableName {
namespace: self.namespace(), namespace: self.namespace(),
name: self.name(), name: self.name(),
@ -257,18 +245,12 @@ mod test {
#[test] #[test]
fn test_new_invariants_maintained() { fn test_new_invariants_maintained() {
assert!( assert!(panic::catch_unwind(|| NamespaceableName::namespaced("", "foo")).is_err(),
panic::catch_unwind(|| NamespaceableName::namespaced("", "foo")).is_err(), "Empty namespace should panic");
"Empty namespace should panic" assert!(panic::catch_unwind(|| NamespaceableName::namespaced("foo", "")).is_err(),
); "Empty name should panic");
assert!( assert!(panic::catch_unwind(|| NamespaceableName::namespaced("", "")).is_err(),
panic::catch_unwind(|| NamespaceableName::namespaced("foo", "")).is_err(), "Should panic if both fields are empty");
"Empty name should panic"
);
assert!(
panic::catch_unwind(|| NamespaceableName::namespaced("", "")).is_err(),
"Should panic if both fields are empty"
);
} }
#[test] #[test]
@ -304,11 +286,19 @@ mod test {
n3.clone(), n3.clone(),
n2.clone(), n2.clone(),
n1.clone(), n1.clone(),
n4.clone(), n4.clone()
]; ];
arr.sort(); arr.sort();
assert_eq!(arr, [n0, n2, n1, n3, n4, n5, n6,]); assert_eq!(arr, [
n0.clone(),
n2.clone(),
n1.clone(),
n3.clone(),
n4.clone(),
n5.clone(),
n6.clone(),
]);
} }
} }

View file

@ -8,15 +8,17 @@
// CONDITIONS OF ANY KIND, either express or implied. See the License for the // CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License. // specific language governing permissions and limitations under the License.
use chrono::SecondsFormat; use chrono::{
SecondsFormat,
};
use itertools::Itertools; use itertools::Itertools;
use pretty; use pretty;
use std::borrow::Cow;
use std::io; use std::io;
use std::borrow::Cow;
use crate::types::Value; use types::Value;
impl Value { impl Value {
/// Return a pretty string representation of this `Value`. /// Return a pretty string representation of this `Value`.
@ -27,10 +29,7 @@ impl Value {
} }
/// Write a pretty representation of this `Value` to the given writer. /// Write a pretty representation of this `Value` to the given writer.
fn write_pretty<W>(&self, width: usize, out: &mut W) -> Result<(), io::Error> fn write_pretty<W>(&self, width: usize, out: &mut W) -> Result<(), io::Error> where W: io::Write {
where
W: io::Write,
{
self.as_doc(&pretty::BoxAllocator).1.render(width, out) self.as_doc(&pretty::BoxAllocator).1.render(width, out)
} }
@ -42,54 +41,28 @@ impl Value {
/// [1, /// [1,
/// 2, /// 2,
/// 3]. /// 3].
fn bracket<'a, A, T, I>( fn bracket<'a, A, T, I>(&'a self, allocator: &'a A, open: T, vs: I, close: T) -> pretty::DocBuilder<'a, A>
&'a self, where A: pretty::DocAllocator<'a>, T: Into<Cow<'a, str>>, I: IntoIterator<Item=&'a Value> {
allocator: &'a A,
open: T,
vs: I,
close: T,
) -> pretty::DocBuilder<'a, A>
where
A: pretty::DocAllocator<'a>,
<A as pretty::DocAllocator<'a>>::Doc: std::clone::Clone,
T: Into<Cow<'a, str>>,
I: IntoIterator<Item = &'a Value>,
{
let open = open.into(); let open = open.into();
let n = open.len() as isize; let n = open.len();
let i = { let i = vs.into_iter().map(|v| v.as_doc(allocator)).intersperse(allocator.space());
let this = vs.into_iter().map(|v| v.as_doc(allocator)); allocator.text(open)
let element = allocator.line();
Itertools::intersperse(this, element)
};
allocator
.text(open)
.append(allocator.concat(i).nest(n)) .append(allocator.concat(i).nest(n))
.append(allocator.text(close)) .append(allocator.text(close))
.group() .group()
} }
/// Recursively traverses this value and creates a pretty.rs document. /// Recursively traverses this value and creates a pretty.rs document.
/// A pretty printing implementation for edn queries optimized for /// This pretty printing implementation is optimized for edn queries
/// readability and limited whitespace expansion. /// readability and limited whitespace expansion.
fn as_doc<'a, A>(&'a self, pp: &'a A) -> pretty::DocBuilder<'a, A> fn as_doc<'a, A>(&'a self, pp: &'a A) -> pretty::DocBuilder<'a, A>
where where A: pretty::DocAllocator<'a> {
A: pretty::DocAllocator<'a>,
<A as pretty::DocAllocator<'a>>::Doc: std::clone::Clone,
{
match *self { match *self {
Value::Vector(ref vs) => self.bracket(pp, "[", vs, "]"), Value::Vector(ref vs) => self.bracket(pp, "[", vs, "]"),
Value::List(ref vs) => self.bracket(pp, "(", vs, ")"), Value::List(ref vs) => self.bracket(pp, "(", vs, ")"),
Value::Set(ref vs) => self.bracket(pp, "#{", vs, "}"), Value::Set(ref vs) => self.bracket(pp, "#{", vs, "}"),
Value::Map(ref vs) => { Value::Map(ref vs) => {
let xs = { let xs = vs.iter().rev().map(|(k, v)| k.as_doc(pp).append(pp.space()).append(v.as_doc(pp)).group()).intersperse(pp.space());
let this = vs
.iter()
.rev()
.map(|(k, v)| k.as_doc(pp).append(pp.line()).append(v.as_doc(pp)).group());
let element = pp.line();
Itertools::intersperse(this, element)
};
pp.text("{") pp.text("{")
.append(pp.concat(xs).nest(1)) .append(pp.concat(xs).nest(1))
.append(pp.text("}")) .append(pp.text("}"))
@ -99,22 +72,16 @@ impl Value {
Value::PlainSymbol(ref v) => pp.text(v.to_string()), Value::PlainSymbol(ref v) => pp.text(v.to_string()),
Value::Keyword(ref v) => pp.text(v.to_string()), Value::Keyword(ref v) => pp.text(v.to_string()),
Value::Text(ref v) => pp.text("\"").append(v.as_str()).append("\""), Value::Text(ref v) => pp.text("\"").append(v.as_str()).append("\""),
Value::Uuid(ref u) => pp Value::Uuid(ref u) => pp.text("#uuid \"").append(u.hyphenated().to_string()).append("\""),
.text("#uuid \"") Value::Instant(ref v) => pp.text("#inst \"").append(v.to_rfc3339_opts(SecondsFormat::AutoSi, true)).append("\""),
.append(u.hyphenated().to_string()) _ => pp.text(self.to_string())
.append("\""),
Value::Instant(ref v) => pp
.text("#inst \"")
.append(v.to_rfc3339_opts(SecondsFormat::AutoSi, true))
.append("\""),
_ => pp.text(self.to_string()),
} }
} }
} }
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use crate::parse; use parse;
#[test] #[test]
fn test_pp_io() { fn test_pp_io() {
@ -138,16 +105,13 @@ mod test {
let data = parse::value(string).unwrap().without_spans(); let data = parse::value(string).unwrap().without_spans();
assert_eq!(data.to_pretty(20).unwrap(), "[1 2 3 4 5 6]"); assert_eq!(data.to_pretty(20).unwrap(), "[1 2 3 4 5 6]");
assert_eq!( assert_eq!(data.to_pretty(10).unwrap(), "\
data.to_pretty(10).unwrap(),
"\
[1 [1
2 2
3 3
4 4
5 5
6]" 6]");
);
} }
#[test] #[test]
@ -156,13 +120,10 @@ mod test {
let data = parse::value(string).unwrap().without_spans(); let data = parse::value(string).unwrap().without_spans();
assert_eq!(data.to_pretty(20).unwrap(), "{:a 1 :b 2 :c 3}"); assert_eq!(data.to_pretty(20).unwrap(), "{:a 1 :b 2 :c 3}");
assert_eq!( assert_eq!(data.to_pretty(10).unwrap(), "\
data.to_pretty(10).unwrap(),
"\
{:a 1 {:a 1
:b 2 :b 2
:c 3}" :c 3}");
);
} }
#[test] #[test]
@ -170,9 +131,7 @@ mod test {
let string = "[ 1 2 ( 3.14 ) #{ 4N } { foo/bar 42 :baz/boz 43 } [ ] :five :six/seven eight nine/ten true false nil #f NaN #f -Infinity #f +Infinity ]"; let string = "[ 1 2 ( 3.14 ) #{ 4N } { foo/bar 42 :baz/boz 43 } [ ] :five :six/seven eight nine/ten true false nil #f NaN #f -Infinity #f +Infinity ]";
let data = parse::value(string).unwrap().without_spans(); let data = parse::value(string).unwrap().without_spans();
assert_eq!( assert_eq!(data.to_pretty(40).unwrap(), "\
data.to_pretty(40).unwrap(),
"\
[1 [1
2 2
(3.14) (3.14)
@ -188,8 +147,7 @@ mod test {
nil nil
#f NaN #f NaN
#f -Infinity #f -Infinity
#f +Infinity]" #f +Infinity]");
);
} }
#[test] #[test]
@ -197,9 +155,7 @@ mod test {
let string = "[:find ?id ?bar ?baz :in $ :where [?id :session/keyword-foo ?symbol1 ?symbol2 \"some string\"] [?tx :db/tx ?ts]]"; let string = "[:find ?id ?bar ?baz :in $ :where [?id :session/keyword-foo ?symbol1 ?symbol2 \"some string\"] [?tx :db/tx ?ts]]";
let data = parse::value(string).unwrap().without_spans(); let data = parse::value(string).unwrap().without_spans();
assert_eq!( assert_eq!(data.to_pretty(40).unwrap(), "\
data.to_pretty(40).unwrap(),
"\
[:find [:find
?id ?id
?bar ?bar
@ -212,8 +168,7 @@ mod test {
?symbol1 ?symbol1
?symbol2 ?symbol2
\"some string\"] \"some string\"]
[?tx :db/tx ?ts]]" [?tx :db/tx ?ts]]");
);
} }
#[test] #[test]
@ -221,9 +176,7 @@ mod test {
let string = "[:find [?id ?bar ?baz] :in [$] :where [?id :session/keyword-foo ?symbol1 ?symbol2 \"some string\"] [?tx :db/tx ?ts] (not-join [?id] [?id :session/keyword-bar _])]"; let string = "[:find [?id ?bar ?baz] :in [$] :where [?id :session/keyword-foo ?symbol1 ?symbol2 \"some string\"] [?tx :db/tx ?ts] (not-join [?id] [?id :session/keyword-bar _])]";
let data = parse::value(string).unwrap().without_spans(); let data = parse::value(string).unwrap().without_spans();
assert_eq!( assert_eq!(data.to_pretty(40).unwrap(), "\
data.to_pretty(40).unwrap(),
"\
[:find [:find
[?id ?bar ?baz] [?id ?bar ?baz]
:in :in
@ -237,7 +190,6 @@ mod test {
[?tx :db/tx ?ts] [?tx :db/tx ?ts]
(not-join (not-join
[?id] [?id]
[?id :session/keyword-bar _])]" [?id :session/keyword-bar _])]");
);
} }
} }

View file

@ -29,19 +29,37 @@
///! inner type directly in conjunction with matching on the enum. Before diving ///! inner type directly in conjunction with matching on the enum. Before diving
///! deeply into this it's worth recognizing that this loss of 'sovereignty' is ///! deeply into this it's worth recognizing that this loss of 'sovereignty' is
///! a tradeoff against well-typed function signatures and other such boundaries. ///! a tradeoff against well-typed function signatures and other such boundaries.
use std::collections::{BTreeSet, HashSet};
use std::collections::{
BTreeSet,
HashSet,
};
use std; use std;
use std::fmt; use std::fmt;
use std::rc::Rc; use std::rc::{
Rc,
};
use crate::{BigInt, DateTime, OrderedFloat, Utc, Uuid}; use ::{
BigInt,
DateTime,
OrderedFloat,
Uuid,
Utc,
};
use crate::value_rc::{FromRc, ValueRc}; use ::value_rc::{
FromRc,
ValueRc,
};
pub use crate::{Keyword, PlainSymbol}; pub use ::{
Keyword,
PlainSymbol,
};
pub type SrcVarName = String; // Do not include the required syntactic '$'. pub type SrcVarName = String; // Do not include the required syntactic '$'.
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] #[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Variable(pub Rc<PlainSymbol>); pub struct Variable(pub Rc<PlainSymbol>);
@ -51,6 +69,10 @@ impl Variable {
self.0.as_ref().0.as_str() self.0.as_ref().0.as_str()
} }
pub fn to_string(&self) -> String {
self.0.as_ref().0.clone()
}
pub fn name(&self) -> PlainSymbol { pub fn name(&self) -> PlainSymbol {
self.0.as_ref().clone() self.0.as_ref().clone()
} }
@ -64,15 +86,15 @@ impl Variable {
} }
pub trait FromValue<T> { pub trait FromValue<T> {
fn from_value(v: &crate::ValueAndSpan) -> Option<T>; fn from_value(v: &::ValueAndSpan) -> Option<T>;
} }
/// If the provided EDN value is a PlainSymbol beginning with '?', return /// If the provided EDN value is a PlainSymbol beginning with '?', return
/// it wrapped in a Variable. If not, return None. /// it wrapped in a Variable. If not, return None.
/// TODO: intern strings. #398. /// TODO: intern strings. #398.
impl FromValue<Variable> for Variable { impl FromValue<Variable> for Variable {
fn from_value(v: &crate::ValueAndSpan) -> Option<Variable> { fn from_value(v: &::ValueAndSpan) -> Option<Variable> {
if let crate::SpannedValue::PlainSymbol(ref s) = v.inner { if let ::SpannedValue::PlainSymbol(ref s) = v.inner {
Variable::from_symbol(s) Variable::from_symbol(s)
} else { } else {
None None
@ -83,7 +105,7 @@ impl FromValue<Variable> for Variable {
impl Variable { impl Variable {
pub fn from_rc(sym: Rc<PlainSymbol>) -> Option<Variable> { pub fn from_rc(sym: Rc<PlainSymbol>) -> Option<Variable> {
if sym.is_var_symbol() { if sym.is_var_symbol() {
Some(Variable(sym)) Some(Variable(sym.clone()))
} else { } else {
None None
} }
@ -115,8 +137,8 @@ impl std::fmt::Display for Variable {
pub struct QueryFunction(pub PlainSymbol); pub struct QueryFunction(pub PlainSymbol);
impl FromValue<QueryFunction> for QueryFunction { impl FromValue<QueryFunction> for QueryFunction {
fn from_value(v: &crate::ValueAndSpan) -> Option<QueryFunction> { fn from_value(v: &::ValueAndSpan) -> Option<QueryFunction> {
if let crate::SpannedValue::PlainSymbol(ref s) = v.inner { if let ::SpannedValue::PlainSymbol(ref s) = v.inner {
QueryFunction::from_symbol(s) QueryFunction::from_symbol(s)
} else { } else {
None None
@ -145,7 +167,7 @@ pub enum Direction {
/// An abstract declaration of ordering: direction and variable. /// An abstract declaration of ordering: direction and variable.
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq)]
pub struct Order(pub Direction, pub Variable); // Future: Element instead of Variable? pub struct Order(pub Direction, pub Variable); // Future: Element instead of Variable?
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum SrcVar { pub enum SrcVar {
@ -154,8 +176,8 @@ pub enum SrcVar {
} }
impl FromValue<SrcVar> for SrcVar { impl FromValue<SrcVar> for SrcVar {
fn from_value(v: &crate::ValueAndSpan) -> Option<SrcVar> { fn from_value(v: &::ValueAndSpan) -> Option<SrcVar> {
if let crate::SpannedValue::PlainSymbol(ref s) = v.inner { if let ::SpannedValue::PlainSymbol(ref s) = v.inner {
SrcVar::from_symbol(s) SrcVar::from_symbol(s)
} else { } else {
None None
@ -213,27 +235,37 @@ pub enum FnArg {
} }
impl FromValue<FnArg> for FnArg { impl FromValue<FnArg> for FnArg {
fn from_value(v: &crate::ValueAndSpan) -> Option<FnArg> { fn from_value(v: &::ValueAndSpan) -> Option<FnArg> {
use crate::SpannedValue::*; use ::SpannedValue::*;
match v.inner { match v.inner {
Integer(x) => Some(FnArg::EntidOrInteger(x)), Integer(x) =>
PlainSymbol(ref x) if x.is_src_symbol() => SrcVar::from_symbol(x).map(FnArg::SrcVar), Some(FnArg::EntidOrInteger(x)),
PlainSymbol(ref x) if x.is_var_symbol() => { PlainSymbol(ref x) if x.is_src_symbol() =>
Variable::from_symbol(x).map(FnArg::Variable) SrcVar::from_symbol(x).map(FnArg::SrcVar),
} PlainSymbol(ref x) if x.is_var_symbol() =>
Variable::from_symbol(x).map(FnArg::Variable),
PlainSymbol(_) => None, PlainSymbol(_) => None,
Keyword(ref x) => Some(FnArg::IdentOrKeyword(x.clone())), Keyword(ref x) =>
Instant(x) => Some(FnArg::Constant(NonIntegerConstant::Instant(x))), Some(FnArg::IdentOrKeyword(x.clone())),
Uuid(x) => Some(FnArg::Constant(NonIntegerConstant::Uuid(x))), Instant(x) =>
Boolean(x) => Some(FnArg::Constant(NonIntegerConstant::Boolean(x))), Some(FnArg::Constant(NonIntegerConstant::Instant(x))),
Float(x) => Some(FnArg::Constant(NonIntegerConstant::Float(x))), Uuid(x) =>
BigInteger(ref x) => Some(FnArg::Constant(NonIntegerConstant::BigInteger(x.clone()))), Some(FnArg::Constant(NonIntegerConstant::Uuid(x))),
Boolean(x) =>
Some(FnArg::Constant(NonIntegerConstant::Boolean(x))),
Float(x) =>
Some(FnArg::Constant(NonIntegerConstant::Float(x))),
BigInteger(ref x) =>
Some(FnArg::Constant(NonIntegerConstant::BigInteger(x.clone()))),
Text(ref x) => Text(ref x) =>
// TODO: intern strings. #398. // TODO: intern strings. #398.
{ Some(FnArg::Constant(x.clone().into())),
Some(FnArg::Constant(x.clone().into())) Nil |
} NamespacedSymbol(_) |
Nil | NamespacedSymbol(_) | Vector(_) | List(_) | Set(_) | Map(_) | Bytes(_) => None, Vector(_) |
List(_) |
Set(_) |
Map(_) => None,
} }
} }
} }
@ -242,18 +274,18 @@ impl FromValue<FnArg> for FnArg {
impl std::fmt::Display for FnArg { impl std::fmt::Display for FnArg {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self { match self {
FnArg::Variable(ref var) => write!(f, "{}", var), &FnArg::Variable(ref var) => write!(f, "{}", var),
FnArg::SrcVar(ref var) => { &FnArg::SrcVar(ref var) => {
if var == &SrcVar::DefaultSrc { if var == &SrcVar::DefaultSrc {
write!(f, "$") write!(f, "$")
} else { } else {
write!(f, "{:?}", var) write!(f, "{:?}", var)
} }
} },
FnArg::EntidOrInteger(entid) => write!(f, "{}", entid), &FnArg::EntidOrInteger(entid) => write!(f, "{}", entid),
FnArg::IdentOrKeyword(ref kw) => write!(f, "{}", kw), &FnArg::IdentOrKeyword(ref kw) => write!(f, "{}", kw),
FnArg::Constant(ref constant) => write!(f, "{:?}", constant), &FnArg::Constant(ref constant) => write!(f, "{:?}", constant),
FnArg::Vector(ref vec) => write!(f, "{:?}", vec), &FnArg::Vector(ref vec) => write!(f, "{:?}", vec),
} }
} }
} }
@ -261,7 +293,7 @@ impl std::fmt::Display for FnArg {
impl FnArg { impl FnArg {
pub fn as_variable(&self) -> Option<&Variable> { pub fn as_variable(&self) -> Option<&Variable> {
match self { match self {
FnArg::Variable(ref v) => Some(v), &FnArg::Variable(ref v) => Some(v),
_ => None, _ => None,
} }
} }
@ -277,7 +309,7 @@ impl FnArg {
pub enum PatternNonValuePlace { pub enum PatternNonValuePlace {
Placeholder, Placeholder,
Variable(Variable), Variable(Variable),
Entid(i64), // Will always be +ve. See #190. Entid(i64), // Will always be +ve. See #190.
Ident(ValueRc<Keyword>), Ident(ValueRc<Keyword>),
} }
@ -300,41 +332,40 @@ impl PatternNonValuePlace {
match self { match self {
PatternNonValuePlace::Placeholder => PatternValuePlace::Placeholder, PatternNonValuePlace::Placeholder => PatternValuePlace::Placeholder,
PatternNonValuePlace::Variable(x) => PatternValuePlace::Variable(x), PatternNonValuePlace::Variable(x) => PatternValuePlace::Variable(x),
PatternNonValuePlace::Entid(x) => PatternValuePlace::EntidOrInteger(x), PatternNonValuePlace::Entid(x) => PatternValuePlace::EntidOrInteger(x),
PatternNonValuePlace::Ident(x) => PatternValuePlace::IdentOrKeyword(x), PatternNonValuePlace::Ident(x) => PatternValuePlace::IdentOrKeyword(x),
} }
} }
fn to_pattern_value_place(&self) -> PatternValuePlace { fn to_pattern_value_place(&self) -> PatternValuePlace {
match *self { match *self {
PatternNonValuePlace::Placeholder => PatternValuePlace::Placeholder, PatternNonValuePlace::Placeholder => PatternValuePlace::Placeholder,
PatternNonValuePlace::Variable(ref x) => PatternValuePlace::Variable(x.clone()), PatternNonValuePlace::Variable(ref x) => PatternValuePlace::Variable(x.clone()),
PatternNonValuePlace::Entid(x) => PatternValuePlace::EntidOrInteger(x), PatternNonValuePlace::Entid(x) => PatternValuePlace::EntidOrInteger(x),
PatternNonValuePlace::Ident(ref x) => PatternValuePlace::IdentOrKeyword(x.clone()), PatternNonValuePlace::Ident(ref x) => PatternValuePlace::IdentOrKeyword(x.clone()),
} }
} }
} }
impl FromValue<PatternNonValuePlace> for PatternNonValuePlace { impl FromValue<PatternNonValuePlace> for PatternNonValuePlace {
fn from_value(v: &crate::ValueAndSpan) -> Option<PatternNonValuePlace> { fn from_value(v: &::ValueAndSpan) -> Option<PatternNonValuePlace> {
match v.inner { match v.inner {
crate::SpannedValue::Integer(x) => { ::SpannedValue::Integer(x) => if x >= 0 {
if x >= 0 { Some(PatternNonValuePlace::Entid(x))
Some(PatternNonValuePlace::Entid(x)) } else {
} else { None
None },
} ::SpannedValue::PlainSymbol(ref x) => if x.0.as_str() == "_" {
} Some(PatternNonValuePlace::Placeholder)
crate::SpannedValue::PlainSymbol(ref x) => { } else {
if x.0.as_str() == "_" { if let Some(v) = Variable::from_symbol(x) {
Some(PatternNonValuePlace::Placeholder)
} else if let Some(v) = Variable::from_symbol(x) {
Some(PatternNonValuePlace::Variable(v)) Some(PatternNonValuePlace::Variable(v))
} else { } else {
None None
} }
} },
crate::SpannedValue::Keyword(ref x) => Some(x.clone().into()), ::SpannedValue::Keyword(ref x) =>
Some(x.clone().into()),
_ => None, _ => None,
} }
} }
@ -371,46 +402,38 @@ impl From<Keyword> for PatternValuePlace {
} }
impl FromValue<PatternValuePlace> for PatternValuePlace { impl FromValue<PatternValuePlace> for PatternValuePlace {
fn from_value(v: &crate::ValueAndSpan) -> Option<PatternValuePlace> { fn from_value(v: &::ValueAndSpan) -> Option<PatternValuePlace> {
match v.inner { match v.inner {
crate::SpannedValue::Integer(x) => Some(PatternValuePlace::EntidOrInteger(x)), ::SpannedValue::Integer(x) =>
crate::SpannedValue::PlainSymbol(ref x) if x.0.as_str() == "_" => { Some(PatternValuePlace::EntidOrInteger(x)),
Some(PatternValuePlace::Placeholder) ::SpannedValue::PlainSymbol(ref x) if x.0.as_str() == "_" =>
} Some(PatternValuePlace::Placeholder),
crate::SpannedValue::PlainSymbol(ref x) => { ::SpannedValue::PlainSymbol(ref x) =>
Variable::from_symbol(x).map(PatternValuePlace::Variable) Variable::from_symbol(x).map(PatternValuePlace::Variable),
} ::SpannedValue::Keyword(ref x) if x.is_namespaced() =>
crate::SpannedValue::Keyword(ref x) if x.is_namespaced() => Some(x.clone().into()), Some(x.clone().into()),
crate::SpannedValue::Boolean(x) => { ::SpannedValue::Boolean(x) =>
Some(PatternValuePlace::Constant(NonIntegerConstant::Boolean(x))) Some(PatternValuePlace::Constant(NonIntegerConstant::Boolean(x))),
} ::SpannedValue::Float(x) =>
crate::SpannedValue::Float(x) => { Some(PatternValuePlace::Constant(NonIntegerConstant::Float(x))),
Some(PatternValuePlace::Constant(NonIntegerConstant::Float(x))) ::SpannedValue::BigInteger(ref x) =>
} Some(PatternValuePlace::Constant(NonIntegerConstant::BigInteger(x.clone()))),
crate::SpannedValue::BigInteger(ref x) => Some(PatternValuePlace::Constant( ::SpannedValue::Instant(x) =>
NonIntegerConstant::BigInteger(x.clone()), Some(PatternValuePlace::Constant(NonIntegerConstant::Instant(x))),
)), ::SpannedValue::Text(ref x) =>
crate::SpannedValue::Instant(x) => { // TODO: intern strings. #398.
Some(PatternValuePlace::Constant(NonIntegerConstant::Instant(x))) Some(PatternValuePlace::Constant(x.clone().into())),
} ::SpannedValue::Uuid(ref u) =>
crate::SpannedValue::Text(ref x) => Some(PatternValuePlace::Constant(NonIntegerConstant::Uuid(u.clone()))),
// TODO: intern strings. #398.
{
Some(PatternValuePlace::Constant(x.clone().into()))
}
crate::SpannedValue::Uuid(ref u) => {
Some(PatternValuePlace::Constant(NonIntegerConstant::Uuid(*u)))
}
// These don't appear in queries. // These don't appear in queries.
crate::SpannedValue::Nil => None, ::SpannedValue::Nil => None,
crate::SpannedValue::NamespacedSymbol(_) => None, ::SpannedValue::NamespacedSymbol(_) => None,
crate::SpannedValue::Keyword(_) => None, // … yet. ::SpannedValue::Keyword(_) => None, // … yet.
crate::SpannedValue::Map(_) => None, ::SpannedValue::Map(_) => None,
crate::SpannedValue::List(_) => None, ::SpannedValue::List(_) => None,
crate::SpannedValue::Set(_) => None, ::SpannedValue::Set(_) => None,
crate::SpannedValue::Vector(_) => None, ::SpannedValue::Vector(_) => None,
crate::SpannedValue::Bytes(_) => None,
} }
} }
} }
@ -420,35 +443,29 @@ impl PatternValuePlace {
#[allow(dead_code)] #[allow(dead_code)]
fn into_pattern_non_value_place(self) -> Option<PatternNonValuePlace> { fn into_pattern_non_value_place(self) -> Option<PatternNonValuePlace> {
match self { match self {
PatternValuePlace::Placeholder => Some(PatternNonValuePlace::Placeholder), PatternValuePlace::Placeholder => Some(PatternNonValuePlace::Placeholder),
PatternValuePlace::Variable(x) => Some(PatternNonValuePlace::Variable(x)), PatternValuePlace::Variable(x) => Some(PatternNonValuePlace::Variable(x)),
PatternValuePlace::EntidOrInteger(x) => { PatternValuePlace::EntidOrInteger(x) => if x >= 0 {
if x >= 0 { Some(PatternNonValuePlace::Entid(x))
Some(PatternNonValuePlace::Entid(x)) } else {
} else { None
None },
}
}
PatternValuePlace::IdentOrKeyword(x) => Some(PatternNonValuePlace::Ident(x)), PatternValuePlace::IdentOrKeyword(x) => Some(PatternNonValuePlace::Ident(x)),
PatternValuePlace::Constant(_) => None, PatternValuePlace::Constant(_) => None,
} }
} }
fn to_pattern_non_value_place(&self) -> Option<PatternNonValuePlace> { fn to_pattern_non_value_place(&self) -> Option<PatternNonValuePlace> {
match *self { match *self {
PatternValuePlace::Placeholder => Some(PatternNonValuePlace::Placeholder), PatternValuePlace::Placeholder => Some(PatternNonValuePlace::Placeholder),
PatternValuePlace::Variable(ref x) => Some(PatternNonValuePlace::Variable(x.clone())), PatternValuePlace::Variable(ref x) => Some(PatternNonValuePlace::Variable(x.clone())),
PatternValuePlace::EntidOrInteger(x) => { PatternValuePlace::EntidOrInteger(x) => if x >= 0 {
if x >= 0 { Some(PatternNonValuePlace::Entid(x))
Some(PatternNonValuePlace::Entid(x)) } else {
} else { None
None },
} PatternValuePlace::IdentOrKeyword(ref x) => Some(PatternNonValuePlace::Ident(x.clone())),
} PatternValuePlace::Constant(_) => None,
PatternValuePlace::IdentOrKeyword(ref x) => {
Some(PatternNonValuePlace::Ident(x.clone()))
}
PatternValuePlace::Constant(_) => None,
} }
} }
} }
@ -493,15 +510,19 @@ pub enum PullAttributeSpec {
impl std::fmt::Display for PullConcreteAttribute { impl std::fmt::Display for PullConcreteAttribute {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self { match self {
PullConcreteAttribute::Ident(ref k) => write!(f, "{}", k), &PullConcreteAttribute::Ident(ref k) => {
PullConcreteAttribute::Entid(i) => write!(f, "{}", i), write!(f, "{}", k)
},
&PullConcreteAttribute::Entid(i) => {
write!(f, "{}", i)
},
} }
} }
} }
impl std::fmt::Display for NamedPullAttribute { impl std::fmt::Display for NamedPullAttribute {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
if let Some(ref alias) = self.alias { if let &Some(ref alias) = &self.alias {
write!(f, "{} :as {}", self.attribute, alias) write!(f, "{} :as {}", self.attribute, alias)
} else { } else {
write!(f, "{}", self.attribute) write!(f, "{}", self.attribute)
@ -509,28 +530,34 @@ impl std::fmt::Display for NamedPullAttribute {
} }
} }
impl std::fmt::Display for PullAttributeSpec { impl std::fmt::Display for PullAttributeSpec {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self { match self {
PullAttributeSpec::Wildcard => write!(f, "*"), &PullAttributeSpec::Wildcard => {
PullAttributeSpec::Attribute(ref attr) => write!(f, "{}", attr), write!(f, "*")
},
&PullAttributeSpec::Attribute(ref attr) => {
write!(f, "{}", attr)
},
} }
} }
} }
#[derive(Clone, Debug, Eq, PartialEq)]
#[derive(Debug, Eq, PartialEq)]
pub struct Pull { pub struct Pull {
pub var: Variable, pub var: Variable,
pub patterns: Vec<PullAttributeSpec>, pub patterns: Vec<PullAttributeSpec>,
} }
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Debug, Eq, PartialEq)]
pub struct Aggregate { pub struct Aggregate {
pub func: QueryFunction, pub func: QueryFunction,
pub args: Vec<FnArg>, pub args: Vec<FnArg>,
} }
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Debug, Eq, PartialEq)]
pub enum Element { pub enum Element {
Variable(Variable), Variable(Variable),
Aggregate(Aggregate), Aggregate(Aggregate),
@ -548,10 +575,10 @@ impl Element {
/// Returns true if the element must yield only one value. /// Returns true if the element must yield only one value.
pub fn is_unit(&self) -> bool { pub fn is_unit(&self) -> bool {
match self { match self {
Element::Variable(_) => false, &Element::Variable(_) => false,
Element::Pull(_) => false, &Element::Pull(_) => false,
Element::Aggregate(_) => true, &Element::Aggregate(_) => true,
Element::Corresponding(_) => true, &Element::Corresponding(_) => true,
} }
} }
} }
@ -565,23 +592,26 @@ impl From<Variable> for Element {
impl std::fmt::Display for Element { impl std::fmt::Display for Element {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self { match self {
Element::Variable(ref var) => write!(f, "{}", var), &Element::Variable(ref var) => {
Element::Pull(Pull { write!(f, "{}", var)
ref var, },
ref patterns, &Element::Pull(Pull { ref var, ref patterns }) => {
}) => {
write!(f, "(pull {} [ ", var)?; write!(f, "(pull {} [ ", var)?;
for p in patterns.iter() { for p in patterns.iter() {
write!(f, "{} ", p)?; write!(f, "{} ", p)?;
} }
write!(f, "])") write!(f, "])")
}
Element::Aggregate(ref agg) => match agg.args.len() {
0 => write!(f, "({})", agg.func),
1 => write!(f, "({} {})", agg.func, agg.args[0]),
_ => write!(f, "({} {:?})", agg.func, agg.args),
}, },
Element::Corresponding(ref var) => write!(f, "(the {})", var), &Element::Aggregate(ref agg) => {
match agg.args.len() {
0 => write!(f, "({})", agg.func),
1 => write!(f, "({} {})", agg.func, agg.args[0]),
_ => write!(f, "({} {:?})", agg.func, agg.args),
}
},
&Element::Corresponding(ref var) => {
write!(f, "(the {})", var)
},
} }
} }
} }
@ -604,18 +634,23 @@ pub enum Limit {
/// ///
/// ```rust /// ```rust
/// # use edn::query::{Element, FindSpec, Variable}; /// # use edn::query::{Element, FindSpec, Variable};
/// let elements = vec![
/// Element::Variable(Variable::from_valid_name("?foo")),
/// Element::Variable(Variable::from_valid_name("?bar")),
/// ];
/// let rel = FindSpec::FindRel(elements);
/// ///
/// if let FindSpec::FindRel(elements) = rel { /// # fn main() {
/// assert_eq!(2, elements.len()); ///
/// } /// let elements = vec![
/// Element::Variable(Variable::from_valid_name("?foo")),
/// Element::Variable(Variable::from_valid_name("?bar")),
/// ];
/// let rel = FindSpec::FindRel(elements);
///
/// if let FindSpec::FindRel(elements) = rel {
/// assert_eq!(2, elements.len());
/// }
///
/// # }
/// ``` /// ```
/// ///
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Debug, Eq, PartialEq)]
pub enum FindSpec { pub enum FindSpec {
/// Returns an array of arrays, represented as a single array with length a multiple of width. /// Returns an array of arrays, represented as a single array with length a multiple of width.
FindRel(Vec<Element>), FindRel(Vec<Element>),
@ -639,22 +674,23 @@ impl FindSpec {
pub fn is_unit_limited(&self) -> bool { pub fn is_unit_limited(&self) -> bool {
use self::FindSpec::*; use self::FindSpec::*;
match self { match self {
FindScalar(..) => true, &FindScalar(..) => true,
FindTuple(..) => true, &FindTuple(..) => true,
FindRel(..) => false, &FindRel(..) => false,
FindColl(..) => false, &FindColl(..) => false,
} }
} }
pub fn expected_column_count(&self) -> usize { pub fn expected_column_count(&self) -> usize {
use self::FindSpec::*; use self::FindSpec::*;
match self { match self {
FindScalar(..) => 1, &FindScalar(..) => 1,
FindColl(..) => 1, &FindColl(..) => 1,
FindTuple(ref elems) | &FindRel(ref elems) => elems.len(), &FindTuple(ref elems) | &FindRel(ref elems) => elems.len(),
} }
} }
/// Returns true if the provided `FindSpec` cares about distinct results. /// Returns true if the provided `FindSpec` cares about distinct results.
/// ///
/// I use the words "cares about" because find is generally defined in terms of producing distinct /// I use the words "cares about" because find is generally defined in terms of producing distinct
@ -677,13 +713,13 @@ impl FindSpec {
!self.is_unit_limited() !self.is_unit_limited()
} }
pub fn columns<'s>(&'s self) -> Box<dyn Iterator<Item = &Element> + 's> { pub fn columns<'s>(&'s self) -> Box<Iterator<Item=&Element> + 's> {
use self::FindSpec::*; use self::FindSpec::*;
match self { match self {
FindScalar(ref e) => Box::new(std::iter::once(e)), &FindScalar(ref e) => Box::new(std::iter::once(e)),
FindColl(ref e) => Box::new(std::iter::once(e)), &FindColl(ref e) => Box::new(std::iter::once(e)),
FindTuple(ref v) => Box::new(v.iter()), &FindTuple(ref v) => Box::new(v.iter()),
FindRel(ref v) => Box::new(v.iter()), &FindRel(ref v) => Box::new(v.iter()),
} }
} }
} }
@ -706,13 +742,13 @@ impl VariableOrPlaceholder {
pub fn var(&self) -> Option<&Variable> { pub fn var(&self) -> Option<&Variable> {
match self { match self {
VariableOrPlaceholder::Placeholder => None, &VariableOrPlaceholder::Placeholder => None,
VariableOrPlaceholder::Variable(ref var) => Some(var), &VariableOrPlaceholder::Variable(ref var) => Some(var),
} }
} }
} }
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone,Debug,Eq,PartialEq)]
pub enum Binding { pub enum Binding {
BindScalar(Variable), BindScalar(Variable),
BindColl(Variable), BindColl(Variable),
@ -725,9 +761,7 @@ impl Binding {
pub fn variables(&self) -> Vec<Option<Variable>> { pub fn variables(&self) -> Vec<Option<Variable>> {
match self { match self {
&Binding::BindScalar(ref var) | &Binding::BindColl(ref var) => vec![Some(var.clone())], &Binding::BindScalar(ref var) | &Binding::BindColl(ref var) => vec![Some(var.clone())],
&Binding::BindRel(ref vars) | &Binding::BindTuple(ref vars) => { &Binding::BindRel(ref vars) | &Binding::BindTuple(ref vars) => vars.iter().map(|x| x.var().cloned()).collect(),
vars.iter().map(|x| x.var().cloned()).collect()
}
} }
} }
@ -735,9 +769,7 @@ impl Binding {
pub fn is_empty(&self) -> bool { pub fn is_empty(&self) -> bool {
match self { match self {
&Binding::BindScalar(_) | &Binding::BindColl(_) => false, &Binding::BindScalar(_) | &Binding::BindColl(_) => false,
&Binding::BindRel(ref vars) | &Binding::BindTuple(ref vars) => { &Binding::BindRel(ref vars) | &Binding::BindTuple(ref vars) => vars.iter().all(|x| x.var().is_none()),
vars.iter().all(|x| x.var().is_none())
}
} }
} }
@ -761,11 +793,11 @@ impl Binding {
/// ``` /// ```
pub fn is_valid(&self) -> bool { pub fn is_valid(&self) -> bool {
match self { match self {
Binding::BindScalar(_) | &Binding::BindColl(_) => true, &Binding::BindScalar(_) | &Binding::BindColl(_) => true,
Binding::BindRel(ref vars) | &Binding::BindTuple(ref vars) => { &Binding::BindRel(ref vars) | &Binding::BindTuple(ref vars) => {
let mut acc = HashSet::<Variable>::new(); let mut acc = HashSet::<Variable>::new();
for var in vars { for var in vars {
if let VariableOrPlaceholder::Variable(ref var) = *var { if let &VariableOrPlaceholder::Variable(ref var) = var {
if !acc.insert(var.clone()) { if !acc.insert(var.clone()) {
// It's invalid if there was an equal var already present in the set -- // It's invalid if there was an equal var already present in the set --
// i.e., we have a duplicate var. // i.e., we have a duplicate var.
@ -794,22 +826,18 @@ pub struct Pattern {
} }
impl Pattern { impl Pattern {
pub fn simple( pub fn simple(e: PatternNonValuePlace,
e: PatternNonValuePlace, a: PatternNonValuePlace,
a: PatternNonValuePlace, v: PatternValuePlace) -> Option<Pattern> {
v: PatternValuePlace,
) -> Option<Pattern> {
Pattern::new(None, e, a, v, PatternNonValuePlace::Placeholder) Pattern::new(None, e, a, v, PatternNonValuePlace::Placeholder)
} }
pub fn new( pub fn new(src: Option<SrcVar>,
src: Option<SrcVar>, e: PatternNonValuePlace,
e: PatternNonValuePlace, a: PatternNonValuePlace,
a: PatternNonValuePlace, v: PatternValuePlace,
v: PatternValuePlace, tx: PatternNonValuePlace) -> Option<Pattern> {
tx: PatternNonValuePlace, let aa = a.clone(); // Too tired of fighting borrow scope for now.
) -> Option<Pattern> {
let aa = a.clone(); // Too tired of fighting borrow scope for now.
if let PatternNonValuePlace::Ident(ref k) = aa { if let PatternNonValuePlace::Ident(ref k) = aa {
if k.is_backward() { if k.is_backward() {
// e and v have different types; we must convert them. // e and v have different types; we must convert them.
@ -822,7 +850,7 @@ impl Pattern {
entity: v_e, entity: v_e,
attribute: k.to_reversed().into(), attribute: k.to_reversed().into(),
value: e_v, value: e_v,
tx, tx: tx,
}); });
} else { } else {
return None; return None;
@ -834,7 +862,7 @@ impl Pattern {
entity: e, entity: e,
attribute: a, attribute: a,
value: v, value: v,
tx, tx: tx,
}) })
} }
} }
@ -883,7 +911,10 @@ pub enum UnifyVars {
impl WhereClause { impl WhereClause {
pub fn is_pattern(&self) -> bool { pub fn is_pattern(&self) -> bool {
matches!(self, WhereClause::Pattern(_)) match self {
&WhereClause::Pattern(_) => true,
_ => false,
}
} }
} }
@ -896,8 +927,8 @@ pub enum OrWhereClause {
impl OrWhereClause { impl OrWhereClause {
pub fn is_pattern_or_patterns(&self) -> bool { pub fn is_pattern_or_patterns(&self) -> bool {
match self { match self {
OrWhereClause::Clause(WhereClause::Pattern(_)) => true, &OrWhereClause::Clause(WhereClause::Pattern(_)) => true,
OrWhereClause::And(ref clauses) => clauses.iter().all(|clause| clause.is_pattern()), &OrWhereClause::And(ref clauses) => clauses.iter().all(|clause| clause.is_pattern()),
_ => false, _ => false,
} }
} }
@ -921,8 +952,8 @@ pub struct NotJoin {
impl NotJoin { impl NotJoin {
pub fn new(unify_vars: UnifyVars, clauses: Vec<WhereClause>) -> NotJoin { pub fn new(unify_vars: UnifyVars, clauses: Vec<WhereClause>) -> NotJoin {
NotJoin { NotJoin {
unify_vars, unify_vars: unify_vars,
clauses, clauses: clauses,
} }
} }
} }
@ -974,9 +1005,7 @@ pub(crate) enum QueryPart {
/// We split `ParsedQuery` from `FindQuery` because it's not easy to generalize over containers /// We split `ParsedQuery` from `FindQuery` because it's not easy to generalize over containers
/// (here, `Vec` and `BTreeSet`) in Rust. /// (here, `Vec` and `BTreeSet`) in Rust.
impl ParsedQuery { impl ParsedQuery {
pub(crate) fn from_parts( pub(crate) fn from_parts(parts: Vec<QueryPart>) -> std::result::Result<ParsedQuery, &'static str> {
parts: Vec<QueryPart>,
) -> std::result::Result<ParsedQuery, &'static str> {
let mut find_spec: Option<FindSpec> = None; let mut find_spec: Option<FindSpec> = None;
let mut with: Option<Vec<Variable>> = None; let mut with: Option<Vec<Variable>> = None;
let mut in_vars: Option<Vec<Variable>> = None; let mut in_vars: Option<Vec<Variable>> = None;
@ -991,45 +1020,45 @@ impl ParsedQuery {
return Err("find query has repeated :find"); return Err("find query has repeated :find");
} }
find_spec = Some(x) find_spec = Some(x)
} },
QueryPart::WithVars(x) => { QueryPart::WithVars(x) => {
if with.is_some() { if with.is_some() {
return Err("find query has repeated :with"); return Err("find query has repeated :with");
} }
with = Some(x) with = Some(x)
} },
QueryPart::InVars(x) => { QueryPart::InVars(x) => {
if in_vars.is_some() { if in_vars.is_some() {
return Err("find query has repeated :in"); return Err("find query has repeated :in");
} }
in_vars = Some(x) in_vars = Some(x)
} },
QueryPart::Limit(x) => { QueryPart::Limit(x) => {
if limit.is_some() { if limit.is_some() {
return Err("find query has repeated :limit"); return Err("find query has repeated :limit");
} }
limit = Some(x) limit = Some(x)
} },
QueryPart::WhereClauses(x) => { QueryPart::WhereClauses(x) => {
if where_clauses.is_some() { if where_clauses.is_some() {
return Err("find query has repeated :where"); return Err("find query has repeated :where");
} }
where_clauses = Some(x) where_clauses = Some(x)
} },
QueryPart::Order(x) => { QueryPart::Order(x) => {
if order.is_some() { if order.is_some() {
return Err("find query has repeated :order"); return Err("find query has repeated :order");
} }
order = Some(x) order = Some(x)
} },
} }
} }
Ok(ParsedQuery { Ok(ParsedQuery {
find_spec: find_spec.ok_or("expected :find")?, find_spec: find_spec.ok_or("expected :find")?,
default_source: SrcVar::DefaultSrc, default_source: SrcVar::DefaultSrc,
with: with.unwrap_or_default(), with: with.unwrap_or(vec![]),
in_vars: in_vars.unwrap_or_default(), in_vars: in_vars.unwrap_or(vec![]),
in_sources: BTreeSet::default(), in_sources: BTreeSet::default(),
limit: limit.unwrap_or(Limit::None), limit: limit.unwrap_or(Limit::None),
where_clauses: where_clauses.ok_or("expected :where")?, where_clauses: where_clauses.ok_or("expected :where")?,
@ -1041,8 +1070,8 @@ impl ParsedQuery {
impl OrJoin { impl OrJoin {
pub fn new(unify_vars: UnifyVars, clauses: Vec<OrWhereClause>) -> OrJoin { pub fn new(unify_vars: UnifyVars, clauses: Vec<OrWhereClause>) -> OrJoin {
OrJoin { OrJoin {
unify_vars, unify_vars: unify_vars,
clauses, clauses: clauses,
mentioned_vars: None, mentioned_vars: None,
} }
} }
@ -1051,8 +1080,8 @@ impl OrJoin {
/// every variable mentioned inside the join is also mentioned in the `UnifyVars` list. /// every variable mentioned inside the join is also mentioned in the `UnifyVars` list.
pub fn is_fully_unified(&self) -> bool { pub fn is_fully_unified(&self) -> bool {
match &self.unify_vars { match &self.unify_vars {
UnifyVars::Implicit => true, &UnifyVars::Implicit => true,
UnifyVars::Explicit(ref vars) => { &UnifyVars::Explicit(ref vars) => {
// We know that the join list must be a subset of the vars in the pattern, or // We know that the join list must be a subset of the vars in the pattern, or
// it would have failed validation. That allows us to simply compare counts here. // it would have failed validation. That allows us to simply compare counts here.
// TODO: in debug mode, do a full intersection, and verify that our count check // TODO: in debug mode, do a full intersection, and verify that our count check
@ -1081,13 +1110,13 @@ impl ContainsVariables for WhereClause {
fn accumulate_mentioned_variables(&self, acc: &mut BTreeSet<Variable>) { fn accumulate_mentioned_variables(&self, acc: &mut BTreeSet<Variable>) {
use self::WhereClause::*; use self::WhereClause::*;
match self { match self {
OrJoin(ref o) => o.accumulate_mentioned_variables(acc), &OrJoin(ref o) => o.accumulate_mentioned_variables(acc),
Pred(ref p) => p.accumulate_mentioned_variables(acc), &Pred(ref p) => p.accumulate_mentioned_variables(acc),
Pattern(ref p) => p.accumulate_mentioned_variables(acc), &Pattern(ref p) => p.accumulate_mentioned_variables(acc),
NotJoin(ref n) => n.accumulate_mentioned_variables(acc), &NotJoin(ref n) => n.accumulate_mentioned_variables(acc),
WhereFn(ref f) => f.accumulate_mentioned_variables(acc), &WhereFn(ref f) => f.accumulate_mentioned_variables(acc),
TypeAnnotation(ref a) => a.accumulate_mentioned_variables(acc), &TypeAnnotation(ref a) => a.accumulate_mentioned_variables(acc),
RuleExpr => (), &RuleExpr => (),
} }
} }
} }
@ -1096,12 +1125,8 @@ impl ContainsVariables for OrWhereClause {
fn accumulate_mentioned_variables(&self, acc: &mut BTreeSet<Variable>) { fn accumulate_mentioned_variables(&self, acc: &mut BTreeSet<Variable>) {
use self::OrWhereClause::*; use self::OrWhereClause::*;
match self { match self {
And(ref clauses) => { &And(ref clauses) => for clause in clauses { clause.accumulate_mentioned_variables(acc) },
for clause in clauses { &Clause(ref clause) => clause.accumulate_mentioned_variables(acc),
clause.accumulate_mentioned_variables(acc)
}
}
Clause(ref clause) => clause.accumulate_mentioned_variables(acc),
} }
} }
} }
@ -1117,9 +1142,9 @@ impl ContainsVariables for OrJoin {
impl OrJoin { impl OrJoin {
pub fn dismember(self) -> (Vec<OrWhereClause>, UnifyVars, BTreeSet<Variable>) { pub fn dismember(self) -> (Vec<OrWhereClause>, UnifyVars, BTreeSet<Variable>) {
let vars = match self.mentioned_vars { let vars = match self.mentioned_vars {
Some(m) => m, Some(m) => m,
None => self.collect_mentioned_variables(), None => self.collect_mentioned_variables(),
}; };
(self.clauses, self.unify_vars, vars) (self.clauses, self.unify_vars, vars)
} }
@ -1148,7 +1173,7 @@ impl ContainsVariables for NotJoin {
impl ContainsVariables for Predicate { impl ContainsVariables for Predicate {
fn accumulate_mentioned_variables(&self, acc: &mut BTreeSet<Variable>) { fn accumulate_mentioned_variables(&self, acc: &mut BTreeSet<Variable>) {
for arg in &self.args { for arg in &self.args {
if let FnArg::Variable(ref v) = *arg { if let &FnArg::Variable(ref v) = arg {
acc_ref(acc, v) acc_ref(acc, v)
} }
} }
@ -1164,14 +1189,16 @@ impl ContainsVariables for TypeAnnotation {
impl ContainsVariables for Binding { impl ContainsVariables for Binding {
fn accumulate_mentioned_variables(&self, acc: &mut BTreeSet<Variable>) { fn accumulate_mentioned_variables(&self, acc: &mut BTreeSet<Variable>) {
match self { match self {
Binding::BindScalar(ref v) | &Binding::BindColl(ref v) => acc_ref(acc, v), &Binding::BindScalar(ref v) | &Binding::BindColl(ref v) => {
Binding::BindRel(ref vs) | &Binding::BindTuple(ref vs) => { acc_ref(acc, v)
},
&Binding::BindRel(ref vs) | &Binding::BindTuple(ref vs) => {
for v in vs { for v in vs {
if let VariableOrPlaceholder::Variable(ref v) = *v { if let &VariableOrPlaceholder::Variable(ref v) = v {
acc_ref(acc, v); acc_ref(acc, v);
} }
} }
} },
} }
} }
} }
@ -1179,7 +1206,7 @@ impl ContainsVariables for Binding {
impl ContainsVariables for WhereFn { impl ContainsVariables for WhereFn {
fn accumulate_mentioned_variables(&self, acc: &mut BTreeSet<Variable>) { fn accumulate_mentioned_variables(&self, acc: &mut BTreeSet<Variable>) {
for arg in &self.args { for arg in &self.args {
if let FnArg::Variable(ref v) = *arg { if let &FnArg::Variable(ref v) = arg {
acc_ref(acc, v) acc_ref(acc, v)
} }
} }

View file

@ -8,22 +8,26 @@
// CONDITIONS OF ANY KIND, either express or implied. See the License for the // CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License. // specific language governing permissions and limitations under the License.
use std::fmt::{Display, Formatter, Write}; use std::fmt::{
Display,
Formatter,
Write,
};
use crate::namespaceable_name::NamespaceableName; use namespaceable_name::NamespaceableName;
#[macro_export] #[macro_export]
macro_rules! ns_keyword { macro_rules! ns_keyword {
($ns: expr, $name: expr) => {{ ($ns: expr, $name: expr) => {{
$crate::Keyword::namespaced($ns, $name) $crate::Keyword::namespaced($ns, $name)
}}; }}
} }
/// A simplification of Clojure's Symbol. /// A simplification of Clojure's Symbol.
#[derive(Clone, Debug, Eq, Hash, Ord, PartialOrd, PartialEq)] #[derive(Clone,Debug,Eq,Hash,Ord,PartialOrd,PartialEq)]
pub struct PlainSymbol(pub String); pub struct PlainSymbol(pub String);
#[derive(Clone, Debug, Eq, Hash, Ord, PartialOrd, PartialEq)] #[derive(Clone,Debug,Eq,Hash,Ord,PartialOrd,PartialEq)]
pub struct NamespacedSymbol(NamespaceableName); pub struct NamespacedSymbol(NamespaceableName);
/// A keyword is a symbol, optionally with a namespace, that prints with a leading colon. /// A keyword is a symbol, optionally with a namespace, that prints with a leading colon.
@ -63,15 +67,12 @@ pub struct NamespacedSymbol(NamespaceableName);
/// ///
/// Future: fast equality (interning?) for keywords. /// Future: fast equality (interning?) for keywords.
/// ///
#[derive(Clone, Debug, Eq, Hash, Ord, PartialOrd, PartialEq)] #[derive(Clone,Debug,Eq,Hash,Ord,PartialOrd,PartialEq)]
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
pub struct Keyword(NamespaceableName); pub struct Keyword(NamespaceableName);
impl PlainSymbol { impl PlainSymbol {
pub fn plain<T>(name: T) -> Self pub fn plain<T>(name: T) -> Self where T: Into<String> {
where
T: Into<String>,
{
let n = name.into(); let n = name.into();
assert!(!n.is_empty(), "Symbols cannot be unnamed."); assert!(!n.is_empty(), "Symbols cannot be unnamed.");
@ -106,16 +107,9 @@ impl PlainSymbol {
} }
impl NamespacedSymbol { impl NamespacedSymbol {
pub fn namespaced<N, T>(namespace: N, name: T) -> Self pub fn namespaced<N, T>(namespace: N, name: T) -> Self where N: AsRef<str>, T: AsRef<str> {
where
N: AsRef<str>,
T: AsRef<str>,
{
let r = namespace.as_ref(); let r = namespace.as_ref();
assert!( assert!(!r.is_empty(), "Namespaced symbols cannot have an empty non-null namespace.");
!r.is_empty(),
"Namespaced symbols cannot have an empty non-null namespace."
);
NamespacedSymbol(NamespaceableName::namespaced(r, name)) NamespacedSymbol(NamespaceableName::namespaced(r, name))
} }
@ -130,16 +124,13 @@ impl NamespacedSymbol {
} }
#[inline] #[inline]
pub fn components(&self) -> (&str, &str) { pub fn components<'a>(&'a self) -> (&'a str, &'a str) {
self.0.components() self.0.components()
} }
} }
impl Keyword { impl Keyword {
pub fn plain<T>(name: T) -> Self pub fn plain<T>(name: T) -> Self where T: Into<String> {
where
T: Into<String>,
{
Keyword(NamespaceableName::plain(name)) Keyword(NamespaceableName::plain(name))
} }
} }
@ -156,16 +147,9 @@ impl Keyword {
/// ``` /// ```
/// ///
/// See also the `kw!` macro in the main `mentat` crate. /// See also the `kw!` macro in the main `mentat` crate.
pub fn namespaced<N, T>(namespace: N, name: T) -> Self pub fn namespaced<N, T>(namespace: N, name: T) -> Self where N: AsRef<str>, T: AsRef<str> {
where
N: AsRef<str>,
T: AsRef<str>,
{
let r = namespace.as_ref(); let r = namespace.as_ref();
assert!( assert!(!r.is_empty(), "Namespaced keywords cannot have an empty non-null namespace.");
!r.is_empty(),
"Namespaced keywords cannot have an empty non-null namespace."
);
Keyword(NamespaceableName::namespaced(r, name)) Keyword(NamespaceableName::namespaced(r, name))
} }
@ -180,7 +164,7 @@ impl Keyword {
} }
#[inline] #[inline]
pub fn components(&self) -> (&str, &str) { pub fn components<'a>(&'a self) -> (&'a str, &'a str) {
self.0.components() self.0.components()
} }
@ -323,12 +307,8 @@ impl Display for Keyword {
#[test] #[test]
fn test_ns_keyword_macro() { fn test_ns_keyword_macro() {
assert_eq!( assert_eq!(ns_keyword!("test", "name").to_string(),
ns_keyword!("test", "name").to_string(), Keyword::namespaced("test", "name").to_string());
Keyword::namespaced("test", "name").to_string() assert_eq!(ns_keyword!("ns", "_name").to_string(),
); Keyword::namespaced("ns", "_name").to_string());
assert_eq!(
ns_keyword!("ns", "_name").to_string(),
Keyword::namespaced("ns", "_name").to_string()
);
} }

View file

@ -8,27 +8,25 @@
// CONDITIONS OF ANY KIND, either express or implied. See the License for the // CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License. // specific language governing permissions and limitations under the License.
#![allow(redundant_semicolons)] #![cfg_attr(feature = "cargo-clippy", allow(linkedlist))]
use std::cmp::{Ord, Ordering, PartialOrd}; use std::collections::{BTreeSet, BTreeMap, LinkedList};
use std::collections::{BTreeMap, BTreeSet, LinkedList}; use std::cmp::{Ordering, Ord, PartialOrd};
use std::f64;
use std::fmt::{Display, Formatter}; use std::fmt::{Display, Formatter};
use std::f64;
use chrono::{ use chrono::{
DateTime, DateTime,
SecondsFormat, SecondsFormat,
TimeZone, // For Utc::timestamp. The compiler incorrectly complains that this is unused. TimeZone, // For Utc::timestamp. The compiler incorrectly complains that this is unused.
Utc, Utc,
}; };
use num::BigInt; use num::BigInt;
use ordered_float::OrderedFloat; use ordered_float::OrderedFloat;
use uuid::Uuid; use uuid::Uuid;
use crate::symbols; use symbols;
use bytes::Bytes;
use hex::encode;
/// Value represents one of the allowed values in an EDN string. /// Value represents one of the allowed values in an EDN string.
#[derive(PartialEq, Eq, Hash, Clone, Debug)] #[derive(PartialEq, Eq, Hash, Clone, Debug)]
pub enum Value { pub enum Value {
@ -54,7 +52,6 @@ pub enum Value {
// See https://internals.rust-lang.org/t/implementing-hash-for-hashset-hashmap/3817/1 // See https://internals.rust-lang.org/t/implementing-hash-for-hashset-hashmap/3817/1
Set(BTreeSet<Value>), Set(BTreeSet<Value>),
Map(BTreeMap<Value, Value>), Map(BTreeMap<Value, Value>),
Bytes(Bytes),
} }
/// `SpannedValue` is the parallel to `Value` but used in `ValueAndSpan`. /// `SpannedValue` is the parallel to `Value` but used in `ValueAndSpan`.
@ -76,7 +73,6 @@ pub enum SpannedValue {
List(LinkedList<ValueAndSpan>), List(LinkedList<ValueAndSpan>),
Set(BTreeSet<ValueAndSpan>), Set(BTreeSet<ValueAndSpan>),
Map(BTreeMap<ValueAndSpan, ValueAndSpan>), Map(BTreeMap<ValueAndSpan, ValueAndSpan>),
Bytes(Bytes),
} }
/// Span represents the current offset (start, end) into the input string. /// Span represents the current offset (start, end) into the input string.
@ -98,10 +94,7 @@ pub struct ValueAndSpan {
} }
impl ValueAndSpan { impl ValueAndSpan {
pub fn new<I>(spanned_value: SpannedValue, span: I) -> ValueAndSpan pub fn new<I>(spanned_value: SpannedValue, span: I) -> ValueAndSpan where I: Into<Option<Span>> {
where
I: Into<Option<Span>>,
{
ValueAndSpan { ValueAndSpan {
inner: spanned_value, inner: spanned_value,
span: span.into().unwrap_or(Span(0, 0)), // TODO: consider if this has implications. span: span.into().unwrap_or(Span(0, 0)), // TODO: consider if this has implications.
@ -143,7 +136,7 @@ impl Value {
/// But right now, it's used in the bootstrapper. We'll fix that soon. /// But right now, it's used in the bootstrapper. We'll fix that soon.
pub fn with_spans(self) -> ValueAndSpan { pub fn with_spans(self) -> ValueAndSpan {
let s = self.to_pretty(120).unwrap(); let s = self.to_pretty(120).unwrap();
use crate::parse; use ::parse;
let with_spans = parse::value(&s).unwrap(); let with_spans = parse::value(&s).unwrap();
assert_eq!(self, with_spans.clone().without_spans()); assert_eq!(self, with_spans.clone().without_spans());
with_spans with_spans
@ -164,19 +157,10 @@ impl From<SpannedValue> for Value {
SpannedValue::PlainSymbol(v) => Value::PlainSymbol(v), SpannedValue::PlainSymbol(v) => Value::PlainSymbol(v),
SpannedValue::NamespacedSymbol(v) => Value::NamespacedSymbol(v), SpannedValue::NamespacedSymbol(v) => Value::NamespacedSymbol(v),
SpannedValue::Keyword(v) => Value::Keyword(v), SpannedValue::Keyword(v) => Value::Keyword(v),
SpannedValue::Vector(v) => { SpannedValue::Vector(v) => Value::Vector(v.into_iter().map(|x| x.without_spans()).collect()),
Value::Vector(v.into_iter().map(|x| x.without_spans()).collect()) SpannedValue::List(v) => Value::List(v.into_iter().map(|x| x.without_spans()).collect()),
}
SpannedValue::List(v) => {
Value::List(v.into_iter().map(|x| x.without_spans()).collect())
}
SpannedValue::Set(v) => Value::Set(v.into_iter().map(|x| x.without_spans()).collect()), SpannedValue::Set(v) => Value::Set(v.into_iter().map(|x| x.without_spans()).collect()),
SpannedValue::Map(v) => Value::Map( SpannedValue::Map(v) => Value::Map(v.into_iter().map(|(x, y)| (x.without_spans(), y.without_spans())).collect()),
v.into_iter()
.map(|(x, y)| (x.without_spans(), y.without_spans()))
.collect(),
),
SpannedValue::Bytes(b) => Value::Bytes(b),
} }
} }
} }
@ -214,9 +198,9 @@ macro_rules! def_from_option {
macro_rules! def_is { macro_rules! def_is {
($name: ident, $pat: pat) => { ($name: ident, $pat: pat) => {
pub fn $name(&self) -> bool { pub fn $name(&self) -> bool {
matches!(*self, $pat) match *self { $pat => true, _ => false }
} }
}; }
} }
/// Creates `as_$TYPE` helper functions for Value or SpannedValue, like /// Creates `as_$TYPE` helper functions for Value or SpannedValue, like
@ -236,12 +220,9 @@ macro_rules! def_as {
macro_rules! def_as_ref { macro_rules! def_as_ref {
($name: ident, $kind: path, $t: ty) => { ($name: ident, $kind: path, $t: ty) => {
pub fn $name(&self) -> Option<&$t> { pub fn $name(&self) -> Option<&$t> {
match *self { match *self { $kind(ref v) => Some(v), _ => None }
$kind(ref v) => Some(v),
_ => None,
}
} }
}; }
} }
/// Creates `into_$TYPE` helper functions for Value or SpannedValue, like /// Creates `into_$TYPE` helper functions for Value or SpannedValue, like
@ -280,9 +261,8 @@ macro_rules! to_symbol {
( $namespace:expr, $name:expr, $t:tt ) => { ( $namespace:expr, $name:expr, $t:tt ) => {
$namespace.into().map_or_else( $namespace.into().map_or_else(
|| $t::PlainSymbol(symbols::PlainSymbol::plain($name)), || $t::PlainSymbol(symbols::PlainSymbol::plain($name)),
|ns| $t::NamespacedSymbol(symbols::NamespacedSymbol::namespaced(ns, $name)), |ns| $t::NamespacedSymbol(symbols::NamespacedSymbol::namespaced(ns, $name)))
) }
};
} }
/// Converts `name` into a plain or namespaced value keyword, depending on /// Converts `name` into a plain or namespaced value keyword, depending on
@ -310,9 +290,8 @@ macro_rules! to_keyword {
( $namespace:expr, $name:expr, $t:tt ) => { ( $namespace:expr, $name:expr, $t:tt ) => {
$namespace.into().map_or_else( $namespace.into().map_or_else(
|| $t::Keyword(symbols::Keyword::plain($name)), || $t::Keyword(symbols::Keyword::plain($name)),
|ns| $t::Keyword(symbols::Keyword::namespaced(ns, $name)), |ns| $t::Keyword(symbols::Keyword::namespaced(ns, $name)))
) }
};
} }
/// Implements multiple is*, as*, into* and from* methods common to /// Implements multiple is*, as*, into* and from* methods common to
@ -333,18 +312,17 @@ macro_rules! def_common_value_methods {
def_is!(is_list, $t::List(_)); def_is!(is_list, $t::List(_));
def_is!(is_set, $t::Set(_)); def_is!(is_set, $t::Set(_));
def_is!(is_map, $t::Map(_)); def_is!(is_map, $t::Map(_));
def_is!(is_bytes, $t::Bytes(_));
pub fn is_keyword(&self) -> bool { pub fn is_keyword(&self) -> bool {
match self { match self {
$t::Keyword(ref k) => !k.is_namespaced(), &$t::Keyword(ref k) => !k.is_namespaced(),
_ => false, _ => false,
} }
} }
pub fn is_namespaced_keyword(&self) -> bool { pub fn is_namespaced_keyword(&self) -> bool {
match self { match self {
$t::Keyword(ref k) => k.is_namespaced(), &$t::Keyword(ref k) => k.is_namespaced(),
_ => false, _ => false,
} }
} }
@ -366,25 +344,24 @@ macro_rules! def_common_value_methods {
def_as_ref!(as_uuid, $t::Uuid, Uuid); def_as_ref!(as_uuid, $t::Uuid, Uuid);
def_as_ref!(as_symbol, $t::PlainSymbol, symbols::PlainSymbol); def_as_ref!(as_symbol, $t::PlainSymbol, symbols::PlainSymbol);
def_as_ref!(as_namespaced_symbol, $t::NamespacedSymbol, symbols::NamespacedSymbol); def_as_ref!(as_namespaced_symbol, $t::NamespacedSymbol, symbols::NamespacedSymbol);
def_as_ref!(as_bytes, $t::Bytes, Bytes);
pub fn as_keyword(&self) -> Option<&symbols::Keyword> { pub fn as_keyword(&self) -> Option<&symbols::Keyword> {
match self { match self {
$t::Keyword(ref k) => Some(k), &$t::Keyword(ref k) => Some(k),
_ => None, _ => None,
} }
} }
pub fn as_plain_keyword(&self) -> Option<&symbols::Keyword> { pub fn as_plain_keyword(&self) -> Option<&symbols::Keyword> {
match self { match self {
$t::Keyword(ref k) if !k.is_namespaced() => Some(k), &$t::Keyword(ref k) if !k.is_namespaced() => Some(k),
_ => None, _ => None,
} }
} }
pub fn as_namespaced_keyword(&self) -> Option<&symbols::Keyword> { pub fn as_namespaced_keyword(&self) -> Option<&symbols::Keyword> {
match self { match self {
$t::Keyword(ref k) if k.is_namespaced() => Some(k), &$t::Keyword(ref k) if k.is_namespaced() => Some(k),
_ => None, _ => None,
} }
} }
@ -404,7 +381,6 @@ macro_rules! def_common_value_methods {
def_into!(into_uuid, $t::Uuid, Uuid,); def_into!(into_uuid, $t::Uuid, Uuid,);
def_into!(into_symbol, $t::PlainSymbol, symbols::PlainSymbol,); def_into!(into_symbol, $t::PlainSymbol, symbols::PlainSymbol,);
def_into!(into_namespaced_symbol, $t::NamespacedSymbol, symbols::NamespacedSymbol,); def_into!(into_namespaced_symbol, $t::NamespacedSymbol, symbols::NamespacedSymbol,);
def_into!(into_bytes, $t::Bytes, Bytes,);
pub fn into_keyword(self) -> Option<symbols::Keyword> { pub fn into_keyword(self) -> Option<symbols::Keyword> {
match self { match self {
@ -475,7 +451,6 @@ macro_rules! def_common_value_methods {
$t::List(_) => 13, $t::List(_) => 13,
$t::Set(_) => 14, $t::Set(_) => 14,
$t::Map(_) => 15, $t::Map(_) => 15,
$t::Bytes(_) => 16,
} }
} }
@ -496,7 +471,6 @@ macro_rules! def_common_value_methods {
$t::List(_) => true, $t::List(_) => true,
$t::Set(_) => true, $t::Set(_) => true,
$t::Map(_) => true, $t::Map(_) => true,
$t::Bytes(_) => false,
} }
} }
@ -534,10 +508,9 @@ macro_rules! def_common_value_ord {
(&$t::List(ref a), &$t::List(ref b)) => b.cmp(a), (&$t::List(ref a), &$t::List(ref b)) => b.cmp(a),
(&$t::Set(ref a), &$t::Set(ref b)) => b.cmp(a), (&$t::Set(ref a), &$t::Set(ref b)) => b.cmp(a),
(&$t::Map(ref a), &$t::Map(ref b)) => b.cmp(a), (&$t::Map(ref a), &$t::Map(ref b)) => b.cmp(a),
(&$t::Bytes(ref a), &$t::Bytes(ref b)) => b.cmp(a), _ => $value.precedence().cmp(&$other.precedence())
_ => $value.precedence().cmp(&$other.precedence()),
} }
}; }
} }
/// Converts a Value or SpannedValue to string, given a formatter. /// Converts a Value or SpannedValue to string, given a formatter.
@ -549,11 +522,7 @@ macro_rules! def_common_value_display {
$t::Nil => write!($f, "nil"), $t::Nil => write!($f, "nil"),
$t::Boolean(v) => write!($f, "{}", v), $t::Boolean(v) => write!($f, "{}", v),
$t::Integer(v) => write!($f, "{}", v), $t::Integer(v) => write!($f, "{}", v),
$t::Instant(v) => write!( $t::Instant(v) => write!($f, "#inst \"{}\"", v.to_rfc3339_opts(SecondsFormat::AutoSi, true)),
$f,
"#inst \"{}\"",
v.to_rfc3339_opts(SecondsFormat::AutoSi, true)
),
$t::BigInteger(ref v) => write!($f, "{}N", v), $t::BigInteger(ref v) => write!($f, "{}N", v),
// TODO: make sure float syntax is correct. // TODO: make sure float syntax is correct.
$t::Float(ref v) => { $t::Float(ref v) => {
@ -601,12 +570,8 @@ macro_rules! def_common_value_display {
} }
write!($f, " }}") write!($f, " }}")
} }
$t::Bytes(ref v) => {
let s = encode(v);
write!($f, "#bytes {}", s)
}
} }
}; }
} }
macro_rules! def_common_value_impl { macro_rules! def_common_value_impl {
@ -632,7 +597,7 @@ macro_rules! def_common_value_impl {
def_common_value_display!($t, self, f) def_common_value_display!($t, self, f)
} }
} }
}; }
} }
def_common_value_impl!(Value<Value>); def_common_value_impl!(Value<Value>);
@ -668,7 +633,7 @@ pub trait FromMicros {
impl FromMicros for DateTime<Utc> { impl FromMicros for DateTime<Utc> {
fn from_micros(ts: i64) -> Self { fn from_micros(ts: i64) -> Self {
Utc.timestamp_opt(ts / 1_000_000, ((ts % 1_000_000).unsigned_abs() as u32) * 1_000).unwrap() Utc.timestamp(ts / 1_000_000, ((ts % 1_000_000).abs() as u32) * 1_000)
} }
} }
@ -684,44 +649,25 @@ impl ToMicros for DateTime<Utc> {
} }
} }
pub trait FromMillis {
fn from_millis(ts: i64) -> Self;
}
impl FromMillis for DateTime<Utc> {
fn from_millis(ts: i64) -> Self {
Utc.timestamp_opt(ts / 1_000, ((ts % 1_000).unsigned_abs() as u32) * 1_000).unwrap()
}
}
pub trait ToMillis {
fn to_millis(&self) -> i64;
}
impl ToMillis for DateTime<Utc> {
fn to_millis(&self) -> i64 {
let major: i64 = self.timestamp() * 1_000;
let minor: i64 = self.timestamp_subsec_millis() as i64;
major + minor
}
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {
extern crate chrono; extern crate chrono;
extern crate num;
extern crate ordered_float; extern crate ordered_float;
extern crate num;
use super::*; use super::*;
use std::cmp::Ordering; use std::collections::{BTreeSet, BTreeMap, LinkedList};
use std::collections::{BTreeMap, BTreeSet, LinkedList}; use std::cmp::{Ordering};
use std::f64;
use std::iter::FromIterator; use std::iter::FromIterator;
use std::f64;
use crate::parse; use parse;
use chrono::{DateTime, Utc}; use chrono::{
DateTime,
Utc,
};
use num::BigInt; use num::BigInt;
use ordered_float::OrderedFloat; use ordered_float::OrderedFloat;
@ -734,34 +680,29 @@ mod test {
#[test] #[test]
fn test_value_from() { fn test_value_from() {
assert_eq!( assert_eq!(Value::from_float(42f64), Value::Float(OrderedFloat::from(42f64)));
Value::from_float(42f64), assert_eq!(Value::from_ordered_float(OrderedFloat::from(42f64)), Value::Float(OrderedFloat::from(42f64)));
Value::Float(OrderedFloat::from(42f64)) assert_eq!(Value::from_bigint("42").unwrap(), Value::BigInteger(BigInt::from(42)));
);
assert_eq!(
Value::from_ordered_float(OrderedFloat::from(42f64)),
Value::Float(OrderedFloat::from(42f64))
);
assert_eq!(
Value::from_bigint("42").unwrap(),
Value::BigInteger(BigInt::from(42))
);
} }
#[test] #[test]
fn test_print_edn() { fn test_print_edn() {
assert_eq!("1234N", Value::from_bigint("1234").unwrap().to_string()); assert_eq!("1234N", Value::from_bigint("1234").unwrap().to_string());
let string = "[ 1 2 ( 7.14 ) #{ 4N } { foo/bar 42 :baz/boz 43 } [ ] :five :six/seven eight nine/ten true false nil #f NaN #f -Infinity #f +Infinity ]"; let string = "[ 1 2 ( 3.14 ) #{ 4N } { foo/bar 42 :baz/boz 43 } [ ] :five :six/seven eight nine/ten true false nil #f NaN #f -Infinity #f +Infinity ]";
let data = Value::Vector(vec![ let data = Value::Vector(vec![
Value::Integer(1), Value::Integer(1),
Value::Integer(2), Value::Integer(2),
Value::List(LinkedList::from_iter(vec![Value::from_float(7.14)])), Value::List(LinkedList::from_iter(vec![
Value::Set(BTreeSet::from_iter(vec![Value::from_bigint("4").unwrap()])), Value::from_float(3.14)
])),
Value::Set(BTreeSet::from_iter(vec![
Value::from_bigint("4").unwrap()
])),
Value::Map(BTreeMap::from_iter(vec![ Value::Map(BTreeMap::from_iter(vec![
(Value::from_symbol("foo", "bar"), Value::Integer(42)), (Value::from_symbol("foo", "bar"), Value::Integer(42)),
(Value::from_keyword("baz", "boz"), Value::Integer(43)), (Value::from_keyword("baz", "boz"), Value::Integer(43))
])), ])),
Value::Vector(vec![]), Value::Vector(vec![]),
Value::from_keyword(None, "five"), Value::from_keyword(None, "five"),
@ -778,68 +719,26 @@ mod test {
assert_eq!(string, data.to_string()); assert_eq!(string, data.to_string());
assert_eq!(string, parse::value(&data.to_string()).unwrap().to_string()); assert_eq!(string, parse::value(&data.to_string()).unwrap().to_string());
assert_eq!( assert_eq!(string, parse::value(&data.to_string()).unwrap().without_spans().to_string());
string,
parse::value(&data.to_string())
.unwrap()
.without_spans()
.to_string()
);
} }
#[test] #[test]
fn test_ord() { fn test_ord() {
// TODO: Check we follow the equality rules at the bottom of https://github.com/edn-format/edn // TODO: Check we follow the equality rules at the bottom of https://github.com/edn-format/edn
assert_eq!(Value::Nil.cmp(&Value::Nil), Ordering::Equal); assert_eq!(Value::Nil.cmp(&Value::Nil), Ordering::Equal);
assert_eq!( assert_eq!(Value::Boolean(false).cmp(&Value::Boolean(true)), Ordering::Greater);
Value::Boolean(false).cmp(&Value::Boolean(true)),
Ordering::Greater
);
assert_eq!(Value::Integer(1).cmp(&Value::Integer(2)), Ordering::Greater); assert_eq!(Value::Integer(1).cmp(&Value::Integer(2)), Ordering::Greater);
assert_eq!( assert_eq!(Value::from_bigint("1").cmp(&Value::from_bigint("2")), Ordering::Greater);
Value::from_bigint("1").cmp(&Value::from_bigint("2")), assert_eq!(Value::from_float(1f64).cmp(&Value::from_float(2f64)), Ordering::Greater);
Ordering::Greater assert_eq!(Value::Text("1".to_string()).cmp(&Value::Text("2".to_string())), Ordering::Greater);
); assert_eq!(Value::from_symbol("a", "b").cmp(&Value::from_symbol("c", "d")), Ordering::Greater);
assert_eq!( assert_eq!(Value::from_symbol(None, "a").cmp(&Value::from_symbol(None, "b")), Ordering::Greater);
Value::from_float(1f64).cmp(&Value::from_float(2f64)), assert_eq!(Value::from_keyword(":a", ":b").cmp(&Value::from_keyword(":c", ":d")), Ordering::Greater);
Ordering::Greater assert_eq!(Value::from_keyword(None, ":a").cmp(&Value::from_keyword(None, ":b")), Ordering::Greater);
); assert_eq!(Value::Vector(vec![]).cmp(&Value::Vector(vec![])), Ordering::Equal);
assert_eq!( assert_eq!(Value::List(LinkedList::new()).cmp(&Value::List(LinkedList::new())), Ordering::Equal);
Value::Text("1".to_string()).cmp(&Value::Text("2".to_string())), assert_eq!(Value::Set(BTreeSet::new()).cmp(&Value::Set(BTreeSet::new())), Ordering::Equal);
Ordering::Greater assert_eq!(Value::Map(BTreeMap::new()).cmp(&Value::Map(BTreeMap::new())), Ordering::Equal);
);
assert_eq!(
Value::from_symbol("a", "b").cmp(&Value::from_symbol("c", "d")),
Ordering::Greater
);
assert_eq!(
Value::from_symbol(None, "a").cmp(&Value::from_symbol(None, "b")),
Ordering::Greater
);
assert_eq!(
Value::from_keyword(":a", ":b").cmp(&Value::from_keyword(":c", ":d")),
Ordering::Greater
);
assert_eq!(
Value::from_keyword(None, ":a").cmp(&Value::from_keyword(None, ":b")),
Ordering::Greater
);
assert_eq!(
Value::Vector(vec![]).cmp(&Value::Vector(vec![])),
Ordering::Equal
);
assert_eq!(
Value::List(LinkedList::new()).cmp(&Value::List(LinkedList::new())),
Ordering::Equal
);
assert_eq!(
Value::Set(BTreeSet::new()).cmp(&Value::Set(BTreeSet::new())),
Ordering::Equal
);
assert_eq!(
Value::Map(BTreeMap::new()).cmp(&Value::Map(BTreeMap::new())),
Ordering::Equal
);
} }
#[test] #[test]
@ -859,10 +758,10 @@ mod test {
assert!(n_v.clone().into_keyword().is_some()); assert!(n_v.clone().into_keyword().is_some());
assert!(n_v.clone().into_plain_keyword().is_none()); assert!(n_v.clone().into_plain_keyword().is_none());
assert!(n_v.into_namespaced_keyword().is_some()); assert!(n_v.clone().into_namespaced_keyword().is_some());
assert!(p_v.clone().into_keyword().is_some()); assert!(p_v.clone().into_keyword().is_some());
assert!(p_v.clone().into_plain_keyword().is_some()); assert!(p_v.clone().into_plain_keyword().is_some());
assert!(p_v.into_namespaced_keyword().is_none()); assert!(p_v.clone().into_namespaced_keyword().is_none());
} }
} }

View file

@ -10,7 +10,7 @@
#![allow(dead_code)] #![allow(dead_code)]
use crate::types::Value; use types::Value;
/// Merge the EDN `Value::Map` instance `right` into `left`. Returns `None` if either `left` or /// Merge the EDN `Value::Map` instance `right` into `left`. Returns `None` if either `left` or
/// `right` is not a `Value::Map`. /// `right` is not a `Value::Map`.
@ -21,11 +21,11 @@ use crate::types::Value;
/// TODO: implement `merge` for [Value], following the `concat`/`SliceConcatExt` pattern. /// TODO: implement `merge` for [Value], following the `concat`/`SliceConcatExt` pattern.
pub fn merge(left: &Value, right: &Value) -> Option<Value> { pub fn merge(left: &Value, right: &Value) -> Option<Value> {
match (left, right) { match (left, right) {
(Value::Map(l), Value::Map(r)) => { (&Value::Map(ref l), &Value::Map(ref r)) => {
let mut result = l.clone(); let mut result = l.clone();
result.extend(r.clone()); result.extend(r.clone().into_iter());
Some(Value::Map(result)) Some(Value::Map(result))
} }
_ => None, _ => None
} }
} }

View file

@ -8,21 +8,22 @@
// CONDITIONS OF ANY KIND, either express or implied. See the License for the // CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License. // specific language governing permissions and limitations under the License.
use std::rc::Rc; use ::std::rc::{
Rc,
};
use std::sync::Arc; use ::std::sync::{
Arc,
};
pub trait FromRc<T> { pub trait FromRc<T> {
fn from_rc(val: Rc<T>) -> Self; fn from_rc(val: Rc<T>) -> Self;
fn from_arc(val: Arc<T>) -> Self; fn from_arc(val: Arc<T>) -> Self;
} }
impl<T> FromRc<T> for Rc<T> impl<T> FromRc<T> for Rc<T> where T: Sized + Clone {
where
T: Sized + Clone,
{
fn from_rc(val: Rc<T>) -> Self { fn from_rc(val: Rc<T>) -> Self {
val val.clone()
} }
fn from_arc(val: Arc<T>) -> Self { fn from_arc(val: Arc<T>) -> Self {
@ -33,10 +34,7 @@ where
} }
} }
impl<T> FromRc<T> for Arc<T> impl<T> FromRc<T> for Arc<T> where T: Sized + Clone {
where
T: Sized + Clone,
{
fn from_rc(val: Rc<T>) -> Self { fn from_rc(val: Rc<T>) -> Self {
match ::std::rc::Rc::<T>::try_unwrap(val) { match ::std::rc::Rc::<T>::try_unwrap(val) {
Ok(v) => Self::new(v), Ok(v) => Self::new(v),
@ -45,14 +43,11 @@ where
} }
fn from_arc(val: Arc<T>) -> Self { fn from_arc(val: Arc<T>) -> Self {
val val.clone()
} }
} }
impl<T> FromRc<T> for Box<T> impl<T> FromRc<T> for Box<T> where T: Sized + Clone {
where
T: Sized + Clone,
{
fn from_rc(val: Rc<T>) -> Self { fn from_rc(val: Rc<T>) -> Self {
match ::std::rc::Rc::<T>::try_unwrap(val) { match ::std::rc::Rc::<T>::try_unwrap(val) {
Ok(v) => Self::new(v), Ok(v) => Self::new(v),
@ -74,10 +69,7 @@ pub trait Cloned<T> {
fn to_value_rc(&self) -> ValueRc<T>; fn to_value_rc(&self) -> ValueRc<T>;
} }
impl<T: Clone> Cloned<T> for Rc<T> impl<T: Clone> Cloned<T> for Rc<T> where T: Sized + Clone {
where
T: Sized + Clone,
{
fn cloned(&self) -> T { fn cloned(&self) -> T {
(*self.as_ref()).clone() (*self.as_ref()).clone()
} }
@ -87,10 +79,7 @@ where
} }
} }
impl<T: Clone> Cloned<T> for Arc<T> impl<T: Clone> Cloned<T> for Arc<T> where T: Sized + Clone {
where
T: Sized + Clone,
{
fn cloned(&self) -> T { fn cloned(&self) -> T {
(*self.as_ref()).clone() (*self.as_ref()).clone()
} }
@ -100,10 +89,7 @@ where
} }
} }
impl<T: Clone> Cloned<T> for Box<T> impl<T: Clone> Cloned<T> for Box<T> where T: Sized + Clone {
where
T: Sized + Clone,
{
fn cloned(&self) -> T { fn cloned(&self) -> T {
self.as_ref().clone() self.as_ref().clone()
} }

View file

@ -10,14 +10,33 @@
extern crate edn; extern crate edn;
use edn::{Keyword, PlainSymbol}; use edn::{
Keyword,
use edn::query::{ PlainSymbol,
Direction, Element, FindSpec, FnArg, Limit, NonIntegerConstant, OrJoin, OrWhereClause, Order,
Pattern, PatternNonValuePlace, PatternValuePlace, Predicate, UnifyVars, Variable, WhereClause,
}; };
use edn::parse::parse_query; use edn::query::{
Direction,
Element,
FindSpec,
FnArg,
Limit,
NonIntegerConstant,
Order,
OrJoin,
OrWhereClause,
Pattern,
PatternNonValuePlace,
PatternValuePlace,
Predicate,
UnifyVars,
Variable,
WhereClause,
};
use edn::parse::{
parse_query,
};
///! N.B., parsing a query can be done without reference to a DB. ///! N.B., parsing a query can be done without reference to a DB.
///! Processing the parsed query into something we can work with ///! Processing the parsed query into something we can work with
@ -29,29 +48,21 @@ fn can_parse_predicates() {
let s = "[:find [?x ...] :where [?x _ ?y] [(< ?y 10)]]"; let s = "[:find [?x ...] :where [?x _ ?y] [(< ?y 10)]]";
let p = parse_query(s).unwrap(); let p = parse_query(s).unwrap();
assert_eq!( assert_eq!(p.find_spec,
p.find_spec, FindSpec::FindColl(Element::Variable(Variable::from_valid_name("?x"))));
FindSpec::FindColl(Element::Variable(Variable::from_valid_name("?x"))) assert_eq!(p.where_clauses,
); vec![
assert_eq!( WhereClause::Pattern(Pattern {
p.where_clauses, source: None,
vec![ entity: PatternNonValuePlace::Variable(Variable::from_valid_name("?x")),
WhereClause::Pattern(Pattern { attribute: PatternNonValuePlace::Placeholder,
source: None, value: PatternValuePlace::Variable(Variable::from_valid_name("?y")),
entity: PatternNonValuePlace::Variable(Variable::from_valid_name("?x")), tx: PatternNonValuePlace::Placeholder,
attribute: PatternNonValuePlace::Placeholder, }),
value: PatternValuePlace::Variable(Variable::from_valid_name("?y")), WhereClause::Pred(Predicate { operator: PlainSymbol::plain("<"), args: vec![
tx: PatternNonValuePlace::Placeholder, FnArg::Variable(Variable::from_valid_name("?y")), FnArg::EntidOrInteger(10),
}), ]}),
WhereClause::Pred(Predicate { ]);
operator: PlainSymbol::plain("<"),
args: vec![
FnArg::Variable(Variable::from_valid_name("?y")),
FnArg::EntidOrInteger(10),
]
}),
]
);
} }
#[test] #[test]
@ -59,32 +70,32 @@ fn can_parse_simple_or() {
let s = "[:find ?x . :where (or [?x _ 10] [?x _ 15])]"; let s = "[:find ?x . :where (or [?x _ 10] [?x _ 15])]";
let p = parse_query(s).unwrap(); let p = parse_query(s).unwrap();
assert_eq!( assert_eq!(p.find_spec,
p.find_spec, FindSpec::FindScalar(Element::Variable(Variable::from_valid_name("?x"))));
FindSpec::FindScalar(Element::Variable(Variable::from_valid_name("?x"))) assert_eq!(p.where_clauses,
); vec![
assert_eq!( WhereClause::OrJoin(OrJoin::new(
p.where_clauses, UnifyVars::Implicit,
vec![WhereClause::OrJoin(OrJoin::new( vec![
UnifyVars::Implicit, OrWhereClause::Clause(
vec![ WhereClause::Pattern(Pattern {
OrWhereClause::Clause(WhereClause::Pattern(Pattern { source: None,
source: None, entity: PatternNonValuePlace::Variable(Variable::from_valid_name("?x")),
entity: PatternNonValuePlace::Variable(Variable::from_valid_name("?x")), attribute: PatternNonValuePlace::Placeholder,
attribute: PatternNonValuePlace::Placeholder, value: PatternValuePlace::EntidOrInteger(10),
value: PatternValuePlace::EntidOrInteger(10), tx: PatternNonValuePlace::Placeholder,
tx: PatternNonValuePlace::Placeholder, })),
})), OrWhereClause::Clause(
OrWhereClause::Clause(WhereClause::Pattern(Pattern { WhereClause::Pattern(Pattern {
source: None, source: None,
entity: PatternNonValuePlace::Variable(Variable::from_valid_name("?x")), entity: PatternNonValuePlace::Variable(Variable::from_valid_name("?x")),
attribute: PatternNonValuePlace::Placeholder, attribute: PatternNonValuePlace::Placeholder,
value: PatternValuePlace::EntidOrInteger(15), value: PatternValuePlace::EntidOrInteger(15),
tx: PatternNonValuePlace::Placeholder, tx: PatternNonValuePlace::Placeholder,
})), })),
], ],
)),] )),
); ]);
} }
#[test] #[test]
@ -92,23 +103,24 @@ fn can_parse_unit_or_join() {
let s = "[:find ?x . :where (or-join [?x] [?x _ 15])]"; let s = "[:find ?x . :where (or-join [?x] [?x _ 15])]";
let p = parse_query(s).expect("to be able to parse find"); let p = parse_query(s).expect("to be able to parse find");
assert_eq!( assert_eq!(p.find_spec,
p.find_spec, FindSpec::FindScalar(Element::Variable(Variable::from_valid_name("?x"))));
FindSpec::FindScalar(Element::Variable(Variable::from_valid_name("?x"))) assert_eq!(p.where_clauses,
); vec![
assert_eq!( WhereClause::OrJoin(OrJoin::new(
p.where_clauses, UnifyVars::Explicit(std::iter::once(Variable::from_valid_name("?x")).collect()),
vec![WhereClause::OrJoin(OrJoin::new( vec![
UnifyVars::Explicit(std::iter::once(Variable::from_valid_name("?x")).collect()), OrWhereClause::Clause(
vec![OrWhereClause::Clause(WhereClause::Pattern(Pattern { WhereClause::Pattern(Pattern {
source: None, source: None,
entity: PatternNonValuePlace::Variable(Variable::from_valid_name("?x")), entity: PatternNonValuePlace::Variable(Variable::from_valid_name("?x")),
attribute: PatternNonValuePlace::Placeholder, attribute: PatternNonValuePlace::Placeholder,
value: PatternValuePlace::EntidOrInteger(15), value: PatternValuePlace::EntidOrInteger(15),
tx: PatternNonValuePlace::Placeholder, tx: PatternNonValuePlace::Placeholder,
})),], })),
)),] ],
); )),
]);
} }
#[test] #[test]
@ -116,32 +128,32 @@ fn can_parse_simple_or_join() {
let s = "[:find ?x . :where (or-join [?x] [?x _ 10] [?x _ -15])]"; let s = "[:find ?x . :where (or-join [?x] [?x _ 10] [?x _ -15])]";
let p = parse_query(s).unwrap(); let p = parse_query(s).unwrap();
assert_eq!( assert_eq!(p.find_spec,
p.find_spec, FindSpec::FindScalar(Element::Variable(Variable::from_valid_name("?x"))));
FindSpec::FindScalar(Element::Variable(Variable::from_valid_name("?x"))) assert_eq!(p.where_clauses,
); vec![
assert_eq!( WhereClause::OrJoin(OrJoin::new(
p.where_clauses, UnifyVars::Explicit(std::iter::once(Variable::from_valid_name("?x")).collect()),
vec![WhereClause::OrJoin(OrJoin::new( vec![
UnifyVars::Explicit(std::iter::once(Variable::from_valid_name("?x")).collect()), OrWhereClause::Clause(
vec![ WhereClause::Pattern(Pattern {
OrWhereClause::Clause(WhereClause::Pattern(Pattern { source: None,
source: None, entity: PatternNonValuePlace::Variable(Variable::from_valid_name("?x")),
entity: PatternNonValuePlace::Variable(Variable::from_valid_name("?x")), attribute: PatternNonValuePlace::Placeholder,
attribute: PatternNonValuePlace::Placeholder, value: PatternValuePlace::EntidOrInteger(10),
value: PatternValuePlace::EntidOrInteger(10), tx: PatternNonValuePlace::Placeholder,
tx: PatternNonValuePlace::Placeholder, })),
})), OrWhereClause::Clause(
OrWhereClause::Clause(WhereClause::Pattern(Pattern { WhereClause::Pattern(Pattern {
source: None, source: None,
entity: PatternNonValuePlace::Variable(Variable::from_valid_name("?x")), entity: PatternNonValuePlace::Variable(Variable::from_valid_name("?x")),
attribute: PatternNonValuePlace::Placeholder, attribute: PatternNonValuePlace::Placeholder,
value: PatternValuePlace::EntidOrInteger(-15), value: PatternValuePlace::EntidOrInteger(-15),
tx: PatternNonValuePlace::Placeholder, tx: PatternNonValuePlace::Placeholder,
})), })),
], ],
)),] )),
); ]);
} }
#[cfg(test)] #[cfg(test)]
@ -154,57 +166,51 @@ fn can_parse_simple_or_and_join() {
let s = "[:find ?x . :where (or [?x _ 10] (and (or [?x :foo/bar ?y] [?x :foo/baz ?y]) [(< ?y 1)]))]"; let s = "[:find ?x . :where (or [?x _ 10] (and (or [?x :foo/bar ?y] [?x :foo/baz ?y]) [(< ?y 1)]))]";
let p = parse_query(s).unwrap(); let p = parse_query(s).unwrap();
assert_eq!( assert_eq!(p.find_spec,
p.find_spec, FindSpec::FindScalar(Element::Variable(Variable::from_valid_name("?x"))));
FindSpec::FindScalar(Element::Variable(Variable::from_valid_name("?x"))) assert_eq!(p.where_clauses,
); vec![
assert_eq!( WhereClause::OrJoin(OrJoin::new(
p.where_clauses, UnifyVars::Implicit,
vec![WhereClause::OrJoin(OrJoin::new( vec![
UnifyVars::Implicit, OrWhereClause::Clause(
vec![ WhereClause::Pattern(Pattern {
OrWhereClause::Clause(WhereClause::Pattern(Pattern { source: None,
source: None, entity: PatternNonValuePlace::Variable(Variable::from_valid_name("?x")),
entity: PatternNonValuePlace::Variable(Variable::from_valid_name("?x")), attribute: PatternNonValuePlace::Placeholder,
attribute: PatternNonValuePlace::Placeholder, value: PatternValuePlace::EntidOrInteger(10),
value: PatternValuePlace::EntidOrInteger(10), tx: PatternNonValuePlace::Placeholder,
tx: PatternNonValuePlace::Placeholder, })),
})), OrWhereClause::And(
OrWhereClause::And(vec![ vec![
WhereClause::OrJoin(OrJoin::new( WhereClause::OrJoin(OrJoin::new(
UnifyVars::Implicit, UnifyVars::Implicit,
vec![ vec![
OrWhereClause::Clause(WhereClause::Pattern(Pattern { OrWhereClause::Clause(WhereClause::Pattern(Pattern {
source: None, source: None,
entity: PatternNonValuePlace::Variable(Variable::from_valid_name( entity: PatternNonValuePlace::Variable(Variable::from_valid_name("?x")),
"?x" attribute: ident("foo", "bar"),
)), value: PatternValuePlace::Variable(Variable::from_valid_name("?y")),
attribute: ident("foo", "bar"), tx: PatternNonValuePlace::Placeholder,
value: PatternValuePlace::Variable(Variable::from_valid_name("?y")), })),
tx: PatternNonValuePlace::Placeholder, OrWhereClause::Clause(WhereClause::Pattern(Pattern {
})), source: None,
OrWhereClause::Clause(WhereClause::Pattern(Pattern { entity: PatternNonValuePlace::Variable(Variable::from_valid_name("?x")),
source: None, attribute: ident("foo", "baz"),
entity: PatternNonValuePlace::Variable(Variable::from_valid_name( value: PatternValuePlace::Variable(Variable::from_valid_name("?y")),
"?x" tx: PatternNonValuePlace::Placeholder,
)), })),
attribute: ident("foo", "baz"), ],
value: PatternValuePlace::Variable(Variable::from_valid_name("?y")), )),
tx: PatternNonValuePlace::Placeholder,
})), WhereClause::Pred(Predicate { operator: PlainSymbol::plain("<"), args: vec![
], FnArg::Variable(Variable::from_valid_name("?y")), FnArg::EntidOrInteger(1),
)), ]}),
WhereClause::Pred(Predicate { ],
operator: PlainSymbol::plain("<"), )
args: vec![ ],
FnArg::Variable(Variable::from_valid_name("?y")), )),
FnArg::EntidOrInteger(1), ]);
]
}),
],)
],
)),]
);
} }
#[test] #[test]
@ -214,40 +220,21 @@ fn can_parse_order_by() {
// Defaults to ascending. // Defaults to ascending.
let default = "[:find ?x :where [?x :foo/baz ?y] :order ?y]"; let default = "[:find ?x :where [?x :foo/baz ?y] :order ?y]";
assert_eq!( assert_eq!(parse_query(default).unwrap().order,
parse_query(default).unwrap().order, Some(vec![Order(Direction::Ascending, Variable::from_valid_name("?y"))]));
Some(vec![Order(
Direction::Ascending,
Variable::from_valid_name("?y")
)])
);
let ascending = "[:find ?x :where [?x :foo/baz ?y] :order (asc ?y)]"; let ascending = "[:find ?x :where [?x :foo/baz ?y] :order (asc ?y)]";
assert_eq!( assert_eq!(parse_query(ascending).unwrap().order,
parse_query(ascending).unwrap().order, Some(vec![Order(Direction::Ascending, Variable::from_valid_name("?y"))]));
Some(vec![Order(
Direction::Ascending,
Variable::from_valid_name("?y")
)])
);
let descending = "[:find ?x :where [?x :foo/baz ?y] :order (desc ?y)]"; let descending = "[:find ?x :where [?x :foo/baz ?y] :order (desc ?y)]";
assert_eq!( assert_eq!(parse_query(descending).unwrap().order,
parse_query(descending).unwrap().order, Some(vec![Order(Direction::Descending, Variable::from_valid_name("?y"))]));
Some(vec![Order(
Direction::Descending,
Variable::from_valid_name("?y")
)])
);
let mixed = "[:find ?x :where [?x :foo/baz ?y] :order (desc ?y) (asc ?x)]"; let mixed = "[:find ?x :where [?x :foo/baz ?y] :order (desc ?y) (asc ?x)]";
assert_eq!( assert_eq!(parse_query(mixed).unwrap().order,
parse_query(mixed).unwrap().order, Some(vec![Order(Direction::Descending, Variable::from_valid_name("?y")),
Some(vec![ Order(Direction::Ascending, Variable::from_valid_name("?x"))]));
Order(Direction::Descending, Variable::from_valid_name("?y")),
Order(Direction::Ascending, Variable::from_valid_name("?x"))
])
);
} }
#[test] #[test]
@ -259,76 +246,55 @@ fn can_parse_limit() {
assert!(parse_query(zero_invalid).is_err()); assert!(parse_query(zero_invalid).is_err());
let none = "[:find ?x :where [?x :foo/baz ?y]]"; let none = "[:find ?x :where [?x :foo/baz ?y]]";
assert_eq!(parse_query(none).unwrap().limit, Limit::None); assert_eq!(parse_query(none).unwrap().limit,
Limit::None);
let one = "[:find ?x :where [?x :foo/baz ?y] :limit 1]"; let one = "[:find ?x :where [?x :foo/baz ?y] :limit 1]";
assert_eq!(parse_query(one).unwrap().limit, Limit::Fixed(1)); assert_eq!(parse_query(one).unwrap().limit,
Limit::Fixed(1));
let onethousand = "[:find ?x :where [?x :foo/baz ?y] :limit 1000]"; let onethousand = "[:find ?x :where [?x :foo/baz ?y] :limit 1000]";
assert_eq!(parse_query(onethousand).unwrap().limit, Limit::Fixed(1000)); assert_eq!(parse_query(onethousand).unwrap().limit,
Limit::Fixed(1000));
let variable_with_in = "[:find ?x :in ?limit :where [?x :foo/baz ?y] :limit ?limit]"; let variable_with_in = "[:find ?x :in ?limit :where [?x :foo/baz ?y] :limit ?limit]";
assert_eq!( assert_eq!(parse_query(variable_with_in).unwrap().limit,
parse_query(variable_with_in).unwrap().limit, Limit::Variable(Variable::from_valid_name("?limit")));
Limit::Variable(Variable::from_valid_name("?limit"))
);
let variable_with_in_used = "[:find ?x :in ?limit :where [?x :foo/baz ?limit] :limit ?limit]"; let variable_with_in_used = "[:find ?x :in ?limit :where [?x :foo/baz ?limit] :limit ?limit]";
assert_eq!( assert_eq!(parse_query(variable_with_in_used).unwrap().limit,
parse_query(variable_with_in_used).unwrap().limit, Limit::Variable(Variable::from_valid_name("?limit")));
Limit::Variable(Variable::from_valid_name("?limit"))
);
} }
#[test] #[test]
fn can_parse_uuid() { fn can_parse_uuid() {
let expected = let expected = edn::Uuid::parse_str("4cb3f828-752d-497a-90c9-b1fd516d5644").expect("valid uuid");
edn::Uuid::parse_str("4cb3f828-752d-497a-90c9-b1fd516d5644").expect("valid uuid");
let s = "[:find ?x :where [?x :foo/baz #uuid \"4cb3f828-752d-497a-90c9-b1fd516d5644\"]]"; let s = "[:find ?x :where [?x :foo/baz #uuid \"4cb3f828-752d-497a-90c9-b1fd516d5644\"]]";
assert_eq!( assert_eq!(parse_query(s).expect("parsed").where_clauses.pop().expect("a where clause"),
parse_query(s) WhereClause::Pattern(
.expect("parsed") Pattern::new(None,
.where_clauses PatternNonValuePlace::Variable(Variable::from_valid_name("?x")),
.pop() Keyword::namespaced("foo", "baz").into(),
.expect("a where clause"), PatternValuePlace::Constant(NonIntegerConstant::Uuid(expected)),
WhereClause::Pattern( PatternNonValuePlace::Placeholder)
Pattern::new( .expect("valid pattern")));
None,
PatternNonValuePlace::Variable(Variable::from_valid_name("?x")),
Keyword::namespaced("foo", "baz").into(),
PatternValuePlace::Constant(NonIntegerConstant::Uuid(expected)),
PatternNonValuePlace::Placeholder
)
.expect("valid pattern")
)
);
} }
#[test] #[test]
fn can_parse_exotic_whitespace() { fn can_parse_exotic_whitespace() {
let expected = let expected = edn::Uuid::parse_str("4cb3f828-752d-497a-90c9-b1fd516d5644").expect("valid uuid");
edn::Uuid::parse_str("4cb3f828-752d-497a-90c9-b1fd516d5644").expect("valid uuid");
// The query string from `can_parse_uuid`, with newlines, commas, and line comments interspersed. // The query string from `can_parse_uuid`, with newlines, commas, and line comments interspersed.
let s = r#"[:find let s = r#"[:find
?x ,, :where, ;atest ?x ,, :where, ;atest
[?x :foo/baz #uuid [?x :foo/baz #uuid
"4cb3f828-752d-497a-90c9-b1fd516d5644", ;testa "4cb3f828-752d-497a-90c9-b1fd516d5644", ;testa
,],, ,],;"#; ,],, ,],;"#;
assert_eq!( assert_eq!(parse_query(s).expect("parsed").where_clauses.pop().expect("a where clause"),
parse_query(s) WhereClause::Pattern(
.expect("parsed") Pattern::new(None,
.where_clauses PatternNonValuePlace::Variable(Variable::from_valid_name("?x")),
.pop() Keyword::namespaced("foo", "baz").into(),
.expect("a where clause"), PatternValuePlace::Constant(NonIntegerConstant::Uuid(expected)),
WhereClause::Pattern( PatternNonValuePlace::Placeholder)
Pattern::new( .expect("valid pattern")));
None,
PatternNonValuePlace::Variable(Variable::from_valid_name("?x")),
Keyword::namespaced("foo", "baz").into(),
PatternValuePlace::Constant(NonIntegerConstant::Uuid(expected)),
PatternNonValuePlace::Placeholder
)
.expect("valid pattern")
)
);
} }

View file

@ -8,10 +8,11 @@
// CONDITIONS OF ANY KIND, either express or implied. See the License for the // CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License. // specific language governing permissions and limitations under the License.
#![cfg(feature = "serde_support")] #![cfg(feature = "serde_support")]
extern crate serde_json;
extern crate serde_test; extern crate serde_test;
extern crate serde_json;
extern crate edn; extern crate edn;
use edn::symbols::Keyword; use edn::symbols::Keyword;
@ -21,24 +22,19 @@ use serde_test::{assert_tokens, Token};
#[test] #[test]
fn test_serialize_keyword() { fn test_serialize_keyword() {
let kw = Keyword::namespaced("foo", "bar"); let kw = Keyword::namespaced("foo", "bar");
assert_tokens( assert_tokens(&kw, &[
&kw, Token::NewtypeStruct { name: "Keyword" },
&[ Token::Struct { name: "NamespaceableName", len: 2 },
Token::NewtypeStruct { name: "Keyword" }, Token::Str("namespace"),
Token::Struct { Token::Some,
name: "NamespaceableName", Token::BorrowedStr("foo"),
len: 2, Token::Str("name"),
}, Token::BorrowedStr("bar"),
Token::Str("namespace"), Token::StructEnd,
Token::Some, ]);
Token::BorrowedStr("foo"),
Token::Str("name"),
Token::BorrowedStr("bar"),
Token::StructEnd,
],
);
} }
#[cfg(feature = "serde_support")] #[cfg(feature = "serde_support")]
#[test] #[test]
fn test_deserialize_keyword() { fn test_deserialize_keyword() {
@ -55,3 +51,6 @@ fn test_deserialize_keyword() {
let not_kw = serde_json::from_str::<Keyword>(bad_ns_json); let not_kw = serde_json::from_str::<Keyword>(bad_ns_json);
assert!(not_kw.is_err()); assert!(not_kw.is_err());
} }

File diff suppressed because it is too large Load diff

View file

@ -1,31 +0,0 @@
;; movie schema
[{:db/ident :movie/title
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one
:db/doc "The title of the movie"}
{:db/ident :movie/genre
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one
:db/doc "The genre of the movie"}
{:db/ident :movie/release-year
:db/valueType :db.type/long
:db/cardinality :db.cardinality/one
:db/doc "The year the movie was released in theaters"}]
;; a few movies
[{:movie/title "The Goonies"
:movie/genre "action/adventure"
:movie/release-year 1985}
{:movie/title "Commando"
:movie/genre "thriller/action"
:movie/release-year 1985}
{:movie/title "Repo Man"
:movie/genre "punk dystopia"
:movie/release-year 1984}]
;; query
[:find ?movie-title
:where [_ :movie/title ?movie-title]]

View file

@ -1,6 +1,6 @@
[package] [package]
name = "mentat_ffi" name = "mentat_ffi"
version = "0.0.2" version = "0.0.1"
authors = ["Emily Toop <etoop@mozilla.com>"] authors = ["Emily Toop <etoop@mozilla.com>"]
[lib] [lib]
@ -13,7 +13,7 @@ sqlcipher = ["mentat/sqlcipher"]
bundled_sqlite3 = ["mentat/bundled_sqlite3"] bundled_sqlite3 = ["mentat/bundled_sqlite3"]
[dependencies] [dependencies]
libc = "~0.2" libc = "0.2"
[dependencies.mentat] [dependencies.mentat]
path = "../" path = "../"

View file

@ -21,6 +21,4 @@ pub enum LogLevel {
Error = 6, Error = 6,
} }
extern "C" { extern { pub fn __android_log_write(prio: c_int, tag: *const c_char, text: *const c_char) -> c_int; }
pub fn __android_log_write(prio: c_int, tag: *const c_char, text: *const c_char) -> c_int;
}

File diff suppressed because it is too large Load diff

View file

@ -9,52 +9,51 @@
// specific language governing permissions and limitations under the License. // specific language governing permissions and limitations under the License.
pub mod strings { pub mod strings {
use std::ffi::{CStr, CString}; use std::ffi::{
CString,
CStr
};
use std::os::raw::c_char; use std::os::raw::c_char;
use mentat::Keyword; use mentat::{
Keyword,
};
/// # Safety pub fn c_char_to_string(cchar: *const c_char) -> &'static str {
///
/// This function TODO
pub unsafe fn c_char_to_string(cchar: *const c_char) -> &'static str {
assert!(!cchar.is_null()); assert!(!cchar.is_null());
let c_str = CStr::from_ptr(cchar); let c_str = unsafe { CStr::from_ptr(cchar) };
c_str.to_str().unwrap_or("") c_str.to_str().unwrap_or("")
} }
pub fn string_to_c_char<T>(r_string: T) -> *mut c_char pub fn string_to_c_char<T>(r_string: T) -> *mut c_char where T: Into<String> {
where
T: Into<String>,
{
CString::new(r_string.into()).unwrap().into_raw() CString::new(r_string.into()).unwrap().into_raw()
} }
pub fn kw_from_string(keyword_string: &'static str) -> Keyword { pub fn kw_from_string(keyword_string: &'static str) -> Keyword {
// TODO: validate. The input might not be a keyword! // TODO: validate. The input might not be a keyword!
let attr_name = keyword_string.trim_start_matches(':'); let attr_name = keyword_string.trim_left_matches(":");
let parts: Vec<&str> = attr_name.split('/').collect(); let parts: Vec<&str> = attr_name.split("/").collect();
Keyword::namespaced(parts[0], parts[1]) Keyword::namespaced(parts[0], parts[1])
} }
} }
pub mod log { pub mod log {
#[cfg(all(target_os = "android", not(test)))] #[cfg(all(target_os="android", not(test)))]
use std::ffi::CString; use std::ffi::CString;
#[cfg(all(target_os = "android", not(test)))] #[cfg(all(target_os="android", not(test)))]
use android; use android;
// TODO far from ideal. And, we might actually want to println in tests. // TODO far from ideal. And, we might actually want to println in tests.
#[cfg(all(not(target_os = "android"), not(target_os = "ios")))] #[cfg(all(not(target_os="android"), not(target_os="ios")))]
pub fn d(_: &str) {} pub fn d(_: &str) {}
#[cfg(all(target_os = "ios", not(test)))] #[cfg(all(target_os="ios", not(test)))]
pub fn d(message: &str) { pub fn d(message: &str) {
eprintln!("{}", message); eprintln!("{}", message);
} }
#[cfg(all(target_os = "android", not(test)))] #[cfg(all(target_os="android", not(test)))]
pub fn d(message: &str) { pub fn d(message: &str) {
let message = CString::new(message).unwrap(); let message = CString::new(message).unwrap();
let message = message.as_ptr(); let message = message.as_ptr();
@ -66,9 +65,9 @@ pub mod log {
pub mod error { pub mod error {
use super::strings::string_to_c_char; use super::strings::string_to_c_char;
use std::os::raw::c_char;
use std::boxed::Box; use std::boxed::Box;
use std::fmt::Display; use std::fmt::Display;
use std::os::raw::c_char;
use std::ptr; use std::ptr;
/// Represents an error that occurred on the mentat side. Many mentat FFI functions take a /// Represents an error that occurred on the mentat side. Many mentat FFI functions take a
@ -97,9 +96,7 @@ pub mod error {
impl Default for ExternError { impl Default for ExternError {
fn default() -> ExternError { fn default() -> ExternError {
ExternError { ExternError { message: ptr::null_mut() }
message: ptr::null_mut(),
}
} }
} }
@ -110,12 +107,8 @@ pub mod error {
/// - If `result` is `Err(e)`, returns a null pointer and stores a string representing the error /// - If `result` is `Err(e)`, returns a null pointer and stores a string representing the error
/// message (which was allocated on the heap and should eventually be freed) into /// message (which was allocated on the heap and should eventually be freed) into
/// `error.message` /// `error.message`
/// # Safety
/// Be afraid... TODO
pub unsafe fn translate_result<T, E>(result: Result<T, E>, error: *mut ExternError) -> *mut T pub unsafe fn translate_result<T, E>(result: Result<T, E>, error: *mut ExternError) -> *mut T
where where E: Display {
E: Display,
{
// TODO: can't unwind across FFI... // TODO: can't unwind across FFI...
assert!(!error.is_null(), "Error output parameter is not optional"); assert!(!error.is_null(), "Error output parameter is not optional");
let error = &mut *error; let error = &mut *error;
@ -138,15 +131,8 @@ pub mod error {
/// - If `result` is `Err(e)`, returns a null pointer and stores a string representing the error /// - If `result` is `Err(e)`, returns a null pointer and stores a string representing the error
/// message (which was allocated on the heap and should eventually be freed) into /// message (which was allocated on the heap and should eventually be freed) into
/// `error.message` /// `error.message`
/// # Safety pub unsafe fn translate_opt_result<T, E>(result: Result<Option<T>, E>, error: *mut ExternError) -> *mut T
/// Be afraid... TODO where E: Display {
pub unsafe fn translate_opt_result<T, E>(
result: Result<Option<T>, E>,
error: *mut ExternError,
) -> *mut T
where
E: Display,
{
assert!(!error.is_null(), "Error output parameter is not optional"); assert!(!error.is_null(), "Error output parameter is not optional");
let error = &mut *error; let error = &mut *error;
error.message = ptr::null_mut(); error.message = ptr::null_mut();
@ -162,14 +148,10 @@ pub mod error {
/// Identical to `translate_result`, but with additional type checking for the case that we have /// Identical to `translate_result`, but with additional type checking for the case that we have
/// a `Result<(), E>` (which we're about to drop on the floor). /// a `Result<(), E>` (which we're about to drop on the floor).
/// # Safety pub unsafe fn translate_void_result<E>(result: Result<(), E>, error: *mut ExternError) where E: Display {
/// Be afraid... TODO
pub unsafe fn translate_void_result<E>(result: Result<(), E>, error: *mut ExternError)
where
E: Display,
{
// Note that Box<T> guarantees that if T is zero sized, it's not heap allocated. So not // Note that Box<T> guarantees that if T is zero sized, it's not heap allocated. So not
// only do we never need to free the return value of this, it would be a problem if someone did. // only do we never need to free the return value of this, it would be a problem if someone did.
translate_result(result, error); translate_result(result, error);
} }
} }

View file

@ -1,57 +0,0 @@
[package]
name = "public_traits"
version = "0.0.2"
workspace = ".."
[lib]
name = "public_traits"
path = "lib.rs"
[features]
default = ["syncable"]
sqlcipher = ["rusqlite/sqlcipher"]
syncable = ["tolstoy_traits", "hyper", "serde_json"]
[dependencies]
failure = "~0.1"
failure_derive = "~0.1"
http = "~0.2"
tokio = { version = "1.8.0", features = ["full"] }
uuid = "~1.0"
[dependencies.rusqlite]
version = "~0.29"
features = ["limits", "bundled"]
[dependencies.hyper]
version = "~0.14"
optional = true
[dependencies.serde_json]
version = "~1.0"
optional = true
[dependencies.edn]
path = "../edn"
[dependencies.core_traits]
path = "../core-traits"
[dependencies.db_traits]
path = "../db-traits"
[dependencies.query_algebrizer_traits]
path = "../query-algebrizer-traits"
[dependencies.query_projector_traits]
path = "../query-projector-traits"
[dependencies.query_pull_traits]
path = "../query-pull-traits"
[dependencies.sql_traits]
path = "../sql-traits"
[dependencies.tolstoy_traits]
path = "../tolstoy-traits"
optional = true

View file

@ -1,227 +0,0 @@
// 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.
#![allow(dead_code)]
use std; // To refer to std::result::Result.
use std::collections::BTreeSet;
use std::error::Error;
use rusqlite;
use uuid;
use edn;
use core_traits::{Attribute, ValueType};
use db_traits::errors::DbError;
use query_algebrizer_traits::errors::AlgebrizerError;
use query_projector_traits::errors::ProjectorError;
use query_pull_traits::errors::PullError;
use sql_traits::errors::SQLError;
#[cfg(feature = "syncable")]
use tolstoy_traits::errors::TolstoyError;
#[cfg(feature = "syncable")]
use hyper;
#[cfg(feature = "syncable")]
use serde_json;
pub type Result<T> = std::result::Result<T, MentatError>;
#[derive(Debug, Fail)]
pub enum MentatError {
#[fail(display = "bad uuid {}", _0)]
BadUuid(String),
#[fail(display = "path {} already exists", _0)]
PathAlreadyExists(String),
#[fail(display = "variables {:?} unbound at query execution time", _0)]
UnboundVariables(BTreeSet<String>),
#[fail(display = "invalid argument name: '{}'", _0)]
InvalidArgumentName(String),
#[fail(display = "unknown attribute: '{}'", _0)]
UnknownAttribute(String),
#[fail(display = "invalid vocabulary version")]
InvalidVocabularyVersion,
#[fail(
display = "vocabulary {}/version {} already has attribute {}, and the requested definition differs",
_0, _1, _2
)]
ConflictingAttributeDefinitions(String, u32, String, Attribute, Attribute),
#[fail(
display = "existing vocabulary {} too new: wanted version {}, got version {}",
_0, _1, _2
)]
ExistingVocabularyTooNew(String, u32, u32),
#[fail(display = "core schema: wanted version {}, got version {:?}", _0, _1)]
UnexpectedCoreSchema(u32, Option<u32>),
#[fail(display = "Lost the transact() race!")]
UnexpectedLostTransactRace,
#[fail(display = "missing core attribute {}", _0)]
MissingCoreVocabulary(edn::query::Keyword),
#[fail(display = "schema changed since query was prepared")]
PreparedQuerySchemaMismatch,
#[fail(
display = "provided value of type {} doesn't match attribute value type {}",
_0, _1
)]
ValueTypeMismatch(ValueType, ValueType),
#[fail(display = "{}", _0)]
IoError(#[cause] std::io::Error),
/// We're just not done yet. Message that the feature is recognized but not yet
/// implemented.
#[fail(display = "not yet implemented: {}", _0)]
NotYetImplemented(String),
// It would be better to capture the underlying `rusqlite::Error`, but that type doesn't
// implement many useful traits, including `Clone`, `Eq`, and `PartialEq`.
#[fail(display = "SQL error: {}, cause: {}", _0, _1)]
RusqliteError(String, String),
#[fail(display = "{}", _0)]
EdnParseError(#[cause] edn::ParseError),
#[fail(display = "{}", _0)]
DbError(#[cause] DbError),
#[fail(display = "{}", _0)]
AlgebrizerError(#[cause] AlgebrizerError),
#[fail(display = "{}", _0)]
ProjectorError(#[cause] ProjectorError),
#[fail(display = "{}", _0)]
PullError(#[cause] PullError),
#[fail(display = "{}", _0)]
SQLError(#[cause] SQLError),
#[fail(display = "{}", _0)]
UuidError(#[cause] uuid::Error),
#[cfg(feature = "syncable")]
#[fail(display = "{}", _0)]
TolstoyError(#[cause] TolstoyError),
#[cfg(feature = "syncable")]
#[fail(display = "{}", _0)]
NetworkError(#[cause] hyper::Error),
#[cfg(feature = "syncable")]
#[fail(display = "{}", _0)]
UriError(#[cause] http::uri::InvalidUri),
#[cfg(feature = "syncable")]
#[fail(display = "{}", _0)]
SerializationError(#[cause] serde_json::Error),
}
impl From<std::io::Error> for MentatError {
fn from(error: std::io::Error) -> Self {
MentatError::IoError(error)
}
}
impl From<rusqlite::Error> for MentatError {
fn from(error: rusqlite::Error) -> Self {
let cause = match error.source() {
Some(e) => e.to_string(),
None => "".to_string(),
};
MentatError::RusqliteError(error.to_string(), cause)
}
}
impl From<uuid::Error> for MentatError {
fn from(error: uuid::Error) -> Self {
MentatError::UuidError(error)
}
}
impl From<edn::ParseError> for MentatError {
fn from(error: edn::ParseError) -> Self {
MentatError::EdnParseError(error)
}
}
impl From<DbError> for MentatError {
fn from(error: DbError) -> Self {
MentatError::DbError(error)
}
}
impl From<AlgebrizerError> for MentatError {
fn from(error: AlgebrizerError) -> Self {
MentatError::AlgebrizerError(error)
}
}
impl From<ProjectorError> for MentatError {
fn from(error: ProjectorError) -> Self {
MentatError::ProjectorError(error)
}
}
impl From<PullError> for MentatError {
fn from(error: PullError) -> Self {
MentatError::PullError(error)
}
}
impl From<SQLError> for MentatError {
fn from(error: SQLError) -> Self {
MentatError::SQLError(error)
}
}
#[cfg(feature = "syncable")]
impl From<TolstoyError> for MentatError {
fn from(error: TolstoyError) -> Self {
MentatError::TolstoyError(error)
}
}
#[cfg(feature = "syncable")]
impl From<serde_json::Error> for MentatError {
fn from(error: serde_json::Error) -> Self {
MentatError::SerializationError(error)
}
}
#[cfg(feature = "syncable")]
impl From<hyper::Error> for MentatError {
fn from(error: hyper::Error) -> Self {
MentatError::NetworkError(error)
}
}
#[cfg(feature = "syncable")]
impl From<http::uri::InvalidUri> for MentatError {
fn from(error: http::uri::InvalidUri) -> Self {
MentatError::UriError(error)
}
}

View file

@ -1,35 +0,0 @@
// Copyright 2018 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.
extern crate failure;
#[macro_use]
extern crate failure_derive;
extern crate rusqlite;
extern crate core_traits;
extern crate db_traits;
extern crate edn;
extern crate query_algebrizer_traits;
extern crate query_projector_traits;
extern crate query_pull_traits;
extern crate sql_traits;
extern crate uuid;
#[cfg(feature = "syncable")]
extern crate tolstoy_traits;
#[cfg(feature = "syncable")]
extern crate hyper;
#[cfg(feature = "syncable")]
extern crate serde_json;
pub mod errors;

View file

@ -1,18 +0,0 @@
[package]
name = "query_algebrizer_traits"
version = "0.0.2"
workspace = ".."
[lib]
name = "query_algebrizer_traits"
path = "lib.rs"
[dependencies]
failure = "~0.1"
failure_derive = "~0.1"
[dependencies.edn]
path = "../edn"
[dependencies.core_traits]
path = "../core-traits"

View file

@ -1,22 +1,17 @@
[package] [package]
name = "mentat_query_algebrizer" name = "mentat_query_algebrizer"
version = "0.0.2" version = "0.0.1"
workspace = ".." workspace = ".."
[dependencies] [dependencies]
failure = "~0.1" failure = "0.1.1"
failure_derive = "0.1.1"
[dependencies.edn]
path = "../edn"
[dependencies.mentat_core] [dependencies.mentat_core]
path = "../core" path = "../core"
[dependencies.core_traits] [dependencies.mentat_query]
path = "../core-traits" path = "../query"
[dependencies.query_algebrizer_traits]
path = "../query-algebrizer-traits"
[dev-dependencies] [dev-dependencies]
itertools = "~0.10" itertools = "0.7"

View file

@ -8,30 +8,46 @@
// CONDITIONS OF ANY KIND, either express or implied. See the License for the // CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License. // specific language governing permissions and limitations under the License.
use core_traits::{TypedValue, ValueType, ValueTypeSet}; use mentat_core::{
HasSchema,
Schema,
SQLValueType,
TypedValue,
ValueType,
ValueTypeSet,
};
use mentat_core::{HasSchema, SQLValueType, Schema}; use mentat_query::{
FnArg,
NonIntegerConstant,
Variable,
};
use edn::query::{FnArg, NonIntegerConstant, Variable}; use clauses::{
ConjoiningClauses,
};
use crate::clauses::ConjoiningClauses; use errors::{
AlgebrizerError,
Result,
};
use query_algebrizer_traits::errors::{AlgebrizerError, Result}; use types::{
EmptyBecause,
use crate::types::EmptyBecause; };
macro_rules! coerce_to_typed_value { macro_rules! coerce_to_typed_value {
($var: ident, $val: ident, $types: expr, $type: path, $constructor: path) => {{ ($var: ident, $val: ident, $types: expr, $type: path, $constructor: path) => { {
Ok(if !$types.contains($type) { Ok(if !$types.contains($type) {
Impossible(EmptyBecause::TypeMismatch { Impossible(EmptyBecause::TypeMismatch {
var: $var.clone(), var: $var.clone(),
existing: $types, existing: $types,
desired: ValueTypeSet::of_one($type), desired: ValueTypeSet::of_one($type),
}) })
} else { } else {
Val($constructor($val).into()) Val($constructor($val).into())
}) })
}}; } }
} }
pub(crate) trait ValueTypes { pub(crate) trait ValueTypes {
@ -41,48 +57,43 @@ pub(crate) trait ValueTypes {
impl ValueTypes for FnArg { impl ValueTypes for FnArg {
fn potential_types(&self, schema: &Schema) -> Result<ValueTypeSet> { fn potential_types(&self, schema: &Schema) -> Result<ValueTypeSet> {
Ok(match self { Ok(match self {
&FnArg::EntidOrInteger(x) => { &FnArg::EntidOrInteger(x) => {
if ValueType::Ref.accommodates_integer(x) { if ValueType::Ref.accommodates_integer(x) {
// TODO: also see if it's a valid entid? // TODO: also see if it's a valid entid?
ValueTypeSet::of_longs() ValueTypeSet::of_longs()
} else { } else {
ValueTypeSet::of_one(ValueType::Long) ValueTypeSet::of_one(ValueType::Long)
} }
} },
&FnArg::IdentOrKeyword(ref x) => { &FnArg::IdentOrKeyword(ref x) => {
if schema.get_entid(x).is_some() { if schema.get_entid(x).is_some() {
ValueTypeSet::of_keywords() ValueTypeSet::of_keywords()
} else { } else {
ValueTypeSet::of_one(ValueType::Keyword) ValueTypeSet::of_one(ValueType::Keyword)
} }
} },
&FnArg::Variable(_) => ValueTypeSet::any(), &FnArg::Variable(_) => {
ValueTypeSet::any()
},
&FnArg::Constant(NonIntegerConstant::BigInteger(_)) => { &FnArg::Constant(NonIntegerConstant::BigInteger(_)) => {
// Not yet implemented. // Not yet implemented.
bail!(AlgebrizerError::UnsupportedArgument) bail!(AlgebrizerError::UnsupportedArgument)
} },
// These don't make sense here. TODO: split FnArg into scalar and non-scalar… // These don't make sense here. TODO: split FnArg into scalar and non-scalar…
&FnArg::Vector(_) | &FnArg::SrcVar(_) => bail!(AlgebrizerError::UnsupportedArgument), &FnArg::Vector(_) |
&FnArg::SrcVar(_) => bail!(AlgebrizerError::UnsupportedArgument),
// These are all straightforward. // These are all straightforward.
&FnArg::Constant(NonIntegerConstant::Boolean(_)) => { &FnArg::Constant(NonIntegerConstant::Boolean(_)) => ValueTypeSet::of_one(ValueType::Boolean),
ValueTypeSet::of_one(ValueType::Boolean) &FnArg::Constant(NonIntegerConstant::Instant(_)) => ValueTypeSet::of_one(ValueType::Instant),
} &FnArg::Constant(NonIntegerConstant::Uuid(_)) => ValueTypeSet::of_one(ValueType::Uuid),
&FnArg::Constant(NonIntegerConstant::Instant(_)) => { &FnArg::Constant(NonIntegerConstant::Float(_)) => ValueTypeSet::of_one(ValueType::Double),
ValueTypeSet::of_one(ValueType::Instant) &FnArg::Constant(NonIntegerConstant::Text(_)) => ValueTypeSet::of_one(ValueType::String),
} })
&FnArg::Constant(NonIntegerConstant::Uuid(_)) => ValueTypeSet::of_one(ValueType::Uuid),
&FnArg::Constant(NonIntegerConstant::Float(_)) => {
ValueTypeSet::of_one(ValueType::Double)
}
&FnArg::Constant(NonIntegerConstant::Text(_)) => {
ValueTypeSet::of_one(ValueType::String)
}
})
} }
} }
@ -97,13 +108,7 @@ impl ConjoiningClauses {
/// The conversion depends on, and can fail because of: /// The conversion depends on, and can fail because of:
/// - Existing known types of a variable to which this arg will be bound. /// - Existing known types of a variable to which this arg will be bound.
/// - Existing bindings of a variable `FnArg`. /// - Existing bindings of a variable `FnArg`.
pub(crate) fn typed_value_from_arg<'s>( pub(crate) fn typed_value_from_arg<'s>(&self, schema: &'s Schema, var: &Variable, arg: FnArg, known_types: ValueTypeSet) -> Result<ValueConversion> {
&self,
schema: &'s Schema,
var: &Variable,
arg: FnArg,
known_types: ValueTypeSet,
) -> Result<ValueConversion> {
use self::ValueConversion::*; use self::ValueConversion::*;
if known_types.is_empty() { if known_types.is_empty() {
// If this happens, it likely means the pattern has already failed! // If this happens, it likely means the pattern has already failed!
@ -116,7 +121,7 @@ impl ConjoiningClauses {
let constrained_types; let constrained_types;
if let Some(required) = self.required_types.get(var) { if let Some(required) = self.required_types.get(var) {
constrained_types = known_types.intersection(*required); constrained_types = known_types.intersection(required);
} else { } else {
constrained_types = known_types; constrained_types = known_types;
} }
@ -124,24 +129,22 @@ impl ConjoiningClauses {
match arg { match arg {
// Longs are potentially ambiguous: they might be longs or entids. // Longs are potentially ambiguous: they might be longs or entids.
FnArg::EntidOrInteger(x) => { FnArg::EntidOrInteger(x) => {
match ( match (ValueType::Ref.accommodates_integer(x),
ValueType::Ref.accommodates_integer(x), constrained_types.contains(ValueType::Ref),
constrained_types.contains(ValueType::Ref), constrained_types.contains(ValueType::Long)) {
constrained_types.contains(ValueType::Long),
) {
(true, true, true) => { (true, true, true) => {
// Ambiguous: this arg could be an entid or a long. // Ambiguous: this arg could be an entid or a long.
// We default to long. // We default to long.
Ok(Val(TypedValue::Long(x))) Ok(Val(TypedValue::Long(x)))
} },
(true, true, false) => { (true, true, false) => {
// This can only be a ref. // This can only be a ref.
Ok(Val(TypedValue::Ref(x))) Ok(Val(TypedValue::Ref(x)))
} },
(_, false, true) => { (_, false, true) => {
// This can only be a long. // This can only be a long.
Ok(Val(TypedValue::Long(x))) Ok(Val(TypedValue::Long(x)))
} },
(false, true, _) => { (false, true, _) => {
// This isn't a valid ref, but that's the type to which this must conform! // This isn't a valid ref, but that's the type to which this must conform!
Ok(Impossible(EmptyBecause::TypeMismatch { Ok(Impossible(EmptyBecause::TypeMismatch {
@ -149,7 +152,7 @@ impl ConjoiningClauses {
existing: known_types, existing: known_types,
desired: ValueTypeSet::of_longs(), desired: ValueTypeSet::of_longs(),
})) }))
} },
(_, false, false) => { (_, false, false) => {
// Non-overlapping type sets. // Non-overlapping type sets.
Ok(Impossible(EmptyBecause::TypeMismatch { Ok(Impossible(EmptyBecause::TypeMismatch {
@ -157,36 +160,38 @@ impl ConjoiningClauses {
existing: known_types, existing: known_types,
desired: ValueTypeSet::of_longs(), desired: ValueTypeSet::of_longs(),
})) }))
} },
} }
} },
// If you definitely want to look up an ident, do it before running the query. // If you definitely want to look up an ident, do it before running the query.
FnArg::IdentOrKeyword(x) => { FnArg::IdentOrKeyword(x) => {
match ( match (constrained_types.contains(ValueType::Ref),
constrained_types.contains(ValueType::Ref), constrained_types.contains(ValueType::Keyword)) {
constrained_types.contains(ValueType::Keyword),
) {
(true, true) => { (true, true) => {
// Ambiguous: this could be a keyword or an ident. // Ambiguous: this could be a keyword or an ident.
// Default to keyword. // Default to keyword.
Ok(Val(x.into())) Ok(Val(x.into()))
} },
(true, false) => { (true, false) => {
// This can only be an ident. Look it up! // This can only be an ident. Look it up!
match schema.get_entid(&x).map(|k| k.into()) { match schema.get_entid(&x).map(|k| k.into()) {
Some(e) => Ok(Val(e)), Some(e) => Ok(Val(e)),
None => Ok(Impossible(EmptyBecause::UnresolvedIdent(x.clone()))), None => Ok(Impossible(EmptyBecause::UnresolvedIdent(x.clone()))),
} }
} },
(false, true) => Ok(Val(TypedValue::Keyword(x.into()))), (false, true) => {
(false, false) => Ok(Impossible(EmptyBecause::TypeMismatch { Ok(Val(TypedValue::Keyword(x.into())))
var: var.clone(), },
existing: known_types, (false, false) => {
desired: ValueTypeSet::of_keywords(), Ok(Impossible(EmptyBecause::TypeMismatch {
})), var: var.clone(),
existing: known_types,
desired: ValueTypeSet::of_keywords(),
}))
},
} }
} },
FnArg::Variable(in_var) => { FnArg::Variable(in_var) => {
// TODO: technically you could ground an existing variable inside the query…. // TODO: technically you could ground an existing variable inside the query….
@ -201,32 +206,33 @@ impl ConjoiningClauses {
// This is a restriction we will eventually relax: we don't yet have a way // This is a restriction we will eventually relax: we don't yet have a way
// to collect variables as part of a computed table or substitution. // to collect variables as part of a computed table or substitution.
bail!(AlgebrizerError::UnboundVariable((*in_var.0).clone())) bail!(AlgebrizerError::UnboundVariable((*in_var.0).clone()))
} },
} }
} },
// This isn't implemented yet. // This isn't implemented yet.
FnArg::Constant(NonIntegerConstant::BigInteger(_)) => unimplemented!(), FnArg::Constant(NonIntegerConstant::BigInteger(_)) => unimplemented!(),
// These don't make sense here. // These don't make sense here.
FnArg::Vector(_) | FnArg::SrcVar(_) => bail!(AlgebrizerError::InvalidGroundConstant), FnArg::Vector(_) |
FnArg::SrcVar(_) => bail!(AlgebrizerError::InvalidGroundConstant),
// These are all straightforward. // These are all straightforward.
FnArg::Constant(NonIntegerConstant::Boolean(x)) => { FnArg::Constant(NonIntegerConstant::Boolean(x)) => {
coerce_to_typed_value!(var, x, known_types, ValueType::Boolean, TypedValue::Boolean) coerce_to_typed_value!(var, x, known_types, ValueType::Boolean, TypedValue::Boolean)
} },
FnArg::Constant(NonIntegerConstant::Instant(x)) => { FnArg::Constant(NonIntegerConstant::Instant(x)) => {
coerce_to_typed_value!(var, x, known_types, ValueType::Instant, TypedValue::Instant) coerce_to_typed_value!(var, x, known_types, ValueType::Instant, TypedValue::Instant)
} },
FnArg::Constant(NonIntegerConstant::Uuid(x)) => { FnArg::Constant(NonIntegerConstant::Uuid(x)) => {
coerce_to_typed_value!(var, x, known_types, ValueType::Uuid, TypedValue::Uuid) coerce_to_typed_value!(var, x, known_types, ValueType::Uuid, TypedValue::Uuid)
} },
FnArg::Constant(NonIntegerConstant::Float(x)) => { FnArg::Constant(NonIntegerConstant::Float(x)) => {
coerce_to_typed_value!(var, x, known_types, ValueType::Double, TypedValue::Double) coerce_to_typed_value!(var, x, known_types, ValueType::Double, TypedValue::Double)
} },
FnArg::Constant(NonIntegerConstant::Text(x)) => { FnArg::Constant(NonIntegerConstant::Text(x)) => {
coerce_to_typed_value!(var, x, known_types, ValueType::String, TypedValue::String) coerce_to_typed_value!(var, x, known_types, ValueType::String, TypedValue::String)
} },
} }
} }
} }

View file

@ -8,50 +8,62 @@
// CONDITIONS OF ANY KIND, either express or implied. See the License for the // CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License. // specific language governing permissions and limitations under the License.
use core_traits::{TypedValue, ValueType}; use mentat_core::{
HasSchema,
use mentat_core::HasSchema; TypedValue,
ValueType,
};
use mentat_core::util::Either; use mentat_core::util::Either;
use edn::query::{Binding, FnArg, NonIntegerConstant, SrcVar, VariableOrPlaceholder, WhereFn}; use mentat_query::{
Binding,
use crate::clauses::ConjoiningClauses; FnArg,
NonIntegerConstant,
use query_algebrizer_traits::errors::{AlgebrizerError, BindingError, Result}; SrcVar,
VariableOrPlaceholder,
use crate::types::{ WhereFn,
Column, ColumnConstraint, DatomsColumn, DatomsTable, EmptyBecause, FulltextColumn,
QualifiedAlias, QueryValue, SourceAlias,
}; };
use crate::Known; use clauses::{
ConjoiningClauses,
};
use errors::{
AlgebrizerError,
BindingError,
Result,
};
use types::{
Column,
ColumnConstraint,
DatomsColumn,
DatomsTable,
EmptyBecause,
FulltextColumn,
QualifiedAlias,
QueryValue,
SourceAlias,
};
use Known;
impl ConjoiningClauses { impl ConjoiningClauses {
#[allow(unused_variables)] #[allow(unused_variables)]
pub(crate) fn apply_fulltext(&mut self, known: Known, where_fn: WhereFn) -> Result<()> { pub(crate) fn apply_fulltext(&mut self, known: Known, where_fn: WhereFn) -> Result<()> {
if where_fn.args.len() != 3 { if where_fn.args.len() != 3 {
bail!(AlgebrizerError::InvalidNumberOfArguments( bail!(AlgebrizerError::InvalidNumberOfArguments(where_fn.operator.clone(), where_fn.args.len(), 3));
where_fn.operator.clone(),
where_fn.args.len(),
3
));
} }
if where_fn.binding.is_empty() { if where_fn.binding.is_empty() {
// The binding must introduce at least one bound variable. // The binding must introduce at least one bound variable.
bail!(AlgebrizerError::InvalidBinding( bail!(AlgebrizerError::InvalidBinding(where_fn.operator.clone(), BindingError::NoBoundVariable));
where_fn.operator.clone(),
BindingError::NoBoundVariable
));
} }
if !where_fn.binding.is_valid() { if !where_fn.binding.is_valid() {
// The binding must not duplicate bound variables. // The binding must not duplicate bound variables.
bail!(AlgebrizerError::InvalidBinding( bail!(AlgebrizerError::InvalidBinding(where_fn.operator.clone(), BindingError::RepeatedBoundVariable));
where_fn.operator.clone(),
BindingError::RepeatedBoundVariable
));
} }
// We should have exactly four bindings. Destructure them now. // We should have exactly four bindings. Destructure them now.
@ -59,52 +71,38 @@ impl ConjoiningClauses {
Binding::BindRel(bindings) => { Binding::BindRel(bindings) => {
let bindings_count = bindings.len(); let bindings_count = bindings.len();
if bindings_count < 1 || bindings_count > 4 { if bindings_count < 1 || bindings_count > 4 {
bail!(AlgebrizerError::InvalidBinding( bail!(AlgebrizerError::InvalidBinding(where_fn.operator.clone(),
where_fn.operator.clone(),
BindingError::InvalidNumberOfBindings { BindingError::InvalidNumberOfBindings {
number: bindings.len(), number: bindings.len(),
expected: 4, expected: 4,
} })
)); );
} }
bindings bindings
} },
Binding::BindScalar(_) | Binding::BindTuple(_) | Binding::BindColl(_) => { Binding::BindScalar(_) |
bail!(AlgebrizerError::InvalidBinding( Binding::BindTuple(_) |
where_fn.operator.clone(), Binding::BindColl(_) => bail!(AlgebrizerError::InvalidBinding(where_fn.operator.clone(), BindingError::ExpectedBindRel)),
BindingError::ExpectedBindRel
))
}
}; };
let mut bindings = bindings.into_iter(); let mut bindings = bindings.into_iter();
let b_entity = bindings.next().unwrap(); let b_entity = bindings.next().unwrap();
let b_value = bindings let b_value = bindings.next().unwrap_or(VariableOrPlaceholder::Placeholder);
.next() let b_tx = bindings.next().unwrap_or(VariableOrPlaceholder::Placeholder);
.unwrap_or(VariableOrPlaceholder::Placeholder); let b_score = bindings.next().unwrap_or(VariableOrPlaceholder::Placeholder);
let b_tx = bindings
.next()
.unwrap_or(VariableOrPlaceholder::Placeholder);
let b_score = bindings
.next()
.unwrap_or(VariableOrPlaceholder::Placeholder);
let mut args = where_fn.args.into_iter(); let mut args = where_fn.args.into_iter();
// TODO(gburd): process source variables. // TODO: process source variables.
match args.next().unwrap() { match args.next().unwrap() {
FnArg::SrcVar(SrcVar::DefaultSrc) => {} FnArg::SrcVar(SrcVar::DefaultSrc) => {},
_ => bail!(AlgebrizerError::InvalidArgument( _ => bail!(AlgebrizerError::InvalidArgument(where_fn.operator.clone(), "source variable", 0)),
where_fn.operator.clone(),
"source variable",
0
)),
} }
let schema = known.schema; let schema = known.schema;
// TODO: accept placeholder and set of attributes. Alternately, consider putting the search // TODO: accept placeholder and set of attributes. Alternately, consider putting the search
// term before the attribute arguments and collect the (variadic) attributes into a set. // term before the attribute arguments and collect the (variadic) attributes into a set.
// let a: Entid = self.resolve_attribute_argument(&where_fn.operator, 1, args.next().unwrap())?; // let a: Entid = self.resolve_attribute_argument(&where_fn.operator, 1, args.next().unwrap())?;
// //
// TODO: improve the expression of this matching, possibly by using attribute_for_* uniformly. // TODO: improve the expression of this matching, possibly by using attribute_for_* uniformly.
let a = match args.next().unwrap() { let a = match args.next().unwrap() {
@ -116,27 +114,25 @@ impl ConjoiningClauses {
// TODO: allow non-constant attributes. // TODO: allow non-constant attributes.
match self.bound_value(&v) { match self.bound_value(&v) {
Some(TypedValue::Ref(entid)) => Some(entid), Some(TypedValue::Ref(entid)) => Some(entid),
Some(tv) => bail!(AlgebrizerError::InputTypeDisagreement( Some(tv) => {
v.name(), bail!(AlgebrizerError::InputTypeDisagreement(v.name().clone(), ValueType::Ref, tv.value_type()))
ValueType::Ref, },
tv.value_type() None => {
)), bail!(AlgebrizerError::UnboundVariable((*v.0).clone()))
None => bail!(AlgebrizerError::UnboundVariable((*v.0).clone())), }
} }
} },
_ => None, _ => None,
}; };
// An unknown ident, or an entity that isn't present in the store, or isn't a fulltext // An unknown ident, or an entity that isn't present in the store, or isn't a fulltext
// attribute, is likely enough to be a coding error that we choose to bail instead of // attribute, is likely enough to be a coding error that we choose to bail instead of
// marking the pattern as known-empty. // marking the pattern as known-empty.
let op = where_fn.operator.clone(); //TODO(gburd): remove me... let a = a.ok_or(AlgebrizerError::InvalidArgument(where_fn.operator.clone(), "attribute", 1))?;
let a = a.ok_or_else(move || AlgebrizerError::InvalidArgument(op, "attribute", 1))?; let attribute = schema.attribute_for_entid(a)
let op = where_fn.operator.clone(); //TODO(gburd): remove me... .cloned()
let attribute = schema .ok_or(AlgebrizerError::InvalidArgument(where_fn.operator.clone(),
.attribute_for_entid(a) "attribute", 1))?;
.cloned()
.ok_or_else(move || AlgebrizerError::InvalidArgument(op, "attribute", 1))?;
if !attribute.fulltext { if !attribute.fulltext {
// We can never get results from a non-fulltext attribute! // We can never get results from a non-fulltext attribute!
@ -150,27 +146,16 @@ impl ConjoiningClauses {
// We do a fulltext lookup by joining the fulltext values table against datoms -- just // We do a fulltext lookup by joining the fulltext values table against datoms -- just
// like applying a pattern, but two tables contribute instead of one. // like applying a pattern, but two tables contribute instead of one.
self.from.push(SourceAlias( self.from.push(SourceAlias(DatomsTable::FulltextValues, fulltext_values_alias.clone()));
DatomsTable::FulltextValues, self.from.push(SourceAlias(DatomsTable::Datoms, datoms_table_alias.clone()));
fulltext_values_alias.clone(),
));
self.from
.push(SourceAlias(DatomsTable::Datoms, datoms_table_alias.clone()));
// TODO: constrain the type in the more general cases (e.g., `a` is a var). // TODO: constrain the type in the more general cases (e.g., `a` is a var).
self.constrain_attribute(datoms_table_alias.clone(), a); self.constrain_attribute(datoms_table_alias.clone(), a);
// Join the datoms table to the fulltext values table. // Join the datoms table to the fulltext values table.
self.wheres.add_intersection(ColumnConstraint::Equals( self.wheres.add_intersection(ColumnConstraint::Equals(
QualifiedAlias( QualifiedAlias(datoms_table_alias.clone(), Column::Fixed(DatomsColumn::Value)),
datoms_table_alias.clone(), QueryValue::Column(QualifiedAlias(fulltext_values_alias.clone(), Column::Fulltext(FulltextColumn::Rowid)))));
Column::Fixed(DatomsColumn::Value),
),
QueryValue::Column(QualifiedAlias(
fulltext_values_alias.clone(),
Column::Fulltext(FulltextColumn::Rowid),
)),
));
// `search` is either text or a variable. // `search` is either text or a variable.
// If it's simple text, great. // If it's simple text, great.
@ -179,24 +164,18 @@ impl ConjoiningClauses {
// - It's not already bound, but it's a defined input of type Text. Not yet implemented: TODO. // - It's not already bound, but it's a defined input of type Text. Not yet implemented: TODO.
// - It's not bound. The query cannot be algebrized. // - It's not bound. The query cannot be algebrized.
let search: Either<TypedValue, QualifiedAlias> = match args.next().unwrap() { let search: Either<TypedValue, QualifiedAlias> = match args.next().unwrap() {
FnArg::Constant(NonIntegerConstant::Text(s)) => Either::Left(TypedValue::String(s)), FnArg::Constant(NonIntegerConstant::Text(s)) => {
Either::Left(TypedValue::String(s))
},
FnArg::Variable(in_var) => { FnArg::Variable(in_var) => {
match self.bound_value(&in_var) { match self.bound_value(&in_var) {
Some(t @ TypedValue::String(_)) => Either::Left(t), Some(t @ TypedValue::String(_)) => Either::Left(t),
Some(_) => bail!(AlgebrizerError::InvalidArgument( Some(_) => bail!(AlgebrizerError::InvalidArgument(where_fn.operator.clone(), "string", 2)),
where_fn.operator.clone(),
"string",
2
)),
None => { None => {
// Regardless of whether we'll be providing a string later, or the value // Regardless of whether we'll be providing a string later, or the value
// comes from a column, it must be a string. // comes from a column, it must be a string.
if self.known_type(&in_var) != Some(ValueType::String) { if self.known_type(&in_var) != Some(ValueType::String) {
bail!(AlgebrizerError::InvalidArgument( bail!(AlgebrizerError::InvalidArgument(where_fn.operator.clone(), "string", 2))
where_fn.operator.clone(),
"string",
2
))
} }
if self.input_variables.contains(&in_var) { if self.input_variables.contains(&in_var) {
@ -206,24 +185,18 @@ impl ConjoiningClauses {
} else { } else {
// It must be bound earlier in the query. We already established that // It must be bound earlier in the query. We already established that
// it must be a string column. // it must be a string column.
if let Some(binding) = self if let Some(binding) = self.column_bindings
.column_bindings .get(&in_var)
.get(&in_var) .and_then(|bindings| bindings.get(0).cloned()) {
.and_then(|bindings| bindings.get(0).cloned())
{
Either::Right(binding) Either::Right(binding)
} else { } else {
bail!(AlgebrizerError::UnboundVariable((*in_var.0).clone())) bail!(AlgebrizerError::UnboundVariable((*in_var.0).clone()))
} }
} }
} },
} }
} },
_ => bail!(AlgebrizerError::InvalidArgument( _ => bail!(AlgebrizerError::InvalidArgument(where_fn.operator.clone(), "string", 2)),
where_fn.operator.clone(),
"string",
2
)),
}; };
let qv = match search { let qv = match search {
@ -231,13 +204,9 @@ impl ConjoiningClauses {
Either::Right(qa) => QueryValue::Column(qa), Either::Right(qa) => QueryValue::Column(qa),
}; };
let constraint = ColumnConstraint::Matches( let constraint = ColumnConstraint::Matches(QualifiedAlias(fulltext_values_alias.clone(),
QualifiedAlias( Column::Fulltext(FulltextColumn::Text)),
fulltext_values_alias.clone(), qv);
Column::Fulltext(FulltextColumn::Text),
),
qv,
);
self.wheres.add_intersection(constraint); self.wheres.add_intersection(constraint);
if let VariableOrPlaceholder::Variable(ref var) = b_entity { if let VariableOrPlaceholder::Variable(ref var) = b_entity {
@ -247,12 +216,7 @@ impl ConjoiningClauses {
return Ok(()); return Ok(());
} }
self.bind_column_to_var( self.bind_column_to_var(schema, datoms_table_alias.clone(), DatomsColumn::Entity, var.clone());
schema,
datoms_table_alias.clone(),
DatomsColumn::Entity,
var.clone(),
);
} }
if let VariableOrPlaceholder::Variable(ref var) = b_value { if let VariableOrPlaceholder::Variable(ref var) = b_value {
@ -262,12 +226,7 @@ impl ConjoiningClauses {
return Ok(()); return Ok(());
} }
self.bind_column_to_var( self.bind_column_to_var(schema, fulltext_values_alias.clone(), Column::Fulltext(FulltextColumn::Text), var.clone());
schema,
fulltext_values_alias,
Column::Fulltext(FulltextColumn::Text),
var.clone(),
);
} }
if let VariableOrPlaceholder::Variable(ref var) = b_tx { if let VariableOrPlaceholder::Variable(ref var) = b_tx {
@ -277,7 +236,7 @@ impl ConjoiningClauses {
return Ok(()); return Ok(());
} }
self.bind_column_to_var(schema, datoms_table_alias, DatomsColumn::Tx, var.clone()); self.bind_column_to_var(schema, datoms_table_alias.clone(), DatomsColumn::Tx, var.clone());
} }
if let VariableOrPlaceholder::Variable(ref var) = b_score { if let VariableOrPlaceholder::Variable(ref var) = b_score {
@ -286,10 +245,7 @@ impl ConjoiningClauses {
// We do not allow the score to be bound. // We do not allow the score to be bound.
if self.value_bindings.contains_key(var) || self.input_variables.contains(var) { if self.value_bindings.contains_key(var) || self.input_variables.contains(var) {
bail!(AlgebrizerError::InvalidBinding( bail!(AlgebrizerError::InvalidBinding(var.name(), BindingError::UnexpectedBinding));
var.name(),
BindingError::UnexpectedBinding
));
} }
// We bind the value ourselves. This handily takes care of substituting into existing uses. // We bind the value ourselves. This handily takes care of substituting into existing uses.
@ -305,13 +261,24 @@ impl ConjoiningClauses {
mod testing { mod testing {
use super::*; use super::*;
use core_traits::{Attribute, ValueType}; use mentat_core::{
Attribute,
Schema,
ValueType,
};
use mentat_core::Schema; use mentat_query::{
Binding,
FnArg,
Keyword,
PlainSymbol,
Variable,
};
use edn::query::{Binding, FnArg, Keyword, PlainSymbol, Variable}; use clauses::{
add_attribute,
use crate::clauses::{add_attribute, associate_ident}; associate_ident,
};
#[test] #[test]
fn test_apply_fulltext() { fn test_apply_fulltext() {
@ -319,49 +286,35 @@ mod testing {
let mut schema = Schema::default(); let mut schema = Schema::default();
associate_ident(&mut schema, Keyword::namespaced("foo", "bar"), 101); associate_ident(&mut schema, Keyword::namespaced("foo", "bar"), 101);
add_attribute( add_attribute(&mut schema, 101, Attribute {
&mut schema, value_type: ValueType::String,
101, fulltext: false,
Attribute { ..Default::default()
value_type: ValueType::String, });
fulltext: false,
..Default::default()
},
);
associate_ident(&mut schema, Keyword::namespaced("foo", "fts"), 100); associate_ident(&mut schema, Keyword::namespaced("foo", "fts"), 100);
add_attribute( add_attribute(&mut schema, 100, Attribute {
&mut schema, value_type: ValueType::String,
100, index: true,
Attribute { fulltext: true,
value_type: ValueType::String, ..Default::default()
index: true, });
fulltext: true,
..Default::default()
},
);
let known = Known::for_schema(&schema); let known = Known::for_schema(&schema);
let op = PlainSymbol::plain("fulltext"); let op = PlainSymbol::plain("fulltext");
cc.apply_fulltext( cc.apply_fulltext(known, WhereFn {
known, operator: op,
WhereFn { args: vec![
operator: op, FnArg::SrcVar(SrcVar::DefaultSrc),
args: vec![ FnArg::IdentOrKeyword(Keyword::namespaced("foo", "fts")),
FnArg::SrcVar(SrcVar::DefaultSrc), FnArg::Constant("needle".into()),
FnArg::IdentOrKeyword(Keyword::namespaced("foo", "fts")), ],
FnArg::Constant("needle".into()), binding: Binding::BindRel(vec![VariableOrPlaceholder::Variable(Variable::from_valid_name("?entity")),
], VariableOrPlaceholder::Variable(Variable::from_valid_name("?value")),
binding: Binding::BindRel(vec![ VariableOrPlaceholder::Variable(Variable::from_valid_name("?tx")),
VariableOrPlaceholder::Variable(Variable::from_valid_name("?entity")), VariableOrPlaceholder::Variable(Variable::from_valid_name("?score"))]),
VariableOrPlaceholder::Variable(Variable::from_valid_name("?value")), }).expect("to be able to apply_fulltext");
VariableOrPlaceholder::Variable(Variable::from_valid_name("?tx")),
VariableOrPlaceholder::Variable(Variable::from_valid_name("?score")),
]),
},
)
.expect("to be able to apply_fulltext");
assert!(!cc.is_known_empty()); assert!(!cc.is_known_empty());
@ -372,136 +325,54 @@ mod testing {
let clauses = cc.wheres; let clauses = cc.wheres;
assert_eq!(clauses.len(), 3); assert_eq!(clauses.len(), 3);
assert_eq!( assert_eq!(clauses.0[0], ColumnConstraint::Equals(QualifiedAlias("datoms01".to_string(), Column::Fixed(DatomsColumn::Attribute)),
clauses.0[0], QueryValue::Entid(100)).into());
ColumnConstraint::Equals( assert_eq!(clauses.0[1], ColumnConstraint::Equals(QualifiedAlias("datoms01".to_string(), Column::Fixed(DatomsColumn::Value)),
QualifiedAlias( QueryValue::Column(QualifiedAlias("fulltext_values00".to_string(), Column::Fulltext(FulltextColumn::Rowid)))).into());
"datoms01".to_string(), assert_eq!(clauses.0[2], ColumnConstraint::Matches(QualifiedAlias("fulltext_values00".to_string(), Column::Fulltext(FulltextColumn::Text)),
Column::Fixed(DatomsColumn::Attribute) QueryValue::TypedValue("needle".into())).into());
),
QueryValue::Entid(100)
)
.into()
);
assert_eq!(
clauses.0[1],
ColumnConstraint::Equals(
QualifiedAlias("datoms01".to_string(), Column::Fixed(DatomsColumn::Value)),
QueryValue::Column(QualifiedAlias(
"fulltext_values00".to_string(),
Column::Fulltext(FulltextColumn::Rowid)
))
)
.into()
);
assert_eq!(
clauses.0[2],
ColumnConstraint::Matches(
QualifiedAlias(
"fulltext_values00".to_string(),
Column::Fulltext(FulltextColumn::Text)
),
QueryValue::TypedValue("needle".into())
)
.into()
);
let bindings = cc.column_bindings; let bindings = cc.column_bindings;
assert_eq!(bindings.len(), 3); assert_eq!(bindings.len(), 3);
assert_eq!( assert_eq!(bindings.get(&Variable::from_valid_name("?entity")).expect("column binding for ?entity").clone(),
bindings vec![QualifiedAlias("datoms01".to_string(), Column::Fixed(DatomsColumn::Entity))]);
.get(&Variable::from_valid_name("?entity")) assert_eq!(bindings.get(&Variable::from_valid_name("?value")).expect("column binding for ?value").clone(),
.expect("column binding for ?entity") vec![QualifiedAlias("fulltext_values00".to_string(), Column::Fulltext(FulltextColumn::Text))]);
.clone(), assert_eq!(bindings.get(&Variable::from_valid_name("?tx")).expect("column binding for ?tx").clone(),
vec![QualifiedAlias( vec![QualifiedAlias("datoms01".to_string(), Column::Fixed(DatomsColumn::Tx))]);
"datoms01".to_string(),
Column::Fixed(DatomsColumn::Entity)
)]
);
assert_eq!(
bindings
.get(&Variable::from_valid_name("?value"))
.expect("column binding for ?value")
.clone(),
vec![QualifiedAlias(
"fulltext_values00".to_string(),
Column::Fulltext(FulltextColumn::Text)
)]
);
assert_eq!(
bindings
.get(&Variable::from_valid_name("?tx"))
.expect("column binding for ?tx")
.clone(),
vec![QualifiedAlias(
"datoms01".to_string(),
Column::Fixed(DatomsColumn::Tx)
)]
);
// Score is a value binding. // Score is a value binding.
let values = cc.value_bindings; let values = cc.value_bindings;
assert_eq!( assert_eq!(values.get(&Variable::from_valid_name("?score")).expect("column binding for ?score").clone(),
values TypedValue::Double(0.0.into()));
.get(&Variable::from_valid_name("?score"))
.expect("column binding for ?score")
.clone(),
TypedValue::Double(0.0.into())
);
let known_types = cc.known_types; let known_types = cc.known_types;
assert_eq!(known_types.len(), 4); assert_eq!(known_types.len(), 4);
assert_eq!( assert_eq!(known_types.get(&Variable::from_valid_name("?entity")).expect("known types for ?entity").clone(),
known_types vec![ValueType::Ref].into_iter().collect());
.get(&Variable::from_valid_name("?entity")) assert_eq!(known_types.get(&Variable::from_valid_name("?value")).expect("known types for ?value").clone(),
.expect("known types for ?entity") vec![ValueType::String].into_iter().collect());
.clone(), assert_eq!(known_types.get(&Variable::from_valid_name("?tx")).expect("known types for ?tx").clone(),
vec![ValueType::Ref].into_iter().collect() vec![ValueType::Ref].into_iter().collect());
); assert_eq!(known_types.get(&Variable::from_valid_name("?score")).expect("known types for ?score").clone(),
assert_eq!( vec![ValueType::Double].into_iter().collect());
known_types
.get(&Variable::from_valid_name("?value"))
.expect("known types for ?value")
.clone(),
vec![ValueType::String].into_iter().collect()
);
assert_eq!(
known_types
.get(&Variable::from_valid_name("?tx"))
.expect("known types for ?tx")
.clone(),
vec![ValueType::Ref].into_iter().collect()
);
assert_eq!(
known_types
.get(&Variable::from_valid_name("?score"))
.expect("known types for ?score")
.clone(),
vec![ValueType::Double].into_iter().collect()
);
let mut cc = ConjoiningClauses::default(); let mut cc = ConjoiningClauses::default();
let op = PlainSymbol::plain("fulltext"); let op = PlainSymbol::plain("fulltext");
cc.apply_fulltext( cc.apply_fulltext(known, WhereFn {
known, operator: op,
WhereFn { args: vec![
operator: op, FnArg::SrcVar(SrcVar::DefaultSrc),
args: vec![ FnArg::IdentOrKeyword(Keyword::namespaced("foo", "bar")),
FnArg::SrcVar(SrcVar::DefaultSrc), FnArg::Constant("needle".into()),
FnArg::IdentOrKeyword(Keyword::namespaced("foo", "bar")), ],
FnArg::Constant("needle".into()), binding: Binding::BindRel(vec![VariableOrPlaceholder::Variable(Variable::from_valid_name("?entity")),
], VariableOrPlaceholder::Variable(Variable::from_valid_name("?value")),
binding: Binding::BindRel(vec![ VariableOrPlaceholder::Variable(Variable::from_valid_name("?tx")),
VariableOrPlaceholder::Variable(Variable::from_valid_name("?entity")), VariableOrPlaceholder::Variable(Variable::from_valid_name("?score"))]),
VariableOrPlaceholder::Variable(Variable::from_valid_name("?value")), }).expect("to be able to apply_fulltext");
VariableOrPlaceholder::Variable(Variable::from_valid_name("?tx")),
VariableOrPlaceholder::Variable(Variable::from_valid_name("?score")),
]),
},
)
.expect("to be able to apply_fulltext");
// It's not a fulltext attribute, so the CC cannot yield results. // It's not a fulltext attribute, so the CC cannot yield results.
assert!(cc.is_known_empty()); assert!(cc.is_known_empty());

View file

@ -8,34 +8,49 @@
// CONDITIONS OF ANY KIND, either express or implied. See the License for the // CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License. // specific language governing permissions and limitations under the License.
use core_traits::{TypedValue, ValueType, ValueTypeSet}; use mentat_core::{
Schema,
TypedValue,
ValueType,
ValueTypeSet,
};
use mentat_core::Schema; use mentat_query::{
Binding,
FnArg,
Variable,
VariableOrPlaceholder,
WhereFn,
};
use edn::query::{Binding, FnArg, Variable, VariableOrPlaceholder, WhereFn}; use clauses::{
ConjoiningClauses,
PushComputed,
};
use crate::clauses::{ConjoiningClauses, PushComputed}; use clauses::convert::ValueConversion;
use crate::clauses::convert::ValueConversion; use errors::{
AlgebrizerError,
BindingError,
Result,
};
use query_algebrizer_traits::errors::{AlgebrizerError, BindingError, Result}; use types::{
ComputedTable,
EmptyBecause,
SourceAlias,
VariableColumn,
};
use crate::types::{ComputedTable, EmptyBecause, SourceAlias, VariableColumn}; use Known;
use crate::Known;
impl ConjoiningClauses { impl ConjoiningClauses {
/// Take a relation: a matrix of values which will successively bind to named variables of /// Take a relation: a matrix of values which will successively bind to named variables of
/// the provided types. /// the provided types.
/// Construct a computed table to yield this relation. /// Construct a computed table to yield this relation.
/// This function will panic if some invariants are not met. /// This function will panic if some invariants are not met.
fn collect_named_bindings<'s>( fn collect_named_bindings<'s>(&mut self, schema: &'s Schema, names: Vec<Variable>, types: Vec<ValueType>, values: Vec<TypedValue>) {
&mut self,
schema: &'s Schema,
names: Vec<Variable>,
types: Vec<ValueType>,
values: Vec<TypedValue>,
) {
if values.is_empty() { if values.is_empty() {
return; return;
} }
@ -43,11 +58,11 @@ impl ConjoiningClauses {
assert!(!names.is_empty()); assert!(!names.is_empty());
assert_eq!(names.len(), types.len()); assert_eq!(names.len(), types.len());
assert!(values.len() >= names.len()); assert!(values.len() >= names.len());
assert_eq!(values.len() % names.len(), 0); // It's an exact multiple. assert_eq!(values.len() % names.len(), 0); // It's an exact multiple.
let named_values = ComputedTable::NamedValues { let named_values = ComputedTable::NamedValues {
names: names.clone(), names: names.clone(),
values, values: values,
}; };
let table = self.computed_tables.push_computed(named_values); let table = self.computed_tables.push_computed(named_values);
@ -56,23 +71,13 @@ impl ConjoiningClauses {
// Stitch the computed table into column_bindings, so we get cross-linking. // Stitch the computed table into column_bindings, so we get cross-linking.
for (name, ty) in names.iter().zip(types.into_iter()) { for (name, ty) in names.iter().zip(types.into_iter()) {
self.constrain_var_to_type(name.clone(), ty); self.constrain_var_to_type(name.clone(), ty);
self.bind_column_to_var( self.bind_column_to_var(schema, alias.clone(), VariableColumn::Variable(name.clone()), name.clone());
schema,
alias.clone(),
VariableColumn::Variable(name.clone()),
name.clone(),
);
} }
self.from.push(SourceAlias(table, alias)); self.from.push(SourceAlias(table, alias));
} }
fn apply_ground_place<'s>( fn apply_ground_place<'s>(&mut self, schema: &'s Schema, var: VariableOrPlaceholder, arg: FnArg) -> Result<()> {
&mut self,
schema: &'s Schema,
var: VariableOrPlaceholder,
arg: FnArg,
) -> Result<()> {
match var { match var {
VariableOrPlaceholder::Placeholder => Ok(()), VariableOrPlaceholder::Placeholder => Ok(()),
VariableOrPlaceholder::Variable(var) => self.apply_ground_var(schema, var, arg), VariableOrPlaceholder::Variable(var) => self.apply_ground_var(schema, var, arg),
@ -81,19 +86,14 @@ impl ConjoiningClauses {
/// Constrain the CC to associate the given var with the given ground argument. /// Constrain the CC to associate the given var with the given ground argument.
/// Marks known-empty on failure. /// Marks known-empty on failure.
fn apply_ground_var<'s>( fn apply_ground_var<'s>(&mut self, schema: &'s Schema, var: Variable, arg: FnArg) -> Result<()> {
&mut self,
schema: &'s Schema,
var: Variable,
arg: FnArg,
) -> Result<()> {
let known_types = self.known_type_set(&var); let known_types = self.known_type_set(&var);
match self.typed_value_from_arg(schema, &var, arg, known_types)? { match self.typed_value_from_arg(schema, &var, arg, known_types)? {
ValueConversion::Val(value) => self.apply_ground_value(var, value), ValueConversion::Val(value) => self.apply_ground_value(var, value),
ValueConversion::Impossible(because) => { ValueConversion::Impossible(because) => {
self.mark_known_empty(because); self.mark_known_empty(because);
Ok(()) Ok(())
} },
} }
} }
@ -103,13 +103,13 @@ impl ConjoiningClauses {
if existing != value { if existing != value {
self.mark_known_empty(EmptyBecause::ConflictingBindings { self.mark_known_empty(EmptyBecause::ConflictingBindings {
var: var.clone(), var: var.clone(),
existing, existing: existing.clone(),
desired: value, desired: value,
}); });
return Ok(()); return Ok(())
} }
} else { } else {
self.bind_value(&var, value); self.bind_value(&var, value.clone());
} }
Ok(()) Ok(())
@ -117,29 +117,19 @@ impl ConjoiningClauses {
pub(crate) fn apply_ground(&mut self, known: Known, where_fn: WhereFn) -> Result<()> { pub(crate) fn apply_ground(&mut self, known: Known, where_fn: WhereFn) -> Result<()> {
if where_fn.args.len() != 1 { if where_fn.args.len() != 1 {
bail!(AlgebrizerError::InvalidNumberOfArguments( bail!(AlgebrizerError::InvalidNumberOfArguments(where_fn.operator.clone(), where_fn.args.len(), 1));
where_fn.operator.clone(),
where_fn.args.len(),
1
));
} }
let mut args = where_fn.args.into_iter(); let mut args = where_fn.args.into_iter();
if where_fn.binding.is_empty() { if where_fn.binding.is_empty() {
// The binding must introduce at least one bound variable. // The binding must introduce at least one bound variable.
bail!(AlgebrizerError::InvalidBinding( bail!(AlgebrizerError::InvalidBinding(where_fn.operator.clone(), BindingError::NoBoundVariable));
where_fn.operator,
BindingError::NoBoundVariable
));
} }
if !where_fn.binding.is_valid() { if !where_fn.binding.is_valid() {
// The binding must not duplicate bound variables. // The binding must not duplicate bound variables.
bail!(AlgebrizerError::InvalidBinding( bail!(AlgebrizerError::InvalidBinding(where_fn.operator.clone(), BindingError::RepeatedBoundVariable));
where_fn.operator,
BindingError::RepeatedBoundVariable
));
} }
let schema = known.schema; let schema = known.schema;
@ -148,7 +138,8 @@ impl ConjoiningClauses {
// we can immediately substitute the value as a known value in the CC, additionally // we can immediately substitute the value as a known value in the CC, additionally
// generating a WHERE clause if columns have already been bound. // generating a WHERE clause if columns have already been bound.
match (where_fn.binding, args.next().unwrap()) { match (where_fn.binding, args.next().unwrap()) {
(Binding::BindScalar(var), constant) => self.apply_ground_var(schema, var, constant), (Binding::BindScalar(var), constant) =>
self.apply_ground_var(schema, var, constant),
(Binding::BindTuple(places), FnArg::Vector(children)) => { (Binding::BindTuple(places), FnArg::Vector(children)) => {
// Just the same, but we bind more than one column at a time. // Just the same, but we bind more than one column at a time.
@ -157,10 +148,10 @@ impl ConjoiningClauses {
bail!(AlgebrizerError::GroundBindingsMismatch) bail!(AlgebrizerError::GroundBindingsMismatch)
} }
for (place, arg) in places.into_iter().zip(children.into_iter()) { for (place, arg) in places.into_iter().zip(children.into_iter()) {
self.apply_ground_place(schema, place, arg)? // TODO: short-circuit on impossible. self.apply_ground_place(schema, place, arg)? // TODO: short-circuit on impossible.
} }
Ok(()) Ok(())
} },
// Collection bindings and rel bindings are similar in that they are both // Collection bindings and rel bindings are similar in that they are both
// implemented as a subquery with a projection list and a set of values. // implemented as a subquery with a projection list and a set of values.
@ -176,32 +167,30 @@ impl ConjoiningClauses {
// Check that every value has the same type. // Check that every value has the same type.
let mut accumulated_types = ValueTypeSet::none(); let mut accumulated_types = ValueTypeSet::none();
let mut skip: Option<EmptyBecause> = None; let mut skip: Option<EmptyBecause> = None;
let values = children let values = children.into_iter()
.into_iter() .filter_map(|arg| -> Option<Result<TypedValue>> {
.filter_map(|arg| -> Option<Result<TypedValue>> { // We need to get conversion errors out.
// We need to get conversion errors out. // We also want to mark known-empty on impossibilty, but
// We also want to mark known-empty on impossibility, but // still detect serious errors.
// still detect serious errors. match self.typed_value_from_arg(schema, &var, arg, known_types) {
match self.typed_value_from_arg(schema, &var, arg, known_types) { Ok(ValueConversion::Val(tv)) => {
Ok(ValueConversion::Val(tv)) => { if accumulated_types.insert(tv.value_type()) &&
if accumulated_types.insert(tv.value_type()) !accumulated_types.is_unit() {
&& !accumulated_types.is_unit() // Values not all of the same type.
{ Some(Err(AlgebrizerError::InvalidGroundConstant.into()))
// Values not all of the same type. } else {
Some(Err(AlgebrizerError::InvalidGroundConstant)) Some(Ok(tv))
} else { }
Some(Ok(tv)) },
} Ok(ValueConversion::Impossible(because)) => {
} // Skip this value.
Ok(ValueConversion::Impossible(because)) => { skip = Some(because);
// Skip this value. None
skip = Some(because); },
None Err(e) => Some(Err(e.into())),
} }
Err(e) => Some(Err(e)), })
} .collect::<Result<Vec<TypedValue>>>()?;
})
.collect::<Result<Vec<TypedValue>>>()?;
if values.is_empty() { if values.is_empty() {
let because = skip.expect("we skipped all rows for a reason"); let because = skip.expect("we skipped all rows for a reason");
@ -211,11 +200,11 @@ impl ConjoiningClauses {
// Otherwise, we now have the values and the type. // Otherwise, we now have the values and the type.
let types = vec![accumulated_types.exemplar().unwrap()]; let types = vec![accumulated_types.exemplar().unwrap()];
let names = vec![var]; let names = vec![var.clone()];
self.collect_named_bindings(schema, names, types, values); self.collect_named_bindings(schema, names, types, values);
Ok(()) Ok(())
} },
(Binding::BindRel(places), FnArg::Vector(rows)) => { (Binding::BindRel(places), FnArg::Vector(rows)) => {
if rows.is_empty() { if rows.is_empty() {
@ -224,20 +213,17 @@ impl ConjoiningClauses {
// Grab the known types to which these args must conform, and track // Grab the known types to which these args must conform, and track
// the places that won't be bound in the output. // the places that won't be bound in the output.
let template: Vec<Option<(Variable, ValueTypeSet)>> = places let template: Vec<Option<(Variable, ValueTypeSet)>> =
.iter() places.iter()
.map(|x| match x { .map(|x| match x {
VariableOrPlaceholder::Placeholder => None, &VariableOrPlaceholder::Placeholder => None,
VariableOrPlaceholder::Variable(ref v) => { &VariableOrPlaceholder::Variable(ref v) => Some((v.clone(), self.known_type_set(v))),
Some((v.clone(), self.known_type_set(v))) })
} .collect();
})
.collect();
// The expected 'width' of the matrix is the number of named variables. // The expected 'width' of the matrix is the number of named variables.
let full_width = places.len(); let full_width = places.len();
let names: Vec<Variable> = let names: Vec<Variable> = places.into_iter().filter_map(|x| x.into_var()).collect();
places.into_iter().filter_map(|x| x.into_var()).collect();
let expected_width = names.len(); let expected_width = names.len();
let expected_rows = rows.len(); let expected_rows = rows.len();
@ -271,14 +257,14 @@ impl ConjoiningClauses {
// Convert each item in the row. // Convert each item in the row.
// If any value in the row is impossible, then skip the row. // If any value in the row is impossible, then skip the row.
// If all rows are impossible, fail the entire CC. // If all rows are impossible, fail the entire CC.
if let Some(ref pair) = pair { if let &Some(ref pair) = pair {
match self.typed_value_from_arg(schema, &pair.0, col, pair.1)? { match self.typed_value_from_arg(schema, &pair.0, col, pair.1)? {
ValueConversion::Val(tv) => vals.push(tv), ValueConversion::Val(tv) => vals.push(tv),
ValueConversion::Impossible(because) => { ValueConversion::Impossible(because) => {
// Skip this row. It cannot produce bindings. // Skip this row. It cannot produce bindings.
skip = Some(because); skip = Some(because);
break; break;
} },
} }
} }
} }
@ -290,10 +276,7 @@ impl ConjoiningClauses {
} }
// Accumulate the values into the matrix and the types into the type set. // Accumulate the values into the matrix and the types into the type set.
for (val, acc) in vals for (val, acc) in vals.into_iter().zip(accumulated_types_for_columns.iter_mut()) {
.into_iter()
.zip(accumulated_types_for_columns.iter_mut())
{
let inserted = acc.insert(val.value_type()); let inserted = acc.insert(val.value_type());
if inserted && !acc.is_unit() { if inserted && !acc.is_unit() {
// Heterogeneous types. // Heterogeneous types.
@ -301,7 +284,8 @@ impl ConjoiningClauses {
} }
matrix.push(val); matrix.push(val);
} }
}
},
_ => bail!(AlgebrizerError::InvalidGroundConstant), _ => bail!(AlgebrizerError::InvalidGroundConstant),
} }
} }
@ -322,13 +306,12 @@ impl ConjoiningClauses {
// type tags. If and when we want to algebrize in two phases and allow for // type tags. If and when we want to algebrize in two phases and allow for
// late-binding input variables, we'll probably be able to loosen this restriction // late-binding input variables, we'll probably be able to loosen this restriction
// with little penalty. // with little penalty.
let types = accumulated_types_for_columns let types = accumulated_types_for_columns.into_iter()
.into_iter() .map(|x| x.exemplar().unwrap())
.map(|x| x.exemplar().unwrap()) .collect();
.collect();
self.collect_named_bindings(schema, names, types, matrix); self.collect_named_bindings(schema, names, types, matrix);
Ok(()) Ok(())
} },
(_, _) => bail!(AlgebrizerError::InvalidGroundConstant), (_, _) => bail!(AlgebrizerError::InvalidGroundConstant),
} }
} }
@ -338,11 +321,23 @@ impl ConjoiningClauses {
mod testing { mod testing {
use super::*; use super::*;
use core_traits::{Attribute, ValueType}; use mentat_core::{
Attribute,
ValueType,
};
use edn::query::{Binding, FnArg, Keyword, PlainSymbol, Variable}; use mentat_query::{
Binding,
FnArg,
Keyword,
PlainSymbol,
Variable,
};
use crate::clauses::{add_attribute, associate_ident}; use clauses::{
add_attribute,
associate_ident,
};
#[test] #[test]
fn test_apply_ground() { fn test_apply_ground() {
@ -352,31 +347,25 @@ mod testing {
let mut schema = Schema::default(); let mut schema = Schema::default();
associate_ident(&mut schema, Keyword::namespaced("foo", "fts"), 100); associate_ident(&mut schema, Keyword::namespaced("foo", "fts"), 100);
add_attribute( add_attribute(&mut schema, 100, Attribute {
&mut schema, value_type: ValueType::String,
100, index: true,
Attribute { fulltext: true,
value_type: ValueType::String, ..Default::default()
index: true, });
fulltext: true,
..Default::default()
},
);
let known = Known::for_schema(&schema); let known = Known::for_schema(&schema);
// It's awkward enough to write these expansions that we give the details for the simplest // It's awkward enough to write these expansions that we give the details for the simplest
// case only. See the tests of the translator for more extensive (albeit looser) coverage. // case only. See the tests of the translator for more extensive (albeit looser) coverage.
let op = PlainSymbol::plain("ground"); let op = PlainSymbol::plain("ground");
cc.apply_ground( cc.apply_ground(known, WhereFn {
known, operator: op,
WhereFn { args: vec![
operator: op, FnArg::EntidOrInteger(10),
args: vec![FnArg::EntidOrInteger(10)], ],
binding: Binding::BindScalar(vz.clone()), binding: Binding::BindScalar(vz.clone()),
}, }).expect("to be able to apply_ground");
)
.expect("to be able to apply_ground");
assert!(!cc.is_known_empty()); assert!(!cc.is_known_empty());
@ -388,20 +377,16 @@ mod testing {
assert_eq!(clauses.len(), 0); assert_eq!(clauses.len(), 0);
let column_bindings = cc.column_bindings; let column_bindings = cc.column_bindings;
assert_eq!(column_bindings.len(), 0); // Scalar doesn't need this. assert_eq!(column_bindings.len(), 0); // Scalar doesn't need this.
let known_types = cc.known_types; let known_types = cc.known_types;
assert_eq!(known_types.len(), 1); assert_eq!(known_types.len(), 1);
assert_eq!( assert_eq!(known_types.get(&vz).expect("to know the type of ?z"),
known_types.get(&vz).expect("to know the type of ?z"), &ValueTypeSet::of_one(ValueType::Long));
&ValueTypeSet::of_one(ValueType::Long)
);
let value_bindings = cc.value_bindings; let value_bindings = cc.value_bindings;
assert_eq!(value_bindings.len(), 1); assert_eq!(value_bindings.len(), 1);
assert_eq!( assert_eq!(value_bindings.get(&vz).expect("to have a value for ?z"),
value_bindings.get(&vz).expect("to have a value for ?z"), &TypedValue::Long(10)); // We default to Long instead of entid.
&TypedValue::Long(10)
); // We default to Long instead of entid.
} }
} }

View file

@ -10,11 +10,19 @@
use std::collections::BTreeMap; use std::collections::BTreeMap;
use core_traits::{TypedValue, ValueType}; use mentat_core::{
TypedValue,
ValueType,
};
use edn::query::Variable; use mentat_query::{
Variable,
};
use query_algebrizer_traits::errors::{AlgebrizerError, Result}; use errors::{
AlgebrizerError,
Result,
};
/// Define the inputs to a query. This is in two parts: a set of values known now, and a set of /// Define the inputs to a query. This is in two parts: a set of values known now, and a set of
/// types known now. /// types known now.
@ -51,18 +59,13 @@ impl QueryInputs {
pub fn with_values(values: BTreeMap<Variable, TypedValue>) -> QueryInputs { pub fn with_values(values: BTreeMap<Variable, TypedValue>) -> QueryInputs {
QueryInputs { QueryInputs {
types: values types: values.iter().map(|(var, val)| (var.clone(), val.value_type())).collect(),
.iter() values: values,
.map(|(var, val)| (var.clone(), val.value_type()))
.collect(),
values,
} }
} }
pub fn new( pub fn new(mut types: BTreeMap<Variable, ValueType>,
mut types: BTreeMap<Variable, ValueType>, values: BTreeMap<Variable, TypedValue>) -> Result<QueryInputs> {
values: BTreeMap<Variable, TypedValue>,
) -> Result<QueryInputs> {
// Make sure that the types of the values agree with those in types, and collect. // Make sure that the types of the values agree with those in types, and collect.
for (var, v) in values.iter() { for (var, v) in values.iter() {
let t = v.value_type(); let t = v.value_type();
@ -73,6 +76,6 @@ impl QueryInputs {
} }
} }
} }
Ok(QueryInputs { types, values }) Ok(QueryInputs { types: types, values: values })
} }
} }

View file

@ -10,46 +10,93 @@
use std::cmp; use std::cmp;
use std::collections::{BTreeMap, BTreeSet, VecDeque}; use std::collections::{
BTreeMap,
BTreeSet,
VecDeque,
};
use std::collections::btree_map::Entry; use std::collections::btree_map::{
Entry,
};
use std::fmt::{Debug, Formatter}; use std::fmt::{
Debug,
Formatter,
};
use core_traits::{Attribute, Entid, KnownEntid, TypedValue, ValueType, ValueTypeSet}; use mentat_core::{
Attribute,
use mentat_core::{Cloned, HasSchema, Schema}; Cloned,
Entid,
HasSchema,
KnownEntid,
Schema,
TypedValue,
ValueType,
ValueTypeSet,
};
use mentat_core::counter::RcCounter; use mentat_core::counter::RcCounter;
use edn::query::{Element, FindSpec, Keyword, PatternNonValuePlace, Pull, Variable, WhereClause}; use mentat_query::{
Element,
use query_algebrizer_traits::errors::{AlgebrizerError, Result}; FindSpec,
Keyword,
use crate::types::{ Pull,
Column, ColumnConstraint, ColumnIntersection, ComputedTable, DatomsColumn, DatomsTable, Variable,
EmptyBecause, EvolvedNonValuePlace, EvolvedPattern, EvolvedValuePlace, FulltextColumn, WhereClause,
PlaceOrEmpty, QualifiedAlias, QueryValue, SourceAlias, TableAlias,
}; };
mod convert; // Converting args to values. use mentat_query::{
PatternNonValuePlace,
};
use errors::{
AlgebrizerError,
Result,
};
use types::{
ColumnConstraint,
ColumnIntersection,
ComputedTable,
Column,
DatomsColumn,
DatomsTable,
EmptyBecause,
EvolvedNonValuePlace,
EvolvedPattern,
EvolvedValuePlace,
FulltextColumn,
PlaceOrEmpty,
QualifiedAlias,
QueryValue,
SourceAlias,
TableAlias,
};
mod convert; // Converting args to values.
mod inputs; mod inputs;
mod not;
mod or; mod or;
mod not;
mod pattern; mod pattern;
mod predicate; mod predicate;
mod resolve; mod resolve;
mod fulltext;
mod ground; mod ground;
mod fulltext;
mod tx_log_api; mod tx_log_api;
mod where_fn; mod where_fn;
use crate::validate::{validate_not_join, validate_or_join}; use validate::{
validate_not_join,
validate_or_join,
};
pub use self::inputs::QueryInputs; pub use self::inputs::QueryInputs;
use crate::Known; use Known;
trait Contains<K, T> { trait Contains<K, T> {
fn when_contains<F: FnOnce() -> T>(&self, k: &K, f: F) -> Option<T>; fn when_contains<F: FnOnce() -> T>(&self, k: &K, f: F) -> Option<T>;
@ -147,8 +194,8 @@ pub struct ConjoiningClauses {
/// A map from var to qualified columns. Used to project. /// A map from var to qualified columns. Used to project.
pub column_bindings: BTreeMap<Variable, Vec<QualifiedAlias>>, pub column_bindings: BTreeMap<Variable, Vec<QualifiedAlias>>,
/// A list of variables mentioned in the enclosing query's `:in` clause all of which must be /// A list of variables mentioned in the enclosing query's :in clause. These must all be bound
/// bound before the query can be executed. TODO: clarify what this means for nested CCs. /// before the query can be executed. TODO: clarify what this means for nested CCs.
pub input_variables: BTreeSet<Variable>, pub input_variables: BTreeSet<Variable>,
/// In some situations -- e.g., when a query is being run only once -- we know in advance the /// In some situations -- e.g., when a query is being run only once -- we know in advance the
@ -180,16 +227,16 @@ pub struct ConjoiningClauses {
impl PartialEq for ConjoiningClauses { impl PartialEq for ConjoiningClauses {
fn eq(&self, other: &ConjoiningClauses) -> bool { fn eq(&self, other: &ConjoiningClauses) -> bool {
self.empty_because.eq(&other.empty_because) self.empty_because.eq(&other.empty_because) &&
&& self.from.eq(&other.from) self.from.eq(&other.from) &&
&& self.computed_tables.eq(&other.computed_tables) self.computed_tables.eq(&other.computed_tables) &&
&& self.wheres.eq(&other.wheres) self.wheres.eq(&other.wheres) &&
&& self.column_bindings.eq(&other.column_bindings) self.column_bindings.eq(&other.column_bindings) &&
&& self.input_variables.eq(&other.input_variables) self.input_variables.eq(&other.input_variables) &&
&& self.value_bindings.eq(&other.value_bindings) self.value_bindings.eq(&other.value_bindings) &&
&& self.known_types.eq(&other.known_types) self.known_types.eq(&other.known_types) &&
&& self.extracted_types.eq(&other.extracted_types) self.extracted_types.eq(&other.extracted_types) &&
&& self.required_types.eq(&other.required_types) self.required_types.eq(&other.required_types)
} }
} }
@ -231,7 +278,9 @@ impl Default for ConjoiningClauses {
} }
} }
pub struct VariableIterator<'a>(::std::collections::btree_map::Keys<'a, Variable, TypedValue>); pub struct VariableIterator<'a>(
::std::collections::btree_map::Keys<'a, Variable, TypedValue>,
);
impl<'a> Iterator for VariableIterator<'a> { impl<'a> Iterator for VariableIterator<'a> {
type Item = &'a Variable; type Item = &'a Variable;
@ -254,45 +303,34 @@ impl ConjoiningClauses {
#[cfg(test)] #[cfg(test)]
pub fn with_inputs<T>(in_variables: BTreeSet<Variable>, inputs: T) -> ConjoiningClauses pub fn with_inputs<T>(in_variables: BTreeSet<Variable>, inputs: T) -> ConjoiningClauses
where where T: Into<Option<QueryInputs>> {
T: Into<Option<QueryInputs>>,
{
ConjoiningClauses::with_inputs_and_alias_counter(in_variables, inputs, RcCounter::new()) ConjoiningClauses::with_inputs_and_alias_counter(in_variables, inputs, RcCounter::new())
} }
pub(crate) fn with_inputs_and_alias_counter<T>( pub(crate) fn with_inputs_and_alias_counter<T>(in_variables: BTreeSet<Variable>,
in_variables: BTreeSet<Variable>, inputs: T,
inputs: T, alias_counter: RcCounter) -> ConjoiningClauses
alias_counter: RcCounter, where T: Into<Option<QueryInputs>> {
) -> ConjoiningClauses
where
T: Into<Option<QueryInputs>>,
{
match inputs.into() { match inputs.into() {
None => ConjoiningClauses::with_alias_counter(alias_counter), None => ConjoiningClauses::with_alias_counter(alias_counter),
Some(QueryInputs { Some(QueryInputs { mut types, mut values }) => {
mut types,
mut values,
}) => {
// Discard any bindings not mentioned in our :in clause. // Discard any bindings not mentioned in our :in clause.
types.keep_intersected_keys(&in_variables); types.keep_intersected_keys(&in_variables);
values.keep_intersected_keys(&in_variables); values.keep_intersected_keys(&in_variables);
let mut cc = ConjoiningClauses { let mut cc = ConjoiningClauses {
alias_counter, alias_counter: alias_counter,
input_variables: in_variables, input_variables: in_variables,
value_bindings: values, value_bindings: values,
..Default::default() ..Default::default()
}; };
// Pre-fill our type mappings with the types of the input bindings. // Pre-fill our type mappings with the types of the input bindings.
cc.known_types.extend( cc.known_types
types .extend(types.iter()
.iter() .map(|(k, v)| (k.clone(), ValueTypeSet::of_one(*v))));
.map(|(k, v)| (k.clone(), ValueTypeSet::of_one(*v))),
);
cc cc
} },
} }
} }
} }
@ -301,8 +339,12 @@ impl ConjoiningClauses {
impl ConjoiningClauses { impl ConjoiningClauses {
pub(crate) fn derive_types_from_find_spec(&mut self, find_spec: &FindSpec) { pub(crate) fn derive_types_from_find_spec(&mut self, find_spec: &FindSpec) {
for spec in find_spec.columns() { for spec in find_spec.columns() {
if let Element::Pull(Pull { ref var, .. }) = spec { match spec {
self.constrain_var_to_type(var.clone(), ValueType::Ref); &Element::Pull(Pull { ref var, patterns: _ }) => {
self.constrain_var_to_type(var.clone(), ValueType::Ref);
},
_ => {
},
} }
} }
} }
@ -349,18 +391,14 @@ impl ConjoiningClauses {
// If so, generate a constraint against the primary column. // If so, generate a constraint against the primary column.
if let Some(vec) = self.column_bindings.get(var) { if let Some(vec) = self.column_bindings.get(var) {
if let Some(col) = vec.first() { if let Some(col) = vec.first() {
self.wheres.add_intersection(ColumnConstraint::Equals( self.wheres.add_intersection(ColumnConstraint::Equals(col.clone(), QueryValue::TypedValue(value.clone())));
col.clone(),
QueryValue::TypedValue(value.clone()),
));
} }
} }
// Are we also trying to figure out the type of the value when the query runs? // Are we also trying to figure out the type of the value when the query runs?
// If so, constrain that! // If so, constrain that!
if let Some(qa) = self.extracted_types.get(&var) { if let Some(qa) = self.extracted_types.get(&var) {
self.wheres self.wheres.add_intersection(ColumnConstraint::has_unit_type(qa.0.clone(), vt));
.add_intersection(ColumnConstraint::has_unit_type(qa.0.clone(), vt));
} }
// Finally, store the binding for future use. // Finally, store the binding for future use.
@ -401,19 +439,10 @@ impl ConjoiningClauses {
} }
pub fn known_type_set(&self, var: &Variable) -> ValueTypeSet { pub fn known_type_set(&self, var: &Variable) -> ValueTypeSet {
self.known_types self.known_types.get(var).cloned().unwrap_or(ValueTypeSet::any())
.get(var)
.cloned()
.unwrap_or_else(ValueTypeSet::any)
} }
pub(crate) fn bind_column_to_var<C: Into<Column>>( pub(crate) fn bind_column_to_var<C: Into<Column>>(&mut self, schema: &Schema, table: TableAlias, column: C, var: Variable) {
&mut self,
schema: &Schema,
table: TableAlias,
column: C,
var: Variable,
) {
let column = column.into(); let column = column.into();
// Do we have an external binding for this? // Do we have an external binding for this?
if let Some(bound_val) = self.bound_value(&var) { if let Some(bound_val) = self.bound_value(&var) {
@ -425,18 +454,18 @@ impl ConjoiningClauses {
// We don't need to handle expansion of attributes here. The subquery that // We don't need to handle expansion of attributes here. The subquery that
// produces the variable projection will do so. // produces the variable projection will do so.
self.constrain_column_to_constant(table, column, bound_val); self.constrain_column_to_constant(table, column, bound_val);
} },
Column::Transactions(_) => { Column::Transactions(_) => {
self.constrain_column_to_constant(table, column, bound_val); self.constrain_column_to_constant(table, column, bound_val);
} },
Column::Fulltext(FulltextColumn::Rowid) Column::Fulltext(FulltextColumn::Rowid) |
| Column::Fulltext(FulltextColumn::Text) => { Column::Fulltext(FulltextColumn::Text) => {
// We never expose `rowid` via queries. We do expose `text`, but only // We never expose `rowid` via queries. We do expose `text`, but only
// indirectly, by joining against `datoms`. Therefore, these are meaningless. // indirectly, by joining against `datoms`. Therefore, these are meaningless.
unimplemented!() unimplemented!()
} },
Column::Fixed(DatomsColumn::ValueTypeTag) => { Column::Fixed(DatomsColumn::ValueTypeTag) => {
// I'm pretty sure this is meaningless right now, because we will never bind // I'm pretty sure this is meaningless right now, because we will never bind
@ -448,18 +477,18 @@ impl ConjoiningClauses {
// [(= (typeof ?y) :db.valueType/double)]] // [(= (typeof ?y) :db.valueType/double)]]
// ``` // ```
unimplemented!(); unimplemented!();
} },
// TODO: recognize when the valueType might be a ref and also translate entids there. // TODO: recognize when the valueType might be a ref and also translate entids there.
Column::Fixed(DatomsColumn::Value) => { Column::Fixed(DatomsColumn::Value) => {
self.constrain_column_to_constant(table, column, bound_val); self.constrain_column_to_constant(table, column, bound_val);
} },
// These columns can only be entities, so attempt to translate keywords. If we can't // These columns can only be entities, so attempt to translate keywords. If we can't
// get an entity out of the bound value, the pattern cannot produce results. // get an entity out of the bound value, the pattern cannot produce results.
Column::Fixed(DatomsColumn::Attribute) Column::Fixed(DatomsColumn::Attribute) |
| Column::Fixed(DatomsColumn::Entity) Column::Fixed(DatomsColumn::Entity) |
| Column::Fixed(DatomsColumn::Tx) => { Column::Fixed(DatomsColumn::Tx) => {
match bound_val { match bound_val {
TypedValue::Keyword(ref kw) => { TypedValue::Keyword(ref kw) => {
if let Some(entid) = self.entid_for_ident(schema, kw) { if let Some(entid) = self.entid_for_ident(schema, kw) {
@ -471,14 +500,14 @@ impl ConjoiningClauses {
// attribute then we should have already marked the pattern as empty. // attribute then we should have already marked the pattern as empty.
self.mark_known_empty(EmptyBecause::UnresolvedIdent(kw.cloned())); self.mark_known_empty(EmptyBecause::UnresolvedIdent(kw.cloned()));
} }
} },
TypedValue::Ref(entid) => { TypedValue::Ref(entid) => {
self.constrain_column_to_entity(table, column, entid); self.constrain_column_to_entity(table, column, entid);
} },
_ => { _ => {
// One can't bind an e, a, or tx to something other than an entity. // One can't bind an e, a, or tx to something other than an entity.
self.mark_known_empty(EmptyBecause::InvalidBinding(column, bound_val)); self.mark_known_empty(EmptyBecause::InvalidBinding(column, bound_val));
} },
} }
} }
} }
@ -492,9 +521,10 @@ impl ConjoiningClauses {
// If this is a value, and we don't already know its type or where // If this is a value, and we don't already know its type or where
// to get its type, record that we can get it from this table. // to get its type, record that we can get it from this table.
let needs_type_extraction = !late_binding && // Never need to extract for bound vars. let needs_type_extraction =
!late_binding && // Never need to extract for bound vars.
self.known_type(&var).is_none() && // Don't need to extract if we know a single type. self.known_type(&var).is_none() && // Don't need to extract if we know a single type.
!self.extracted_types.contains_key(&var); // We're already extracting the type. !self.extracted_types.contains_key(&var); // We're already extracting the type.
let alias = QualifiedAlias(table, column); let alias = QualifiedAlias(table, column);
@ -506,42 +536,23 @@ impl ConjoiningClauses {
} }
} }
self.column_bindings self.column_bindings.entry(var).or_insert(vec![]).push(alias);
.entry(var)
.or_insert_with(Vec::new)
.push(alias);
} }
pub(crate) fn constrain_column_to_constant<C: Into<Column>>( pub(crate) fn constrain_column_to_constant<C: Into<Column>>(&mut self, table: TableAlias, column: C, constant: TypedValue) {
&mut self,
table: TableAlias,
column: C,
constant: TypedValue,
) {
match constant { match constant {
// Be a little more explicit. // Be a little more explicit.
TypedValue::Ref(entid) => self.constrain_column_to_entity(table, column, entid), TypedValue::Ref(entid) => self.constrain_column_to_entity(table, column, entid),
_ => { _ => {
let column = column.into(); let column = column.into();
self.wheres.add_intersection(ColumnConstraint::Equals( self.wheres.add_intersection(ColumnConstraint::Equals(QualifiedAlias(table, column), QueryValue::TypedValue(constant)))
QualifiedAlias(table, column), },
QueryValue::TypedValue(constant),
))
}
} }
} }
pub(crate) fn constrain_column_to_entity<C: Into<Column>>( pub(crate) fn constrain_column_to_entity<C: Into<Column>>(&mut self, table: TableAlias, column: C, entity: Entid) {
&mut self,
table: TableAlias,
column: C,
entity: Entid,
) {
let column = column.into(); let column = column.into();
self.wheres.add_intersection(ColumnConstraint::Equals( self.wheres.add_intersection(ColumnConstraint::Equals(QualifiedAlias(table, column), QueryValue::Entid(entity)))
QualifiedAlias(table, column),
QueryValue::Entid(entity),
))
} }
pub(crate) fn constrain_attribute(&mut self, table: TableAlias, attribute: Entid) { pub(crate) fn constrain_attribute(&mut self, table: TableAlias, attribute: Entid) {
@ -551,8 +562,7 @@ impl ConjoiningClauses {
pub(crate) fn constrain_value_to_numeric(&mut self, table: TableAlias, value: i64) { pub(crate) fn constrain_value_to_numeric(&mut self, table: TableAlias, value: i64) {
self.wheres.add_intersection(ColumnConstraint::Equals( self.wheres.add_intersection(ColumnConstraint::Equals(
QualifiedAlias(table, Column::Fixed(DatomsColumn::Value)), QualifiedAlias(table, Column::Fixed(DatomsColumn::Value)),
QueryValue::PrimitiveLong(value), QueryValue::PrimitiveLong(value)))
))
} }
/// Mark the given value as a long. /// Mark the given value as a long.
@ -565,24 +575,16 @@ impl ConjoiningClauses {
self.narrow_types_for_var(variable, ValueTypeSet::of_numeric_types()); self.narrow_types_for_var(variable, ValueTypeSet::of_numeric_types());
} }
pub(crate) fn can_constrain_var_to_type( pub(crate) fn can_constrain_var_to_type(&self, var: &Variable, this_type: ValueType) -> Option<EmptyBecause> {
&self,
var: &Variable,
this_type: ValueType,
) -> Option<EmptyBecause> {
self.can_constrain_var_to_types(var, ValueTypeSet::of_one(this_type)) self.can_constrain_var_to_types(var, ValueTypeSet::of_one(this_type))
} }
fn can_constrain_var_to_types( fn can_constrain_var_to_types(&self, var: &Variable, these_types: ValueTypeSet) -> Option<EmptyBecause> {
&self,
var: &Variable,
these_types: ValueTypeSet,
) -> Option<EmptyBecause> {
if let Some(existing) = self.known_types.get(var) { if let Some(existing) = self.known_types.get(var) {
if existing.intersection(these_types).is_empty() { if existing.intersection(&these_types).is_empty() {
return Some(EmptyBecause::TypeMismatch { return Some(EmptyBecause::TypeMismatch {
var: var.clone(), var: var.clone(),
existing: *existing, existing: existing.clone(),
desired: these_types, desired: these_types,
}); });
} }
@ -601,11 +603,7 @@ impl ConjoiningClauses {
if let Some(existing) = self.known_types.insert(var.clone(), this_type_set) { if let Some(existing) = self.known_types.insert(var.clone(), this_type_set) {
// There was an existing mapping. Does this type match? // There was an existing mapping. Does this type match?
if !existing.contains(this_type) { if !existing.contains(this_type) {
self.mark_known_empty(EmptyBecause::TypeMismatch { self.mark_known_empty(EmptyBecause::TypeMismatch { var, existing, desired: this_type_set });
var,
existing,
desired: this_type_set,
});
} }
} }
} }
@ -629,12 +627,12 @@ impl ConjoiningClauses {
Entry::Vacant(entry) => { Entry::Vacant(entry) => {
entry.insert(types); entry.insert(types);
return; return;
} },
Entry::Occupied(mut entry) => { Entry::Occupied(mut entry) => {
// We have an existing requirement. The new requirement will be // We have an existing requirement. The new requirement will be
// the intersection, but we'll `mark_known_empty` if that's empty. // the intersection, but we'll `mark_known_empty` if that's empty.
let existing = *entry.get(); let existing = *entry.get();
let intersection = types.intersection(existing); let intersection = types.intersection(&existing);
entry.insert(intersection); entry.insert(intersection);
if !intersection.is_empty() { if !intersection.is_empty() {
@ -642,11 +640,11 @@ impl ConjoiningClauses {
} }
EmptyBecause::TypeMismatch { EmptyBecause::TypeMismatch {
var, var: var,
existing, existing: existing,
desired: types, desired: types,
} }
} },
}; };
self.mark_known_empty(empty_because); self.mark_known_empty(empty_because);
} }
@ -666,22 +664,21 @@ impl ConjoiningClauses {
self.extracted_types.remove(e.key()); self.extracted_types.remove(e.key());
} }
e.insert(new_types); e.insert(new_types);
} },
Entry::Occupied(mut e) => { Entry::Occupied(mut e) => {
let new; let new;
// Scoped borrow of `e`. // Scoped borrow of `e`.
{ {
let existing_types = e.get(); let existing_types = e.get();
if existing_types.is_empty() && // The set is empty: no types are possible. if existing_types.is_empty() && // The set is empty: no types are possible.
self.empty_because.is_some() self.empty_because.is_some() {
{
panic!("Uh oh: we failed this pattern, probably because {:?} couldn't match, but now we're broadening its type.", panic!("Uh oh: we failed this pattern, probably because {:?} couldn't match, but now we're broadening its type.",
e.key()); e.key());
} }
new = existing_types.union(new_types); new = existing_types.union(&new_types);
} }
e.insert(new); e.insert(new);
} },
} }
} }
} }
@ -702,20 +699,18 @@ impl ConjoiningClauses {
match self.known_types.entry(var) { match self.known_types.entry(var) {
Entry::Vacant(e) => { Entry::Vacant(e) => {
e.insert(types); e.insert(types);
} },
Entry::Occupied(mut e) => { Entry::Occupied(mut e) => {
let intersected: ValueTypeSet = types.intersection(*e.get()); let intersected: ValueTypeSet = types.intersection(e.get());
if intersected.is_empty() { if intersected.is_empty() {
let reason = EmptyBecause::TypeMismatch { let reason = EmptyBecause::TypeMismatch { var: e.key().clone(),
var: e.key().clone(), existing: e.get().clone(),
existing: *e.get(), desired: types };
desired: types,
};
empty_because = Some(reason); empty_because = Some(reason);
} }
// Always insert, even if it's empty! // Always insert, even if it's empty!
e.insert(intersected); e.insert(intersected);
} },
} }
if let Some(e) = empty_because { if let Some(e) = empty_because {
@ -745,7 +740,7 @@ impl ConjoiningClauses {
// If it's a variable, record that it has the right type. // If it's a variable, record that it has the right type.
// Ident or attribute resolution errors (the only other check we need to do) will be done // Ident or attribute resolution errors (the only other check we need to do) will be done
// by the caller. // by the caller.
if let EvolvedNonValuePlace::Variable(ref v) = value { if let &EvolvedNonValuePlace::Variable(ref v) = value {
self.constrain_var_to_type(v.clone(), ValueType::Ref) self.constrain_var_to_type(v.clone(), ValueType::Ref)
} }
} }
@ -759,47 +754,39 @@ impl ConjoiningClauses {
if self.empty_because.is_some() { if self.empty_because.is_some() {
return; return;
} }
println!("CC known empty: {:?}.", &why); // TODO: proper logging. println!("CC known empty: {:?}.", &why); // TODO: proper logging.
self.empty_because = Some(why); self.empty_because = Some(why);
} }
fn entid_for_ident<'s, 'a>( fn entid_for_ident<'s, 'a>(&self, schema: &'s Schema, ident: &'a Keyword) -> Option<KnownEntid> {
&self,
schema: &'s Schema,
ident: &'a Keyword,
) -> Option<KnownEntid> {
schema.get_entid(&ident) schema.get_entid(&ident)
} }
fn table_for_attribute_and_value<'s, 'a>( fn table_for_attribute_and_value<'s, 'a>(&self, attribute: &'s Attribute, value: &'a EvolvedValuePlace) -> ::std::result::Result<DatomsTable, EmptyBecause> {
&self,
attribute: &'s Attribute,
value: &'a EvolvedValuePlace,
) -> ::std::result::Result<DatomsTable, EmptyBecause> {
if attribute.fulltext { if attribute.fulltext {
match value { match value {
EvolvedValuePlace::Placeholder => Ok(DatomsTable::Datoms), // We don't need the value. &EvolvedValuePlace::Placeholder =>
Ok(DatomsTable::Datoms), // We don't need the value.
// TODO: an existing non-string binding can cause this pattern to fail. // TODO: an existing non-string binding can cause this pattern to fail.
EvolvedValuePlace::Variable(_) => Ok(DatomsTable::FulltextDatoms), &EvolvedValuePlace::Variable(_) =>
Ok(DatomsTable::FulltextDatoms),
EvolvedValuePlace::Value(TypedValue::String(_)) => Ok(DatomsTable::FulltextDatoms), &EvolvedValuePlace::Value(TypedValue::String(_)) =>
Ok(DatomsTable::FulltextDatoms),
_ => { _ => {
// We can't succeed if there's a non-string constant value for a fulltext // We can't succeed if there's a non-string constant value for a fulltext
// field. // field.
Err(EmptyBecause::NonStringFulltextValue) Err(EmptyBecause::NonStringFulltextValue)
} },
} }
} else { } else {
Ok(DatomsTable::Datoms) Ok(DatomsTable::Datoms)
} }
} }
fn table_for_unknown_attribute( fn table_for_unknown_attribute<'s, 'a>(&self, value: &'a EvolvedValuePlace) -> ::std::result::Result<DatomsTable, EmptyBecause> {
&self,
value: &EvolvedValuePlace,
) -> ::std::result::Result<DatomsTable, EmptyBecause> {
// If the value is known to be non-textual, we can simply use the regular datoms // If the value is known to be non-textual, we can simply use the regular datoms
// table (TODO: and exclude on `index_fulltext`!). // table (TODO: and exclude on `index_fulltext`!).
// //
@ -808,87 +795,72 @@ impl ConjoiningClauses {
// //
// If the value is a variable or string, we must use `all_datoms`, or do the join // If the value is a variable or string, we must use `all_datoms`, or do the join
// ourselves, because we'll need to either extract or compare on the string. // ourselves, because we'll need to either extract or compare on the string.
Ok(match value { Ok(
// TODO: see if the variable is projected, aggregated, or compared elsewhere in match value {
// the query. If it's not, we don't need to use all_datoms here. // TODO: see if the variable is projected, aggregated, or compared elsewhere in
EvolvedValuePlace::Variable(ref v) => { // the query. If it's not, we don't need to use all_datoms here.
// If `required_types` and `known_types` don't exclude strings, &EvolvedValuePlace::Variable(ref v) => {
// we need to query `all_datoms`. // If `required_types` and `known_types` don't exclude strings,
if self // we need to query `all_datoms`.
.required_types if self.required_types.get(v).map_or(true, |s| s.contains(ValueType::String)) &&
.get(v) self.known_types.get(v).map_or(true, |s| s.contains(ValueType::String)) {
.map_or(true, |s| s.contains(ValueType::String)) DatomsTable::AllDatoms
&& self } else {
.known_types DatomsTable::Datoms
.get(v) }
.map_or(true, |s| s.contains(ValueType::String))
{
DatomsTable::AllDatoms
} else {
DatomsTable::Datoms
} }
} &EvolvedValuePlace::Value(TypedValue::String(_)) =>
EvolvedValuePlace::Value(TypedValue::String(_)) => DatomsTable::AllDatoms, DatomsTable::AllDatoms,
_ => DatomsTable::Datoms, _ =>
}) DatomsTable::Datoms,
})
} }
/// Decide which table to use for the provided attribute and value. /// Decide which table to use for the provided attribute and value.
/// If the attribute input or value binding doesn't name an attribute, or doesn't name an /// If the attribute input or value binding doesn't name an attribute, or doesn't name an
/// attribute that is congruent with the supplied value, we return an `EmptyBecause`. /// attribute that is congruent with the supplied value, we return an `EmptyBecause`.
/// The caller is responsible for marking the CC as known-empty if this is a fatal failure. /// The caller is responsible for marking the CC as known-empty if this is a fatal failure.
fn table_for_places<'s, 'a>( fn table_for_places<'s, 'a>(&self, schema: &'s Schema, attribute: &'a EvolvedNonValuePlace, value: &'a EvolvedValuePlace) -> ::std::result::Result<DatomsTable, EmptyBecause> {
&self,
schema: &'s Schema,
attribute: &'a EvolvedNonValuePlace,
value: &'a EvolvedValuePlace,
) -> ::std::result::Result<DatomsTable, EmptyBecause> {
match attribute { match attribute {
EvolvedNonValuePlace::Entid(id) => schema &EvolvedNonValuePlace::Entid(id) =>
.attribute_for_entid(*id) schema.attribute_for_entid(id)
.ok_or_else(|| EmptyBecause::InvalidAttributeEntid(*id)) .ok_or_else(|| EmptyBecause::InvalidAttributeEntid(id))
.and_then(|attribute| self.table_for_attribute_and_value(attribute, value)), .and_then(|attribute| self.table_for_attribute_and_value(attribute, value)),
// TODO: In a prepared context, defer this decision until a second algebrizing phase. // TODO: In a prepared context, defer this decision until a second algebrizing phase.
// #278. // #278.
EvolvedNonValuePlace::Placeholder => self.table_for_unknown_attribute(value), &EvolvedNonValuePlace::Placeholder =>
EvolvedNonValuePlace::Variable(ref v) => { self.table_for_unknown_attribute(value),
&EvolvedNonValuePlace::Variable(ref v) => {
// See if we have a binding for the variable. // See if we have a binding for the variable.
match self.bound_value(v) { match self.bound_value(v) {
// TODO: In a prepared context, defer this decision until a second algebrizing phase. // TODO: In a prepared context, defer this decision until a second algebrizing phase.
// #278. // #278.
None => self.table_for_unknown_attribute(value), None =>
self.table_for_unknown_attribute(value),
Some(TypedValue::Ref(id)) => Some(TypedValue::Ref(id)) =>
// Recurse: it's easy. // Recurse: it's easy.
{ self.table_for_places(schema, &EvolvedNonValuePlace::Entid(id), value),
self.table_for_places(schema, &EvolvedNonValuePlace::Entid(id), value)
}
Some(TypedValue::Keyword(ref kw)) => Some(TypedValue::Keyword(ref kw)) =>
// Don't recurse: avoid needing to clone the keyword. // Don't recurse: avoid needing to clone the keyword.
{ schema.attribute_for_ident(kw)
schema .ok_or_else(|| EmptyBecause::InvalidAttributeIdent(kw.cloned()))
.attribute_for_ident(kw) .and_then(|(attribute, _entid)| self.table_for_attribute_and_value(attribute, value)),
.ok_or_else(|| EmptyBecause::InvalidAttributeIdent(kw.cloned()))
.and_then(|(attribute, _entid)| {
self.table_for_attribute_and_value(attribute, value)
})
}
Some(v) => { Some(v) => {
// This pattern cannot match: the caller has bound a non-entity value to an // This pattern cannot match: the caller has bound a non-entity value to an
// attribute place. // attribute place.
Err(EmptyBecause::InvalidBinding( Err(EmptyBecause::InvalidBinding(Column::Fixed(DatomsColumn::Attribute), v.clone()))
Column::Fixed(DatomsColumn::Attribute), },
v,
))
}
} }
} },
} }
} }
pub(crate) fn next_alias_for_table(&mut self, table: DatomsTable) -> TableAlias { pub(crate) fn next_alias_for_table(&mut self, table: DatomsTable) -> TableAlias {
match table { match table {
DatomsTable::Computed(u) => format!("{}{:02}", table.name(), u), DatomsTable::Computed(u) =>
_ => format!("{}{:02}", table.name(), self.alias_counter.next()), format!("{}{:02}", table.name(), u),
_ =>
format!("{}{:02}", table.name(), self.alias_counter.next()),
} }
} }
@ -896,11 +868,7 @@ impl ConjoiningClauses {
/// This is a mutating method because it mutates the aliaser function! /// This is a mutating method because it mutates the aliaser function!
/// Note that if this function decides that a pattern cannot match, it will flip /// Note that if this function decides that a pattern cannot match, it will flip
/// `empty_because`. /// `empty_because`.
fn alias_table<'s, 'a>( fn alias_table<'s, 'a>(&mut self, schema: &'s Schema, pattern: &'a EvolvedPattern) -> Option<SourceAlias> {
&mut self,
schema: &'s Schema,
pattern: &'a EvolvedPattern,
) -> Option<SourceAlias> {
self.table_for_places(schema, &pattern.attribute, &pattern.value) self.table_for_places(schema, &pattern.attribute, &pattern.value)
.map_err(|reason| { .map_err(|reason| {
self.mark_known_empty(reason); self.mark_known_empty(reason);
@ -909,54 +877,38 @@ impl ConjoiningClauses {
.ok() .ok()
} }
fn get_attribute_for_value<'s>( fn get_attribute_for_value<'s>(&self, schema: &'s Schema, value: &TypedValue) -> Option<&'s Attribute> {
&self,
schema: &'s Schema,
value: &TypedValue,
) -> Option<&'s Attribute> {
match value { match value {
// We know this one is known if the attribute lookup succeeds… // We know this one is known if the attribute lookup succeeds…
TypedValue::Ref(id) => schema.attribute_for_entid(*id), &TypedValue::Ref(id) => schema.attribute_for_entid(id),
TypedValue::Keyword(ref kw) => schema.attribute_for_ident(kw).map(|(a, _id)| a), &TypedValue::Keyword(ref kw) => schema.attribute_for_ident(kw).map(|(a, _id)| a),
_ => None, _ => None,
} }
} }
fn get_attribute<'s, 'a>( fn get_attribute<'s, 'a>(&self, schema: &'s Schema, pattern: &'a EvolvedPattern) -> Option<&'s Attribute> {
&self,
schema: &'s Schema,
pattern: &'a EvolvedPattern,
) -> Option<&'s Attribute> {
match pattern.attribute { match pattern.attribute {
EvolvedNonValuePlace::Entid(id) => EvolvedNonValuePlace::Entid(id) =>
// We know this one is known if the attribute lookup succeeds… // We know this one is known if the attribute lookup succeeds…
{ schema.attribute_for_entid(id),
schema.attribute_for_entid(id)
}
EvolvedNonValuePlace::Variable(ref var) => EvolvedNonValuePlace::Variable(ref var) =>
// If the pattern has a variable, we've already determined that the binding -- if // If the pattern has a variable, we've already determined that the binding -- if
// any -- is acceptable and yields a table. Here, simply look to see if it names // any -- is acceptable and yields a table. Here, simply look to see if it names
// an attribute so we can find out the type. // an attribute so we can find out the type.
{ self.value_bindings.get(var)
self.value_bindings .and_then(|val| self.get_attribute_for_value(schema, val)),
.get(var)
.and_then(|val| self.get_attribute_for_value(schema, val))
}
EvolvedNonValuePlace::Placeholder => None, EvolvedNonValuePlace::Placeholder => None,
} }
} }
fn get_value_type<'s, 'a>( fn get_value_type<'s, 'a>(&self, schema: &'s Schema, pattern: &'a EvolvedPattern) -> Option<ValueType> {
&self,
schema: &'s Schema,
pattern: &'a EvolvedPattern,
) -> Option<ValueType> {
self.get_attribute(schema, pattern).map(|a| a.value_type) self.get_attribute(schema, pattern).map(|a| a.value_type)
} }
} }
/// Expansions. /// Expansions.
impl ConjoiningClauses { impl ConjoiningClauses {
/// Take the contents of `column_bindings` and generate inter-constraints for the appropriate /// Take the contents of `column_bindings` and generate inter-constraints for the appropriate
/// columns into `wheres`. /// columns into `wheres`.
/// ///
@ -975,16 +927,13 @@ impl ConjoiningClauses {
pub(crate) fn expand_column_bindings(&mut self) { pub(crate) fn expand_column_bindings(&mut self) {
for cols in self.column_bindings.values() { for cols in self.column_bindings.values() {
if cols.len() > 1 { if cols.len() > 1 {
let primary = &cols[0]; let ref primary = cols[0];
let secondaries = cols.iter().skip(1); let secondaries = cols.iter().skip(1);
for secondary in secondaries { for secondary in secondaries {
// TODO: if both primary and secondary are .v, should we make sure // TODO: if both primary and secondary are .v, should we make sure
// the type tag columns also match? // the type tag columns also match?
// We don't do so in the ClojureScript version. // We don't do so in the ClojureScript version.
self.wheres.add_intersection(ColumnConstraint::Equals( self.wheres.add_intersection(ColumnConstraint::Equals(primary.clone(), QueryValue::Column(secondary.clone())));
primary.clone(),
QueryValue::Column(secondary.clone()),
));
} }
} }
} }
@ -1013,7 +962,7 @@ impl ConjoiningClauses {
/// This step also updates `known_types` to match. /// This step also updates `known_types` to match.
pub(crate) fn process_required_types(&mut self) -> Result<()> { pub(crate) fn process_required_types(&mut self) -> Result<()> {
if self.empty_because.is_some() { if self.empty_because.is_some() {
return Ok(()); return Ok(())
} }
// We can't call `mark_known_empty` inside the loop since it would be a // We can't call `mark_known_empty` inside the loop since it would be a
@ -1023,18 +972,18 @@ impl ConjoiningClauses {
let mut empty_because: Option<EmptyBecause> = None; let mut empty_because: Option<EmptyBecause> = None;
for (var, types) in self.required_types.clone().into_iter() { for (var, types) in self.required_types.clone().into_iter() {
if let Some(already_known) = self.known_types.get(&var) { if let Some(already_known) = self.known_types.get(&var) {
if already_known.is_disjoint(types) { if already_known.is_disjoint(&types) {
// If we know the constraint can't be one of the types // If we know the constraint can't be one of the types
// the variable could take, then we know we're empty. // the variable could take, then we know we're empty.
empty_because = Some(EmptyBecause::TypeMismatch { empty_because = Some(EmptyBecause::TypeMismatch {
var, var: var,
existing: *already_known, existing: *already_known,
desired: types, desired: types,
}); });
break; break;
} }
if already_known.is_subset(types) { if already_known.is_subset(&types) {
// TODO: I'm not convinced that we can do nothing here. // TODO: I'm not convinced that we can do nothing here.
// //
// Consider `[:find ?x ?v :where [_ _ ?v] [(> ?v 10)] [?x :foo/long ?v]]`. // Consider `[:find ?x ?v :where [_ _ ?v] [(> ?v 10)] [?x :foo/long ?v]]`.
@ -1062,10 +1011,9 @@ impl ConjoiningClauses {
// Update known types. // Update known types.
self.narrow_types_for_var(var.clone(), types); self.narrow_types_for_var(var.clone(), types);
let qa = self let qa = self.extracted_types
.extracted_types .get(&var)
.get(&var) .ok_or_else(|| AlgebrizerError::UnboundVariable(var.name()))?;
.ok_or_else(|| AlgebrizerError::UnboundVariable(var.name()))?;
self.wheres.add_intersection(ColumnConstraint::HasTypes { self.wheres.add_intersection(ColumnConstraint::HasTypes {
value: qa.0.clone(), value: qa.0.clone(),
value_types: types, value_types: types,
@ -1105,52 +1053,44 @@ impl ConjoiningClauses {
} }
impl ConjoiningClauses { impl ConjoiningClauses {
fn apply_evolved_patterns( fn apply_evolved_patterns(&mut self, known: Known, mut patterns: VecDeque<EvolvedPattern>) -> Result<()> {
&mut self,
known: Known,
mut patterns: VecDeque<EvolvedPattern>,
) -> Result<()> {
while let Some(pattern) = patterns.pop_front() { while let Some(pattern) = patterns.pop_front() {
match self.evolve_pattern(known, pattern) { match self.evolve_pattern(known, pattern) {
PlaceOrEmpty::Place(re_evolved) => self.apply_pattern(known, re_evolved), PlaceOrEmpty::Place(re_evolved) => self.apply_pattern(known, re_evolved),
PlaceOrEmpty::Empty(because) => { PlaceOrEmpty::Empty(because) => {
self.mark_known_empty(because); self.mark_known_empty(because);
patterns.clear(); patterns.clear();
} },
} }
} }
Ok(()) Ok(())
} }
fn mark_as_ref(&mut self, pos: &PatternNonValuePlace) { fn mark_as_ref(&mut self, pos: &PatternNonValuePlace) {
if let PatternNonValuePlace::Variable(ref var) = pos { if let &PatternNonValuePlace::Variable(ref var) = pos {
self.constrain_var_to_type(var.clone(), ValueType::Ref) self.constrain_var_to_type(var.clone(), ValueType::Ref)
} }
} }
pub(crate) fn apply_clauses( pub(crate) fn apply_clauses(&mut self, known: Known, where_clauses: Vec<WhereClause>) -> Result<()> {
&mut self,
known: Known,
where_clauses: Vec<WhereClause>,
) -> Result<()> {
// We apply (top level) type predicates first as an optimization. // We apply (top level) type predicates first as an optimization.
for clause in where_clauses.iter() { for clause in where_clauses.iter() {
match clause { match clause {
WhereClause::TypeAnnotation(ref anno) => { &WhereClause::TypeAnnotation(ref anno) => {
self.apply_type_anno(anno)?; self.apply_type_anno(anno)?;
} },
// Patterns are common, so let's grab as much type information from // Patterns are common, so let's grab as much type information from
// them as we can. // them as we can.
WhereClause::Pattern(ref p) => { &WhereClause::Pattern(ref p) => {
self.mark_as_ref(&p.entity); self.mark_as_ref(&p.entity);
self.mark_as_ref(&p.attribute); self.mark_as_ref(&p.attribute);
self.mark_as_ref(&p.tx); self.mark_as_ref(&p.tx);
} },
// TODO: if we wish we can include other kinds of clauses in this type // TODO: if we wish we can include other kinds of clauses in this type
// extraction phase. // extraction phase.
_ => {} _ => {},
} }
} }
@ -1161,15 +1101,17 @@ impl ConjoiningClauses {
let mut patterns: VecDeque<EvolvedPattern> = VecDeque::with_capacity(remaining); let mut patterns: VecDeque<EvolvedPattern> = VecDeque::with_capacity(remaining);
for clause in where_clauses { for clause in where_clauses {
remaining -= 1; remaining -= 1;
if let WhereClause::TypeAnnotation(_) = &clause { if let &WhereClause::TypeAnnotation(_) = &clause {
continue; continue;
} }
match clause { match clause {
WhereClause::Pattern(p) => match self.make_evolved_pattern(known, p) { WhereClause::Pattern(p) => {
PlaceOrEmpty::Place(evolved) => patterns.push_back(evolved), match self.make_evolved_pattern(known, p) {
PlaceOrEmpty::Empty(because) => { PlaceOrEmpty::Place(evolved) => patterns.push_back(evolved),
self.mark_known_empty(because); PlaceOrEmpty::Empty(because) => {
return Ok(()); self.mark_known_empty(because);
return Ok(());
}
} }
}, },
_ => { _ => {
@ -1178,7 +1120,7 @@ impl ConjoiningClauses {
patterns = VecDeque::with_capacity(remaining); patterns = VecDeque::with_capacity(remaining);
} }
self.apply_clause(known, clause)?; self.apply_clause(known, clause)?;
} },
} }
} }
self.apply_evolved_patterns(known, patterns) self.apply_evolved_patterns(known, patterns)
@ -1194,18 +1136,24 @@ impl ConjoiningClauses {
PlaceOrEmpty::Empty(because) => self.mark_known_empty(because), PlaceOrEmpty::Empty(because) => self.mark_known_empty(because),
} }
Ok(()) Ok(())
} },
WhereClause::Pred(p) => self.apply_predicate(known, p), WhereClause::Pred(p) => {
WhereClause::WhereFn(f) => self.apply_where_fn(known, f), self.apply_predicate(known, p)
},
WhereClause::WhereFn(f) => {
self.apply_where_fn(known, f)
},
WhereClause::OrJoin(o) => { WhereClause::OrJoin(o) => {
validate_or_join(&o)?; validate_or_join(&o)?;
self.apply_or_join(known, o) self.apply_or_join(known, o)
} },
WhereClause::NotJoin(n) => { WhereClause::NotJoin(n) => {
validate_not_join(&n)?; validate_not_join(&n)?;
self.apply_not_join(known, n) self.apply_not_join(known, n)
} },
WhereClause::TypeAnnotation(anno) => self.apply_type_anno(&anno), WhereClause::TypeAnnotation(anno) => {
self.apply_type_anno(&anno)
},
_ => unimplemented!(), _ => unimplemented!(),
} }
} }
@ -1227,7 +1175,7 @@ impl PushComputed for Vec<ComputedTable> {
#[cfg(test)] #[cfg(test)]
fn associate_ident(schema: &mut Schema, i: Keyword, e: Entid) { fn associate_ident(schema: &mut Schema, i: Keyword, e: Entid) {
schema.entid_map.insert(e, i.clone()); schema.entid_map.insert(e, i.clone());
schema.ident_map.insert(i, e); schema.ident_map.insert(i.clone(), e);
} }
#[cfg(test)] #[cfg(test)]

View file

@ -8,15 +8,25 @@
// CONDITIONS OF ANY KIND, either express or implied. See the License for the // CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License. // specific language governing permissions and limitations under the License.
use edn::query::{ContainsVariables, NotJoin, UnifyVars}; use mentat_query::{
ContainsVariables,
NotJoin,
UnifyVars,
};
use crate::clauses::ConjoiningClauses; use clauses::ConjoiningClauses;
use query_algebrizer_traits::errors::{AlgebrizerError, Result}; use errors::{
AlgebrizerError,
Result,
};
use crate::types::{ColumnConstraint, ComputedTable}; use types::{
ColumnConstraint,
ComputedTable,
};
use crate::Known; use Known;
impl ConjoiningClauses { impl ConjoiningClauses {
pub(crate) fn apply_not_join(&mut self, known: Known, not_join: NotJoin) -> Result<()> { pub(crate) fn apply_not_join(&mut self, known: Known, not_join: NotJoin) -> Result<()> {
@ -66,10 +76,9 @@ impl ConjoiningClauses {
return Ok(()); return Ok(());
} }
let subquery = ComputedTable::Subquery(Box::new(template)); let subquery = ComputedTable::Subquery(template);
self.wheres self.wheres.add_intersection(ColumnConstraint::NotExists(subquery));
.add_intersection(ColumnConstraint::NotExists(subquery));
Ok(()) Ok(())
} }
@ -81,22 +90,48 @@ mod testing {
use super::*; use super::*;
use core_traits::{Attribute, TypedValue, ValueType, ValueTypeSet}; use mentat_core::{
Attribute,
use mentat_core::Schema; Schema,
TypedValue,
use edn::query::{Keyword, PlainSymbol, Variable}; ValueType,
ValueTypeSet,
use crate::clauses::{add_attribute, associate_ident, QueryInputs};
use query_algebrizer_traits::errors::AlgebrizerError;
use crate::types::{
ColumnAlternation, ColumnConstraint, ColumnConstraintOrAlternation, ColumnIntersection,
DatomsColumn, DatomsTable, Inequality, QualifiedAlias, QueryValue, SourceAlias,
}; };
use crate::{algebrize, algebrize_with_inputs, parse_find_string}; use mentat_query::{
Keyword,
PlainSymbol,
Variable
};
use clauses::{
QueryInputs,
add_attribute,
associate_ident,
};
use errors::{
AlgebrizerError,
};
use types::{
ColumnAlternation,
ColumnConstraint,
ColumnConstraintOrAlternation,
ColumnIntersection,
DatomsColumn,
DatomsTable,
Inequality,
QualifiedAlias,
QueryValue,
SourceAlias,
};
use {
algebrize,
algebrize_with_inputs,
parse_find_string,
};
fn alg(schema: &Schema, input: &str) -> ConjoiningClauses { fn alg(schema: &Schema, input: &str) -> ConjoiningClauses {
let known = Known::for_schema(schema); let known = Known::for_schema(schema);
@ -107,9 +142,7 @@ mod testing {
fn alg_with_inputs(schema: &Schema, input: &str, inputs: QueryInputs) -> ConjoiningClauses { fn alg_with_inputs(schema: &Schema, input: &str, inputs: QueryInputs) -> ConjoiningClauses {
let known = Known::for_schema(schema); let known = Known::for_schema(schema);
let parsed = parse_find_string(input).expect("parse failed"); let parsed = parse_find_string(input).expect("parse failed");
algebrize_with_inputs(known, parsed, 0, inputs) algebrize_with_inputs(known, parsed, 0, inputs).expect("algebrize failed").cc
.expect("algebrize failed")
.cc
} }
fn prepopulated_schema() -> Schema { fn prepopulated_schema() -> Schema {
@ -119,51 +152,41 @@ mod testing {
associate_ident(&mut schema, Keyword::namespaced("foo", "parent"), 67); associate_ident(&mut schema, Keyword::namespaced("foo", "parent"), 67);
associate_ident(&mut schema, Keyword::namespaced("foo", "age"), 68); associate_ident(&mut schema, Keyword::namespaced("foo", "age"), 68);
associate_ident(&mut schema, Keyword::namespaced("foo", "height"), 69); associate_ident(&mut schema, Keyword::namespaced("foo", "height"), 69);
add_attribute( add_attribute(&mut schema,
&mut schema, 65,
65, Attribute {
Attribute { value_type: ValueType::String,
value_type: ValueType::String, multival: false,
multival: false, ..Default::default()
..Default::default() });
}, add_attribute(&mut schema,
); 66,
add_attribute( Attribute {
&mut schema, value_type: ValueType::String,
66, multival: true,
Attribute { ..Default::default()
value_type: ValueType::String, });
multival: true, add_attribute(&mut schema,
..Default::default() 67,
}, Attribute {
); value_type: ValueType::String,
add_attribute( multival: true,
&mut schema, ..Default::default()
67, });
Attribute { add_attribute(&mut schema,
value_type: ValueType::String, 68,
multival: true, Attribute {
..Default::default() value_type: ValueType::Long,
}, multival: false,
); ..Default::default()
add_attribute( });
&mut schema, add_attribute(&mut schema,
68, 69,
Attribute { Attribute {
value_type: ValueType::Long, value_type: ValueType::Long,
multival: false, multival: false,
..Default::default() ..Default::default()
}, });
);
add_attribute(
&mut schema,
69,
Attribute {
value_type: ValueType::Long,
multival: false,
..Default::default()
},
);
schema schema
} }
@ -208,43 +231,24 @@ mod testing {
let daphne = QueryValue::TypedValue(TypedValue::typed_string("Daphne")); let daphne = QueryValue::TypedValue(TypedValue::typed_string("Daphne"));
let mut subquery = ConjoiningClauses::default(); let mut subquery = ConjoiningClauses::default();
subquery.from = vec![ subquery.from = vec![SourceAlias(DatomsTable::Datoms, d1),
SourceAlias(DatomsTable::Datoms, d1), SourceAlias(DatomsTable::Datoms, d2)];
SourceAlias(DatomsTable::Datoms, d2), subquery.column_bindings.insert(vx.clone(), vec![d0e.clone(), d1e.clone(), d2e.clone()]);
]; subquery.wheres = ColumnIntersection(vec![ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d1a.clone(), parent)),
subquery ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d1v.clone(), ambar)),
.column_bindings ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d2a.clone(), knows.clone())),
.insert(vx.clone(), vec![d0e.clone(), d1e.clone(), d2e.clone()]); ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d2v.clone(), daphne)),
subquery.wheres = ColumnIntersection(vec![ ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d0e.clone(), QueryValue::Column(d1e.clone()))),
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d1a, parent)), ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d0e.clone(), QueryValue::Column(d2e.clone())))]);
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d1v, ambar)),
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d2a, knows.clone())),
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d2v, daphne)),
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(
d0e.clone(),
QueryValue::Column(d1e),
)),
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(
d0e.clone(),
QueryValue::Column(d2e),
)),
]);
subquery subquery.known_types.insert(vx.clone(), ValueTypeSet::of_one(ValueType::Ref));
.known_types
.insert(vx.clone(), ValueTypeSet::of_one(ValueType::Ref));
assert!(!cc.is_known_empty()); assert!(!cc.is_known_empty());
assert_eq!( assert_eq!(cc.wheres, ColumnIntersection(vec![
cc.wheres, ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d0a.clone(), knows.clone())),
ColumnIntersection(vec![ ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d0v.clone(), john)),
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d0a, knows)), ColumnConstraintOrAlternation::Constraint(ColumnConstraint::NotExists(ComputedTable::Subquery(subquery))),
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d0v, john)), ]));
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::NotExists(
ComputedTable::Subquery(Box::new(subquery))
)),
])
);
assert_eq!(cc.column_bindings.get(&vx), Some(&vec![d0e])); assert_eq!(cc.column_bindings.get(&vx), Some(&vec![d0e]));
assert_eq!(cc.from, vec![SourceAlias(DatomsTable::Datoms, d0)]); assert_eq!(cc.from, vec![SourceAlias(DatomsTable::Datoms, d0)]);
} }
@ -295,60 +299,31 @@ mod testing {
let mut subquery = ConjoiningClauses::default(); let mut subquery = ConjoiningClauses::default();
subquery.from = vec![SourceAlias(DatomsTable::Datoms, d3)]; subquery.from = vec![SourceAlias(DatomsTable::Datoms, d3)];
subquery subquery.column_bindings.insert(vx.clone(), vec![d0e.clone(), d3e.clone()]);
.column_bindings subquery.column_bindings.insert(vy.clone(), vec![d0v.clone(), d3v.clone()]);
.insert(vx.clone(), vec![d0e.clone(), d3e.clone()]); subquery.wheres = ColumnIntersection(vec![ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d3a.clone(), parent)),
subquery ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d0e.clone(), QueryValue::Column(d3e.clone()))),
.column_bindings ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d0v.clone(), QueryValue::Column(d3v.clone())))]);
.insert(vy.clone(), vec![d0v.clone(), d3v.clone()]);
subquery.wheres = ColumnIntersection(vec![
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d3a, parent)),
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(
d0e.clone(),
QueryValue::Column(d3e),
)),
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(
d0v,
QueryValue::Column(d3v),
)),
]);
subquery subquery.known_types.insert(vx.clone(), ValueTypeSet::of_one(ValueType::Ref));
.known_types subquery.known_types.insert(vy.clone(), ValueTypeSet::of_one(ValueType::String));
.insert(vx.clone(), ValueTypeSet::of_one(ValueType::Ref));
subquery
.known_types
.insert(vy, ValueTypeSet::of_one(ValueType::String));
assert!(!cc.is_known_empty()); assert!(!cc.is_known_empty());
let expected_wheres = ColumnIntersection(vec![ let expected_wheres = ColumnIntersection(vec![
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d0a, knows)), ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d0a.clone(), knows)),
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d1a, age)), ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d1a.clone(), age.clone())),
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d1v, eleven)), ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d1v.clone(), eleven)),
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d2a, name)), ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d2a.clone(), name.clone())),
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d2v, john)), ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d2v.clone(), john)),
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::NotExists( ColumnConstraintOrAlternation::Constraint(ColumnConstraint::NotExists(ComputedTable::Subquery(subquery))),
ComputedTable::Subquery(Box::new(subquery)), ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d0e.clone(), QueryValue::Column(d1e.clone()))),
)), ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d0e.clone(), QueryValue::Column(d2e.clone()))),
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals( ]);
d0e.clone(),
QueryValue::Column(d1e.clone()),
)),
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(
d0e.clone(),
QueryValue::Column(d2e.clone()),
)),
]);
assert_eq!(cc.wheres, expected_wheres); assert_eq!(cc.wheres, expected_wheres);
assert_eq!(cc.column_bindings.get(&vx), Some(&vec![d0e, d1e, d2e])); assert_eq!(cc.column_bindings.get(&vx), Some(&vec![d0e, d1e, d2e]));
assert_eq!( assert_eq!(cc.from, vec![SourceAlias(DatomsTable::Datoms, d0),
cc.from, SourceAlias(DatomsTable::Datoms, d1),
vec![ SourceAlias(DatomsTable::Datoms, d2)]);
SourceAlias(DatomsTable::Datoms, d0),
SourceAlias(DatomsTable::Datoms, d1),
SourceAlias(DatomsTable::Datoms, d2)
]
);
} }
// Not with a pattern and a predicate. // Not with a pattern and a predicate.
@ -388,47 +363,28 @@ mod testing {
let daphne = QueryValue::TypedValue(TypedValue::typed_string("Daphne")); let daphne = QueryValue::TypedValue(TypedValue::typed_string("Daphne"));
let mut subquery = ConjoiningClauses::default(); let mut subquery = ConjoiningClauses::default();
subquery.from = vec![ subquery.from = vec![SourceAlias(DatomsTable::Datoms, d1),
SourceAlias(DatomsTable::Datoms, d1), SourceAlias(DatomsTable::Datoms, d2)];
SourceAlias(DatomsTable::Datoms, d2), subquery.column_bindings.insert(vx.clone(), vec![d0e.clone(), d1e.clone(), d2e.clone()]);
]; subquery.wheres = ColumnIntersection(vec![ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d1a.clone(), knows.clone())),
subquery ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d1v.clone(), john.clone())),
.column_bindings ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d2a.clone(), knows.clone())),
.insert(vx.clone(), vec![d0e.clone(), d1e.clone(), d2e.clone()]); ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d2v.clone(), daphne.clone())),
subquery.wheres = ColumnIntersection(vec![ ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d0e.clone(), QueryValue::Column(d1e.clone()))),
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d1a, knows.clone())), ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d0e.clone(), QueryValue::Column(d2e.clone())))]);
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d1v, john)),
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d2a, knows)),
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d2v, daphne)),
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(
d0e.clone(),
QueryValue::Column(d1e),
)),
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(
d0e.clone(),
QueryValue::Column(d2e),
)),
]);
subquery subquery.known_types.insert(vx.clone(), ValueTypeSet::of_one(ValueType::Ref));
.known_types
.insert(vx.clone(), ValueTypeSet::of_one(ValueType::Ref));
assert!(!cc.is_known_empty()); assert!(!cc.is_known_empty());
assert_eq!( assert_eq!(cc.wheres, ColumnIntersection(vec![
cc.wheres, ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d0a.clone(), age.clone())),
ColumnIntersection(vec![
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d0a, age)),
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Inequality { ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Inequality {
operator: Inequality::LessThan, operator: Inequality::LessThan,
left: QueryValue::Column(d0v), left: QueryValue::Column(d0v.clone()),
right: QueryValue::TypedValue(TypedValue::Long(30)), right: QueryValue::TypedValue(TypedValue::Long(30)),
}), }),
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::NotExists( ColumnConstraintOrAlternation::Constraint(ColumnConstraint::NotExists(ComputedTable::Subquery(subquery))),
ComputedTable::Subquery(Box::new(subquery)) ]));
)),
])
);
assert_eq!(cc.column_bindings.get(&vx), Some(&vec![d0e])); assert_eq!(cc.column_bindings.get(&vx), Some(&vec![d0e]));
assert_eq!(cc.from, vec![SourceAlias(DatomsTable::Datoms, d0)]); assert_eq!(cc.from, vec![SourceAlias(DatomsTable::Datoms, d0)]);
} }
@ -448,7 +404,7 @@ mod testing {
let d0 = "datoms00".to_string(); let d0 = "datoms00".to_string();
let d0e = QualifiedAlias::new(d0.clone(), DatomsColumn::Entity); let d0e = QualifiedAlias::new(d0.clone(), DatomsColumn::Entity);
let d0a = QualifiedAlias::new(d0.clone(), DatomsColumn::Attribute); let d0a = QualifiedAlias::new(d0.clone(), DatomsColumn::Attribute);
let d0v = QualifiedAlias::new(d0, DatomsColumn::Value); let d0v = QualifiedAlias::new(d0.clone(), DatomsColumn::Value);
let d1 = "datoms01".to_string(); let d1 = "datoms01".to_string();
let d1e = QualifiedAlias::new(d1.clone(), DatomsColumn::Entity); let d1e = QualifiedAlias::new(d1.clone(), DatomsColumn::Entity);
@ -470,61 +426,32 @@ mod testing {
let ambar = QueryValue::TypedValue(TypedValue::typed_string("Ámbar")); let ambar = QueryValue::TypedValue(TypedValue::typed_string("Ámbar"));
let daphne = QueryValue::TypedValue(TypedValue::typed_string("Daphne")); let daphne = QueryValue::TypedValue(TypedValue::typed_string("Daphne"));
let mut subquery = ConjoiningClauses::default();
subquery.from = vec![
SourceAlias(DatomsTable::Datoms, d1),
SourceAlias(DatomsTable::Datoms, d2),
];
subquery
.column_bindings
.insert(vx.clone(), vec![d0e.clone(), d1e.clone(), d2e.clone()]);
subquery.wheres = ColumnIntersection(vec![
ColumnConstraintOrAlternation::Alternation(ColumnAlternation(vec![
ColumnIntersection(vec![
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(
d1a.clone(),
knows.clone(),
)),
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(
d1v.clone(),
john,
)),
]),
ColumnIntersection(vec![
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(
d1a,
knows.clone(),
)),
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d1v, ambar)),
]),
])),
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d2a, parent)),
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d2v, daphne)),
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(
d0e.clone(),
QueryValue::Column(d1e),
)),
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(
d0e,
QueryValue::Column(d2e),
)),
]);
subquery let mut subquery = ConjoiningClauses::default();
.known_types subquery.from = vec![SourceAlias(DatomsTable::Datoms, d1),
.insert(vx, ValueTypeSet::of_one(ValueType::Ref)); SourceAlias(DatomsTable::Datoms, d2)];
subquery.column_bindings.insert(vx.clone(), vec![d0e.clone(), d1e.clone(), d2e.clone()]);
subquery.wheres = ColumnIntersection(vec![ColumnConstraintOrAlternation::Alternation(ColumnAlternation(vec![
ColumnIntersection(vec![
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d1a.clone(), knows.clone())),
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d1v.clone(), john))]),
ColumnIntersection(vec![
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d1a.clone(), knows.clone())),
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d1v.clone(), ambar))]),
])),
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d2a.clone(), parent)),
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d2v.clone(), daphne)),
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d0e.clone(), QueryValue::Column(d1e.clone()))),
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d0e.clone(), QueryValue::Column(d2e.clone())))]);
subquery.known_types.insert(vx.clone(), ValueTypeSet::of_one(ValueType::Ref));
assert!(!cc.is_known_empty()); assert!(!cc.is_known_empty());
assert_eq!( assert_eq!(cc.wheres, ColumnIntersection(vec![
cc.wheres, ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d0a.clone(), knows)),
ColumnIntersection(vec![ ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d0v.clone(), bill)),
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d0a, knows)), ColumnConstraintOrAlternation::Constraint(ColumnConstraint::NotExists(ComputedTable::Subquery(subquery))),
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d0v, bill)), ]));
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::NotExists(
ComputedTable::Subquery(Box::new(subquery))
)),
])
);
} }
// not-join with an input variable // not-join with an input variable
@ -537,10 +464,9 @@ mod testing {
:where [?x :foo/knows "Bill"] :where [?x :foo/knows "Bill"]
(not [?x :foo/knows ?y])]"#; (not [?x :foo/knows ?y])]"#;
let inputs = QueryInputs::with_value_sequence(vec![( let inputs = QueryInputs::with_value_sequence(vec![
Variable::from_valid_name("?y"), (Variable::from_valid_name("?y"), "John".into())
"John".into(), ]);
)]);
let cc = alg_with_inputs(&schema, query, inputs); let cc = alg_with_inputs(&schema, query, inputs);
let vx = Variable::from_valid_name("?x"); let vx = Variable::from_valid_name("?x");
@ -554,7 +480,7 @@ mod testing {
let d0 = "datoms00".to_string(); let d0 = "datoms00".to_string();
let d0e = QualifiedAlias::new(d0.clone(), DatomsColumn::Entity); let d0e = QualifiedAlias::new(d0.clone(), DatomsColumn::Entity);
let d0a = QualifiedAlias::new(d0.clone(), DatomsColumn::Attribute); let d0a = QualifiedAlias::new(d0.clone(), DatomsColumn::Attribute);
let d0v = QualifiedAlias::new(d0, DatomsColumn::Value); let d0v = QualifiedAlias::new(d0.clone(), DatomsColumn::Value);
let d1 = "datoms01".to_string(); let d1 = "datoms01".to_string();
let d1e = QualifiedAlias::new(d1.clone(), DatomsColumn::Entity); let d1e = QualifiedAlias::new(d1.clone(), DatomsColumn::Entity);
@ -563,43 +489,25 @@ mod testing {
let mut subquery = ConjoiningClauses::default(); let mut subquery = ConjoiningClauses::default();
subquery.from = vec![SourceAlias(DatomsTable::Datoms, d1)]; subquery.from = vec![SourceAlias(DatomsTable::Datoms, d1)];
subquery subquery.column_bindings.insert(vx.clone(), vec![d0e.clone(), d1e.clone()]);
.column_bindings subquery.wheres = ColumnIntersection(vec![ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d1a.clone(), knows.clone())),
.insert(vx.clone(), vec![d0e.clone(), d1e.clone()]); ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d1v.clone(), john)),
subquery.wheres = ColumnIntersection(vec![ ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d0e.clone(), QueryValue::Column(d1e.clone())))]);
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d1a, knows.clone())),
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d1v, john)),
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(
d0e,
QueryValue::Column(d1e),
)),
]);
subquery subquery.known_types.insert(vx.clone(), ValueTypeSet::of_one(ValueType::Ref));
.known_types subquery.known_types.insert(vy.clone(), ValueTypeSet::of_one(ValueType::String));
.insert(vx, ValueTypeSet::of_one(ValueType::Ref));
subquery
.known_types
.insert(vy.clone(), ValueTypeSet::of_one(ValueType::String));
let mut input_vars: BTreeSet<Variable> = BTreeSet::default(); let mut input_vars: BTreeSet<Variable> = BTreeSet::default();
input_vars.insert(vy.clone()); input_vars.insert(vy.clone());
subquery.input_variables = input_vars; subquery.input_variables = input_vars;
subquery subquery.value_bindings.insert(vy.clone(), TypedValue::typed_string("John"));
.value_bindings
.insert(vy, TypedValue::typed_string("John"));
assert!(!cc.is_known_empty()); assert!(!cc.is_known_empty());
assert_eq!( assert_eq!(cc.wheres, ColumnIntersection(vec![
cc.wheres, ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d0a.clone(), knows)),
ColumnIntersection(vec![ ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d0v.clone(), bill)),
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d0a, knows)), ColumnConstraintOrAlternation::Constraint(ColumnConstraint::NotExists(ComputedTable::Subquery(subquery))),
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d0v, bill)), ]));
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::NotExists(
ComputedTable::Subquery(Box::new(subquery))
)),
])
);
} }
// Test that if any single clause in the `not` fails to resolve the whole clause is considered empty // Test that if any single clause in the `not` fails to resolve the whole clause is considered empty
@ -614,10 +522,9 @@ mod testing {
[?x :foo/nope "Daphne"])]"#; [?x :foo/nope "Daphne"])]"#;
let cc = alg(&schema, query); let cc = alg(&schema, query);
assert!(!cc.is_known_empty()); assert!(!cc.is_known_empty());
compare_ccs( compare_ccs(cc,
cc, alg(&schema,
alg(&schema, r#"[:find ?x :where [?x :foo/knows "Bill"]]"#), r#"[:find ?x :where [?x :foo/knows "Bill"]]"#));
);
} }
/// Test that if all the attributes in an `not` fail to resolve, the `cc` isn't considered empty. /// Test that if all the attributes in an `not` fail to resolve, the `cc` isn't considered empty.
@ -631,10 +538,9 @@ mod testing {
[?x :foo/nope "Daphne"])]"#; [?x :foo/nope "Daphne"])]"#;
let cc = alg(&schema, query); let cc = alg(&schema, query);
assert!(!cc.is_known_empty()); assert!(!cc.is_known_empty());
compare_ccs( compare_ccs(cc,
cc, alg(&schema, r#"[:find ?x :where [?x :foo/knows "John"]]"#));
alg(&schema, r#"[:find ?x :where [?x :foo/knows "John"]]"#),
);
} }
#[test] #[test]
@ -648,9 +554,7 @@ mod testing {
let parsed = parse_find_string(query).expect("parse failed"); let parsed = parse_find_string(query).expect("parse failed");
let err = algebrize(known, parsed).expect_err("algebrization should have failed"); let err = algebrize(known, parsed).expect_err("algebrization should have failed");
match err { match err {
AlgebrizerError::UnboundVariable(var) => { AlgebrizerError::UnboundVariable(var) => { assert_eq!(var, PlainSymbol("?x".to_string())); },
assert_eq!(var, PlainSymbol("?x".to_string()));
}
x => panic!("expected Unbound Variable error, got {:?}", x), x => panic!("expected Unbound Variable error, got {:?}", x),
} }
} }

View file

@ -9,36 +9,60 @@
// specific language governing permissions and limitations under the License. // specific language governing permissions and limitations under the License.
use std::collections::btree_map::Entry; use std::collections::btree_map::Entry;
use std::collections::{BTreeMap, BTreeSet}; use std::collections::{
BTreeMap,
BTreeSet,
};
use core_traits::ValueTypeSet; use mentat_core::{
ValueTypeSet,
};
use edn::query::{ use mentat_query::{
OrJoin, OrWhereClause, Pattern, PatternNonValuePlace, PatternValuePlace, UnifyVars, Variable, OrJoin,
OrWhereClause,
Pattern,
PatternValuePlace,
PatternNonValuePlace,
UnifyVars,
Variable,
WhereClause, WhereClause,
}; };
use crate::clauses::{ConjoiningClauses, PushComputed}; use clauses::{
ConjoiningClauses,
PushComputed,
};
use query_algebrizer_traits::errors::Result; use errors::{
Result,
};
use crate::types::{ use types::{
ColumnAlternation, ColumnConstraintOrAlternation, ColumnIntersection, ComputedTable, ColumnConstraintOrAlternation,
DatomsTable, EmptyBecause, EvolvedPattern, PlaceOrEmpty, QualifiedAlias, SourceAlias, ColumnAlternation,
ColumnIntersection,
ComputedTable,
DatomsTable,
EmptyBecause,
EvolvedPattern,
PlaceOrEmpty,
QualifiedAlias,
SourceAlias,
VariableColumn, VariableColumn,
}; };
use crate::Known; use Known;
/// Return true if both left and right are the same variable or both are non-variable. /// Return true if both left and right are the same variable or both are non-variable.
fn _simply_matches_place(left: &PatternNonValuePlace, right: &PatternNonValuePlace) -> bool { fn _simply_matches_place(left: &PatternNonValuePlace, right: &PatternNonValuePlace) -> bool {
match (left, right) { match (left, right) {
(&PatternNonValuePlace::Variable(ref a), &PatternNonValuePlace::Variable(ref b)) => a == b, (&PatternNonValuePlace::Variable(ref a), &PatternNonValuePlace::Variable(ref b)) => a == b,
(&PatternNonValuePlace::Placeholder, &PatternNonValuePlace::Placeholder) => true, (&PatternNonValuePlace::Placeholder, &PatternNonValuePlace::Placeholder) => true,
(&PatternNonValuePlace::Entid(_), &PatternNonValuePlace::Entid(_)) => true, (&PatternNonValuePlace::Entid(_), &PatternNonValuePlace::Entid(_)) => true,
(&PatternNonValuePlace::Entid(_), &PatternNonValuePlace::Ident(_)) => true, (&PatternNonValuePlace::Entid(_), &PatternNonValuePlace::Ident(_)) => true,
(&PatternNonValuePlace::Ident(_), &PatternNonValuePlace::Ident(_)) => true, (&PatternNonValuePlace::Ident(_), &PatternNonValuePlace::Ident(_)) => true,
(&PatternNonValuePlace::Ident(_), &PatternNonValuePlace::Entid(_)) => true, (&PatternNonValuePlace::Ident(_), &PatternNonValuePlace::Entid(_)) => true,
_ => false, _ => false,
} }
} }
@ -77,7 +101,7 @@ impl ConjoiningClauses {
OrWhereClause::And(clauses) => { OrWhereClause::And(clauses) => {
self.apply_clauses(known, clauses)?; self.apply_clauses(known, clauses)?;
Ok(()) Ok(())
} },
} }
} }
@ -93,7 +117,7 @@ impl ConjoiningClauses {
1 if or_join.is_fully_unified() => { 1 if or_join.is_fully_unified() => {
let clause = or_join.clauses.pop().expect("there's a clause"); let clause = or_join.clauses.pop().expect("there's a clause");
self.apply_or_where_clause(known, clause) self.apply_or_where_clause(known, clause)
} },
// Either there's only one clause pattern, and it's not fully unified, or we // Either there's only one clause pattern, and it's not fully unified, or we
// have multiple clauses. // have multiple clauses.
// In the former case we can't just apply it: it includes a variable that we don't want // In the former case we can't just apply it: it includes a variable that we don't want
@ -198,10 +222,12 @@ impl ConjoiningClauses {
let table = match self.make_evolved_attribute(&known, p.attribute.clone()) { let table = match self.make_evolved_attribute(&known, p.attribute.clone()) {
Place((aaa, value_type)) => { Place((aaa, value_type)) => {
match self.make_evolved_value(&known, value_type, p.value.clone()) { match self.make_evolved_value(&known, value_type, p.value.clone()) {
Place(v) => self.table_for_places(known.schema, &aaa, &v), Place(v) => {
self.table_for_places(known.schema, &aaa, &v)
},
Empty(e) => Err(e), Empty(e) => Err(e),
} }
} },
Empty(e) => Err(e), Empty(e) => Err(e),
}; };
@ -211,19 +237,20 @@ impl ConjoiningClauses {
// Do not accumulate this pattern at all. Add lightness! // Do not accumulate this pattern at all. Add lightness!
continue; continue;
} },
Ok(table) => { Ok(table) => {
// Check the shape of the pattern against a previous pattern. // Check the shape of the pattern against a previous pattern.
let same_shape = if let Some(template) = patterns.get(0) { let same_shape =
template.source == p.source && // or-arms all use the same source anyway. if let Some(template) = patterns.get(0) {
template.source == p.source && // or-arms all use the same source anyway.
_simply_matches_place(&template.entity, &p.entity) && _simply_matches_place(&template.entity, &p.entity) &&
_simply_matches_place(&template.attribute, &p.attribute) && _simply_matches_place(&template.attribute, &p.attribute) &&
_simply_matches_value_place(&template.value, &p.value) && _simply_matches_value_place(&template.value, &p.value) &&
_simply_matches_place(&template.tx, &p.tx) _simply_matches_place(&template.tx, &p.tx)
} else { } else {
// No previous pattern. // No previous pattern.
true true
}; };
// All of our clauses that _do_ yield a table -- that are possible -- // All of our clauses that _do_ yield a table -- that are possible --
// must use the same table in order for this to be a simple `or`! // must use the same table in order for this to be a simple `or`!
@ -259,7 +286,10 @@ impl ConjoiningClauses {
.chain(clauses) .chain(clauses)
.collect(); .collect();
return DeconstructedOrJoin::Complex(OrJoin::new(UnifyVars::Implicit, reconstructed)); return DeconstructedOrJoin::Complex(OrJoin::new(
UnifyVars::Implicit,
reconstructed,
));
} }
// If we got here without returning, then `patterns` is what we're working with. // If we got here without returning, then `patterns` is what we're working with.
@ -268,7 +298,7 @@ impl ConjoiningClauses {
0 => { 0 => {
assert!(empty_because.is_some()); assert!(empty_because.is_some());
DeconstructedOrJoin::KnownEmpty(empty_because.unwrap()) DeconstructedOrJoin::KnownEmpty(empty_because.unwrap())
} },
1 => DeconstructedOrJoin::UnitPattern(patterns.pop().unwrap()), 1 => DeconstructedOrJoin::UnitPattern(patterns.pop().unwrap()),
_ => DeconstructedOrJoin::Simple(patterns, mentioned_vars), _ => DeconstructedOrJoin::Simple(patterns, mentioned_vars),
} }
@ -279,42 +309,43 @@ impl ConjoiningClauses {
DeconstructedOrJoin::KnownSuccess => { DeconstructedOrJoin::KnownSuccess => {
// The pattern came to us empty -- `(or)`. Do nothing. // The pattern came to us empty -- `(or)`. Do nothing.
Ok(()) Ok(())
} },
DeconstructedOrJoin::KnownEmpty(reason) => { DeconstructedOrJoin::KnownEmpty(reason) => {
// There were no arms of the join that could be mapped to a table. // There were no arms of the join that could be mapped to a table.
// The entire `or`, and thus the CC, cannot yield results. // The entire `or`, and thus the CC, cannot yield results.
self.mark_known_empty(reason); self.mark_known_empty(reason);
Ok(()) Ok(())
} },
DeconstructedOrJoin::Unit(clause) => { DeconstructedOrJoin::Unit(clause) => {
// There was only one clause. We're unifying all variables, so we can just apply here. // There was only one clause. We're unifying all variables, so we can just apply here.
self.apply_or_where_clause(known, clause) self.apply_or_where_clause(known, clause)
} },
DeconstructedOrJoin::UnitPattern(pattern) => { DeconstructedOrJoin::UnitPattern(pattern) => {
// Same, but simpler. // Same, but simpler.
match self.make_evolved_pattern(known, pattern) { match self.make_evolved_pattern(known, pattern) {
PlaceOrEmpty::Empty(e) => { PlaceOrEmpty::Empty(e) => {
self.mark_known_empty(e); self.mark_known_empty(e);
} },
PlaceOrEmpty::Place(pattern) => { PlaceOrEmpty::Place(pattern) => {
self.apply_pattern(known, pattern); self.apply_pattern(known, pattern);
} },
}; };
Ok(()) Ok(())
} },
DeconstructedOrJoin::Simple(patterns, mentioned_vars) => { DeconstructedOrJoin::Simple(patterns, mentioned_vars) => {
// Hooray! Fully unified and plain ol' patterns that all use the same table. // Hooray! Fully unified and plain ol' patterns that all use the same table.
// Go right ahead and produce a set of constraint alternations that we can collect, // Go right ahead and produce a set of constraint alternations that we can collect,
// using a single table alias. // using a single table alias.
self.apply_simple_or_join(known, patterns, mentioned_vars) self.apply_simple_or_join(known, patterns, mentioned_vars)
} },
DeconstructedOrJoin::Complex(or_join) => { DeconstructedOrJoin::Complex(or_join) => {
// Do this the hard way. // Do this the hard way.
self.apply_complex_or_join(known, or_join) self.apply_complex_or_join(known, or_join)
} },
} }
} }
/// A simple `or` join is effectively a single pattern in which an individual column's bindings /// A simple `or` join is effectively a single pattern in which an individual column's bindings
/// are not a single value. Rather than a pattern like /// are not a single value. Rather than a pattern like
/// ///
@ -342,30 +373,27 @@ impl ConjoiningClauses {
/// OR (datoms00.a = 98 AND datoms00.v = 'Peter') /// OR (datoms00.a = 98 AND datoms00.v = 'Peter')
/// ``` /// ```
/// ///
fn apply_simple_or_join( fn apply_simple_or_join(&mut self,
&mut self, known: Known,
known: Known, patterns: Vec<Pattern>,
patterns: Vec<Pattern>, mentioned_vars: BTreeSet<Variable>)
mentioned_vars: BTreeSet<Variable>, -> Result<()> {
) -> Result<()> {
if self.is_known_empty() { if self.is_known_empty() {
return Ok(()); return Ok(())
} }
assert!(patterns.len() >= 2); assert!(patterns.len() >= 2);
let patterns: Vec<EvolvedPattern> = patterns let patterns: Vec<EvolvedPattern> = patterns.into_iter().filter_map(|pattern| {
.into_iter() match self.make_evolved_pattern(known, pattern) {
.filter_map(|pattern| { PlaceOrEmpty::Empty(_e) => {
match self.make_evolved_pattern(known, pattern) { // Never mind.
PlaceOrEmpty::Empty(_e) => { None
// Never mind. },
None PlaceOrEmpty::Place(p) => Some(p),
} }
PlaceOrEmpty::Place(p) => Some(p), }).collect();
}
})
.collect();
// Begin by building a base CC that we'll use to produce constraints from each pattern. // Begin by building a base CC that we'll use to produce constraints from each pattern.
// Populate this base CC with whatever variables are already known from the CC to which // Populate this base CC with whatever variables are already known from the CC to which
@ -377,9 +405,7 @@ impl ConjoiningClauses {
// We expect this to always work: if it doesn't, it means we should never have got to this // We expect this to always work: if it doesn't, it means we should never have got to this
// point. // point.
let source_alias = self let source_alias = self.alias_table(known.schema, &patterns[0]).expect("couldn't get table");
.alias_table(known.schema, &patterns[0])
.expect("couldn't get table");
// This is where we'll collect everything we eventually add to the destination CC. // This is where we'll collect everything we eventually add to the destination CC.
let mut folded = ConjoiningClauses::default(); let mut folded = ConjoiningClauses::default();
@ -406,26 +432,24 @@ impl ConjoiningClauses {
// :where [?a :some/int ?x] // :where [?a :some/int ?x]
// [_ :some/otherint ?x]] // [_ :some/otherint ?x]]
// ``` // ```
let mut receptacles = patterns let mut receptacles =
.into_iter() patterns.into_iter()
.map(|pattern| { .map(|pattern| {
let mut receptacle = template.make_receptacle(); let mut receptacle = template.make_receptacle();
receptacle.apply_pattern_clause_for_alias(known, &pattern, &source_alias); receptacle.apply_pattern_clause_for_alias(known, &pattern, &source_alias);
receptacle receptacle
}) })
.peekable(); .peekable();
// Let's see if we can grab a reason if every pattern failed. // Let's see if we can grab a reason if every pattern failed.
// If every pattern failed, we can just take the first! // If every pattern failed, we can just take the first!
let reason = receptacles let reason = receptacles.peek()
.peek() .map(|r| r.empty_because.clone())
.map(|r| r.empty_because.clone()) .unwrap_or(None);
.unwrap_or(None);
// Filter out empties. // Filter out empties.
let mut receptacles = receptacles let mut receptacles = receptacles.filter(|receptacle| !receptacle.is_known_empty())
.filter(|receptacle| !receptacle.is_known_empty()) .peekable();
.peekable();
// We need to copy the column bindings from one of the receptacles. Because this is a simple // We need to copy the column bindings from one of the receptacles. Because this is a simple
// or, we know that they're all the same. // or, we know that they're all the same.
@ -436,10 +460,10 @@ impl ConjoiningClauses {
match self.column_bindings.entry(v.clone()) { match self.column_bindings.entry(v.clone()) {
Entry::Vacant(e) => { Entry::Vacant(e) => {
e.insert(cols.clone()); e.insert(cols.clone());
} },
Entry::Occupied(mut e) => { Entry::Occupied(mut e) => {
e.get_mut().append(&mut cols.clone()); e.get_mut().append(&mut cols.clone());
} },
} }
} }
} else { } else {
@ -459,10 +483,7 @@ impl ConjoiningClauses {
// we might know that if `?x` is an integer then `?y` is a string, or vice versa, but at // we might know that if `?x` is an integer then `?y` is a string, or vice versa, but at
// this point we'll simply state that `?x` and `?y` can both be integers or strings. // this point we'll simply state that `?x` and `?y` can both be integers or strings.
fn vec_for_iterator<T, I, U>(iter: &I) -> Vec<T> fn vec_for_iterator<T, I, U>(iter: &I) -> Vec<T> where I: Iterator<Item=U> {
where
I: Iterator<Item = U>,
{
match iter.size_hint().1 { match iter.size_hint().1 {
None => Vec::new(), None => Vec::new(),
Some(expected) => Vec::with_capacity(expected), Some(expected) => Vec::with_capacity(expected),
@ -572,10 +593,10 @@ impl ConjoiningClauses {
match clause { match clause {
OrWhereClause::And(clauses) => { OrWhereClause::And(clauses) => {
receptacle.apply_clauses(known, clauses)?; receptacle.apply_clauses(known, clauses)?;
} },
OrWhereClause::Clause(clause) => { OrWhereClause::Clause(clause) => {
receptacle.apply_clause(known, clause)?; receptacle.apply_clause(known, clause)?;
} },
} }
if receptacle.is_known_empty() { if receptacle.is_known_empty() {
empty_because = receptacle.empty_because; empty_because = receptacle.empty_because;
@ -642,7 +663,7 @@ impl ConjoiningClauses {
// For any variable which has an imprecise type anywhere in the UNION, add it to the // For any variable which has an imprecise type anywhere in the UNION, add it to the
// set that needs type extraction. All UNION arms must project the same columns. // set that needs type extraction. All UNION arms must project the same columns.
for var in projection.iter() { for var in projection.iter() {
if acc.iter().any(|cc| cc.known_type(var).is_none()) { if acc.iter().any(|cc| !cc.known_type(var).is_some()) {
type_needed.insert(var.clone()); type_needed.insert(var.clone());
} }
} }
@ -660,11 +681,10 @@ impl ConjoiningClauses {
// Note that we start with the first clause's type information. // Note that we start with the first clause's type information.
{ {
let mut clauses = acc.iter(); let mut clauses = acc.iter();
let mut additional_types = clauses let mut additional_types = clauses.next()
.next() .expect("there to be at least one clause")
.expect("there to be at least one clause") .known_types
.known_types .clone();
.clone();
for cc in clauses { for cc in clauses {
union_types(&mut additional_types, &cc.known_types); union_types(&mut additional_types, &cc.known_types);
} }
@ -672,7 +692,7 @@ impl ConjoiningClauses {
} }
let union = ComputedTable::Union { let union = ComputedTable::Union {
projection, projection: projection,
type_extraction: type_needed, type_extraction: type_needed,
arms: acc, arms: acc,
}; };
@ -682,18 +702,10 @@ impl ConjoiningClauses {
// Stitch the computed table into column_bindings, so we get cross-linking. // Stitch the computed table into column_bindings, so we get cross-linking.
let schema = known.schema; let schema = known.schema;
for var in var_associations.into_iter() { for var in var_associations.into_iter() {
self.bind_column_to_var( self.bind_column_to_var(schema, alias.clone(), VariableColumn::Variable(var.clone()), var);
schema,
alias.clone(),
VariableColumn::Variable(var.clone()),
var,
);
} }
for var in type_associations.into_iter() { for var in type_associations.into_iter() {
self.extracted_types.insert( self.extracted_types.insert(var.clone(), QualifiedAlias::new(alias.clone(), VariableColumn::VariableTypeTag(var)));
var.clone(),
QualifiedAlias::new(alias.clone(), VariableColumn::VariableTypeTag(var)),
);
} }
self.from.push(SourceAlias(table, alias)); self.from.push(SourceAlias(table, alias));
Ok(()) Ok(())
@ -701,10 +713,8 @@ impl ConjoiningClauses {
} }
/// Helper to fold together a set of type maps. /// Helper to fold together a set of type maps.
fn union_types( fn union_types(into: &mut BTreeMap<Variable, ValueTypeSet>,
into: &mut BTreeMap<Variable, ValueTypeSet>, additional_types: &BTreeMap<Variable, ValueTypeSet>) {
additional_types: &BTreeMap<Variable, ValueTypeSet>,
) {
// We want the exclusive disjunction -- any variable not mentioned in both sets -- to default // We want the exclusive disjunction -- any variable not mentioned in both sets -- to default
// to ValueTypeSet::Any. // to ValueTypeSet::Any.
// This is necessary because we lazily populate known_types, so sometimes the type set will // This is necessary because we lazily populate known_types, so sometimes the type set will
@ -717,22 +727,21 @@ fn union_types(
{ {
let i: BTreeSet<&Variable> = into.keys().collect(); let i: BTreeSet<&Variable> = into.keys().collect();
let a: BTreeSet<&Variable> = additional_types.keys().collect(); let a: BTreeSet<&Variable> = additional_types.keys().collect();
any = i any = i.symmetric_difference(&a)
.symmetric_difference(&a) .map(|v| ((*v).clone(), ValueTypeSet::any()))
.map(|v| ((*v).clone(), ValueTypeSet::any())) .collect();
.collect();
} }
// Collect the additional types. // Collect the additional types.
for (var, new_types) in additional_types { for (var, new_types) in additional_types {
match into.entry(var.clone()) { match into.entry(var.clone()) {
Entry::Vacant(e) => { Entry::Vacant(e) => {
e.insert(*new_types); e.insert(new_types.clone());
} },
Entry::Occupied(mut e) => { Entry::Occupied(mut e) => {
let new = e.get().union(*new_types); let new = e.get().union(&new_types);
e.insert(new); e.insert(new);
} },
} }
} }
@ -744,20 +753,38 @@ fn union_types(
mod testing { mod testing {
use super::*; use super::*;
use core_traits::{Attribute, TypedValue, ValueType}; use mentat_core::{
Attribute,
Schema,
TypedValue,
ValueType,
};
use mentat_core::Schema; use mentat_query::{
Keyword,
Variable,
};
use edn::query::{Keyword, Variable}; use clauses::{
add_attribute,
associate_ident,
};
use crate::clauses::{add_attribute, associate_ident}; use types::{
ColumnConstraint,
use crate::types::{ DatomsColumn,
ColumnConstraint, DatomsColumn, DatomsTable, Inequality, QualifiedAlias, QueryValue, DatomsTable,
Inequality,
QualifiedAlias,
QueryValue,
SourceAlias, SourceAlias,
}; };
use crate::{algebrize, algebrize_with_counter, parse_find_string}; use {
algebrize,
algebrize_with_counter,
parse_find_string,
};
fn alg(known: Known, input: &str) -> ConjoiningClauses { fn alg(known: Known, input: &str) -> ConjoiningClauses {
let parsed = parse_find_string(input).expect("parse failed"); let parsed = parse_find_string(input).expect("parse failed");
@ -768,9 +795,7 @@ mod testing {
/// simpler version. /// simpler version.
fn alg_c(known: Known, counter: usize, input: &str) -> ConjoiningClauses { fn alg_c(known: Known, counter: usize, input: &str) -> ConjoiningClauses {
let parsed = parse_find_string(input).expect("parse failed"); let parsed = parse_find_string(input).expect("parse failed");
algebrize_with_counter(known, parsed, counter) algebrize_with_counter(known, parsed, counter).expect("algebrize failed").cc
.expect("algebrize failed")
.cc
} }
fn compare_ccs(left: ConjoiningClauses, right: ConjoiningClauses) { fn compare_ccs(left: ConjoiningClauses, right: ConjoiningClauses) {
@ -785,51 +810,31 @@ mod testing {
associate_ident(&mut schema, Keyword::namespaced("foo", "parent"), 67); associate_ident(&mut schema, Keyword::namespaced("foo", "parent"), 67);
associate_ident(&mut schema, Keyword::namespaced("foo", "age"), 68); associate_ident(&mut schema, Keyword::namespaced("foo", "age"), 68);
associate_ident(&mut schema, Keyword::namespaced("foo", "height"), 69); associate_ident(&mut schema, Keyword::namespaced("foo", "height"), 69);
add_attribute( add_attribute(&mut schema, 65, Attribute {
&mut schema, value_type: ValueType::String,
65, multival: false,
Attribute { ..Default::default()
value_type: ValueType::String, });
multival: false, add_attribute(&mut schema, 66, Attribute {
..Default::default() value_type: ValueType::String,
}, multival: true,
); ..Default::default()
add_attribute( });
&mut schema, add_attribute(&mut schema, 67, Attribute {
66, value_type: ValueType::String,
Attribute { multival: true,
value_type: ValueType::String, ..Default::default()
multival: true, });
..Default::default() add_attribute(&mut schema, 68, Attribute {
}, value_type: ValueType::Long,
); multival: false,
add_attribute( ..Default::default()
&mut schema, });
67, add_attribute(&mut schema, 69, Attribute {
Attribute { value_type: ValueType::Long,
value_type: ValueType::String, multival: false,
multival: true, ..Default::default()
..Default::default() });
},
);
add_attribute(
&mut schema,
68,
Attribute {
value_type: ValueType::Long,
multival: false,
..Default::default()
},
);
add_attribute(
&mut schema,
69,
Attribute {
value_type: ValueType::Long,
multival: false,
..Default::default()
},
);
schema schema
} }
@ -845,12 +850,7 @@ mod testing {
[?x :foo/nope3 "Daphne"])]"#; [?x :foo/nope3 "Daphne"])]"#;
let cc = alg(known, query); let cc = alg(known, query);
assert!(cc.is_known_empty()); assert!(cc.is_known_empty());
assert_eq!( assert_eq!(cc.empty_because, Some(EmptyBecause::UnresolvedIdent(Keyword::namespaced("foo", "nope3"))));
cc.empty_because,
Some(EmptyBecause::UnresolvedIdent(Keyword::namespaced(
"foo", "nope3"
)))
);
} }
/// Test that if only one of the attributes in an `or` resolves, it's equivalent to a simple query. /// Test that if only one of the attributes in an `or` resolves, it's equivalent to a simple query.
@ -865,10 +865,7 @@ mod testing {
[?x :foo/nope "Daphne"])]"#; [?x :foo/nope "Daphne"])]"#;
let cc = alg(known, query); let cc = alg(known, query);
assert!(!cc.is_known_empty()); assert!(!cc.is_known_empty());
compare_ccs( compare_ccs(cc, alg(known, r#"[:find ?x :where [?x :foo/parent "Ámbar"]]"#));
cc,
alg(known, r#"[:find ?x :where [?x :foo/parent "Ámbar"]]"#),
);
} }
// Simple alternation. // Simple alternation.
@ -894,41 +891,19 @@ mod testing {
let daphne = QueryValue::TypedValue(TypedValue::typed_string("Daphne")); let daphne = QueryValue::TypedValue(TypedValue::typed_string("Daphne"));
assert!(!cc.is_known_empty()); assert!(!cc.is_known_empty());
assert_eq!( assert_eq!(cc.wheres, ColumnIntersection(vec![
cc.wheres, ColumnConstraintOrAlternation::Alternation(
ColumnIntersection(vec![ColumnConstraintOrAlternation::Alternation(
ColumnAlternation(vec![ ColumnAlternation(vec![
ColumnIntersection(vec![ ColumnIntersection(vec![
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals( ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d0a.clone(), knows.clone())),
d0a.clone(), ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d0v.clone(), john))]),
knows.clone()
)),
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(
d0v.clone(),
john
))
]),
ColumnIntersection(vec![ ColumnIntersection(vec![
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals( ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d0a.clone(), parent)),
d0a.clone(), ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d0v.clone(), ambar))]),
parent
)),
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(
d0v.clone(),
ambar
))
]),
ColumnIntersection(vec![ ColumnIntersection(vec![
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals( ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d0a.clone(), knows)),
d0a, knows ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d0v.clone(), daphne))]),
)), ]))]));
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(
d0v, daphne
))
]),
])
)])
);
assert_eq!(cc.column_bindings.get(&vx), Some(&vec![d0e])); assert_eq!(cc.column_bindings.get(&vx), Some(&vec![d0e]));
assert_eq!(cc.from, vec![SourceAlias(DatomsTable::Datoms, d0)]); assert_eq!(cc.from, vec![SourceAlias(DatomsTable::Datoms, d0)]);
} }
@ -962,55 +937,26 @@ mod testing {
let daphne = QueryValue::TypedValue(TypedValue::typed_string("Daphne")); let daphne = QueryValue::TypedValue(TypedValue::typed_string("Daphne"));
assert!(!cc.is_known_empty()); assert!(!cc.is_known_empty());
assert_eq!( assert_eq!(cc.wheres, ColumnIntersection(vec![
cc.wheres, ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d0a.clone(), name.clone())),
ColumnIntersection(vec![ ColumnConstraintOrAlternation::Alternation(
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d0a, name)), ColumnAlternation(vec![
ColumnConstraintOrAlternation::Alternation(ColumnAlternation(vec![
ColumnIntersection(vec![ ColumnIntersection(vec![
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals( ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d1a.clone(), knows.clone())),
d1a.clone(), ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d1v.clone(), john))]),
knows.clone()
)),
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(
d1v.clone(),
john
))
]),
ColumnIntersection(vec![ ColumnIntersection(vec![
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals( ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d1a.clone(), parent)),
d1a.clone(), ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d1v.clone(), ambar))]),
parent
)),
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(
d1v.clone(),
ambar
))
]),
ColumnIntersection(vec![ ColumnIntersection(vec![
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals( ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d1a.clone(), knows)),
d1a, knows ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d1v.clone(), daphne))]),
)), ])),
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals( // The outer pattern joins against the `or`.
d1v, daphne ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d0e.clone(), QueryValue::Column(d1e.clone()))),
)) ]));
]),
])),
// The outer pattern joins against the `or`.
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(
d0e.clone(),
QueryValue::Column(d1e.clone())
)),
])
);
assert_eq!(cc.column_bindings.get(&vx), Some(&vec![d0e, d1e])); assert_eq!(cc.column_bindings.get(&vx), Some(&vec![d0e, d1e]));
assert_eq!( assert_eq!(cc.from, vec![SourceAlias(DatomsTable::Datoms, d0),
cc.from, SourceAlias(DatomsTable::Datoms, d1)]);
vec![
SourceAlias(DatomsTable::Datoms, d0),
SourceAlias(DatomsTable::Datoms, d1)
]
);
} }
// Alternation with a pattern and a predicate. // Alternation with a pattern and a predicate.
@ -1041,50 +987,28 @@ mod testing {
let daphne = QueryValue::TypedValue(TypedValue::typed_string("Daphne")); let daphne = QueryValue::TypedValue(TypedValue::typed_string("Daphne"));
assert!(!cc.is_known_empty()); assert!(!cc.is_known_empty());
assert_eq!( assert_eq!(cc.wheres, ColumnIntersection(vec![
cc.wheres, ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d0a.clone(), age.clone())),
ColumnIntersection(vec![ ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Inequality {
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d0a, age)), operator: Inequality::LessThan,
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Inequality { left: QueryValue::Column(d0v.clone()),
operator: Inequality::LessThan, right: QueryValue::TypedValue(TypedValue::Long(30)),
left: QueryValue::Column(d0v), }),
right: QueryValue::TypedValue(TypedValue::Long(30)), ColumnConstraintOrAlternation::Alternation(
}), ColumnAlternation(vec![
ColumnConstraintOrAlternation::Alternation(ColumnAlternation(vec![
ColumnIntersection(vec![ ColumnIntersection(vec![
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals( ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d1a.clone(), knows.clone())),
d1a.clone(), ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d1v.clone(), john))]),
knows.clone()
)),
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(
d1v.clone(),
john
))
]),
ColumnIntersection(vec![ ColumnIntersection(vec![
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals( ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d1a.clone(), knows)),
d1a, knows ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d1v.clone(), daphne))]),
)), ])),
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals( // The outer pattern joins against the `or`.
d1v, daphne ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d0e.clone(), QueryValue::Column(d1e.clone()))),
)) ]));
]),
])),
// The outer pattern joins against the `or`.
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(
d0e.clone(),
QueryValue::Column(d1e.clone())
)),
])
);
assert_eq!(cc.column_bindings.get(&vx), Some(&vec![d0e, d1e])); assert_eq!(cc.column_bindings.get(&vx), Some(&vec![d0e, d1e]));
assert_eq!( assert_eq!(cc.from, vec![SourceAlias(DatomsTable::Datoms, d0),
cc.from, SourceAlias(DatomsTable::Datoms, d1)]);
vec![
SourceAlias(DatomsTable::Datoms, d0),
SourceAlias(DatomsTable::Datoms, d1)
]
);
} }
// These two are not equivalent: // These two are not equivalent:
@ -1109,29 +1033,18 @@ mod testing {
let knows = QueryValue::Entid(66); let knows = QueryValue::Entid(66);
assert!(!cc.is_known_empty()); assert!(!cc.is_known_empty());
assert_eq!( assert_eq!(cc.wheres, ColumnIntersection(vec![
cc.wheres, ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d0a.clone(), knows.clone())),
ColumnIntersection(vec![ // The outer pattern joins against the `or` on the entity, but not value -- ?y means
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d0a, knows)), // different things in each place.
// The outer pattern joins against the `or` on the entity, but not value -- ?y means ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d0e.clone(), QueryValue::Column(c0x.clone()))),
// different things in each place. ]));
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(
d0e.clone(),
QueryValue::Column(c0x.clone())
)),
])
);
assert_eq!(cc.column_bindings.get(&vx), Some(&vec![d0e, c0x])); assert_eq!(cc.column_bindings.get(&vx), Some(&vec![d0e, c0x]));
// ?y does not have a binding in the `or-join` pattern. // ?y does not have a binding in the `or-join` pattern.
assert_eq!(cc.column_bindings.get(&vy), Some(&vec![d0v])); assert_eq!(cc.column_bindings.get(&vy), Some(&vec![d0v]));
assert_eq!( assert_eq!(cc.from, vec![SourceAlias(DatomsTable::Datoms, d0),
cc.from, SourceAlias(DatomsTable::Computed(0), c0)]);
vec![
SourceAlias(DatomsTable::Datoms, d0),
SourceAlias(DatomsTable::Computed(0), c0)
]
);
} }
// These two are equivalent: // These two are equivalent:
@ -1141,13 +1054,14 @@ mod testing {
fn test_unit_or_does_flatten() { fn test_unit_or_does_flatten() {
let schema = prepopulated_schema(); let schema = prepopulated_schema();
let known = Known::for_schema(&schema); let known = Known::for_schema(&schema);
let or_query = r#"[:find ?x let or_query = r#"[:find ?x
:where [?x :foo/knows ?y] :where [?x :foo/knows ?y]
(or [?x :foo/parent ?y])]"#; (or [?x :foo/parent ?y])]"#;
let flat_query = r#"[:find ?x let flat_query = r#"[:find ?x
:where [?x :foo/knows ?y] :where [?x :foo/knows ?y]
[?x :foo/parent ?y]]"#; [?x :foo/parent ?y]]"#;
compare_ccs(alg(known, or_query), alg(known, flat_query)); compare_ccs(alg(known, or_query),
alg(known, flat_query));
} }
// Elision of `and`. // Elision of `and`.
@ -1155,13 +1069,14 @@ mod testing {
fn test_unit_or_and_does_flatten() { fn test_unit_or_and_does_flatten() {
let schema = prepopulated_schema(); let schema = prepopulated_schema();
let known = Known::for_schema(&schema); let known = Known::for_schema(&schema);
let or_query = r#"[:find ?x let or_query = r#"[:find ?x
:where (or (and [?x :foo/parent ?y] :where (or (and [?x :foo/parent ?y]
[?x :foo/age 7]))]"#; [?x :foo/age 7]))]"#;
let flat_query = r#"[:find ?x let flat_query = r#"[:find ?x
:where [?x :foo/parent ?y] :where [?x :foo/parent ?y]
[?x :foo/age 7]]"#; [?x :foo/age 7]]"#;
compare_ccs(alg(known, or_query), alg(known, flat_query)); compare_ccs(alg(known, or_query),
alg(known, flat_query));
} }
// Alternation with `and`. // Alternation with `and`.
@ -1183,45 +1098,31 @@ mod testing {
let cc = alg(known, query); let cc = alg(known, query);
let mut tables = cc.computed_tables.into_iter(); let mut tables = cc.computed_tables.into_iter();
match (tables.next(), tables.next()) { match (tables.next(), tables.next()) {
( (Some(ComputedTable::Union { projection, type_extraction, arms }), None) => {
Some(ComputedTable::Union { assert_eq!(projection, vec![Variable::from_valid_name("?x")].into_iter().collect());
projection,
type_extraction,
arms,
}),
None,
) => {
assert_eq!(
projection,
vec![Variable::from_valid_name("?x")].into_iter().collect()
);
assert!(type_extraction.is_empty()); assert!(type_extraction.is_empty());
let mut arms = arms.into_iter(); let mut arms = arms.into_iter();
match (arms.next(), arms.next(), arms.next()) { match (arms.next(), arms.next(), arms.next()) {
(Some(and), Some(pattern), None) => { (Some(and), Some(pattern), None) => {
let expected_and = alg_c( let expected_and = alg_c(known,
known, 0, // The first pattern to be processed.
0, // The first pattern to be processed. r#"[:find ?x :where [?x :foo/knows "John"] [?x :foo/parent "Ámbar"]]"#);
r#"[:find ?x :where [?x :foo/knows "John"] [?x :foo/parent "Ámbar"]]"#,
);
compare_ccs(and, expected_and); compare_ccs(and, expected_and);
let expected_pattern = alg_c( let expected_pattern = alg_c(known,
known, 2, // Two aliases taken by the other arm.
2, // Two aliases taken by the other arm. r#"[:find ?x :where [?x :foo/knows "Daphne"]]"#);
r#"[:find ?x :where [?x :foo/knows "Daphne"]]"#,
);
compare_ccs(pattern, expected_pattern); compare_ccs(pattern, expected_pattern);
} },
_ => { _ => {
panic!("Expected two arms"); panic!("Expected two arms");
} }
} }
} },
_ => { _ => {
panic!("Didn't get two inner tables."); panic!("Didn't get two inner tables.");
} },
} }
} }

File diff suppressed because it is too large Load diff

View file

@ -8,21 +8,36 @@
// CONDITIONS OF ANY KIND, either express or implied. See the License for the // CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License. // specific language governing permissions and limitations under the License.
use core_traits::{ValueType, ValueTypeSet}; use mentat_core::{
Schema,
ValueType,
ValueTypeSet,
};
use mentat_core::Schema; use mentat_query::{
FnArg,
PlainSymbol,
Predicate,
TypeAnnotation,
};
use edn::query::{FnArg, PlainSymbol, Predicate, TypeAnnotation}; use clauses::ConjoiningClauses;
use crate::clauses::ConjoiningClauses; use clauses::convert::ValueTypes;
use crate::clauses::convert::ValueTypes; use errors::{
AlgebrizerError,
Result,
};
use query_algebrizer_traits::errors::{AlgebrizerError, Result}; use types::{
ColumnConstraint,
EmptyBecause,
Inequality,
QueryValue,
};
use crate::types::{ColumnConstraint, EmptyBecause, Inequality, QueryValue}; use Known;
use crate::Known;
/// Application of predicates. /// Application of predicates.
impl ConjoiningClauses { impl ConjoiningClauses {
@ -44,7 +59,7 @@ impl ConjoiningClauses {
fn potential_types(&self, schema: &Schema, fn_arg: &FnArg) -> Result<ValueTypeSet> { fn potential_types(&self, schema: &Schema, fn_arg: &FnArg) -> Result<ValueTypeSet> {
match fn_arg { match fn_arg {
FnArg::Variable(ref v) => Ok(self.known_type_set(v)), &FnArg::Variable(ref v) => Ok(self.known_type_set(v)),
_ => fn_arg.potential_types(schema), _ => fn_arg.potential_types(schema),
} }
} }
@ -53,14 +68,8 @@ impl ConjoiningClauses {
/// to be a specific ValueType. /// to be a specific ValueType.
pub(crate) fn apply_type_anno(&mut self, anno: &TypeAnnotation) -> Result<()> { pub(crate) fn apply_type_anno(&mut self, anno: &TypeAnnotation) -> Result<()> {
match ValueType::from_keyword(&anno.value_type) { match ValueType::from_keyword(&anno.value_type) {
Some(value_type) => { Some(value_type) => self.add_type_requirement(anno.variable.clone(), ValueTypeSet::of_one(value_type)),
self.add_type_requirement(anno.variable.clone(), ValueTypeSet::of_one(value_type)) None => bail!(AlgebrizerError::InvalidArgumentType(PlainSymbol::plain("type"), ValueTypeSet::any(), 2)),
}
None => bail!(AlgebrizerError::InvalidArgumentType(
PlainSymbol::plain("type"),
ValueTypeSet::any(),
2
)),
} }
Ok(()) Ok(())
} }
@ -69,18 +78,9 @@ impl ConjoiningClauses {
/// - Resolves variables and converts types to those more amenable to SQL. /// - Resolves variables and converts types to those more amenable to SQL.
/// - Ensures that the predicate functions name a known operator. /// - Ensures that the predicate functions name a known operator.
/// - Accumulates an `Inequality` constraint into the `wheres` list. /// - Accumulates an `Inequality` constraint into the `wheres` list.
pub(crate) fn apply_inequality( pub(crate) fn apply_inequality(&mut self, known: Known, comparison: Inequality, predicate: Predicate) -> Result<()> {
&mut self,
known: Known,
comparison: Inequality,
predicate: Predicate,
) -> Result<()> {
if predicate.args.len() != 2 { if predicate.args.len() != 2 {
bail!(AlgebrizerError::InvalidNumberOfArguments( bail!(AlgebrizerError::InvalidNumberOfArguments(predicate.operator.clone(), predicate.args.len(), 2));
predicate.operator.clone(),
predicate.args.len(),
2
));
} }
// Go from arguments -- parser output -- to columns or values. // Go from arguments -- parser output -- to columns or values.
@ -90,29 +90,20 @@ impl ConjoiningClauses {
let left = args.next().expect("two args"); let left = args.next().expect("two args");
let right = args.next().expect("two args"); let right = args.next().expect("two args");
// The types we're handling here must be the intersection of the possible types of the arguments, // The types we're handling here must be the intersection of the possible types of the arguments,
// the known types of any variables, and the types supported by our inequality operators. // the known types of any variables, and the types supported by our inequality operators.
let supported_types = comparison.supported_types(); let supported_types = comparison.supported_types();
let mut left_types = self let mut left_types = self.potential_types(known.schema, &left)?
.potential_types(known.schema, &left)? .intersection(&supported_types);
.intersection(supported_types);
if left_types.is_empty() { if left_types.is_empty() {
bail!(AlgebrizerError::InvalidArgumentType( bail!(AlgebrizerError::InvalidArgumentType(predicate.operator.clone(), supported_types, 0));
predicate.operator,
supported_types,
0
));
} }
let mut right_types = self let mut right_types = self.potential_types(known.schema, &right)?
.potential_types(known.schema, &right)? .intersection(&supported_types);
.intersection(supported_types);
if right_types.is_empty() { if right_types.is_empty() {
bail!(AlgebrizerError::InvalidArgumentType( bail!(AlgebrizerError::InvalidArgumentType(predicate.operator.clone(), supported_types, 1));
predicate.operator,
supported_types,
1
));
} }
// We would like to allow longs to compare to doubles. // We would like to allow longs to compare to doubles.
@ -125,7 +116,7 @@ impl ConjoiningClauses {
left_types.insert(ValueType::Double); left_types.insert(ValueType::Double);
} }
let shared_types = left_types.intersection(right_types); let shared_types = left_types.intersection(&right_types);
if shared_types.is_empty() { if shared_types.is_empty() {
// In isolation these are both valid inputs to the operator, but the query cannot // In isolation these are both valid inputs to the operator, but the query cannot
// succeed because the types don't match. // succeed because the types don't match.
@ -141,8 +132,7 @@ impl ConjoiningClauses {
left: left_types, left: left_types,
right: right_types, right: right_types,
} }
}, });
);
return Ok(()); return Ok(());
} }
@ -160,11 +150,7 @@ impl ConjoiningClauses {
left_v = self.resolve_ref_argument(known.schema, &predicate.operator, 0, left)?; left_v = self.resolve_ref_argument(known.schema, &predicate.operator, 0, left)?;
right_v = self.resolve_ref_argument(known.schema, &predicate.operator, 1, right)?; right_v = self.resolve_ref_argument(known.schema, &predicate.operator, 1, right)?;
} else { } else {
bail!(AlgebrizerError::InvalidArgumentType( bail!(AlgebrizerError::InvalidArgumentType(predicate.operator.clone(), supported_types, 0));
predicate.operator,
supported_types,
0
));
} }
// These arguments must be variables or instant/numeric constants. // These arguments must be variables or instant/numeric constants.
@ -176,21 +162,23 @@ impl ConjoiningClauses {
} }
impl Inequality { impl Inequality {
fn to_constraint(self, left: QueryValue, right: QueryValue) -> ColumnConstraint { fn to_constraint(&self, left: QueryValue, right: QueryValue) -> ColumnConstraint {
match self { match *self {
Inequality::TxAfter | Inequality::TxBefore => { Inequality::TxAfter |
Inequality::TxBefore => {
// TODO: both ends of the range must be inside the tx partition! // TODO: both ends of the range must be inside the tx partition!
// If we know the partition map -- and at this point we do, it's just // If we know the partition map -- and at this point we do, it's just
// not passed to this function -- then we can generate two constraints, // not passed to this function -- then we can generate two constraints,
// or clamp a fixed value. // or clamp a fixed value.
} },
_ => {} _ => {
},
} }
ColumnConstraint::Inequality { ColumnConstraint::Inequality {
operator: self, operator: *self,
left, left: left,
right, right: right,
} }
} }
} }
@ -199,16 +187,34 @@ impl Inequality {
mod testing { mod testing {
use super::*; use super::*;
use core_traits::attribute::Unique; use mentat_core::attribute::Unique;
use core_traits::{Attribute, TypedValue, ValueType}; use mentat_core::{
Attribute,
use edn::query::{ TypedValue,
FnArg, Keyword, Pattern, PatternNonValuePlace, PatternValuePlace, PlainSymbol, Variable, ValueType,
}; };
use crate::clauses::{add_attribute, associate_ident, ident}; use mentat_query::{
FnArg,
Keyword,
Pattern,
PatternNonValuePlace,
PatternValuePlace,
PlainSymbol,
Variable,
};
use crate::types::{ColumnConstraint, EmptyBecause, QueryValue}; use clauses::{
add_attribute,
associate_ident,
ident,
};
use types::{
ColumnConstraint,
EmptyBecause,
QueryValue,
};
#[test] #[test]
/// Apply two patterns: a pattern and a numeric predicate. /// Apply two patterns: a pattern and a numeric predicate.
@ -219,45 +225,30 @@ mod testing {
let mut schema = Schema::default(); let mut schema = Schema::default();
associate_ident(&mut schema, Keyword::namespaced("foo", "bar"), 99); associate_ident(&mut schema, Keyword::namespaced("foo", "bar"), 99);
add_attribute( add_attribute(&mut schema, 99, Attribute {
&mut schema, value_type: ValueType::Long,
99, ..Default::default()
Attribute { });
value_type: ValueType::Long,
..Default::default()
},
);
let x = Variable::from_valid_name("?x"); let x = Variable::from_valid_name("?x");
let y = Variable::from_valid_name("?y"); let y = Variable::from_valid_name("?y");
let known = Known::for_schema(&schema); let known = Known::for_schema(&schema);
cc.apply_parsed_pattern( cc.apply_parsed_pattern(known, Pattern {
known, source: None,
Pattern { entity: PatternNonValuePlace::Variable(x.clone()),
source: None, attribute: PatternNonValuePlace::Placeholder,
entity: PatternNonValuePlace::Variable(x), value: PatternValuePlace::Variable(y.clone()),
attribute: PatternNonValuePlace::Placeholder, tx: PatternNonValuePlace::Placeholder,
value: PatternValuePlace::Variable(y.clone()), });
tx: PatternNonValuePlace::Placeholder,
},
);
assert!(!cc.is_known_empty()); assert!(!cc.is_known_empty());
let op = PlainSymbol::plain("<"); let op = PlainSymbol::plain("<");
let comp = Inequality::from_datalog_operator(op.name()).unwrap(); let comp = Inequality::from_datalog_operator(op.name()).unwrap();
assert!(cc assert!(cc.apply_inequality(known, comp, Predicate {
.apply_inequality( operator: op,
known, args: vec![
comp, FnArg::Variable(Variable::from_valid_name("?y")), FnArg::EntidOrInteger(10),
Predicate { ]}).is_ok());
operator: op,
args: vec![
FnArg::Variable(Variable::from_valid_name("?y")),
FnArg::EntidOrInteger(10),
]
}
)
.is_ok());
assert!(!cc.is_known_empty()); assert!(!cc.is_known_empty());
@ -267,21 +258,17 @@ mod testing {
// After processing those two clauses, we know that ?y must be numeric, but not exactly // After processing those two clauses, we know that ?y must be numeric, but not exactly
// which type it must be. // which type it must be.
assert_eq!(None, cc.known_type(&y)); // Not just one. assert_eq!(None, cc.known_type(&y)); // Not just one.
let expected = ValueTypeSet::of_numeric_types(); let expected = ValueTypeSet::of_numeric_types();
assert_eq!(Some(&expected), cc.known_types.get(&y)); assert_eq!(Some(&expected), cc.known_types.get(&y));
let clauses = cc.wheres; let clauses = cc.wheres;
assert_eq!(clauses.len(), 1); assert_eq!(clauses.len(), 1);
assert_eq!( assert_eq!(clauses.0[0], ColumnConstraint::Inequality {
clauses.0[0], operator: Inequality::LessThan,
ColumnConstraint::Inequality { left: QueryValue::Column(cc.column_bindings.get(&y).unwrap()[0].clone()),
operator: Inequality::LessThan, right: QueryValue::TypedValue(TypedValue::Long(10)),
left: QueryValue::Column(cc.column_bindings.get(&y).unwrap()[0].clone()), }.into());
right: QueryValue::TypedValue(TypedValue::Long(10)),
}
.into()
);
} }
#[test] #[test]
@ -294,78 +281,54 @@ mod testing {
associate_ident(&mut schema, Keyword::namespaced("foo", "bar"), 99); associate_ident(&mut schema, Keyword::namespaced("foo", "bar"), 99);
associate_ident(&mut schema, Keyword::namespaced("foo", "roz"), 98); associate_ident(&mut schema, Keyword::namespaced("foo", "roz"), 98);
add_attribute( add_attribute(&mut schema, 99, Attribute {
&mut schema, value_type: ValueType::Long,
99, ..Default::default()
Attribute { });
value_type: ValueType::Long, add_attribute(&mut schema, 98, Attribute {
..Default::default() value_type: ValueType::String,
}, unique: Some(Unique::Identity),
); ..Default::default()
add_attribute( });
&mut schema,
98,
Attribute {
value_type: ValueType::String,
unique: Some(Unique::Identity),
..Default::default()
},
);
let x = Variable::from_valid_name("?x"); let x = Variable::from_valid_name("?x");
let y = Variable::from_valid_name("?y"); let y = Variable::from_valid_name("?y");
let known = Known::for_schema(&schema); let known = Known::for_schema(&schema);
cc.apply_parsed_pattern( cc.apply_parsed_pattern(known, Pattern {
known, source: None,
Pattern { entity: PatternNonValuePlace::Variable(x.clone()),
source: None, attribute: PatternNonValuePlace::Placeholder,
entity: PatternNonValuePlace::Variable(x.clone()), value: PatternValuePlace::Variable(y.clone()),
attribute: PatternNonValuePlace::Placeholder, tx: PatternNonValuePlace::Placeholder,
value: PatternValuePlace::Variable(y.clone()), });
tx: PatternNonValuePlace::Placeholder,
},
);
assert!(!cc.is_known_empty()); assert!(!cc.is_known_empty());
let op = PlainSymbol::plain(">="); let op = PlainSymbol::plain(">=");
let comp = Inequality::from_datalog_operator(op.name()).unwrap(); let comp = Inequality::from_datalog_operator(op.name()).unwrap();
assert!(cc assert!(cc.apply_inequality(known, comp, Predicate {
.apply_inequality( operator: op,
known, args: vec![
comp, FnArg::Variable(Variable::from_valid_name("?y")), FnArg::EntidOrInteger(10),
Predicate { ]}).is_ok());
operator: op,
args: vec![
FnArg::Variable(Variable::from_valid_name("?y")),
FnArg::EntidOrInteger(10),
]
}
)
.is_ok());
assert!(!cc.is_known_empty()); assert!(!cc.is_known_empty());
cc.apply_parsed_pattern( cc.apply_parsed_pattern(known, Pattern {
known, source: None,
Pattern { entity: PatternNonValuePlace::Variable(x.clone()),
source: None, attribute: ident("foo", "roz"),
entity: PatternNonValuePlace::Variable(x), value: PatternValuePlace::Variable(y.clone()),
attribute: ident("foo", "roz"), tx: PatternNonValuePlace::Placeholder,
value: PatternValuePlace::Variable(y.clone()), });
tx: PatternNonValuePlace::Placeholder,
},
);
// Finally, expand column bindings to get the overlaps for ?x. // Finally, expand column bindings to get the overlaps for ?x.
cc.expand_column_bindings(); cc.expand_column_bindings();
assert!(cc.is_known_empty()); assert!(cc.is_known_empty());
assert_eq!( assert_eq!(cc.empty_because.unwrap(),
cc.empty_because.unwrap(), EmptyBecause::TypeMismatch {
EmptyBecause::TypeMismatch { var: y.clone(),
var: y, existing: ValueTypeSet::of_numeric_types(),
existing: ValueTypeSet::of_numeric_types(), desired: ValueTypeSet::of_one(ValueType::String),
desired: ValueTypeSet::of_one(ValueType::String), });
}
);
} }
} }

View file

@ -8,17 +8,30 @@
// CONDITIONS OF ANY KIND, either express or implied. See the License for the // CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License. // specific language governing permissions and limitations under the License.
use core_traits::{TypedValue, ValueType}; use mentat_core::{
HasSchema,
Schema,
TypedValue,
ValueType,
};
use mentat_core::{HasSchema, Schema}; use mentat_query::{
FnArg,
NonIntegerConstant,
PlainSymbol,
};
use edn::query::{FnArg, NonIntegerConstant, PlainSymbol}; use clauses::ConjoiningClauses;
use crate::clauses::ConjoiningClauses; use errors::{
AlgebrizerError,
Result,
};
use query_algebrizer_traits::errors::{AlgebrizerError, Result}; use types::{
EmptyBecause,
use crate::types::{EmptyBecause, QueryValue}; QueryValue,
};
/// Argument resolution. /// Argument resolution.
impl ConjoiningClauses { impl ConjoiningClauses {
@ -27,12 +40,7 @@ impl ConjoiningClauses {
/// Additionally, do two things: /// Additionally, do two things:
/// - Mark the pattern as known-empty if any argument is known non-numeric. /// - Mark the pattern as known-empty if any argument is known non-numeric.
/// - Mark any variables encountered as numeric. /// - Mark any variables encountered as numeric.
pub(crate) fn resolve_numeric_argument( pub(crate) fn resolve_numeric_argument(&mut self, function: &PlainSymbol, position: usize, arg: FnArg) -> Result<QueryValue> {
&mut self,
function: &PlainSymbol,
position: usize,
arg: FnArg,
) -> Result<QueryValue> {
use self::FnArg::*; use self::FnArg::*;
match arg { match arg {
FnArg::Variable(var) => { FnArg::Variable(var) => {
@ -41,14 +49,14 @@ impl ConjoiningClauses {
if v.value_type().is_numeric() { if v.value_type().is_numeric() {
Ok(QueryValue::TypedValue(v)) Ok(QueryValue::TypedValue(v))
} else { } else {
bail!(AlgebrizerError::InputTypeDisagreement(var.name(), ValueType::Long, v.value_type())) bail!(AlgebrizerError::InputTypeDisagreement(var.name().clone(), ValueType::Long, v.value_type()))
} }
} else { } else {
self.constrain_var_to_numeric(var.clone()); self.constrain_var_to_numeric(var.clone());
self.column_bindings self.column_bindings
.get(&var) .get(&var)
.and_then(|cols| cols.first().map(|col| QueryValue::Column(col.clone()))) .and_then(|cols| cols.first().map(|col| QueryValue::Column(col.clone())))
.ok_or_else(|| AlgebrizerError::UnboundVariable(var.name())) .ok_or_else(|| AlgebrizerError::UnboundVariable(var.name()).into())
} }
}, },
// Can't be an entid. // Can't be an entid.
@ -69,62 +77,45 @@ impl ConjoiningClauses {
} }
/// Just like `resolve_numeric_argument`, but for `ValueType::Instant`. /// Just like `resolve_numeric_argument`, but for `ValueType::Instant`.
pub(crate) fn resolve_instant_argument( pub(crate) fn resolve_instant_argument(&mut self, function: &PlainSymbol, position: usize, arg: FnArg) -> Result<QueryValue> {
&mut self,
function: &PlainSymbol,
position: usize,
arg: FnArg,
) -> Result<QueryValue> {
use self::FnArg::*; use self::FnArg::*;
match arg { match arg {
FnArg::Variable(var) => match self.bound_value(&var) { FnArg::Variable(var) => {
Some(TypedValue::Instant(v)) => Ok(QueryValue::TypedValue(TypedValue::Instant(v))), match self.bound_value(&var) {
Some(v) => bail!(AlgebrizerError::InputTypeDisagreement( Some(TypedValue::Instant(v)) => Ok(QueryValue::TypedValue(TypedValue::Instant(v))),
var.name(), Some(v) => bail!(AlgebrizerError::InputTypeDisagreement(var.name().clone(), ValueType::Instant, v.value_type())),
ValueType::Instant, None => {
v.value_type() self.constrain_var_to_type(var.clone(), ValueType::Instant);
)), self.column_bindings
None => { .get(&var)
self.constrain_var_to_type(var.clone(), ValueType::Instant); .and_then(|cols| cols.first().map(|col| QueryValue::Column(col.clone())))
self.column_bindings .ok_or_else(|| AlgebrizerError::UnboundVariable(var.name()).into())
.get(&var) },
.and_then(|cols| cols.first().map(|col| QueryValue::Column(col.clone())))
.ok_or_else(|| AlgebrizerError::UnboundVariable(var.name()))
} }
}, },
Constant(NonIntegerConstant::Instant(v)) => { Constant(NonIntegerConstant::Instant(v)) => {
Ok(QueryValue::TypedValue(TypedValue::Instant(v))) Ok(QueryValue::TypedValue(TypedValue::Instant(v)))
} },
// TODO: should we allow integers if they seem to be timestamps? It's ambiguous… // TODO: should we allow integers if they seem to be timestamps? It's ambiguous…
EntidOrInteger(_) EntidOrInteger(_) |
| IdentOrKeyword(_) IdentOrKeyword(_) |
| SrcVar(_) SrcVar(_) |
| Constant(NonIntegerConstant::Boolean(_)) Constant(NonIntegerConstant::Boolean(_)) |
| Constant(NonIntegerConstant::Float(_)) Constant(NonIntegerConstant::Float(_)) |
| Constant(NonIntegerConstant::Text(_)) Constant(NonIntegerConstant::Text(_)) |
| Constant(NonIntegerConstant::Uuid(_)) Constant(NonIntegerConstant::Uuid(_)) |
| Constant(NonIntegerConstant::BigInteger(_)) Constant(NonIntegerConstant::BigInteger(_)) |
| Vector(_) => { Vector(_) => {
self.mark_known_empty(EmptyBecause::NonInstantArgument); self.mark_known_empty(EmptyBecause::NonInstantArgument);
bail!(AlgebrizerError::InvalidArgumentType( bail!(AlgebrizerError::InvalidArgumentType(function.clone(), ValueType::Instant.into(), position))
function.clone(), },
ValueType::Instant.into(),
position
))
}
} }
} }
/// Take a function argument and turn it into a `QueryValue` suitable for use in a concrete /// Take a function argument and turn it into a `QueryValue` suitable for use in a concrete
/// constraint. /// constraint.
pub(crate) fn resolve_ref_argument( pub(crate) fn resolve_ref_argument(&mut self, schema: &Schema, function: &PlainSymbol, position: usize, arg: FnArg) -> Result<QueryValue> {
&mut self,
schema: &Schema,
function: &PlainSymbol,
position: usize,
arg: FnArg,
) -> Result<QueryValue> {
use self::FnArg::*; use self::FnArg::*;
match arg { match arg {
FnArg::Variable(var) => { FnArg::Variable(var) => {
@ -136,41 +127,33 @@ impl ConjoiningClauses {
self.column_bindings self.column_bindings
.get(&var) .get(&var)
.and_then(|cols| cols.first().map(|col| QueryValue::Column(col.clone()))) .and_then(|cols| cols.first().map(|col| QueryValue::Column(col.clone())))
.ok_or_else(|| AlgebrizerError::UnboundVariable(var.name())) .ok_or_else(|| AlgebrizerError::UnboundVariable(var.name()).into())
} }
} },
EntidOrInteger(i) => Ok(QueryValue::TypedValue(TypedValue::Ref(i))), EntidOrInteger(i) => Ok(QueryValue::TypedValue(TypedValue::Ref(i))),
IdentOrKeyword(i) => schema IdentOrKeyword(i) => {
.get_entid(&i) schema.get_entid(&i)
.map(|known_entid| QueryValue::Entid(known_entid.into())) .map(|known_entid| QueryValue::Entid(known_entid.into()))
.ok_or_else(|| AlgebrizerError::UnrecognizedIdent(i.to_string())), .ok_or_else(|| AlgebrizerError::UnrecognizedIdent(i.to_string()).into())
Constant(NonIntegerConstant::Boolean(_)) },
| Constant(NonIntegerConstant::Float(_)) Constant(NonIntegerConstant::Boolean(_)) |
| Constant(NonIntegerConstant::Text(_)) Constant(NonIntegerConstant::Float(_)) |
| Constant(NonIntegerConstant::Uuid(_)) Constant(NonIntegerConstant::Text(_)) |
| Constant(NonIntegerConstant::Instant(_)) Constant(NonIntegerConstant::Uuid(_)) |
| Constant(NonIntegerConstant::BigInteger(_)) Constant(NonIntegerConstant::Instant(_)) |
| SrcVar(_) Constant(NonIntegerConstant::BigInteger(_)) |
| Vector(_) => { SrcVar(_) |
Vector(_) => {
self.mark_known_empty(EmptyBecause::NonEntityArgument); self.mark_known_empty(EmptyBecause::NonEntityArgument);
bail!(AlgebrizerError::InvalidArgumentType( bail!(AlgebrizerError::InvalidArgumentType(function.clone(), ValueType::Ref.into(), position))
function.clone(), },
ValueType::Ref.into(),
position
))
}
} }
} }
/// Take a transaction ID function argument and turn it into a `QueryValue` suitable for use in /// Take a transaction ID function argument and turn it into a `QueryValue` suitable for use in
/// a concrete constraint. /// a concrete constraint.
pub(crate) fn resolve_tx_argument( pub(crate) fn resolve_tx_argument(&mut self, schema: &Schema, function: &PlainSymbol, position: usize, arg: FnArg) -> Result<QueryValue> {
&mut self,
schema: &Schema,
function: &PlainSymbol,
position: usize,
arg: FnArg,
) -> Result<QueryValue> {
// Under the hood there's nothing special about a transaction ID -- it's just another ref. // Under the hood there's nothing special about a transaction ID -- it's just another ref.
// In the future, we might handle instants specially. // In the future, we might handle instants specially.
self.resolve_ref_argument(schema, function, position, arg) self.resolve_ref_argument(schema, function, position, arg)
@ -182,34 +165,27 @@ impl ConjoiningClauses {
fn resolve_argument(&self, arg: FnArg) -> Result<QueryValue> { fn resolve_argument(&self, arg: FnArg) -> Result<QueryValue> {
use self::FnArg::*; use self::FnArg::*;
match arg { match arg {
FnArg::Variable(var) => match self.bound_value(&var) { FnArg::Variable(var) => {
Some(v) => Ok(QueryValue::TypedValue(v)), match self.bound_value(&var) {
None => self Some(v) => Ok(QueryValue::TypedValue(v)),
.column_bindings None => {
.get(&var) self.column_bindings
.and_then(|cols| cols.first().map(|col| QueryValue::Column(col.clone()))) .get(&var)
.ok_or_else(|| AlgebrizerError::UnboundVariable(var.name())), .and_then(|cols| cols.first().map(|col| QueryValue::Column(col.clone())))
.ok_or_else(|| AlgebrizerError::UnboundVariable(var.name()).into())
},
}
}, },
EntidOrInteger(i) => Ok(QueryValue::PrimitiveLong(i)), EntidOrInteger(i) => Ok(QueryValue::PrimitiveLong(i)),
IdentOrKeyword(_) => unimplemented!(), // TODO IdentOrKeyword(_) => unimplemented!(), // TODO
Constant(NonIntegerConstant::Boolean(val)) => { Constant(NonIntegerConstant::Boolean(val)) => Ok(QueryValue::TypedValue(TypedValue::Boolean(val))),
Ok(QueryValue::TypedValue(TypedValue::Boolean(val))) Constant(NonIntegerConstant::Float(f)) => Ok(QueryValue::TypedValue(TypedValue::Double(f))),
} Constant(NonIntegerConstant::Text(s)) => Ok(QueryValue::TypedValue(TypedValue::typed_string(s.as_str()))),
Constant(NonIntegerConstant::Float(f)) => { Constant(NonIntegerConstant::Uuid(u)) => Ok(QueryValue::TypedValue(TypedValue::Uuid(u))),
Ok(QueryValue::TypedValue(TypedValue::Double(f))) Constant(NonIntegerConstant::Instant(u)) => Ok(QueryValue::TypedValue(TypedValue::Instant(u))),
}
Constant(NonIntegerConstant::Text(s)) => {
Ok(QueryValue::TypedValue(TypedValue::typed_string(s.as_str())))
}
Constant(NonIntegerConstant::Uuid(u)) => {
Ok(QueryValue::TypedValue(TypedValue::Uuid(u)))
}
Constant(NonIntegerConstant::Instant(u)) => {
Ok(QueryValue::TypedValue(TypedValue::Instant(u)))
}
Constant(NonIntegerConstant::BigInteger(_)) => unimplemented!(), Constant(NonIntegerConstant::BigInteger(_)) => unimplemented!(),
SrcVar(_) => unimplemented!(), SrcVar(_) => unimplemented!(),
Vector(_) => unimplemented!(), // TODO Vector(_) => unimplemented!(), // TODO
} }
} }
} }

View file

@ -8,20 +8,40 @@
// CONDITIONS OF ANY KIND, either express or implied. See the License for the // CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License. // specific language governing permissions and limitations under the License.
use core_traits::ValueType; use mentat_core::{
ValueType,
};
use edn::query::{Binding, FnArg, SrcVar, VariableOrPlaceholder, WhereFn}; use mentat_query::{
Binding,
FnArg,
SrcVar,
VariableOrPlaceholder,
WhereFn,
};
use crate::clauses::ConjoiningClauses; use clauses::{
ConjoiningClauses,
};
use query_algebrizer_traits::errors::{AlgebrizerError, BindingError, Result}; use errors::{
AlgebrizerError,
BindingError,
Result,
};
use crate::types::{ use types::{
Column, ColumnConstraint, DatomsTable, Inequality, QualifiedAlias, QueryValue, SourceAlias, Column,
ColumnConstraint,
DatomsTable,
Inequality,
QualifiedAlias,
QueryValue,
SourceAlias,
TransactionsColumn, TransactionsColumn,
}; };
use crate::Known; use Known;
impl ConjoiningClauses { impl ConjoiningClauses {
// Log in Query: tx-ids and tx-data // Log in Query: tx-ids and tx-data
@ -40,27 +60,17 @@ impl ConjoiningClauses {
// transactions that impact one of the given attributes. // transactions that impact one of the given attributes.
pub(crate) fn apply_tx_ids(&mut self, known: Known, where_fn: WhereFn) -> Result<()> { pub(crate) fn apply_tx_ids(&mut self, known: Known, where_fn: WhereFn) -> Result<()> {
if where_fn.args.len() != 3 { if where_fn.args.len() != 3 {
bail!(AlgebrizerError::InvalidNumberOfArguments( bail!(AlgebrizerError::InvalidNumberOfArguments(where_fn.operator.clone(), where_fn.args.len(), 3));
where_fn.operator.clone(),
where_fn.args.len(),
3
));
} }
if where_fn.binding.is_empty() { if where_fn.binding.is_empty() {
// The binding must introduce at least one bound variable. // The binding must introduce at least one bound variable.
bail!(AlgebrizerError::InvalidBinding( bail!(AlgebrizerError::InvalidBinding(where_fn.operator.clone(), BindingError::NoBoundVariable));
where_fn.operator.clone(),
BindingError::NoBoundVariable
));
} }
if !where_fn.binding.is_valid() { if !where_fn.binding.is_valid() {
// The binding must not duplicate bound variables. // The binding must not duplicate bound variables.
bail!(AlgebrizerError::InvalidBinding( bail!(AlgebrizerError::InvalidBinding(where_fn.operator.clone(), BindingError::RepeatedBoundVariable));
where_fn.operator.clone(),
BindingError::RepeatedBoundVariable
));
} }
// We should have exactly one binding. Destructure it now. // We should have exactly one binding. Destructure it now.
@ -68,49 +78,38 @@ impl ConjoiningClauses {
Binding::BindRel(bindings) => { Binding::BindRel(bindings) => {
let bindings_count = bindings.len(); let bindings_count = bindings.len();
if bindings_count != 1 { if bindings_count != 1 {
bail!(AlgebrizerError::InvalidBinding( bail!(AlgebrizerError::InvalidBinding(where_fn.operator.clone(),
where_fn.operator.clone(), BindingError::InvalidNumberOfBindings {
BindingError::InvalidNumberOfBindings { number: bindings_count,
number: bindings_count, expected: 1,
expected: 1, }));
}
));
} }
match bindings.into_iter().next().unwrap() { match bindings.into_iter().next().unwrap() {
VariableOrPlaceholder::Placeholder => unreachable!("binding.is_empty()!"), VariableOrPlaceholder::Placeholder => unreachable!("binding.is_empty()!"),
VariableOrPlaceholder::Variable(v) => v, VariableOrPlaceholder::Variable(v) => v,
} }
} },
Binding::BindColl(v) => v, Binding::BindColl(v) => v,
Binding::BindScalar(_) | Binding::BindTuple(_) => { Binding::BindScalar(_) |
bail!(AlgebrizerError::InvalidBinding( Binding::BindTuple(_) => {
where_fn.operator.clone(), bail!(AlgebrizerError::InvalidBinding(where_fn.operator.clone(), BindingError::ExpectedBindRelOrBindColl))
BindingError::ExpectedBindRelOrBindColl },
))
}
}; };
let mut args = where_fn.args.into_iter(); let mut args = where_fn.args.into_iter();
// TODO: process source variables. // TODO: process source variables.
match args.next().unwrap() { match args.next().unwrap() {
FnArg::SrcVar(SrcVar::DefaultSrc) => {} FnArg::SrcVar(SrcVar::DefaultSrc) => {},
_ => bail!(AlgebrizerError::InvalidArgument( _ => bail!(AlgebrizerError::InvalidArgument(where_fn.operator.clone(), "source variable", 0)),
where_fn.operator.clone(),
"source variable",
0
)),
} }
let tx1 = let tx1 = self.resolve_tx_argument(&known.schema, &where_fn.operator, 1, args.next().unwrap())?;
self.resolve_tx_argument(&known.schema, &where_fn.operator, 1, args.next().unwrap())?; let tx2 = self.resolve_tx_argument(&known.schema, &where_fn.operator, 2, args.next().unwrap())?;
let tx2 =
self.resolve_tx_argument(&known.schema, &where_fn.operator, 2, args.next().unwrap())?;
let transactions = self.next_alias_for_table(DatomsTable::Transactions); let transactions = self.next_alias_for_table(DatomsTable::Transactions);
self.from self.from.push(SourceAlias(DatomsTable::Transactions, transactions.clone()));
.push(SourceAlias(DatomsTable::Transactions, transactions.clone()));
// Bound variable must be a ref. // Bound variable must be a ref.
self.constrain_var_to_type(tx_var.clone(), ValueType::Ref); self.constrain_var_to_type(tx_var.clone(), ValueType::Ref);
@ -118,29 +117,18 @@ impl ConjoiningClauses {
return Ok(()); return Ok(());
} }
self.bind_column_to_var( self.bind_column_to_var(known.schema, transactions.clone(), TransactionsColumn::Tx, tx_var.clone());
known.schema,
transactions.clone(),
TransactionsColumn::Tx,
tx_var,
);
let after_constraint = ColumnConstraint::Inequality { let after_constraint = ColumnConstraint::Inequality {
operator: Inequality::LessThanOrEquals, operator: Inequality::LessThanOrEquals,
left: tx1, left: tx1,
right: QueryValue::Column(QualifiedAlias( right: QueryValue::Column(QualifiedAlias(transactions.clone(), Column::Transactions(TransactionsColumn::Tx))),
transactions.clone(),
Column::Transactions(TransactionsColumn::Tx),
)),
}; };
self.wheres.add_intersection(after_constraint); self.wheres.add_intersection(after_constraint);
let before_constraint = ColumnConstraint::Inequality { let before_constraint = ColumnConstraint::Inequality {
operator: Inequality::LessThan, operator: Inequality::LessThan,
left: QueryValue::Column(QualifiedAlias( left: QueryValue::Column(QualifiedAlias(transactions.clone(), Column::Transactions(TransactionsColumn::Tx))),
transactions,
Column::Transactions(TransactionsColumn::Tx),
)),
right: tx2, right: tx2,
}; };
self.wheres.add_intersection(before_constraint); self.wheres.add_intersection(before_constraint);
@ -150,27 +138,17 @@ impl ConjoiningClauses {
pub(crate) fn apply_tx_data(&mut self, known: Known, where_fn: WhereFn) -> Result<()> { pub(crate) fn apply_tx_data(&mut self, known: Known, where_fn: WhereFn) -> Result<()> {
if where_fn.args.len() != 2 { if where_fn.args.len() != 2 {
bail!(AlgebrizerError::InvalidNumberOfArguments( bail!(AlgebrizerError::InvalidNumberOfArguments(where_fn.operator.clone(), where_fn.args.len(), 2));
where_fn.operator.clone(),
where_fn.args.len(),
2
));
} }
if where_fn.binding.is_empty() { if where_fn.binding.is_empty() {
// The binding must introduce at least one bound variable. // The binding must introduce at least one bound variable.
bail!(AlgebrizerError::InvalidBinding( bail!(AlgebrizerError::InvalidBinding(where_fn.operator.clone(), BindingError::NoBoundVariable));
where_fn.operator.clone(),
BindingError::NoBoundVariable
));
} }
if !where_fn.binding.is_valid() { if !where_fn.binding.is_valid() {
// The binding must not duplicate bound variables. // The binding must not duplicate bound variables.
bail!(AlgebrizerError::InvalidBinding( bail!(AlgebrizerError::InvalidBinding(where_fn.operator.clone(), BindingError::RepeatedBoundVariable));
where_fn.operator.clone(),
BindingError::RepeatedBoundVariable
));
} }
// We should have at most five bindings. Destructure them now. // We should have at most five bindings. Destructure them now.
@ -178,67 +156,42 @@ impl ConjoiningClauses {
Binding::BindRel(bindings) => { Binding::BindRel(bindings) => {
let bindings_count = bindings.len(); let bindings_count = bindings.len();
if bindings_count < 1 || bindings_count > 5 { if bindings_count < 1 || bindings_count > 5 {
bail!(AlgebrizerError::InvalidBinding( bail!(AlgebrizerError::InvalidBinding(where_fn.operator.clone(),
where_fn.operator.clone(), BindingError::InvalidNumberOfBindings {
BindingError::InvalidNumberOfBindings { number: bindings.len(),
number: bindings.len(), expected: 5,
expected: 5, }));
}
));
} }
bindings bindings
} },
Binding::BindScalar(_) | Binding::BindTuple(_) | Binding::BindColl(_) => { Binding::BindScalar(_) |
bail!(AlgebrizerError::InvalidBinding( Binding::BindTuple(_) |
where_fn.operator.clone(), Binding::BindColl(_) => bail!(AlgebrizerError::InvalidBinding(where_fn.operator.clone(), BindingError::ExpectedBindRel)),
BindingError::ExpectedBindRel
))
}
}; };
let mut bindings = bindings.into_iter(); let mut bindings = bindings.into_iter();
let b_e = bindings let b_e = bindings.next().unwrap_or(VariableOrPlaceholder::Placeholder);
.next() let b_a = bindings.next().unwrap_or(VariableOrPlaceholder::Placeholder);
.unwrap_or(VariableOrPlaceholder::Placeholder); let b_v = bindings.next().unwrap_or(VariableOrPlaceholder::Placeholder);
let b_a = bindings let b_tx = bindings.next().unwrap_or(VariableOrPlaceholder::Placeholder);
.next() let b_op = bindings.next().unwrap_or(VariableOrPlaceholder::Placeholder);
.unwrap_or(VariableOrPlaceholder::Placeholder);
let b_v = bindings
.next()
.unwrap_or(VariableOrPlaceholder::Placeholder);
let b_tx = bindings
.next()
.unwrap_or(VariableOrPlaceholder::Placeholder);
let b_op = bindings
.next()
.unwrap_or(VariableOrPlaceholder::Placeholder);
let mut args = where_fn.args.into_iter(); let mut args = where_fn.args.into_iter();
// TODO: process source variables. // TODO: process source variables.
match args.next().unwrap() { match args.next().unwrap() {
FnArg::SrcVar(SrcVar::DefaultSrc) => {} FnArg::SrcVar(SrcVar::DefaultSrc) => {},
_ => bail!(AlgebrizerError::InvalidArgument( _ => bail!(AlgebrizerError::InvalidArgument(where_fn.operator.clone(), "source variable", 0)),
where_fn.operator.clone(),
"source variable",
0
)),
} }
let tx = let tx = self.resolve_tx_argument(&known.schema, &where_fn.operator, 1, args.next().unwrap())?;
self.resolve_tx_argument(&known.schema, &where_fn.operator, 1, args.next().unwrap())?;
let transactions = self.next_alias_for_table(DatomsTable::Transactions); let transactions = self.next_alias_for_table(DatomsTable::Transactions);
self.from self.from.push(SourceAlias(DatomsTable::Transactions, transactions.clone()));
.push(SourceAlias(DatomsTable::Transactions, transactions.clone()));
let tx_constraint = ColumnConstraint::Equals( let tx_constraint = ColumnConstraint::Equals(
QualifiedAlias( QualifiedAlias(transactions.clone(), Column::Transactions(TransactionsColumn::Tx)),
transactions.clone(), tx);
Column::Transactions(TransactionsColumn::Tx),
),
tx,
);
self.wheres.add_intersection(tx_constraint); self.wheres.add_intersection(tx_constraint);
if let VariableOrPlaceholder::Variable(ref var) = b_e { if let VariableOrPlaceholder::Variable(ref var) = b_e {
@ -248,12 +201,7 @@ impl ConjoiningClauses {
return Ok(()); return Ok(());
} }
self.bind_column_to_var( self.bind_column_to_var(known.schema, transactions.clone(), TransactionsColumn::Entity, var.clone());
known.schema,
transactions.clone(),
TransactionsColumn::Entity,
var.clone(),
);
} }
if let VariableOrPlaceholder::Variable(ref var) = b_a { if let VariableOrPlaceholder::Variable(ref var) = b_a {
@ -263,21 +211,11 @@ impl ConjoiningClauses {
return Ok(()); return Ok(());
} }
self.bind_column_to_var( self.bind_column_to_var(known.schema, transactions.clone(), TransactionsColumn::Attribute, var.clone());
known.schema,
transactions.clone(),
TransactionsColumn::Attribute,
var.clone(),
);
} }
if let VariableOrPlaceholder::Variable(ref var) = b_v { if let VariableOrPlaceholder::Variable(ref var) = b_v {
self.bind_column_to_var( self.bind_column_to_var(known.schema, transactions.clone(), TransactionsColumn::Value, var.clone());
known.schema,
transactions.clone(),
TransactionsColumn::Value,
var.clone(),
);
} }
if let VariableOrPlaceholder::Variable(ref var) = b_tx { if let VariableOrPlaceholder::Variable(ref var) = b_tx {
@ -289,12 +227,7 @@ impl ConjoiningClauses {
// TODO: this might be a programming error if var is our tx argument. Perhaps we can be // TODO: this might be a programming error if var is our tx argument. Perhaps we can be
// helpful in that case. // helpful in that case.
self.bind_column_to_var( self.bind_column_to_var(known.schema, transactions.clone(), TransactionsColumn::Tx, var.clone());
known.schema,
transactions.clone(),
TransactionsColumn::Tx,
var.clone(),
);
} }
if let VariableOrPlaceholder::Variable(ref var) = b_op { if let VariableOrPlaceholder::Variable(ref var) = b_op {
@ -304,12 +237,7 @@ impl ConjoiningClauses {
return Ok(()); return Ok(());
} }
self.bind_column_to_var( self.bind_column_to_var(known.schema, transactions.clone(), TransactionsColumn::Added, var.clone());
known.schema,
transactions,
TransactionsColumn::Added,
var.clone(),
);
} }
Ok(()) Ok(())
@ -320,11 +248,18 @@ impl ConjoiningClauses {
mod testing { mod testing {
use super::*; use super::*;
use core_traits::{TypedValue, ValueType}; use mentat_core::{
Schema,
TypedValue,
ValueType,
};
use mentat_core::Schema; use mentat_query::{
Binding,
use edn::query::{Binding, FnArg, PlainSymbol, Variable}; FnArg,
PlainSymbol,
Variable,
};
#[test] #[test]
fn test_apply_tx_ids() { fn test_apply_tx_ids() {
@ -334,21 +269,16 @@ mod testing {
let known = Known::for_schema(&schema); let known = Known::for_schema(&schema);
let op = PlainSymbol::plain("tx-ids"); let op = PlainSymbol::plain("tx-ids");
cc.apply_tx_ids( cc.apply_tx_ids(known, WhereFn {
known, operator: op,
WhereFn { args: vec![
operator: op, FnArg::SrcVar(SrcVar::DefaultSrc),
args: vec![ FnArg::EntidOrInteger(1000),
FnArg::SrcVar(SrcVar::DefaultSrc), FnArg::EntidOrInteger(2000),
FnArg::EntidOrInteger(1000), ],
FnArg::EntidOrInteger(2000), binding: Binding::BindRel(vec![VariableOrPlaceholder::Variable(Variable::from_valid_name("?tx")),
], ]),
binding: Binding::BindRel(vec![VariableOrPlaceholder::Variable( }).expect("to be able to apply_tx_ids");
Variable::from_valid_name("?tx"),
)]),
},
)
.expect("to be able to apply_tx_ids");
assert!(!cc.is_known_empty()); assert!(!cc.is_known_empty());
@ -359,56 +289,31 @@ mod testing {
let clauses = cc.wheres; let clauses = cc.wheres;
assert_eq!(clauses.len(), 2); assert_eq!(clauses.len(), 2);
assert_eq!( assert_eq!(clauses.0[0],
clauses.0[0], ColumnConstraint::Inequality {
ColumnConstraint::Inequality { operator: Inequality::LessThanOrEquals,
operator: Inequality::LessThanOrEquals, left: QueryValue::TypedValue(TypedValue::Ref(1000)),
left: QueryValue::TypedValue(TypedValue::Ref(1000)), right: QueryValue::Column(QualifiedAlias("transactions00".to_string(), Column::Transactions(TransactionsColumn::Tx))),
right: QueryValue::Column(QualifiedAlias( }.into());
"transactions00".to_string(),
Column::Transactions(TransactionsColumn::Tx)
)),
}
.into()
);
assert_eq!( assert_eq!(clauses.0[1],
clauses.0[1], ColumnConstraint::Inequality {
ColumnConstraint::Inequality { operator: Inequality::LessThan,
operator: Inequality::LessThan, left: QueryValue::Column(QualifiedAlias("transactions00".to_string(), Column::Transactions(TransactionsColumn::Tx))),
left: QueryValue::Column(QualifiedAlias( right: QueryValue::TypedValue(TypedValue::Ref(2000)),
"transactions00".to_string(), }.into());
Column::Transactions(TransactionsColumn::Tx)
)),
right: QueryValue::TypedValue(TypedValue::Ref(2000)),
}
.into()
);
let bindings = cc.column_bindings; let bindings = cc.column_bindings;
assert_eq!(bindings.len(), 1); assert_eq!(bindings.len(), 1);
assert_eq!( assert_eq!(bindings.get(&Variable::from_valid_name("?tx")).expect("column binding for ?tx").clone(),
bindings vec![QualifiedAlias("transactions00".to_string(), Column::Transactions(TransactionsColumn::Tx))]);
.get(&Variable::from_valid_name("?tx"))
.expect("column binding for ?tx")
.clone(),
vec![QualifiedAlias(
"transactions00".to_string(),
Column::Transactions(TransactionsColumn::Tx)
)]
);
let known_types = cc.known_types; let known_types = cc.known_types;
assert_eq!(known_types.len(), 1); assert_eq!(known_types.len(), 1);
assert_eq!( assert_eq!(known_types.get(&Variable::from_valid_name("?tx")).expect("known types for ?tx").clone(),
known_types vec![ValueType::Ref].into_iter().collect());
.get(&Variable::from_valid_name("?tx"))
.expect("known types for ?tx")
.clone(),
vec![ValueType::Ref].into_iter().collect()
);
} }
#[test] #[test]
@ -419,24 +324,20 @@ mod testing {
let known = Known::for_schema(&schema); let known = Known::for_schema(&schema);
let op = PlainSymbol::plain("tx-data"); let op = PlainSymbol::plain("tx-data");
cc.apply_tx_data( cc.apply_tx_data(known, WhereFn {
known, operator: op,
WhereFn { args: vec![
operator: op, FnArg::SrcVar(SrcVar::DefaultSrc),
args: vec![ FnArg::EntidOrInteger(1000),
FnArg::SrcVar(SrcVar::DefaultSrc), ],
FnArg::EntidOrInteger(1000), binding: Binding::BindRel(vec![
], VariableOrPlaceholder::Variable(Variable::from_valid_name("?e")),
binding: Binding::BindRel(vec![ VariableOrPlaceholder::Variable(Variable::from_valid_name("?a")),
VariableOrPlaceholder::Variable(Variable::from_valid_name("?e")), VariableOrPlaceholder::Variable(Variable::from_valid_name("?v")),
VariableOrPlaceholder::Variable(Variable::from_valid_name("?a")), VariableOrPlaceholder::Variable(Variable::from_valid_name("?tx")),
VariableOrPlaceholder::Variable(Variable::from_valid_name("?v")), VariableOrPlaceholder::Variable(Variable::from_valid_name("?added")),
VariableOrPlaceholder::Variable(Variable::from_valid_name("?tx")), ]),
VariableOrPlaceholder::Variable(Variable::from_valid_name("?added")), }).expect("to be able to apply_tx_data");
]),
},
)
.expect("to be able to apply_tx_data");
assert!(!cc.is_known_empty()); assert!(!cc.is_known_empty());
@ -447,123 +348,47 @@ mod testing {
let clauses = cc.wheres; let clauses = cc.wheres;
assert_eq!(clauses.len(), 1); assert_eq!(clauses.len(), 1);
assert_eq!( assert_eq!(clauses.0[0],
clauses.0[0], ColumnConstraint::Equals(QualifiedAlias("transactions00".to_string(), Column::Transactions(TransactionsColumn::Tx)),
ColumnConstraint::Equals( QueryValue::TypedValue(TypedValue::Ref(1000))).into());
QualifiedAlias(
"transactions00".to_string(),
Column::Transactions(TransactionsColumn::Tx)
),
QueryValue::TypedValue(TypedValue::Ref(1000))
)
.into()
);
let bindings = cc.column_bindings; let bindings = cc.column_bindings;
assert_eq!(bindings.len(), 5); assert_eq!(bindings.len(), 5);
assert_eq!( assert_eq!(bindings.get(&Variable::from_valid_name("?e")).expect("column binding for ?e").clone(),
bindings vec![QualifiedAlias("transactions00".to_string(), Column::Transactions(TransactionsColumn::Entity))]);
.get(&Variable::from_valid_name("?e"))
.expect("column binding for ?e")
.clone(),
vec![QualifiedAlias(
"transactions00".to_string(),
Column::Transactions(TransactionsColumn::Entity)
)]
);
assert_eq!( assert_eq!(bindings.get(&Variable::from_valid_name("?a")).expect("column binding for ?a").clone(),
bindings vec![QualifiedAlias("transactions00".to_string(), Column::Transactions(TransactionsColumn::Attribute))]);
.get(&Variable::from_valid_name("?a"))
.expect("column binding for ?a")
.clone(),
vec![QualifiedAlias(
"transactions00".to_string(),
Column::Transactions(TransactionsColumn::Attribute)
)]
);
assert_eq!( assert_eq!(bindings.get(&Variable::from_valid_name("?v")).expect("column binding for ?v").clone(),
bindings vec![QualifiedAlias("transactions00".to_string(), Column::Transactions(TransactionsColumn::Value))]);
.get(&Variable::from_valid_name("?v"))
.expect("column binding for ?v")
.clone(),
vec![QualifiedAlias(
"transactions00".to_string(),
Column::Transactions(TransactionsColumn::Value)
)]
);
assert_eq!( assert_eq!(bindings.get(&Variable::from_valid_name("?tx")).expect("column binding for ?tx").clone(),
bindings vec![QualifiedAlias("transactions00".to_string(), Column::Transactions(TransactionsColumn::Tx))]);
.get(&Variable::from_valid_name("?tx"))
.expect("column binding for ?tx")
.clone(),
vec![QualifiedAlias(
"transactions00".to_string(),
Column::Transactions(TransactionsColumn::Tx)
)]
);
assert_eq!( assert_eq!(bindings.get(&Variable::from_valid_name("?added")).expect("column binding for ?added").clone(),
bindings vec![QualifiedAlias("transactions00".to_string(), Column::Transactions(TransactionsColumn::Added))]);
.get(&Variable::from_valid_name("?added"))
.expect("column binding for ?added")
.clone(),
vec![QualifiedAlias(
"transactions00".to_string(),
Column::Transactions(TransactionsColumn::Added)
)]
);
let known_types = cc.known_types; let known_types = cc.known_types;
assert_eq!(known_types.len(), 4); assert_eq!(known_types.len(), 4);
assert_eq!( assert_eq!(known_types.get(&Variable::from_valid_name("?e")).expect("known types for ?e").clone(),
known_types vec![ValueType::Ref].into_iter().collect());
.get(&Variable::from_valid_name("?e"))
.expect("known types for ?e")
.clone(),
vec![ValueType::Ref].into_iter().collect()
);
assert_eq!( assert_eq!(known_types.get(&Variable::from_valid_name("?a")).expect("known types for ?a").clone(),
known_types vec![ValueType::Ref].into_iter().collect());
.get(&Variable::from_valid_name("?a"))
.expect("known types for ?a")
.clone(),
vec![ValueType::Ref].into_iter().collect()
);
assert_eq!( assert_eq!(known_types.get(&Variable::from_valid_name("?tx")).expect("known types for ?tx").clone(),
known_types vec![ValueType::Ref].into_iter().collect());
.get(&Variable::from_valid_name("?tx"))
.expect("known types for ?tx")
.clone(),
vec![ValueType::Ref].into_iter().collect()
);
assert_eq!( assert_eq!(known_types.get(&Variable::from_valid_name("?added")).expect("known types for ?added").clone(),
known_types vec![ValueType::Boolean].into_iter().collect());
.get(&Variable::from_valid_name("?added"))
.expect("known types for ?added")
.clone(),
vec![ValueType::Boolean].into_iter().collect()
);
let extracted_types = cc.extracted_types; let extracted_types = cc.extracted_types;
assert_eq!(extracted_types.len(), 1); assert_eq!(extracted_types.len(), 1);
assert_eq!( assert_eq!(extracted_types.get(&Variable::from_valid_name("?v")).expect("extracted types for ?v").clone(),
extracted_types QualifiedAlias("transactions00".to_string(), Column::Transactions(TransactionsColumn::ValueTypeTag)));
.get(&Variable::from_valid_name("?v"))
.expect("extracted types for ?v")
.clone(),
QualifiedAlias(
"transactions00".to_string(),
Column::Transactions(TransactionsColumn::ValueTypeTag)
)
);
} }
} }

View file

@ -8,13 +8,20 @@
// CONDITIONS OF ANY KIND, either express or implied. See the License for the // CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License. // specific language governing permissions and limitations under the License.
use edn::query::WhereFn; use mentat_query::{
WhereFn,
};
use crate::clauses::ConjoiningClauses; use clauses::{
ConjoiningClauses,
};
use query_algebrizer_traits::errors::{AlgebrizerError, Result}; use errors::{
AlgebrizerError,
Result,
};
use crate::Known; use Known;
/// Application of `where` functions. /// Application of `where` functions.
impl ConjoiningClauses { impl ConjoiningClauses {

Some files were not shown because too many files have changed in this diff Show more