-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstorage.go
59 lines (51 loc) · 1 KB
/
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
package main
import (
"github.com/syndtr/goleveldb/leveldb"
)
type Storage struct {
DB *leveldb.DB
}
func NewStorage(dbname string) (*Storage, error) {
db, err := leveldb.OpenFile(dbname, nil)
if err != nil {
return nil, err
}
return &Storage{DB: db}, nil
}
func (s *Storage) Close() {
s.DB.Close()
}
func (s *Storage) SaveEntry(e *Entry) error {
err := s.DB.Put([]byte(e.ID), e.Bytes(), nil)
if err != nil {
return err
}
return nil
}
func (s *Storage) AllEntries() []*Entry {
result := make([]*Entry, 0)
iter := s.DB.NewIterator(nil, nil)
for iter.Next() {
entry, _ := NewEntryFromBytes(iter.Value())
result = append(result, entry)
}
return result
}
func (s *Storage) GetEntry(id string) *Entry {
data, err := s.DB.Get([]byte(id), nil)
if err != nil {
return nil
}
entry, err := NewEntryFromBytes(data)
if err != nil {
return nil
}
return entry
}
func (s *Storage) DeleteEntry(id string) error {
err := s.DB.Delete([]byte(id), nil)
if err != nil {
return err
}
return nil
}