Skip to content
This repository has been archived by the owner on Mar 9, 2019. It is now read-only.

Commit

Permalink
sort pgids on file open only if they aren't sorted
Browse files Browse the repository at this point in the history
  • Loading branch information
funny-falcon committed Aug 19, 2015
1 parent 064e0fb commit 87b6edd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
4 changes: 3 additions & 1 deletion freelist.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,9 @@ func (f *freelist) read(p *page) {
copy(f.ids, ids)

// Make sure they're sorted.
sort.Sort(pgids(f.ids))
if !pgids(f.ids).isSorted() {
sort.Sort(pgids(f.ids))
}

// Rebuild the page cache.
f.reindex()
Expand Down
11 changes: 11 additions & 0 deletions page.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,17 @@ 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) isSorted() bool {
if len(s) == 0 {
return true
}
for i, v := range s[:len(s)-1] {
if s[i+1] < v {
return false
}
}
return true
}

// merge returns the sorted union of a and b.
func (a pgids) merge(b pgids) pgids {
Expand Down

0 comments on commit 87b6edd

Please sign in to comment.