-
Notifications
You must be signed in to change notification settings - Fork 4
/
snapshot.go
149 lines (136 loc) · 3.05 KB
/
snapshot.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package snapshot
import (
"encoding/gob"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"sync"
)
const (
extension = ".gob"
prefix = "_sc_"
)
var mutexList = make(map[string]*sync.Mutex)
// Collection describes a collection of key value pairs
type Collection struct {
mutex sync.Mutex
dir string
items []string
}
// New return a instance of collection
func New(name string) (*Collection, error) {
if len(name) <= 0 {
return &Collection{}, errors.New("Collection name can not be empty!")
}
//make file path correct
dir := prefix + filepath.Clean(name)
collection := Collection{
dir: dir,
}
//create directory with collection name
return &collection, os.MkdirAll(dir, 0755)
}
//Put store a new key with value in the collection
func (c *Collection) Put(key string, value interface{}) error {
if len(key) <= 0 {
return errors.New("Key can not be empty!")
}
path := filepath.Join(c.dir, key+extension)
m := c.getMutex(path)
m.Lock()
defer m.Unlock()
file, err := os.Create(path)
defer file.Close()
if err == nil {
encoder := gob.NewEncoder(file)
encoder.Encode(value)
}
return err
}
//Get retrieve a value from collection by key
func (c *Collection) Get(key string, value interface{}) error {
if len(key) <= 0 {
return errors.New("Key can not be empty!")
}
path := filepath.Join(c.dir, key+extension)
m := c.getMutex(path)
m.Lock()
defer m.Unlock()
if !c.Has(key) {
return fmt.Errorf("Key %s does not exist!", key)
}
file, err := os.Open(path)
defer file.Close()
if err == nil {
decoder := gob.NewDecoder(file)
err = decoder.Decode(value)
}
return err
}
//Remove delete a key from collection
func (c *Collection) Remove(key string) error {
if len(key) <= 0 {
return errors.New("Key can not be empty!")
}
path := filepath.Join(c.dir, key+extension)
m := c.getMutex(path)
m.Lock()
defer m.Unlock()
if c.Has(key) {
return os.Remove(path)
}
return fmt.Errorf("Key %s does not exist!", key)
}
//Flush delete a collection with its value
func (c *Collection) Flush() error {
if _, err := os.Stat(c.dir); err == nil {
os.RemoveAll(c.dir)
return err
}
return nil
}
//Has check a key exist in the collection
func (c *Collection) Has(key string) bool {
if len(key) <= 0 {
return false
}
path := filepath.Join(c.dir, key+extension)
if _, err := os.Stat(path); !os.IsNotExist(err) {
return true
}
return false
}
//List fetch all items key in collection
func (c *Collection) List() ([]string, error) {
var items []string
files, err := ioutil.ReadDir(c.dir)
if err != nil {
return items, err
}
for _, f := range files {
item := f.Name()
item = strings.Trim(item, extension)
items = append(items, item)
}
return items, err
}
//TotalItem return total item count
func (c *Collection) TotalItem() int {
list, _ := c.List()
return len(list)
}
//populate a package level mutex list
// with key of full path of an item
func (c *Collection) getMutex(path string) *sync.Mutex {
c.mutex.Lock()
defer c.mutex.Unlock()
m, ok := mutexList[path]
if !ok {
m = &sync.Mutex{}
mutexList[path] = m
}
return m
}