-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemorypoyntstore.go
74 lines (65 loc) · 1.27 KB
/
memorypoyntstore.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"crypto/sha1"
"encoding/hex"
"fmt"
"sync"
)
type MemoryPoyntStore struct {
store map[string]map[string]Poynt
sync.Mutex
}
func NewMemoryPoyntStore() *MemoryPoyntStore {
return &MemoryPoyntStore{
store: make(map[string]map[string]Poynt),
}
}
func (s *MemoryPoyntStore) List() []string {
keys := []string{}
for k, _ := range s.store {
keys = append(keys, k)
}
return keys
}
func (s *MemoryPoyntStore) Get(key string) shaper {
s.Lock()
defer s.Unlock()
var pc PoyntCollection
if _, ok := s.store[key]; !ok {
return &pc
} else {
pc.store = mapToArray(s.store[key])
return &pc
}
}
func (s *MemoryPoyntStore) Write(key string, p Poynt) bool {
s.Lock()
defer s.Unlock()
id := uniqueId(p)
if namespace, ok := s.store[key]; ok {
if _, ok := namespace[id]; ok {
fmt.Println("Error this poynt already exists")
return false
} else {
namespace[id] = p
}
} else {
s.store[key] = map[string]Poynt{id: p}
}
return true
}
func mapToArray(m map[string]Poynt) []Poynt {
results := make([]Poynt, len(m))
var i = 0
for _, p := range m {
results[i] = p
i++
}
return results
}
func uniqueId(p Poynt) string {
jp := p.ToJson()
h := sha1.New()
h.Write([]byte(jp.Opt + jp.Logt + jp.Obst))
return hex.EncodeToString(h.Sum(nil))
}