node.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 or updates 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 or updates a node in EliasDB.", nil
  62. }
  63. /*
  64. RemoveNodeFunc removes a node in EliasDB.
  65. */
  66. type RemoveNodeFunc struct {
  67. GM *graph.Manager
  68. }
  69. /*
  70. Run executes the ECAL function.
  71. */
  72. func (f *RemoveNodeFunc) 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 != 3 && arglen != 4 {
  75. err = fmt.Errorf("Function requires 3 or 4 parameters: partition, node key" +
  76. " node kind and optionally a transaction")
  77. }
  78. if err == nil {
  79. var trans graph.Trans
  80. part := fmt.Sprint(args[0])
  81. key := fmt.Sprint(args[1])
  82. kind := fmt.Sprint(args[2])
  83. // Check parameters
  84. if len(args) > 3 {
  85. var ok bool
  86. if trans, ok = args[3].(graph.Trans); !ok {
  87. err = fmt.Errorf("Fourth parameter must be a transaction")
  88. }
  89. }
  90. // Remove the node
  91. if err == nil {
  92. if trans != nil {
  93. err = trans.RemoveNode(part, key, kind)
  94. } else {
  95. _, err = f.GM.RemoveNode(part, key, kind)
  96. }
  97. }
  98. }
  99. return nil, err
  100. }
  101. /*
  102. DocString returns a descriptive string.
  103. */
  104. func (f *RemoveNodeFunc) DocString() (string, error) {
  105. return "Removes a node in EliasDB.", nil
  106. }
  107. /*
  108. FetchNodeFunc fetches a node in EliasDB.
  109. */
  110. type FetchNodeFunc struct {
  111. GM *graph.Manager
  112. }
  113. /*
  114. Run executes the ECAL function.
  115. */
  116. func (f *FetchNodeFunc) Run(instanceID string, vs parser.Scope, is map[string]interface{}, tid uint64, args []interface{}) (interface{}, error) {
  117. var res interface{}
  118. var err error
  119. if arglen := len(args); arglen != 3 {
  120. err = fmt.Errorf("Function requires 3 parameters: partition, node key" +
  121. " node kind")
  122. }
  123. if err == nil {
  124. var node data.Node
  125. part := fmt.Sprint(args[0])
  126. key := fmt.Sprint(args[1])
  127. kind := fmt.Sprint(args[2])
  128. conv := func(m map[string]interface{}) map[interface{}]interface{} {
  129. c := make(map[interface{}]interface{})
  130. for k, v := range m {
  131. c[k] = v
  132. }
  133. return c
  134. }
  135. // Fetch the node
  136. if node, err = f.GM.FetchNode(part, key, kind); node != nil {
  137. res = conv(node.Data())
  138. }
  139. }
  140. return res, err
  141. }
  142. /*
  143. DocString returns a descriptive string.
  144. */
  145. func (f *FetchNodeFunc) DocString() (string, error) {
  146. return "Fetches a node in EliasDB.", nil
  147. }
  148. // Helper functions
  149. // ================
  150. /*
  151. NewGraphNodeFromECALMap creates a new Node instance from a given map.
  152. */
  153. func NewGraphNodeFromECALMap(d map[interface{}]interface{}) data.Node {
  154. node := data.NewGraphNode()
  155. for k, v := range d {
  156. node.SetAttr(fmt.Sprint(k), v)
  157. }
  158. return node
  159. }