You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by hx...@apache.org on 2021/03/08 04:37:44 UTC

[iotdb-client-go] branch main updated: Fix sort bug in session.InsertRecordsOfOneDevice (#16)

This is an automated email from the ASF dual-hosted git repository.

hxd pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/iotdb-client-go.git


The following commit(s) were added to refs/heads/main by this push:
     new 03a810b  Fix sort bug in session.InsertRecordsOfOneDevice (#16)
03a810b is described below

commit 03a810bf61559583da85c625382b8044cc13909d
Author: Mark Liu <ma...@gmail.com>
AuthorDate: Mon Mar 8 12:37:23 2021 +0800

    Fix sort bug in session.InsertRecordsOfOneDevice (#16)
    
    * Fix sort bug in session.InsertRecordsOfOneDevice
    
    * Added testify to go.mod
---
 Makefile                   |  6 +++---
 client/session.go          | 35 ++++++++++++++++++++++++++++-------
 example/session_example.go |  2 +-
 go.mod                     |  5 ++++-
 go.sum                     | 11 +++++++++++
 5 files changed, 47 insertions(+), 12 deletions(-)

diff --git a/Makefile b/Makefile
index 3666264..c4415f2 100644
--- a/Makefile
+++ b/Makefile
@@ -24,12 +24,12 @@ generate:
 	fi
 
 	@if ! command -v thrift &> /dev/null; then \
-		echo "thrift could not be found, please install thrift 0.13.x"; \
+		echo "thrift could not be found, please install thrift 0.13.0"; \
 		exit 1; \
 	fi
 
-	@if [[ "`thrift --version|grep -o '0.13.\d'`" == "" ]]; then \
-		echo "please install thrift 0.13.x"; \
+	@if [[ "`thrift --version|grep -o '0.13.[0-9]'`" == "" ]]; then \
+		echo "please install thrift 0.13.0"; \
 		exit 1; \
 	fi
 
diff --git a/client/session.go b/client/session.go
index a7ed4ec..43fae9d 100644
--- a/client/session.go
+++ b/client/session.go
@@ -308,6 +308,28 @@ func (s *Session) InsertRecord(deviceId string, measurements []string, dataTypes
 	return r, err
 }
 
+type deviceData struct {
+	timestamps        []int64
+	measurementsSlice [][]string
+	dataTypesSlice    [][]TSDataType
+	valuesSlice       [][]interface{}
+}
+
+func (d *deviceData) Len() int {
+	return len(d.timestamps)
+}
+
+func (d *deviceData) Less(i, j int) bool {
+	return d.timestamps[i] < d.timestamps[j]
+}
+
+func (d *deviceData) Swap(i, j int) {
+	d.timestamps[i], d.timestamps[j] = d.timestamps[j], d.timestamps[i]
+	d.measurementsSlice[i], d.measurementsSlice[j] = d.measurementsSlice[j], d.measurementsSlice[i]
+	d.dataTypesSlice[i], d.dataTypesSlice[j] = d.dataTypesSlice[j], d.dataTypesSlice[i]
+	d.valuesSlice[i], d.valuesSlice[j] = d.valuesSlice[j], d.valuesSlice[i]
+}
+
 // InsertRecordsOfOneDevice Insert multiple rows, which can reduce the overhead of network. This method is just like jdbc
 // executeBatch, we pack some insert request in batch and send them to server. If you want improve
 // your performance, please see insertTablet method
@@ -319,13 +341,12 @@ func (s *Session) InsertRecordsOfOneDevice(deviceId string, timestamps []int64,
 	}
 
 	if !sorted {
-		sortFunc := func(i, j int) bool {
-			return timestamps[i] < timestamps[j]
-		}
-		sort.Slice(measurementsSlice, sortFunc)
-		sort.Slice(dataTypesSlice, sortFunc)
-		sort.Slice(valuesSlice, sortFunc)
-		sort.Slice(timestamps, sortFunc)
+		sort.Sort(&deviceData{
+			timestamps:        timestamps,
+			measurementsSlice: measurementsSlice,
+			dataTypesSlice:    dataTypesSlice,
+			valuesSlice:       valuesSlice,
+		})
 	}
 
 	valuesList := make([][]byte, length)
diff --git a/example/session_example.go b/example/session_example.go
index baa3328..7e356b2 100644
--- a/example/session_example.go
+++ b/example/session_example.go
@@ -342,7 +342,7 @@ func insertRecordsOfOneDevice() {
 		}
 		timestamps = []int64{ts, ts - 1}
 	)
-	checkError(session.InsertRecordsOfOneDevice(deviceId, timestamps, measurementsSlice, dataTypes, values, true))
+	checkError(session.InsertRecordsOfOneDevice(deviceId, timestamps, measurementsSlice, dataTypes, values, false))
 }
 
 func deleteData() {
diff --git a/go.mod b/go.mod
index ea91d7b..a87f66a 100644
--- a/go.mod
+++ b/go.mod
@@ -2,4 +2,7 @@ module github.com/apache/iotdb-client-go
 
 go 1.13
 
-require github.com/apache/thrift v0.13.0
+require (
+	github.com/apache/thrift v0.13.0
+	github.com/stretchr/testify v1.7.0
+)
diff --git a/go.sum b/go.sum
index 171755b..8639507 100644
--- a/go.sum
+++ b/go.sum
@@ -1,2 +1,13 @@
 github.com/apache/thrift v0.13.0 h1:5hryIiq9gtn+MiLVn0wP37kb/uTeRZgN08WoCsAhIhI=
 github.com/apache/thrift v0.13.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ=
+github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
+github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
+github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
+github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
+github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
+github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
+gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=