indexing_strategy.rst 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. RDF Store & Index Design
  2. ========================
  3. This is a log of subsequent strategies employed to store triples in
  4. LMDB.
  5. Strategy #4a is the one currently used. The rest is kept for historic
  6. reasons and academic curiosity (and also because this took too much work to
  7. just wipe out of memory).
  8. Storage approach
  9. ----------------
  10. - Pickle quad and create MD5 or SHA1 hash.
  11. - Store triples in one database paired with key; store indices
  12. separately.
  13. Different strategies involve layout and number of databases.
  14. Strategy #1
  15. -----------
  16. - kq: key: serialized triple (1:1)
  17. - sk: Serialized subject: key (1:m)
  18. - pk: Serialized predicate: key (1:m)
  19. - ok: Serialized object: key (1:m)
  20. - (optional) lok: Serialized literal object: key (1:m)
  21. - (optional) tok: Serialized RDF type: key (1:m)
  22. - ck: Serialized context: key (1:m)
  23. Retrieval approach
  24. ~~~~~~~~~~~~~~~~~~
  25. To find all matches for a quad:
  26. - If all terms in the quad are bound, generate the key from the pickled
  27. quad and look up the triple in ``kt``
  28. - If all terms are unbound, return an iterator of all values in ``kt``.
  29. - If some values are bound and some unbound (most common query):
  30. - Get a base list of keys associated wirh the first bound term
  31. - For each subsequent bound term, check if each key associated with
  32. the term matches a key in the base list
  33. - Continue through all the bound terms. If a match is not found at
  34. any point, continue to the next term
  35. - If a match is found in all the bound term databases, look up the
  36. pickled quad matching the key in ``kq`` and yield it
  37. More optimization can be introduced later, e.g. separating literal and
  38. RDF type objects in separate databases. Literals can have very long
  39. values and a database with a longer key setting may be useful. RDF terms
  40. can be indexed separately because they are the most common bound term.
  41. Example lookup
  42. ~~~~~~~~~~~~~~
  43. Keys and Triples (should actually be quads but this is a simplified
  44. version):
  45. - A:
  46. - s1
  47. - p1
  48. - o1
  49. - B:
  50. - s1
  51. - p2
  52. - o2
  53. - C:
  54. - s2
  55. - p3
  56. - o1
  57. - D:
  58. - s2
  59. - p3
  60. - o3
  61. Indices:
  62. - SK:
  63. - s1: A, B
  64. - s2: C, D
  65. - PK:
  66. - p1: A
  67. - p2: B
  68. - p3: C, D
  69. - OK:
  70. - o1: A, C
  71. - o2: B
  72. - o3: D
  73. Queries:
  74. - s1 ?p ?o → {A, B}
  75. - s1 p2 ?o → {A, B} & {B} = {B}
  76. - ?s ?p o3 → {D}
  77. - s1 p2 o5 → {} (Exit at OK: no term matches ‘o5’)
  78. - s2 p3 o2 → {C, D} & {C, D} & {B} = {}
  79. Strategy #2
  80. -----------
  81. Separate data and indices in two environments.
  82. Main data store
  83. ~~~~~~~~~~~~~~~
  84. Key to quad; main keyspace; all unique.
  85. Indices
  86. ~~~~~~~
  87. None of these databases is of critical preservation concern. They can be
  88. rebuilt from the main data store.
  89. All dupsort and dupfixed.
  90. @TODO The first three may not be needed if computing term hash is fast
  91. enough.
  92. - t2k (term to term key)
  93. - lt2k (literal to term key: longer keys)
  94. - k2t (term key to term)
  95. - s2k (subject key to quad key)
  96. - p2k (pred key to quad key)
  97. - o2k (object key to quad key)
  98. - c2k (context key to quad key)
  99. - sc2qk (subject + context keys to quad key)
  100. - po2qk (predicate + object keys to quad key)
  101. - sp2qk (subject + predicate keys to quad key)
  102. - oc2qk (object + context keys to quad key)
  103. - so2qk (subject + object keys to quad key)
  104. - pc2qk (predicate + context keys to quad key)
  105. Strategy #3
  106. -----------
  107. Contexts are much fewer (even in graph per aspect, 5-10 triples per
  108. graph)
  109. .. _main-data-store-1:
  110. Main data store
  111. ~~~~~~~~~~~~~~~
  112. Preservation-worthy data
  113. - tk:t (triple key: triple; dupsort, dupfixed)
  114. - tk:c (context key: triple; unique)
  115. .. _indices-1:
  116. Indices
  117. ~~~~~~~
  118. Rebuildable from main data store
  119. - s2k (subject key: triple key)
  120. - p2k (pred key: triple key)
  121. - o2k (object key: triple key)
  122. - sp2k
  123. - so2k
  124. - po2k
  125. - spo2k
  126. Lookup
  127. ~~~~~~
  128. 1. Look up triples by s, p, o, sp, so, po and get keys
  129. 2. If a context is specified, for each key try to seek to (context, key)
  130. in ct to verify it exists
  131. 3. Intersect sets
  132. 4. Match triple keys with data using kt
  133. Shortcuts
  134. ^^^^^^^^^
  135. - Get all contexts: return list of keys from ct
  136. - Get all triples for a context: get all values for a contex from ct
  137. and match triple data with kt
  138. - Get one triple match for all contexts: look up in triple indices and
  139. match triple data with kt
  140. Strategy #4
  141. -----------
  142. Terms are entered individually in main data store. Also, shorter keys
  143. are used rather than hashes. These two aspects save a great deal of
  144. space and I/O, but require an additional index to put the terms together
  145. in a triple.
  146. .. _main-data-store-2:
  147. Main Data Store
  148. ~~~~~~~~~~~~~~~
  149. - t:st (term key: serialized term; 1:1)
  150. - spo:c (joined S, P, O keys: context key; 1:m)
  151. - c: (context keys only, values are the empty bytestring)
  152. Storage total: variable
  153. .. _indices-2:
  154. Indices
  155. ~~~~~~~
  156. - th:t (term hash: term key; 1:1)
  157. - c:spo (context key: joined triple keys; 1:m)
  158. - s:po (S key: P + O key; 1:m)
  159. - p:so (P key: S + O keys; 1:m)
  160. - o:sp (object key: triple key; 1:m)
  161. - sp:o (S + P keys: O key; 1:m)
  162. - so:p (S + O keys: P key; 1:m)
  163. - po:s (P + O keys: S key; 1:m)
  164. Storage total: 143 bytes per triple
  165. Disadvantages
  166. ~~~~~~~~~~~~~
  167. - Lots of indices
  168. - Terms can get orphaned:
  169. - No easy way to know if a term is used anywhere in a quad
  170. - Needs some routine cleanup
  171. - On the other hand, terms are relatively light-weight and can be
  172. reused
  173. - Almost surely not reusable are UUIDs, message digests, timestamps
  174. etc.
  175. Strategy #5
  176. -----------
  177. Reduce number of indices and rely on parsing and splitting keys to find
  178. triples with two bound parameters.
  179. This is especially important for keeping indexing synchronous to achieve
  180. fully ACID writes.
  181. .. _main-data-store-3:
  182. Main data store
  183. ~~~~~~~~~~~~~~~
  184. Same as Strategy #4:
  185. - t:st (term key: serialized term; 1:1)
  186. - spo:c (joined S, P, O keys: context key; dupsort, dupfixed)
  187. - c: (context keys only, values are the empty bytestring; 1:1)
  188. Storage total: variable (same as #4)
  189. .. _indices-3:
  190. Indices
  191. ~~~~~~~
  192. - th:t (term hash: term key; 1:1)
  193. - s:po (S key: joined P, O keys; dupsort, dupfixed)
  194. - p:so (P key: joined S, O keys; dupsort, dupfixed)
  195. - o:sp (O key: joined S, P keys; dupsort, dupfixed)
  196. - c:spo (context → triple association; dupsort, dupfixed)
  197. Storage total: 95 bytes per triple
  198. Lookup strategy
  199. ~~~~~~~~~~~~~~~
  200. - ? ? ? c: [c:spo] all SPO for C → split key → [t:st] term from term
  201. key
  202. - s p o c: [c:spo] exact SPO & C match → split key → [t:st] term from
  203. term key
  204. - s ? ?: [s:po] All PO for S → split key → [t:st] term from term key
  205. - s p ?: [s:po] All PO for S → filter result by P in split key → [t:st]
  206. term from term key
  207. Advantages
  208. ~~~~~~~~~~
  209. - Less indices: smaller index size and less I/O
  210. .. _disadvantages-1:
  211. Disadvantages
  212. ~~~~~~~~~~~~~
  213. - Slower retrieval for queries with 2 bound terms
  214. Further optimization
  215. ~~~~~~~~~~~~~~~~~~~~
  216. In order to minimize traversing and splittig results, the first
  217. retrieval should be made on the term with less average keys. Search
  218. order can be balanced by establishing a lookup order for indices.
  219. This can be achieved by calling stats on the index databases and looking
  220. up the database with *most* keys. Since there is an equal number of
  221. entries in each of the (s:po, p:so, o:sp) indices, the one with most
  222. keys will have the least average number of values per key. If that
  223. lookup is done first, the initial data set to traverse and filter will
  224. be smaller.
  225. Strategy #5a
  226. ------------
  227. This is a slightly different implementation of #5 that somewhat
  228. simplifies and perhaps speeds up things a bit.
  229. The indexing and lookup strtegy is the same; but instead of using a
  230. separator byte for splitting compound keys, the logic relies on the fact
  231. that keys have a fixed length and are sliced instead. This *should*
  232. result in faster key manipulation, also because in most cases
  233. ``memoryview`` buffers can be used directly instead of being copied from
  234. memory.
  235. Index storage is 90 bytes per triple.
  236. Strategy #4a
  237. ------------
  238. This is a variation of Strategy 4 using fixed-size keys. It is the currently
  239. employed solution starting with alpha18.
  240. After using #5a up to alpha17, it was apparent that 2-bound queries were quite
  241. penalized in queries which return few results. All the keys for a 1-bound
  242. lookup had to be retrieved and iterated over to verify that they contained the
  243. second ("filter") term. This approach, instead, only looks up the relevant
  244. keys and composes the results. It is slower on writes and nearly doubles the
  245. size of the indices, but it makes reads faster and more memory-efficient.
  246. Alpha20 uses the same strategy but keys are treated as ``size_t`` integers
  247. rather than ``char*`` strings, thus making the code much cleaner.