-
Notifications
You must be signed in to change notification settings - Fork 1
/
bean.go
176 lines (153 loc) · 4.12 KB
/
bean.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
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
171
172
173
174
175
176
// Copyright (c) 2023, donnie <donnie4w@gmail.com>
// All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
// github.com/donnie4w/tldb
//
package level1
import (
"fmt"
"strings"
. "github.com/donnie4w/gofer/buffer"
. "github.com/donnie4w/tldb/container"
. "github.com/donnie4w/tldb/key"
"github.com/donnie4w/tldb/log"
. "github.com/donnie4w/tldb/stub"
"github.com/donnie4w/tldb/sys"
"github.com/donnie4w/tldb/util"
)
func newDataBeen(isLocal bool, uuids []int64, value []byte) (db *DataBeen) {
db = NewDataBeen()
db.IsLocal, db.Uuids, db.Value = isLocal, uuids, value
return
}
func decodeDataBeen(bs []byte) (_data *DataBeen) {
switch _DTYPE(bs[0]) {
case T:
// _data, _ = util.TDecode(bs[1:], NewDataBeen())
_data = DecodeDataBeen(bs[1:])
case Z:
if _r, err := util.ZlibUnCz(bs[1:]); err == nil {
// _data, _ = util.TDecode(_r, NewDataBeen())
_data = DecodeDataBeen(_r)
}
}
return
}
func PrintBeen(bs []byte) string {
d := decodeDataBeen(bs)
return fmt.Sprint("[isLocal:]", d.IsLocal, "[uuids:]", d.Uuids, "[value:]", string(d.Value))
}
func transfer(kvmap map[string][]byte, isLocal bool, excuUUids []int64) map[string][]byte {
if isLocal {
for k, v := range kvmap {
if strings.HasPrefix(k, KEY2_ID_) {
var t = T
// bs := util.TEncode(newDataBeen(isLocal, nil, v))
edb := EncodeDataBean(newDataBeen(isLocal, nil, v))
defer edb.Free()
bs := edb.Bytes()
if sys.DATAZLIB {
if _r, err := util.ZlibCz(bs); err == nil {
bs = _r
t = Z
}
}
kvmap[k] = append([]byte{byte(t)}, bs...)
} else {
kvmap[k] = v
}
}
} else {
for k, v := range kvmap {
if strings.HasPrefix(k, KEY2_ID_) {
var t = T
// bs := util.TEncode(newDataBeen(isLocal, excuUUids, nil))
edb := EncodeDataBean(newDataBeen(isLocal, nil, v))
defer edb.Free()
bs := edb.Bytes()
if _r, err := util.ZlibCz(bs); err == nil {
bs = _r
t = Z
}
kvmap[k] = append([]byte{byte(t)}, bs...)
} else {
kvmap[k] = v
}
}
}
return kvmap
}
// ///////////////////////////////////////////////////////////////
var incrExcuPool = newIncrPool()
type _incrPool struct {
pool *LruCache[*int64]
}
func newIncrPool() *_incrPool {
return &_incrPool{pool: (NewLruCache[*int64](1 << 20))}
}
func (this *_incrPool) add(key string, value *int64) {
this.pool.Add(key, value)
}
func (this *_incrPool) del(key string) {
this.pool.Remove(key)
}
func (this *_incrPool) delMulti(keys []string) {
this.pool.RemoveMulti(keys)
}
func (this *_incrPool) get(key string) (v *int64, b bool) {
// defer netLock.RUnlock(key)
// netLock.RLock(key)
return this.pool.Get(key)
}
func (this *_incrPool) getWithoutLock(key string) (v *int64, b bool) {
return this.pool.Get(key)
}
// ///////////////////////////////////////////////////////////////
func newPonBean(txid int64, ptype int16, optype int16, uuids, clusUUids []int64) (pb *PonBean) {
pb = NewPonBean()
pb.Txid = txid
pb.ExecNode = uuids
pb.ClusterNode = clusUUids
pb.OpType = optype
pb.Ptype = ptype
pb.Fromuuid = sys.UUID
pb.IsFirst = false
pb.IsPass = true
pb.IsExcu = false
pb.IsVerify = false
pb.DoCommit = true
pb.Mode = 0
return
}
type _DTYPE byte
var T _DTYPE = 0
var Z _DTYPE = 1
// ////////////////////////////////////////////////////////////////////////////////
type batchLog struct {
batch *BatchPacket
txid int64
timenano int64
}
// STEP+batchLog
func (this *batchLog) toBuffer() (write *Buffer) {
bpbuf := EncodeBatchPacket(this.batch)
defer bpbuf.Free()
batchBys := util.SnappyEncode(bpbuf.Bytes())
write = NewBufferByPool()
write.Write(log.STEP)
write.Write(util.Int32ToBytes(4 + 8 + 8 + int32(len(batchBys)))) //4
write.Write(util.Int64ToBytes(this.timenano)) //8
write.Write(util.Int64ToBytes(this.txid)) //8
write.Write(batchBys)
return
}
func parseToBatchLog(bs []byte) (bl *batchLog) {
defer errRecover()
timenano := util.BytesToInt64(bs[4:12])
txid := util.BytesToInt64(bs[12:20])
batch := DecodeBatchPacket(util.SnappyDecode(bs[20:]))
bl = &batchLog{batch, txid, timenano}
return
}