-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocid.go
More file actions
72 lines (59 loc) · 1.58 KB
/
docid.go
File metadata and controls
72 lines (59 loc) · 1.58 KB
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
package gin
// DocID represents an external document identifier.
type DocID uint64
// DocIDCodec encodes/decodes composite information into a single DocID.
type DocIDCodec interface {
Encode(indices ...int) DocID
Decode(docID DocID) []int
Name() string
}
// IdentityCodec treats the position as the DocID (1:1 mapping).
type IdentityCodec struct{}
func NewIdentityCodec() *IdentityCodec {
return &IdentityCodec{}
}
func (c *IdentityCodec) Encode(indices ...int) DocID {
if len(indices) == 0 {
return 0
}
return DocID(indices[0])
}
func (c *IdentityCodec) Decode(docID DocID) []int {
return []int{int(docID)}
}
func (c *IdentityCodec) Name() string {
return "identity"
}
// RowGroupCodec encodes file index and row group index into a DocID.
// Layout: DocID = fileIndex * rowGroupsPerFile + rgIndex
type RowGroupCodec struct {
rowGroupsPerFile int
}
func NewRowGroupCodec(rowGroupsPerFile int) *RowGroupCodec {
if rowGroupsPerFile <= 0 {
rowGroupsPerFile = 1
}
return &RowGroupCodec{rowGroupsPerFile: rowGroupsPerFile}
}
func (c *RowGroupCodec) Encode(indices ...int) DocID {
if len(indices) < 2 {
if len(indices) == 1 {
return DocID(indices[0])
}
return 0
}
fileIndex, rgIndex := indices[0], indices[1]
return DocID(fileIndex*c.rowGroupsPerFile + rgIndex)
}
func (c *RowGroupCodec) Decode(docID DocID) []int {
id := int(docID)
fileIndex := id / c.rowGroupsPerFile
rgIndex := id % c.rowGroupsPerFile
return []int{fileIndex, rgIndex}
}
func (c *RowGroupCodec) Name() string {
return "rowgroup"
}
func (c *RowGroupCodec) RowGroupsPerFile() int {
return c.rowGroupsPerFile
}