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.

110 lines
2.7 KiB

  1. // Copyright 2020 Andrew Thornton. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package levelqueue
  5. import (
  6. "sync"
  7. "github.com/syndtr/goleveldb/leveldb"
  8. "github.com/syndtr/goleveldb/leveldb/util"
  9. )
  10. const (
  11. setPrefixStr = "set"
  12. )
  13. // Set defines a set struct
  14. type Set struct {
  15. db *leveldb.DB
  16. closeUnderlyingDB bool
  17. lock sync.Mutex
  18. prefix []byte
  19. }
  20. // OpenSet opens a set from the db path or creates a set if it doesn't exist.
  21. // The keys will be prefixed with "set-" by default
  22. func OpenSet(dataDir string) (*Set, error) {
  23. db, err := leveldb.OpenFile(dataDir, nil)
  24. if err != nil {
  25. return nil, err
  26. }
  27. return NewSet(db, []byte(setPrefixStr), true)
  28. }
  29. // NewSet creates a set from a db. The keys will be prefixed with prefix
  30. // and at close the db will be closed as per closeUnderlyingDB
  31. func NewSet(db *leveldb.DB, prefix []byte, closeUnderlyingDB bool) (*Set, error) {
  32. set := &Set{
  33. db: db,
  34. closeUnderlyingDB: closeUnderlyingDB,
  35. }
  36. set.prefix = make([]byte, len(prefix))
  37. copy(set.prefix, prefix)
  38. return set, nil
  39. }
  40. // Add adds a member string to a key set, returns true if the member was not already present
  41. func (set *Set) Add(value []byte) (bool, error) {
  42. set.lock.Lock()
  43. defer set.lock.Unlock()
  44. setKey := withPrefix(set.prefix, value)
  45. has, err := set.db.Has(setKey, nil)
  46. if err != nil || has {
  47. return !has, err
  48. }
  49. return !has, set.db.Put(setKey, []byte(""), nil)
  50. }
  51. // Members returns the current members of the set
  52. func (set *Set) Members() ([][]byte, error) {
  53. set.lock.Lock()
  54. defer set.lock.Unlock()
  55. var members [][]byte
  56. prefix := withPrefix(set.prefix, []byte{})
  57. iter := set.db.NewIterator(util.BytesPrefix(prefix), nil)
  58. for iter.Next() {
  59. slice := iter.Key()[len(prefix):]
  60. value := make([]byte, len(slice))
  61. copy(value, slice)
  62. members = append(members, value)
  63. }
  64. iter.Release()
  65. return members, iter.Error()
  66. }
  67. // Has returns if the member is in the set
  68. func (set *Set) Has(value []byte) (bool, error) {
  69. set.lock.Lock()
  70. defer set.lock.Unlock()
  71. setKey := withPrefix(set.prefix, value)
  72. return set.db.Has(setKey, nil)
  73. }
  74. // Remove removes a member from the set, returns true if the member was present
  75. func (set *Set) Remove(value []byte) (bool, error) {
  76. set.lock.Lock()
  77. defer set.lock.Unlock()
  78. setKey := withPrefix(set.prefix, value)
  79. has, err := set.db.Has(setKey, nil)
  80. if err != nil || !has {
  81. return has, err
  82. }
  83. return has, set.db.Delete(setKey, nil)
  84. }
  85. // Close closes the set (and the underlying db if set to closeUnderlyingDB)
  86. func (set *Set) Close() error {
  87. if !set.closeUnderlyingDB {
  88. set.db = nil
  89. return nil
  90. }
  91. err := set.db.Close()
  92. set.db = nil
  93. return err
  94. }