-
Notifications
You must be signed in to change notification settings - Fork 0
Memory datastore #111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Memory datastore #111
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
911f9bd
Support a memory datastore for testing only
majst01 2ed3a7b
simpler
majst01 31ed9a1
implement deleteall
majst01 52b3958
Merge branch 'master' of https://github.com/metal-stack/masterdata-ap…
majst01 8b84089
Expose init tables
majst01 2919322
updates
majst01 e87d5e6
Merge branch 'master' into memory-datastore
majst01 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,130 @@ | ||
| package datastore | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "log/slog" | ||
| "sync" | ||
| "time" | ||
|
|
||
| v1 "github.com/metal-stack/masterdata-api/api/v1" | ||
| ) | ||
|
|
||
| type memoryDatastore[E Entity] struct { | ||
| lock sync.RWMutex | ||
| entities map[string]E | ||
| entity string | ||
| log *slog.Logger | ||
| } | ||
|
|
||
| func NewMemory[E Entity](log *slog.Logger, e E) Storage[E] { | ||
| entity := e.JSONField() | ||
| return &memoryDatastore[E]{ | ||
| lock: sync.RWMutex{}, | ||
| entities: make(map[string]E), | ||
| entity: entity, | ||
| log: log, | ||
| } | ||
| } | ||
|
|
||
| // Create implements Storage. | ||
| func (m *memoryDatastore[E]) Create(ctx context.Context, ve E) error { | ||
| m.log.Debug("create", "entity", m.entity, "value", ve) | ||
| meta := ve.GetMeta() | ||
| if meta == nil { | ||
| return fmt.Errorf("create of type:%s failed, meta is nil", m.entity) | ||
| } | ||
|
|
||
| id := ve.GetMeta().Id | ||
| m.lock.Lock() | ||
| defer m.lock.Unlock() | ||
|
|
||
| _, ok := m.entities[id] | ||
| if ok { | ||
| return NewDuplicateKeyError(fmt.Sprintf("an entity of type:%s with the id:%s already exists", m.entity, id)) | ||
| } | ||
| m.entities[id] = ve | ||
| return nil | ||
| } | ||
|
|
||
| // Delete implements Storage. | ||
| func (m *memoryDatastore[E]) Delete(ctx context.Context, id string) error { | ||
| m.log.Debug("delete", "entity", m.entity, "id", id) | ||
|
|
||
| m.lock.Lock() | ||
| defer m.lock.Unlock() | ||
|
|
||
| _, ok := m.entities[id] | ||
| if !ok { | ||
| return NewNotFoundError(fmt.Sprintf("not found: delete of %s with id %s", m.entity, id)) | ||
| } | ||
| delete(m.entities, id) | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // DeleteAll implements Storage. | ||
| func (m *memoryDatastore[E]) DeleteAll(ctx context.Context, ids ...string) error { | ||
| panic("unimplemented") | ||
| } | ||
|
|
||
| // Find implements Storage. | ||
| func (m *memoryDatastore[E]) Find(ctx context.Context, filter map[string]any, paging *v1.Paging) ([]E, *uint64, error) { | ||
| m.log.Debug("find", "entity", m.entity, "filter", filter) | ||
|
|
||
| m.lock.Lock() | ||
| defer m.lock.Unlock() | ||
|
|
||
| var result []E | ||
| for _, e := range m.entities { | ||
| // FIXME implement filtering | ||
| result = append(result, e) | ||
| } | ||
|
|
||
| return result, nil, nil | ||
| } | ||
|
|
||
| // Get implements Storage. | ||
| func (m *memoryDatastore[E]) Get(ctx context.Context, id string) (E, error) { | ||
| m.log.Debug("get", "entity", m.entity, "id", id) | ||
| var zero E | ||
| m.lock.Lock() | ||
| defer m.lock.Unlock() | ||
|
|
||
| e, ok := m.entities[id] | ||
| if !ok { | ||
| return zero, NewNotFoundError(fmt.Sprintf("not found: delete of %s with id %s", m.entity, id)) | ||
| } | ||
|
|
||
| return e, nil | ||
| } | ||
|
|
||
| // GetHistory implements Storage. | ||
| func (m *memoryDatastore[E]) GetHistory(ctx context.Context, id string, at time.Time, ve E) error { | ||
| panic("unimplemented") | ||
| } | ||
|
|
||
| // GetHistoryCreated implements Storage. | ||
| func (m *memoryDatastore[E]) GetHistoryCreated(ctx context.Context, id string, ve E) error { | ||
| panic("unimplemented") | ||
| } | ||
|
|
||
| // Update implements Storage. | ||
| func (m *memoryDatastore[E]) Update(ctx context.Context, ve E) error { | ||
| m.log.Debug("update", "entity", m.entity) | ||
| meta := ve.GetMeta() | ||
| if meta == nil { | ||
| return fmt.Errorf("update of type:%s failed, meta is nil", m.entity) | ||
| } | ||
| id := ve.GetMeta().Id | ||
| m.lock.Lock() | ||
| defer m.lock.Unlock() | ||
|
|
||
| _, ok := m.entities[id] | ||
| if !ok { | ||
| return NewNotFoundError(fmt.Sprintf("not found: delete of %s with id %s", m.entity, id)) | ||
| } | ||
|
|
||
| m.entities[id] = ve | ||
| return nil | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want to implement this? Should be fairly easy
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done