WriteToBuffer -> WriteTo

This commit is contained in:
Jae Kwon 2014-06-03 17:03:29 -07:00
parent 2f1db219fd
commit 15f8441068
5 changed files with 85 additions and 81 deletions

View File

@ -1,6 +1,9 @@
package merkle package merkle
import ( import (
//"fmt"
"bytes"
"io"
"crypto/sha256" "crypto/sha256"
) )
@ -191,11 +194,9 @@ func (self *IAVLNode) Hash() (ByteSlice, uint64) {
return self.hash, 0 return self.hash, 0
} }
size := self.ByteSize()
buf := make([]byte, size, size)
hasher := sha256.New() hasher := sha256.New()
_, hashCount := self.saveToCountHashes(buf) _, hashCount, err := self.saveToCountHashes(hasher)
hasher.Write(buf) if err != nil { panic(err) }
self.hash = hasher.Sum(nil) self.hash = hasher.Sum(nil)
return self.hash, hashCount+1 return self.hash, hashCount+1
@ -217,9 +218,11 @@ func (self *IAVLNode) Save(db Db) {
} }
// save self // save self
buf := make([]byte, self.ByteSize(), self.ByteSize()) buf := make([]byte, 0, self.ByteSize())
self.WriteTo(buf) n, err := self.WriteTo(bytes.NewBuffer(buf))
db.Put([]byte(self.hash), buf) if err != nil { panic(err) }
if n != int64(cap(buf)) { panic("unexpected write length") }
db.Put([]byte(self.hash), buf[0:cap(buf)])
self.flags |= IAVLNODE_FLAG_PERSISTED self.flags |= IAVLNODE_FLAG_PERSISTED
} }
@ -321,43 +324,48 @@ func (self *IAVLNode) ByteSize() int {
return size return size
} }
func (self *IAVLNode) WriteTo(buf []byte) int { func (self *IAVLNode) WriteTo(w io.Writer) (n int64, err error) {
written, _ := self.saveToCountHashes(buf) n, _, err = self.saveToCountHashes(w)
return written return
} }
func (self *IAVLNode) saveToCountHashes(buf []byte) (int, uint64) { func (self *IAVLNode) saveToCountHashes(w io.Writer) (n int64, hashCount uint64, err error) {
cur := 0 var _n int64
hashCount := uint64(0)
// height & size // height & size
cur += UInt8(self.height).WriteTo(buf[cur:]) _n, err = UInt8(self.height).WriteTo(w)
cur += UInt64(self.size).WriteTo(buf[cur:]) if err != nil { return } else { n += _n }
_n, err = UInt64(self.size).WriteTo(w)
if err != nil { return } else { n += _n }
// key // key
buf[cur] = GetBinaryType(self.key) _n, err = Byte(GetBinaryType(self.key)).WriteTo(w)
cur += 1 if err != nil { return } else { n += _n }
cur += self.key.WriteTo(buf[cur:]) _n, err = self.key.WriteTo(w)
if err != nil { return } else { n += _n }
if self.height == 0 { if self.height == 0 {
// value // value
buf[cur] = GetBinaryType(self.value) _n, err = Byte(GetBinaryType(self.value)).WriteTo(w)
cur += 1 if err != nil { return } else { n += _n }
if self.value != nil { if self.value != nil {
cur += self.value.WriteTo(buf[cur:]) _n, err = self.value.WriteTo(w)
if err != nil { return } else { n += _n }
} }
} else { } else {
// left // left
leftHash, leftCount := self.left.Hash() leftHash, leftCount := self.left.Hash()
hashCount += leftCount hashCount += leftCount
cur += leftHash.WriteTo(buf[cur:]) _n, err = leftHash.WriteTo(w)
if err != nil { return } else { n += _n }
// right // right
rightHash, rightCount := self.right.Hash() rightHash, rightCount := self.right.Hash()
hashCount += rightCount hashCount += rightCount
cur += rightHash.WriteTo(buf[cur:]) _n, err = rightHash.WriteTo(w)
if err != nil { return } else { n += _n }
} }
return cur, hashCount return
} }
// Given a placeholder node which has only the hash set, // Given a placeholder node which has only the hash set,

View File

@ -1,8 +1,7 @@
package merkle package merkle
import "testing"
import ( import (
"testing"
"fmt" "fmt"
"os" "os"
"bytes" "bytes"
@ -10,6 +9,7 @@ import (
"encoding/binary" "encoding/binary"
"github.com/tendermint/tendermint/db" "github.com/tendermint/tendermint/db"
"crypto/sha256" "crypto/sha256"
"runtime"
) )
func init() { func init() {
@ -271,6 +271,8 @@ func BenchmarkImmutableAvlTree(b *testing.B) {
fmt.Println("ok, starting") fmt.Println("ok, starting")
runtime.GC()
b.StartTimer() b.StartTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
r := randomRecord() r := randomRecord()

View File

@ -1,6 +1,7 @@
package merkle package merkle
import ( import (
"io"
"encoding/binary" "encoding/binary"
) )
@ -35,10 +36,9 @@ func (self Byte) ByteSize() int {
return 1 return 1
} }
func (self Byte) WriteTo(b []byte) int { func (self Byte) WriteTo(w io.Writer) (int64, error) {
if cap(b) < 1 { panic("buf too small") } n, err := w.Write([]byte{byte(self)})
b[0] = byte(self) return int64(n), err
return 1
} }
func ReadByte(bytes []byte) Byte { func ReadByte(bytes []byte) Byte {
@ -64,10 +64,9 @@ func (self Int8) ByteSize() int {
return 1 return 1
} }
func (self Int8) WriteTo(b []byte) int { func (self Int8) WriteTo(w io.Writer) (int64, error) {
if cap(b) < 1 { panic("buf too small") } n, err := w.Write([]byte{byte(self)})
b[0] = byte(self) return int64(n), err
return 1
} }
func ReadInt8(bytes []byte) Int8 { func ReadInt8(bytes []byte) Int8 {
@ -93,10 +92,9 @@ func (self UInt8) ByteSize() int {
return 1 return 1
} }
func (self UInt8) WriteTo(b []byte) int { func (self UInt8) WriteTo(w io.Writer) (int64, error) {
if cap(b) < 1 { panic("buf too small") } n, err := w.Write([]byte{byte(self)})
b[0] = byte(self) return int64(n), err
return 1
} }
func ReadUInt8(bytes []byte) UInt8 { func ReadUInt8(bytes []byte) UInt8 {
@ -122,10 +120,9 @@ func (self Int16) ByteSize() int {
return 2 return 2
} }
func (self Int16) WriteTo(b []byte) int { func (self Int16) WriteTo(w io.Writer) (int64, error) {
if cap(b) < 2 { panic("buf too small") } err := binary.Write(w, binary.LittleEndian, int16(self))
binary.LittleEndian.PutUint16(b, uint16(self)) return 2, err
return 2
} }
func ReadInt16(bytes []byte) Int16 { func ReadInt16(bytes []byte) Int16 {
@ -151,10 +148,9 @@ func (self UInt16) ByteSize() int {
return 2 return 2
} }
func (self UInt16) WriteTo(b []byte) int { func (self UInt16) WriteTo(w io.Writer) (int64, error) {
if cap(b) < 2 { panic("buf too small") } err := binary.Write(w, binary.LittleEndian, uint16(self))
binary.LittleEndian.PutUint16(b, uint16(self)) return 2, err
return 2
} }
func ReadUInt16(bytes []byte) UInt16 { func ReadUInt16(bytes []byte) UInt16 {
@ -180,10 +176,9 @@ func (self Int32) ByteSize() int {
return 4 return 4
} }
func (self Int32) WriteTo(b []byte) int { func (self Int32) WriteTo(w io.Writer) (int64, error) {
if cap(b) < 4 { panic("buf too small") } err := binary.Write(w, binary.LittleEndian, int32(self))
binary.LittleEndian.PutUint32(b, uint32(self)) return 4, err
return 4
} }
func ReadInt32(bytes []byte) Int32 { func ReadInt32(bytes []byte) Int32 {
@ -209,10 +204,9 @@ func (self UInt32) ByteSize() int {
return 4 return 4
} }
func (self UInt32) WriteTo(b []byte) int { func (self UInt32) WriteTo(w io.Writer) (int64, error) {
if cap(b) < 4 { panic("buf too small") } err := binary.Write(w, binary.LittleEndian, uint32(self))
binary.LittleEndian.PutUint32(b, uint32(self)) return 4, err
return 4
} }
func ReadUInt32(bytes []byte) UInt32 { func ReadUInt32(bytes []byte) UInt32 {
@ -238,10 +232,9 @@ func (self Int64) ByteSize() int {
return 8 return 8
} }
func (self Int64) WriteTo(b []byte) int { func (self Int64) WriteTo(w io.Writer) (int64, error) {
if cap(b) < 8 { panic("buf too small") } err := binary.Write(w, binary.LittleEndian, int64(self))
binary.LittleEndian.PutUint64(b, uint64(self)) return 8, err
return 8
} }
func ReadInt64(bytes []byte) Int64 { func ReadInt64(bytes []byte) Int64 {
@ -267,10 +260,9 @@ func (self UInt64) ByteSize() int {
return 8 return 8
} }
func (self UInt64) WriteTo(b []byte) int { func (self UInt64) WriteTo(w io.Writer) (int64, error) {
if cap(b) < 8 { panic("buf too small") } err := binary.Write(w, binary.LittleEndian, uint64(self))
binary.LittleEndian.PutUint64(b, uint64(self)) return 8, err
return 8
} }
func ReadUInt64(bytes []byte) UInt64 { func ReadUInt64(bytes []byte) UInt64 {
@ -296,10 +288,9 @@ func (self Int) ByteSize() int {
return 8 return 8
} }
func (self Int) WriteTo(b []byte) int { func (self Int) WriteTo(w io.Writer) (int64, error) {
if cap(b) < 8 { panic("buf too small") } err := binary.Write(w, binary.LittleEndian, int64(self))
binary.LittleEndian.PutUint64(b, uint64(self)) return 8, err
return 8
} }
func ReadInt(bytes []byte) Int { func ReadInt(bytes []byte) Int {
@ -324,10 +315,9 @@ func (self UInt) ByteSize() int {
return 8 return 8
} }
func (self UInt) WriteTo(b []byte) int { func (self UInt) WriteTo(w io.Writer) (int64, error) {
if cap(b) < 8 { panic("buf too small") } err := binary.Write(w, binary.LittleEndian, uint64(self))
binary.LittleEndian.PutUint64(b, uint64(self)) return 8, err
return 8
} }
func ReadUInt(bytes []byte) UInt { func ReadUInt(bytes []byte) UInt {

View File

@ -1,5 +1,6 @@
package merkle package merkle
import "io"
import "bytes" import "bytes"
type String string type String string
@ -23,11 +24,12 @@ func (self String) ByteSize() int {
return len(self)+4 return len(self)+4
} }
func (self String) WriteTo(buf []byte) int { func (self String) WriteTo(w io.Writer) (n int64, err error) {
if len(buf) < self.ByteSize() { panic("buf too small") } var n_ int
UInt32(len(self)).WriteTo(buf) _, err = UInt32(len(self)).WriteTo(w)
copy(buf[4:], []byte(self)) if err != nil { return n, err }
return len(self)+4 n_, err = w.Write([]byte(self))
return int64(n_+4), err
} }
// NOTE: keeps a reference to the original byte slice // NOTE: keeps a reference to the original byte slice
@ -59,11 +61,12 @@ func (self ByteSlice) ByteSize() int {
return len(self)+4 return len(self)+4
} }
func (self ByteSlice) WriteTo(buf []byte) int { func (self ByteSlice) WriteTo(w io.Writer) (n int64, err error) {
if len(buf) < self.ByteSize() { panic("buf too small") } var n_ int
UInt32(len(self)).WriteTo(buf) _, err = UInt32(len(self)).WriteTo(w)
copy(buf[4:], self) if err != nil { return n, err }
return len(self)+4 n_, err = w.Write([]byte(self))
return int64(n_+4), err
} }
// NOTE: keeps a reference to the original byte slice // NOTE: keeps a reference to the original byte slice

View File

@ -1,12 +1,13 @@
package merkle package merkle
import ( import (
"io"
"fmt" "fmt"
) )
type Binary interface { type Binary interface {
ByteSize() int ByteSize() int
WriteTo([]byte) int WriteTo(io.Writer) (int64, error)
Equals(Binary) bool Equals(Binary) bool
} }