Skip to content

Commit 9f0a039

Browse files
committed
[WIP] Switch to Internal Resource Identifier in the Index
1 parent a99052c commit 9f0a039

File tree

96 files changed

+1685
-1696
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

96 files changed

+1685
-1696
lines changed

docs/implementation/database.md

Lines changed: 68 additions & 44 deletions
Large diffs are not rendered by default.

docs/performance/fhir-search.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ The result is a dataset which consists only of the resource types Patient, Obser
9797

9898
## Controlling and Monitoring the Caches
9999

100-
The size of the resource cache and the resource handle cache can be set by their respective environment variables `DB_RESOURCE_CACHE_SIZE` and `DB_RESOURCE_HANDLE_CACHE_SIZE`. The size denotes the number of resources / resource handles. Because one has to specify a number of resources / resource handles, it's important to know how many bytes a resource / resource handle allocates on the heap. For resource handles, it can be said that they allocate between 272 and 328 bytes depending on the size of the resource id. For resources, the size varies widely. Monitoring of the heap usage is critical.
100+
The size of the resource cache and the resource handle cache can be set by their respective environment variables `DB_RESOURCE_CACHE_SIZE` and `DB_RESOURCE_HANDLE_CACHE_SIZE`. The size denotes the number of resources / resource handles. Because one has to specify a number of resources / resource handles, it's important to know how many bytes a resource / resource handle allocates on the heap. For resource handles, it can be said that they allocate between 152 and 208 bytes depending on the size of the resource id. For resources, the size varies widely. Monitoring of the heap usage is critical.
101101

102102
### Monitoring
103103

modules/byte-buffer/src/blaze/byte_buffer.clj

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@
7878
(.putInt ^ByteBuffer byte-buffer x))
7979

8080

81+
(defn put-5-byte-long!
82+
[byte-buffer ^long x]
83+
(put-byte! byte-buffer (bit-shift-right (unchecked-long x) 32))
84+
(put-int! byte-buffer x))
85+
86+
8187
(defn put-long!
8288
{:inline
8389
(fn [byte-buffer x]
@@ -223,6 +229,11 @@
223229
(.getInt ^ByteBuffer byte-buffer))
224230

225231

232+
(defn get-5-byte-long! [byte-buffer]
233+
(+ (bit-shift-left (bit-and (get-byte! byte-buffer) 0xFF) 32)
234+
(bit-and (get-int! byte-buffer) 0xFFFFFFFF)))
235+
236+
226237
(defn get-long!
227238
{:inline
228239
(fn [byte-buffer]
@@ -246,24 +257,6 @@
246257
(.get ^ByteBuffer byte-buffer ^bytes byte-array offset length)))
247258

248259

249-
(defn size-up-to-null [byte-buffer]
250-
(when (pos? (remaining byte-buffer))
251-
(mark! byte-buffer)
252-
(loop [byte (bit-and (long (get-byte! byte-buffer)) 0xFF)
253-
size 0]
254-
(cond
255-
(zero? byte)
256-
(do (reset! byte-buffer)
257-
size)
258-
259-
(pos? (remaining byte-buffer))
260-
(recur (bit-and (long (get-byte! byte-buffer)) 0xFF) (inc size))
261-
262-
:else
263-
(do (reset! byte-buffer)
264-
nil)))))
265-
266-
267260
(defn mismatch
268261
"Finds and returns the relative index of the first mismatch between `a` and
269262
`b`.

modules/byte-buffer/test/blaze/byte_buffer_test.clj

Lines changed: 17 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[blaze.byte-buffer :as bb]
44
[blaze.test-util :as tu :refer [satisfies-prop]]
55
[clojure.spec.test.alpha :as st]
6-
[clojure.test :as test :refer [deftest is testing]]
6+
[clojure.test :as test :refer [are deftest]]
77
[clojure.test.check.generators :as gen]
88
[clojure.test.check.properties :as prop]))
99

@@ -20,41 +20,19 @@
2020
(= capacity (bb/limit (bb/allocate capacity))))))
2121

2222

23-
(deftest size-up-to-null-test
24-
(testing "empty buffer"
25-
(let [buf (bb/allocate 0)]
26-
(is (nil? (bb/size-up-to-null buf)))))
27-
28-
(testing "buffer with only one null byte"
29-
(let [buf (bb/allocate 1)]
30-
(bb/put-byte! buf 0)
31-
(bb/flip! buf)
32-
(is (zero? (bb/size-up-to-null buf)))))
33-
34-
(testing "buffer with only one non-null byte"
35-
(let [buf (bb/allocate 1)]
36-
(bb/put-byte! buf 1)
37-
(bb/flip! buf)
38-
(is (nil? (bb/size-up-to-null buf)))))
39-
40-
(testing "buffer with one non-null and one null byte"
41-
(let [buf (bb/allocate 2)]
42-
(bb/put-byte! buf 1)
43-
(bb/put-byte! buf 0)
44-
(bb/flip! buf)
45-
(is (= 1 (bb/size-up-to-null buf)))))
46-
47-
(testing "buffer with two null bytes"
48-
(let [buf (bb/allocate 2)]
49-
(bb/put-byte! buf 0)
50-
(bb/put-byte! buf 0)
51-
(bb/flip! buf)
52-
(is (zero? (bb/size-up-to-null buf)))))
53-
54-
(testing "buffer with two non-null and one null byte"
55-
(let [buf (bb/allocate 3)]
56-
(bb/put-byte! buf 1)
57-
(bb/put-byte! buf 2)
58-
(bb/put-byte! buf 0)
59-
(bb/flip! buf)
60-
(is (= 2 (bb/size-up-to-null buf))))))
23+
(defn- transcode-5-byte-long [x]
24+
(let [buf (bb/allocate 5)]
25+
(bb/put-5-byte-long! buf x)
26+
(bb/flip! buf)
27+
(= x (bb/get-5-byte-long! buf))))
28+
29+
30+
(deftest transcode-5-byte-long-test
31+
(are [x] (transcode-5-byte-long x)
32+
0
33+
0xFFFFFFFF
34+
0xFFFFFFFFFF)
35+
36+
(satisfies-prop 100000
37+
(prop/for-all [x (gen/choose 0 0xFFFFFFFFFF)]
38+
(transcode-5-byte-long x))))

modules/db-protocols/src/blaze/db/impl/protocols.clj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,17 @@
7171
(-compile-value [search-param modifier value] "Can return an anomaly.")
7272
(-resource-handles
7373
[search-param context tid modifier compiled-value]
74-
[search-param context tid modifier compiled-value start-id]
74+
[search-param context tid modifier compiled-value start-did]
7575
"Returns a reducible collection.")
7676
(-sorted-resource-handles
7777
[search-param context tid direction]
78-
[search-param context tid direction start-id]
78+
[search-param context tid direction start-did]
7979
"Returns a reducible collection.")
8080
(-compartment-keys [search-param context compartment tid compiled-value])
8181
(-matches? [search-param context resource-handle modifier compiled-values])
8282
(-compartment-ids [_ resolver resource])
83-
(-index-values [_ resolver resource])
84-
(-index-value-compiler [_]))
83+
(-index-values [_ resource-id resolver resource])
84+
(-index-value-compiler [_ resource-id]))
8585

8686

8787
(defprotocol Pull

modules/db-stub/src/blaze/db/api_stub.clj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
:tx-success-index {:reverse-comparator? true}
5959
:tx-error-index nil
6060
:t-by-instant-index {:reverse-comparator? true}
61+
:resource-id-index nil
6162
:resource-as-of-index nil
6263
:type-as-of-index nil
6364
:system-as-of-index nil

modules/db-tx-log/src/blaze/db/tx_log/spec.clj

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
[blaze.db.tx-log :as tx-log]
55
[blaze.fhir.spec]
66
[blaze.spec]
7-
[clojure.spec.alpha :as s]))
7+
[clojure.spec.alpha :as s]
8+
[clojure.spec.gen.alpha :as gen]))
89

910

1011
(defn tx-log? [x]
@@ -35,8 +36,20 @@
3536
(s/coll-of :blaze.fhir/local-ref-tuple))
3637

3738

39+
(def ^:private ^:const ^long max-t 0xFFFFFFFFFF)
40+
41+
42+
;; With a 5 byte long `t`, we can create one transaction each millisecond
43+
;; for 34 years. Alone the t-values would need a storage of 5 TB.
44+
(comment
45+
(let [millis-per-year (* 1000 3600 24 365)
46+
five-bytes (long (Math/pow 2 40))]
47+
(double (/ five-bytes millis-per-year))))
48+
49+
50+
;; The point in time `t` of a database value.
3851
(s/def :blaze.db/t
39-
(s/and int? #(<= 0 % 0xFFFFFFFFFFFFFF)))
52+
(s/with-gen (s/and int? #(<= 0 % max-t)) #(gen/choose 0 max-t)))
4053

4154

4255
(s/def :blaze.db.tx-cmd/if-none-exist

modules/db-tx-log/test/blaze/db/tx_log/spec_test.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
(are [x] (s/valid? :blaze.db/t x)
2121
0
2222
1
23-
0xFFFFFFFFFFFFFF))
23+
0xFFFFFFFFFF))
2424

2525

2626
(def patient-hash-0 (hash/generate {:fhir/type :fhir/Patient :id "0"}))

modules/db/NOTES.md

Lines changed: 0 additions & 125 deletions
This file was deleted.

0 commit comments

Comments
 (0)