Refactor StorageMap to BlobList

This commit is contained in:
Alexander Neumann
2014-11-21 21:21:44 +01:00
parent d594cd89b7
commit d1e4431514
10 changed files with 285 additions and 111 deletions
+20 -28
View File
@@ -3,7 +3,7 @@ package khepri
import (
"encoding/json"
"errors"
"sync"
"fmt"
"github.com/fd0/khepri/backend"
)
@@ -12,16 +12,15 @@ type ContentHandler struct {
be backend.Server
key *Key
m sync.Mutex
content *StorageMap
bl *BlobList
}
// NewContentHandler creates a new content handler.
func NewContentHandler(be backend.Server, key *Key) (*ContentHandler, error) {
ch := &ContentHandler{
be: be,
key: key,
content: NewStorageMap(),
be: be,
key: key,
bl: NewBlobList(),
}
return ch, nil
@@ -34,9 +33,8 @@ func (ch *ContentHandler) LoadSnapshot(id backend.ID) (*Snapshot, error) {
return nil, err
}
ch.m.Lock()
defer ch.m.Unlock()
ch.content.Merge(sn.StorageMap)
ch.bl.Merge(sn.BlobList)
return sn, nil
}
@@ -50,9 +48,7 @@ func (ch *ContentHandler) LoadAllSnapshots() error {
return
}
ch.m.Lock()
defer ch.m.Unlock()
ch.content.Merge(sn.StorageMap)
ch.bl.Merge(sn.BlobList)
})
if err != nil {
return err
@@ -63,20 +59,18 @@ func (ch *ContentHandler) LoadAllSnapshots() error {
// Save encrypts data and stores it to the backend as type t. If the data was
// already saved before, the blob is returned.
func (ch *ContentHandler) Save(t backend.Type, data []byte) (*Blob, error) {
func (ch *ContentHandler) Save(t backend.Type, data []byte) (Blob, error) {
// compute plaintext hash
id := backend.Hash(data)
// test if the hash is already in the backend
ch.m.Lock()
defer ch.m.Unlock()
blob := ch.content.Find(id)
if blob != nil {
blob, err := ch.bl.Find(Blob{ID: id})
if err == nil {
return blob, nil
}
// else create a new blob
blob = &Blob{
blob = Blob{
ID: id,
Size: uint64(len(data)),
}
@@ -84,30 +78,30 @@ func (ch *ContentHandler) Save(t backend.Type, data []byte) (*Blob, error) {
// encrypt blob
ciphertext, err := ch.key.Encrypt(data)
if err != nil {
return nil, err
return Blob{}, err
}
// save blob
sid, err := ch.be.Create(t, ciphertext)
if err != nil {
return nil, err
return Blob{}, err
}
blob.Storage = sid
blob.StorageSize = uint64(len(ciphertext))
// insert blob into the storage map
ch.content.Insert(blob)
ch.bl.Insert(blob)
return blob, nil
}
// SaveJSON serialises item as JSON and uses Save() to store it to the backend as type t.
func (ch *ContentHandler) SaveJSON(t backend.Type, item interface{}) (*Blob, error) {
func (ch *ContentHandler) SaveJSON(t backend.Type, item interface{}) (Blob, error) {
// convert to json
data, err := json.Marshal(item)
if err != nil {
return nil, err
return Blob{}, err
}
// compress and save data
@@ -133,11 +127,9 @@ func (ch *ContentHandler) Load(t backend.Type, id backend.ID) ([]byte, error) {
}
// lookup storage hash
ch.m.Lock()
defer ch.m.Unlock()
blob := ch.content.Find(id)
if blob == nil {
return nil, errors.New("Storage ID not found")
blob, err := ch.bl.Find(Blob{ID: id})
if err != nil {
return nil, fmt.Errorf("Storage ID %s not found", id)
}
// load data