z.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 | uint | 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 uint:
  33. return uint64(k), 0
  34. case int:
  35. return uint64(k), 0
  36. case int32:
  37. return uint64(k), 0
  38. case uint32:
  39. return uint64(k), 0
  40. case int64:
  41. return uint64(k), 0
  42. default:
  43. panic("Key type not supported")
  44. }
  45. }
  46. var (
  47. dummyCloserChan <-chan struct{}
  48. tmpDir string
  49. )
  50. // Closer holds the two things we need to close a goroutine and wait for it to
  51. // finish: a chan to tell the goroutine to shut down, and a WaitGroup with
  52. // which to wait for it to finish shutting down.
  53. type Closer struct {
  54. waiting sync.WaitGroup
  55. ctx context.Context
  56. cancel context.CancelFunc
  57. }
  58. // SetTmpDir sets the temporary directory for the temporary buffers.
  59. func SetTmpDir(dir string) {
  60. tmpDir = dir
  61. }
  62. // NewCloser constructs a new Closer, with an initial count on the WaitGroup.
  63. func NewCloser(initial int) *Closer {
  64. ret := &Closer{}
  65. ret.ctx, ret.cancel = context.WithCancel(context.Background())
  66. ret.waiting.Add(initial)
  67. return ret
  68. }
  69. // AddRunning Add()'s delta to the WaitGroup.
  70. func (lc *Closer) AddRunning(delta int) {
  71. lc.waiting.Add(delta)
  72. }
  73. // Ctx can be used to get a context, which would automatically get cancelled when Signal is called.
  74. func (lc *Closer) Ctx() context.Context {
  75. if lc == nil {
  76. return context.Background()
  77. }
  78. return lc.ctx
  79. }
  80. // Signal signals the HasBeenClosed signal.
  81. func (lc *Closer) Signal() {
  82. // Todo(ibrahim): Change Signal to return error on next badger breaking change.
  83. lc.cancel()
  84. }
  85. // HasBeenClosed gets signaled when Signal() is called.
  86. func (lc *Closer) HasBeenClosed() <-chan struct{} {
  87. if lc == nil {
  88. return dummyCloserChan
  89. }
  90. return lc.ctx.Done()
  91. }
  92. // Done calls Done() on the WaitGroup.
  93. func (lc *Closer) Done() {
  94. if lc == nil {
  95. return
  96. }
  97. lc.waiting.Done()
  98. }
  99. // Wait waits on the WaitGroup. (It waits for NewCloser's initial value, AddRunning, and Done
  100. // calls to balance out.)
  101. func (lc *Closer) Wait() {
  102. lc.waiting.Wait()
  103. }
  104. // SignalAndWait calls Signal(), then Wait().
  105. func (lc *Closer) SignalAndWait() {
  106. lc.Signal()
  107. lc.Wait()
  108. }
  109. // ZeroOut zeroes out all the bytes in the range [start, end).
  110. func ZeroOut(dst []byte, start, end int) {
  111. if start < 0 || start >= len(dst) {
  112. return // BAD
  113. }
  114. if end >= len(dst) {
  115. end = len(dst)
  116. }
  117. if end-start <= 0 {
  118. return
  119. }
  120. Memclr(dst[start:end])
  121. // b := dst[start:end]
  122. // for i := range b {
  123. // b[i] = 0x0
  124. // }
  125. }