mirror of
https://github.com/derfenix/webarchive.git
synced 2026-03-11 22:40:58 +03:00
Initial commit
This commit is contained in:
40
adapters/repository/badger/file.go
Normal file
40
adapters/repository/badger/file.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package badger
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/dgraph-io/badger/v4"
|
||||
|
||||
"github.com/derfenix/webarchive/entity"
|
||||
)
|
||||
|
||||
func NewFile(db *badger.DB) *File {
|
||||
return &File{db: db, prefix: []byte("file:")}
|
||||
}
|
||||
|
||||
type File struct {
|
||||
db *badger.DB
|
||||
prefix []byte
|
||||
}
|
||||
|
||||
func (f *File) SaveTx(_ context.Context, txn *badger.Txn, file *entity.File) error {
|
||||
if f.db.IsClosed() {
|
||||
return ErrDBClosed
|
||||
}
|
||||
|
||||
marshaled, err := marshal(file)
|
||||
if err != nil {
|
||||
return fmt.Errorf("marshal data: %w", err)
|
||||
}
|
||||
|
||||
if err := txn.Set(f.key(file), marshaled); err != nil {
|
||||
return fmt.Errorf("put data: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *File) key(file *entity.File) []byte {
|
||||
return append(f.prefix, []byte(file.ID.String())...)
|
||||
}
|
||||
Reference in New Issue
Block a user