aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--db_test.go4
-rw-r--r--freelist.go4
-rw-r--r--freelist_test.go16
-rw-r--r--page.go2
-rw-r--r--tx_test.go2
5 files changed, 16 insertions, 12 deletions
diff --git a/db_test.go b/db_test.go
index 903f65e..f34b731 100644
--- a/db_test.go
+++ b/db_test.go
@@ -319,10 +319,10 @@ func TestDB_Consistency(t *testing.T) {
assert.Equal(t, "free", p.Type)
}
if p, _ := tx.Page(4); assert.NotNil(t, p) {
- assert.Equal(t, "freelist", p.Type)
+ assert.Equal(t, "leaf", p.Type) // root leaf
}
if p, _ := tx.Page(5); assert.NotNil(t, p) {
- assert.Equal(t, "leaf", p.Type) // root leaf
+ assert.Equal(t, "freelist", p.Type)
}
p, _ := tx.Page(6)
assert.Nil(t, p)
diff --git a/freelist.go b/freelist.go
index 0d79bb4..149e595 100644
--- a/freelist.go
+++ b/freelist.go
@@ -55,7 +55,7 @@ func (f *freelist) allocate(n int) pgid {
if (i + 1) == n {
f.ids = f.ids[i+1:]
} else {
- copy(f.ids[i-1:], f.ids[i+n-1:])
+ copy(f.ids[i-n+1:], f.ids[i+1:])
f.ids = f.ids[:len(f.ids)-n]
}
return initial
@@ -111,7 +111,7 @@ func (f *freelist) read(p *page) {
ids := ((*[maxAllocSize]pgid)(unsafe.Pointer(&p.ptr)))[0:p.count]
f.ids = make([]pgid, len(ids))
copy(f.ids, ids)
- sort.Sort(pgids(ids))
+ sort.Sort(pgids(f.ids))
}
// write writes the page ids onto a freelist page. All free and pending ids are
diff --git a/freelist_test.go b/freelist_test.go
index 00c71cf..5948f3b 100644
--- a/freelist_test.go
+++ b/freelist_test.go
@@ -29,9 +29,9 @@ func TestFreelist_release(t *testing.T) {
f.free(102, &page{id: 39})
f.release(100)
f.release(101)
- assert.Equal(t, f.ids, []pgid{13, 12, 9})
+ assert.Equal(t, []pgid{9, 12, 13}, f.ids)
f.release(102)
- assert.Equal(t, f.ids, []pgid{39, 13, 12, 9})
+ assert.Equal(t, []pgid{9, 12, 13, 39}, f.ids)
}
// Ensure that a freelist can find contiguous blocks of pages.
@@ -44,6 +44,10 @@ func TestFreelist_allocate(t *testing.T) {
assert.Equal(t, 7, int(f.allocate(1)))
assert.Equal(t, 0, int(f.allocate(0)))
assert.Equal(t, []pgid{9, 18}, f.ids)
+ assert.Equal(t, 9, int(f.allocate(1)))
+ assert.Equal(t, 18, int(f.allocate(1)))
+ assert.Equal(t, 0, int(f.allocate(1)))
+ assert.Equal(t, []pgid{}, f.ids)
}
// Ensure that a freelist can deserialize from a freelist page.
@@ -86,9 +90,9 @@ func TestFreelist_write(t *testing.T) {
// Ensure that the freelist is correct.
// All pages should be present and in reverse order.
assert.Equal(t, len(f2.ids), 5)
- assert.Equal(t, f2.ids[0], pgid(39))
- assert.Equal(t, f2.ids[1], pgid(28))
+ assert.Equal(t, f2.ids[0], pgid(3))
+ assert.Equal(t, f2.ids[1], pgid(11))
assert.Equal(t, f2.ids[2], pgid(12))
- assert.Equal(t, f2.ids[3], pgid(11))
- assert.Equal(t, f2.ids[4], pgid(3))
+ assert.Equal(t, f2.ids[3], pgid(28))
+ assert.Equal(t, f2.ids[4], pgid(39))
}
diff --git a/page.go b/page.go
index cd213a4..78ca898 100644
--- a/page.go
+++ b/page.go
@@ -133,4 +133,4 @@ type pgids []pgid
func (s pgids) Len() int { return len(s) }
func (s pgids) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
-func (s pgids) Less(i, j int) bool { return s[i] > s[j] }
+func (s pgids) Less(i, j int) bool { return s[i] < s[j] }
diff --git a/tx_test.go b/tx_test.go
index 7bf369b..a2612c8 100644
--- a/tx_test.go
+++ b/tx_test.go
@@ -219,7 +219,7 @@ func TestTx_DeleteBucket(t *testing.T) {
db.Update(func(tx *Tx) error {
// Verify that the bucket's page is free.
- assert.Equal(t, []pgid{5, 4}, db.freelist.all())
+ assert.Equal(t, []pgid{4, 5}, db.freelist.all())
// Create the bucket again and make sure there's not a phantom value.
b, err := tx.CreateBucket([]byte("widgets"))