Rename rac.Parser as rac.ChunkReader

This commit is contained in:
Nigel Tao committed 2019-08-17 17:59:47 +10:00
1 parent c36b4d79c4
commit 7a82d135ff
4 files changed
+56 -56

No files matched your search

+34 -34
View File
@@ -56,7 +56,7 @@ type Range [2]int64
func (r *Range) Empty() bool { return r[0] == r[1] }
func (r *Range) Size() int64 { return r[1] - r[0] }
// Chunk is a compressed chunk returned by a Parser.
// Chunk is a compressed chunk returned by a ChunkReader.
//
// See the RAC specification for further discussion.
type Chunk struct {
@@ -75,29 +75,29 @@ func nodeSize(arity uint8) int {
return (16 * int(arity)) + 16
}
// pNode is the Parser's representation of a node.
// rNode is the ChunkReader's representation of a node.
//
// None of its methods, other than valid, should be called unless valid returns
// true.
type pNode [4096]byte
type rNode [4096]byte
func (b *pNode) arity() int { return int(b[3]) }
func (b *pNode) codec() Codec { return Codec(b[(8*int(b[3]))+7]) }
func (b *pNode) cPtrMax() int64 { return u48LE(b[(16*int(b[3]))+8:]) }
func (b *pNode) dPtrMax() int64 { return u48LE(b[8*int(b[3]):]) }
func (b *pNode) version() uint8 { return b[(16*int(b[3]))+14] }
func (b *rNode) arity() int { return int(b[3]) }
func (b *rNode) codec() Codec { return Codec(b[(8*int(b[3]))+7]) }
func (b *rNode) cPtrMax() int64 { return u48LE(b[(16*int(b[3]))+8:]) }
func (b *rNode) dPtrMax() int64 { return u48LE(b[8*int(b[3]):]) }
func (b *rNode) version() uint8 { return b[(16*int(b[3]))+14] }
func (b *pNode) cLen(i int) uint8 {
func (b *rNode) cLen(i int) uint8 {
base := (8 * int(b[3])) + 14
return b[(8*i)+base]
}
func (b *pNode) cOff(i int, cBias int64) int64 {
func (b *rNode) cOff(i int, cBias int64) int64 {
base := (8 * int(b[3])) + 8
return cBias + u48LE(b[(8*i)+base:])
}
func (b *pNode) cOffRange(i int, cBias int64) Range {
func (b *rNode) cOffRange(i int, cBias int64) Range {
m := cBias + b.cPtrMax()
if i >= b.arity() {
return Range{m, m}
@@ -111,18 +111,18 @@ func (b *pNode) cOffRange(i int, cBias int64) Range {
return Range{cOff, m}
}
func (b *pNode) dOff(i int, dBias int64) int64 {
func (b *rNode) dOff(i int, dBias int64) int64 {
if i == 0 {
return dBias
}
return dBias + u48LE(b[8*i:])
}
func (b *pNode) dOffRange(i int, dBias int64) Range {
func (b *rNode) dOffRange(i int, dBias int64) Range {
return Range{b.dOff(i, dBias), b.dOff(i+1, dBias)}
}
func (b *pNode) dSize(i int) int64 {
func (b *rNode) dSize(i int) int64 {
x := int64(0)
if i > 0 {
x = u48LE(b[8*i:])
@@ -130,20 +130,20 @@ func (b *pNode) dSize(i int) int64 {
return u48LE(b[(8*i)+8:]) - x
}
func (b *pNode) sTag(i int) uint8 {
func (b *rNode) sTag(i int) uint8 {
base := (8 * int(b[3])) + 15
return b[(8*i)+base]
}
func (b *pNode) tTag(i int) uint8 {
func (b *rNode) tTag(i int) uint8 {
return b[(8*i)+7]
}
func (b *pNode) isLeaf(i int) bool {
func (b *rNode) isLeaf(i int) bool {
return b[(8*i)+7] != 0xFE
}
func (b *pNode) findChunkContaining(dOff int64, dBias int64) int {
func (b *rNode) findChunkContaining(dOff int64, dBias int64) int {
// TODO: binary search instead of linear search.
for i, n := 0, b.arity(); i < n; i++ {
if dOff < b.dOff(i+1, dBias) {
@@ -155,7 +155,7 @@ func (b *pNode) findChunkContaining(dOff int64, dBias int64) int {
panic("rac: internal error: could not find containing chunk")
}
func (b *pNode) chunk(i int, cBias int64, dBias int64) Chunk {
func (b *rNode) chunk(i int, cBias int64, dBias int64) Chunk {
sTag := b.sTag(i)
tTag := b.tTag(i)
return Chunk{
@@ -169,7 +169,7 @@ func (b *pNode) chunk(i int, cBias int64, dBias int64) Chunk {
}
}
func (b *pNode) valid() bool {
func (b *rNode) valid() bool {
// Check the magic and arity.
if (b[0] != magic[0]) || (b[1] != magic[1]) || (b[2] != magic[2]) || (b[3] == 0) {
return false
@@ -231,10 +231,10 @@ func (b *pNode) valid() bool {
return true
}
// Parser parses a RAC file.
// ChunkReader parses a RAC file.
//
// Do not modify its exported fields after calling any of its methods.
type Parser struct {
type ChunkReader struct {
// ReadSeeker is where the RAC-encoded data is read from.
//
// It may also implement io.ReaderAt, in which case its ReadAt method will
@@ -249,7 +249,7 @@ type Parser struct {
// Nil is an invalid value.
ReadSeeker io.ReadSeeker
// initialized is set true after the first call on this Parser.
// initialized is set true after the first call on this ChunkReader.
initialized bool
// rootNodeArity is the root node's arity.
@@ -287,10 +287,10 @@ type Parser struct {
currNodeDBias int64
// currNode is the 4096 byte buffer to hold the current node.
currNode pNode
currNode rNode
}
func (r *Parser) checkParameters() error {
func (r *ChunkReader) checkParameters() error {
if r.ReadSeeker == nil {
r.err = errInvalidReadSeeker
return r.err
@@ -298,7 +298,7 @@ func (r *Parser) checkParameters() error {
return nil
}
func (r *Parser) initialize() error {
func (r *ChunkReader) initialize() error {
if r.err != nil {
return r.err
}
@@ -338,7 +338,7 @@ func (r *Parser) initialize() error {
return nil
}
func (r *Parser) findRootNode() error {
func (r *ChunkReader) findRootNode() error {
// Look at the start of the compressed file.
if err := readAt(r.ReadSeeker, r.currNode[:4], 0); err != nil {
r.err = err
@@ -370,7 +370,7 @@ func (r *Parser) findRootNode() error {
return errInvalidInputMissingRootNode
}
func (r *Parser) tryRootNode(arity uint8, fromEnd bool) (found bool, ioErr error) {
func (r *ChunkReader) tryRootNode(arity uint8, fromEnd bool) (found bool, ioErr error) {
if arity == 0 {
return false, nil
}
@@ -401,7 +401,7 @@ func (r *Parser) tryRootNode(arity uint8, fromEnd bool) (found bool, ioErr error
// load loads a node from the RAC file into r.currNode. It does not check that
// the result is valid, and the caller should do so if it doesn't already know
// that it is valid.
func (r *Parser) load(cOffset int64, arity uint8) error {
func (r *ChunkReader) load(cOffset int64, arity uint8) error {
if arity == 0 {
r.err = errInternalInconsistentArity
return r.err
@@ -414,7 +414,7 @@ func (r *Parser) load(cOffset int64, arity uint8) error {
return nil
}
func (r *Parser) loadAndValidate(cOffset int64,
func (r *ChunkReader) loadAndValidate(cOffset int64,
parentCodec Codec, parentVersion uint8, parentCOffMax int64,
childCBias int64, childDSize int64) error {
@@ -458,7 +458,7 @@ func (r *Parser) loadAndValidate(cOffset int64,
}
// DecompressedSize returns the total size of the decompressed data.
func (r *Parser) DecompressedSize() (int64, error) {
func (r *ChunkReader) DecompressedSize() (int64, error) {
if err := r.initialize(); err != nil {
return 0, err
}
@@ -469,7 +469,7 @@ func (r *Parser) DecompressedSize() (int64, error) {
// dSpaceOffset. That chunk does not necessarily start at dSpaceOffset.
//
// It is an error to seek to a negative value.
func (r *Parser) SeekToChunkContaining(dSpaceOffset int64) error {
func (r *ChunkReader) SeekToChunkContaining(dSpaceOffset int64) error {
if err := r.initialize(); err != nil {
return err
}
@@ -487,7 +487,7 @@ func (r *Parser) SeekToChunkContaining(dSpaceOffset int64) error {
//
// Empty chunks (those that contain no decompressed data, only metadata) are
// skipped.
func (r *Parser) NextChunk() (Chunk, error) {
func (r *ChunkReader) NextChunk() (Chunk, error) {
if err := r.initialize(); err != nil {
return Chunk{}, err
}
@@ -513,7 +513,7 @@ func (r *Parser) NextChunk() (Chunk, error) {
}
}
func (r *Parser) resolveSeekPosition() error {
func (r *ChunkReader) resolveSeekPosition() error {
// Load the root node. It has already been validated, during initialize.
if err := r.load(r.rootNodeCOffset, r.rootNodeArity); err != nil {
return err
+2 -2
View File
@@ -159,12 +159,12 @@ func Example_indexLocationAtStart() {
// original "One sheep.\nTwo sheep\.Three sheep.\n" source.
fmt.Printf("Decoded:\n")
p := &rac.Parser{
r := &rac.ChunkReader{
ReadSeeker: bytes.NewReader(encoded),
}
zr := io.ReadCloser(nil)
for {
chunk, err := p.NextChunk()
chunk, err := r.NextChunk()
if err == io.EOF {
break
} else if err != nil {
+11 -11
View File
@@ -349,12 +349,12 @@ func TestMultiLevelIndex(t *testing.T) {
t.Fatalf("\ngot:\n%s\nwant:\n%s", gotHexDump, wantHexDump)
}
p := &Parser{
r := &ChunkReader{
ReadSeeker: bytes.NewReader(encoded),
}
gotPrimaries := []byte(nil)
for {
c, err := p.NextChunk()
c, err := r.NextChunk()
if err == io.EOF {
break
} else if err != nil {
@@ -401,11 +401,11 @@ loop:
}
encoded := buf.Bytes()
p := &Parser{
r := &ChunkReader{
ReadSeeker: bytes.NewReader(encoded),
}
for n := 0; ; n++ {
if _, err := p.NextChunk(); err == io.EOF {
if _, err := r.NextChunk(); err == io.EOF {
if n != 1000 {
t.Errorf("i=%d: number of chunks: got %d, want %d", i, n, 1000)
continue loop
@@ -428,7 +428,7 @@ func printChunks(chunks []Chunk) string {
return strings.Join(ss, "\n")
}
func TestParser(t *testing.T) {
func TestChunkReader(t *testing.T) {
testCases := []struct {
name string
compressed []byte
@@ -468,11 +468,11 @@ loop:
`DRangeSize:0x44, C0:"Cc...", C1:"Rr...", C2:"Ss..."`
}
p := &Parser{
r := &ChunkReader{
ReadSeeker: bytes.NewReader(tc.compressed),
}
if gotDecompressedSize, err := p.DecompressedSize(); err != nil {
if gotDecompressedSize, err := r.DecompressedSize(); err != nil {
t.Errorf("%q test case: %v", tc.name, err)
continue loop
} else if gotDecompressedSize != wantDecompressedSize {
@@ -485,7 +485,7 @@ loop:
description := &bytes.Buffer{}
prevDRange1 := int64(0)
for {
c, err := p.NextChunk()
c, err := r.NextChunk()
if err == io.EOF {
break
} else if err != nil {
@@ -523,18 +523,18 @@ loop:
}
// NextChunk should return io.EOF.
if _, err := p.NextChunk(); err != io.EOF {
if _, err := r.NextChunk(); err != io.EOF {
t.Errorf("%q test case: NextChunk: got %v, want io.EOF", tc.name, err)
continue loop
}
if err := p.SeekToChunkContaining(0x30); err != nil {
if err := r.SeekToChunkContaining(0x30); err != nil {
t.Errorf("%q test case: SeekToChunkContaining: %v", tc.name, err)
continue loop
}
// NextChunk should return the "Bb..." chunk.
if c, err := p.NextChunk(); err != nil {
if c, err := r.NextChunk(); err != nil {
t.Errorf("%q test case: NextChunk: %v", tc.name, err)
continue loop
} else if got, want := snippet(c.CPrimary), "Bb..."; got != want {
+9 -9
View File
@@ -90,8 +90,8 @@ type Reader struct {
// occurs, all public methods will return that error.
err error
// parser is the low-level RAC parser.
parser Parser
// chunkReader is the low-level RAC chunk reader.
chunkReader ChunkReader
// These two fields combine for a 3-state state machine:
//
@@ -152,7 +152,7 @@ func (r *Reader) initialize() error {
if r.err != nil {
return r.err
}
if r.parser.ReadSeeker != nil {
if r.chunkReader.ReadSeeker != nil {
// We're already initialized.
return nil
}
@@ -164,14 +164,14 @@ func (r *Reader) initialize() error {
r.err = err
return r.err
} else {
r.parser.compressedSize = size
r.chunkReader.compressedSize = size
}
if _, err := r.ReadSeeker.Seek(0, io.SeekStart); err != nil {
r.err = err
return r.err
}
r.parser.ReadSeeker = r.ReadSeeker
r.chunkReader.ReadSeeker = r.ReadSeeker
r.currChunk.R = r.ReadSeeker
return nil
}
@@ -307,7 +307,7 @@ func (r *Reader) readImplicitZeroes(p []byte) (int, error) {
// It may return io.EOF, in which case the Reader stays in "State A", and the
// r.err "sticky error" field stays nil.
func (r *Reader) nextChunk() error {
chunk, err := r.parser.NextChunk()
chunk, err := r.chunkReader.NextChunk()
if err != nil {
if err == io.EOF {
return io.EOF
@@ -349,7 +349,7 @@ func (r *Reader) nextChunk() error {
return r.err
}
rctx, err := codecReader.MakeReaderContext(r.ReadSeeker, r.parser.compressedSize, chunk)
rctx, err := codecReader.MakeReaderContext(r.ReadSeeker, r.chunkReader.compressedSize, chunk)
if err != nil {
if err == io.EOF {
err = io.ErrUnexpectedEOF
@@ -393,7 +393,7 @@ func (r *Reader) Seek(offset int64, whence int) (int64, error) {
case io.SeekCurrent:
pos += offset
case io.SeekEnd:
end, err := r.parser.DecompressedSize()
end, err := r.chunkReader.DecompressedSize()
if err != nil {
r.err = err
return 0, r.err
@@ -408,7 +408,7 @@ func (r *Reader) Seek(offset int64, whence int) (int64, error) {
r.err = errSeekToNegativePosition
return 0, r.err
}
if err := r.parser.SeekToChunkContaining(pos); err != nil {
if err := r.chunkReader.SeekToChunkContaining(pos); err != nil {
r.err = err
return 0, r.err
}