-
Notifications
You must be signed in to change notification settings - Fork 36
/
log_index.go
140 lines (118 loc) · 3.13 KB
/
log_index.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
// Copyright 2018 The tiglabs raft Authors.
//
// 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 wal
import (
"encoding/binary"
"fmt"
"io"
"github.com/tiglabs/raft/proto"
)
const indexItemSize = 8 + 8 + 4
type indexItem struct {
logindex uint64 // 日志的index
logterm uint64 // 日志的term
offset uint32 // 日志在文件中的偏移
}
type logEntryIndex []indexItem
func (li logEntryIndex) First() uint64 {
if len(li) == 0 {
return 0
}
return li[0].logindex
}
func (li logEntryIndex) Last() uint64 {
size := len(li)
if size == 0 {
return 0
}
return li[size-1].logindex
}
func (li logEntryIndex) Get(i uint64) (item indexItem, err error) {
size := len(li)
if size == 0 {
err = fmt.Errorf("maybe index(%d) is out of bound lastindex(%d)", i, li.Last())
return
}
ibegin := li[0].logindex
iend := li[size-1].logindex
if i < ibegin || i > iend {
err = fmt.Errorf("maybe index(%d) is out of bound lastindex(%d)", i, li.Last())
return
}
return li[i-ibegin], nil
}
func (li logEntryIndex) Append(offset uint32, entry *proto.Entry) logEntryIndex {
return append(li, indexItem{
logindex: entry.Index,
logterm: entry.Term,
offset: offset,
})
}
func (li logEntryIndex) Truncate(i uint64) (logEntryIndex, error) {
if _, err := li.Get(i); err != nil {
return nil, err
}
return li[:i-li[0].logindex], nil
}
func (li logEntryIndex) Len() int {
return len(li)
}
// 实现recordData接口Encode方法
func (li logEntryIndex) Encode(w io.Writer) (err error) {
u32Buf := make([]byte, 4)
u64Buf := make([]byte, 8)
// write index items count
binary.BigEndian.PutUint32(u32Buf, uint32(li.Len()))
if _, err = w.Write(u32Buf); err != nil {
return
}
// write indexs data
for _, item := range li {
// logindex
binary.BigEndian.PutUint64(u64Buf, item.logindex)
if _, err = w.Write(u64Buf); err != nil {
return
}
// logitem
binary.BigEndian.PutUint64(u64Buf, item.logterm)
if _, err = w.Write(u64Buf); err != nil {
return
}
// logoffset
binary.BigEndian.PutUint32(u32Buf, item.offset)
if _, err = w.Write(u32Buf); err != nil {
return
}
}
return
}
// 实现recordData接口Size方法
func (li logEntryIndex) Size() uint64 {
return uint64(4 + li.Len()*indexItemSize)
}
func decodeLogIndex(data []byte) logEntryIndex {
offset := 0
nItems := binary.BigEndian.Uint32(data[offset:])
offset += 4
li := make([]indexItem, nItems)
for i := 0; i < int(nItems); i++ {
li[i].logindex = binary.BigEndian.Uint64(data[offset:])
offset += 8
li[i].logterm = binary.BigEndian.Uint64(data[offset:])
offset += 8
li[i].offset = binary.BigEndian.Uint32(data[offset:])
offset += 4
}
return li
}