Part 2: Get rid of {0, 1} -> {2, 3} mapping for added/added0. Fixes #28.

Now that we copying from tx_lookup_before -> tx_lookup_after, we don't
need to avoid duplicating rows.
This commit is contained in:
Nick Alexander 2016-09-06 14:56:29 -07:00
parent da1250d210
commit c46f0eb8ae

View file

@ -183,7 +183,7 @@
(apply str (apply str
"INSERT INTO temp.tx_lookup_after (e0, a0, v0, tx0, added0, value_type_tag0, sv, svalue_type_tag, "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) rid, e, a, v, tx, value_type_tag)
SELECT e, a, v, ?, 0 + 2, value_type_tag, v, value_type_tag, SELECT e, a, v, ?, 0, value_type_tag, v, value_type_tag,
rowid, e, a, v, ?, value_type_tag rowid, e, a, v, ?, value_type_tag
FROM datoms FROM datoms
WHERE " WHERE "
@ -211,7 +211,7 @@
(apply str (apply str
"INSERT INTO temp.tx_lookup_after (e0, a0, v0, tx0, added0, value_type_tag0, sv, svalue_type_tag, "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) rid, e, a, v, tx, value_type_tag)
SELECT e, a, v, ?, 0 + 2, value_type_tag, v, value_type_tag, SELECT e, a, v, ?, 0, value_type_tag, v, value_type_tag,
rowid, e, a, v, ?, value_type_tag rowid, e, a, v, ?, value_type_tag
FROM datoms FROM datoms
WHERE " WHERE "
@ -429,7 +429,7 @@
;; Second is slower, but still only one table walk: lookup old value by ea. ;; Second is slower, but still only one table walk: lookup old value by ea.
insert-into-tx-lookup insert-into-tx-lookup
["INSERT INTO temp.tx_lookup_after ["INSERT INTO temp.tx_lookup_after
SELECT t.e0, t.a0, t.v0, t.tx0, t.added0 + 2, t.value_type_tag0, t.index_avet0, t.index_vaet0, t.index_fulltext0, t.unique_value0, t.sv, t.svalue_type_tag, d.rowid, d.e, d.a, d.v, d.tx, d.value_type_tag SELECT t.e0, t.a0, t.v0, t.tx0, t.added0, t.value_type_tag0, t.index_avet0, t.index_vaet0, t.index_fulltext0, t.unique_value0, t.sv, t.svalue_type_tag, d.rowid, d.e, d.a, d.v, d.tx, d.value_type_tag
FROM temp.tx_lookup_before AS t FROM temp.tx_lookup_before AS t
LEFT JOIN datoms AS d LEFT JOIN datoms AS d
ON t.e0 = d.e AND ON t.e0 = d.e AND
@ -439,7 +439,7 @@
t.sv IS NOT NULL t.sv IS NOT NULL
UNION ALL UNION ALL
SELECT t.e0, t.a0, t.v0, t.tx0, t.added0 + 2, t.value_type_tag0, t.index_avet0, t.index_vaet0, t.index_fulltext0, t.unique_value0, t.sv, t.svalue_type_tag, d.rowid, d.e, d.a, d.v, d.tx, d.value_type_tag SELECT t.e0, t.a0, t.v0, t.tx0, t.added0, t.value_type_tag0, t.index_avet0, t.index_vaet0, t.index_fulltext0, t.unique_value0, t.sv, t.svalue_type_tag, d.rowid, d.e, d.a, d.v, d.tx, d.value_type_tag
FROM temp.tx_lookup_before AS t, FROM temp.tx_lookup_before AS t,
datoms AS d datoms AS d
WHERE t.sv IS NULL AND WHERE t.sv IS NULL AND
@ -450,13 +450,13 @@
["INSERT INTO transactions (e, a, v, tx, added, value_type_tag) ["INSERT INTO transactions (e, a, v, tx, added, value_type_tag)
SELECT e0, a0, v0, ?, 1, value_type_tag0 SELECT e0, a0, v0, ?, 1, value_type_tag0
FROM temp.tx_lookup_after FROM temp.tx_lookup_after
WHERE added0 IS 3 AND e IS NULL" tx] ;; TODO: get rid of magic value 3. XXX WHERE added0 IS 1 AND e IS NULL" tx]
t-retract-datoms-carefully t-retract-datoms-carefully
["INSERT INTO transactions (e, a, v, tx, added, value_type_tag) ["INSERT INTO transactions (e, a, v, tx, added, value_type_tag)
SELECT e, a, v, ?, 0, value_type_tag SELECT e, a, v, ?, 0, value_type_tag
FROM temp.tx_lookup_after FROM temp.tx_lookup_after
WHERE added0 IS 2 AND ((sv IS NOT NULL) OR (sv IS NULL AND v0 IS NOT v)) AND v IS NOT NULL" tx] ;; TODO: get rid of magic value 2. XXX WHERE added0 IS 0 AND ((sv IS NOT NULL) OR (sv IS NULL AND v0 IS NOT v)) AND v IS NOT NULL" tx]
] ]
(go-pair (go-pair
(doseq [q [build-indices insert-into-tx-lookup (doseq [q [build-indices insert-into-tx-lookup
@ -470,12 +470,12 @@
SELECT e0, a0, v0, ?, value_type_tag0, SELECT e0, a0, v0, ?, value_type_tag0,
index_avet0, index_vaet0, index_fulltext0, unique_value0 index_avet0, index_vaet0, index_fulltext0, unique_value0
FROM temp.tx_lookup_after FROM temp.tx_lookup_after
WHERE added0 IS 3 AND e IS NULL" tx] ;; TODO: get rid of magic value 3. XXX WHERE added0 IS 1 AND e IS NULL" tx]
;; TODO: retract fulltext datoms correctly. ;; TODO: retract fulltext datoms correctly.
d-retract-datoms-carefully d-retract-datoms-carefully
["WITH ids AS (SELECT l.rid FROM temp.tx_lookup_after AS l WHERE l.added0 IS 2 AND ((l.sv IS NOT NULL) OR (l.sv IS NULL AND l.v0 IS NOT l.v))) ["WITH ids AS (SELECT l.rid FROM temp.tx_lookup_after AS l WHERE l.added0 IS 0 AND ((l.sv IS NOT NULL) OR (l.sv IS NULL AND l.v0 IS NOT l.v)))
DELETE FROM datoms WHERE rowid IN ids" ;; TODO: get rid of magic value 2. XXX DELETE FROM datoms WHERE rowid IN ids"
]] ]]
(-run-queries conn [d-datoms-not-already-present d-retract-datoms-carefully] (-run-queries conn [d-datoms-not-already-present d-retract-datoms-carefully]
"Transaction violates unique constraint"))) "Transaction violates unique constraint")))