globals.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. * EliasDB
  3. *
  4. * Copyright 2016 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the Mozilla Public
  7. * License, v. 2.0. If a copy of the MPL was not distributed with this
  8. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. */
  10. /*
  11. Package graph contains the main API to the graph datastore.
  12. Manager API
  13. The main API is provided by a Manager object which can be created with the
  14. NewGraphManager() constructor function. The manager CRUD functionality for
  15. nodes and edges through store, fetch and remove functions. It also provides
  16. the basic traversal functionality which allos the traversal from one node to
  17. other nodes.
  18. Node iterator
  19. All available node keys in a partition of a given kind can be iterated by using
  20. a NodeKeyIterator. The manager can produce these with the NodeKeyIterator()
  21. function.
  22. Fulltext search
  23. All nodes and edges in the datastore are indexed. The index can be queried
  24. using a IndexQuery object. The manager can produce these with the NodeIndexQuery()
  25. or EdgeIndexQuery function.
  26. Transactions
  27. A transaction is used to build up multiple store and delete tasks for the
  28. graph database. Nothing is written to the database before calling commit().
  29. A transaction commit does an automatic rollback if an error occurs
  30. (except fatal disk write errors which might cause a panic).
  31. A trans object can be created with the NewGraphTrans() function.
  32. Rules
  33. (Use with caution)
  34. Graph rules provide automatic operations which help to keep the graph consistent.
  35. Rules trigger on global graph events. The rules SystemRuleDeleteNodeEdges and
  36. SystemRuleUpdateNodeStats are automatically loaded when a new Manager is created.
  37. See the code for further details.
  38. Graph databases
  39. A graph manager handles the graph storage and provides the API for
  40. the graph database. The storage is divided into several databases:
  41. Main database
  42. MainDB stores various meta information such as known node/edge kinds, attributes
  43. or version information.
  44. Names database
  45. Names can be encoded (into a number) or decoded (into a string)
  46. 32 bit values for any given node attribute names
  47. 16 bit values for any given edge role names
  48. 16 bit values for any given edge kind names
  49. Nodes database
  50. Each node kind database stores:
  51. PrefixNSAttrs + node key -> [ ATTRS ]
  52. (a list of attributes of a certain node)
  53. PrefixNSAttr + node key + attr num -> value
  54. (attribute value of a certain node)
  55. PrefixNSSpecs + node key -> map[spec]<empty string>
  56. (a lookup for available specs for a certain node)
  57. PrefixNSEdge + node key + spec -> map[edge key]edgeinfo{other node key, other node kind}]
  58. (connection from one node to another via a spec)
  59. Edges database
  60. Each edge kind database stores:
  61. PrefixNSAttrs + edge key -> [ ATTRS ]
  62. (a list of attributes of a certain edge)
  63. PrefixNSAttr + edge key + attr num -> value
  64. (attribute value of a certain edge)
  65. Index database
  66. The text index managed by util/indexmanager.go. IndexQuery provides access to
  67. the full text search index.
  68. */
  69. package graph
  70. import "errors"
  71. /*
  72. VERSION of the GraphManager
  73. */
  74. const VERSION = 1
  75. /*
  76. MainDBEntryPrefix is the prefix for entries stored in the main database
  77. */
  78. const MainDBEntryPrefix = "\x02"
  79. // MainDB entries
  80. // ==============
  81. /*
  82. MainDBVersion is the MainDB entry key for version information
  83. */
  84. const MainDBVersion = MainDBEntryPrefix + "ver"
  85. /*
  86. MainDBNodeKinds is the MainDB entry key for node kind information
  87. */
  88. const MainDBNodeKinds = MainDBEntryPrefix + "nodekind"
  89. /*
  90. MainDBEdgeKinds is the MainDB entry key for edge kind information
  91. */
  92. const MainDBEdgeKinds = MainDBEntryPrefix + "edgekind"
  93. /*
  94. MainDBParts is the MainDB entry key for partition information
  95. */
  96. const MainDBParts = MainDBEntryPrefix + "part"
  97. /*
  98. MainDBNodeAttrs is the MainDB entry key for a list of node attributes
  99. */
  100. const MainDBNodeAttrs = MainDBEntryPrefix + "natt"
  101. /*
  102. MainDBNodeEdges is the MainDB entry key for a list of node relationships
  103. */
  104. const MainDBNodeEdges = MainDBEntryPrefix + "nrel"
  105. /*
  106. MainDBNodeCount is the MainDB entry key for a node count
  107. */
  108. const MainDBNodeCount = MainDBEntryPrefix + "ncnt"
  109. /*
  110. MainDBEdgeAttrs is the MainDB entry key for a list of edge attributes
  111. */
  112. const MainDBEdgeAttrs = MainDBEntryPrefix + "eatt"
  113. /*
  114. MainDBEdgeCount is the MainDB entry key for an edge count
  115. */
  116. const MainDBEdgeCount = MainDBEntryPrefix + "ecnt"
  117. // Root IDs for StorageManagers
  118. // ============================
  119. /*
  120. RootIDNodeHTree is the root ID for the HTree holding primary information
  121. */
  122. const RootIDNodeHTree = 2
  123. /*
  124. RootIDNodeHTreeSecond is the root ID for the HTree holding secondary information
  125. */
  126. const RootIDNodeHTreeSecond = 3
  127. // Suffixes for StorageManagers
  128. // ============================
  129. /*
  130. StorageSuffixNodes is the suffix for a node storage
  131. */
  132. const StorageSuffixNodes = ".nodes"
  133. /*
  134. StorageSuffixNodesIndex is the suffix for a node index
  135. */
  136. const StorageSuffixNodesIndex = ".nodeidx"
  137. /*
  138. StorageSuffixEdges is the suffix for an edge storage
  139. */
  140. const StorageSuffixEdges = ".edges"
  141. /*
  142. StorageSuffixEdgesIndex is the suffix for an edge index
  143. */
  144. const StorageSuffixEdgesIndex = ".edgeidx"
  145. // PREFIXES for Node storage
  146. // =========================
  147. // Prefixes are only one byte. They should be followed by the node key so
  148. // similar entries are stored near each other physically.
  149. //
  150. /*
  151. PrefixNSAttrs is the prefix for storing attributes of a node
  152. */
  153. const PrefixNSAttrs = "\x01"
  154. /*
  155. PrefixNSAttr is the prefix for storing the value of a node attribute
  156. */
  157. const PrefixNSAttr = "\x02"
  158. /*
  159. PrefixNSSpecs is the prefix for storing specs of edges related to a node
  160. */
  161. const PrefixNSSpecs = "\x03"
  162. /*
  163. PrefixNSEdge is the prefix for storing a link from a node (and a spec) to an edge
  164. */
  165. const PrefixNSEdge = "\x04"
  166. // Graph events
  167. //=============
  168. /*
  169. EventNodeCreated is thrown when a node was created.
  170. Parameters: partition of created node, created node
  171. */
  172. const EventNodeCreated = 0x01
  173. /*
  174. EventNodeUpdated is thrown when a node was updated.
  175. Parameters: partition of updated node, updated node, old node
  176. */
  177. const EventNodeUpdated = 0x02
  178. /*
  179. EventNodeDeleted is thrown when a node was deleted.
  180. Parameters: partition of deleted node, deleted node
  181. */
  182. const EventNodeDeleted = 0x03
  183. /*
  184. EventEdgeCreated is thrown when an edge was created.
  185. Parameters: partition of created edge, created edge
  186. */
  187. const EventEdgeCreated = 0x04
  188. /*
  189. EventEdgeUpdated is thrown when an edge was updated.
  190. Parameters: partition of updated edge, updated edge, old edge
  191. */
  192. const EventEdgeUpdated = 0x05
  193. /*
  194. EventEdgeDeleted is thrown when an edge was deleted.
  195. Parameters: partition of deleted edge, deleted edge
  196. */
  197. const EventEdgeDeleted = 0x06
  198. /*
  199. EventNodeStore is thrown before a node is stored (always overwriting existing values).
  200. Parameters: partition of node to store, node to store
  201. */
  202. const EventNodeStore = 0x07
  203. /*
  204. EventNodeUpdate is thrown before a node is updated.
  205. Parameters: partition of node to update, node to update
  206. */
  207. const EventNodeUpdate = 0x08
  208. /*
  209. EventNodeDelete is thrown before a node is deleted.
  210. Parameters: partition of node to delete, key of node to delete, kind of node to delete
  211. */
  212. const EventNodeDelete = 0x09
  213. /*
  214. EventEdgeStore is thrown before an edge is stored (always overwriting existing values).
  215. Parameters: partition of stored edge, stored edge
  216. */
  217. const EventEdgeStore = 0x0A
  218. /*
  219. EventEdgeDelete is thrown before an edge is deleted.
  220. Parameters: partition of deleted edge, key of edge to delete, kind of edge to delete
  221. */
  222. const EventEdgeDelete = 0x0B
  223. /*
  224. ErrEventHandled is a special error which an event handler can return to
  225. notify the GraphManager that no further action is necessary. No error will
  226. be returned by the GraphManager operation.
  227. */
  228. var ErrEventHandled = errors.New("Event handled upstream")