edge.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 dbfunc contains EliasDB specific functions for the event condition action language (ECAL).
  12. */
  13. package dbfunc
  14. import (
  15. "fmt"
  16. "devt.de/krotik/ecal/parser"
  17. "devt.de/krotik/eliasdb/graph"
  18. "devt.de/krotik/eliasdb/graph/data"
  19. )
  20. /*
  21. StoreEdgeFunc inserts or updates an edge in EliasDB.
  22. */
  23. type StoreEdgeFunc struct {
  24. GM *graph.Manager
  25. }
  26. /*
  27. Run executes the ECAL function.
  28. */
  29. func (f *StoreEdgeFunc) Run(instanceID string, vs parser.Scope, is map[string]interface{}, tid uint64, args []interface{}) (interface{}, error) {
  30. var err error
  31. if arglen := len(args); arglen != 2 && arglen != 3 {
  32. err = fmt.Errorf("Function requires 2 or 3 parameters: partition, edge" +
  33. " map and optionally a transaction")
  34. }
  35. if err == nil {
  36. var trans graph.Trans
  37. part := fmt.Sprint(args[0])
  38. nodeMap, ok := args[1].(map[interface{}]interface{})
  39. // Check parameters
  40. if !ok {
  41. err = fmt.Errorf("Second parameter must be a map")
  42. }
  43. if err == nil && len(args) > 2 {
  44. if trans, ok = args[2].(graph.Trans); !ok {
  45. err = fmt.Errorf("Third parameter must be a transaction")
  46. }
  47. }
  48. // Build up node to store
  49. edge := data.NewGraphEdgeFromNode(NewGraphNodeFromECALMap(nodeMap))
  50. // Store the edge
  51. if err == nil {
  52. if trans != nil {
  53. err = trans.StoreEdge(part, edge)
  54. } else {
  55. err = f.GM.StoreEdge(part, edge)
  56. }
  57. }
  58. }
  59. return nil, err
  60. }
  61. /*
  62. DocString returns a descriptive string.
  63. */
  64. func (f *StoreEdgeFunc) DocString() (string, error) {
  65. return "Inserts or updates an edge in EliasDB.", nil
  66. }
  67. /*
  68. RemoveEdgeFunc removes an edge in EliasDB.
  69. */
  70. type RemoveEdgeFunc struct {
  71. GM *graph.Manager
  72. }
  73. /*
  74. Run executes the ECAL function.
  75. */
  76. func (f *RemoveEdgeFunc) Run(instanceID string, vs parser.Scope, is map[string]interface{}, tid uint64, args []interface{}) (interface{}, error) {
  77. var err error
  78. if arglen := len(args); arglen != 3 && arglen != 4 {
  79. err = fmt.Errorf("Function requires 3 or 4 parameters: partition, edge key," +
  80. " edge kind and optionally a transaction")
  81. }
  82. if err == nil {
  83. var trans graph.Trans
  84. part := fmt.Sprint(args[0])
  85. key := fmt.Sprint(args[1])
  86. kind := fmt.Sprint(args[2])
  87. // Check parameters
  88. if len(args) > 3 {
  89. var ok bool
  90. if trans, ok = args[3].(graph.Trans); !ok {
  91. err = fmt.Errorf("Fourth parameter must be a transaction")
  92. }
  93. }
  94. // Remove the edge
  95. if err == nil {
  96. if trans != nil {
  97. err = trans.RemoveEdge(part, key, kind)
  98. } else {
  99. _, err = f.GM.RemoveEdge(part, key, kind)
  100. }
  101. }
  102. }
  103. return nil, err
  104. }
  105. /*
  106. DocString returns a descriptive string.
  107. */
  108. func (f *RemoveEdgeFunc) DocString() (string, error) {
  109. return "Removes an edge in EliasDB.", nil
  110. }
  111. /*
  112. FetchEdgeFunc fetches an edge in EliasDB.
  113. */
  114. type FetchEdgeFunc struct {
  115. GM *graph.Manager
  116. }
  117. /*
  118. Run executes the ECAL function.
  119. */
  120. func (f *FetchEdgeFunc) Run(instanceID string, vs parser.Scope, is map[string]interface{}, tid uint64, args []interface{}) (interface{}, error) {
  121. var res interface{}
  122. var err error
  123. if arglen := len(args); arglen != 3 {
  124. err = fmt.Errorf("Function requires 3 parameters: partition, edge key and" +
  125. " edge kind")
  126. }
  127. if err == nil {
  128. var node data.Node
  129. part := fmt.Sprint(args[0])
  130. key := fmt.Sprint(args[1])
  131. kind := fmt.Sprint(args[2])
  132. conv := func(m map[string]interface{}) map[interface{}]interface{} {
  133. c := make(map[interface{}]interface{})
  134. for k, v := range m {
  135. c[k] = v
  136. }
  137. return c
  138. }
  139. // Fetch the node
  140. if node, err = f.GM.FetchEdge(part, key, kind); node != nil {
  141. res = conv(node.Data())
  142. }
  143. }
  144. return res, err
  145. }
  146. /*
  147. DocString returns a descriptive string.
  148. */
  149. func (f *FetchEdgeFunc) DocString() (string, error) {
  150. return "Fetches an edge in EliasDB.", nil
  151. }
  152. /*
  153. TraverseFunc traverses an edge in EliasDB.
  154. */
  155. type TraverseFunc struct {
  156. GM *graph.Manager
  157. }
  158. /*
  159. Run executes the ECAL function.
  160. */
  161. func (f *TraverseFunc) Run(instanceID string, vs parser.Scope, is map[string]interface{}, tid uint64, args []interface{}) (interface{}, error) {
  162. var res interface{}
  163. var err error
  164. if arglen := len(args); arglen != 4 {
  165. err = fmt.Errorf("Function requires 4 parameters: partition, node key," +
  166. " node kind and a traversal spec")
  167. }
  168. if err == nil {
  169. var nodes []data.Node
  170. var edges []data.Edge
  171. part := fmt.Sprint(args[0])
  172. key := fmt.Sprint(args[1])
  173. kind := fmt.Sprint(args[2])
  174. spec := fmt.Sprint(args[3])
  175. conv := func(m map[string]interface{}) map[interface{}]interface{} {
  176. c := make(map[interface{}]interface{})
  177. for k, v := range m {
  178. c[k] = v
  179. }
  180. return c
  181. }
  182. // Do the traversal
  183. if nodes, edges, err = f.GM.TraverseMulti(part, key, kind, spec, true); err == nil {
  184. resNodes := make([]interface{}, len(nodes))
  185. for i, n := range nodes {
  186. resNodes[i] = conv(n.Data())
  187. }
  188. resEdges := make([]interface{}, len(edges))
  189. for i, e := range edges {
  190. resEdges[i] = conv(e.Data())
  191. }
  192. res = []interface{}{resNodes, resEdges}
  193. }
  194. }
  195. return res, err
  196. }
  197. /*
  198. DocString returns a descriptive string.
  199. */
  200. func (f *TraverseFunc) DocString() (string, error) {
  201. return "Traverses an edge in EliasDB from a given node.", nil
  202. }