-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
433 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package tester_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/micvbang/simple-event-broker/internal/infrastructure/tester" | ||
"github.com/micvbang/simple-event-broker/internal/sebrecords" | ||
) | ||
|
||
func TestBatchIndividualRecords(t *testing.T) { | ||
batch := sebrecords.BatchFromRecords([][]byte{{1}, {2}, {3}}) | ||
|
||
tests := map[string]struct { | ||
start int | ||
end int | ||
err error | ||
expected [][]byte | ||
}{ | ||
// "start > end": { | ||
// start: 1, | ||
// end: 0, | ||
// err: seb.ErrBadInput, | ||
// }, | ||
// "end out of bounds": { | ||
// start: 0, | ||
// end: batch.Len() + 1, | ||
// err: seb.ErrOutOfBounds, | ||
// }, | ||
// "both out of bounds": { | ||
// start: batch.Len() + 1, | ||
// end: batch.Len() + 2, | ||
// err: seb.ErrOutOfBounds, | ||
// }, | ||
"first": { | ||
start: 0, | ||
end: 1, | ||
expected: [][]byte{{1}}, | ||
}, | ||
"second": { | ||
start: 1, | ||
end: 2, | ||
expected: [][]byte{{2}}, | ||
}, | ||
"third": { | ||
start: 2, | ||
end: 3, | ||
expected: [][]byte{{3}}, | ||
}, | ||
"first two": { | ||
start: 0, | ||
end: 2, | ||
expected: [][]byte{{1}, {2}}, | ||
}, | ||
"second two": { | ||
start: 1, | ||
end: 3, | ||
expected: [][]byte{{2}, {3}}, | ||
}, | ||
"all": { | ||
start: 0, | ||
end: 3, | ||
expected: [][]byte{{1}, {2}, {3}}, | ||
}, | ||
} | ||
|
||
for name, test := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
tester.BatchIndividualRecords(t, batch, test.start, test.end) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package sebrecords | ||
|
||
import ( | ||
"fmt" | ||
|
||
seb "github.com/micvbang/simple-event-broker" | ||
) | ||
|
||
type Batch struct { | ||
sizes []uint32 | ||
data []byte | ||
indexes []int32 | ||
} | ||
|
||
func NewBatch(recordSizes []uint32, data []byte) Batch { | ||
indexes := make([]int32, len(recordSizes)+1) | ||
index := int32(0) | ||
for i, recordSize := range recordSizes { | ||
indexes[i] = index | ||
index += int32(recordSize) | ||
} | ||
indexes[len(recordSizes)] = int32(len(data)) | ||
|
||
return Batch{ | ||
sizes: recordSizes, | ||
data: data, | ||
indexes: indexes, | ||
} | ||
} | ||
|
||
func BatchFromRecords(records [][]byte) Batch { | ||
totalSize := 0 | ||
recordSizes := make([]uint32, len(records)) | ||
for i, record := range records { | ||
recordSizes[i] = uint32(len(record)) | ||
totalSize += len(record) | ||
} | ||
|
||
data := make([]byte, 0, totalSize) | ||
for _, record := range records { | ||
data = append(data, record...) | ||
} | ||
|
||
return NewBatch(recordSizes, data) | ||
} | ||
|
||
func (b Batch) Len() int { | ||
return len(b.sizes) | ||
} | ||
|
||
func (b Batch) Sizes() []uint32 { | ||
return b.sizes | ||
} | ||
|
||
func (b Batch) Data() []byte { | ||
return b.data | ||
} | ||
|
||
func (b Batch) Records(startIndex int, endIndex int) ([]byte, error) { | ||
if startIndex >= len(b.sizes) || endIndex > len(b.sizes) { | ||
return nil, seb.ErrOutOfBounds | ||
} | ||
|
||
if startIndex >= endIndex { | ||
return nil, fmt.Errorf("%w: start (%d) must be smaller than end (%d)", seb.ErrBadInput, startIndex, endIndex) | ||
} | ||
|
||
startByte, endByte := b.indexes[startIndex], b.indexes[endIndex] | ||
return b.data[startByte:endByte], nil | ||
} | ||
|
||
func (b Batch) IndividualRecords(startIndex int, endIndex int) ([][]byte, error) { | ||
recordsData, err := b.Records(startIndex, endIndex) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
records := make([][]byte, endIndex-startIndex) | ||
bytesUsed := uint32(0) | ||
for i := range records { | ||
size := b.sizes[startIndex+i] | ||
records[i] = recordsData[bytesUsed : bytesUsed+size] | ||
bytesUsed += size | ||
} | ||
return records, nil | ||
} |
Oops, something went wrong.