From ab7fbc05ad248c8e935201eb0a45598e3591c00a Mon Sep 17 00:00:00 2001 From: Fabian Reinartz Date: Wed, 28 Dec 2016 08:50:20 +0100 Subject: [PATCH] Zero timestamp as base, use binary search list postings --- db.go | 5 ++--- postings.go | 6 +++++- postings_test.go | 21 --------------------- 3 files changed, 7 insertions(+), 25 deletions(-) diff --git a/db.go b/db.go index e04bdd3c2..f4902f4d5 100644 --- a/db.go +++ b/db.go @@ -201,13 +201,12 @@ func OpenShard(path string, logger log.Logger) (*Shard, error) { } // TODO(fabxc): get time from client-defined `now` function. - baset := time.Now().UnixNano() / int64(time.Millisecond) + // baset := time.Now().UnixNano() / int64(time.Millisecond) + baset := time.Unix(0, 0).UnixNano() / int64(time.Millisecond) if len(pbs) > 0 { baset = pbs[len(pbs)-1].stats.MaxTime } if head == nil { - fmt.Println("creating new head", baset) - head, err = OpenHeadBlock(filepath.Join(path, fmt.Sprintf("%d", baset)), baset) if err != nil { return nil, err diff --git a/postings.go b/postings.go index db0f91077..0beb3339c 100644 --- a/postings.go +++ b/postings.go @@ -164,6 +164,10 @@ type listPostings struct { idx int } +func newListPostings(list []uint32) *listPostings { + return &listPostings{list: list, idx: -1} +} + func (it *listPostings) Value() uint32 { return it.list[it.idx] } @@ -175,7 +179,7 @@ func (it *listPostings) Next() bool { func (it *listPostings) Seek(x uint32) bool { // Do binary search between current position and end. - it.idx = sort.Search(len(it.list)-it.idx, func(i int) bool { + it.idx += sort.Search(len(it.list)-it.idx, func(i int) bool { return it.list[i+it.idx] >= x }) return it.idx < len(it.list) diff --git a/postings_test.go b/postings_test.go index f348e4d2a..f3fde043d 100644 --- a/postings_test.go +++ b/postings_test.go @@ -17,27 +17,6 @@ func (m *mockPostings) Seek(v uint32) bool { return m.seek(v) } func (m *mockPostings) Value() uint32 { return m.value() } func (m *mockPostings) Err() error { return m.err() } -func newListPostings(list []uint32) *mockPostings { - i := -1 - return &mockPostings{ - next: func() bool { - i++ - return i < len(list) - }, - seek: func(v uint32) bool { - for ; i < len(list); i++ { - if list[i] >= v { - return true - } - } - return false - }, - value: func() uint32 { - return list[i] - }, - err: func() error { return nil }, - } -} func TestIntersectIterator(t *testing.T) { var cases = []struct { a, b []uint32