github.com/dolthub/go-mysql-server@v0.18.0/sql/index_driver.go (about) 1 // Copyright 2020-2021 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 sql 16 17 import ( 18 "gopkg.in/src-d/go-errors.v1" 19 ) 20 21 // IndexBatchSize is the number of rows to save at a time when creating indexes. 22 const IndexBatchSize = uint64(10000) 23 24 // ChecksumKey is the key in an index config to store the checksum. 25 const ChecksumKey = "checksum" 26 27 // IndexDriver manages the coordination between the indexes and their 28 // representation on disk. 29 type IndexDriver interface { 30 // ID returns the unique name of the driver. 31 ID() string 32 // Create a new index. If exprs is more than one expression, it means the 33 // index has multiple columns indexed. If it's just one, it means it may 34 // be an expression or a column. 35 Create(db, table, id string, expressions []Expression, config map[string]string) (DriverIndex, error) 36 // LoadAll loads all indexes for given db and table. 37 LoadAll(ctx *Context, db, table string) ([]DriverIndex, error) 38 // Save the given index for all partitions. 39 Save(*Context, DriverIndex, PartitionIndexKeyValueIter) error 40 // Delete the given index for all partitions in the iterator. 41 Delete(DriverIndex, PartitionIter) error 42 } 43 44 // DriverIndexableTable represents a table that supports being indexed and receiving indexes to be able to speed up its 45 // execution. 46 // Deprecated. DriverIndexableTable support is currently incomplete. The engine will pass CREATE INDEX with a custom 47 // driver through to |IndexKeyValues|, but will not apply DriverIndexes via |WithDriverIndexLookup|. There are 48 // currently no plans to revive this interface. 49 type DriverIndexableTable interface { 50 IndexAddressableTable 51 // WithDriverIndexLookup returns a version of this table with the given lookup applied. 52 // This method is currently unused in the engine. 53 WithDriverIndexLookup(DriverIndexLookup) Table 54 // IndexKeyValues returns an iterator over partitions and ultimately the rows of the table to compute the value of an 55 // index for every row in this table. Used when creating an index for access through an IndexDriver. 56 IndexKeyValues(*Context, []string) (PartitionIndexKeyValueIter, error) 57 } 58 59 // DriverIndex is an index managed by a driver, as opposed to natively by a DB table. 60 // Deprecated. This interface is incompletely supported and may be removed. 61 type DriverIndex interface { 62 Index 63 // Driver ID of the index. 64 Driver() string 65 } 66 67 // DriverIndexLookup is a subset of an index. More specific interfaces can be 68 // implemented to grant more capabilities to the index lookup. 69 // Deprecated. This interface is incompletely supported and may be removed. 70 type DriverIndexLookup interface { 71 Lookup() IndexLookup 72 73 // Values returns the values in the subset of the index. These are used to populate the index via the driver. 74 Values(Partition) (IndexValueIter, error) 75 76 // Indexes returns the IDs of all indexes involved in this lookup. 77 Indexes() []string 78 } 79 80 // Checksumable provides the checksum of some data. 81 type Checksumable interface { 82 // Checksum returns a checksum and an error if there was any problem 83 // computing or obtaining the checksum. 84 Checksum() (string, error) 85 } 86 87 // PartitionIndexKeyValueIter is an iterator of partitions that will return 88 // the partition and the IndexKeyValueIter of that partition. 89 type PartitionIndexKeyValueIter interface { 90 // Next returns the next partition and the IndexKeyValueIter for that 91 // partition. 92 Next(*Context) (Partition, IndexKeyValueIter, error) 93 Closer 94 } 95 96 // IndexKeyValueIter is an iterator of index key values, that is, a tuple of 97 // the values that will be index keys. 98 type IndexKeyValueIter interface { 99 // Next returns the next tuple of index key values. The length of the 100 // returned slice will be the same as the number of columns used to 101 // create this iterator. The second returned parameter is a repo's location. 102 Next(*Context) ([]interface{}, []byte, error) 103 Closer 104 } 105 106 // IndexValueIter is an iterator of index values. 107 type IndexValueIter interface { 108 // Next returns the next value (repo's location) - see IndexKeyValueIter. 109 Next(*Context) ([]byte, error) 110 Closer 111 } 112 113 var ( 114 // ErrIndexIDAlreadyRegistered is the error returned when there is already 115 // an index with the same ID. 116 ErrIndexIDAlreadyRegistered = errors.NewKind("an index with id %q has already been registered") 117 118 // ErrIndexExpressionAlreadyRegistered is the error returned when there is 119 // already an index with the same expression. 120 ErrIndexExpressionAlreadyRegistered = errors.NewKind("there is already an index registered for the expressions: %s") 121 122 // ErrIndexNotFound is returned when the index could not be found. 123 ErrIndexNotFound = errors.NewKind("index %q was not found") 124 125 // ErrIndexDeleteInvalidStatus is returned when the index trying to delete 126 // does not have a ready or outdated state. 127 ErrIndexDeleteInvalidStatus = errors.NewKind("can't delete index %q because it's not ready for removal") 128 )