-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshard_meta.go
118 lines (104 loc) · 3.34 KB
/
shard_meta.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
package main
import (
"bytes"
"encoding/binary"
"sync"
)
// Metadata on a shard
type ShardMeta struct {
MetaVersion uint32
FileCount uint32
IndexLength uint32
FileMetaLength uint32
ContentsLength uint32
// Lock
mux sync.RWMutex
}
// File count
func (this *Shard) FileCount() uint32 {
this.shardMeta.mux.RLock()
defer this.shardMeta.mux.RUnlock()
return this.shardMeta.FileCount
}
// Set index length
func (this *ShardMeta) SetIndexLength(v uint32) {
this.mux.Lock()
// Should never be empty
if v < 1 {
panic("Index length seems to be invalid")
}
this.IndexLength = v
this.mux.Unlock()
}
// Set contents length
func (this *ShardMeta) SetContentsLength(v uint32) {
this.mux.Lock()
// Empty is acceptable
if v < 0 {
panic("Content length seems to be invalid")
}
this.ContentsLength = v
this.mux.Unlock()
}
// Set file metadata length
func (this *ShardMeta) SetFileMetaLength(v uint32) {
this.mux.Lock()
// Should never be empty
if v < 1 {
panic("File meta length length seems to be invalid")
}
this.FileMetaLength = v
this.mux.Unlock()
}
// To bytes
func (this *ShardMeta) Bytes() []byte {
this.mux.RLock()
defer this.mux.RUnlock()
buf := new(bytes.Buffer)
buf.Write(BINARY_METADATA_MAGIC_STRING) // magic string
binary.Write(buf, binary.BigEndian, this.MetaVersion) // meta version
binary.Write(buf, binary.BigEndian, this.FileCount) // file count
binary.Write(buf, binary.BigEndian, this.IndexLength) // bloom filter length
binary.Write(buf, binary.BigEndian, this.FileMetaLength) // file metadata length
binary.Write(buf, binary.BigEndian, this.ContentsLength) // contents (actual file bytes) length
binary.Write(buf, binary.BigEndian, BINARY_METADATA_LENGTH) // length of the metadata
return buf.Bytes()
}
// From bytes
func (this *ShardMeta) FromBytes(b []byte) {
buf := bytes.NewReader(b)
buf.Seek(int64(len(BINARY_METADATA_MAGIC_STRING)), 0) // Skip magic string
var err error
err = binary.Read(buf, binary.BigEndian, &this.MetaVersion) // meta version
panicErr(err)
err = binary.Read(buf, binary.BigEndian, &this.FileCount) // file count
panicErr(err)
err = binary.Read(buf, binary.BigEndian, &this.IndexLength) // bloom filter length
panicErr(err)
err = binary.Read(buf, binary.BigEndian, &this.FileMetaLength) // file metadata length
panicErr(err)
err = binary.Read(buf, binary.BigEndian, &this.ContentsLength) // contents (actual file bytes) length
panicErr(err)
// We don't have to read the binary metadata length, because we already know
}
// Version
const BINARY_VERSION uint32 = 1
const BINARY_METADATA_LENGTH uint32 = 3 + 4 + 4 + 4 + 4 + 4 + 4
var BINARY_METADATA_MAGIC_STRING []byte = []byte("YXZ")
// New metadata
func newShardMeta() *ShardMeta {
return &ShardMeta{
MetaVersion: BINARY_VERSION,
FileCount: 0,
IndexLength: 0,
FileMetaLength: 0,
}
}
// Binary file format description
// [3]byte - Magic header (string XYZ)
// uint32 - Meta Version - Numeric incremental ID that indicates the version of this file
// uint32 - FileCount - Number of files in this shard
// uint32 - Number of bytes that the metadata takes
// uint32 - IndexLength - Number of bytes that contains the ShardIndex
// uint32 - FileMetaLength - Number of bytes that contains the file metadata contents
// uint32 - ContentsLength - Number of bytes that contain the actual file bytes