2023-06-07 14:47:00 -04:00
|
|
|
// Copyright (c) 2015-2023 MinIO, Inc.
|
2021-04-18 15:41:13 -04:00
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2019-02-25 21:01:13 -05:00
|
|
|
|
2023-04-04 13:52:24 -04:00
|
|
|
package store
|
2019-02-25 21:01:13 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2023-04-04 13:52:24 -04:00
|
|
|
"errors"
|
2019-02-25 21:01:13 -05:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2019-05-22 16:34:48 -04:00
|
|
|
"sort"
|
2022-11-08 19:36:47 -05:00
|
|
|
"strings"
|
2019-02-25 21:01:13 -05:00
|
|
|
"sync"
|
2022-11-08 19:36:47 -05:00
|
|
|
"time"
|
2019-02-25 21:01:13 -05:00
|
|
|
|
2023-04-04 13:52:24 -04:00
|
|
|
"github.com/google/uuid"
|
2019-02-25 21:01:13 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-04-18 04:20:56 -04:00
|
|
|
defaultLimit = 100000 // Default store limit.
|
2023-04-04 13:52:24 -04:00
|
|
|
defaultExt = ".unknown"
|
2019-02-25 21:01:13 -05:00
|
|
|
)
|
|
|
|
|
2023-04-04 13:52:24 -04:00
|
|
|
// errLimitExceeded error is sent when the maximum limit is reached.
|
|
|
|
var errLimitExceeded = errors.New("the maximum store limit reached")
|
|
|
|
|
|
|
|
// QueueStore - Filestore for persisting items.
|
|
|
|
type QueueStore[_ any] struct {
|
2019-02-25 21:01:13 -05:00
|
|
|
sync.RWMutex
|
2022-11-08 19:36:47 -05:00
|
|
|
entryLimit uint64
|
|
|
|
directory string
|
2023-04-04 13:52:24 -04:00
|
|
|
fileExt string
|
2022-11-08 19:36:47 -05:00
|
|
|
|
|
|
|
entries map[string]int64 // key -> modtime as unix nano
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewQueueStore - Creates an instance for QueueStore.
|
2023-04-04 13:52:24 -04:00
|
|
|
func NewQueueStore[I any](directory string, limit uint64, ext string) *QueueStore[I] {
|
2019-02-25 21:01:13 -05:00
|
|
|
if limit == 0 {
|
2020-04-18 04:20:56 -04:00
|
|
|
limit = defaultLimit
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|
2019-07-11 22:53:20 -04:00
|
|
|
|
2023-04-04 13:52:24 -04:00
|
|
|
if ext == "" {
|
|
|
|
ext = defaultExt
|
|
|
|
}
|
|
|
|
|
|
|
|
return &QueueStore[I]{
|
2019-10-11 20:46:03 -04:00
|
|
|
directory: directory,
|
|
|
|
entryLimit: limit,
|
2023-04-04 13:52:24 -04:00
|
|
|
fileExt: ext,
|
2022-11-08 19:36:47 -05:00
|
|
|
entries: make(map[string]int64, limit),
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open - Creates the directory if not present.
|
2023-04-04 13:52:24 -04:00
|
|
|
func (store *QueueStore[_]) Open() error {
|
2019-02-25 21:01:13 -05:00
|
|
|
store.Lock()
|
|
|
|
defer store.Unlock()
|
|
|
|
|
2022-01-02 12:15:06 -05:00
|
|
|
if err := os.MkdirAll(store.directory, os.FileMode(0o770)); err != nil {
|
2019-10-11 20:46:03 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-08 19:36:47 -05:00
|
|
|
files, err := store.list()
|
2019-10-11 20:46:03 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|
|
|
|
|
2022-11-08 19:36:47 -05:00
|
|
|
// Truncate entries.
|
|
|
|
if uint64(len(files)) > store.entryLimit {
|
|
|
|
files = files[:store.entryLimit]
|
|
|
|
}
|
|
|
|
for _, file := range files {
|
|
|
|
if file.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
2023-04-04 13:52:24 -04:00
|
|
|
key := strings.TrimSuffix(file.Name(), store.fileExt)
|
2022-11-08 19:36:47 -05:00
|
|
|
if fi, err := file.Info(); err == nil {
|
|
|
|
store.entries[key] = fi.ModTime().UnixNano()
|
|
|
|
}
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-04 13:52:24 -04:00
|
|
|
// write - writes an item to the directory.
|
|
|
|
func (store *QueueStore[I]) write(key string, item I) error {
|
|
|
|
// Marshalls the item.
|
|
|
|
eventData, err := json.Marshal(item)
|
2019-02-25 21:01:13 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-04 13:52:24 -04:00
|
|
|
path := filepath.Join(store.directory, key+store.fileExt)
|
2022-09-19 14:05:16 -04:00
|
|
|
if err := os.WriteFile(path, eventData, os.FileMode(0o770)); err != nil {
|
2019-02-25 21:01:13 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-04 13:52:24 -04:00
|
|
|
// Increment the item count.
|
2022-11-08 19:36:47 -05:00
|
|
|
store.entries[key] = time.Now().UnixNano()
|
2019-02-25 21:01:13 -05:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-04 13:52:24 -04:00
|
|
|
// Put - puts an item to the store.
|
|
|
|
func (store *QueueStore[I]) Put(item I) error {
|
2019-02-25 21:01:13 -05:00
|
|
|
store.Lock()
|
|
|
|
defer store.Unlock()
|
2022-11-08 19:36:47 -05:00
|
|
|
if uint64(len(store.entries)) >= store.entryLimit {
|
2019-04-10 08:46:01 -04:00
|
|
|
return errLimitExceeded
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|
2023-04-04 13:52:24 -04:00
|
|
|
// Generate a new UUID for the key.
|
|
|
|
key, err := uuid.NewRandom()
|
2019-10-11 20:46:03 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|
2023-04-04 13:52:24 -04:00
|
|
|
return store.write(key.String(), item)
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|
|
|
|
|
2023-04-04 13:52:24 -04:00
|
|
|
// Get - gets an item from the store.
|
|
|
|
func (store *QueueStore[I]) Get(key string) (item I, err error) {
|
2019-02-25 21:01:13 -05:00
|
|
|
store.RLock()
|
|
|
|
|
2023-04-04 13:52:24 -04:00
|
|
|
defer func(store *QueueStore[I]) {
|
2019-10-11 20:46:03 -04:00
|
|
|
store.RUnlock()
|
|
|
|
if err != nil {
|
|
|
|
// Upon error we remove the entry.
|
|
|
|
store.Del(key)
|
|
|
|
}
|
|
|
|
}(store)
|
2019-02-25 21:01:13 -05:00
|
|
|
|
2019-10-11 20:46:03 -04:00
|
|
|
var eventData []byte
|
2023-04-04 13:52:24 -04:00
|
|
|
eventData, err = os.ReadFile(filepath.Join(store.directory, key+store.fileExt))
|
2019-10-11 20:46:03 -04:00
|
|
|
if err != nil {
|
2023-04-04 13:52:24 -04:00
|
|
|
return item, err
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(eventData) == 0 {
|
2023-04-04 13:52:24 -04:00
|
|
|
return item, os.ErrNotExist
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|
|
|
|
|
2023-04-04 13:52:24 -04:00
|
|
|
if err = json.Unmarshal(eventData, &item); err != nil {
|
|
|
|
return item, err
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|
|
|
|
|
2023-04-04 13:52:24 -04:00
|
|
|
return item, nil
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Del - Deletes an entry from the store.
|
2023-04-04 13:52:24 -04:00
|
|
|
func (store *QueueStore[_]) Del(key string) error {
|
2019-02-25 21:01:13 -05:00
|
|
|
store.Lock()
|
|
|
|
defer store.Unlock()
|
2019-04-10 08:46:01 -04:00
|
|
|
return store.del(key)
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|
|
|
|
|
2022-11-08 19:36:47 -05:00
|
|
|
// Len returns the entry count.
|
2023-04-04 13:52:24 -04:00
|
|
|
func (store *QueueStore[_]) Len() int {
|
2022-11-08 19:36:47 -05:00
|
|
|
store.RLock()
|
|
|
|
l := len(store.entries)
|
|
|
|
defer store.RUnlock()
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
2019-02-25 21:01:13 -05:00
|
|
|
// lockless call
|
2023-04-04 13:52:24 -04:00
|
|
|
func (store *QueueStore[_]) del(key string) error {
|
|
|
|
err := os.Remove(filepath.Join(store.directory, key+store.fileExt))
|
2019-02-25 21:01:13 -05:00
|
|
|
|
2022-11-08 19:36:47 -05:00
|
|
|
// Delete as entry no matter the result
|
|
|
|
delete(store.entries, key)
|
|
|
|
|
|
|
|
return err
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|
|
|
|
|
2022-11-08 19:36:47 -05:00
|
|
|
// List - lists all files registered in the store.
|
2023-04-04 13:52:24 -04:00
|
|
|
func (store *QueueStore[_]) List() ([]string, error) {
|
2019-02-25 21:01:13 -05:00
|
|
|
store.RLock()
|
2022-11-08 19:36:47 -05:00
|
|
|
l := make([]string, 0, len(store.entries))
|
|
|
|
for k := range store.entries {
|
|
|
|
l = append(l, k)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort entries...
|
|
|
|
sort.Slice(l, func(i, j int) bool {
|
|
|
|
return store.entries[l[i]] < store.entries[l[j]]
|
|
|
|
})
|
|
|
|
store.RUnlock()
|
|
|
|
|
|
|
|
return l, nil
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|
|
|
|
|
2022-11-08 19:36:47 -05:00
|
|
|
// list will read all entries from disk.
|
|
|
|
// Entries are returned sorted by modtime, oldest first.
|
|
|
|
// Underlying entry list in store is *not* updated.
|
2023-04-04 13:52:24 -04:00
|
|
|
func (store *QueueStore[_]) list() ([]os.DirEntry, error) {
|
2022-09-19 14:05:16 -04:00
|
|
|
files, err := os.ReadDir(store.directory)
|
2019-10-11 20:46:03 -04:00
|
|
|
if err != nil {
|
2022-11-08 19:36:47 -05:00
|
|
|
return nil, err
|
2019-10-11 20:46:03 -04:00
|
|
|
}
|
2019-05-22 16:34:48 -04:00
|
|
|
|
2022-11-08 19:36:47 -05:00
|
|
|
// Sort the entries.
|
2019-05-22 16:34:48 -04:00
|
|
|
sort.Slice(files, func(i, j int) bool {
|
2022-09-19 14:05:16 -04:00
|
|
|
ii, err := files[i].Info()
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
ji, err := files[j].Info()
|
|
|
|
if err != nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return ii.ModTime().Before(ji.ModTime())
|
2019-05-22 16:34:48 -04:00
|
|
|
})
|
|
|
|
|
2022-11-08 19:36:47 -05:00
|
|
|
return files, nil
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|
2023-04-04 13:52:24 -04:00
|
|
|
|
|
|
|
// Extension will return the file extension used
|
|
|
|
// for the items stored in the queue.
|
|
|
|
func (store *QueueStore[_]) Extension() string {
|
|
|
|
return store.fileExt
|
|
|
|
}
|