-
Notifications
You must be signed in to change notification settings - Fork 0
/
crud.go
75 lines (63 loc) · 1.66 KB
/
crud.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
package store
import (
"fmt"
"github.com/google/uuid"
"strings"
)
func (col *Collection) Put(value []byte) []byte {
col.lastPos.mutex.Lock()
defer col.lastPos.mutex.Unlock()
key := uuid.New().String()
fmt.Println("Put : key :", key)
wNoOfBytes, err := col.writer.Write(value)
if err != nil {
panic(err)
}
col.kv[key] = valuePostion{
Offset: col.lastPos.Pos.Offset,
Length: wNoOfBytes,
}
col.lastPos.Pos.Offset = col.lastPos.Pos.Offset + int64(wNoOfBytes)
col.lastPos.Pos.Length = wNoOfBytes
col.SyncIndexToFile()
return []byte(fmt.Sprintf(`{"id":"%s"}`, key))
}
func (col *Collection) Get(key string) []byte {
col.lastPos.mutex.Lock()
rNoOfBytes := col.kv[key].Length
offset := col.kv[key].Offset
col.lastPos.mutex.Unlock()
reacolytes := make([]byte, rNoOfBytes, rNoOfBytes)
_, err := col.reader.ReadAt(reacolytes, offset)
if err != nil {
panic(err)
}
return reacolytes
}
func (col *Collection) Delete(key string) []byte {
col.lastPos.mutex.Lock()
delete(col.kv, key)
col.lastPos.mutex.Unlock()
col.SyncIndexToFile()
return []byte(fmt.Sprintf(`{%s}`, "ok"))
}
func (col *Collection) GetAll() []byte {
col.lastPos.mutex.Lock()
defer col.lastPos.mutex.Unlock()
var values string
for k, _ := range col.kv {
rNoOfBytes := col.kv[k].Length
offset := col.kv[k].Offset
readColBytes := make([]byte, rNoOfBytes, rNoOfBytes)
_, err := col.reader.ReadAt(readColBytes, offset)
if err != nil {
fmt.Println(err)
}
values = values + fmt.Sprintf(`{"%s":"%s"},`, k, string(readColBytes))
}
if len(values) > 0 {
values = strings.TrimRight(values, ",")
}
collection := fmt.Sprintf(`{"%s": [%s]}`, col.name, values)
return []byte(collection)
}