Skip to content

Commit

Permalink
fix: write failed when value is zero (#38)
Browse files Browse the repository at this point in the history
* fix: write failed when value is zero

* Update examples/object/README.md

Co-authored-by: tison <[email protected]>

* refactor: filter value kind is null ptr

* fix: omit tables length check

* chore: write host val in example

* refactor: rename nil table function

---------

Co-authored-by: tison <[email protected]>
  • Loading branch information
daviderli614 and tisonkun authored Aug 15, 2024
1 parent a3ebb89 commit cf6fa66
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 16 deletions.
12 changes: 7 additions & 5 deletions examples/object/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ mysql> select * from monitors_with_tag;
+------+-----------+--------+------+-------------+---------+----------------------------+
| id | host | memory | cpu | temperature | running | ts |
+------+-----------+--------+------+-------------+---------+----------------------------+
| 1 | 127.0.0.1 | 1 | 1.1 | -1 | 1 | 2024-03-23 14:36:06.591000 |
| 1 | 127.0.0.1 | 1 | 1.1 | -1 | 1 | 2024-03-23 14:36:06.732000 |
| 2 | 127.0.0.2 | 2 | 2 | -2 | 1 | 2024-03-23 14:36:06.591000 |
| 2 | 127.0.0.2 | 2 | 2 | -2 | 1 | 2024-03-23 14:36:06.732000 |
| 0 | 127.0.0.1 | 1 | 1.3 | -1 | 0 | 2024-08-14 09:20:04.475000 |
| 0 | 127.0.0.1 | 1 | 1.3 | -1 | 0 | 2024-08-14 09:20:04.664000 |
| 1 | 127.0.0.2 | 1 | 1.1 | -1 | 1 | 2024-08-14 09:20:04.475000 |
| 1 | 127.0.0.2 | 1 | 1.1 | -1 | 1 | 2024-08-14 09:20:04.664000 |
| 2 | 127.0.0.3 | 2 | 2 | -2 | 1 | 2024-08-14 09:20:04.475000 |
| 2 | 127.0.0.3 | 2 | 2 | -2 | 1 | 2024-08-14 09:20:04.664000 |
+------+-----------+--------+------+-------------+---------+----------------------------+
4 rows in set (0.12 sec)
6 rows in set (0.03 sec)
```
22 changes: 15 additions & 7 deletions examples/object/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,24 @@ func (Monitor) TableName() string {
func initData() []Monitor {
return []Monitor{
{
ID: 1,
Host: "127.0.0.1",
Memory: 1,
Cpu: 1.3,
Temperature: -1,
Ts: time.Now(),
},
{
ID: 1,
Host: "127.0.0.2",
Memory: 1,
Cpu: 1.0,
Temperature: -1,
Ts: time.Now(),
Running: true,
},
{
ID: 2,
Host: "127.0.0.2",
Host: "127.0.0.3",
Memory: 2,
Cpu: 2.0,
Temperature: -2,
Expand All @@ -72,7 +79,7 @@ func initData() []Monitor {
},
{
ID: 3,
Host: "127.0.0.3",
Host: "127.0.0.4",
Memory: 3,
Cpu: 3.0,
Temperature: -3,
Expand All @@ -81,6 +88,7 @@ func initData() []Monitor {
},
}
}

func writeObject(data []Monitor) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
defer cancel()
Expand Down Expand Up @@ -136,19 +144,19 @@ func main() {
// insert
writeObject(data)
// update
data[0].Cpu = 1.1
data[1].Cpu = 1.1
writeObject(data)
// delete
deleteObject(data[2:])
deleteObject(data[3:])

time.Sleep(time.Millisecond * 100)

data = initData()
// stream insert
streamWriteObject(data)
data[0].Cpu = 1.1
data[1].Cpu = 1.1
// stream update
streamWriteObject(data)
// stream delete
streamDeleteObject(data[2:])
streamDeleteObject(data[3:])
}
6 changes: 3 additions & 3 deletions request/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ func (r *Request) WithTables(tables ...*table.Table) *Request {
return r
}

func (r *Request) IsZero() bool {
return r.tables == nil || len(r.tables) == 0
func (r *Request) IsNilTable() bool {
return r.tables == nil
}

func (r *Request) Build() (*gpb.GreptimeRequest, error) {
if r.IsZero() {
if r.IsNilTable() {
return nil, errs.ErrEmptyTable
}

Expand Down
2 changes: 1 addition & 1 deletion schema/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func parseIntOrTimeValue(typ gpb.ColumnDataType, val reflect.Value) (*gpb.Value,

func parseValue(typ gpb.ColumnDataType, val reflect.Value) (*gpb.Value, error) {
val = reflect.Indirect(val)
if !val.IsValid() || val.IsZero() {
if !val.IsValid() {
return nil, nil
}

Expand Down

0 comments on commit cf6fa66

Please sign in to comment.