Skip to content

Commit

Permalink
Merge pull request #29 from takashabe/fix-options
Browse files Browse the repository at this point in the history
Use ChainFilters if multiple ReadOption
  • Loading branch information
takashabe authored Sep 17, 2018
2 parents 10cac97 + 0aa161d commit 457be34
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 31 deletions.
20 changes: 7 additions & 13 deletions api/infrastructure/bigtable/bigtable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,22 +117,16 @@ func TestGetRows(t *testing.T) {
},
{
"users",
bigtable.NewRange("3", ""),
bigtable.PrefixRange("4"),
[]bigtable.ReadOption{
bigtable.RowFilter(bigtable.LatestNFilter(1)),
bigtable.RowFilter(
bigtable.ChainFilters(
bigtable.FamilyFilter("^d$"),
bigtable.LatestNFilter(1),
),
),
},
[]*domain.Row{
&domain.Row{
Key: "3",
Columns: []*domain.Column{
&domain.Column{
Family: "d",
Qualifier: "d:row",
Value: []byte("sayaka"),
Version: tm,
},
},
},
&domain.Row{
Key: "4",
Columns: []*domain.Column{
Expand Down
38 changes: 24 additions & 14 deletions api/interfaces/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,30 +239,40 @@ func rowRange(parsedArgs map[string]string) (bigtable.RowRange, error) {
}

func readOption(parsedArgs map[string]string) ([]bigtable.ReadOption, error) {
var opts []bigtable.ReadOption
if count := parsedArgs["count"]; count != "" {
n, err := strconv.ParseInt(count, 0, 64)
if err != nil {
return nil, err
}
opts = append(opts, bigtable.LimitRows(n))
}
var (
opts []bigtable.ReadOption
fils []bigtable.Filter
)

// filters
if regex := parsedArgs["regex"]; regex != "" {
opts = append(opts, bigtable.RowFilter(bigtable.RowKeyFilter(regex)))
// opts = append(opts, bigtable.RowFilter(bigtable.RowKeyFilter(regex)))
fils = append(fils, bigtable.RowKeyFilter(regex))
}
if family := parsedArgs["family"]; family != "" {
// opts = append(opts, bigtable.RowFilter(bigtable.FamilyFilter(fmt.Sprintf("^%s$", family))))
fils = append(fils, bigtable.FamilyFilter(fmt.Sprintf("^%s$", family)))
}
if version := parsedArgs["version"]; version != "" {
n, err := strconv.ParseInt(version, 0, 64)
if err != nil {
return nil, err
}
opts = append(opts, bigtable.RowFilter(bigtable.LatestNFilter(int(n))))
// opts = append(opts, bigtable.RowFilter(bigtable.LatestNFilter(int(n))))
fils = append(fils, bigtable.LatestNFilter(int(n)))
}
if family := parsedArgs["family"]; family != "" {
opts = append(opts, bigtable.RowFilter(bigtable.FamilyFilter(fmt.Sprintf("^%s$", family))))
if len(fils) > 0 {
opts = append(opts, bigtable.RowFilter(bigtable.ChainFilters(fils...)))
}

// TODO: Add read options. refs hbase-shell

// isolated readOption
if count := parsedArgs["count"]; count != "" {
n, err := strconv.ParseInt(count, 0, 64)
if err != nil {
return nil, err
}
opts = append(opts, bigtable.LimitRows(n))
}
return opts, nil
}

Expand Down
49 changes: 45 additions & 4 deletions api/interfaces/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,16 @@ func TestReadOption(t *testing.T) {
"regex": "a",
},
[]bigtable.ReadOption{
chainFilters(bigtable.RowKeyFilter("a")),
bigtable.LimitRows(1),
bigtable.RowFilter(bigtable.RowKeyFilter("a")),
},
},
{
map[string]string{
"family": "d",
},
[]bigtable.ReadOption{
bigtable.RowFilter(bigtable.FamilyFilter("^d$")),
chainFilters(bigtable.FamilyFilter("^d$")),
},
},
}
Expand Down Expand Up @@ -99,7 +99,12 @@ func TestDoReadRowExecutor(t *testing.T) {
"lookup table a version=1 decode=int decode_columns=row:string,404:float",
"----------------------------------------\na\n d:row @ 2018/01/01-00:00:00.000000\n \"a1\"\n",
func(mock *repository.MockBigtable) {
mock.EXPECT().Get(gomock.Any(), "table", "a", bigtable.RowFilter(bigtable.LatestNFilter(1))).Return(
mock.EXPECT().Get(
gomock.Any(),
"table",
"a",
chainFilters(bigtable.LatestNFilter(1)),
).Return(
&domain.Bigtable{
Table: "table",
Rows: []*domain.Row{
Expand All @@ -122,7 +127,43 @@ func TestDoReadRowExecutor(t *testing.T) {
"read table prefix=a version=1 decode=int decode_columns=row:string,404:float",
"----------------------------------------\na\n d:row @ 2018/01/01-00:00:00.000000\n \"a1\"\n",
func(mock *repository.MockBigtable) {
mock.EXPECT().GetRows(gomock.Any(), "table", bigtable.PrefixRange("a"), bigtable.RowFilter(bigtable.LatestNFilter(1))).Return(
mock.EXPECT().GetRows(
gomock.Any(),
"table",
bigtable.PrefixRange("a"),
chainFilters(bigtable.LatestNFilter(1)),
).Return(
&domain.Bigtable{
Table: "table",
Rows: []*domain.Row{
&domain.Row{
Key: "a",
Columns: []*domain.Column{
&domain.Column{
Family: "d",
Qualifier: "d:row",
Value: []byte("a1"),
Version: tm,
},
},
},
},
}, nil).Times(1)
},
},
{
"read table version=1 family=d",
"----------------------------------------\na\n d:row @ 2018/01/01-00:00:00.000000\n \"a1\"\n",
func(mock *repository.MockBigtable) {
mock.EXPECT().GetRows(
gomock.Any(),
"table",
bigtable.RowRange{},
chainFilters(
bigtable.FamilyFilter("^d$"),
bigtable.LatestNFilter(1),
),
).Return(
&domain.Bigtable{
Table: "table",
Rows: []*domain.Row{
Expand Down
6 changes: 6 additions & 0 deletions api/interfaces/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@ package interfaces
import (
"os"
"testing"

"cloud.google.com/go/bigtable"
)

func TestMain(m *testing.M) {
os.Exit(m.Run())
}

func chainFilters(filters ...bigtable.Filter) bigtable.ReadOption {
return bigtable.RowFilter(bigtable.ChainFilters(filters...))
}

0 comments on commit 457be34

Please sign in to comment.