Skip to content

Commit

Permalink
remove cryptic literals
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed May 12, 2023
1 parent dd212f0 commit 05cba9f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
17 changes: 17 additions & 0 deletions internal/chunk/chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ const (
chanInfoPrefix = "ic"
)

// Chunk ID categories
const (
catFile = 'f'
catInfo = 'i'
catList = 'l'
)

// ID returns a Group ID for the chunk.
func (c *Chunk) ID() GroupID {
switch c.Type {
Expand Down Expand Up @@ -145,3 +152,13 @@ func (c *Chunk) messageTimestamps() ([]int64, error) {
}
return ts, nil
}

// isInfo returns true, if the chunk is an info chunk.
func (g GroupID) isInfo() bool {
return g[0] == catInfo
}

// isList returns true, if the chunk is a list chunk.
func (g GroupID) isList() bool {
return g[0] == catList
}
13 changes: 7 additions & 6 deletions internal/chunk/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ type decoder interface {
func indexChunks(dec decoder) (index, error) {
idx := make(index, 200) // buffer for 200 chunks to avoid reallocations.

var id GroupID
for i := 0; ; i++ {
offset := dec.InputOffset() // record current offset

Expand All @@ -93,7 +94,8 @@ func indexChunks(dec decoder) (index, error) {
}
return nil, err
}
idx[chunk.ID()] = append(idx[chunk.ID()], offset)
id = chunk.ID()
idx[id] = append(idx[id], offset)
}
return idx, nil
}
Expand Down Expand Up @@ -238,7 +240,7 @@ func (p *File) AllChannelInfos() ([]slack.Channel, error) {
if len(id) == 0 {
return nil, fmt.Errorf("internal error: invalid id: %q", id)
}
if id[0] == 'i' && id[1] == 'c' {
if strings.HasPrefix(string(id), chanInfoPrefix) {
offsets = append(offsets, off...)
}
}
Expand All @@ -247,6 +249,7 @@ func (p *File) AllChannelInfos() ([]slack.Channel, error) {
})
}

// int64s implements sort.Interface for []int64.
type int64s []int64

func (a int64s) Len() int { return len(a) }
Expand Down Expand Up @@ -322,7 +325,7 @@ func (p *File) AllChannelIDs() []string {
var ids = make([]string, 0, 1)
for gid := range p.idx {
id := string(gid)
if !strings.Contains(id, ":") && id[0] != 'i' && id[0] != 'l' {
if !strings.Contains(id, ":") && !gid.isInfo() && !gid.isList() {
ids = append(ids, id)
}
}
Expand Down Expand Up @@ -357,7 +360,7 @@ func (f *File) offsetTimestamps() (offts, error) {
var ret = make(offts, f.idx.OffsetCount())
for id, offsets := range f.idx {
switch id[0] {
case 'i', 'f', 'l': // ignoring files, information and list chunks
case catInfo, catFile, catList: // ignoring files, information and list chunks
continue
}
for _, offset := range offsets {
Expand Down Expand Up @@ -408,8 +411,6 @@ func (f *File) Sorted(ctx context.Context, desc bool, fn func(ts time.Time, m *s
ctx, task := trace.NewTask(ctx, "file.Sorted")
defer task.End()

trace.Log(ctx, "mutex", "lock")

rgnOt := trace.StartRegion(ctx, "offsetTimestamps")
ots, err := f.offsetTimestamps()
rgnOt.End()
Expand Down

0 comments on commit 05cba9f

Please sign in to comment.