2020-01-07 16:53:09 +05:30
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2020-01-07 16:53:09 +05:30
|
|
|
|
|
|
|
package queue
|
|
|
|
|
|
|
|
import (
|
2021-05-15 19:52:26 +05:30
|
|
|
"context"
|
|
|
|
|
2020-09-28 02:39:46 +05:30
|
|
|
"code.gitea.io/gitea/modules/nosql"
|
|
|
|
|
2020-01-07 16:53:09 +05:30
|
|
|
"gitea.com/lunny/levelqueue"
|
|
|
|
)
|
|
|
|
|
|
|
|
// LevelQueueType is the type for level queue
|
|
|
|
const LevelQueueType Type = "level"
|
|
|
|
|
|
|
|
// LevelQueueConfiguration is the configuration for a LevelQueue
|
|
|
|
type LevelQueueConfiguration struct {
|
2020-02-03 04:49:58 +05:30
|
|
|
ByteFIFOQueueConfiguration
|
2020-09-28 02:39:46 +05:30
|
|
|
DataDir string
|
|
|
|
ConnectionString string
|
|
|
|
QueueName string
|
2020-01-07 16:53:09 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// LevelQueue implements a disk library queue
|
|
|
|
type LevelQueue struct {
|
2020-02-03 04:49:58 +05:30
|
|
|
*ByteFIFOQueue
|
2020-01-07 16:53:09 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// NewLevelQueue creates a ledis local queue
|
|
|
|
func NewLevelQueue(handle HandlerFunc, cfg, exemplar interface{}) (Queue, error) {
|
|
|
|
configInterface, err := toConfig(LevelQueueConfiguration{}, cfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
config := configInterface.(LevelQueueConfiguration)
|
|
|
|
|
2020-09-28 02:39:46 +05:30
|
|
|
if len(config.ConnectionString) == 0 {
|
|
|
|
config.ConnectionString = config.DataDir
|
|
|
|
}
|
2021-05-15 19:52:26 +05:30
|
|
|
config.WaitOnEmpty = true
|
2020-09-28 02:39:46 +05:30
|
|
|
|
|
|
|
byteFIFO, err := NewLevelQueueByteFIFO(config.ConnectionString, config.QueueName)
|
2020-02-03 04:49:58 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
byteFIFOQueue, err := NewByteFIFOQueue(LevelQueueType, byteFIFO, handle, config.ByteFIFOQueueConfiguration, exemplar)
|
2020-01-07 16:53:09 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
queue := &LevelQueue{
|
2020-02-03 04:49:58 +05:30
|
|
|
ByteFIFOQueue: byteFIFOQueue,
|
2020-01-07 16:53:09 +05:30
|
|
|
}
|
2020-01-29 06:31:06 +05:30
|
|
|
queue.qid = GetManager().Add(queue, LevelQueueType, config, exemplar)
|
2020-01-07 16:53:09 +05:30
|
|
|
return queue, nil
|
|
|
|
}
|
|
|
|
|
2021-04-09 13:10:34 +05:30
|
|
|
var _ ByteFIFO = &LevelQueueByteFIFO{}
|
2020-01-07 16:53:09 +05:30
|
|
|
|
2020-02-03 04:49:58 +05:30
|
|
|
// LevelQueueByteFIFO represents a ByteFIFO formed from a LevelQueue
|
|
|
|
type LevelQueueByteFIFO struct {
|
2020-09-28 02:39:46 +05:30
|
|
|
internal *levelqueue.Queue
|
|
|
|
connection string
|
2020-01-07 16:53:09 +05:30
|
|
|
}
|
|
|
|
|
2020-02-03 04:49:58 +05:30
|
|
|
// NewLevelQueueByteFIFO creates a ByteFIFO formed from a LevelQueue
|
2020-09-28 02:39:46 +05:30
|
|
|
func NewLevelQueueByteFIFO(connection, prefix string) (*LevelQueueByteFIFO, error) {
|
|
|
|
db, err := nosql.GetManager().GetLevelDB(connection)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
internal, err := levelqueue.NewQueue(db, []byte(prefix), false)
|
2020-01-07 16:53:09 +05:30
|
|
|
if err != nil {
|
2020-02-03 04:49:58 +05:30
|
|
|
return nil, err
|
2020-01-07 16:53:09 +05:30
|
|
|
}
|
2020-02-03 04:49:58 +05:30
|
|
|
|
|
|
|
return &LevelQueueByteFIFO{
|
2020-09-28 02:39:46 +05:30
|
|
|
connection: connection,
|
|
|
|
internal: internal,
|
2020-02-03 04:49:58 +05:30
|
|
|
}, nil
|
2020-01-07 16:53:09 +05:30
|
|
|
}
|
|
|
|
|
2020-02-03 04:49:58 +05:30
|
|
|
// PushFunc will push data into the fifo
|
2021-05-15 19:52:26 +05:30
|
|
|
func (fifo *LevelQueueByteFIFO) PushFunc(ctx context.Context, data []byte, fn func() error) error {
|
2020-02-03 04:49:58 +05:30
|
|
|
if fn != nil {
|
|
|
|
if err := fn(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-29 06:31:06 +05:30
|
|
|
}
|
2020-02-03 04:49:58 +05:30
|
|
|
return fifo.internal.LPush(data)
|
2020-01-29 06:31:06 +05:30
|
|
|
}
|
|
|
|
|
2022-01-23 02:52:14 +05:30
|
|
|
// PushBack pushes data to the top of the fifo
|
|
|
|
func (fifo *LevelQueueByteFIFO) PushBack(ctx context.Context, data []byte) error {
|
|
|
|
return fifo.internal.RPush(data)
|
|
|
|
}
|
|
|
|
|
2020-02-03 04:49:58 +05:30
|
|
|
// Pop pops data from the start of the fifo
|
2021-05-15 19:52:26 +05:30
|
|
|
func (fifo *LevelQueueByteFIFO) Pop(ctx context.Context) ([]byte, error) {
|
2020-02-03 04:49:58 +05:30
|
|
|
data, err := fifo.internal.RPop()
|
|
|
|
if err != nil && err != levelqueue.ErrNotFound {
|
|
|
|
return nil, err
|
2020-01-07 16:53:09 +05:30
|
|
|
}
|
2020-02-03 04:49:58 +05:30
|
|
|
return data, nil
|
2020-01-07 16:53:09 +05:30
|
|
|
}
|
|
|
|
|
2020-02-03 04:49:58 +05:30
|
|
|
// Close this fifo
|
|
|
|
func (fifo *LevelQueueByteFIFO) Close() error {
|
2020-09-28 02:39:46 +05:30
|
|
|
err := fifo.internal.Close()
|
|
|
|
_ = nosql.GetManager().CloseLevelDB(fifo.connection)
|
|
|
|
return err
|
2020-01-07 16:53:09 +05:30
|
|
|
}
|
|
|
|
|
2020-02-03 04:49:58 +05:30
|
|
|
// Len returns the length of the fifo
|
2021-05-15 19:52:26 +05:30
|
|
|
func (fifo *LevelQueueByteFIFO) Len(ctx context.Context) int64 {
|
2020-02-03 04:49:58 +05:30
|
|
|
return fifo.internal.Len()
|
2020-01-07 16:53:09 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
queuesMap[LevelQueueType] = NewLevelQueue
|
|
|
|
}
|