Skip to content

Commit

Permalink
Minor update and new comments
Browse files Browse the repository at this point in the history
  • Loading branch information
tidwall committed Aug 1, 2021
1 parent 04bc915 commit f701479
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
16 changes: 11 additions & 5 deletions buntdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -1263,12 +1263,15 @@ type dbItem struct {
keyless bool // keyless item for scanning
}

// estIntSize returns the string representions size.
// Has the same result as len(strconv.Itoa(x)).
func estIntSize(x int) int {
if x == 0 {
return 1
n := 1
if x < 0 {
n++
x *= -1
}
var n int
for x > 0 {
for x >= 10 {
n++
x /= 10
}
Expand All @@ -1283,7 +1286,10 @@ func estBulkStringSize(s string) int {
return 1 + estIntSize(len(s)) + 2 + len(s) + 2
}

func (dbi *dbItem) estAOFSetSize() (n int) {
// estAOFSetSize returns an estimated number of bytes that this item will use
// when stored in the aof file.
func (dbi *dbItem) estAOFSetSize() int {
var n int
if dbi.opts != nil && dbi.opts.ex {
n += estArraySize(5)
n += estBulkStringSize("set")
Expand Down
23 changes: 23 additions & 0 deletions buntdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"testing"
"time"

"github.com/tidwall/assert"
"github.com/tidwall/lotsa"
)

Expand Down Expand Up @@ -2886,3 +2887,25 @@ func TestReloadNotInvalid(t *testing.T) {
ii++
}
}

func TestEstSize(t *testing.T) {
t.Run("estIntSize", func(t *testing.T) {
assert.Assert(estIntSize(0) == 1)
assert.Assert(estIntSize(1) == 1)
assert.Assert(estIntSize(9) == 1)
assert.Assert(estIntSize(10) == 2)
assert.Assert(estIntSize(11) == 2)
assert.Assert(estIntSize(19) == 2)
assert.Assert(estIntSize(20) == 2)
assert.Assert(estIntSize(113) == 3)
assert.Assert(estIntSize(3822) == 4)
assert.Assert(estIntSize(-1) == 2)
assert.Assert(estIntSize(-12) == 3)
assert.Assert(estIntSize(-124) == 4)
})
// t.Run("estI
// // dbi := dbItem{
// // key: "hello",
// // value: "
// // }
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/tidwall/buntdb
go 1.16

require (
github.com/tidwall/assert v0.1.0
github.com/tidwall/btree v0.6.0
github.com/tidwall/gjson v1.8.0
github.com/tidwall/grect v0.1.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/tidwall/assert v0.1.0 h1:aWcKyRBUAdLoVebxo95N7+YZVTFF/ASTr7BN4sLP6XI=
github.com/tidwall/assert v0.1.0/go.mod h1:QLYtGyeqse53vuELQheYl9dngGCJQ+mTtlxcktb+Kj8=
github.com/tidwall/btree v0.6.0 h1:JLYAFGV+1gjyFi3iQbO/fupBin+Ooh7dxqVV0twJ1Bo=
github.com/tidwall/btree v0.6.0/go.mod h1:TzIRzen6yHbibdSfK6t8QimqbUnoxUSrZfeW7Uob0q4=
github.com/tidwall/gjson v1.8.0 h1:Qt+orfosKn0rbNTZqHYDqBrmm3UDA4KRkv70fDzG+PQ=
Expand Down

0 comments on commit f701479

Please sign in to comment.