-
Notifications
You must be signed in to change notification settings - Fork 14
/
chunk.go
41 lines (32 loc) · 817 Bytes
/
chunk.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
package protocol
import (
"regexp"
"strconv"
"strings"
)
var filenameRegexp = regexp.MustCompile("^chunk([0-9]+)$")
// Chunk is a piece of data that contains the messages that were written to it.
// It can be incomplete which means that it currently being written into.
type Chunk struct {
Name string `json:"name"`
Complete bool `json:"complete"`
Size uint64 `json:"size"`
}
func ParseChunkFileName(filename string) (instance string, chunkIdx int) {
idx := strings.LastIndexByte(filename, '-')
if idx < 0 {
return "", 0
}
instance = filename[0:idx]
chunkName := filename[idx+1:]
var err error
res := filenameRegexp.FindStringSubmatch(chunkName)
if res == nil {
return "", 0
}
chunkIdx, err = strconv.Atoi(res[1])
if err != nil {
return "", 0
}
return instance, chunkIdx
}