Skip to content

Commit 572d6f4

Browse files
committed
perf: no need to transact property values to ui db
1 parent da74228 commit 572d6f4

File tree

7 files changed

+128
-148
lines changed

7 files changed

+128
-148
lines changed

deps/db/src/logseq/db/frontend/view.cljs

Lines changed: 52 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -113,17 +113,17 @@
113113
(sort-by-multiple-properties db sorting rows))))
114114

115115
(defn get-property-value-content
116-
[db entity]
117-
(when entity
116+
[db value]
117+
(when value
118118
(cond
119-
(uuid? entity)
120-
(db-property/property-value-content (d/entity db [:block/uuid entity]))
121-
(de/entity? entity)
122-
(db-property/property-value-content entity)
123-
(keyword? entity)
124-
(str entity)
119+
(uuid? value)
120+
(db-property/property-value-content (d/entity db [:block/uuid value]))
121+
(de/entity? value)
122+
(db-property/property-value-content value)
123+
(keyword? value)
124+
(str value)
125125
:else
126-
entity)))
126+
value)))
127127

128128
(defn- ^:large-vars/cleanup-todo row-matched?
129129
[db row filters input]
@@ -379,27 +379,49 @@
379379
(get-entities db view feat-type index-attr view-for-id)))
380380

381381
(defn get-property-values
382-
[db view-id property-ident]
383-
(let [entities-result (get-view-entities db view-id)
384-
entities (if (map? entities-result)
385-
(:ref-blocks entities-result)
386-
entities-result)
387-
values (->> (mapcat (fn [entity]
388-
(let [v (get entity property-ident)]
389-
(if (set? v) v #{v})))
390-
entities)
391-
(remove nil?))]
392-
(->>
393-
(keep (fn [e]
394-
(let [label (get-property-value-content db e)]
395-
(when-not (string/blank? (str label))
396-
{:label (str label)
397-
:value (if (de/entity? e)
398-
(select-keys e [:db/id :block/uuid])
399-
e)})))
400-
values)
401-
(common-util/distinct-by :label)
402-
(sort-by :label))))
382+
[db property-ident {:keys [view-id]}]
383+
(let [property (d/entity db property-ident)
384+
default-value (:logseq.property/default-value property)
385+
empty-id (:db/id (d/entity db :logseq.property/empty-placeholder))
386+
ref-type? (= :db.type/ref (:db/valueType property))
387+
values (if view-id
388+
(let [entities-result (get-view-entities db view-id)
389+
entities (if (map? entities-result)
390+
(:ref-blocks entities-result)
391+
entities-result)]
392+
(->> (mapcat (fn [entity]
393+
(let [v (get entity property-ident)]
394+
(if (set? v) v #{v})))
395+
entities)
396+
(remove nil?)
397+
(keep (fn [e]
398+
(when-let [label (get-property-value-content db e)]
399+
(when-not (or (string/blank? (str label))
400+
(= empty-id (:db/id e)))
401+
{:label (str label)
402+
:value (if (de/entity? e)
403+
(select-keys e [:db/id :block/uuid])
404+
e)}))))
405+
(common-util/distinct-by :label)))
406+
;; get all values
407+
(->> (d/datoms db :avet property-ident)
408+
(map (fn [d]
409+
(:v d)))
410+
distinct
411+
(map (fn [v]
412+
(let [e (when ref-type? (d/entity db v))
413+
[label value] (if ref-type?
414+
[(db-property/property-value-content e)
415+
(select-keys e [:db/id :block/uuid])]
416+
[(str v) v])]
417+
{:label label
418+
:value value})))
419+
(common-util/distinct-by :label)))]
420+
(if default-value
421+
(cons {:label (get-property-value-content db default-value)
422+
:value (select-keys default-value [:db/id :block/uuid])}
423+
values)
424+
values)))
403425

404426
(defn get-view-data
405427
[db view-id {:keys [journals? _view-for-id view-feature-type input]

src/main/frontend/components/property/config.cljs

Lines changed: 36 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -363,27 +363,19 @@
363363

364364
(rum/defc add-existing-values
365365
[property values {:keys [toggle-fn]}]
366-
(let [uuid-values? (every? uuid? values)
367-
values' (if uuid-values?
368-
(let [values' (map #(db/entity [:block/uuid %]) values)]
369-
(->> values'
370-
(util/distinct-by db-property/closed-value-content)
371-
(map :block/uuid)))
372-
values)]
373-
[:div.flex.flex-col.gap-1.w-64.p-4.overflow-y-auto
374-
{:class "max-h-[50dvh]"}
375-
[:div "Existing values:"]
376-
[:ol
377-
(for [value values']
378-
[:li (if (uuid? value)
379-
(let [result (db/entity [:block/uuid value])]
380-
(db-property/closed-value-content result))
381-
(str value))])]
382-
(shui/button
383-
{:on-click (fn []
384-
(p/let [_ (db-property-handler/add-existing-values-to-closed-values! (:db/id property) values')]
385-
(toggle-fn)))}
386-
"Add choices")]))
366+
[:div.flex.flex-col.gap-1.w-64.p-4.overflow-y-auto
367+
{:class "max-h-[50dvh]"}
368+
[:div "Existing values:"]
369+
[:ol
370+
(for [value values]
371+
[:li (:label value)])]
372+
(shui/button
373+
{:on-click (fn []
374+
(p/let [_ (db-property-handler/add-existing-values-to-closed-values! (:db/id property)
375+
(map (fn [{:keys [value]}]
376+
(:block/uuid value)) values))]
377+
(toggle-fn)))}
378+
"Add choices")])
387379

388380
(rum/defc choices-sub-pane < rum/reactive db-mixins/query
389381
[property {:keys [disabled?] :as opts}]
@@ -431,29 +423,29 @@
431423
{:icon :plus :title "Add choice"
432424
:item-props {:on-click
433425
(fn [^js e]
434-
(p/let [values (db-async/<get-block-property-values (state/get-current-repo) (:db/ident property))
426+
(p/let [values (db-async/<get-property-values (:db/ident property) {})
435427
existing-values (seq (:property/closed-values property))
436-
values (if (seq existing-values)
437-
(let [existing-ids (set (map :db/id existing-values))]
438-
(remove (fn [id] (existing-ids id)) values))
439-
values)]
440-
(shui/popup-show! (.-target e)
441-
(fn [{:keys [id]}]
442-
(let [opts {:toggle-fn (fn [] (shui/popup-hide! id))}
443-
values' (->> (if (contains? db-property-type/all-ref-property-types (:logseq.property/type property))
444-
(->> values
445-
(map db/entity)
446-
(remove (fn [e]
447-
(let [value (db-property/property-value-content e)]
448-
(and (string? value) (string/blank? value)))))
449-
(map :block/uuid))
450-
(remove string/blank? values))
451-
distinct)]
452-
(if (seq values')
453-
(add-existing-values property values' opts)
454-
(choice-base-edit-form property {:create? true}))))
455-
{:id :ls-base-edit-form
456-
:align "start"})))}}))]))
428+
values' (if (seq existing-values)
429+
(let [existing-ids (set (map :db/id existing-values))
430+
existing-titles (set (map db-property/property-value-content existing-values))]
431+
(remove (fn [{:keys [label value]}]
432+
(or (existing-ids (:db/id value))
433+
(existing-titles label)
434+
(string/blank? label))) values))
435+
(remove (fn [{:keys [label _value]}]
436+
(string/blank? label))
437+
values))]
438+
(p/do!
439+
(when (seq values')
440+
(db-async/<get-blocks (state/get-current-repo) (map (fn [{:keys [value]}] (:db/id value)) values)))
441+
(shui/popup-show! (.-target e)
442+
(fn [{:keys [id]}]
443+
(let [opts {:toggle-fn (fn [] (shui/popup-hide! id))}]
444+
(if (seq values')
445+
(add-existing-values property values' opts)
446+
(choice-base-edit-form property {:create? true}))))
447+
{:id :ls-base-edit-form
448+
:align "start"}))))}}))]))
457449

458450
(rum/defc checkbox-state-mapping
459451
[choices]
@@ -766,7 +758,7 @@
766758
property (first (:rum/args state))
767759
ident (:db/ident property)]
768760
(p/let [_ (db-async/<get-block repo (:block/uuid property))
769-
result (db-async/<get-block-property-values repo ident)]
761+
result (db-async/<get-property-values ident)]
770762
(reset! *values result))
771763
(assoc state ::values *values)))}
772764
[state property* owner-block opts]

src/main/frontend/components/property/value.cljs

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,11 @@
236236
[block property value selected? {:keys [refresh-result-f] :as opts}]
237237
(let [many? (db-property/many? property)
238238
blocks (get-operating-blocks block)]
239+
(when (and selected?
240+
(= :db.type/ref (:db/valueType property))
241+
(number? value)
242+
(not (db/entity value)))
243+
(db-async/<get-block (state/get-current-repo) value {:children? false}))
239244
(p/do!
240245
(if selected?
241246
(<add-property! block (:db/ident property) value
@@ -827,14 +832,7 @@
827832
(p/let [property-ident (if (= :logseq.property/default-value (:db/ident property))
828833
(:db/ident block)
829834
(:db/ident property))
830-
result (db-async/<get-block-property-values (state/get-current-repo)
831-
property-ident)
832-
non-loaded-ids (filter (fn [id] (not (db/entity id))) result)
833-
repo (state/get-current-repo)
834-
_ (p/all (map (fn [id] (db-async/<get-block repo id
835-
{:children? false
836-
:including-property-vals? false
837-
:skip-refresh? true})) non-loaded-ids))]
835+
result (db-async/<get-property-values property-ident)]
838836
(reset! *values result))))]
839837
(refresh-result-f)
840838
(assoc state
@@ -849,7 +847,6 @@
849847
(when-not (= :loading values)
850848
(let [type (:logseq.property/type property)
851849
closed-values? (seq (:property/closed-values property))
852-
ref-type? (db-property-type/all-ref-property-types type)
853850
items (if closed-values?
854851
(let [date? (and
855852
(= (:db/ident property) :logseq.task/recur-unit)
@@ -869,17 +866,9 @@
869866
:label-value value}))
870867
values))
871868
(->> values
872-
(mapcat (fn [value]
873-
(if (coll? value)
874-
(map (fn [v] {:value v}) value)
875-
[{:value value}])))
876-
(map (fn [{:keys [value]}]
877-
(if (and ref-type? (number? value))
878-
(when-let [e (db/entity value)]
879-
{:label (db-property/property-value-content e)
880-
:value value})
881-
{:label value
882-
:value value})))
869+
(map (fn [{:keys [value label]}]
870+
{:label label
871+
:value (:db/id value)}))
883872
(distinct)))
884873
items (->> (if (= :date type)
885874
(map (fn [m] (let [label (:block/title (db/entity (:value m)))]

src/main/frontend/components/query/builder.cljs

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -172,55 +172,42 @@
172172

173173
(rum/defc property-value-select-inner
174174
< rum/reactive db-mixins/query
175-
[repo *property *private-property? *find *tree opts loc values {:keys [db-graph? ref-property? property-type]}]
176-
(let [;; FIXME: lazy load property values consistently on first call
177-
;; Guard against non ref properties like :logseq.property/icon
178-
_ (when (and db-graph? ref-property?)
179-
(doseq [id values] (db/sub-block id)))
180-
values' (if db-graph?
181-
(if ref-property?
182-
(map #(db-property/property-value-content (db/entity repo %)) values)
183-
(if (contains? #{:checkbox :keyword :raw-number :string} property-type)
184-
values
185-
;; Don't display non-ref property values as they don't have display and query support
186-
[]))
187-
values)
188-
values'' (map #(hash-map :value (str %)
189-
;; Preserve original-value as some values like boolean do not display in select
190-
:original-value %)
191-
(cons "Select all" values'))]
192-
(select values''
193-
(fn [{:keys [original-value]}]
175+
[*property *private-property? *find *tree opts loc values {:keys [db-graph?]}]
176+
(let [values' (cons {:label "Select all"
177+
:value "Select all"}
178+
values)]
179+
(select values'
180+
(fn [{:keys [value]}]
194181
(let [k (cond
195182
db-graph? (if @*private-property? :private-property :property)
196183
(= (rum/react *find) :page) :page-property
197184
:else :property)
198-
x (if (= original-value "Select all")
185+
x (if (= value "Select all")
199186
[k @*property]
200-
[k @*property original-value])]
187+
[k @*property value])]
201188
(reset! *property nil)
202189
(append-tree! *tree opts loc x))))))
203190

204191
(rum/defc property-value-select
205192
[repo *property *private-property? *find *tree opts loc]
206193
(let [db-graph? (sqlite-util/db-based-graph? repo)
207-
property-type (when db-graph? (:logseq.property/type (db/entity repo @*property)))
208-
ref-property? (and db-graph? (contains? db-property-type/all-ref-property-types property-type))
209194
[values set-values!] (rum/use-state nil)]
210195
(hooks/use-effect!
211-
(fn []
196+
(fn [_property]
212197
(p/let [result (if db-graph?
213-
(db-async/<get-block-property-values repo @*property)
214-
(db-async/<file-get-property-values repo @*property))]
215-
(when (and db-graph? ref-property?)
216-
(doseq [db-id result]
217-
(db-async/<get-block repo db-id :children? false)))
198+
(p/let [result (db-async/<get-property-values @*property)]
199+
(map (fn [{:keys [label _value]}]
200+
{:label label
201+
:value label})
202+
result))
203+
(p/let [result (db-async/<file-get-property-values repo @*property)]
204+
(map (fn [value]
205+
{:label (str value)
206+
:value value}) result)))]
218207
(set-values! result)))
219208
[@*property])
220-
(property-value-select-inner repo *property *private-property? *find *tree opts loc values
221-
{:db-graph? db-graph?
222-
:ref-property? ref-property?
223-
:property-type property-type})))
209+
(property-value-select-inner *property *private-property? *find *tree opts loc values
210+
{:db-graph? db-graph?})))
224211

225212
(rum/defc tags
226213
[repo *tree opts loc]

src/main/frontend/components/views.cljs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
[logseq.db.frontend.property :as db-property]
4040
[logseq.db.frontend.view :as db-view]
4141
[logseq.shui.ui :as shui]
42-
[medley.core :as medley]
4342
[promesa.core :as p]
4443
[rum.core :as rum]))
4544

@@ -709,7 +708,7 @@
709708
(hooks/use-effect!
710709
(fn []
711710
(when (and view-entity property-ident (not (or timestamp? checkbox?)))
712-
(p/let [data (get-property-values (:db/id view-entity) property-ident)]
711+
(p/let [data (db-async/<get-property-values property-ident {:view-id (:db/id view-entity)})]
713712
(set-values! data))))
714713
[property-ident])
715714
(let [option (cond
@@ -911,7 +910,7 @@
911910
(when (seq ids) (db-async/<get-blocks (state/get-current-repo) ids)))
912911
(when (and property-ident dropdown-open?
913912
(not (contains? #{:data :datetime :checkbox} type)))
914-
(p/let [data (get-property-values (:db/id view-entity) property-ident)]
913+
(p/let [data (db-async/<get-property-values property-ident {:view-id (:db/id view-entity)})]
915914
(set-values! (map (fn [v] (if (map? (:value v))
916915
(assoc v :value (:block/uuid (:value v)))
917916
v)) data)))))

0 commit comments

Comments
 (0)