Skip to content

Commit cf6185c

Browse files
committed
[WIP] Switch to Internal Resource Identifier in the Index
1 parent 8bf8145 commit cf6185c

File tree

97 files changed

+1751
-1849
lines changed

Some content is hidden

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

97 files changed

+1751
-1849
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]
@@ -220,6 +226,11 @@
220226
(.getInt ^ByteBuffer byte-buffer))
221227

222228

229+
(defn get-5-byte-long! [byte-buffer]
230+
(+ (bit-shift-left (bit-and (get-byte! byte-buffer) 0xFF) 32)
231+
(bit-and (get-int! byte-buffer) 0xFFFFFFFF)))
232+
233+
223234
(defn get-long!
224235
{:inline
225236
(fn [byte-buffer]
@@ -243,24 +254,6 @@
243254
(.get ^ByteBuffer byte-buffer ^bytes byte-array offset length)))
244255

245256

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

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

Lines changed: 14 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[blaze.byte-buffer :as bb]
44
[blaze.test-util :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

@@ -26,41 +26,19 @@
2626
(= capacity (bb/limit (bb/allocate capacity))))))
2727

2828

29-
(deftest size-up-to-null-test
30-
(testing "empty buffer"
31-
(let [buf (bb/allocate 0)]
32-
(is (nil? (bb/size-up-to-null buf)))))
29+
(defn- transcode-5-byte-long [x]
30+
(let [buf (bb/allocate 5)]
31+
(bb/put-5-byte-long! buf x)
32+
(bb/flip! buf)
33+
(= x (bb/get-5-byte-long! buf))))
3334

34-
(testing "buffer with only one null byte"
35-
(let [buf (bb/allocate 1)]
36-
(bb/put-byte! buf 0)
37-
(bb/flip! buf)
38-
(is (zero? (bb/size-up-to-null buf)))))
3935

40-
(testing "buffer with only one non-null byte"
41-
(let [buf (bb/allocate 1)]
42-
(bb/put-byte! buf 1)
43-
(bb/flip! buf)
44-
(is (nil? (bb/size-up-to-null buf)))))
36+
(deftest transcode-5-byte-long-test
37+
(are [x] (transcode-5-byte-long x)
38+
0
39+
0xFFFFFFFF
40+
0xFFFFFFFFFF)
4541

46-
(testing "buffer with one non-null and one null byte"
47-
(let [buf (bb/allocate 2)]
48-
(bb/put-byte! buf 1)
49-
(bb/put-byte! buf 0)
50-
(bb/flip! buf)
51-
(is (= 1 (bb/size-up-to-null buf)))))
52-
53-
(testing "buffer with two null bytes"
54-
(let [buf (bb/allocate 2)]
55-
(bb/put-byte! buf 0)
56-
(bb/put-byte! buf 0)
57-
(bb/flip! buf)
58-
(is (zero? (bb/size-up-to-null buf)))))
59-
60-
(testing "buffer with two non-null and one null byte"
61-
(let [buf (bb/allocate 3)]
62-
(bb/put-byte! buf 1)
63-
(bb/put-byte! buf 2)
64-
(bb/put-byte! buf 0)
65-
(bb/flip! buf)
66-
(is (= 2 (bb/size-up-to-null buf))))))
42+
(satisfies-prop 100000
43+
(prop/for-all [x (gen/choose 0 0xFFFFFFFFFF)]
44+
(transcode-5-byte-long x))))

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@
7676
(-compartment-keys [search-param context compartment tid compiled-value])
7777
(-matches? [search-param context resource-handle modifier compiled-values])
7878
(-compartment-ids [_ resolver resource])
79-
(-index-values [_ resolver resource])
80-
(-index-value-compiler [_]))
79+
(-index-values [_ resource-id resolver resource])
80+
(-index-value-compiler [_ resource-id]))
8181

8282

8383
(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/deps.edn

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@
3131
{cloverage/cloverage
3232
{:mvn/version "1.2.4"}}
3333

34-
:main-opts ["-m" "cloverage.coverage" "--codecov" "-p" "src" "-s" "test"]}}}
34+
:main-opts ["-m" "cloverage.coverage" "--codecov" "-p" "src" "-s" "test"
35+
"-e" ".+spec"]}}}

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
(s/def :blaze.db/tx-log
@@ -27,8 +28,20 @@
2728
(s/coll-of :blaze.fhir/local-ref-tuple))
2829

2930

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

3346

3447
(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
@@ -27,7 +27,7 @@
2727
(are [x] (s/valid? :blaze.db/t x)
2828
0
2929
1
30-
0xFFFFFFFFFFFFFF))
30+
0xFFFFFFFFFF))
3131

3232

3333
(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)