-
Notifications
You must be signed in to change notification settings - Fork 20
/
MemTable.go
80 lines (72 loc) · 1.62 KB
/
MemTable.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
package lsm
import (
"github.com/huiming23344/lsm/config"
"github.com/huiming23344/lsm/kv"
"github.com/huiming23344/lsm/sortTree"
"github.com/huiming23344/lsm/wal"
"log"
"sync"
)
type MemTable struct {
// 内存表
MemoryTree *sortTree.Tree
// WalF 文件句柄
Wal *wal.Wal
swapLock *sync.RWMutex
}
func (m *MemTable) InitMemTree() {
log.Println("Initializing MemTable MemTree...")
m.MemoryTree = &sortTree.Tree{}
m.MemoryTree.Init()
m.swapLock = &sync.RWMutex{}
}
func (m *MemTable) InitWal(dir string) {
log.Println("Initializing MemTable Wal...")
m.Wal = &wal.Wal{}
m.Wal.Init(dir)
}
func (m *MemTable) Swap() *MemTable {
con := config.GetConfig()
m.swapLock.Lock()
tmpTree := m.MemoryTree.Swap()
// 创建一个新的只读内存表
table := &MemTable{
MemoryTree: tmpTree,
Wal: m.Wal,
}
// creat new wal
newWal := &wal.Wal{}
newWal.Init(con.DataDir)
m.Wal = newWal
m.swapLock.Unlock()
return table
}
func (m *MemTable) Search(key string) (kv.Value, kv.SearchResult) {
m.swapLock.RLock()
defer m.swapLock.RUnlock()
return m.MemoryTree.Search(key)
}
func (m *MemTable) Set(key string, value []byte) (kv.Value, bool) {
m.swapLock.RLock()
defer m.swapLock.RUnlock()
oldValue, hasOld := m.MemoryTree.Set(key, value)
m.Wal.Write(kv.Value{
Key: key,
Value: value,
Deleted: false,
})
return oldValue, hasOld
}
func (m *MemTable) Delete(key string) (kv.Value, bool) {
m.swapLock.RLock()
defer m.swapLock.RUnlock()
oldValue, success := m.MemoryTree.Delete(key)
if success {
m.Wal.Write(kv.Value{
Key: key,
Value: nil,
Deleted: true,
})
}
return oldValue, success
}