Skip to content

Commit

Permalink
Refactor storage package to use StorageService interface instead of *…
Browse files Browse the repository at this point in the history
…storage.Storage
  • Loading branch information
i5heu committed May 16, 2024
1 parent a6f9a9b commit 88e9311
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
6 changes: 3 additions & 3 deletions pkg/storage/normalEvent.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type EventOptions struct {
FullTextSearch bool // optional
}

func (ss *storage) CreateNewEvent(options EventOptions) (types.Event, error) {
func (ss *Storage) CreateNewEvent(options EventOptions) (types.Event, error) {
// Create a new Event
item := types.Event{
Key: []byte{},
Expand Down Expand Up @@ -87,7 +87,7 @@ func (ss *storage) CreateNewEvent(options EventOptions) (types.Event, error) {
return item, err
}

func (ss *storage) GetEvent(hashOfEvent [64]byte) (types.Event, error) {
func (ss *Storage) GetEvent(hashOfEvent [64]byte) (types.Event, error) {
// Read the EventChainItem from the keyValStore
value, err := ss.kv.Read(GenerateKeyFromPrefixAndHash("Event:", hashOfEvent))
if err != nil {
Expand All @@ -99,7 +99,7 @@ func (ss *storage) GetEvent(hashOfEvent [64]byte) (types.Event, error) {
return binaryCoder.ByteToEvent(value)
}

func (ss *storage) GetAllEvents() ([]types.Event, error) {
func (ss *Storage) GetAllEvents() ([]types.Event, error) {
items, err := ss.kv.GetItemsWithPrefix([]byte("Event:"))
if err != nil {
return nil, err
Expand Down
8 changes: 4 additions & 4 deletions pkg/storage/rootEvents.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func init() {

// Same as Event struct in storageService.go but without some unnecessary fields

func (ss *storage) CreateRootEvent(title string) (types.Event, error) {
func (ss *Storage) CreateRootEvent(title string) (types.Event, error) {
// Create a new IndexEvent
item := types.Event{
Key: []byte{},
Expand Down Expand Up @@ -70,7 +70,7 @@ func (ss *storage) CreateRootEvent(title string) (types.Event, error) {
return item, err
}

func (ss *storage) GetAllRootEvents() ([]types.Event, error) {
func (ss *Storage) GetAllRootEvents() ([]types.Event, error) {
// Get all keys from the keyValStore
rootIndex, err := ss.GetRootIndex()
if err != nil {
Expand All @@ -97,7 +97,7 @@ func (ss *storage) GetAllRootEvents() ([]types.Event, error) {
return rootEvents, nil
}

func (ss *storage) GetRootIndex() ([]types.RootEventsIndex, error) {
func (ss *Storage) GetRootIndex() ([]types.RootEventsIndex, error) {
// Get all keys from the keyValStore
rootIndex, err := ss.kv.GetItemsWithPrefix([]byte("RootEvent:"))
if err != nil {
Expand All @@ -117,7 +117,7 @@ func (ss *storage) GetRootIndex() ([]types.RootEventsIndex, error) {
return revi, nil
}

func (ss *storage) GetRootEventsWithTitle(title string) ([]types.Event, error) {
func (ss *Storage) GetRootEventsWithTitle(title string) ([]types.Event, error) {
rootIndex, err := ss.kv.GetItemsWithPrefix([]byte("RootEvent:" + title + ":"))
if err != nil {
log.Fatalf("Error getting keys: %v", err)
Expand Down
16 changes: 8 additions & 8 deletions pkg/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/i5heu/ouroboros-db/pkg/types"
)

type storage struct {
type Storage struct {
kv *keyValStore.KeyValStore
}

Expand All @@ -23,17 +23,17 @@ type StoreFileOptions struct {
}

func NewStorage(kv *keyValStore.KeyValStore) StorageService {
return &storage{
return &Storage{
kv: kv,
}
}

func (ss *storage) Close() {
func (ss *Storage) Close() {
ss.kv.Close()
}

// will store the file in the chunkStore and create new Event as child of given event
func (ss *storage) StoreFile(options StoreFileOptions) (types.Event, error) {
func (ss *Storage) StoreFile(options StoreFileOptions) (types.Event, error) {
// Validate options before proceeding
err := options.ValidateOptions()
if err != nil {
Expand Down Expand Up @@ -119,7 +119,7 @@ func (options *StoreFileOptions) ValidateOptions() error {
return nil
}

func (ss *storage) GetFile(eventOfFile types.Event) ([]byte, error) {
func (ss *Storage) GetFile(eventOfFile types.Event) ([]byte, error) {
file := []byte{}

for _, hash := range eventOfFile.ContentHashes {
Expand All @@ -134,7 +134,7 @@ func (ss *storage) GetFile(eventOfFile types.Event) ([]byte, error) {
return file, nil
}

func (ss *storage) GetMetadata(eventOfFile types.Event) ([]byte, error) {
func (ss *Storage) GetMetadata(eventOfFile types.Event) ([]byte, error) {
metadata := []byte{}

for _, hash := range eventOfFile.MetadataHashes {
Expand All @@ -149,7 +149,7 @@ func (ss *storage) GetMetadata(eventOfFile types.Event) ([]byte, error) {
return metadata, nil
}

func (ss *storage) storeDataInChunkStore(data []byte) ([][64]byte, error) {
func (ss *Storage) storeDataInChunkStore(data []byte) ([][64]byte, error) {
if len(data) == 0 {
return nil, fmt.Errorf("Error storing data: Data is empty")
}
Expand All @@ -175,6 +175,6 @@ func (ss *storage) storeDataInChunkStore(data []byte) ([][64]byte, error) {
return keys, nil
}

func (ss *storage) GarbageCollection() error {
func (ss *Storage) GarbageCollection() error {
return ss.kv.Clean()
}

0 comments on commit 88e9311

Please sign in to comment.