github.com/matrixorigin/matrixone@v0.7.0/pkg/vm/engine/tae/tables/jobs/flushblk.go (about)

     1  // Copyright 2021 Matrix Origin
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package jobs
    16  
    17  import (
    18  	"context"
    19  	"github.com/matrixorigin/matrixone/pkg/container/types"
    20  	"github.com/matrixorigin/matrixone/pkg/objectio"
    21  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/catalog"
    22  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/common"
    23  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/containers"
    24  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/dataio/blockio"
    25  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/tasks"
    26  )
    27  
    28  type flushBlkTask struct {
    29  	*tasks.BaseTask
    30  	data   *containers.Batch
    31  	delta  *containers.Batch
    32  	meta   *catalog.BlockEntry
    33  	fs     *objectio.ObjectFS
    34  	ts     types.TS
    35  	blocks []objectio.BlockObject
    36  }
    37  
    38  func NewFlushBlkTask(
    39  	ctx *tasks.Context,
    40  	fs *objectio.ObjectFS,
    41  	ts types.TS,
    42  	meta *catalog.BlockEntry,
    43  	data *containers.Batch,
    44  	delta *containers.Batch,
    45  ) *flushBlkTask {
    46  	task := &flushBlkTask{
    47  		ts:    ts,
    48  		data:  data,
    49  		meta:  meta,
    50  		fs:    fs,
    51  		delta: delta,
    52  	}
    53  	task.BaseTask = tasks.NewBaseTask(task, tasks.IOTask, ctx)
    54  	return task
    55  }
    56  
    57  func (task *flushBlkTask) Scope() *common.ID { return task.meta.AsCommonID() }
    58  
    59  func (task *flushBlkTask) Execute() error {
    60  	name := blockio.EncodeObjectName()
    61  	writer := blockio.NewWriter(context.Background(), task.fs, name)
    62  	block, err := writer.WriteBlock(task.data)
    63  	if err != nil {
    64  		return err
    65  	}
    66  	if err = BuildBlockIndex(writer.GetWriter(), block, task.meta.GetSchema(), task.data, true); err != nil {
    67  		return err
    68  	}
    69  	if task.delta != nil {
    70  		_, err := writer.WriteBlock(task.delta)
    71  		if err != nil {
    72  			return err
    73  		}
    74  	}
    75  	task.blocks, err = writer.Sync()
    76  	return err
    77  }