github.com/matrixorigin/matrixone@v0.7.0/pkg/vm/engine/tae/tables/indexwrapper/mutindex.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 indexwrapper 16 17 import ( 18 "github.com/RoaringBitmap/roaring" 19 "github.com/matrixorigin/matrixone/pkg/common/moerr" 20 "github.com/matrixorigin/matrixone/pkg/container/types" 21 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/containers" 22 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/index" 23 ) 24 25 var _ Index = (*mutableIndex)(nil) 26 27 type mutableIndex struct { 28 art index.SecondaryIndex 29 zonemap *index.ZoneMap 30 } 31 32 func NewPkMutableIndex(keyT types.Type) *mutableIndex { 33 return &mutableIndex{ 34 art: index.NewSimpleARTMap(keyT), 35 zonemap: index.NewZoneMap(keyT), 36 } 37 } 38 39 func (idx *mutableIndex) BatchUpsert(keysCtx *index.KeysCtx, 40 offset int) (err error) { 41 defer func() { 42 err = TranslateError(err) 43 }() 44 if err = idx.zonemap.BatchUpdate(keysCtx); err != nil { 45 return 46 } 47 // logutil.Infof("Pre: %s", idx.art.String()) 48 err = idx.art.BatchInsert(keysCtx, uint32(offset)) 49 // logutil.Infof("Post: %s", idx.art.String()) 50 return 51 } 52 53 func (idx *mutableIndex) Delete(key any) (err error) { 54 defer func() { 55 err = TranslateError(err) 56 }() 57 if _, err = idx.art.Delete(key); err != nil { 58 return 59 } 60 return 61 } 62 63 func (idx *mutableIndex) GetActiveRow(key any) (row []uint32, err error) { 64 defer func() { 65 err = TranslateError(err) 66 // logutil.Infof("[Trace][GetActiveRow] key=%v: err=%v", key, err) 67 }() 68 exist := idx.zonemap.Contains(key) 69 // 1. key is definitely not existed 70 if !exist { 71 err = moerr.NewNotFoundNoCtx() 72 return 73 } 74 // 2. search art tree for key 75 row, err = idx.art.Search(key) 76 err = TranslateError(err) 77 return 78 } 79 80 func (idx *mutableIndex) String() string { 81 return idx.art.String() 82 } 83 func (idx *mutableIndex) Dedup(key any, skipfn func(row uint32) (err error)) (err error) { 84 exist := idx.zonemap.Contains(key) 85 if !exist { 86 return 87 } 88 rows, err := idx.art.Search(key) 89 if err == index.ErrNotFound { 90 err = nil 91 return 92 } 93 for _, row := range rows { 94 if err = skipfn(row); err != nil { 95 return 96 } 97 } 98 return 99 } 100 101 func (idx *mutableIndex) BatchDedup(keys containers.Vector, 102 skipfn func(row uint32) (err error)) (keyselects *roaring.Bitmap, err error) { 103 if keys.Length() == 1 { 104 err = idx.Dedup(keys.Get(0), skipfn) 105 return 106 } 107 exist := idx.zonemap.FastContainsAny(keys) 108 // 1. all keys are definitely not existed 109 if !exist { 110 return 111 } 112 op := func(v any, _ int) error { 113 rows, err := idx.art.Search(v) 114 if err == index.ErrNotFound { 115 return nil 116 } 117 for _, row := range rows { 118 if err = skipfn(row); err != nil { 119 return err 120 } 121 } 122 return nil 123 } 124 if err = keys.ForeachWindow(0, keys.Length(), op, nil); err != nil { 125 if moerr.IsMoErrCode(err, moerr.OkExpectedDup) || moerr.IsMoErrCode(err, moerr.ErrTxnWWConflict) { 126 return 127 } else { 128 panic(err) 129 } 130 } 131 return 132 } 133 134 func (idx *mutableIndex) Destroy() error { 135 return idx.Close() 136 } 137 138 func (idx *mutableIndex) Close() error { 139 idx.art = nil 140 idx.zonemap = nil 141 return nil 142 } 143 144 var _ Index = (*nonPkMutIndex)(nil) 145 146 type nonPkMutIndex struct { 147 zonemap *index.ZoneMap 148 } 149 150 func NewMutableIndex(keyT types.Type) *nonPkMutIndex { 151 return &nonPkMutIndex{ 152 zonemap: index.NewZoneMap(keyT), 153 } 154 } 155 156 func (idx *nonPkMutIndex) Destroy() error { 157 idx.zonemap = nil 158 return nil 159 } 160 161 func (idx *nonPkMutIndex) Close() error { 162 idx.zonemap = nil 163 return nil 164 } 165 func (idx *nonPkMutIndex) GetActiveRow(any) ([]uint32, error) { panic("not support") } 166 func (idx *nonPkMutIndex) String() string { return "nonpk" } 167 func (idx *nonPkMutIndex) BatchUpsert(keysCtx *index.KeysCtx, offset int) (err error) { 168 return TranslateError(idx.zonemap.BatchUpdate(keysCtx)) 169 } 170 171 func (idx *nonPkMutIndex) Dedup(key any, _ func(uint32) error) (err error) { 172 exist := idx.zonemap.Contains(key) 173 // 1. if not in [min, max], key is definitely not found 174 if !exist { 175 return 176 } 177 err = moerr.GetOkExpectedPossibleDup() 178 return 179 } 180 181 func (idx *nonPkMutIndex) BatchDedup( 182 keys containers.Vector, 183 skipfn func(row uint32) (err error)) (keyselects *roaring.Bitmap, err error) { 184 keyselects, exist := idx.zonemap.ContainsAny(keys) 185 // 1. all keys are definitely not existed 186 if !exist { 187 return 188 } 189 err = moerr.GetOkExpectedPossibleDup() 190 return 191 }