trans.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 rumble
  11. import (
  12. "fmt"
  13. "strconv"
  14. "devt.de/krotik/common/defs/rumble"
  15. "devt.de/krotik/eliasdb/api"
  16. "devt.de/krotik/eliasdb/graph"
  17. )
  18. // Function: newTrans
  19. // ==================
  20. /*
  21. NewTransFunc creates a new transaction for EliasDB.
  22. */
  23. type NewTransFunc struct {
  24. }
  25. /*
  26. Name returns the name of the function.
  27. */
  28. func (f *NewTransFunc) Name() string {
  29. return "db.newTrans"
  30. }
  31. /*
  32. Validate is called for parameter validation and to reset the function state.
  33. */
  34. func (f *NewTransFunc) Validate(argsNum int, rt rumble.Runtime) rumble.RuntimeError {
  35. var err rumble.RuntimeError
  36. if argsNum != 0 {
  37. err = rt.NewRuntimeError(rumble.ErrInvalidConstruct,
  38. "Function newTrans does not require any parameters")
  39. }
  40. return err
  41. }
  42. /*
  43. Execute executes the rumble function.
  44. */
  45. func (f *NewTransFunc) Execute(argsVal []interface{}, vars rumble.Variables,
  46. rt rumble.Runtime) (interface{}, rumble.RuntimeError) {
  47. return graph.NewConcurrentGraphTrans(api.GM), nil
  48. }
  49. // Function: newRollingTrans
  50. // =========================
  51. /*
  52. NewRollingTransFunc creates a new rolling transaction for EliasDB.
  53. A rolling transaction commits after n entries.
  54. */
  55. type NewRollingTransFunc struct {
  56. }
  57. /*
  58. Name returns the name of the function.
  59. */
  60. func (f *NewRollingTransFunc) Name() string {
  61. return "db.newRollingTrans"
  62. }
  63. /*
  64. Validate is called for parameter validation and to reset the function state.
  65. */
  66. func (f *NewRollingTransFunc) Validate(argsNum int, rt rumble.Runtime) rumble.RuntimeError {
  67. var err rumble.RuntimeError
  68. if argsNum != 1 {
  69. err = rt.NewRuntimeError(rumble.ErrInvalidConstruct,
  70. "Function newRollingTrans requires the rolling threshold (number of operations before rolling)")
  71. }
  72. return err
  73. }
  74. /*
  75. Execute executes the rumble function.
  76. */
  77. func (f *NewRollingTransFunc) Execute(argsVal []interface{}, vars rumble.Variables,
  78. rt rumble.Runtime) (interface{}, rumble.RuntimeError) {
  79. i, err := strconv.Atoi(fmt.Sprint(argsVal[0]))
  80. if err != nil {
  81. return nil, rt.NewRuntimeError(rumble.ErrNotANumber,
  82. fmt.Sprintf("Rolling threshold must be a number not: %v", argsVal[0]))
  83. }
  84. trans := graph.NewRollingTrans(graph.NewConcurrentGraphTrans(api.GM),
  85. i, api.GM, graph.NewConcurrentGraphTrans)
  86. return trans, nil
  87. }
  88. // Function: commitTrans
  89. // =====================
  90. /*
  91. CommitTransFunc commits an existing transaction for EliasDB.
  92. */
  93. type CommitTransFunc struct {
  94. }
  95. /*
  96. Name returns the name of the function.
  97. */
  98. func (f *CommitTransFunc) Name() string {
  99. return "db.commitTrans"
  100. }
  101. /*
  102. Validate is called for parameter validation and to reset the function state.
  103. */
  104. func (f *CommitTransFunc) Validate(argsNum int, rt rumble.Runtime) rumble.RuntimeError {
  105. var err rumble.RuntimeError
  106. if argsNum != 1 {
  107. err = rt.NewRuntimeError(rumble.ErrInvalidConstruct,
  108. "Function commitTrans requires the transaction to commit as parameter")
  109. }
  110. return err
  111. }
  112. /*
  113. Execute executes the rumble function.
  114. */
  115. func (f *CommitTransFunc) Execute(argsVal []interface{}, vars rumble.Variables,
  116. rt rumble.Runtime) (interface{}, rumble.RuntimeError) {
  117. var err rumble.RuntimeError
  118. trans, ok := argsVal[0].(graph.Trans)
  119. // Check parameters
  120. if !ok {
  121. err = rt.NewRuntimeError(rumble.ErrInvalidConstruct,
  122. "Parameter must be a transaction")
  123. }
  124. if err == nil {
  125. if err = trans.Commit(); err != nil {
  126. err = rt.NewRuntimeError(rumble.ErrInvalidConstruct,
  127. fmt.Sprintf("Cannot store node: %v", err.Error()))
  128. }
  129. }
  130. return nil, err
  131. }