From c330c2e4c010dcf4b733da2d3286fa2cea71acec Mon Sep 17 00:00:00 2001 From: Ben Johnson Date: Thu, 9 Jun 2016 14:25:41 -0600 Subject: [PATCH 1/2] sort bits on import This commit sorts bits from import files before performing the input. --- client.go | 17 +++++++++++++++++ fragment.go | 7 ++++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/client.go b/client.go index 18a116b07..e941dce04 100644 --- a/client.go +++ b/client.go @@ -12,6 +12,7 @@ import ( "math/rand" "net/http" "net/url" + "sort" "strconv" "time" @@ -567,6 +568,16 @@ type Bit struct { // Bits represents a slice of bits. type Bits []Bit +func (p Bits) Swap(i, j int) { p[i], p[j] = p[j], p[i] } +func (p Bits) Len() int { return len(p) } + +func (p Bits) Less(i, j int) bool { + if p[i].BitmapID == p[j].BitmapID { + return p[i].ProfileID < p[j].ProfileID + } + return p[i].BitmapID < p[j].BitmapID +} + // BitmapIDs returns a slice of all the bitmap IDs. func (a Bits) BitmapIDs() []uint64 { other := make([]uint64, len(a)) @@ -592,5 +603,11 @@ func (a Bits) GroupBySlice() map[uint64][]Bit { slice := bit.ProfileID / SliceWidth m[slice] = append(m[slice], bit) } + + for slice, bits := range m { + sort.Sort(Bits(bits)) + m[slice] = bits + } + return m } diff --git a/fragment.go b/fragment.go index b81266045..ef9d22c77 100644 --- a/fragment.go +++ b/fragment.go @@ -848,13 +848,14 @@ func (f *Fragment) Import(bitmapIDs, profileIDs []uint64) error { bmCounter = bitmap.Count() lastID = bitmapID } - - // Invalidate block checksum. - delete(f.checksums, int(bitmapID/HashBlockSize)) if bitmap.SetBit(profileID) { bmCounter += 1 } + + // Invalidate block checksum. + delete(f.checksums, int(bitmapID/HashBlockSize)) } + f.cache.Invalidate() return nil }(); err != nil { From 64c29d4b6e6b774436927ef1c0356b1ae32b3530 Mon Sep 17 00:00:00 2001 From: Ben Johnson Date: Thu, 9 Jun 2016 14:25:41 -0600 Subject: [PATCH 2/2] add buffered snapshot writer This commit wraps the snapshot in a `bufio.Writer`. --- fragment.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/fragment.go b/fragment.go index ef9d22c77..38ed99567 100644 --- a/fragment.go +++ b/fragment.go @@ -2,6 +2,7 @@ package pilosa import ( "archive/tar" + "bufio" "bytes" "crypto/sha1" "encoding/binary" @@ -906,8 +907,11 @@ func (f *Fragment) snapshot() error { defer file.Close() // Write storage to snapshot. - if _, err := f.storage.WriteTo(file); err != nil { + bw := bufio.NewWriter(file) + if _, err := f.storage.WriteTo(bw); err != nil { return fmt.Errorf("snapshot write to: %s", err) + } else if err := bw.Flush(); err != nil { + return fmt.Errorf("flush: %s", err) } // Close current storage.