z.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * SPDX-FileCopyrightText: © Hypermode Inc. <hello@hypermode.com>
  3. * SPDX-License-Identifier: Apache-2.0
  4. */
  5. package z
  6. import (
  7. "context"
  8. "sync"
  9. "github.com/cespare/xxhash/v2"
  10. )
  11. type Key interface {
  12. uint64 | string | []byte | byte | int | int32 | uint32 | int64
  13. }
  14. // TODO: Figure out a way to re-use memhash for the second uint64 hash,
  15. // we already know that appending bytes isn't reliable for generating a
  16. // second hash (see Ristretto PR #88).
  17. // We also know that while the Go runtime has a runtime memhash128
  18. // function, it's not possible to use it to generate [2]uint64 or
  19. // anything resembling a 128bit hash, even though that's exactly what
  20. // we need in this situation.
  21. func KeyToHash[K Key](key K) (uint64, uint64) {
  22. keyAsAny := any(key)
  23. switch k := keyAsAny.(type) {
  24. case uint64:
  25. return k, 0
  26. case string:
  27. return MemHashString(k), xxhash.Sum64String(k)
  28. case []byte:
  29. return MemHash(k), xxhash.Sum64(k)
  30. case byte:
  31. return uint64(k), 0
  32. case int:
  33. return uint64(k), 0
  34. case int32:
  35. return uint64(k), 0
  36. case uint32:
  37. return uint64(k), 0
  38. case int64:
  39. return uint64(k), 0
  40. default:
  41. panic("Key type not supported")
  42. }
  43. }
  44. var (
  45. dummyCloserChan <-chan struct{}
  46. tmpDir string
  47. )
  48. // Closer holds the two things we need to close a goroutine and wait for it to
  49. // finish: a chan to tell the goroutine to shut down, and a WaitGroup with
  50. // which to wait for it to finish shutting down.
  51. type Closer struct {
  52. waiting sync.WaitGroup
  53. ctx context.Context
  54. cancel context.CancelFunc
  55. }
  56. // SetTmpDir sets the temporary directory for the temporary buffers.
  57. func SetTmpDir(dir string) {
  58. tmpDir = dir
  59. }
  60. // NewCloser constructs a new Closer, with an initial count on the WaitGroup.
  61. func NewCloser(initial int) *Closer {
  62. ret := &Closer{}
  63. ret.ctx, ret.cancel = context.WithCancel(context.Background())
  64. ret.waiting.Add(initial)
  65. return ret
  66. }
  67. // AddRunning Add()'s delta to the WaitGroup.
  68. func (lc *Closer) AddRunning(delta int) {
  69. lc.waiting.Add(delta)
  70. }
  71. // Ctx can be used to get a context, which would automatically get cancelled when Signal is called.
  72. func (lc *Closer) Ctx() context.Context {
  73. if lc == nil {
  74. return context.Background()
  75. }
  76. return lc.ctx
  77. }
  78. // Signal signals the HasBeenClosed signal.
  79. func (lc *Closer) Signal() {
  80. // Todo(ibrahim): Change Signal to return error on next badger breaking change.
  81. lc.cancel()
  82. }
  83. // HasBeenClosed gets signaled when Signal() is called.
  84. func (lc *Closer) HasBeenClosed() <-chan struct{} {
  85. if lc == nil {
  86. return dummyCloserChan
  87. }
  88. return lc.ctx.Done()
  89. }
  90. // Done calls Done() on the WaitGroup.
  91. func (lc *Closer) Done() {
  92. if lc == nil {
  93. return
  94. }
  95. lc.waiting.Done()
  96. }
  97. // Wait waits on the WaitGroup. (It waits for NewCloser's initial value, AddRunning, and Done
  98. // calls to balance out.)
  99. func (lc *Closer) Wait() {
  100. lc.waiting.Wait()
  101. }
  102. // SignalAndWait calls Signal(), then Wait().
  103. func (lc *Closer) SignalAndWait() {
  104. lc.Signal()
  105. lc.Wait()
  106. }
  107. // ZeroOut zeroes out all the bytes in the range [start, end).
  108. func ZeroOut(dst []byte, start, end int) {
  109. if start < 0 || start >= len(dst) {
  110. return // BAD
  111. }
  112. if end >= len(dst) {
  113. end = len(dst)
  114. }
  115. if end-start <= 0 {
  116. return
  117. }
  118. Memclr(dst[start:end])
  119. // b := dst[start:end]
  120. // for i := range b {
  121. // b[i] = 0x0
  122. // }
  123. }