Skip to content

Commit

Permalink
Add rac.Writer code path for empty input
Browse files Browse the repository at this point in the history
  • Loading branch information
nigeltao committed Apr 7, 2019
1 parent 7e26fe8 commit 0e3d691
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 20 deletions.
23 changes: 19 additions & 4 deletions lib/rac/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,23 @@ func (w *Writer) AddChunk(dRangeSize uint64, primary []byte, secondary OptResour
return nil
}

func writeEmpty(w io.Writer, codec Codec) error {
buf := [32]byte{
0x72, 0xC3, 0x63, 0x01, 0x00, 0x00, 0x00, 0xFF,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, uint8(codec),
0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xFF,
0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01,
}

checksum := crc32.ChecksumIEEE(buf[6:])
checksum ^= checksum >> 16
buf[4] = uint8(checksum >> 0)
buf[5] = uint8(checksum >> 8)

_, err := w.Write(buf[:])
return err
}

// Close writes the RAC index to w.Writer and marks that w accepts no further
// method calls.
//
Expand All @@ -394,12 +411,10 @@ func (w *Writer) Close() error {
}
}

rootNode := node{}
if len(w.leafNodes) == 0 {
rootNode.children = []node{{}}
} else {
rootNode = gather(w.leafNodes)
return writeEmpty(w.Writer, w.Codec)
}
rootNode := gather(w.leafNodes)
indexSize := rootNode.calcEncodedSize(0)

nw := &nodeWriter{
Expand Down
54 changes: 38 additions & 16 deletions lib/rac/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ import (
// to golden output" test is admittedly brittle, as the standard library's zlib
// package's output isn't necessarily stable across Go releases.

const writerWantEmpty = "" +
"00000000 72 c3 63 01 30 14 00 ff 00 00 00 00 00 00 00 ee |r.c.0...........|\n" +
"00000010 20 00 00 00 00 00 01 ff 20 00 00 00 00 00 01 01 | ....... .......|\n"

const writerWantILAEnd = "" +
"00000000 72 c3 63 00 52 72 72 53 73 41 61 61 42 62 62 62 |r.c.RrrSsAaaBbbb|\n" +
"00000010 43 63 63 63 63 63 63 63 63 63 31 32 72 c3 63 05 |Cccccccccc12r.c.|\n" +
Expand Down Expand Up @@ -82,23 +86,36 @@ const writerWantILAStartCPageSize128 = "" +
"00000080 52 72 72 53 73 41 61 61 42 62 62 62 43 63 63 63 |RrrSsAaaBbbbCccc|\n" +
"00000090 63 63 63 63 63 63 31 32 |cccccc12|\n"

func TestWriterILAEndEmpty(t *testing.T) {
if err := testWriter(IndexLocationAtEnd, nil, 0, true); err != nil {
t.Fatal(err)
}
}

func TestWriterILAStartEmpty(t *testing.T) {
tempFile := &bytes.Buffer{}
if err := testWriter(IndexLocationAtStart, tempFile, 0, true); err != nil {
t.Fatal(err)
}
}

func TestWriterILAEndNoTempFile(t *testing.T) {
if err := testWriter(IndexLocationAtEnd, nil, 0); err != nil {
if err := testWriter(IndexLocationAtEnd, nil, 0, false); err != nil {
t.Fatal(err)
}
}

func TestWriterILAEndMemTempFile(t *testing.T) {
tempFile := &bytes.Buffer{}
if err := testWriter(IndexLocationAtEnd, tempFile, 0); err == nil {
if err := testWriter(IndexLocationAtEnd, tempFile, 0, false); err == nil {
t.Fatal("err: got nil, want non-nil")
} else if !strings.HasPrefix(err.Error(), "rac: IndexLocationAtEnd requires") {
t.Fatal(err)
}
}

func TestWriterILAStartNoTempFile(t *testing.T) {
if err := testWriter(IndexLocationAtStart, nil, 0); err == nil {
if err := testWriter(IndexLocationAtStart, nil, 0, false); err == nil {
t.Fatal("err: got nil, want non-nil")
} else if !strings.HasPrefix(err.Error(), "rac: IndexLocationAtStart requires") {
t.Fatal(err)
Expand All @@ -107,7 +124,7 @@ func TestWriterILAStartNoTempFile(t *testing.T) {

func TestWriterILAStartMemTempFile(t *testing.T) {
tempFile := &bytes.Buffer{}
if err := testWriter(IndexLocationAtStart, tempFile, 0); err != nil {
if err := testWriter(IndexLocationAtStart, tempFile, 0, false); err != nil {
t.Fatal(err)
}
}
Expand All @@ -120,32 +137,32 @@ func TestWriterILAStartRealTempFile(t *testing.T) {
defer os.Remove(f.Name())
defer f.Close()

if err := testWriter(IndexLocationAtStart, f, 0); err != nil {
if err := testWriter(IndexLocationAtStart, f, 0, false); err != nil {
t.Fatal(err)
}
}

func TestWriterILAEndCPageSize8(t *testing.T) {
if err := testWriter(IndexLocationAtEnd, nil, 8); err != nil {
if err := testWriter(IndexLocationAtEnd, nil, 8, false); err != nil {
t.Fatal(err)
}
}

func TestWriterILAStartCPageSize4(t *testing.T) {
tempFile := &bytes.Buffer{}
if err := testWriter(IndexLocationAtStart, tempFile, 4); err != nil {
if err := testWriter(IndexLocationAtStart, tempFile, 4, false); err != nil {
t.Fatal(err)
}
}

func TestWriterILAStartCPageSize128(t *testing.T) {
tempFile := &bytes.Buffer{}
if err := testWriter(IndexLocationAtStart, tempFile, 128); err != nil {
if err := testWriter(IndexLocationAtStart, tempFile, 128, false); err != nil {
t.Fatal(err)
}
}

func testWriter(iloc IndexLocation, tempFile io.ReadWriter, cPageSize uint64) error {
func testWriter(iloc IndexLocation, tempFile io.ReadWriter, cPageSize uint64, empty bool) error {
buf := &bytes.Buffer{}
const fakeCodec = Codec(0xEE)
w := &Writer{
Expand All @@ -156,20 +173,25 @@ func testWriter(iloc IndexLocation, tempFile io.ReadWriter, cPageSize uint64) er
CPageSize: cPageSize,
}

// We ignore errors (assigning them to _) from the AddXxx calls. Any
// non-nil errors are sticky, and should be returned by Close.
res0, _ := w.AddResource([]byte("Rrr"))
res1, _ := w.AddResource([]byte("Ss"))
_ = w.AddChunk(0x11, []byte("Aaa"), 0, 0)
_ = w.AddChunk(0x22, []byte("Bbbb"), res0, 0)
_ = w.AddChunk(0x44, []byte("Cccccccccc12"), res0, res1)
if !empty {
// We ignore errors (assigning them to _) from the AddXxx calls. Any
// non-nil errors are sticky, and should be returned by Close.
res0, _ := w.AddResource([]byte("Rrr"))
res1, _ := w.AddResource([]byte("Ss"))
_ = w.AddChunk(0x11, []byte("Aaa"), 0, 0)
_ = w.AddChunk(0x22, []byte("Bbbb"), res0, 0)
_ = w.AddChunk(0x44, []byte("Cccccccccc12"), res0, res1)
}

if err := w.Close(); err != nil {
return err
}
got := hex.Dump(buf.Bytes())

want := ""
switch {
case empty:
want = writerWantEmpty
case (iloc == IndexLocationAtEnd) && (cPageSize == 0):
want = writerWantILAEnd
case (iloc == IndexLocationAtEnd) && (cPageSize == 8):
Expand Down

0 comments on commit 0e3d691

Please sign in to comment.