Skip to content

Commit

Permalink
use io.Writer interface for Dump methods
Browse files Browse the repository at this point in the history
Signed-off-by: p4u <[email protected]>
  • Loading branch information
p4u committed Aug 25, 2022
1 parent 40a0fb9 commit c965055
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 13 deletions.
11 changes: 3 additions & 8 deletions tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -1327,7 +1327,7 @@ func (t *Tree) Dump(fromRoot []byte) ([]byte, error) {
}

// DumpWriter exports all the Tree leafs writing the bytes in the given Writer
func (t *Tree) DumpWriter(fromRoot []byte, w *bufio.Writer) error {
func (t *Tree) DumpWriter(fromRoot []byte, w io.Writer) error {
_, err := t.dump(fromRoot, w)
return err
}
Expand All @@ -1340,7 +1340,7 @@ func (t *Tree) DumpWriter(fromRoot []byte, w *bufio.Writer) error {
// [ 1 byte | 1 byte | S bytes | len(v) bytes ]
// [ len(k) | len(v) | key | value ]
// Where S is the size of the output of the hash function used for the Tree.
func (t *Tree) dump(fromRoot []byte, w *bufio.Writer) ([]byte, error) {
func (t *Tree) dump(fromRoot []byte, w io.Writer) ([]byte, error) {
// allow to define which root to use
if fromRoot == nil {
var err error
Expand Down Expand Up @@ -1385,11 +1385,6 @@ func (t *Tree) dump(fromRoot []byte, w *bufio.Writer) ([]byte, error) {
callbackErr = fmt.Errorf("dump: w.Write n!=len(kv), %s", err)
return true
}
err = w.Flush()
if err != nil {
callbackErr = fmt.Errorf("dump: w.Flush, %s", err)
return true
}
}
return false
})
Expand All @@ -1409,7 +1404,7 @@ func (t *Tree) ImportDump(b []byte) error {

// ImportDumpReader imports the leafs (that have been exported with the Dump
// method) in the Tree, reading them from the given reader.
func (t *Tree) ImportDumpReader(r *bufio.Reader) error {
func (t *Tree) ImportDumpReader(r io.Reader) error {
if !t.editable() {
return ErrSnapshotNotEditable
}
Expand Down
7 changes: 2 additions & 5 deletions tree_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package arbo

import (
"bufio"
"encoding/hex"
"math"
"math/big"
Expand Down Expand Up @@ -492,8 +491,7 @@ func testDumpAndImportDump(t *testing.T, inFile bool) {
if inFile {
f, err := os.Create(fileName)
c.Assert(err, qt.IsNil)
w := bufio.NewWriter(f)
err = tree1.DumpWriter(nil, w)
err = tree1.DumpWriter(nil, f)
c.Assert(err, qt.IsNil)
} else {
e, err = tree1.Dump(nil)
Expand All @@ -510,8 +508,7 @@ func testDumpAndImportDump(t *testing.T, inFile bool) {
if inFile {
f, err := os.Open(filepath.Clean(fileName))
c.Assert(err, qt.IsNil)
r := bufio.NewReader(f)
err = tree2.ImportDumpReader(r)
err = tree2.ImportDumpReader(f)
c.Assert(err, qt.IsNil)
} else {
err = tree2.ImportDump(e)
Expand Down

0 comments on commit c965055

Please sign in to comment.