github.com/matrixorigin/matrixone@v1.2.0/pkg/util/export/writer_factory.go (about) 1 // Copyright 2022 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 export 16 17 import ( 18 "context" 19 "io" 20 "time" 21 22 "github.com/matrixorigin/matrixone/pkg/fileservice" 23 "github.com/matrixorigin/matrixone/pkg/util/export/etl" 24 "github.com/matrixorigin/matrixone/pkg/util/export/table" 25 ) 26 27 var _ table.RowWriter = (*reactWriter)(nil) 28 var _ table.AfterWrite = (*reactWriter)(nil) 29 30 // reactWriter implement table.AfterWrite, it can react before/after FlushAndClose 31 type reactWriter struct { 32 ctx context.Context 33 w table.RowWriter 34 35 // implement AfterWrite 36 afters []table.CheckWriteHook 37 } 38 39 func newWriter(ctx context.Context, w table.RowWriter) *reactWriter { 40 return &reactWriter{ 41 ctx: ctx, 42 w: w, 43 } 44 } 45 46 func (rw *reactWriter) WriteRow(row *table.Row) error { 47 return rw.w.WriteRow(row) 48 } 49 50 func (rw *reactWriter) GetContent() string { 51 return rw.w.GetContent() 52 } 53 54 func (rw *reactWriter) FlushAndClose() (int, error) { 55 n, err := rw.w.FlushAndClose() 56 if err == nil { 57 for _, hook := range rw.afters { 58 hook(rw.ctx) 59 } 60 } 61 return n, err 62 } 63 64 func (rw *reactWriter) AddAfter(hook table.CheckWriteHook) { 65 rw.afters = append(rw.afters, hook) 66 } 67 68 func GetWriterFactory(fs fileservice.FileService, nodeUUID, nodeType string, enableSqlWriter bool) table.WriterFactory { 69 70 var extension = table.CsvExtension 71 var cfg = table.FilePathCfg{NodeUUID: nodeUUID, NodeType: nodeType, Extension: extension} 72 var factory func(ctx context.Context, account string, tbl *table.Table, ts time.Time) table.RowWriter 73 74 switch extension { 75 case table.CsvExtension: 76 factory = func(ctx context.Context, account string, tbl *table.Table, ts time.Time) table.RowWriter { 77 options := []etl.FSWriterOption{ 78 etl.WithFilePath(cfg.LogsFilePathFactory(account, tbl, ts)), 79 } 80 cw := etl.NewCSVWriter(ctx, etl.NewFSWriter(ctx, fs, options...)) 81 if enableSqlWriter { 82 return newWriter(ctx, etl.NewSqlWriter(ctx, tbl, cw)) 83 } else { 84 return newWriter(ctx, cw) 85 } 86 } 87 case table.TaeExtension: 88 // Deprecated 89 } 90 91 bufferWriterFactory := func(ctx context.Context, filepath string) io.WriteCloser { 92 return etl.NewBufWriter(ctx, etl.NewFSWriter(ctx, fs, etl.WithFilePath(filepath))) 93 } 94 95 return table.NewWriterFactoryGetter(factory, bufferWriterFactory) 96 }