7

使用 Datomic,我试图考虑如何在列表中嵌套列表?

一个示例 Clojure 数据结构将是某种东西(见图1)。我知道 Datomic 有引用的概念(例如在图 2 中。但那些看起来像标量类型,而不是对象(本身)。

  [{:id 0,
    :symbol DDD,
    :company 3D Systems Corporation,
    :price-difference 1.3100000000000023,
    :event-list [{high 35.11,
                  tickerId 0,
                  WAP 34.491,
                  open 35.07,
                  date 20130426,
                  count 3403,
                  low 33.8,
                  hasGaps false,
                  close 34.53,
                  field historicalData,
                  volume 8667,
                  type historicalData}]}]

图。1

 {:db/id #db/id[:db.part/db]
  :db/ident :district/region
  :db/valueType :db.type/ref
  :db/cardinality :db.cardinality/one
  :db/doc "A district region enum value"
  :db.install/_attribute :db.part/db}

 ;; district/region enum values
 [:db/add #db/id[:db.part/user] :db/ident :region/n]
 [:db/add #db/id[:db.part/user] :db/ident :region/ne]
 [:db/add #db/id[:db.part/user] :db/ident :region/e]
 [:db/add #db/id[:db.part/user] :db/ident :region/se]
 [:db/add #db/id[:db.part/user] :db/ident :region/s]
 [:db/add #db/id[:db.part/user] :db/ident :region/sw]
 [:db/add #db/id[:db.part/user] :db/ident :region/w]
 [:db/add #db/id[:db.part/user] :db/ident :region/nw]

图2

[编辑]

其实,我想我可能在这里找到了答案。我正在浏览Day-Of-Datomic代码库,并找到了下面的示例。但是我仍然不清楚:comments命名空间与:comment/body(或:comment/author)命名空间的明确联系。Datomic 是否仅使用约定将:comments链接到:comment

  ;; comments
  [{:db/id #db/id[:db.part/db]
  :db/ident :comments
    :db/valueType :db.type/ref
    :db/cardinality :db.cardinality/many
    :db/isComponent true
    :db.install/_attribute :db.part/db}
   {:db/id #db/id[:db.part/db]
    :db/ident :comment/body
    :db/valueType :db.type/string
    :db/cardinality :db.cardinality/one
    :db.install/_attribute :db.part/db}
   {:db/id #db/id[:db.part/db]
    :db/ident :comment/author
    :db/valueType :db.type/ref
    :db/cardinality :db.cardinality/one
    :db.install/_attribute :db.part/db}]

图。1

4

1 回答 1

10

您不能直接保留多维列表/向量,但可以使用链接实体来完成类似的操作:

;; sample attributes
[{:db/id #db/id[:db.part/db]
  :db/ident :some/ref-value
  :db/valueType :db.type/ref
  :db/isComponent true
  :db/cardinality :db.cardinality/many  
  :db.install/_attribute :db.part/db}
  {:db/id #db/id[:db.part/db]
  :db/ident :some/list-value
  :db/valueType :db.type/string
  :db/cardinality :db.cardinality/many
  :db.install/_attribute :db.part/db}]

;; [["a" "b" "c"]["d" "e" "f"]]
 [{:db/id #db/id[:db.part/user -1]
 :some/list-value ["a" "b" "c"]}
 {:db/id #db/id[:db.part/user -2]
 :some/list-value ["d" "e" "f"]} 
 {:db/id #db/id[:db.part/user]
 :some/ref-value [#db/id[:db.part/user -1] #db/id[:db.part/user -2]]}]

请注意,您可以使用负数将实体链接在一起。当您进行交易时,它们将被真实的实体 ID 替换。

编辑:从版本 0.8.4020 开始,Datomic 支持嵌套组件实体(由 指定的包含关系:db/isComponent)作为事务数据的一部分。例如

 ;; [["a" "b" "c"]["d" "e" "f"]]
 [{:db/id #db/id[:db.part/db]
   :some/ref-value [{:some/list-value ["a" "b" "c"]}
                    {:some/list-value ["d" "e" "f"]}]}]

嵌套映射扩展为两个子实体,它们都创建在与容器/父实体相同的分区中。有关完整的代码示例,请参阅https://gist.github.com/a2ndrade/5820364

于 2013-04-26T19:24:50.057 回答