code.gitea.io/gitea@v1.22.3/modules/queue/lqinternal/lqinternal.go (about)

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