github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/doltcore/table/table_reader.go (about) 1 // Copyright 2020 Dolthub, Inc. 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 table 16 17 import ( 18 "context" 19 "fmt" 20 21 "github.com/dolthub/go-mysql-server/sql" 22 23 "github.com/dolthub/dolt/go/libraries/doltcore/doltdb" 24 "github.com/dolthub/dolt/go/libraries/doltcore/row" 25 "github.com/dolthub/dolt/go/libraries/doltcore/schema" 26 "github.com/dolthub/dolt/go/store/types" 27 ) 28 29 // TableReader is an interface for reading rows from a table 30 type TableReader interface { 31 // GetSchema gets the schema of the rows that this reader will return 32 GetSchema() schema.Schema 33 34 // ReadRow reads a row from a table. If there is a bad row the returned error will be non nil, and calling 35 // IsBadRow(err) will be return true. This is a potentially non-fatal error and callers can decide if they want to 36 // continue on a bad row, or fail. 37 ReadRow(ctx context.Context) (row.Row, error) 38 } 39 40 // TableCloser is an interface for a table stream that can be closed to release resources 41 type TableCloser interface { 42 // Close should release resources being held 43 Close(ctx context.Context) error 44 } 45 46 // TableReadCloser is an interface for reading rows from a table, that can be closed. 47 type TableReadCloser interface { 48 TableReader 49 TableCloser 50 } 51 52 // SqlTableReader is a TableReader that can read rows as sql.Row. 53 type SqlTableReader interface { 54 // GetSchema gets the schema of the rows that this reader will return 55 GetSchema() schema.Schema 56 57 // ReadRow reads a row from a table as go-mysql-server sql.Row. 58 ReadSqlRow(ctx context.Context) (sql.Row, error) 59 } 60 61 // NewTableReader creates a SqlTableReader from |tbl| starting from the first record. 62 func NewTableReader(ctx context.Context, tbl *doltdb.Table) (SqlTableReader, error) { 63 sch, err := tbl.GetSchema(ctx) 64 if err != nil { 65 return nil, err 66 } 67 68 if schema.IsKeyless(sch) { 69 return newKeylessTableReader(ctx, tbl, sch, false) 70 } 71 return newPkTableReader(ctx, tbl, sch, false) 72 } 73 74 // NewDoltTableReader creates a SqlTableReader from |tbl| starting from the first record. 75 func NewDoltTableReader(ctx context.Context, tbl *doltdb.Table) (TableReadCloser, error) { 76 sch, err := tbl.GetSchema(ctx) 77 if err != nil { 78 return nil, err 79 } 80 81 if schema.IsKeyless(sch) { 82 return newKeylessTableReader(ctx, tbl, sch, false) 83 } 84 return newPkTableReader(ctx, tbl, sch, false) 85 } 86 87 // NewBufferedTableReader creates a buffered SqlTableReader from |tbl| starting from the first record. 88 func NewBufferedTableReader(ctx context.Context, tbl *doltdb.Table) (SqlTableReader, error) { 89 sch, err := tbl.GetSchema(ctx) 90 if err != nil { 91 return nil, err 92 } 93 94 if schema.IsKeyless(sch) { 95 return newKeylessTableReader(ctx, tbl, sch, true) 96 } 97 return newPkTableReader(ctx, tbl, sch, true) 98 } 99 100 // NewBufferedTableReader creates a buffered SqlTableReader from |tbl| starting from the first record. 101 func NewBufferedReaderForRows(ctx context.Context, rowData types.Map, sch schema.Schema) (SqlTableReader, error) { 102 if schema.IsKeyless(sch) { 103 return newKeylessTableReaderForRows(ctx, rowData, sch, true) 104 } 105 return newPkTableReaderForRows(ctx, rowData, sch, true) 106 } 107 108 // NewBufferedTableReaderForPartition creates a SqlTableReader that reads the rows of |tbl| with indexes 109 // in the half-open interval [start, end). 110 func NewBufferedTableReaderForPartition(ctx context.Context, tbl *doltdb.Table, start, end uint64) (SqlTableReader, error) { 111 if start > end { 112 return nil, fmt.Errorf("invalid partition table reader, start (%d) > end (%d)", start, end) 113 } 114 115 sch, err := tbl.GetSchema(ctx) 116 if err != nil { 117 return nil, err 118 } 119 120 if schema.IsKeyless(sch) { 121 return newKeylessTableReaderForPartition(ctx, tbl, sch, start, end) 122 } 123 return newPkTableReaderForPartition(ctx, tbl, sch, start, end) 124 } 125 126 // NewTableReaderFrom creates a SqlTableReader that reads the rows of |tbl| beginning at the record 127 // whose types.Map key is >= |val|. 128 func NewTableReaderFrom(ctx context.Context, tbl *doltdb.Table, val types.Value) (SqlTableReader, error) { 129 sch, err := tbl.GetSchema(ctx) 130 if err != nil { 131 return nil, err 132 } 133 134 if schema.IsKeyless(sch) { 135 return newKeylessTableReaderFrom(ctx, tbl, sch, val) 136 } 137 return newPkTableReaderFrom(ctx, tbl, sch, val) 138 }