2021-12-23 12:03:28 +08:00
|
|
|
// Licensed to the LF AI & Data foundation under one
|
|
|
|
// or more contributor license agreements. See the NOTICE file
|
|
|
|
// distributed with this work for additional information
|
|
|
|
// regarding copyright ownership. The ASF licenses this file
|
|
|
|
// to you under the Apache License, Version 2.0 (the
|
|
|
|
// "License"); you may not use this file except in compliance
|
2021-04-19 11:32:24 +08:00
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
//
|
2021-12-23 12:03:28 +08:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2021-04-19 11:32:24 +08:00
|
|
|
//
|
2021-12-23 12:03:28 +08:00
|
|
|
// 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.
|
2021-04-19 11:32:24 +08:00
|
|
|
|
2020-12-09 09:55:56 +08:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2023-09-21 09:45:27 +08:00
|
|
|
|
|
|
|
"github.com/milvus-io/milvus-proto/go-api/v2/schemapb"
|
2020-12-09 09:55:56 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBinlogWriterReader(t *testing.T) {
|
2021-04-19 10:36:19 +08:00
|
|
|
binlogWriter := NewInsertBinlogWriter(schemapb.DataType_Int32, 10, 20, 30, 40)
|
2021-09-09 17:12:55 +08:00
|
|
|
tp := binlogWriter.GetBinlogType()
|
|
|
|
assert.Equal(t, tp, InsertBinlog)
|
|
|
|
|
2021-07-07 19:10:07 +08:00
|
|
|
binlogWriter.SetEventTimeStamp(1000, 2000)
|
2020-12-09 09:55:56 +08:00
|
|
|
defer binlogWriter.Close()
|
|
|
|
eventWriter, err := binlogWriter.NextInsertEventWriter()
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2020-12-09 09:55:56 +08:00
|
|
|
err = eventWriter.AddInt32ToPayload([]int32{1, 2, 3})
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2020-12-10 15:50:09 +08:00
|
|
|
_, err = binlogWriter.GetBuffer()
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.Error(t, err)
|
2021-07-07 19:10:07 +08:00
|
|
|
eventWriter.SetEventTimestamp(1000, 2000)
|
2021-04-12 14:07:46 +08:00
|
|
|
nums, err := binlogWriter.GetRowNums()
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-04-12 14:07:46 +08:00
|
|
|
assert.EqualValues(t, 3, nums)
|
2021-10-11 21:02:37 +08:00
|
|
|
sizeTotal := 20000000
|
|
|
|
binlogWriter.baseBinlogWriter.descriptorEventData.AddExtra(originalSizeKey, fmt.Sprintf("%v", sizeTotal))
|
2021-12-09 12:37:06 +08:00
|
|
|
err = binlogWriter.Finish()
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2020-12-09 09:55:56 +08:00
|
|
|
assert.EqualValues(t, 1, binlogWriter.GetEventNums())
|
2021-04-12 14:07:46 +08:00
|
|
|
nums, err = binlogWriter.GetRowNums()
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2020-12-09 09:55:56 +08:00
|
|
|
assert.EqualValues(t, 3, nums)
|
|
|
|
err = eventWriter.AddInt32ToPayload([]int32{1, 2, 3})
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.Error(t, err)
|
2020-12-09 09:55:56 +08:00
|
|
|
nums, err = binlogWriter.GetRowNums()
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2020-12-09 09:55:56 +08:00
|
|
|
assert.EqualValues(t, 3, nums)
|
|
|
|
|
2020-12-10 15:50:09 +08:00
|
|
|
buffer, err := binlogWriter.GetBuffer()
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-12-09 12:37:06 +08:00
|
|
|
binlogWriter.Close()
|
2020-12-09 09:55:56 +08:00
|
|
|
|
|
|
|
binlogReader, err := NewBinlogReader(buffer)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2020-12-09 09:55:56 +08:00
|
|
|
eventReader, err := binlogReader.NextEventReader()
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2020-12-09 09:55:56 +08:00
|
|
|
_, err = eventReader.GetInt8FromPayload()
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.Error(t, err)
|
2020-12-09 09:55:56 +08:00
|
|
|
payload, err := eventReader.GetInt32FromPayload()
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2020-12-09 09:55:56 +08:00
|
|
|
assert.EqualValues(t, 3, len(payload))
|
|
|
|
assert.EqualValues(t, 1, payload[0])
|
|
|
|
assert.EqualValues(t, 2, payload[1])
|
|
|
|
assert.EqualValues(t, 3, payload[2])
|
|
|
|
|
|
|
|
reader, err := binlogReader.NextEventReader()
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2020-12-09 09:55:56 +08:00
|
|
|
assert.Nil(t, reader)
|
|
|
|
}
|