forked from galaxydi/go-loghub
-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathsub_store.go
73 lines (68 loc) · 1.44 KB
/
sub_store.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
package sls
// SubStoreKey key define
type SubStoreKey struct {
Name string `json:"name"`
Type string `json:"type"`
}
// IsValid ...
func (s *SubStoreKey) IsValid() bool {
if len(s.Name) == 0 {
return false
}
if s.Type != "text" &&
s.Type != "long" &&
s.Type != "double" {
return false
}
return true
}
// SubStore define
type SubStore struct {
Name string `json:"name,omitempty"`
TTL int `json:"ttl"`
SortedKeyCount int `json:"sortedKeyCount"`
TimeIndex int `json:"timeIndex"`
Keys []SubStoreKey `json:"keys"`
}
// NewSubStore create a new sorted sub store
func NewSubStore(name string,
ttl int,
sortedKeyCount int,
timeIndex int,
keys []SubStoreKey) *SubStore {
sss := &SubStore{
Name: name,
TTL: ttl,
SortedKeyCount: sortedKeyCount,
TimeIndex: timeIndex,
Keys: keys,
}
if sss.IsValid() {
return sss
}
return nil
}
// IsValid ...
func (s *SubStore) IsValid() bool {
if s.SortedKeyCount <= 0 || s.SortedKeyCount >= len(s.Keys) {
return false
}
if s.TimeIndex >= len(s.Keys) || s.TimeIndex < s.SortedKeyCount {
return false
}
if s.TTL <= 0 || s.TTL > 3650 {
return false
}
for index, key := range s.Keys {
if !key.IsValid() {
return false
}
if index == s.TimeIndex && key.Type != "long" {
return false
}
if index < s.SortedKeyCount && key.Type == "double" {
return false
}
}
return true
}