2021-04-19 11:32:24 +08:00
|
|
|
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
|
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2020-12-09 09:55:56 +08:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-03-05 10:15:27 +08:00
|
|
|
"fmt"
|
2020-12-09 09:55:56 +08:00
|
|
|
|
2021-04-22 14:45:57 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/schemapb"
|
2020-12-09 09:55:56 +08:00
|
|
|
)
|
|
|
|
|
2021-10-03 20:56:05 +08:00
|
|
|
// EventReader is used to parse the events contained in the Binlog file.
|
2020-12-09 09:55:56 +08:00
|
|
|
type EventReader struct {
|
|
|
|
eventHeader
|
|
|
|
eventData
|
|
|
|
PayloadReaderInterface
|
|
|
|
buffer *bytes.Buffer
|
|
|
|
isClosed bool
|
|
|
|
}
|
|
|
|
|
2021-07-07 19:10:07 +08:00
|
|
|
func (reader *EventReader) readHeader() error {
|
2020-12-09 09:55:56 +08:00
|
|
|
if reader.isClosed {
|
2021-07-07 19:10:07 +08:00
|
|
|
return fmt.Errorf("event reader is closed")
|
2020-12-09 09:55:56 +08:00
|
|
|
}
|
|
|
|
header, err := readEventHeader(reader.buffer)
|
|
|
|
if err != nil {
|
2021-07-07 19:10:07 +08:00
|
|
|
return err
|
2020-12-09 09:55:56 +08:00
|
|
|
}
|
|
|
|
reader.eventHeader = *header
|
2021-07-07 19:10:07 +08:00
|
|
|
return nil
|
2020-12-09 09:55:56 +08:00
|
|
|
}
|
|
|
|
|
2021-07-07 19:10:07 +08:00
|
|
|
func (reader *EventReader) readData() error {
|
|
|
|
if reader.isClosed {
|
|
|
|
return fmt.Errorf("event reader is closed")
|
2020-12-09 09:55:56 +08:00
|
|
|
}
|
|
|
|
var data eventData
|
|
|
|
var err error
|
|
|
|
switch reader.TypeCode {
|
|
|
|
case InsertEventType:
|
2020-12-09 11:18:13 +08:00
|
|
|
data, err = readInsertEventDataFixPart(reader.buffer)
|
2020-12-09 09:55:56 +08:00
|
|
|
case DeleteEventType:
|
2020-12-09 11:18:13 +08:00
|
|
|
data, err = readDeleteEventDataFixPart(reader.buffer)
|
2020-12-09 09:55:56 +08:00
|
|
|
case CreateCollectionEventType:
|
2020-12-09 11:18:13 +08:00
|
|
|
data, err = readCreateCollectionEventDataFixPart(reader.buffer)
|
2020-12-09 09:55:56 +08:00
|
|
|
case DropCollectionEventType:
|
2020-12-09 11:18:13 +08:00
|
|
|
data, err = readDropCollectionEventDataFixPart(reader.buffer)
|
2020-12-09 09:55:56 +08:00
|
|
|
case CreatePartitionEventType:
|
2020-12-09 11:18:13 +08:00
|
|
|
data, err = readCreatePartitionEventDataFixPart(reader.buffer)
|
2020-12-09 09:55:56 +08:00
|
|
|
case DropPartitionEventType:
|
2020-12-09 11:18:13 +08:00
|
|
|
data, err = readDropPartitionEventDataFixPart(reader.buffer)
|
2021-09-29 09:52:12 +08:00
|
|
|
case IndexFileEventType:
|
|
|
|
data, err = readIndexFileEventDataFixPart(reader.buffer)
|
2020-12-09 09:55:56 +08:00
|
|
|
default:
|
2021-07-07 19:10:07 +08:00
|
|
|
return fmt.Errorf("unknown header type code: %d", reader.TypeCode)
|
2020-12-09 09:55:56 +08:00
|
|
|
}
|
|
|
|
if err != nil {
|
2021-07-07 19:10:07 +08:00
|
|
|
return err
|
2020-12-09 09:55:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
reader.eventData = data
|
2021-07-07 19:10:07 +08:00
|
|
|
return nil
|
2020-12-09 09:55:56 +08:00
|
|
|
}
|
|
|
|
|
2021-10-03 20:56:05 +08:00
|
|
|
// Close closes EventReader object. It mainly calls the Close method of inner PayloadReaderInterface and
|
|
|
|
// mark itself as closed.
|
2021-12-08 21:11:39 +08:00
|
|
|
func (reader *EventReader) Close() {
|
2020-12-09 09:55:56 +08:00
|
|
|
if !reader.isClosed {
|
|
|
|
reader.isClosed = true
|
2021-12-08 21:11:39 +08:00
|
|
|
reader.PayloadReaderInterface.Close()
|
2020-12-09 09:55:56 +08:00
|
|
|
}
|
|
|
|
}
|
2021-07-07 19:10:07 +08:00
|
|
|
|
2020-12-09 09:55:56 +08:00
|
|
|
func newEventReader(datatype schemapb.DataType, buffer *bytes.Buffer) (*EventReader, error) {
|
|
|
|
reader := &EventReader{
|
|
|
|
eventHeader: eventHeader{
|
|
|
|
baseEventHeader{},
|
|
|
|
},
|
|
|
|
buffer: buffer,
|
|
|
|
isClosed: false,
|
|
|
|
}
|
|
|
|
|
2021-07-07 19:10:07 +08:00
|
|
|
if err := reader.readHeader(); err != nil {
|
2020-12-09 09:55:56 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-07-07 19:10:07 +08:00
|
|
|
if err := reader.readData(); err != nil {
|
2020-12-09 09:55:56 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-07-07 19:10:07 +08:00
|
|
|
next := int(reader.EventLength - reader.eventHeader.GetMemoryUsageInBytes() - reader.GetEventDataFixPartSize())
|
|
|
|
payloadBuffer := buffer.Next(next)
|
2020-12-09 20:07:27 +08:00
|
|
|
payloadReader, err := NewPayloadReader(datatype, payloadBuffer)
|
2020-12-09 09:55:56 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
reader.PayloadReaderInterface = payloadReader
|
|
|
|
return reader, nil
|
|
|
|
}
|