From 5a9802c3b7b140c8b422edfff4efcc2b0b105f1e Mon Sep 17 00:00:00 2001 From: Matt Jaffee Date: Mon, 2 Jul 2018 17:07:49 -0500 Subject: [PATCH] get rid of test.Holder.ViewRow, replace with more restricted RowTime --- executor_test.go | 5 ++-- field.go | 25 ++++++++++--------- field_internal_test.go | 56 ++++++++++++++++++++++++++++++++++++++++++ test/holder.go | 6 ++--- 4 files changed, 76 insertions(+), 16 deletions(-) diff --git a/executor_test.go b/executor_test.go index 4e708fd62..f54fb7e33 100644 --- a/executor_test.go +++ b/executor_test.go @@ -21,6 +21,7 @@ import ( "strconv" "strings" "testing" + "time" "github.com/davecgh/go-spew/spew" "github.com/google/go-cmp/cmp" @@ -1190,8 +1191,8 @@ func TestExecutor_Execute_Remote_Row(t *testing.T) { t.Fatalf("quuerying remote: %v", err) } - if !reflect.DeepEqual(hldr1.ViewRow("i", "z", "standard_2010", 5).Columns(), []uint64{1500000}) { - t.Fatalf("unexpected cols from row 7: %v", hldr1.ViewRow("i", "z", "standard_2010", 5).Columns()) + if !reflect.DeepEqual(hldr1.RowTime("i", "z", 5, time.Date(2010, time.January, 1, 0, 0, 0, 0, time.UTC), "Y").Columns(), []uint64{1500000}) { + t.Fatalf("unexpected cols from row 7: %v", hldr1.RowTime("i", "z", 5, time.Date(2010, time.January, 1, 0, 0, 0, 0, time.UTC), "Y").Columns()) } }) diff --git a/field.go b/field.go index dc40c1074..93901dda1 100644 --- a/field.go +++ b/field.go @@ -535,6 +535,20 @@ func (f *Field) SetTimeQuantum(q TimeQuantum) error { return nil } +// RowTime gets the row at the particular time with the granularity specified by +// the quantum. +func (f *Field) RowTime(rowID uint64, time time.Time, quantum string) (*Row, error) { + if !TimeQuantum(quantum).Valid() { + return nil, ErrInvalidTimeQuantum + } + viewname := viewsByTime(ViewStandard, time, TimeQuantum(quantum[len(quantum)-1:]))[0] + view := f.view(viewname) + if view == nil { + return nil, errors.Errorf("view with quantum %v not found.", quantum) + } + return view.row(rowID), nil +} + // ViewPath returns the path to a view in the field. func (f *Field) ViewPath(name string) string { return filepath.Join(f.path, "views", name) @@ -668,17 +682,6 @@ func (f *Field) Row(rowID uint64) (*Row, error) { return view.row(rowID), nil } -// ViewRow returns a row for a view and shard. -// TODO: unexport this with views (it's only used in tests). -// TODO we need some blessed interface to get rows directly off of time fields. Field.RowTime(rowID, timestamp, quantum), maybe -func (f *Field) ViewRow(viewName string, rowID uint64) (*Row, error) { - view := f.view(viewName) - if view == nil { - return nil, ErrInvalidView - } - return view.row(rowID), nil -} - // SetBit sets a bit on a view within the field. func (f *Field) SetBit(rowID, colID uint64, t *time.Time) (changed bool, err error) { viewName := ViewStandard diff --git a/field_internal_test.go b/field_internal_test.go index 176a4d0e8..15a81077c 100644 --- a/field_internal_test.go +++ b/field_internal_test.go @@ -19,6 +19,7 @@ import ( "os" "reflect" "testing" + "time" "github.com/pilosa/pilosa/pql" ) @@ -235,6 +236,15 @@ func (f *TestField) Reopen() error { return nil } +func (f *TestField) MustSetBit(row, col uint64, ts ...time.Time) { + for _, t := range ts { + _, err := f.Field.SetBit(row, col, &t) + if err != nil { + panic(err) + } + } +} + // Ensure field can open and retrieve a view. func TestField_CreateViewIfNotExists(t *testing.T) { f := MustOpenField(FieldOptions{}) @@ -279,3 +289,49 @@ func TestField_SetTimeQuantum(t *testing.T) { t.Fatalf("unexpected quantum (reopen): %s", q) } } + +func TestField_RowTime(t *testing.T) { + f := MustOpenField(FieldOptions{Type: FieldTypeTime}) + defer f.Close() + + if err := f.SetTimeQuantum(TimeQuantum("YMDH")); err != nil { + t.Fatal(err) + } + + f.MustSetBit(1, 1, time.Date(2010, time.January, 5, 12, 0, 0, 0, time.UTC)) + f.MustSetBit(1, 2, time.Date(2011, time.January, 5, 12, 0, 0, 0, time.UTC)) + f.MustSetBit(1, 3, time.Date(2010, time.February, 5, 12, 0, 0, 0, time.UTC)) + f.MustSetBit(1, 4, time.Date(2010, time.January, 6, 12, 0, 0, 0, time.UTC)) + f.MustSetBit(1, 5, time.Date(2010, time.January, 5, 13, 0, 0, 0, time.UTC)) + + if r, err := f.RowTime(1, time.Date(2010, time.November, 5, 12, 0, 0, 0, time.UTC), "Y"); err != nil { + t.Fatal(err) + } else if !reflect.DeepEqual(r.Columns(), []uint64{1, 3, 4, 5}) { + t.Fatalf("wrong columns: %#v", r.Columns()) + } + + if r, err := f.RowTime(1, time.Date(2010, time.February, 7, 13, 0, 0, 0, time.UTC), "YM"); err != nil { + t.Fatal(err) + } else if !reflect.DeepEqual(r.Columns(), []uint64{3}) { + t.Fatalf("wrong columns: %#v", r.Columns()) + } + + if r, err := f.RowTime(1, time.Date(2010, time.February, 7, 13, 0, 0, 0, time.UTC), "M"); err != nil { + t.Fatal(err) + } else if !reflect.DeepEqual(r.Columns(), []uint64{3}) { + t.Fatalf("wrong columns: %#v", r.Columns()) + } + + if r, err := f.RowTime(1, time.Date(2010, time.January, 5, 12, 0, 0, 0, time.UTC), "MD"); err != nil { + t.Fatal(err) + } else if !reflect.DeepEqual(r.Columns(), []uint64{1, 5}) { + t.Fatalf("wrong columns: %#v", r.Columns()) + } + + if r, err := f.RowTime(1, time.Date(2010, time.January, 5, 13, 0, 0, 0, time.UTC), "MDH"); err != nil { + t.Fatal(err) + } else if !reflect.DeepEqual(r.Columns(), []uint64{5}) { + t.Fatalf("wrong columns: %#v", r.Columns()) + } + +} diff --git a/test/holder.go b/test/holder.go index d313c037d..94d1c778f 100644 --- a/test/holder.go +++ b/test/holder.go @@ -17,6 +17,7 @@ package test import ( "io/ioutil" "os" + "time" "github.com/pilosa/pilosa" "github.com/pilosa/pilosa/boltdb" @@ -112,14 +113,13 @@ func (h *Holder) RowAttrStore(index, field string) pilosa.AttrStore { return f.RowAttrStore() } -// ViewRow returns a Row for a given field and view. -func (h *Holder) ViewRow(index, field, view string, rowID uint64) *pilosa.Row { +func (h *Holder) RowTime(index, field string, rowID uint64, t time.Time, quantum string) *pilosa.Row { idx := h.MustCreateIndexIfNotExists(index, pilosa.IndexOptions{}) f, err := idx.CreateFieldIfNotExists(field, pilosa.FieldOptions{}) if err != nil { panic(err) } - row, err := f.ViewRow(view, rowID) + row, err := f.RowTime(rowID, t, quantum) if err != nil { panic(err) }