Skip to content

Commit

Permalink
Add initial Set interface and Memory impl
Browse files Browse the repository at this point in the history
  • Loading branch information
lucperkins committed Jul 27, 2019
1 parent bfafcd0 commit cd29d99
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -3,7 +3,7 @@
An all-in-one data service with support for:

* Key/value operations
* Counters
* Counters and sets
* Caching with TTL
* Search indexing and querying

Expand Down
35 changes: 35 additions & 0 deletions memory.go
Expand Up @@ -10,13 +10,15 @@ type Memory struct {
counters map[string]int32
values map[Location]*Value
docs []*Document
sets map[string][]string
}

var (
_ Cache = (*Memory)(nil)
_ Counter = (*Memory)(nil)
_ KV = (*Memory)(nil)
_ Search = (*Memory)(nil)
_ Set = (*Memory)(nil)
)

func New() *Memory {
Expand All @@ -28,11 +30,14 @@ func New() *Memory {

docs := make([]*Document, 0)

sets := make(map[string][]string)

return &Memory{
cache: cache,
counters: counters,
values: values,
docs: docs,
sets: sets,
}
}

Expand Down Expand Up @@ -140,3 +145,33 @@ func (m *Memory) Query(q string) []*Document {

return docs
}

func (m *Memory) GetSet(set string) []string {
s, ok := m.sets[set]

if !ok {
return []string{}
}

return s
}

func (m *Memory) AddToSet(set, item string) {
if _, ok := m.sets[set]; !ok {
m.sets[set] = []string{item}
}

m.sets[set] = append(m.sets[set], item)
}

func (m *Memory) RemoveFromSet(set, item string) {
if _, ok := m.sets[set]; !ok {
return
}

for idx, it := range m.sets[set] {
if it == item {
m.sets[set] = append(m.sets[set][:idx], m.sets[set][idx+1:]...)
}
}
}
7 changes: 7 additions & 0 deletions set.go
@@ -0,0 +1,7 @@
package strato

type Set interface {
GetSet(set string) []string
AddToSet(set, item string)
RemoveFromSet(set, item string)
}

0 comments on commit cd29d99

Please sign in to comment.