From 7979b4d69b872103ec2020d08175dfffc3cf5d35 Mon Sep 17 00:00:00 2001 From: Linh Vo Date: Fri, 24 Mar 2017 12:18:59 -0500 Subject: [PATCH 1/3] set profile attr for columnLabel --- cache.go | 2 +- executor.go | 24 +++++++++++++++--------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/cache.go b/cache.go index d3e2b6710..f0cd98603 100644 --- a/cache.go +++ b/cache.go @@ -271,7 +271,7 @@ func encodePair(p Pair) *internal.Pair { func decodePair(pb *internal.Pair) Pair { return Pair{ - ID: pb.Key, + ID: pb.Key, Count: pb.Count, } } diff --git a/executor.go b/executor.go index 6e95346e1..3c7a16f04 100644 --- a/executor.go +++ b/executor.go @@ -772,21 +772,27 @@ func (e *Executor) executeBulkSetBitmapAttrs(ctx context.Context, db string, cal // executeSetProfileAttrs executes a SetProfileAttrs() call. func (e *Executor) executeSetProfileAttrs(ctx context.Context, db string, c *pql.Call, opt *ExecOptions) error { - id, ok := c.Args["id"].(uint64) - if !ok { - return errors.New("SetProfileAttrs() id required") - } - - // Copy args and remove reserved fields. - attrs := pql.CopyArgs(c.Args) - delete(attrs, "id") - // Retrieve database. d := e.Index.DB(db) if d == nil { return ErrDatabaseNotFound } + id, ok := c.Args["id"].(uint64) + if !ok { + // Retrieve columnLabel + columnLabel := d.columnLabel + col, ok := c.Args[columnLabel].(uint64) + if !ok { + return errors.New("SetProfileAttrs() id required") + } + id = col + } + + // Copy args and remove reserved fields. + attrs := pql.CopyArgs(c.Args) + delete(attrs, "id") + // Set attributes. if err := d.ProfileAttrStore().SetAttrs(id, attrs); err != nil { return err From 22bac56dc582032879f85e3bd9fe710a4c9e7662 Mon Sep 17 00:00:00 2001 From: Linh Vo Date: Fri, 24 Mar 2017 12:26:49 -0500 Subject: [PATCH 2/3] test setProfileAttrs with option column --- server/server_test.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/server/server_test.go b/server/server_test.go index 56ff9c109..0c717c16d 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -205,6 +205,40 @@ func TestMain_SetProfileAttrs(t *testing.T) { } } +// Ensure program can set profile attributes with columnLabel option. +func TestMain_SetProfileAttrsWithColumnOption(t *testing.T) { + m := MustRunMain() + defer m.Close() + + // Create frames. + client := m.Client() + if err := client.CreateDB(context.Background(), "d", pilosa.DBOptions{ColumnLabel: "col"}); err != nil && err != pilosa.ErrDatabaseExists { + t.Fatal(err) + } else if err := client.CreateFrame(context.Background(), "d", "x.n", pilosa.FrameOptions{}); err != nil { + t.Fatal(err) + } + + // Set bits on bitmap. + if _, err := m.Query("db=d", `SetBit(id=1, frame="x.n", col=100)`); err != nil { + t.Fatal(err) + } else if _, err := m.Query("db=d", `SetBit(id=1, frame="x.n", col=101)`); err != nil { + t.Fatal(err) + } + + // Set profile attributes. + if _, err := m.Query("db=d", `SetProfileAttrs(col=100, foo="bar")`); err != nil { + t.Fatal(err) + } + + // Query bitmap. + if res, err := m.Query("db=d&profiles=true", `Bitmap(id=1, frame="x.n")`); err != nil { + t.Fatal(err) + } else if res != `{"results":[{"attrs":{},"bits":[100,101]}],"profiles":[{"id":100,"attrs":{"foo":"bar"}}]}`+"\n" { + t.Fatalf("unexpected result: %s", res) + } + +} + // Ensure program can set bits on one cluster and then restore to a second cluster. func TestMain_FrameRestore(t *testing.T) { m0 := MustRunMain() From 292302f60ba56ded6687f2b565fe2871758479ea Mon Sep 17 00:00:00 2001 From: Linh Vo Date: Fri, 24 Mar 2017 13:45:00 -0500 Subject: [PATCH 3/3] fix test --- executor.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/executor.go b/executor.go index 3c7a16f04..2b650d12e 100644 --- a/executor.go +++ b/executor.go @@ -778,6 +778,7 @@ func (e *Executor) executeSetProfileAttrs(ctx context.Context, db string, c *pql return ErrDatabaseNotFound } + var colName string id, ok := c.Args["id"].(uint64) if !ok { // Retrieve columnLabel @@ -787,11 +788,14 @@ func (e *Executor) executeSetProfileAttrs(ctx context.Context, db string, c *pql return errors.New("SetProfileAttrs() id required") } id = col + colName = columnLabel + } else { + colName = "id" } // Copy args and remove reserved fields. attrs := pql.CopyArgs(c.Args) - delete(attrs, "id") + delete(attrs, colName) // Set attributes. if err := d.ProfileAttrStore().SetAttrs(id, attrs); err != nil {