/
dentry.go
209 lines (191 loc) · 5.66 KB
/
dentry.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright 2018 The Chubao 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 metanode
import (
"bytes"
"encoding/binary"
)
// Dentry wraps necessary properties of the `dentry` information in file system.
// Marshal exporterKey:
// +-------+----------+------+
// | item | ParentId | Name |
// +-------+----------+------+
// | bytes | 8 | rest |
// +-------+----------+------+
// Marshal value:
// +-------+-------+------+
// | item | Inode | Type |
// +-------+-------+------+
// | bytes | 8 | 4 |
// +-------+-------+------+
// Marshal entity:
// +-------+-----------+--------------+-----------+--------------+
// | item | KeyLength | MarshaledKey | ValLength | MarshaledVal |
// +-------+-----------+--------------+-----------+--------------+
// | bytes | 4 | KeyLength | 4 | ValLength |
// +-------+-----------+--------------+-----------+--------------+
type Dentry struct {
ParentId uint64 // FileID value of the parent inode.
Name string // Name of the current dentry.
Inode uint64 // FileID value of the current inode.
Type uint32
}
type DentryBatch []*Dentry
// Marshal marshals a dentry into a byte array.
func (d *Dentry) Marshal() (result []byte, err error) {
keyBytes := d.MarshalKey()
valBytes := d.MarshalValue()
keyLen := uint32(len(keyBytes))
valLen := uint32(len(valBytes))
buff := bytes.NewBuffer(make([]byte, 0))
buff.Grow(64)
if err = binary.Write(buff, binary.BigEndian, keyLen); err != nil {
return
}
if _, err = buff.Write(keyBytes); err != nil {
return
}
if err = binary.Write(buff, binary.BigEndian, valLen); err != nil {
}
if _, err = buff.Write(valBytes); err != nil {
return
}
result = buff.Bytes()
return
}
// Unmarshal unmarshals the dentry from a byte array.
func (d *Dentry) Unmarshal(raw []byte) (err error) {
var (
keyLen uint32
valLen uint32
)
buff := bytes.NewBuffer(raw)
if err = binary.Read(buff, binary.BigEndian, &keyLen); err != nil {
return
}
keyBytes := make([]byte, keyLen)
if _, err = buff.Read(keyBytes); err != nil {
return
}
if err = d.UnmarshalKey(keyBytes); err != nil {
return
}
if err = binary.Read(buff, binary.BigEndian, &valLen); err != nil {
return
}
valBytes := make([]byte, valLen)
if _, err = buff.Read(valBytes); err != nil {
return
}
err = d.UnmarshalValue(valBytes)
return
}
// Marshal marshals the dentryBatch into a byte array.
func (d DentryBatch) Marshal() ([]byte, error) {
buff := bytes.NewBuffer(make([]byte, 0))
if err := binary.Write(buff, binary.BigEndian, uint32(len(d))); err != nil {
return nil, err
}
for _, dentry := range d {
bs, err := dentry.Marshal()
if err != nil {
return nil, err
}
if err = binary.Write(buff, binary.BigEndian, uint32(len(bs))); err != nil {
return nil, err
}
if _, err := buff.Write(bs); err != nil {
return nil, err
}
}
return buff.Bytes(), nil
}
// Unmarshal unmarshals the dentryBatch.
func DentryBatchUnmarshal(raw []byte) (DentryBatch, error) {
buff := bytes.NewBuffer(raw)
var batchLen uint32
if err := binary.Read(buff, binary.BigEndian, &batchLen); err != nil {
return nil, err
}
result := make(DentryBatch, 0, int(batchLen))
var dataLen uint32
for j := 0; j < int(batchLen); j++ {
if err := binary.Read(buff, binary.BigEndian, &dataLen); err != nil {
return nil, err
}
data := make([]byte, int(dataLen))
if _, err := buff.Read(data); err != nil {
return nil, err
}
den := &Dentry{}
if err := den.Unmarshal(data); err != nil {
return nil, err
}
result = append(result, den)
}
return result, nil
}
// Less tests whether the current dentry is less than the given one.
// This method is necessary fot B-Tree item implementation.
func (d *Dentry) Less(than BtreeItem) (less bool) {
dentry, ok := than.(*Dentry)
less = ok && ((d.ParentId < dentry.ParentId) || ((d.ParentId == dentry.ParentId) && (d.Name < dentry.Name)))
return
}
func (d *Dentry) Copy() BtreeItem {
newDentry := *d
return &newDentry
}
// MarshalKey is the bytes version of the MarshalKey method which returns the byte slice result.
func (d *Dentry) MarshalKey() (k []byte) {
buff := bytes.NewBuffer(make([]byte, 0))
buff.Grow(32)
if err := binary.Write(buff, binary.BigEndian, &d.ParentId); err != nil {
panic(err)
}
buff.Write([]byte(d.Name))
k = buff.Bytes()
return
}
// UnmarshalKey unmarshals the exporterKey from bytes.
func (d *Dentry) UnmarshalKey(k []byte) (err error) {
buff := bytes.NewBuffer(k)
if err = binary.Read(buff, binary.BigEndian, &d.ParentId); err != nil {
return
}
d.Name = string(buff.Bytes())
return
}
// MarshalValue marshals the exporterKey to bytes.
func (d *Dentry) MarshalValue() (k []byte) {
buff := bytes.NewBuffer(make([]byte, 0))
buff.Grow(12)
if err := binary.Write(buff, binary.BigEndian, &d.Inode); err != nil {
panic(err)
}
if err := binary.Write(buff, binary.BigEndian, &d.Type); err != nil {
panic(err)
}
k = buff.Bytes()
return
}
// UnmarshalValue unmarshals the value from bytes.
func (d *Dentry) UnmarshalValue(val []byte) (err error) {
buff := bytes.NewBuffer(val)
if err = binary.Read(buff, binary.BigEndian, &d.Inode); err != nil {
return
}
err = binary.Read(buff, binary.BigEndian, &d.Type)
return
}