-
Notifications
You must be signed in to change notification settings - Fork 1
/
chunk_spilt.go
45 lines (41 loc) · 1.04 KB
/
chunk_spilt.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
package metaserver
import (
"errors"
"math/rand"
"github.com/rfeverything/rfs/internal/proto/rfs"
)
func (e *Entry) SplitToChunks(chunkSize uint64) error {
if e.Chunks != nil {
return errors.New("entry already has chunks")
}
if e.Content == nil || len(e.Content) == 0 {
return errors.New("entry has no content")
}
e.Chunks = make([]*rfs.FileChunk, 0)
for i := uint64(0); i < uint64(len(e.Content)); i += chunkSize {
chunk := &rfs.FileChunk{
Chunkid: rand.Int63(),
Offset: i,
Size: chunkSize,
}
if i+chunkSize > uint64(len(e.Content)) {
chunk.Size = uint64(len(e.Content)) - i
}
chunk.Content = e.Content[i : i+chunk.Size]
e.Chunks = append(e.Chunks, chunk)
}
return nil
}
func (e *Entry) CombineChunksGetContent() error {
if e.Chunks == nil || len(e.Chunks) == 0 {
return errors.New("entry has no chunks")
}
if e.Content != nil {
return errors.New("entry already has content")
}
e.Content = make([]byte, 0)
for _, chunk := range e.Chunks {
e.Content = append(e.Content, chunk.Content...)
}
return nil
}