Skip to content

Commit

Permalink
db/leveldb: only sync type for obj
Browse files Browse the repository at this point in the history
  • Loading branch information
wdvxdr1123 committed Feb 14, 2022
1 parent b7abd83 commit dad4cb2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 33 deletions.
27 changes: 3 additions & 24 deletions db/leveldb/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,41 +52,30 @@ func (r *reader) sync(c coder) {
}

func (r *reader) int() int {
r.sync(coderInt)
return int(r.varint())
}

func (r *reader) uint() uint {
r.sync(coderUint)
return uint(r.uvarint())
}

func (r *reader) int32() int32 {
r.sync(coderInt32)
return int32(r.varint())
}

func (r *reader) uint32() uint32 {
r.sync(coderUint32)
return uint32(r.uvarint())
}

func (r *reader) int64() int64 {
r.sync(coderInt64)
return r.varint()
}

func (r *reader) uint64() uint64 {
r.sync(coderUint64)
return r.uvarint()
}

func (r *reader) string() string {
r.sync(coderString)
return r.stringNoSync()
}

func (r *reader) stringNoSync() string {
off := r.data.uvarint()
if s, ok := r.stringIndex[off]; ok {
return s
Expand All @@ -100,11 +89,6 @@ func (r *reader) stringNoSync() string {
}

func (r *reader) msg() global.MSG {
r.sync(coderMSG)
return r.msgNoSync()
}

func (r *reader) msgNoSync() global.MSG {
length := r.uvarint()
msg := make(global.MSG, length)
for i := uint64(0); i < length; i++ {
Expand All @@ -115,11 +99,6 @@ func (r *reader) msgNoSync() global.MSG {
}

func (r *reader) arrayMsg() []global.MSG {
r.sync(coderArrayMSG)
return r.arrayMsgNoSync()
}

func (r *reader) arrayMsgNoSync() []global.MSG {
length := r.uvarint()
msgs := make([]global.MSG, length)
for i := range msgs {
Expand All @@ -145,11 +124,11 @@ func (r *reader) obj() interface{} {
case coderUint64:
return r.uvarint()
case coderString:
return r.stringNoSync()
return r.string()
case coderMSG:
return r.msgNoSync()
return r.msg()
case coderArrayMSG:
return r.arrayMsgNoSync()
return r.arrayMsg()
default:
panic("db/leveldb: invalid coder " + strconv.Itoa(int(coder)))
}
Expand Down
20 changes: 11 additions & 9 deletions db/leveldb/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,37 +50,30 @@ func (w *writer) uvarint(x uint64) { w.data.uvarint(x) }
func (w *writer) nil() { w.coder(coderNil) }

func (w *writer) int(i int) {
w.coder(coderInt)
w.varint(int64(i))
}

func (w *writer) uint(i uint) {
w.coder(coderUint)
w.uvarint(uint64(i))
}

func (w *writer) int32(i int32) {
w.coder(coderInt32)
w.varint(int64(i))
}

func (w *writer) uint32(i uint32) {
w.coder(coderUint32)
w.uvarint(uint64(i))
}

func (w *writer) int64(i int64) {
w.coder(coderInt64)
w.varint(i)
}

func (w *writer) uint64(i uint64) {
w.coder(coderUint64)
w.uvarint(i)
}

func (w *writer) string(s string) {
w.coder(coderString)
off, ok := w.stringIndex[s]
if !ok {
// not found write to string data part
Expand All @@ -95,7 +88,6 @@ func (w *writer) string(s string) {
}

func (w *writer) msg(m global.MSG) {
w.coder(coderMSG)
w.uvarint(uint64(len(m)))
for s, obj := range m {
w.string(s)
Expand All @@ -104,7 +96,6 @@ func (w *writer) msg(m global.MSG) {
}

func (w *writer) arrayMsg(a []global.MSG) {
w.coder(coderArrayMSG)
w.uvarint(uint64(len(a)))
for _, v := range a {
w.msg(v)
Expand All @@ -116,21 +107,32 @@ func (w *writer) obj(o interface{}) {
case nil:
w.nil()
case int:
w.coder(coderInt)
w.int(x)
case int32:
w.coder(coderInt32)
w.int32(x)
case int64:
w.coder(coderInt64)
w.int64(x)
case uint:
w.coder(coderUint)
w.uint(x)
case uint32:
w.coder(coderUint32)
w.uint32(x)
case uint64:
w.coder(coderUint64)
w.uint64(x)
case string:
w.coder(coderString)
w.string(x)
case global.MSG:
w.coder(coderMSG)
w.msg(x)
case []global.MSG:
w.coder(coderArrayMSG)
w.arrayMsg(x)
default:
panic("unsupported type")
}
Expand Down

0 comments on commit dad4cb2

Please sign in to comment.