forked from pengtianyue025/cubefs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
obj_extent_key.go
154 lines (142 loc) · 4.04 KB
/
obj_extent_key.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
// Copyright 2022 The CubeFS 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 proto
import (
"bytes"
"encoding/binary"
"fmt"
"github.com/cubefs/cubefs/util/btree"
)
type Blob struct {
MinBid uint64
Count uint64
Vid uint64
}
// ExtentKey defines the extent key struct.
type ObjExtentKey struct {
Cid uint64 // cluster id
CodeMode uint8 // EC encode and decode mode
BlobSize uint32 // block size
BlobsLen uint32 // blob array length
Size uint64 // objExtentKey size
Blobs []Blob
FileOffset uint64 // obj offset in file
Crc uint32
}
// String returns the string format of the extentKey.
func (k ObjExtentKey) String() string {
return fmt.Sprintf("ObjExtentKey{FileOffset(%v),Cid(%v),CodeMode(%v),BlobSize(%v),BlobsLen(%v),Blobs(%v),Size(%v),Crc(%v)}", k.FileOffset, k.Cid, k.CodeMode, k.BlobSize, k.BlobsLen, k.Blobs, k.Size, k.Crc)
}
// Less defines the less comparator.
func (k *ObjExtentKey) Less(than btree.Item) bool {
that := than.(*ObjExtentKey)
return k.FileOffset < that.FileOffset
}
// Marshal marshals the obj extent key.
func (k *ObjExtentKey) Copy() btree.Item {
return k
}
func (k *ObjExtentKey) IsEquals(obj *ObjExtentKey) bool {
if k.FileOffset != obj.FileOffset {
return false
}
if k.Cid != obj.Cid {
return false
}
if k.CodeMode != obj.CodeMode {
return false
}
if k.BlobSize != obj.BlobSize {
return false
}
if k.BlobsLen != obj.BlobsLen {
return false
}
if k.Size != obj.Size {
return false
}
if k.Crc != obj.Crc {
return false
}
if len(k.Blobs) > 0 {
for i := len(k.Blobs) - 1; i >= 0; i-- {
if k.Blobs[i].Count != obj.Blobs[i].Count || k.Blobs[i].MinBid != obj.Blobs[i].MinBid || k.Blobs[i].Vid != obj.Blobs[i].Vid {
return false
}
}
}
return true
}
// MarshalBinary marshals the binary format of the extent key.
func (k *ObjExtentKey) MarshalBinary() ([]byte, error) {
buf := bytes.NewBuffer(make([]byte, 0))
if err := binary.Write(buf, binary.BigEndian, uint32(len(k.Blobs))); err != nil {
return nil, err
}
if err := binary.Write(buf, binary.BigEndian, k.FileOffset); err != nil {
return nil, err
}
if err := binary.Write(buf, binary.BigEndian, k.Size); err != nil {
return nil, err
}
if err := binary.Write(buf, binary.BigEndian, k.Crc); err != nil {
return nil, err
}
if err := binary.Write(buf, binary.BigEndian, k.CodeMode); err != nil {
return nil, err
}
if err := binary.Write(buf, binary.BigEndian, k.Cid); err != nil {
return nil, err
}
if err := binary.Write(buf, binary.BigEndian, k.BlobSize); err != nil {
return nil, err
}
if err := binary.Write(buf, binary.BigEndian, k.Blobs); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func (k *ObjExtentKey) UnmarshalBinary(buf *bytes.Buffer) (err error) {
if err = binary.Read(buf, binary.BigEndian, &k.BlobsLen); err != nil {
return
}
if err = binary.Read(buf, binary.BigEndian, &k.FileOffset); err != nil {
return
}
if err = binary.Read(buf, binary.BigEndian, &k.Size); err != nil {
return
}
if err = binary.Read(buf, binary.BigEndian, &k.Crc); err != nil {
return
}
if err = binary.Read(buf, binary.BigEndian, &k.CodeMode); err != nil {
return
}
if err = binary.Read(buf, binary.BigEndian, &k.Cid); err != nil {
return
}
if err = binary.Read(buf, binary.BigEndian, &k.BlobSize); err != nil {
return
}
blobs := make([]Blob, 0, int(k.BlobsLen))
for i := 0; i < int(k.BlobsLen); i++ {
tmpBlob := Blob{}
if err = binary.Read(buf, binary.BigEndian, &tmpBlob); err != nil {
return
}
blobs = append(blobs, tmpBlob)
}
k.Blobs = blobs
return
}