You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
1.9 KiB

  1. package kv
  2. import (
  3. "encoding/binary"
  4. "fmt"
  5. "strconv"
  6. "github.com/google/orderedcode"
  7. "github.com/tendermint/tendermint/internal/pubsub/query/syntax"
  8. "github.com/tendermint/tendermint/types"
  9. )
  10. func intInSlice(a int, list []int) bool {
  11. for _, b := range list {
  12. if b == a {
  13. return true
  14. }
  15. }
  16. return false
  17. }
  18. func int64FromBytes(bz []byte) int64 {
  19. v, _ := binary.Varint(bz)
  20. return v
  21. }
  22. func int64ToBytes(i int64) []byte {
  23. buf := make([]byte, binary.MaxVarintLen64)
  24. n := binary.PutVarint(buf, i)
  25. return buf[:n]
  26. }
  27. func heightKey(height int64) ([]byte, error) {
  28. return orderedcode.Append(
  29. nil,
  30. types.BlockHeightKey,
  31. height,
  32. )
  33. }
  34. func eventKey(compositeKey, typ, eventValue string, height int64) ([]byte, error) {
  35. return orderedcode.Append(
  36. nil,
  37. compositeKey,
  38. eventValue,
  39. height,
  40. typ,
  41. )
  42. }
  43. func parseValueFromPrimaryKey(key []byte) (string, error) {
  44. var (
  45. compositeKey string
  46. height int64
  47. )
  48. remaining, err := orderedcode.Parse(string(key), &compositeKey, &height)
  49. if err != nil {
  50. return "", fmt.Errorf("failed to parse event key: %w", err)
  51. }
  52. if len(remaining) != 0 {
  53. return "", fmt.Errorf("unexpected remainder in key: %s", remaining)
  54. }
  55. return strconv.FormatInt(height, 10), nil
  56. }
  57. func parseValueFromEventKey(key []byte) (string, error) {
  58. var (
  59. compositeKey, typ, eventValue string
  60. height int64
  61. )
  62. remaining, err := orderedcode.Parse(string(key), &compositeKey, &eventValue, &height, &typ)
  63. if err != nil {
  64. return "", fmt.Errorf("failed to parse event key: %w", err)
  65. }
  66. if len(remaining) != 0 {
  67. return "", fmt.Errorf("unexpected remainder in key: %s", remaining)
  68. }
  69. return eventValue, nil
  70. }
  71. func lookForHeight(conditions []syntax.Condition) (int64, bool) {
  72. for _, c := range conditions {
  73. if c.Tag == types.BlockHeightKey && c.Op == syntax.TEq {
  74. return int64(c.Arg.Number()), true
  75. }
  76. }
  77. return 0, false
  78. }