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 b27a24b  fix sortTablet bug (#14)
b27a24b is described below

commit b27a24b52d5007a18de087cbdf2ea5a531502886
Author: yanhong wang <[email protected]>
AuthorDate: Tue Feb 23 14:36:39 2021 +0800

    fix sortTablet bug (#14)
---
 client/tablet.go      | 57 +++++++++++++++++++++++++++++++--------------------
 client/tablet_test.go | 28 +++++++++++++++++++------
 2 files changed, 57 insertions(+), 28 deletions(-)

diff --git a/client/tablet.go b/client/tablet.go
index 9a7b988..20050c4 100644
--- a/client/tablet.go
+++ b/client/tablet.go
@@ -44,6 +44,40 @@ type Tablet struct {
        rowCount           int
 }
 
+func (t *Tablet) Len() int {
+       return t.GetRowCount()
+}
+
+func (t *Tablet) Swap(i, j int) {
+       for index, schema := range t.measurementSchemas {
+               switch schema.DataType {
+               case BOOLEAN:
+                       sortedSlice := t.values[index].([]bool)
+                       sortedSlice[i], sortedSlice[j] = sortedSlice[j], 
sortedSlice[i]
+               case INT32:
+                       sortedSlice := t.values[index].([]int32)
+                       sortedSlice[i], sortedSlice[j] = sortedSlice[j], 
sortedSlice[i]
+               case INT64:
+                       sortedSlice := t.values[index].([]int64)
+                       sortedSlice[i], sortedSlice[j] = sortedSlice[j], 
sortedSlice[i]
+               case FLOAT:
+                       sortedSlice := t.values[index].([]float32)
+                       sortedSlice[i], sortedSlice[j] = sortedSlice[j], 
sortedSlice[i]
+               case DOUBLE:
+                       sortedSlice := t.values[index].([]float64)
+                       sortedSlice[i], sortedSlice[j] = sortedSlice[j], 
sortedSlice[i]
+               case TEXT:
+                       sortedSlice := t.values[index].([]string)
+                       sortedSlice[i], sortedSlice[j] = sortedSlice[j], 
sortedSlice[i]
+               }
+       }
+       t.timestamps[i], t.timestamps[j] = t.timestamps[j], t.timestamps[i]
+}
+
+func (t *Tablet) Less(i, j int) bool {
+       return t.timestamps[i] < t.timestamps[j]
+}
+
 func (t *Tablet) SetTimestamp(timestamp int64, rowIndex int) {
        t.timestamps[rowIndex] = timestamp
 }
@@ -209,28 +243,7 @@ func (t *Tablet) getValuesBytes() ([]byte, error) {
 }
 
 func (t *Tablet) Sort() error {
-       sortFunc := func(i int, j int) bool {
-               return t.timestamps[i] < t.timestamps[j]
-       }
-       for i, schema := range t.measurementSchemas {
-               switch schema.DataType {
-               case BOOLEAN:
-                       sort.Slice(t.values[i].([]bool), sortFunc)
-               case INT32:
-                       sort.Slice(t.values[i].([]int32), sortFunc)
-               case INT64:
-                       sort.Slice(t.values[i].([]int64), sortFunc)
-               case FLOAT:
-                       sort.Slice(t.values[i].([]float32), sortFunc)
-               case DOUBLE:
-                       sort.Slice(t.values[i].([]float64), sortFunc)
-               case TEXT:
-                       sort.Slice(t.values[i].([]string), sortFunc)
-               default:
-                       return fmt.Errorf("Illegal datatype %v", 
schema.DataType)
-               }
-       }
-       sort.Slice(t.timestamps, sortFunc)
+       sort.Sort(t)
        return nil
 }
 
diff --git a/client/tablet_test.go b/client/tablet_test.go
index 5f1c7b4..13c397d 100644
--- a/client/tablet_test.go
+++ b/client/tablet_test.go
@@ -400,23 +400,24 @@ func TestTablet_Sort(t *testing.T) {
                {
                        name: "item-1",
                        want: [][]interface{}{
-                               {int32(2), float64(2.0), int64(2), 
float32(2.0), "2", true},
+                               {int32(3), float64(3.0), int64(3), 
float32(3.0), "3", true},
+                               {int32(4), float64(4.0), int64(4), 
float32(4.0), "4", true},
                                {int32(1), float64(1.0), int64(1), 
float32(1.0), "1", true},
+                               {int32(2), float64(2.0), int64(2), 
float32(2.0), "2", true},
                        },
                        wantErr: false,
                },
        }
        for _, tt := range tests {
                t.Run(tt.name, func(t *testing.T) {
-                       tablet, _ := createTablet(2)
-
+                       tablet, _ := createTablet(4)
                        tablet.SetValueAt(int32(1), 0, 0)
                        tablet.SetValueAt(float64(1.0), 1, 0)
                        tablet.SetValueAt(int64(1), 2, 0)
                        tablet.SetValueAt(float32(1.0), 3, 0)
                        tablet.SetValueAt("1", 4, 0)
                        tablet.SetValueAt(true, 5, 0)
-                       tablet.SetTimestamp(1, 0)
+                       tablet.SetTimestamp(3, 0)
 
                        tablet.SetValueAt(int32(2), 0, 1)
                        tablet.SetValueAt(float64(2.0), 1, 1)
@@ -424,12 +425,27 @@ func TestTablet_Sort(t *testing.T) {
                        tablet.SetValueAt(float32(2.0), 3, 1)
                        tablet.SetValueAt("2", 4, 1)
                        tablet.SetValueAt(true, 5, 1)
-                       tablet.SetTimestamp(0, 1)
+                       tablet.SetTimestamp(4, 1)
+
+                       tablet.SetValueAt(int32(3), 0, 2)
+                       tablet.SetValueAt(float64(3.0), 1, 2)
+                       tablet.SetValueAt(int64(3), 2, 2)
+                       tablet.SetValueAt(float32(3.0), 3, 2)
+                       tablet.SetValueAt("3", 4, 2)
+                       tablet.SetValueAt(true, 5, 2)
+                       tablet.SetTimestamp(1, 2)
+
+                       tablet.SetValueAt(int32(4), 0, 3)
+                       tablet.SetValueAt(float64(4.0), 1, 3)
+                       tablet.SetValueAt(int64(4), 2, 3)
+                       tablet.SetValueAt(float32(4.0), 3, 3)
+                       tablet.SetValueAt("4", 4, 3)
+                       tablet.SetValueAt(true, 5, 3)
+                       tablet.SetTimestamp(2, 3)
 
                        if err := tablet.Sort(); (err != nil) != tt.wantErr {
                                t.Errorf("Tablet.Sort() error = %v, wantErr 
%v", err, tt.wantErr)
                        }
-
                        for rowIndex, row := range tt.want {
                                for columnIndex, wantValue := range row {
                                        value, _ := 
tablet.GetValueAt(columnIndex, rowIndex)

Reply via email to