ussd/internal/storage/db.go

44 lines
756 B
Go
Raw Normal View History

2024-09-27 22:10:03 +02:00
package storage
import (
"context"
"git.defalsify.org/vise.git/db"
)
const (
DATATYPE_USERSUB = 64
)
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(DATATYPE_USERSUB)
2024-09-27 22:10:03 +02:00
key = s.toKey(key)
2024-10-07 07:59:15 +02:00
v, err := s.store.Get(ctx, key)
if err != nil {
return nil, err
}
return v, nil
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(DATATYPE_USERSUB)
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
}