ussd/internal/storage/sub_prefix_db.go

44 lines
908 B
Go
Raw Normal View History

2024-09-27 22:10:03 +02:00
package storage
import (
"context"
"git.defalsify.org/vise.git/db"
)
2024-10-28 14:21:31 +01:00
// PrefixDb interface abstracts the database operations.
type PrefixDb interface {
Get(ctx context.Context, key []byte) ([]byte, error)
Put(ctx context.Context, key []byte, val []byte) error
}
var _ PrefixDb = (*SubPrefixDb)(nil)
2024-09-27 22:10:03 +02:00
type SubPrefixDb struct {
store db.Db
2024-10-07 07:59:15 +02:00
pfx []byte
2024-09-27 22:10:03 +02:00
}
func NewSubPrefixDb(store db.Db, pfx []byte) *SubPrefixDb {
return &SubPrefixDb{
store: store,
2024-10-07 07:59:15 +02:00
pfx: pfx,
2024-09-27 22:10:03 +02:00
}
}
2024-10-07 07:59:15 +02:00
func (s *SubPrefixDb) toKey(k []byte) []byte {
return append(s.pfx, k...)
2024-09-27 22:10:03 +02:00
}
2024-10-07 07:59:15 +02:00
func (s *SubPrefixDb) Get(ctx context.Context, key []byte) ([]byte, error) {
s.store.SetPrefix(db.DATATYPE_USERDATA)
2024-09-27 22:10:03 +02:00
key = s.toKey(key)
2024-10-28 14:21:31 +01:00
return s.store.Get(ctx, key)
2024-09-27 22:10:03 +02:00
}
2024-10-07 07:59:15 +02:00
func (s *SubPrefixDb) Put(ctx context.Context, key []byte, val []byte) error {
s.store.SetPrefix(db.DATATYPE_USERDATA)
2024-09-27 22:10:03 +02:00
key = s.toKey(key)
2024-10-07 07:59:15 +02:00
return s.store.Put(ctx, key, val)
2024-09-27 22:10:03 +02:00
}