-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemtable.go
More file actions
170 lines (128 loc) · 3.28 KB
/
Copy pathmemtable.go
File metadata and controls
170 lines (128 loc) · 3.28 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package main
import (
"encoding/binary"
"errors"
"fmt"
"github.com/huandu/skiplist"
"os"
"sync"
"time"
)
type MemtableEntry struct {
value string
tombstone bool
timestamp uint64
}
type Memtable struct {
currentMemtable *skiplist.SkipList
readOnlyMemtable *skiplist.SkipList
currentMemtableLock sync.RWMutex
flushLock sync.Mutex
}
func (m *Memtable) Get(key string) (string, error) {
m.flushLock.Lock()
m.currentMemtableLock.RLock()
defer m.currentMemtableLock.RUnlock()
defer m.flushLock.Unlock()
val, ok := m.currentMemtable.GetValue(key)
if !ok {
val, ok = m.readOnlyMemtable.GetValue(key)
if !ok {
return "", errors.New("Key does not exist!")
}
entry := val.(MemtableEntry)
if entry.tombstone {
return "", errors.New("Key does not exist!")
}
return entry.value, nil
}
entry := val.(MemtableEntry)
if entry.tombstone {
return "", errors.New("Key does not exist!")
}
return entry.value, nil
}
func (m *Memtable) Put(key string, value string, timestamp int64) {
m.flushLock.Lock()
m.currentMemtableLock.Lock()
entry := MemtableEntry{
value: value,
tombstone: false,
timestamp: uint64(timestamp),
}
m.currentMemtable.Set(key, entry)
m.flushLock.Unlock()
m.currentMemtableLock.Unlock()
}
func (m *Memtable) Delete(key string, timestamp int64) {
m.flushLock.Lock()
m.currentMemtableLock.Lock()
deletedEntry := MemtableEntry{
value: "",
tombstone: true,
timestamp: uint64(timestamp),
}
m.currentMemtable.Set(key, deletedEntry)
m.flushLock.Unlock()
m.currentMemtableLock.Unlock()
}
func (m *Memtable) MemtableSize() int {
return m.currentMemtable.Len()
}
func (m *Memtable) TriggerBackgroundFlush() {
m.flushLock.Lock()
defer m.flushLock.Unlock()
m.readOnlyMemtable = m.currentMemtable
m.currentMemtable = skiplist.New(skiplist.StringAsc)
timestamp := time.Now().UnixNano()
FlushToFile(timestamp, m.readOnlyMemtable)
}
func FlushToFile(timestamp int64, s *skiplist.SkipList) {
indexFilePath := fmt.Sprintf("index-%d.bin", timestamp)
dataFilePath := fmt.Sprintf("data-%d.bin", timestamp)
walFilePath := fmt.Sprintf("wal-%d.bin", timestamp)
indexFilePathTemp := fmt.Sprintf("%s.temp", indexFilePath)
dataFilePathTemp := fmt.Sprintf("%s.temp", dataFilePath)
el := s.Front()
indexFile, err := os.Create(indexFilePathTemp)
if err != nil {
panic(err)
}
dataFile, err := os.Create(dataFilePathTemp)
if err != nil {
panic(err)
}
dataOffset := 0
indexOffset := 0
for el != nil {
strKey := el.Key().(string)
val := el.Value.(MemtableEntry)
binKey := []byte(strKey)
binKeySize := binary.Size(binKey)
binValue := []byte(val.value)
binValueSize := binary.Size(binValue)
data := DataEntry{
keySize: uint32(binKeySize),
valueSize: uint32(binValueSize),
timestamp: val.timestamp,
tombstone: val.tombstone,
key: strKey,
value: val.value,
}
index := IndexEntry{
keySize: uint32(binKeySize),
key: strKey,
dataOffset: uint32(dataOffset),
}
WriteDataRow(dataFile, &data)
WriteIndexRow(indexFile, &index)
dataOffset += data.BinarySize()
indexOffset += index.BinarySize()
el = el.Next()
}
indexFile.Close()
dataFile.Close()
os.Rename(indexFilePathTemp, indexFilePath)
os.Rename(dataFilePathTemp, dataFilePath)
os.Remove(walFilePath)
}