node.go 5.3 KB

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