forked from mesos/mr-redis
/
InMem.go
62 lines (44 loc) · 1.15 KB
/
InMem.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
package types
//A common datatype that implements an in memeory structure and methods for remembering the Instances
type InMem struct {
I map[string]*Instance //Map of instances
}
func NewInMem() *InMem {
inMem := &InMem{}
inMem.I = make(map[string]*Instance)
return inMem
}
func (inMem *InMem) IsValid(name string) bool {
_, ok := inMem.I[name]
return ok
}
//Use add to add a new Instance entry in the inmemory, throws error if the element already exist
func (inMem *InMem) Add(name string, instance *Instance) (bool, error) {
if inMem.IsValid(name) == true {
return false, nil
}
inMem.I[name] = instance
return true, nil
}
//use this to update an existing value, throws error otherwise
func (inMem *InMem) Update(name string, instance *Instance) (bool, error) {
if inMem.IsValid(name) == false {
return false, nil
}
inMem.I[name] = instance
return true, nil
}
func (inMem *InMem) Delete(name string) (bool, error) {
if inMem.IsValid(name) == false {
return false, nil
}
delete(inMem.I, name)
return true, nil
}
func (inMem *InMem) Get(name string) *Instance {
if i, ok := inMem.I[name]; ok {
return i
} else {
return nil
}
}