trans.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. "strconv"
  14. "devt.de/krotik/ecal/parser"
  15. "devt.de/krotik/eliasdb/graph"
  16. )
  17. /*
  18. NewTransFunc creates a new transaction for EliasDB.
  19. */
  20. type NewTransFunc struct {
  21. GM *graph.Manager
  22. }
  23. /*
  24. Run executes the ECAL function.
  25. */
  26. func (f *NewTransFunc) Run(instanceID string, vs parser.Scope, is map[string]interface{}, tid uint64, args []interface{}) (interface{}, error) {
  27. var err error
  28. if len(args) != 0 {
  29. err = fmt.Errorf("Function does not require any parameters")
  30. }
  31. return graph.NewConcurrentGraphTrans(f.GM), err
  32. }
  33. /*
  34. DocString returns a descriptive string.
  35. */
  36. func (f *NewTransFunc) DocString() (string, error) {
  37. return "Creates a new transaction for EliasDB.", nil
  38. }
  39. /*
  40. NewRollingTransFunc creates a new rolling transaction for EliasDB.
  41. A rolling transaction commits after n entries.
  42. */
  43. type NewRollingTransFunc struct {
  44. GM *graph.Manager
  45. }
  46. /*
  47. Run executes the ECAL function.
  48. */
  49. func (f *NewRollingTransFunc) Run(instanceID string, vs parser.Scope, is map[string]interface{}, tid uint64, args []interface{}) (interface{}, error) {
  50. var err error
  51. var trans graph.Trans
  52. if arglen := len(args); arglen != 1 {
  53. err = fmt.Errorf(
  54. "Function requires the rolling threshold (number of operations before rolling)")
  55. }
  56. if err == nil {
  57. var i int
  58. if i, err = strconv.Atoi(fmt.Sprint(args[0])); err != nil {
  59. err = fmt.Errorf("Rolling threshold must be a number not: %v", args[0])
  60. } else {
  61. trans = graph.NewRollingTrans(graph.NewConcurrentGraphTrans(f.GM),
  62. i, f.GM, graph.NewConcurrentGraphTrans)
  63. }
  64. }
  65. return trans, err
  66. }
  67. /*
  68. DocString returns a descriptive string.
  69. */
  70. func (f *NewRollingTransFunc) DocString() (string, error) {
  71. return "Creates a new rolling transaction for EliasDB. A rolling transaction commits after n entries.", nil
  72. }
  73. /*
  74. CommitTransFunc commits an existing transaction for EliasDB.
  75. */
  76. type CommitTransFunc struct {
  77. GM *graph.Manager
  78. }
  79. /*
  80. Run executes the ECAL function.
  81. */
  82. func (f *CommitTransFunc) Run(instanceID string, vs parser.Scope, is map[string]interface{}, tid uint64, args []interface{}) (interface{}, error) {
  83. var err error
  84. if arglen := len(args); arglen != 1 {
  85. err = fmt.Errorf(
  86. "Function requires the transaction to commit as parameter")
  87. }
  88. if err == nil {
  89. trans, ok := args[0].(graph.Trans)
  90. // Check parameters
  91. if !ok {
  92. err = fmt.Errorf("Parameter must be a transaction")
  93. } else {
  94. err = trans.Commit()
  95. }
  96. }
  97. return nil, err
  98. }
  99. /*
  100. DocString returns a descriptive string.
  101. */
  102. func (f *CommitTransFunc) DocString() (string, error) {
  103. return "Commits an existing transaction for EliasDB.", nil
  104. }