Skip to content

Commit e5122a5

Browse files
authored
optimize the implement of tablet (#85)
1 parent dc64b1a commit e5122a5

File tree

6 files changed

+51
-113
lines changed

6 files changed

+51
-113
lines changed

client/session.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -821,7 +821,7 @@ func (s *Session) genInsertTabletsReq(tablets []*Tablet, isAligned bool) (*rpc.T
821821
valuesList[index] = values
822822
timestampsList[index] = tablet.GetTimestampBytes()
823823
typesList[index] = tablet.getDataTypes()
824-
sizeList[index] = int32(tablet.rowCount)
824+
sizeList[index] = int32(tablet.maxRowNumber)
825825
}
826826
request := rpc.TSInsertTabletsReq{
827827
SessionId: s.sessionId,
@@ -977,7 +977,7 @@ func (s *Session) genTSInsertTabletReq(tablet *Tablet, isAligned bool) (*rpc.TSI
977977
Values: values,
978978
Timestamps: tablet.GetTimestampBytes(),
979979
Types: tablet.getDataTypes(),
980-
Size: int32(tablet.rowCount),
980+
Size: int32(tablet.RowSize),
981981
IsAligned: &isAligned,
982982
}
983983
return request, nil

client/tablet.go

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,19 @@ import (
3131
type MeasurementSchema struct {
3232
Measurement string
3333
DataType TSDataType
34-
Encoding TSEncoding
35-
Compressor TSCompressionType
36-
Properties map[string]string
3734
}
3835

3936
type Tablet struct {
4037
deviceId string
4138
measurementSchemas []*MeasurementSchema
4239
timestamps []int64
4340
values []interface{}
44-
rowCount int
41+
maxRowNumber int
42+
RowSize int
4543
}
4644

4745
func (t *Tablet) Len() int {
48-
return t.GetRowCount()
46+
return t.RowSize
4947
}
5048

5149
func (t *Tablet) Swap(i, j int) {
@@ -91,7 +89,7 @@ func (t *Tablet) SetValueAt(value interface{}, columnIndex, rowIndex int) error
9189
return fmt.Errorf("illegal argument columnIndex %d", columnIndex)
9290
}
9391

94-
if rowIndex < 0 || rowIndex > int(t.rowCount) {
92+
if rowIndex < 0 || rowIndex > int(t.maxRowNumber) {
9593
return fmt.Errorf("illegal argument rowIndex %d", rowIndex)
9694
}
9795

@@ -161,15 +159,15 @@ func (t *Tablet) SetValueAt(value interface{}, columnIndex, rowIndex int) error
161159
}
162160

163161
func (t *Tablet) GetRowCount() int {
164-
return t.rowCount
162+
return t.maxRowNumber
165163
}
166164

167165
func (t *Tablet) GetValueAt(columnIndex, rowIndex int) (interface{}, error) {
168166
if columnIndex < 0 || columnIndex > len(t.measurementSchemas) {
169167
return nil, fmt.Errorf("illegal argument columnIndex %d", columnIndex)
170168
}
171169

172-
if rowIndex < 0 || rowIndex > int(t.rowCount) {
170+
if rowIndex < 0 || rowIndex > int(t.maxRowNumber) {
173171
return nil, fmt.Errorf("illegal argument rowIndex %d", rowIndex)
174172
}
175173

@@ -194,9 +192,7 @@ func (t *Tablet) GetValueAt(columnIndex, rowIndex int) (interface{}, error) {
194192

195193
func (t *Tablet) GetTimestampBytes() []byte {
196194
buff := &bytes.Buffer{}
197-
for _, v := range t.timestamps {
198-
binary.Write(buff, binary.BigEndian, v)
199-
}
195+
binary.Write(buff, binary.BigEndian, t.timestamps[0:t.RowSize])
200196
return buff.Bytes()
201197
}
202198

@@ -221,17 +217,17 @@ func (t *Tablet) getValuesBytes() ([]byte, error) {
221217
for i, schema := range t.measurementSchemas {
222218
switch schema.DataType {
223219
case BOOLEAN:
224-
binary.Write(buff, binary.BigEndian, t.values[i].([]bool))
220+
binary.Write(buff, binary.BigEndian, t.values[i].([]bool)[0:t.RowSize])
225221
case INT32:
226-
binary.Write(buff, binary.BigEndian, t.values[i].([]int32))
222+
binary.Write(buff, binary.BigEndian, t.values[i].([]int32)[0:t.RowSize])
227223
case INT64:
228-
binary.Write(buff, binary.BigEndian, t.values[i].([]int64))
224+
binary.Write(buff, binary.BigEndian, t.values[i].([]int64)[0:t.RowSize])
229225
case FLOAT:
230-
binary.Write(buff, binary.BigEndian, t.values[i].([]float32))
226+
binary.Write(buff, binary.BigEndian, t.values[i].([]float32)[0:t.RowSize])
231227
case DOUBLE:
232-
binary.Write(buff, binary.BigEndian, t.values[i].([]float64))
228+
binary.Write(buff, binary.BigEndian, t.values[i].([]float64)[0:t.RowSize])
233229
case TEXT:
234-
for _, s := range t.values[i].([]string) {
230+
for _, s := range t.values[i].([]string)[0:t.RowSize] {
235231
binary.Write(buff, binary.BigEndian, int32(len(s)))
236232
binary.Write(buff, binary.BigEndian, []byte(s))
237233
}
@@ -247,28 +243,32 @@ func (t *Tablet) Sort() error {
247243
return nil
248244
}
249245

250-
func NewTablet(deviceId string, measurementSchemas []*MeasurementSchema, rowCount int) (*Tablet, error) {
246+
func (t *Tablet) Reset() {
247+
t.RowSize = 0
248+
}
249+
250+
func NewTablet(deviceId string, measurementSchemas []*MeasurementSchema, maxRowNumber int) (*Tablet, error) {
251251
tablet := &Tablet{
252252
deviceId: deviceId,
253253
measurementSchemas: measurementSchemas,
254-
rowCount: rowCount,
254+
maxRowNumber: maxRowNumber,
255255
}
256-
tablet.timestamps = make([]int64, rowCount)
256+
tablet.timestamps = make([]int64, maxRowNumber)
257257
tablet.values = make([]interface{}, len(measurementSchemas))
258258
for i, schema := range tablet.measurementSchemas {
259259
switch schema.DataType {
260260
case BOOLEAN:
261-
tablet.values[i] = make([]bool, rowCount)
261+
tablet.values[i] = make([]bool, maxRowNumber)
262262
case INT32:
263-
tablet.values[i] = make([]int32, rowCount)
263+
tablet.values[i] = make([]int32, maxRowNumber)
264264
case INT64:
265-
tablet.values[i] = make([]int64, rowCount)
265+
tablet.values[i] = make([]int64, maxRowNumber)
266266
case FLOAT:
267-
tablet.values[i] = make([]float32, rowCount)
267+
tablet.values[i] = make([]float32, maxRowNumber)
268268
case DOUBLE:
269-
tablet.values[i] = make([]float64, rowCount)
269+
tablet.values[i] = make([]float64, maxRowNumber)
270270
case TEXT:
271-
tablet.values[i] = make([]string, rowCount)
271+
tablet.values[i] = make([]string, maxRowNumber)
272272
default:
273273
return nil, fmt.Errorf("illegal datatype %v", schema.DataType)
274274
}

client/tablet_test.go

Lines changed: 5 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -29,46 +29,22 @@ func createTablet(size int) (*Tablet, error) {
2929
{
3030
Measurement: "restart_count",
3131
DataType: INT32,
32-
Encoding: RLE,
33-
Compressor: SNAPPY,
34-
Properties: map[string]string{
35-
"owner": "Mark Liu",
36-
},
3732
}, {
3833
Measurement: "price",
3934
DataType: DOUBLE,
40-
Encoding: GORILLA,
41-
Compressor: SNAPPY,
4235
}, {
4336
Measurement: "tick_count",
4437
DataType: INT64,
45-
Encoding: RLE,
46-
Compressor: SNAPPY,
4738
}, {
4839
Measurement: "temperature",
4940
DataType: FLOAT,
50-
Encoding: GORILLA,
51-
Compressor: SNAPPY,
52-
Properties: map[string]string{
53-
"owner": "Mark Liu",
54-
},
5541
}, {
5642
Measurement: "description",
5743
DataType: TEXT,
58-
Encoding: PLAIN,
59-
Compressor: SNAPPY,
60-
Properties: map[string]string{
61-
"owner": "Mark Liu",
62-
},
6344
},
6445
{
6546
Measurement: "status",
6647
DataType: BOOLEAN,
67-
Encoding: RLE,
68-
Compressor: SNAPPY,
69-
Properties: map[string]string{
70-
"owner": "Mark Liu",
71-
},
7248
},
7349
}, size)
7450
return tablet, err
@@ -95,46 +71,22 @@ func TestTablet_getDataTypes(t *testing.T) {
9571
{
9672
Measurement: "restart_count",
9773
DataType: INT32,
98-
Encoding: RLE,
99-
Compressor: SNAPPY,
100-
Properties: map[string]string{
101-
"owner": "Mark Liu",
102-
},
10374
}, {
10475
Measurement: "price",
10576
DataType: DOUBLE,
106-
Encoding: GORILLA,
107-
Compressor: SNAPPY,
10877
}, {
10978
Measurement: "tick_count",
11079
DataType: INT64,
111-
Encoding: RLE,
112-
Compressor: SNAPPY,
11380
}, {
11481
Measurement: "temperature",
11582
DataType: FLOAT,
116-
Encoding: GORILLA,
117-
Compressor: SNAPPY,
118-
Properties: map[string]string{
119-
"owner": "Mark Liu",
120-
},
12183
}, {
12284
Measurement: "description",
12385
DataType: TEXT,
124-
Encoding: PLAIN,
125-
Compressor: SNAPPY,
126-
Properties: map[string]string{
127-
"owner": "Mark Liu",
128-
},
12986
},
13087
{
13188
Measurement: "status",
13289
DataType: BOOLEAN,
133-
Encoding: RLE,
134-
Compressor: SNAPPY,
135-
Properties: map[string]string{
136-
"owner": "Mark Liu",
137-
},
13890
},
13991
},
14092
timestamps: []int64{},
@@ -151,7 +103,7 @@ func TestTablet_getDataTypes(t *testing.T) {
151103
measurementSchemas: tt.fields.measurementSchemas,
152104
timestamps: tt.fields.timestamps,
153105
values: tt.fields.values,
154-
rowCount: tt.fields.rowCount,
106+
maxRowNumber: tt.fields.rowCount,
155107
}
156108
if got := tablet.getDataTypes(); !reflect.DeepEqual(got, tt.want) {
157109
t.Errorf("Tablet.getDataTypes() = %v, want %v", got, tt.want)
@@ -418,6 +370,7 @@ func TestTablet_Sort(t *testing.T) {
418370
tablet.SetValueAt("1", 4, 0)
419371
tablet.SetValueAt(true, 5, 0)
420372
tablet.SetTimestamp(3, 0)
373+
tablet.RowSize++
421374

422375
tablet.SetValueAt(int32(2), 0, 1)
423376
tablet.SetValueAt(float64(2.0), 1, 1)
@@ -426,6 +379,7 @@ func TestTablet_Sort(t *testing.T) {
426379
tablet.SetValueAt("2", 4, 1)
427380
tablet.SetValueAt(true, 5, 1)
428381
tablet.SetTimestamp(4, 1)
382+
tablet.RowSize++
429383

430384
tablet.SetValueAt(int32(3), 0, 2)
431385
tablet.SetValueAt(float64(3.0), 1, 2)
@@ -434,6 +388,7 @@ func TestTablet_Sort(t *testing.T) {
434388
tablet.SetValueAt("3", 4, 2)
435389
tablet.SetValueAt(true, 5, 2)
436390
tablet.SetTimestamp(1, 2)
391+
tablet.RowSize++
437392

438393
tablet.SetValueAt(int32(4), 0, 3)
439394
tablet.SetValueAt(float64(4.0), 1, 3)
@@ -442,6 +397,7 @@ func TestTablet_Sort(t *testing.T) {
442397
tablet.SetValueAt("4", 4, 3)
443398
tablet.SetValueAt(true, 5, 3)
444399
tablet.SetTimestamp(2, 3)
400+
tablet.RowSize++
445401

446402
if err := tablet.Sort(); (err != nil) != tt.wantErr {
447403
t.Errorf("Tablet.Sort() error = %v, wantErr %v", err, tt.wantErr)

example/session_example.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,7 @@ func deleteData() {
475475
func insertTablet() {
476476
if tablet, err := createTablet(12); err == nil {
477477
status, err := session.InsertTablet(tablet, false)
478+
tablet.Reset()
478479
checkError(status, err)
479480
} else {
480481
log.Fatal(err)
@@ -484,6 +485,7 @@ func insertTablet() {
484485
func insertAlignedTablet() {
485486
if tablet, err := createTablet(12); err == nil {
486487
status, err := session.InsertAlignedTablet(tablet, false)
488+
tablet.Reset()
487489
checkError(status, err)
488490
} else {
489491
log.Fatal(err)
@@ -502,34 +504,22 @@ func createTablet(rowCount int) (*client.Tablet, error) {
502504
{
503505
Measurement: "restart_count",
504506
DataType: client.INT32,
505-
Encoding: client.RLE,
506-
Compressor: client.SNAPPY,
507507
}, {
508508
Measurement: "price",
509509
DataType: client.DOUBLE,
510-
Encoding: client.GORILLA,
511-
Compressor: client.SNAPPY,
512510
}, {
513511
Measurement: "tick_count",
514512
DataType: client.INT64,
515-
Encoding: client.RLE,
516-
Compressor: client.SNAPPY,
517513
}, {
518514
Measurement: "temperature",
519515
DataType: client.FLOAT,
520-
Encoding: client.GORILLA,
521-
Compressor: client.SNAPPY,
522516
}, {
523517
Measurement: "description",
524518
DataType: client.TEXT,
525-
Encoding: client.PLAIN,
526-
Compressor: client.SNAPPY,
527519
},
528520
{
529521
Measurement: "status",
530522
DataType: client.BOOLEAN,
531-
Encoding: client.RLE,
532-
Compressor: client.SNAPPY,
533523
},
534524
}, rowCount)
535525

@@ -546,6 +536,7 @@ func createTablet(rowCount int) (*client.Tablet, error) {
546536
tablet.SetValueAt(rand.Float32(), 3, row)
547537
tablet.SetValueAt(fmt.Sprintf("Test Device %d", row+1), 4, row)
548538
tablet.SetValueAt(bool(ts%2 == 0), 5, row)
539+
tablet.RowSize++
549540
}
550541
return tablet, nil
551542
}

0 commit comments

Comments
 (0)