forked from lni/dragonboat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
95 lines (83 loc) · 2.09 KB
/
context.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright 2017-2019 Lei Ni (nilei81@gmail.com) and other contributors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package logdb
import (
"github.com/vyevenko/dragonboat/v3/internal/logdb/kv"
pb "github.com/vyevenko/dragonboat/v3/raftpb"
)
// context is an IContext implementation suppose to be owned and used
// by a single thread throughout its life time.
type context struct {
size uint64
maxSize uint64
eb pb.EntryBatch
lb pb.EntryBatch
key *Key
val []byte
wb kv.IWriteBatch
}
// newContext creates a new RDB context instance.
func newContext(size uint64, maxSize uint64) *context {
ctx := &context{
size: size,
maxSize: maxSize,
key: newKey(maxKeySize, nil),
val: make([]byte, size),
}
ctx.lb.Entries = make([]pb.Entry, 0, batchSize)
ctx.eb.Entries = make([]pb.Entry, 0, batchSize)
return ctx
}
func (c *context) Destroy() {
if c.wb != nil {
c.wb.Destroy()
}
c.val = nil
c.lb.Entries = nil
c.eb.Entries = nil
}
func (c *context) Reset() {
if c.wb != nil {
c.wb.Clear()
}
}
func (c *context) GetKey() IReusableKey {
return c.key
}
func (c *context) GetValueBuffer(sz uint64) []byte {
if sz <= c.size {
return c.val
}
val := make([]byte, sz)
if sz < c.maxSize {
c.size = sz
c.val = val
}
return val
}
func (c *context) GetEntryBatch() pb.EntryBatch {
return c.eb
}
func (c *context) GetLastEntryBatch() pb.EntryBatch {
return c.lb
}
func (c *context) GetWriteBatch() interface{} {
return c.wb
}
func (c *context) SetWriteBatch(wb interface{}) {
if c.wb != nil {
panic("c.wb is not nil")
}
c.wb = wb.(kv.IWriteBatch)
}