diff options
| author | chrislu <chris.lu@gmail.com> | 2022-06-04 19:10:52 -0700 |
|---|---|---|
| committer | chrislu <chris.lu@gmail.com> | 2022-06-04 19:10:52 -0700 |
| commit | 685643d506b7593cfe2b66c066f7f9a68655a2ff (patch) | |
| tree | 6506eeb3d58b8c4fab2b5f37eccc2b5e00f14f21 | |
| parent | 663bc5dc232ea304db0d3570f643c5dd94cb9eda (diff) | |
| download | seaweedfs-685643d506b7593cfe2b66c066f7f9a68655a2ff.tar.xz seaweedfs-685643d506b7593cfe2b66c066f7f9a68655a2ff.zip | |
needle read into a writer
| -rw-r--r-- | weed/storage/needle/needle_read_page.go | 26 | ||||
| -rw-r--r-- | weed/storage/needle/needle_read_test.go | 20 |
2 files changed, 28 insertions, 18 deletions
diff --git a/weed/storage/needle/needle_read_page.go b/weed/storage/needle/needle_read_page.go index 4600f4314..300b415c9 100644 --- a/weed/storage/needle/needle_read_page.go +++ b/weed/storage/needle/needle_read_page.go @@ -9,6 +9,32 @@ import ( "io" ) +// ReadNeedleDataInto uses a needle without n.Data to read the content into an io.Writer +func (n *Needle) ReadNeedleDataInto(r backend.BackendStorageFile, offset int64, buf []byte, writer io.Writer, expectedChecksumValue uint32) (err error) { + crc := CRC(0) + for x := 0; ; x += len(buf) { + count, err := n.ReadNeedleData(r, offset, buf, int64(x)) + if err != nil { + if err == io.EOF { + break + } + return fmt.Errorf("ReadNeedleData: %v", err) + } + if count > 0 { + crc = crc.Update(buf[0:count]) + if _, err = writer.Write(buf[0:count]); err != nil { + return fmt.Errorf("ReadNeedleData write: %v", err) + } + } else { + break + } + } + if expectedChecksumValue != crc.Value() { + return fmt.Errorf("ReadNeedleData checksum %v expected %v", crc.Value(), expectedChecksumValue) + } + return nil +} + // ReadNeedleData uses a needle without n.Data to read the content // volumeOffset: the offset within the volume // needleOffset: the offset within the needle Data diff --git a/weed/storage/needle/needle_read_test.go b/weed/storage/needle/needle_read_test.go index b519b0241..688df0d53 100644 --- a/weed/storage/needle/needle_read_test.go +++ b/weed/storage/needle/needle_read_test.go @@ -3,7 +3,6 @@ package needle import ( "fmt" "github.com/chrislusf/seaweedfs/weed/storage/backend" - "github.com/stretchr/testify/assert" "io" "os" "testing" @@ -75,24 +74,9 @@ func TestPageRead(t *testing.T) { fmt.Printf("Checksum value %d\n", checksumValue) buf := make([]byte, 1024) - crc := CRC(0) - for x := int64(0); ; x += 1024 { - count, err := n.ReadNeedleData(datBackend, offset, buf, x) - if err != nil { - if err == io.EOF { - break - } - t.Fatalf("ReadNeedleData: %v", err) - } - if count > 0 { - crc = crc.Update(buf[0:count]) - } else { - break - } + if err = n.ReadNeedleDataInto(datBackend, offset, buf, io.Discard, checksumValue); err != nil { + t.Fatalf("ReadNeedleDataInto: %v", err) } - fmt.Printf("read checksum value %d\n", crc.Value()) - - assert.Equal(t, checksumValue, crc.Value(), "validate checksum value") } |
