Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

table & schema #5

Merged
merged 4 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .github/workflows/license.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
name: License checker

on:
push:
branches:
- main
pull_request:
types: [opened, synchronize, reopened, ready_for_review]

push:
branches:
- "main"

jobs:
license-header-check:
name: license-header-check
Expand Down
9 changes: 8 additions & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
name: golangci-lint
on: [push]

on:
pull_request:
types: [opened, synchronize, reopened, ready_for_review]

push:
branches:
- "main"

permissions:
contents: read
Expand Down
27 changes: 11 additions & 16 deletions client.go → client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package greptime
package client

import (
"context"
Expand All @@ -22,37 +22,32 @@ import (

"github.com/GreptimeTeam/greptimedb-ingester-go/config"
"github.com/GreptimeTeam/greptimedb-ingester-go/insert"
"github.com/GreptimeTeam/greptimedb-ingester-go/table"
)

// Client helps to Insert/Query data Into/From GreptimeDB. A Client is safe for concurrent
// Client helps to write data into GreptimeDB. A Client is safe for concurrent
// use by multiple goroutines,you can have one Client instance in your application.
type Client struct {
cfg *config.Config

// For `insert`, unary calls are supported
greptimeClient greptimepb.GreptimeDatabaseClient
client greptimepb.GreptimeDatabaseClient
}

// NewClient helps to create the greptimedb client, which will be responsible Write/Read data To/From GreptimeDB
func NewClient(cfg *config.Config) (*Client, error) {
// New helps to create the greptimedb client, which will be responsible write data into GreptimeDB.
func New(cfg *config.Config) (*Client, error) {
conn, err := grpc.Dial(cfg.GetGRPCAddr(), cfg.DialOptions...)
if err != nil {
return nil, err
}

greptimeClient := greptimepb.NewGreptimeDatabaseClient(conn)

return &Client{
cfg: cfg,
greptimeClient: greptimeClient,
}, nil
client := greptimepb.NewGreptimeDatabaseClient(conn)
return &Client{cfg: cfg, client: client}, nil
}

// Insert helps to insert multiple rows of multiple tables into greptimedb
func (c *Client) Insert(ctx context.Context, req insert.InsertsRequest) (*greptimepb.GreptimeResponse, error) {
request, err := req.Build(c.cfg)
func (c *Client) Write(ctx context.Context, tables ...*table.Table) (*greptimepb.GreptimeResponse, error) {
req, err := insert.NewRowInsertsRequest(tables...).Build(c.cfg)
if err != nil {
return nil, err
}
return c.greptimeClient.Handle(ctx, request, c.cfg.CallOptions...)
return c.client.Handle(ctx, req, c.cfg.CallOptions...)
}
2 changes: 1 addition & 1 deletion client_test.go → client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package greptime
package client

import (
"testing"
Expand Down
18 changes: 9 additions & 9 deletions stream_client.go → client/stream/stream_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,18 @@ import (

"github.com/GreptimeTeam/greptimedb-ingester-go/config"
"github.com/GreptimeTeam/greptimedb-ingester-go/insert"
"github.com/GreptimeTeam/greptimedb-ingester-go/table"
)

// StreamClient is only for inserting
type StreamClient struct {
client greptimepb.GreptimeDatabase_HandleRequestsClient
type Client struct {
cfg *config.Config
client greptimepb.GreptimeDatabase_HandleRequestsClient
}

// NewStreamClient helps to create a stream insert client.
// If Client has performance issue, you can try the stream client.
func NewStreamClient(cfg *config.Config) (*StreamClient, error) {
func New(cfg *config.Config) (*Client, error) {
conn, err := grpc.Dial(cfg.GetGRPCAddr(), cfg.DialOptions...)
if err != nil {
return nil, err
Expand All @@ -43,19 +44,18 @@ func NewStreamClient(cfg *config.Config) (*StreamClient, error) {
return nil, err
}

return &StreamClient{client: client, cfg: cfg}, nil
return &Client{client: client, cfg: cfg}, nil
}

func (c *StreamClient) Send(ctx context.Context, req insert.InsertsRequest) error {
request, err := req.Build(c.cfg)
func (c *Client) Send(ctx context.Context, tables ...*table.Table) error {
req, err := insert.NewRowInsertsRequest(tables...).Build(c.cfg)
if err != nil {
return err
}

return c.client.Send(request)
return c.client.Send(req)
}

func (c *StreamClient) CloseAndRecv(ctx context.Context) (*greptimepb.AffectedRows, error) {
func (c *Client) CloseAndRecv(ctx context.Context) (*greptimepb.AffectedRows, error) {
resp, err := c.client.CloseAndRecv()
if err != nil {
return nil, err
Expand Down
File renamed without changes.
36 changes: 0 additions & 36 deletions error/error.go

This file was deleted.

30 changes: 30 additions & 0 deletions errs/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2024 Greptime Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package errs

import (
"errors"
)

var (
ErrEmptyName = errors.New("name should not be be empty")
ErrEmptyDatabaseName = errors.New("name of database should not be empty")
ErrEmptyTableName = errors.New("name of table should not be be empty")
ErrEmptyTables = errors.New("please add at least one record before sending insert request")
ErrEmptyTimestamp = errors.New("timestamp should not be empty")
ErrInvalidTimePrecision = errors.New("precision of timestamp is not valid")

ErrColumnNotSet = errors.New("column not set, please call AddColumn first")
)
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ module github.com/GreptimeTeam/greptimedb-ingester-go
go 1.20

require (
github.com/GreptimeTeam/greptime-proto v0.4.0
github.com/bits-and-blooms/bitset v1.7.0
github.com/GreptimeTeam/greptime-proto v0.4.2
github.com/stoewer/go-strcase v1.3.0
github.com/stretchr/testify v1.8.1
google.golang.org/grpc v1.56.3
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
github.com/GreptimeTeam/greptime-proto v0.4.0 h1:2bUcWwz3qJ7g5z+CgLCLll0hvktlHcZGZcNrrGlv8pc=
github.com/GreptimeTeam/greptime-proto v0.4.0/go.mod h1:jk5XBR9qIbSBiDF2Gix1KALyIMCVktcpx91AayOWxmE=
github.com/bits-and-blooms/bitset v1.7.0 h1:YjAGVd3XmtK9ktAbX8Zg2g2PwLIMjGREZJHlV4j7NEo=
github.com/bits-and-blooms/bitset v1.7.0/go.mod h1:gIdJ4wp64HaoK2YrL1Q5/N7Y16edYb8uY+O0FJTyyDA=
github.com/GreptimeTeam/greptime-proto v0.4.2 h1:oX6N1v6+bYQDrh/QM+JmMHqEWjlxAVogCII15JGqbpQ=
github.com/GreptimeTeam/greptime-proto v0.4.2/go.mod h1:jk5XBR9qIbSBiDF2Gix1KALyIMCVktcpx91AayOWxmE=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand Down
40 changes: 5 additions & 35 deletions insert/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@ import (
greptimepb "github.com/GreptimeTeam/greptime-proto/go/greptime/v1"

"github.com/GreptimeTeam/greptimedb-ingester-go/config"
gerr "github.com/GreptimeTeam/greptimedb-ingester-go/error"
gutil "github.com/GreptimeTeam/greptimedb-ingester-go/util"
"github.com/GreptimeTeam/greptimedb-ingester-go/errs"
"github.com/GreptimeTeam/greptimedb-ingester-go/util"
)

type reqHeader struct {
database string
}

func (h *reqHeader) build(cfg *config.Config) (*greptimepb.RequestHeader, error) {
if gutil.IsEmptyString(h.database) {
if util.IsEmptyString(h.database) {
h.database = cfg.Database
}

if gutil.IsEmptyString(h.database) {
return nil, gerr.ErrEmptyDatabase
if util.IsEmptyString(h.database) {
return nil, errs.ErrEmptyDatabaseName
}

header := &greptimepb.RequestHeader{
Expand All @@ -42,33 +42,3 @@ func (h *reqHeader) build(cfg *config.Config) (*greptimepb.RequestHeader, error)

return header, nil
}

type RespHeader struct {
Code uint32
Msg string
}

func (h RespHeader) IsSuccess() bool {
return h.Code == 0
}

func (h RespHeader) IsRateLimited() bool {
return h.Code == 6001
}

func (h RespHeader) IsNil() bool {
return h.Code == 0 && gutil.IsEmptyString(h.Msg)
}

type getRespHeader interface {
GetHeader() *greptimepb.ResponseHeader
}

func ParseRespHeader[T getRespHeader](r T) RespHeader {
header := &RespHeader{}
if r.GetHeader() != nil && r.GetHeader().Status != nil {
header.Code = r.GetHeader().Status.StatusCode
header.Msg = r.GetHeader().Status.ErrMsg
}
return *header
}
4 changes: 2 additions & 2 deletions insert/header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ import (
"github.com/stretchr/testify/assert"

"github.com/GreptimeTeam/greptimedb-ingester-go/config"
gerr "github.com/GreptimeTeam/greptimedb-ingester-go/error"
"github.com/GreptimeTeam/greptimedb-ingester-go/errs"
)

func TestHeaderBuild(t *testing.T) {
h := &reqHeader{}

gh, err := h.build(&config.Config{})
assert.ErrorIs(t, err, gerr.ErrEmptyDatabase)
assert.ErrorIs(t, err, errs.ErrEmptyDatabaseName)
assert.Nil(t, gh)

gh, err = h.build(&config.Config{Database: "database"})
Expand Down
Loading
Loading