// Helper function. Given a client, key, columnFamily, value inserts into the table under column 'a' func insertKeyValue(c *gohbase.Client, key, columnFamily string, value []byte) error { values := map[string]map[string][]byte{columnFamily: map[string][]byte{}} values[columnFamily]["a"] = value putRequest, err := hrpc.NewPutStr(context.Background(), table, key, values) _, err = c.Put(putRequest) return err }
func BenchmarkMutateSerializeWithNestedMaps(b *testing.B) { b.ReportAllocs() for i := 0; i < b.N; i++ { data := map[string]map[string][]byte{ "cf": map[string][]byte{ "a": []byte{10}, "b": []byte{20}, "c": []byte{30, 0}, "d": []byte{40, 0, 0, 0}, "e": []byte{50, 0, 0, 0, 0, 0, 0, 0}, "f": []byte{60}, "g": []byte{70}, "h": []byte{80, 0}, "i": []byte{90, 0, 0, 0}, "j": []byte{100, 0, 0, 0, 0, 0, 0, 0}, "k": []byte{0, 0, 220, 66}, "l": []byte{0, 0, 0, 0, 0, 0, 94, 64}, "m": []byte{0, 0, 2, 67, 0, 0, 0, 0}, "n": []byte{0, 0, 0, 0, 0, 128, 97, 64, 0, 0, 0, 0, 0, 0, 0, 0}, "o": []byte{150}, "p": []byte{4, 8, 15, 26, 23, 42}, "q": []byte{1, 1, 3, 5, 8, 13, 21, 34, 55}, "r": []byte("This is a test string."), }, } mutate, err := hrpc.NewPutStr(context.Background(), "", "", data) if err != nil { b.Errorf("Error creating mutate: %v", err) } mutate.SetRegion(region.NewInfo(nil, nil, nil, nil)) mutate.Serialize() } }
func TestPutMultipleCells(t *testing.T) { key := "row2.5" values := map[string]map[string][]byte{"cf": map[string][]byte{}, "cf2": map[string][]byte{}} values["cf"]["a"] = []byte("a") values["cf"]["b"] = []byte("b") values["cf2"]["a"] = []byte("a") c := gohbase.NewClient(*host) putRequest, err := hrpc.NewPutStr(context.Background(), table, key, values) _, err = c.Put(putRequest) if err != nil { t.Errorf("Put returned an error: %v", err) } families := map[string][]string{"cf": nil, "cf2": nil} get, err := hrpc.NewGetStr(context.Background(), table, key, hrpc.Families(families)) rsp, err := c.Get(get) if err != nil { t.Errorf("Get returned an error: %v", err) } cells := rsp.GetResult().Cell if len(cells) != 3 { t.Errorf("Get expected 3 cells. Received: %d", len(cells)) } for _, cell := range cells { if !bytes.Equal(cell.GetQualifier(), cell.GetValue()) { t.Errorf("Get returned an incorrect result. Expected: %v, Received: %v", cell.GetQualifier(), cell.GetValue()) } } }
func TestCheckAndPutParallel(t *testing.T) { c := gohbase.NewClient(*host) keyPrefix := "row100.5" // TODO: Currently have to CheckTable before initiating the requests // otherwise we face runaway client generation - one for each request. c.CheckTable(context.Background(), table) values := map[string]map[string][]byte{"cf": map[string][]byte{"a": []byte("1")}} capTestFunc := func(p *hrpc.Mutate, ch chan bool) { casRes, err := c.CheckAndPut(p, "cf", "a", []byte{}) if err != nil { t.Errorf("CheckAndPut error: %s", err) } ch <- casRes } // make 10 pairs of CheckAndPut requests for i := 0; i < 10; i++ { ch := make(chan bool, 2) putRequest1, err := hrpc.NewPutStr(context.Background(), table, keyPrefix+string(i), values) if err != nil { t.Fatalf("NewPutStr returned an error: %v", err) } putRequest2, err := hrpc.NewPutStr(context.Background(), table, keyPrefix+string(i), values) if err != nil { t.Fatalf("NewPutStr returned an error: %v", err) } go capTestFunc(putRequest1, ch) go capTestFunc(putRequest2, ch) first := <-ch second := <-ch if first && second { t.Error("CheckAndPut: both requests cannot succeed") } if !first && !second { t.Error("CheckAndPut: both requests cannot fail") } } }
func TestPut(t *testing.T) { key := "row2" values := map[string]map[string][]byte{"cf": map[string][]byte{"a": []byte("1")}} if host == nil { t.Fatal("Host is not set!") } c := gohbase.NewClient(*host) putRequest, err := hrpc.NewPutStr(context.Background(), table, key, values) _, err = c.Put(putRequest) if err != nil { t.Errorf("Put returned an error: %v", err) } ctx, _ := context.WithTimeout(context.Background(), 0) putRequest, err = hrpc.NewPutStr(ctx, table, key, values) _, err = c.Put(putRequest) if err != gohbase.ErrDeadline { t.Errorf("Put ignored the deadline") } }
func doTest(client gohbase.Client, action string) (err error) { switch action { case "get": var getReq *hrpc.Get getReq, err = hrpc.NewGetStr(context.Background(), "t1", "10001") if err != nil { fmt.Printf("hrpc.NewGetStr failed, err is %v", err) return } _, err = client.Get(getReq) if err != nil { fmt.Printf("client.Get failed, err is %v", err) return } case "put": var putReq *hrpc.Mutate rowID, _ := IntRange(0, 3000000) putReq, err = hrpc.NewPutStr(context.Background(), "t1", strconv.Itoa(rowID), map[string]map[string][]byte{ "fam1": map[string][]byte{ "col1": []byte("testvalue"), }, }) if err != nil { fmt.Printf("hrpc.NewPutStr failed, err is %v", err) return } _, err = client.Put(putReq) if err != nil { fmt.Printf("client.Put failed, err is %v", err) return } case "delete": var delReq *hrpc.Mutate rowID, _ := IntRange(0, 3000000) delReq, err = hrpc.NewDelStr(context.Background(), "t1", strconv.Itoa(rowID), map[string]map[string][]byte{ "fam1": map[string][]byte{ "col1": []byte("testvalue"), }, }) if err != nil { fmt.Printf("hrpc.NewDelStr failed, err is %v", err) return } _, err = client.Delete(delReq) if err != nil { fmt.Printf("client.Del failed, err is %v", err) return } } return }
func TestCheckAndPut(t *testing.T) { c := gohbase.NewClient(*host) key := "row100" ef := "cf" eq := "a" var castests = []struct { inValues map[string]map[string][]byte inExpectedValue []byte out bool }{ {map[string]map[string][]byte{"cf": map[string][]byte{"b": []byte("2")}}, nil, true}, // nil instead of empty byte array {map[string]map[string][]byte{"cf": map[string][]byte{"a": []byte("1")}}, []byte{}, true}, {map[string]map[string][]byte{"cf": map[string][]byte{"a": []byte("1")}}, []byte{}, false}, {map[string]map[string][]byte{"cf": map[string][]byte{"a": []byte("2")}}, []byte("1"), true}, {map[string]map[string][]byte{"cf": map[string][]byte{"b": []byte("2")}}, []byte("2"), true}, // put diff column {map[string]map[string][]byte{"cf": map[string][]byte{"b": []byte("2")}}, []byte{}, false}, // diff column {map[string]map[string][]byte{"cf": map[string][]byte{ "b": []byte("100"), "a": []byte("100"), }}, []byte("2"), true}, // multiple values } for _, tt := range castests { putRequest, err := hrpc.NewPutStr(context.Background(), table, key, tt.inValues) if err != nil { t.Fatalf("NewPutStr returned an error: %v", err) } casRes, err := c.CheckAndPut(putRequest, ef, eq, tt.inExpectedValue) if err != nil { t.Fatalf("CheckAndPut error: %s", err) } if casRes != tt.out { t.Errorf("CheckAndPut with put values=%q and expectedValue=%q returned %v, want %v", tt.inValues, tt.inExpectedValue, casRes, tt.out) } } // TODO: check the resulting state by performing a Get request }
func main() { client := gohbase.NewClient("localhost", gohbase.RpcQueueSize(1), gohbase.FlushInterval(time.Nanosecond)) t := time.Now() for i := 0; i < 100; i++ { // Values maps a ColumnFamily -> Qualifiers -> Values. values := map[string]map[string][]byte{"cf": map[string][]byte{"ab": []byte("kmztest")}} putRequest, err := hrpc.NewPutStr(context.Background(), "test", "key"+strconv.Itoa(i), values) checkErr(err) _, err = client.Put(putRequest) checkErr(err) } fmt.Println(float64(time.Now().UnixNano()-t.UnixNano()) / 1000000) fmt.Println("=====================") getRequest, err := hrpc.NewGetStr(context.Background(), "test", "row") checkErr(err) getRsp, err := client.Get(getRequest) checkErr(err) fmt.Println(getRsp) }