This repository has been archived by the owner on Jan 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory_storage.go
89 lines (72 loc) · 1.7 KB
/
memory_storage.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package dor
import (
"sync"
"time"
)
// LookupMap represents map with domain - rank pairs
type LookupMap map[string]uint32
// MemoryCollection is a struct that is capable to hold data
type memoryCollection struct {
sync.Mutex
Map LookupMap
Description string
Timestamp time.Time
}
// MemoryStorage implements Storage interface as in-memory storage
type MemoryStorage struct {
Maps map[string]*memoryCollection
}
func (mc *memoryCollection) get(d string) (rank uint32, presence bool) {
rank, pr := mc.Map[d]
return rank, pr
}
// Get implements Get method of the Storage interface
func (ms *MemoryStorage) Get(d string, sources ...string) ([]*Entry, error) {
var ranks []*Entry
for k := range ms.Maps {
if len(sources) > 0 && !sliceContains(sources, ms.Maps[k].Description) {
continue
}
rank, prs := ms.Maps[k].get(d)
if prs != true {
continue
}
r := &Entry{
Domain: d,
Rank: rank,
Date: ms.Maps[k].Timestamp,
Source: ms.Maps[k].Description,
}
ranks = append(ranks, r)
}
return ranks, nil
}
// GetMore is not supported for the memory storage
func (ms *MemoryStorage) GetMore(d string, lps int, sources ...string) ([]*Entry, error) {
return ms.Get(d, sources...)
}
// Put implements Put method of the Storage interface
func (ms *MemoryStorage) Put(c <-chan *Entry, s string, t time.Time) error {
lm := make(LookupMap)
for r := range c {
lm[r.Domain] = r.Rank
}
if _, ok := ms.Maps[s]; ok {
ms.Maps[s].Map = lm
return nil
}
ms.Maps[s] = &memoryCollection{
Map: lm,
Description: s,
Timestamp: t,
}
return nil
}
func sliceContains(ss []string, s string) bool {
for i := range ss {
if ss[i] == s {
return true
}
}
return false
}