forked from hmarui66/blink-tree-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparent_buf_mgr_dummy.go
64 lines (56 loc) · 1.61 KB
/
parent_buf_mgr_dummy.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
package blink_tree
import (
"github.com/ryogrid/bltree-go-for-embedding/interfaces"
"sync"
"sync/atomic"
)
// for ParentBufMgrDummy
var nectPageID int32 = 0
// this class is ParentBufMgr interface implementation sample
// store data in memory only and don't manage memory usage
type ParentBufMgrDummy struct {
pageMap *sync.Map // key: pageID, value: ParentPage
}
func NewParentBufMgrDummy(baseMap *sync.Map) interfaces.ParentBufMgr {
if baseMap != nil {
// when BufMgr is reconstructed, use the given map
return &ParentBufMgrDummy{pageMap: baseMap}
} else {
// when BufMgr is newly created, create new map
return &ParentBufMgrDummy{pageMap: &sync.Map{}}
}
}
func (p *ParentBufMgrDummy) FetchPPage(pageID int32) interfaces.ParentPage {
if val, ok := p.pageMap.Load(pageID); ok {
ret := val.(interfaces.ParentPage)
tmp := ret.(*ParentPageDummy)
// increment pin count
atomic.AddInt32(&tmp.pincCount, 1)
return ret
} else {
panic("unknown pageID")
}
}
func (p *ParentBufMgrDummy) UnpinPPage(pageID int32, isDirty bool) error {
if val, ok := p.pageMap.Load(pageID); ok {
ppage := val.(interfaces.ParentPage)
ppage.DecPPinCount()
return nil
} else {
panic("unknown pageID")
}
}
func (p *ParentBufMgrDummy) NewPPage() interfaces.ParentPage {
newPageID := atomic.AddInt32(&nectPageID, 1)
newPage := NewParentPageDummy(newPageID, 1, [4096]byte{})
p.pageMap.Store(newPageID, newPage)
return newPage
}
func (p *ParentBufMgrDummy) DeallocatePPage(pageID int32, _isNoWait bool) error {
if _, ok := p.pageMap.Load(pageID); ok {
p.pageMap.Delete(pageID)
return nil
} else {
panic("unknown pageID")
}
}