-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Refactor OuroborosDB to use storage.StorageService instead of …
…*storage.Storage and build index test as well as ChildToParent Index
- Loading branch information
Showing
12 changed files
with
778 additions
and
224 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package index | ||
|
||
import "github.com/i5heu/ouroboros-db/pkg/types" | ||
|
||
func (i *Index) RebuildChildrenToParents(allEvents []types.Event) error { | ||
i.evChildToParentLock.Lock() | ||
defer i.evChildToParentLock.Unlock() | ||
|
||
// Clear the existing mapping | ||
clear(i.evChildToParent) | ||
|
||
// Rebuild the mapping | ||
for _, event := range allEvents { | ||
i.evChildToParent[event.EventHash] = event.HashOfParentEvent | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (i *Index) GetParentHashOfEvent(eventHash [64]byte) ([64]byte, bool) { | ||
i.evChildToParentLock.RLock() | ||
defer i.evChildToParentLock.RUnlock() | ||
parentHash, exists := i.evChildToParent[eventHash] | ||
return parentHash, exists | ||
} | ||
|
||
func (i *Index) GetDirectParentOfEvent(eventHash [64]byte) (*types.Event, error) { | ||
parentHash, exists := i.GetParentHashOfEvent(eventHash) | ||
if !exists { | ||
return nil, nil // No parent found | ||
} | ||
|
||
parentEvent, err := i.ss.GetEvent(parentHash) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &parentEvent, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package index | ||
|
||
import "github.com/i5heu/ouroboros-db/pkg/types" | ||
|
||
func (i *Index) RebuildParentsToChildren(allEvents []types.Event) error { | ||
i.evParentToChildLock.Lock() | ||
defer i.evParentToChildLock.Unlock() | ||
|
||
clear(i.evParentToChild) | ||
|
||
for _, event := range allEvents { | ||
i.evParentToChild[event.HashOfParentEvent] = append(i.evParentToChild[event.HashOfParentEvent], event.EventHash) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (i *Index) GetChildrenHashesOfEvent(eventHash [64]byte) [][64]byte { | ||
i.evParentToChildLock.RLock() | ||
defer i.evParentToChildLock.RUnlock() | ||
return i.evParentToChild[eventHash] | ||
} | ||
|
||
func (i *Index) GetDirectChildrenOfEvent(eventHash [64]byte) ([]types.Event, error) { | ||
childrenHashes := i.GetChildrenHashesOfEvent(eventHash) | ||
children := make([]types.Event, 0) | ||
|
||
for _, childHash := range childrenHashes { | ||
|
||
child, err := i.ss.GetEvent(childHash) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
children = append(children, child) | ||
} | ||
|
||
return children, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package index | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/i5heu/ouroboros-db/pkg/mocks" | ||
"github.com/i5heu/ouroboros-db/pkg/types" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestIndex_RebuildIndex(t *testing.T) { | ||
mockStorageService := new(mocks.StorageService) | ||
|
||
events := []types.Event{ | ||
{EventHash: [64]byte{1}, HashOfParentEvent: [64]byte{0}}, | ||
{EventHash: [64]byte{2}, HashOfParentEvent: [64]byte{1}}, | ||
} | ||
|
||
mockStorageService.On("GetAllEvents").Return(events, nil) | ||
|
||
index := NewIndex(mockStorageService) | ||
|
||
count, err := index.RebuildIndex() | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, uint64(2), count) | ||
mockStorageService.AssertExpectations(t) | ||
} | ||
|
||
func TestIndex_RebuildParentsToChildren(t *testing.T) { | ||
mockStorageService := new(mocks.StorageService) | ||
index := NewIndex(mockStorageService) | ||
|
||
parentHash := [64]byte{1} | ||
childHash1 := [64]byte{2} | ||
childHash2 := [64]byte{3} | ||
|
||
events := []types.Event{ | ||
{EventHash: childHash1, HashOfParentEvent: parentHash}, | ||
{EventHash: childHash2, HashOfParentEvent: parentHash}, | ||
} | ||
|
||
err := index.RebuildParentsToChildren(events) | ||
assert.NoError(t, err) | ||
|
||
index.evParentToChildLock.RLock() | ||
defer index.evParentToChildLock.RUnlock() | ||
assert.Contains(t, index.evParentToChild[parentHash], childHash1) | ||
assert.Contains(t, index.evParentToChild[parentHash], childHash2) | ||
} | ||
|
||
func TestIndex_GetChildrenHashesOfEvent(t *testing.T) { | ||
index := &Index{ | ||
evParentToChild: make(map[[64]byte][][64]byte), | ||
} | ||
|
||
parentHash := [64]byte{1} | ||
childHash := [64]byte{2} | ||
index.evParentToChild[parentHash] = append(index.evParentToChild[parentHash], childHash) | ||
|
||
retrievedChildren := index.GetChildrenHashesOfEvent(parentHash) | ||
assert.Contains(t, retrievedChildren, childHash) | ||
} | ||
|
||
func TestIndex_GetDirectChildrenOfEvent(t *testing.T) { | ||
mockStorageService := new(mocks.StorageService) | ||
index := NewIndex(mockStorageService) | ||
|
||
parentHash := [64]byte{1} | ||
childHash := [64]byte{2} | ||
index.evParentToChild[parentHash] = append(index.evParentToChild[parentHash], childHash) | ||
|
||
childEvent := types.Event{EventHash: childHash} | ||
mockStorageService.On("GetEvent", childHash).Return(childEvent, nil) | ||
|
||
retrievedChildren, err := index.GetDirectChildrenOfEvent(parentHash) | ||
assert.NoError(t, err) | ||
assert.Contains(t, retrievedChildren, childEvent) | ||
mockStorageService.AssertExpectations(t) | ||
} | ||
|
||
func TestIndex_RebuildChildrenToParents(t *testing.T) { | ||
mockStorageService := new(mocks.StorageService) | ||
index := NewIndex(mockStorageService) | ||
|
||
parentHash := [64]byte{1} | ||
childHash1 := [64]byte{2} | ||
childHash2 := [64]byte{3} | ||
|
||
events := []types.Event{ | ||
{EventHash: childHash1, HashOfParentEvent: parentHash}, | ||
{EventHash: childHash2, HashOfParentEvent: parentHash}, | ||
} | ||
|
||
err := index.RebuildChildrenToParents(events) | ||
assert.NoError(t, err) | ||
|
||
index.evChildToParentLock.RLock() | ||
defer index.evChildToParentLock.RUnlock() | ||
assert.Equal(t, parentHash, index.evChildToParent[childHash1]) | ||
assert.Equal(t, parentHash, index.evChildToParent[childHash2]) | ||
} | ||
|
||
func TestIndex_GetParentHashOfEvent(t *testing.T) { | ||
index := &Index{ | ||
evChildToParent: make(map[[64]byte][64]byte), | ||
} | ||
|
||
parentHash := [64]byte{1} | ||
childHash := [64]byte{2} | ||
index.evChildToParent[childHash] = parentHash | ||
|
||
retrievedParentHash, exists := index.GetParentHashOfEvent(childHash) | ||
assert.True(t, exists) | ||
assert.Equal(t, parentHash, retrievedParentHash) | ||
} | ||
|
||
func TestIndex_GetDirectParentOfEvent(t *testing.T) { | ||
mockStorageService := new(mocks.StorageService) | ||
index := NewIndex(mockStorageService) | ||
|
||
parentHash := [64]byte{1} | ||
childHash := [64]byte{2} | ||
index.evChildToParent[childHash] = parentHash | ||
|
||
parentEvent := types.Event{EventHash: parentHash} | ||
mockStorageService.On("GetEvent", parentHash).Return(parentEvent, nil) | ||
|
||
retrievedParentEvent, err := index.GetDirectParentOfEvent(childHash) | ||
assert.NoError(t, err) | ||
assert.Equal(t, &parentEvent, retrievedParentEvent) | ||
mockStorageService.AssertExpectations(t) | ||
} |
Oops, something went wrong.