Browse Source

SaveTo -> WriteTo, Load -> Read

pull/9/head
Jae Kwon 11 years ago
parent
commit
2f1db219fd
5 changed files with 60 additions and 58 deletions
  1. +13
    -13
      merkle/binary.go
  2. +14
    -14
      merkle/iavl.go
  3. +22
    -22
      merkle/int.go
  4. +10
    -8
      merkle/string.go
  5. +1
    -1
      merkle/types.go

+ 13
- 13
merkle/binary.go View File

@ -38,22 +38,22 @@ func GetBinaryType(o Binary) byte {
}
}
func LoadBinary(buf []byte, start int) (Binary, int) {
func ReadBinary(buf []byte, start int) (Binary, int) {
typeByte := buf[start]
switch typeByte {
case TYPE_NIL: return nil, start+1
case TYPE_BYTE: return LoadByte(buf[start+1:]), start+2
case TYPE_INT8: return LoadInt8(buf[start+1:]), start+2
case TYPE_UINT8: return LoadUInt8(buf[start+1:]), start+2
case TYPE_INT16: return LoadInt16(buf[start+1:]), start+3
case TYPE_UINT16: return LoadUInt16(buf[start+1:]), start+3
case TYPE_INT32: return LoadInt32(buf[start+1:]), start+5
case TYPE_UINT32: return LoadUInt32(buf[start+1:]), start+5
case TYPE_INT64: return LoadInt64(buf[start+1:]), start+9
case TYPE_UINT64: return LoadUInt64(buf[start+1:]), start+9
case TYPE_STRING: return LoadString(buf, start+1)
case TYPE_BYTESLICE:return LoadByteSlice(buf, start+1)
case TYPE_BYTE: return ReadByte(buf[start+1:]), start+2
case TYPE_INT8: return ReadInt8(buf[start+1:]), start+2
case TYPE_UINT8: return ReadUInt8(buf[start+1:]), start+2
case TYPE_INT16: return ReadInt16(buf[start+1:]), start+3
case TYPE_UINT16: return ReadUInt16(buf[start+1:]), start+3
case TYPE_INT32: return ReadInt32(buf[start+1:]), start+5
case TYPE_UINT32: return ReadUInt32(buf[start+1:]), start+5
case TYPE_INT64: return ReadInt64(buf[start+1:]), start+9
case TYPE_UINT64: return ReadUInt64(buf[start+1:]), start+9
case TYPE_STRING: return ReadString(buf, start+1)
case TYPE_BYTESLICE:return ReadByteSlice(buf, start+1)
default: panic("Unsupported type")
}


+ 14
- 14
merkle/iavl.go View File

@ -218,7 +218,7 @@ func (self *IAVLNode) Save(db Db) {
// save self
buf := make([]byte, self.ByteSize(), self.ByteSize())
self.SaveTo(buf)
self.WriteTo(buf)
db.Put([]byte(self.hash), buf)
self.flags |= IAVLNODE_FLAG_PERSISTED
@ -321,7 +321,7 @@ func (self *IAVLNode) ByteSize() int {
return size
}
func (self *IAVLNode) SaveTo(buf []byte) int {
func (self *IAVLNode) WriteTo(buf []byte) int {
written, _ := self.saveToCountHashes(buf)
return written
}
@ -331,30 +331,30 @@ func (self *IAVLNode) saveToCountHashes(buf []byte) (int, uint64) {
hashCount := uint64(0)
// height & size
cur += UInt8(self.height).SaveTo(buf[cur:])
cur += UInt64(self.size).SaveTo(buf[cur:])
cur += UInt8(self.height).WriteTo(buf[cur:])
cur += UInt64(self.size).WriteTo(buf[cur:])
// key
buf[cur] = GetBinaryType(self.key)
cur += 1
cur += self.key.SaveTo(buf[cur:])
cur += self.key.WriteTo(buf[cur:])
if self.height == 0 {
// value
buf[cur] = GetBinaryType(self.value)
cur += 1
if self.value != nil {
cur += self.value.SaveTo(buf[cur:])
cur += self.value.WriteTo(buf[cur:])
}
} else {
// left
leftHash, leftCount := self.left.Hash()
hashCount += leftCount
cur += leftHash.SaveTo(buf[cur:])
cur += leftHash.WriteTo(buf[cur:])
// right
rightHash, rightCount := self.right.Hash()
hashCount += rightCount
cur += rightHash.SaveTo(buf[cur:])
cur += rightHash.WriteTo(buf[cur:])
}
return cur, hashCount
@ -370,26 +370,26 @@ func (self *IAVLNode) fill(db Db) {
buf := db.Get(self.hash)
cur := 0
// node header
self.height = uint8(LoadUInt8(buf[0:]))
self.size = uint64(LoadUInt64(buf[1:]))
self.height = uint8(ReadUInt8(buf[0:]))
self.size = uint64(ReadUInt64(buf[1:]))
// key
key, cur := LoadBinary(buf, 9)
key, cur := ReadBinary(buf, 9)
self.key = key.(Key)
if self.height == 0 {
// value
self.value, cur = LoadBinary(buf, cur)
self.value, cur = ReadBinary(buf, cur)
} else {
// left
var leftHash ByteSlice
leftHash, cur = LoadByteSlice(buf, cur)
leftHash, cur = ReadByteSlice(buf, cur)
self.left = &IAVLNode{
hash: leftHash,
flags: IAVLNODE_FLAG_PERSISTED | IAVLNODE_FLAG_PLACEHOLDER,
}
// right
var rightHash ByteSlice
rightHash, cur = LoadByteSlice(buf, cur)
rightHash, cur = ReadByteSlice(buf, cur)
self.right = &IAVLNode{
hash: rightHash,
flags: IAVLNODE_FLAG_PERSISTED | IAVLNODE_FLAG_PLACEHOLDER,


+ 22
- 22
merkle/int.go View File

@ -35,13 +35,13 @@ func (self Byte) ByteSize() int {
return 1
}
func (self Byte) SaveTo(b []byte) int {
func (self Byte) WriteTo(b []byte) int {
if cap(b) < 1 { panic("buf too small") }
b[0] = byte(self)
return 1
}
func LoadByte(bytes []byte) Byte {
func ReadByte(bytes []byte) Byte {
return Byte(bytes[0])
}
@ -64,13 +64,13 @@ func (self Int8) ByteSize() int {
return 1
}
func (self Int8) SaveTo(b []byte) int {
func (self Int8) WriteTo(b []byte) int {
if cap(b) < 1 { panic("buf too small") }
b[0] = byte(self)
return 1
}
func LoadInt8(bytes []byte) Int8 {
func ReadInt8(bytes []byte) Int8 {
return Int8(bytes[0])
}
@ -93,13 +93,13 @@ func (self UInt8) ByteSize() int {
return 1
}
func (self UInt8) SaveTo(b []byte) int {
func (self UInt8) WriteTo(b []byte) int {
if cap(b) < 1 { panic("buf too small") }
b[0] = byte(self)
return 1
}
func LoadUInt8(bytes []byte) UInt8 {
func ReadUInt8(bytes []byte) UInt8 {
return UInt8(bytes[0])
}
@ -122,13 +122,13 @@ func (self Int16) ByteSize() int {
return 2
}
func (self Int16) SaveTo(b []byte) int {
func (self Int16) WriteTo(b []byte) int {
if cap(b) < 2 { panic("buf too small") }
binary.LittleEndian.PutUint16(b, uint16(self))
return 2
}
func LoadInt16(bytes []byte) Int16 {
func ReadInt16(bytes []byte) Int16 {
return Int16(binary.LittleEndian.Uint16(bytes))
}
@ -151,13 +151,13 @@ func (self UInt16) ByteSize() int {
return 2
}
func (self UInt16) SaveTo(b []byte) int {
func (self UInt16) WriteTo(b []byte) int {
if cap(b) < 2 { panic("buf too small") }
binary.LittleEndian.PutUint16(b, uint16(self))
return 2
}
func LoadUInt16(bytes []byte) UInt16 {
func ReadUInt16(bytes []byte) UInt16 {
return UInt16(binary.LittleEndian.Uint16(bytes))
}
@ -180,13 +180,13 @@ func (self Int32) ByteSize() int {
return 4
}
func (self Int32) SaveTo(b []byte) int {
func (self Int32) WriteTo(b []byte) int {
if cap(b) < 4 { panic("buf too small") }
binary.LittleEndian.PutUint32(b, uint32(self))
return 4
}
func LoadInt32(bytes []byte) Int32 {
func ReadInt32(bytes []byte) Int32 {
return Int32(binary.LittleEndian.Uint32(bytes))
}
@ -209,13 +209,13 @@ func (self UInt32) ByteSize() int {
return 4
}
func (self UInt32) SaveTo(b []byte) int {
func (self UInt32) WriteTo(b []byte) int {
if cap(b) < 4 { panic("buf too small") }
binary.LittleEndian.PutUint32(b, uint32(self))
return 4
}
func LoadUInt32(bytes []byte) UInt32 {
func ReadUInt32(bytes []byte) UInt32 {
return UInt32(binary.LittleEndian.Uint32(bytes))
}
@ -238,13 +238,13 @@ func (self Int64) ByteSize() int {
return 8
}
func (self Int64) SaveTo(b []byte) int {
func (self Int64) WriteTo(b []byte) int {
if cap(b) < 8 { panic("buf too small") }
binary.LittleEndian.PutUint64(b, uint64(self))
return 8
}
func LoadInt64(bytes []byte) Int64 {
func ReadInt64(bytes []byte) Int64 {
return Int64(binary.LittleEndian.Uint64(bytes))
}
@ -267,13 +267,13 @@ func (self UInt64) ByteSize() int {
return 8
}
func (self UInt64) SaveTo(b []byte) int {
func (self UInt64) WriteTo(b []byte) int {
if cap(b) < 8 { panic("buf too small") }
binary.LittleEndian.PutUint64(b, uint64(self))
return 8
}
func LoadUInt64(bytes []byte) UInt64 {
func ReadUInt64(bytes []byte) UInt64 {
return UInt64(binary.LittleEndian.Uint64(bytes))
}
@ -296,13 +296,13 @@ func (self Int) ByteSize() int {
return 8
}
func (self Int) SaveTo(b []byte) int {
func (self Int) WriteTo(b []byte) int {
if cap(b) < 8 { panic("buf too small") }
binary.LittleEndian.PutUint64(b, uint64(self))
return 8
}
func LoadInt(bytes []byte) Int {
func ReadInt(bytes []byte) Int {
return Int(binary.LittleEndian.Uint64(bytes))
}
@ -324,12 +324,12 @@ func (self UInt) ByteSize() int {
return 8
}
func (self UInt) SaveTo(b []byte) int {
func (self UInt) WriteTo(b []byte) int {
if cap(b) < 8 { panic("buf too small") }
binary.LittleEndian.PutUint64(b, uint64(self))
return 8
}
func LoadUInt(bytes []byte) UInt {
func ReadUInt(bytes []byte) UInt {
return UInt(binary.LittleEndian.Uint64(bytes))
}

+ 10
- 8
merkle/string.go View File

@ -23,15 +23,16 @@ func (self String) ByteSize() int {
return len(self)+4
}
func (self String) SaveTo(buf []byte) int {
func (self String) WriteTo(buf []byte) int {
if len(buf) < self.ByteSize() { panic("buf too small") }
UInt32(len(self)).SaveTo(buf)
UInt32(len(self)).WriteTo(buf)
copy(buf[4:], []byte(self))
return len(self)+4
}
func LoadString(bytes []byte, start int) (String, int) {
length := int(LoadUInt32(bytes[start:]))
// NOTE: keeps a reference to the original byte slice
func ReadString(bytes []byte, start int) (String, int) {
length := int(ReadUInt32(bytes[start:]))
return String(bytes[start+4:start+4+length]), start+4+length
}
@ -58,14 +59,15 @@ func (self ByteSlice) ByteSize() int {
return len(self)+4
}
func (self ByteSlice) SaveTo(buf []byte) int {
func (self ByteSlice) WriteTo(buf []byte) int {
if len(buf) < self.ByteSize() { panic("buf too small") }
UInt32(len(self)).SaveTo(buf)
UInt32(len(self)).WriteTo(buf)
copy(buf[4:], self)
return len(self)+4
}
func LoadByteSlice(bytes []byte, start int) (ByteSlice, int) {
length := int(LoadUInt32(bytes[start:]))
// NOTE: keeps a reference to the original byte slice
func ReadByteSlice(bytes []byte, start int) (ByteSlice, int) {
length := int(ReadUInt32(bytes[start:]))
return ByteSlice(bytes[start+4:start+4+length]), start+4+length
}

+ 1
- 1
merkle/types.go View File

@ -6,7 +6,7 @@ import (
type Binary interface {
ByteSize() int
SaveTo([]byte) int
WriteTo([]byte) int
Equals(Binary) bool
}


Loading…
Cancel
Save