node.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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: storeNode
  22. // ===================
  23. /*
  24. StoreNodeFunc inserts or updates a node in EliasDB.
  25. */
  26. type StoreNodeFunc struct {
  27. }
  28. /*
  29. Name returns the name of the function.
  30. */
  31. func (f *StoreNodeFunc) Name() string {
  32. return "db.storeNode"
  33. }
  34. /*
  35. Validate is called for parameter validation and to reset the function state.
  36. */
  37. func (f *StoreNodeFunc) 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 storeNode requires 2 or 3 parameters: partition, node"+
  42. " map and optionally a transaction")
  43. }
  44. return err
  45. }
  46. /*
  47. Execute executes the rumble function.
  48. */
  49. func (f *StoreNodeFunc) 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. node := NewGraphNodeFromRumbleMap(nodeMap)
  68. // Store the node
  69. if err == nil {
  70. if trans != nil {
  71. err = trans.StoreNode(part, node)
  72. } else {
  73. err = api.GM.StoreNode(part, node)
  74. }
  75. if err != nil {
  76. // Wrap error message in RuntimeError
  77. err = rt.NewRuntimeError(rumble.ErrInvalidState,
  78. fmt.Sprintf("Cannot store node: %v", err.Error()))
  79. }
  80. }
  81. return nil, err
  82. }
  83. // Function: removeNode
  84. // ====================
  85. /*
  86. RemoveNodeFunc removes a node in EliasDB.
  87. */
  88. type RemoveNodeFunc struct {
  89. }
  90. /*
  91. Name returns the name of the function.
  92. */
  93. func (f *RemoveNodeFunc) Name() string {
  94. return "db.removeNode"
  95. }
  96. /*
  97. Validate is called for parameter validation and to reset the function state.
  98. */
  99. func (f *RemoveNodeFunc) 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 removeNode requires 3 or 4 parameters: partition, node key"+
  104. " node kind and optionally a transaction")
  105. }
  106. return err
  107. }
  108. /*
  109. Execute executes the rumble function.
  110. */
  111. func (f *RemoveNodeFunc) 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 node
  127. if err == nil {
  128. if trans != nil {
  129. err = trans.RemoveNode(part, key, kind)
  130. } else {
  131. _, err = api.GM.RemoveNode(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 node: %v", err.Error()))
  137. }
  138. }
  139. return nil, err
  140. }
  141. // Function: fetchNode
  142. // ===================
  143. /*
  144. FetchNodeFunc fetches a node in EliasDB.
  145. */
  146. type FetchNodeFunc struct {
  147. }
  148. /*
  149. Name returns the name of the function.
  150. */
  151. func (f *FetchNodeFunc) Name() string {
  152. return "db.fetchNode"
  153. }
  154. /*
  155. Validate is called for parameter validation and to reset the function state.
  156. */
  157. func (f *FetchNodeFunc) 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 fetchNode requires 3 parameters: partition, node key"+
  162. " node kind")
  163. }
  164. return err
  165. }
  166. /*
  167. Execute executes the rumble function.
  168. */
  169. func (f *FetchNodeFunc) 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.FetchNode(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 node: %v", err.Error()))
  192. }
  193. return res, err
  194. }
  195. // Helper functions
  196. // ================
  197. /*
  198. NewGraphNodeFromRumbleMap creates a new Node instance.
  199. */
  200. func NewGraphNodeFromRumbleMap(d map[interface{}]interface{}) data.Node {
  201. node := data.NewGraphNode()
  202. for k, v := range d {
  203. node.SetAttr(fmt.Sprint(k), v)
  204. }
  205. return node
  206. }