edge.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 rumble contains Rumble functions which interface with EliasDB.
  12. */
  13. package rumble
  14. import (
  15. "fmt"
  16. "devt.de/krotik/common/defs/rumble"
  17. "devt.de/krotik/eliasdb/api"
  18. "devt.de/krotik/eliasdb/graph"
  19. "devt.de/krotik/eliasdb/graph/data"
  20. )
  21. // Function: storeEdge
  22. // ===================
  23. /*
  24. StoreEdgeFunc inserts or updates an edge in EliasDB.
  25. */
  26. type StoreEdgeFunc struct {
  27. }
  28. /*
  29. Name returns the name of the function.
  30. */
  31. func (f *StoreEdgeFunc) Name() string {
  32. return "db.storeEdge"
  33. }
  34. /*
  35. Validate is called for parameter validation and to reset the function state.
  36. */
  37. func (f *StoreEdgeFunc) Validate(argsNum int, rt rumble.Runtime) rumble.RuntimeError {
  38. var err rumble.RuntimeError
  39. if argsNum != 2 && argsNum != 3 {
  40. err = rt.NewRuntimeError(rumble.ErrInvalidConstruct,
  41. "Function storeEdge requires 2 or 3 parameters: partition, edge"+
  42. " map and optionally a transaction")
  43. }
  44. return err
  45. }
  46. /*
  47. Execute executes the rumble function.
  48. */
  49. func (f *StoreEdgeFunc) Execute(argsVal []interface{}, vars rumble.Variables,
  50. rt rumble.Runtime) (interface{}, rumble.RuntimeError) {
  51. var trans graph.Trans
  52. var err rumble.RuntimeError
  53. part := fmt.Sprint(argsVal[0])
  54. nodeMap, ok := argsVal[1].(map[interface{}]interface{})
  55. // Check parameters
  56. if !ok {
  57. err = rt.NewRuntimeError(rumble.ErrNotAMap,
  58. "Second parameter must be a map")
  59. }
  60. if err == nil && len(argsVal) > 2 {
  61. if trans, ok = argsVal[2].(graph.Trans); !ok {
  62. err = rt.NewRuntimeError(rumble.ErrInvalidConstruct,
  63. "Third parameter must be a transaction")
  64. }
  65. }
  66. // Build up node to store
  67. edge := data.NewGraphEdgeFromNode(NewGraphNodeFromRumbleMap(nodeMap))
  68. // Store the edge
  69. if err == nil {
  70. if trans != nil {
  71. err = trans.StoreEdge(part, edge)
  72. } else {
  73. err = api.GM.StoreEdge(part, edge)
  74. }
  75. if err != nil {
  76. // Wrap error message in RuntimeError
  77. err = rt.NewRuntimeError(rumble.ErrInvalidState,
  78. fmt.Sprintf("Cannot store edge: %v", err.Error()))
  79. }
  80. }
  81. return nil, err
  82. }
  83. // Function: removeEdge
  84. // ====================
  85. /*
  86. RemoveEdgeFunc removes an edge in EliasDB.
  87. */
  88. type RemoveEdgeFunc struct {
  89. }
  90. /*
  91. Name returns the name of the function.
  92. */
  93. func (f *RemoveEdgeFunc) Name() string {
  94. return "db.removeEdge"
  95. }
  96. /*
  97. Validate is called for parameter validation and to reset the function state.
  98. */
  99. func (f *RemoveEdgeFunc) Validate(argsNum int, rt rumble.Runtime) rumble.RuntimeError {
  100. var err rumble.RuntimeError
  101. if argsNum != 3 && argsNum != 4 {
  102. err = rt.NewRuntimeError(rumble.ErrInvalidConstruct,
  103. "Function removeEdge requires 3 or 4 parameters: partition, edge key,"+
  104. " edge kind and optionally a transaction")
  105. }
  106. return err
  107. }
  108. /*
  109. Execute executes the rumble function.
  110. */
  111. func (f *RemoveEdgeFunc) Execute(argsVal []interface{}, vars rumble.Variables,
  112. rt rumble.Runtime) (interface{}, rumble.RuntimeError) {
  113. var trans graph.Trans
  114. var err rumble.RuntimeError
  115. part := fmt.Sprint(argsVal[0])
  116. key := fmt.Sprint(argsVal[1])
  117. kind := fmt.Sprint(argsVal[2])
  118. // Check parameters
  119. if len(argsVal) > 3 {
  120. var ok bool
  121. if trans, ok = argsVal[3].(graph.Trans); !ok {
  122. err = rt.NewRuntimeError(rumble.ErrInvalidConstruct,
  123. "Fourth parameter must be a transaction")
  124. }
  125. }
  126. // Remove the edge
  127. if err == nil {
  128. if trans != nil {
  129. err = trans.RemoveEdge(part, key, kind)
  130. } else {
  131. _, err = api.GM.RemoveEdge(part, key, kind)
  132. }
  133. if err != nil {
  134. // Wrap error message in RuntimeError
  135. err = rt.NewRuntimeError(rumble.ErrInvalidState,
  136. fmt.Sprintf("Cannot remove edge: %v", err.Error()))
  137. }
  138. }
  139. return nil, err
  140. }
  141. // Function: fetchEdge
  142. // ===================
  143. /*
  144. FetchEdgeFunc fetches an edge in EliasDB.
  145. */
  146. type FetchEdgeFunc struct {
  147. }
  148. /*
  149. Name returns the name of the function.
  150. */
  151. func (f *FetchEdgeFunc) Name() string {
  152. return "db.fetchEdge"
  153. }
  154. /*
  155. Validate is called for parameter validation and to reset the function state.
  156. */
  157. func (f *FetchEdgeFunc) Validate(argsNum int, rt rumble.Runtime) rumble.RuntimeError {
  158. var err rumble.RuntimeError
  159. if argsNum != 3 {
  160. err = rt.NewRuntimeError(rumble.ErrInvalidConstruct,
  161. "Function fetchEdge requires 3 parameters: partition, edge key and"+
  162. " edge kind")
  163. }
  164. return err
  165. }
  166. /*
  167. Execute executes the rumble function.
  168. */
  169. func (f *FetchEdgeFunc) Execute(argsVal []interface{}, vars rumble.Variables,
  170. rt rumble.Runtime) (interface{}, rumble.RuntimeError) {
  171. var node data.Node
  172. var res interface{}
  173. var err rumble.RuntimeError
  174. part := fmt.Sprint(argsVal[0])
  175. key := fmt.Sprint(argsVal[1])
  176. kind := fmt.Sprint(argsVal[2])
  177. conv := func(m map[string]interface{}) map[interface{}]interface{} {
  178. c := make(map[interface{}]interface{})
  179. for k, v := range m {
  180. c[k] = v
  181. }
  182. return c
  183. }
  184. // Fetch the node
  185. if node, err = api.GM.FetchEdge(part, key, kind); node != nil {
  186. res = conv(node.Data())
  187. }
  188. if err != nil {
  189. // Wrap error message in RuntimeError
  190. err = rt.NewRuntimeError(rumble.ErrInvalidState,
  191. fmt.Sprintf("Cannot fetch edge: %v", err.Error()))
  192. }
  193. return res, err
  194. }
  195. // Function: traverse
  196. // ==================
  197. /*
  198. TraverseFunc traverses an edge in EliasDB.
  199. */
  200. type TraverseFunc struct {
  201. }
  202. /*
  203. Name returns the name of the function.
  204. */
  205. func (f *TraverseFunc) Name() string {
  206. return "db.traverse"
  207. }
  208. /*
  209. Validate is called for parameter validation and to reset the function state.
  210. */
  211. func (f *TraverseFunc) Validate(argsNum int, rt rumble.Runtime) rumble.RuntimeError {
  212. var err rumble.RuntimeError
  213. if argsNum != 4 {
  214. err = rt.NewRuntimeError(rumble.ErrInvalidConstruct,
  215. "Function traverse requires 4 parameters: partition, node key,"+
  216. " node kind and a traversal spec")
  217. }
  218. return err
  219. }
  220. /*
  221. Execute executes the rumble function.
  222. */
  223. func (f *TraverseFunc) Execute(argsVal []interface{}, vars rumble.Variables,
  224. rt rumble.Runtime) (interface{}, rumble.RuntimeError) {
  225. var nodes []data.Node
  226. var edges []data.Edge
  227. var res interface{}
  228. var err rumble.RuntimeError
  229. part := fmt.Sprint(argsVal[0])
  230. key := fmt.Sprint(argsVal[1])
  231. kind := fmt.Sprint(argsVal[2])
  232. spec := fmt.Sprint(argsVal[3])
  233. conv := func(m map[string]interface{}) map[interface{}]interface{} {
  234. c := make(map[interface{}]interface{})
  235. for k, v := range m {
  236. c[k] = v
  237. }
  238. return c
  239. }
  240. // Do the traversal
  241. if nodes, edges, err = api.GM.TraverseMulti(part, key, kind, spec, true); err == nil {
  242. resNodes := make([]interface{}, len(nodes))
  243. for i, n := range nodes {
  244. resNodes[i] = conv(n.Data())
  245. }
  246. resEdges := make([]interface{}, len(edges))
  247. for i, e := range edges {
  248. resEdges[i] = conv(e.Data())
  249. }
  250. res = []interface{}{resNodes, resEdges}
  251. }
  252. if err != nil {
  253. // Wrap error message in RuntimeError
  254. err = rt.NewRuntimeError(rumble.ErrInvalidState,
  255. fmt.Sprintf("Cannot traverse: %v", err.Error()))
  256. }
  257. return res, err
  258. }