From da1250d2105a28ea2fe3fe3b9f412a4ea65c4f4d Mon Sep 17 00:00:00 2001 From: Nick Alexander Date: Tue, 6 Sep 2016 14:51:57 -0700 Subject: [PATCH] Part 1: Separate tx_lookup into tx_lookup_before and tx_lookup_after. --- src/common/datomish/db.cljc | 124 ++++++++++++++----------- src/common/datomish/sqlite_schema.cljc | 43 +++++---- 2 files changed, 94 insertions(+), 73 deletions(-) diff --git a/src/common/datomish/db.cljc b/src/common/datomish/db.cljc index 5fd28115..eab3049f 100644 --- a/src/common/datomish/db.cljc +++ b/src/common/datomish/db.cljc @@ -172,9 +172,7 @@ :table-alias source/gensym-table-alias :make-constraints nil})) -;; TODO: make this not do the tx_lookup. We could achieve this by having additional special values -;; of added0, or by separating the tx_lookup table into before and after tables. -(defn- retractAttributes->queries [eas tx] +(defn- retractAttributes->queries [oeas tx] (let [where-part "(e = ? AND a = ?)" @@ -183,21 +181,23 @@ (fn [chunk] (cons (apply str - "INSERT INTO tx_lookup (e0, a0, v0, tx0, added0, value_type_tag0, sv, svalue_type_tag) - SELECT e, a, v, ?, 0, value_type_tag, v, value_type_tag - FROM datoms - WHERE " + "INSERT INTO temp.tx_lookup_after (e0, a0, v0, tx0, added0, value_type_tag0, sv, svalue_type_tag, + rid, e, a, v, tx, value_type_tag) + SELECT e, a, v, ?, 0 + 2, value_type_tag, v, value_type_tag, + rowid, e, a, v, ?, value_type_tag + FROM datoms + WHERE " (repeater (count chunk))) (cons tx - (mapcat (fn [[_ e a]] - [e a]) - chunk)))) - (partition-all (quot (dec max-sql-vars) 2) eas)))) + (cons + tx + (mapcat (fn [[_ e a]] + [e a]) + chunk))))) + (partition-all (quot (- max-sql-vars 2) 2) oeas)))) -;; TODO: make this not do the tx_lookup. We could achieve this by having additional special values -;; of added0, or by separating the tx_lookup table into before and after tables. -(defn- retractEntities->queries [es tx] +(defn- retractEntities->queries [oes tx] (let [ref-tag (sqlite-schema/->tag :db.type/ref) ;; TODO: include index_vaet flag here, so we can use that index to speed up the deletion. @@ -209,27 +209,31 @@ (fn [chunk] (cons (apply str - "INSERT INTO tx_lookup (e0, a0, v0, tx0, added0, value_type_tag0, sv, svalue_type_tag) - SELECT e, a, v, ?, 0, value_type_tag, v, value_type_tag - FROM datoms - WHERE " + "INSERT INTO temp.tx_lookup_after (e0, a0, v0, tx0, added0, value_type_tag0, sv, svalue_type_tag, + rid, e, a, v, tx, value_type_tag) + SELECT e, a, v, ?, 0 + 2, value_type_tag, v, value_type_tag, + rowid, e, a, v, ?, value_type_tag + FROM datoms + WHERE " (repeater (count chunk))) (cons tx - (mapcat (fn [[_ e]] - [e e]) - chunk)))) - (partition-all (quot (dec max-sql-vars) 2) es)))) + (cons + tx + (mapcat (fn [[_ e]] + [e e]) + chunk))))) + (partition-all (quot (- max-sql-vars 2) 2) oes)))) (defn- retractions->queries [retractions tx fulltext? ->SQLite] (let [f-q "WITH vv AS (SELECT rowid FROM fulltext_values WHERE text = ?) - INSERT INTO tx_lookup (e0, a0, v0, tx0, added0, value_type_tag0, sv, svalue_type_tag) + INSERT INTO temp.tx_lookup_before (e0, a0, v0, tx0, added0, value_type_tag0, sv, svalue_type_tag) VALUES (?, ?, (SELECT rowid FROM vv), ?, 0, ?, (SELECT rowid FROM vv), ?)" non-f-q - "INSERT INTO tx_lookup (e0, a0, v0, tx0, added0, value_type_tag0, sv, svalue_type_tag) + "INSERT INTO temp.tx_lookup_before (e0, a0, v0, tx0, added0, value_type_tag0, sv, svalue_type_tag) VALUES (?, ?, ?, ?, 0, ?, ?, ?)"] (map (fn [[_ e a v]] @@ -242,7 +246,7 @@ retractions))) (defn- non-fts-many->queries [ops tx ->SQLite indexing? ref? unique?] - (let [q "INSERT INTO tx_lookup (e0, a0, v0, tx0, added0, value_type_tag0, index_avet0, index_vaet0, index_fulltext0, unique_value0, sv, svalue_type_tag) VALUES " + (let [q "INSERT INTO temp.tx_lookup_before (e0, a0, v0, tx0, added0, value_type_tag0, index_avet0, index_vaet0, index_fulltext0, unique_value0, sv, svalue_type_tag) VALUES " values-part ;; e0, a0, v0, tx0, added0, value_type_tag0 @@ -290,7 +294,7 @@ [(cons (apply str - "INSERT INTO tx_lookup (e0, a0, v0, tx0, added0, value_type_tag0, index_avet0, index_vaet0, index_fulltext0, unique_value0, sv, svalue_type_tag) VALUES " + "INSERT INTO temp.tx_lookup_before (e0, a0, v0, tx0, added0, value_type_tag0, index_avet0, index_vaet0, index_fulltext0, unique_value0, sv, svalue_type_tag) VALUES " (first-repeater (count chunk))) (mapcat (fn [[_ e a v]] (let [[v tag] (->SQLite a v)] @@ -304,7 +308,7 @@ (cons (apply str - "INSERT INTO tx_lookup (e0, a0, v0, tx0, added0, value_type_tag0) VALUES " + "INSERT INTO temp.tx_lookup_before (e0, a0, v0, tx0, added0, value_type_tag0) VALUES " (second-repeater (count chunk))) (mapcat (fn [[_ e a v]] (let [[v tag] (->SQLite a v)] @@ -341,10 +345,10 @@ [["INSERT INTO fulltext_values_view (text, searchid) VALUES (?, ?)" v searchid] - ;; Second query: tx_lookup. + ;; Second query: lookup. [(str "WITH vv(rowid) AS (SELECT rowid FROM fulltext_values WHERE searchid = ?) " - "INSERT INTO tx_lookup (e0, a0, v0, tx0, added0, value_type_tag0, index_avet0, index_vaet0, index_fulltext0, unique_value0, sv, svalue_type_tag) VALUES " + "INSERT INTO temp.tx_lookup_before (e0, a0, v0, tx0, added0, value_type_tag0, index_avet0, index_vaet0, index_fulltext0, unique_value0, sv, svalue_type_tag) VALUES " "(?, ?, (SELECT rowid FROM vv), ?, 1, ?, ?, ?, 1, ?, (SELECT rowid FROM vv), ?)") searchid e a tx tag @@ -365,10 +369,10 @@ [["INSERT INTO fulltext_values_view (text, searchid) VALUES (?, ?)" v searchid] - ;; Second and third queries: tx_lookup. + ;; Second and third queries: lookup. [(str "WITH vv(rowid) AS (SELECT rowid FROM fulltext_values WHERE searchid = ?) " - "INSERT INTO tx_lookup (e0, a0, v0, tx0, added0, value_type_tag0, index_avet0, index_vaet0, index_fulltext0, unique_value0, sv, svalue_type_tag) VALUES " + "INSERT INTO temp.tx_lookup_before (e0, a0, v0, tx0, added0, value_type_tag0, index_avet0, index_vaet0, index_fulltext0, unique_value0, sv, svalue_type_tag) VALUES " "(?, ?, (SELECT rowid FROM vv), ?, 1, ?, ?, ?, 1, ?, (SELECT rowid FROM vv), ?)") searchid e a tx tag @@ -378,7 +382,7 @@ tag] [(str - "INSERT INTO tx_lookup (e0, a0, v0, tx0, added0, value_type_tag0) VALUES " + "INSERT INTO temp.tx_lookup_before (e0, a0, v0, tx0, added0, value_type_tag0) VALUES " "(?, ?, (SELECT rowid FROM fulltext_values WHERE searchid = ?), ?, 0, ?)") e a searchid tx tag]])) ops @@ -390,33 +394,43 @@ (try (doseq [q queries] (>