133 lines
2.6 KiB
Go
133 lines
2.6 KiB
Go
package leveldb
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"fmt"
|
|
"io"
|
|
|
|
"github.com/d1nch8g/mesh/database"
|
|
"github.com/google/uuid"
|
|
"github.com/syndtr/goleveldb/leveldb"
|
|
"github.com/syndtr/goleveldb/leveldb/util"
|
|
)
|
|
|
|
type files struct {
|
|
db *leveldb.DB
|
|
}
|
|
|
|
const fileChunkSize = 4 << 20 // 4 MB
|
|
|
|
func (f *files) Put(user string, r io.Reader) (uuid.UUID, error) {
|
|
id := uuid.New()
|
|
|
|
batch := new(leveldb.Batch)
|
|
defer batch.Reset()
|
|
|
|
var totalSize uint64
|
|
chunk := make([]byte, fileChunkSize)
|
|
var chunkIndex uint32
|
|
hasData := false
|
|
|
|
for {
|
|
n, err := r.Read(chunk)
|
|
if n > 0 {
|
|
var idxBuf [4]byte
|
|
binary.BigEndian.PutUint32(idxBuf[:], chunkIndex)
|
|
|
|
k := key(user, prefixFileChunk, id, idxBuf[:])
|
|
buf := make([]byte, n)
|
|
copy(buf, chunk[:n])
|
|
batch.Put(k, buf)
|
|
totalSize += uint64(n)
|
|
chunkIndex++
|
|
hasData = true
|
|
}
|
|
if err == io.EOF {
|
|
break
|
|
}
|
|
if err != nil {
|
|
return uuid.Nil, fmt.Errorf("read file %q: %w", id, err)
|
|
}
|
|
}
|
|
|
|
if !hasData {
|
|
var idxBuf [4]byte
|
|
binary.BigEndian.PutUint32(idxBuf[:], 0)
|
|
k := key(user, prefixFileChunk, id, idxBuf[:])
|
|
batch.Put(k, []byte{})
|
|
}
|
|
|
|
metaKey := key(user, prefixFileMeta, id)
|
|
var sizeBuf [8]byte
|
|
binary.LittleEndian.PutUint64(sizeBuf[:], totalSize)
|
|
batch.Put(metaKey, sizeBuf[:])
|
|
|
|
if err := f.db.Write(batch, nil); err != nil {
|
|
return uuid.Nil, fmt.Errorf("commit file %q: %w", id, err)
|
|
}
|
|
|
|
return id, nil
|
|
}
|
|
|
|
func (f *files) Get(user string, id uuid.UUID, w io.Writer) error {
|
|
prefix := key(user, prefixFileChunk, id)
|
|
|
|
iter := f.db.NewIterator(util.BytesPrefix(prefix), nil)
|
|
defer iter.Release()
|
|
|
|
chunkCount := 0
|
|
for iter.Next() {
|
|
if _, err := w.Write(iter.Value()); err != nil {
|
|
return fmt.Errorf("write file %q: %w", id, err)
|
|
}
|
|
chunkCount++
|
|
}
|
|
|
|
if chunkCount == 0 {
|
|
return database.ErrNotFound
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (f *files) Delete(user string, id uuid.UUID) error {
|
|
prefix := key(user, prefixFileChunk, id)
|
|
|
|
iter := f.db.NewIterator(util.BytesPrefix(prefix), nil)
|
|
defer iter.Release()
|
|
|
|
if !iter.Next() {
|
|
return database.ErrNotFound
|
|
}
|
|
|
|
batch := new(leveldb.Batch)
|
|
defer batch.Reset()
|
|
|
|
iter.Prev()
|
|
for iter.Next() {
|
|
batch.Delete(iter.Key())
|
|
}
|
|
|
|
metaKey := key(user, prefixFileMeta, id)
|
|
batch.Delete(metaKey)
|
|
|
|
if err := f.db.Write(batch, nil); err != nil {
|
|
return fmt.Errorf("commit delete %q: %w", id, err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (f *files) Size(user string, id uuid.UUID) (int64, error) {
|
|
metaKey := key(user, prefixFileMeta, id)
|
|
data, err := f.db.Get(metaKey, nil)
|
|
if err != nil {
|
|
if err == leveldb.ErrNotFound {
|
|
return 0, database.ErrNotFound
|
|
}
|
|
return 0, fmt.Errorf("size file %q: %w", id, err)
|
|
}
|
|
|
|
return int64(binary.LittleEndian.Uint64(data)), nil
|
|
}
|