Skip to content
This repository has been archived by the owner on Aug 16, 2022. It is now read-only.

feat: memory infrastructure #46

Merged
merged 4 commits into from
Sep 15, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
176 changes: 176 additions & 0 deletions internal/infrastructure/memory/tag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
package memory

import (
"context"
"sync"

"github.com/reearth/reearth-backend/pkg/rerror"

"github.com/reearth/reearth-backend/internal/usecase/repo"
"github.com/reearth/reearth-backend/pkg/id"
"github.com/reearth/reearth-backend/pkg/tag"
)

type Tag struct {
lock sync.Mutex
data map[id.TagID]tag.Tag
}

func NewTag() repo.Tag {
return &Tag{
data: map[id.TagID]tag.Tag{},
}
}

func (t *Tag) FindByID(ctx context.Context, tagID id.TagID, ids []id.SceneID) (*tag.Tag, error) {
t.lock.Lock()
defer t.lock.Unlock()

res, ok := t.data[tagID]
if ok && isSceneIncludes(res.Scene(), ids) {
return &res, nil
}
return nil, rerror.ErrNotFound
}

func (t *Tag) FindByIDs(ctx context.Context, tids []id.TagID, ids []id.SceneID) ([]*tag.Tag, error) {
t.lock.Lock()
defer t.lock.Unlock()

var res []*tag.Tag
for _, id := range tids {
if d, ok := t.data[id]; ok {
if isSceneIncludes(d.Scene(), ids) {
res = append(res, &d)
continue
}
}
res = append(res, nil)
}
return res, nil
}

func (t *Tag) FindItemByID(ctx context.Context, tagID id.TagID, ids []id.SceneID) (*tag.Item, error) {
t.lock.Lock()
defer t.lock.Unlock()

if d, ok := t.data[tagID]; ok {
if res := tag.ItemFrom(d); res != nil {
if isSceneIncludes(res.Scene(), ids) {
return res, nil
}
}
}
return nil, rerror.ErrNotFound
}

func (t *Tag) FindItemByIDs(ctx context.Context, tagIDs []id.TagID, ids []id.SceneID) ([]*tag.Item, error) {
t.lock.Lock()
defer t.lock.Unlock()

var res []*tag.Item
for _, id := range tagIDs {
if d, ok := t.data[id]; ok {
if ti := tag.ItemFrom(d); ti != nil {
if isSceneIncludes(ti.Scene(), ids) {
res = append(res, ti)
}
}
}
}
return res, nil
}

func (t *Tag) FindGroupByID(ctx context.Context, tagID id.TagID, ids []id.SceneID) (*tag.Group, error) {
t.lock.Lock()
defer t.lock.Unlock()

if d, ok := t.data[tagID]; ok {
if res := tag.GroupFrom(d); res != nil {
if isSceneIncludes(res.Scene(), ids) {
return res, nil
}
}
}
return nil, rerror.ErrNotFound
}

func (t *Tag) FindGroupByIDs(ctx context.Context, tagIDs []id.TagID, ids []id.SceneID) ([]*tag.Group, error) {
t.lock.Lock()
defer t.lock.Unlock()

var res []*tag.Group
for _, id := range tagIDs {
if d, ok := t.data[id]; ok {
if tg := tag.GroupFrom(d); tg != nil {
if isSceneIncludes(tg.Scene(), ids) {
res = append(res, tg)
}
}
}
}
return res, nil
}

func (t *Tag) FindByScene(ctx context.Context, sceneID id.SceneID) ([]*tag.Tag, error) {
t.lock.Lock()
defer t.lock.Unlock()

var res []*tag.Tag
for _, tag := range t.data {
tag := tag
if tag.Scene() == sceneID {
res = append(res, &tag)
}
}
return res, nil
}

func (t *Tag) Save(ctx context.Context, tag tag.Tag) error {
t.lock.Lock()
defer t.lock.Unlock()

t.data[tag.ID()] = tag
return nil
}

func (t *Tag) SaveAll(ctx context.Context, tags []*tag.Tag) error {
t.lock.Lock()
defer t.lock.Unlock()

for _, tagRef := range tags {
tag := *tagRef
t.data[tag.ID()] = tag
}
return nil
}

func (t *Tag) Remove(ctx context.Context, tagID id.TagID) error {
t.lock.Lock()
defer t.lock.Unlock()

delete(t.data, tagID)
return nil
}

func (t *Tag) RemoveAll(ctx context.Context, ids []id.TagID) error {
t.lock.Lock()
defer t.lock.Unlock()

for _, tagID := range ids {
delete(t.data, tagID)
}
return nil
}

func (t *Tag) RemoveByScene(ctx context.Context, sceneID id.SceneID) error {
t.lock.Lock()
defer t.lock.Unlock()

for tid, v := range t.data {
if v.Scene() == sceneID {
delete(t.data, tid)
}
}
return nil
}
62 changes: 62 additions & 0 deletions internal/infrastructure/memory/tag_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package memory

import (
"context"
"testing"

"github.com/reearth/reearth-backend/pkg/id"
"github.com/stretchr/testify/assert"

"github.com/reearth/reearth-backend/pkg/tag"
)

func TestTag(t *testing.T) {
repo := NewTag()
assert.NotNil(t, repo)
ctx := context.Background()
sid := id.NewSceneID()
sid2 := id.NewSceneID()
sl := []id.SceneID{sid}
t1, _ := tag.NewItem().NewID().Scene(sid).Label("item").Build()
tl := tag.NewListFromTags([]id.TagID{t1.ID()})
t2, _ := tag.NewGroup().NewID().Scene(sid).Label("group").Tags(*tl).Build()
t3, _ := tag.NewItem().NewID().Scene(sid2).Label("item2").Build()
tti2 := tag.Tag(t3)
ttg := tag.Tag(t2)
tti := tag.Tag(t1)
err := repo.Save(ctx, t1)
assert.NoError(t, err)
out, err := repo.FindByID(ctx, t1.ID(), sl)
assert.NoError(t, err)
assert.Equal(t, &tti, out)
err = repo.SaveAll(ctx, []*tag.Tag{&ttg, &tti2})
assert.NoError(t, err)
out2, err := repo.FindByIDs(ctx, []id.TagID{t1.ID(), t2.ID()}, sl)
assert.NoError(t, err)
assert.Equal(t, []*tag.Tag{&tti, &ttg}, out2)
out3, err := repo.FindByScene(ctx, sid2)
assert.NoError(t, err)
assert.Equal(t, []*tag.Tag{&tti2}, out3)
out4, err := repo.FindGroupByID(ctx, t2.ID(), sl)
assert.NoError(t, err)
assert.Equal(t, t2, out4)
out5, err := repo.FindItemByID(ctx, t1.ID(), sl)
assert.NoError(t, err)
assert.Equal(t, t1, out5)
out6, err := repo.FindGroupByIDs(ctx, []id.TagID{t2.ID()}, sl)
assert.NoError(t, err)
assert.Equal(t, []*tag.Group{t2}, out6)
out7, err := repo.FindItemByIDs(ctx, []id.TagID{t1.ID()}, sl)
assert.NoError(t, err)
assert.Equal(t, []*tag.Item{t1}, out7)
_ = repo.Remove(ctx, t1.ID())
out8, _ := repo.FindByID(ctx, t1.ID(), sl)
assert.Nil(t, out8)
_ = repo.RemoveAll(ctx, []id.TagID{t2.ID()})
out9, _ := repo.FindByID(ctx, t2.ID(), sl)
assert.Nil(t, out9)
_ = repo.RemoveByScene(ctx, sid2)
out10, _ := repo.FindByID(ctx, t3.ID(), []id.SceneID{sid2})
assert.Nil(t, out10)

}