gitea源码

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package lqinternal
  4. import (
  5. "bytes"
  6. "encoding/binary"
  7. "github.com/syndtr/goleveldb/leveldb"
  8. "github.com/syndtr/goleveldb/leveldb/opt"
  9. )
  10. func QueueItemIDBytes(id int64) []byte {
  11. buf := make([]byte, 8)
  12. binary.PutVarint(buf, id)
  13. return buf
  14. }
  15. func QueueItemKeyBytes(prefix []byte, id int64) []byte {
  16. key := make([]byte, len(prefix), len(prefix)+1+8)
  17. copy(key, prefix)
  18. key = append(key, '-')
  19. return append(key, QueueItemIDBytes(id)...)
  20. }
  21. func RemoveLevelQueueKeys(db *leveldb.DB, namePrefix []byte) {
  22. keyPrefix := make([]byte, len(namePrefix)+1)
  23. copy(keyPrefix, namePrefix)
  24. keyPrefix[len(namePrefix)] = '-'
  25. it := db.NewIterator(nil, &opt.ReadOptions{Strict: opt.NoStrict})
  26. defer it.Release()
  27. for it.Next() {
  28. if bytes.HasPrefix(it.Key(), keyPrefix) {
  29. _ = db.Delete(it.Key(), nil)
  30. }
  31. }
  32. }
  33. func ListLevelQueueKeys(db *leveldb.DB) (res [][]byte) {
  34. it := db.NewIterator(nil, &opt.ReadOptions{Strict: opt.NoStrict})
  35. defer it.Release()
  36. for it.Next() {
  37. res = append(res, it.Key())
  38. }
  39. return res
  40. }