Example #1
0
func createSketch(fields []string, in *pb.Sketch) error {
	if in.GetType() != pb.SketchType_CARD {
		if len(fields) > 4 {
			return fmt.Errorf("Too many argumets, expected 4 got %d", len(fields))
		}
		num, err := strconv.Atoi(fields[3])
		if err != nil {
			return fmt.Errorf("Expected last argument to be of type int: %q", err)
		}

		in.Properties = &pb.SketchProperties{
			Size:           proto.Int64(int64(num)),
			MaxUniqueItems: proto.Int64(int64(num)),
		}
	}
	_, err := client.CreateSketch(context.Background(), in)
	return err
}
Example #2
0
func getFromSketch(fields []string, in *pb.Sketch) error {
	if len(fields) < 3 {
		return fmt.Errorf("Expected at least 3 values, got %q", len(fields))
	}
	getRequest := &pb.GetRequest{
		Sketches: []*pb.Sketch{in},
		Values:   fields[3:],
	}

	switch in.GetType() {
	case pb.SketchType_CARD:
		reply, err := client.GetCardinality(context.Background(), getRequest)
		if err == nil {
			if len(reply.GetResults()) == 0 {
				fmt.Println(in.GetName(), "does not exist")
			} else {
				fmt.Println("Cardinality:", reply.GetResults()[0].GetCardinality())
			}
		}
		return err
	case pb.SketchType_FREQ:
		reply, err := client.GetFrequency(context.Background(), getRequest)
		if err == nil {
			if len(reply.GetResults()) == 0 {
				fmt.Println(in.GetName(), "does not exist")
			} else {
				for _, v := range reply.GetResults()[0].GetFrequencies() {
					line := fmt.Sprintf("Value: %s\t  Hits: %d", v.GetValue(), v.GetCount())
					_, _ = fmt.Fprintln(w, line)
				}
			}
			_ = w.Flush()
		}
		return err
	case pb.SketchType_MEMB:
		reply, err := client.GetMembership(context.Background(), getRequest)
		if err == nil {
			if len(reply.GetResults()) == 0 {
				fmt.Println(in.GetName(), "does not exist")
			} else {
				for _, v := range reply.GetResults()[0].GetMemberships() {
					line := fmt.Sprintf("Value: %s\t  Member: %t", v.GetValue(), v.GetIsMember())
					_, _ = fmt.Fprintln(w, line)
				}
				_ = w.Flush()
			}
		}
		return err
	case pb.SketchType_RANK:
		reply, err := client.GetRankings(context.Background(), getRequest)
		if err == nil {
			if len(reply.GetResults()) == 0 {
				fmt.Println(in.GetName(), "does not exist")
			} else {
				for i, v := range reply.GetResults()[0].GetRankings() {
					line := fmt.Sprintf("Rank: %d\t  Value: %s\t  Hits: %d", i+1, v.GetValue(), v.GetCount())
					_, _ = fmt.Fprintln(w, line)
				}
				_ = w.Flush()
			}
		}
		return err
	default:
		return fmt.Errorf("Unkown Type %s", in.GetType().String())
	}
}