Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A few improvements #536

Merged
merged 35 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
ef9f322
combine query-filter xf and flake xf for efficiency
zonotope Jun 18, 2023
f7622ce
remove deprecated :block parameter
zonotope Jun 19, 2023
cb5e9a6
Merge remote-tracking branch 'origin/main' into fix/delete-all-matches
zonotope Jul 18, 2023
02ee48d
remove unused fns
zonotope Jun 22, 2023
16a066e
use subrange fn defined in flake ns
zonotope Jun 22, 2023
b1971b9
remove unused fns and references to network attribute
zonotope Jun 22, 2023
d88f4b1
ledger-id -> ledger-alias
zonotope Jun 22, 2023
7fb543e
remove references to network/ledger-id in storage
zonotope Jun 22, 2023
5a06b27
fluree.db.storage.core -> fluree.db.storage
zonotope Jun 22, 2023
32739b1
fix recursive invocation arity
zonotope Jun 26, 2023
33dd70e
remove persistent-sorted-set dependency
zonotope Jul 18, 2023
0323a1a
remove unused functions
zonotope Jun 27, 2023
6428114
remove unused namespace
zonotope Jun 28, 2023
f3b397d
fix typo
zonotope Jun 28, 2023
95f9a05
use pre-existing util/sequential fn
zonotope Jul 12, 2023
ce880df
resolve empty branches too
zonotope Jul 12, 2023
a8883fa
don't resolve empty nodes unless necessary
zonotope Jul 14, 2023
ac3d736
use sorted map for branch children again
zonotope Jul 18, 2023
366784b
remove include? argument to tree-chan in favor of a filtering xf
zonotope Jul 18, 2023
067de5d
add min/max values for flake components
zonotope Jul 19, 2023
d9bcd02
ensure child maps are *always* avl maps
zonotope Jul 21, 2023
90582b2
use transients to add/remove flakes to novelty sets for perfermance
zonotope Jul 21, 2023
d6a1f7d
cleanup ns requires
zonotope Jul 21, 2023
68407a1
add minimum flake for consistency with max; add nearest ss fn
zonotope Jul 21, 2023
8366750
add start/end flakes to tree-chan; trim resolved nodes based on them
zonotope Jul 21, 2023
969638e
rely on tree-chan to trim resolved nodes instead of with an xf
zonotope Jul 21, 2023
e8b737f
bump up reindexing parallelism because why not?
zonotope Jul 21, 2023
6de6c2b
add/update docstrings
zonotope Jul 21, 2023
7f5e626
remove unused fns
zonotope Jul 21, 2023
479215e
Merge remote-tracking branch 'origin/main' into fix/delete-all-matches
zonotope Jul 21, 2023
a0900ed
Merge remote-tracking branch 'origin/main' into fix/delete-all-matches
zonotope Jul 24, 2023
1b38569
use loops instead of reduce for performance
zonotope Jul 24, 2023
a589708
use 1 for buffersize
zonotope Jul 24, 2023
6ef7eaa
correct indentation
zonotope Jul 24, 2023
d98579f
change function def to please linter
zonotope Jul 24, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion dev/json_ld/shacl.clj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
[fluree.db.util.log :as log]
[fluree.db.index :as index]
[criterium.core :as criterium]
[clojure.data.avl :as avl]
[clojure.tools.reader.edn :as edn]))


Expand Down
2 changes: 1 addition & 1 deletion dev/user.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
[fluree.db.flake :as flake]
[fluree.db.util.json :as json]
[fluree.db.serde.json :as serdejson]
[fluree.db.storage.core :as storage]
[fluree.db.storage :as storage]
[fluree.db.query.fql :as fql]
[fluree.db.query.range :as query-range]
[fluree.db.dbproto :as dbproto]
Expand Down
4 changes: 2 additions & 2 deletions src/fluree/db/conn/file.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
[fluree.db.conn.cache :as conn-cache]
[fluree.db.conn.state-machine :as state-machine]
[fluree.db.util.log :as log :include-macros true]
[fluree.db.storage.core :as storage]
[fluree.db.storage :as storage]
[fluree.db.indexer.default :as idx-default]
[fluree.db.serde.json :refer [json-serde]]
#?@(:cljs [["fs" :as fs]
Expand Down Expand Up @@ -255,7 +255,7 @@
[conn {:keys [id leaf tempid] :as node}]
(let [cache-key [::resolve id tempid]]
(if (= :empty id)
(storage/resolve-empty-leaf node)
(storage/resolve-empty-node node)
(conn-cache/lru-lookup
lru-cache-atom
cache-key
Expand Down
4 changes: 2 additions & 2 deletions src/fluree/db/conn/ipfs.cljc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(ns fluree.db.conn.ipfs
(:require [fluree.db.storage.core :as storage]
(:require [fluree.db.storage :as storage]
[fluree.db.index :as index]
[fluree.db.util.context :as ctx-util]
[fluree.db.util.core :as util :refer [exception?]]
Expand Down Expand Up @@ -131,7 +131,7 @@
[conn {:keys [id leaf tempid] :as node}]
(let [cache-key [::resolve id tempid]]
(if (= :empty id)
(storage/resolve-empty-leaf node)
(storage/resolve-empty-node node)
(conn-cache/lru-lookup
lru-cache-atom
cache-key
Expand Down
4 changes: 2 additions & 2 deletions src/fluree/db/conn/memory.cljc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(ns fluree.db.conn.memory
(:require [clojure.core.async :as async :refer [go]]
[fluree.db.storage.core :as storage]
[fluree.db.storage :as storage]
[fluree.db.index :as index]
[fluree.db.util.context :as ctx-util]
[fluree.db.util.core :as util]
Expand Down Expand Up @@ -163,7 +163,7 @@
[_ node]
;; all root index nodes will be empty

(storage/resolve-empty-leaf node))
(storage/resolve-empty-node node))

#?@(:clj
[full-text/IndexConnection
Expand Down
4 changes: 2 additions & 2 deletions src/fluree/db/conn/s3.clj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
[fluree.db.indexer.default :as idx-default]
[fluree.db.ledger.proto :as ledger-proto]
[fluree.db.serde.json :refer [json-serde]]
[fluree.db.storage.core :as storage]
[fluree.db.storage :as storage]
[fluree.db.util.context :as ctx-util]
[fluree.db.util.json :as json]
[fluree.db.util.log :as log]
Expand Down Expand Up @@ -224,7 +224,7 @@
(resolve [conn {:keys [id leaf tempid] :as node}]
(let [cache-key [::resolve id tempid]]
(if (= :empty id)
(storage/resolve-empty-leaf node)
(storage/resolve-empty-node node)
(conn-cache/lru-lookup lru-cache-atom cache-key
(fn [_]
(storage/resolve-index-node
Expand Down
12 changes: 6 additions & 6 deletions src/fluree/db/db/json_ld.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -313,11 +313,11 @@
opst-cmp :opst
tspo-cmp :tspo} index/default-comparators

spot (index/empty-branch method alias spot-cmp)
psot (index/empty-branch method alias psot-cmp)
post (index/empty-branch method alias post-cmp)
opst (index/empty-branch method alias opst-cmp)
tspo (index/empty-branch method alias tspo-cmp)
spot (index/empty-branch alias spot-cmp)
psot (index/empty-branch alias psot-cmp)
post (index/empty-branch alias post-cmp)
opst (index/empty-branch alias opst-cmp)
tspo (index/empty-branch alias tspo-cmp)
stats {:flakes 0, :size 0, :indexed 0}
schema (vocab/base-schema)
branch (branch/branch-meta ledger)
Expand All @@ -327,7 +327,7 @@
(map->JsonLdDb {:ledger ledger
:conn conn
:method method
:alias alias
:ledger-alias alias
:branch (:name branch)
:commit (:commit branch)
:t 0
Expand Down
124 changes: 36 additions & 88 deletions src/fluree/db/flake.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,18 @@
(def ^:const MIN-PREDICATE-ID (min-subject-id const/$_predicate))
(def ^:const MAX-PREDICATE-ID (max-subject-id const/$_predicate))

(def ^:const min-s util/max-long)
(def ^:const max-s util/min-long)
(def ^:const min-p 0)
(def ^:const max-p MAX-PREDICATE-ID)
(def ^:const min-dt util/min-integer)
(def ^:const max-dt util/max-integer)
(def ^:const min-t 0)
(def ^:const max-t util/min-long)
(def ^:const min-op false)
(def ^:const max-op true)
(def ^:const min-meta util/min-integer)
(def ^:const max-meta util/max-integer)

(defn sid->cid
"Will return a collection id from a subject-id."
Expand Down Expand Up @@ -228,8 +240,10 @@
[(s flake) (p flake) (o flake) (dt flake) (t flake) (op flake) (m flake)])

(def maximum
"The largest flake possible"
(->Flake util/max-long 0 util/max-long const/$xsd:decimal 0 true nil))
(->Flake max-s max-p max-s max-dt max-t max-op max-meta))

(def minimum
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a docstring of "The smallest flake possible"?

(->Flake min-s min-p min-s min-dt min-t min-op min-meta))

(defn- assoc-flake
"Assoc for Flakes"
Expand Down Expand Up @@ -417,28 +431,6 @@
(cmp-bool (op f1) (op f2))
(cmp-meta (m f1) (m f2))))


(defn cmp-flakes-history
"Note this is not suitable for a set, only a vector/list."
[f1 f2]
(combine-cmp
(cmp-long (t f1) (t f2))
#?(:clj (Boolean/compare (op f2) (op f1))
:cljs (compare (op f2) (op f1)))))


(defn cmp-history-quick-reverse-sort
"Sorts by transaction time in ascending order (newest first), then by
the boolean operation descending so assertions (true) come before retractions (false)
so that we can 're-play' the log in reverse order to come up with historical states.
Suitable only for sorting a vector, not a sorted set."
[f1 f2]
(combine-cmp
(cmp-long (t f1) (t f2))
#?(:clj (Boolean/compare (op f2) (op f1))
:cljs (compare (op f2) (op f1)))))


(defn flip-flake
"Takes a flake and returns one with the provided block and op flipped from true/false.
Don't over-ride no-history, even if no-history for this predicate has changed. New inserts
Expand All @@ -448,56 +440,22 @@
([flake t]
(->Flake (s flake) (p flake) (o flake) (dt flake) t (not (op flake)) (m flake))))

(defn slice
"From and to are Flakes"
[ss from to]
(cond
(and from to) (avl/subrange ss >= from <= to)
(nil? from) (avl/subrange ss <= to)
(nil? to) (avl/subrange ss >= from)
:else (throw (ex-info "Unexpected error performing slice, both from and to conditions are nil. Please report."
{:status 500
:error :db/unexpected-error}))))

(defn match-spot
"Returns all matching flakes to a specific subject, and optionaly also a predicate if provided
Must be provided with subject/predicate integer ids, no lookups are performed."
[ss sid pid]
(if pid
(avl/subrange ss >= (->Flake sid pid nil -1 nil nil nil)
<= (->Flake sid (inc pid) nil util/max-long nil nil nil))
(avl/subrange ss > (->Flake (inc sid) MAX-COLL-SUBJECTS nil nil nil nil nil)
< (->Flake (dec sid) -1 nil nil nil nil nil))))


(defn match-post
"Returns all matching flakes to a predicate + object match."
[ss pid o dt]
(avl/subrange ss
>= (->Flake util/max-long pid o dt nil nil nil)
<= (->Flake 0 pid o dt nil nil nil)))

(defn match-tspo
"Returns all matching flakes to a specific 't' value."
[ss t]
(avl/subrange ss
>= (->Flake util/max-long nil nil nil t nil nil)
<= (->Flake util/min-long nil nil nil t nil nil)))

(defn lookup
[ss start-flake end-flake]
(avl/subrange ss >= start-flake <= end-flake))

(defn subrange
([ss test flake]
(avl/subrange ss test flake))
([ss start-test start-flake end-test end-flake]
(avl/subrange ss start-test start-flake end-test end-flake)))


(defn split-at
[n ss]
(avl/split-at n ss))
(defn nearest
[ss test f]
(avl/nearest ss test f))

(defn lower-than-all?
[f ss]
Expand All @@ -511,14 +469,6 @@
(and (nil? e)
(empty? upper))))

(defn split-by-flake
"Splits a sorted set at a given flake. If there is an exact match for flake,
puts it in the left-side. Primarily for use with last-flake."
[f ss]
(let [[l e r] (avl/split-key f ss)]
[(if e (conj l e) l) r]))


(defn sorted-set-by
[comparator & flakes]
(apply avl/sorted-set-by comparator flakes))
Expand Down Expand Up @@ -549,6 +499,23 @@
[ss to-remove]
(transient-reduce disj! ss to-remove))

(defn revise
"Changes the composition of the sorted set `ss` by adding all the flakes in the
`to-add` collection and removing all flakes in the `to-remove` collection."
[ss to-add to-remove]
(let [trans (transient ss)
removed (loop [[f & r] to-remove
t-set trans]
(if f
(recur r (disj! t-set f))
t-set))
added (loop [[f & r] to-add
t-set removed]
(if f
(recur r (conj! t-set f))
t-set))]
(persistent! added)))

(defn assoc-all
[sm entries]
(transient-reduce (fn [m [k v]]
Expand Down Expand Up @@ -607,22 +574,3 @@
"Returns approx number of bytes in a collection of flakes."
[flakes]
(reduce (fn [size f] (+ size (size-flake f))) 0 flakes))


(defn size-kb
"Like size-bytes, but kb.
Rounds down for simplicity, as bytes is just an estimate anyhow."
[flakes]
(-> (size-bytes flakes)
(/ 1000)
(double)
(Math/round)))


(defn take
"Takes n flakes from a sorted flake set, retaining the set itself."
[n flake-set]
(if (>= n (count flake-set))
flake-set
(let [k (nth flake-set n)]
(first (avl/split-key k flake-set)))))
Loading