github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/soliton/prefix_helper.go (about) 1 // Copyright 2020 The ql Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSES/QL-LICENSE file. 4 5 // Copyright 2020 WHTCORPS INC, Inc. 6 // 7 // Licensed under the Apache License, Version 2.0 (the "License"); 8 // you may not use this file except in compliance with the License. 9 // You may obtain a copy of the License at 10 // 11 // http://www.apache.org/licenses/LICENSE-2.0 12 // 13 // Unless required by applicable law or agreed to in writing, software 14 // distributed under the License is distributed on an "AS IS" BASIS, 15 // See the License for the specific language governing permissions and 16 // limitations under the License. 17 18 package soliton 19 20 import ( 21 "bytes" 22 23 "github.com/whtcorpsinc/errors" 24 "github.com/whtcorpsinc/milevadb/ekv" 25 ) 26 27 // ScanMetaWithPrefix scans spacetimedata with the prefix. 28 func ScanMetaWithPrefix(retriever ekv.Retriever, prefix ekv.Key, filter func(ekv.Key, []byte) bool) error { 29 iter, err := retriever.Iter(prefix, prefix.PrefixNext()) 30 if err != nil { 31 return errors.Trace(err) 32 } 33 defer iter.Close() 34 35 for { 36 if iter.Valid() && iter.Key().HasPrefix(prefix) { 37 if !filter(iter.Key(), iter.Value()) { 38 break 39 } 40 err = iter.Next() 41 if err != nil { 42 return errors.Trace(err) 43 } 44 } else { 45 break 46 } 47 } 48 49 return nil 50 } 51 52 // DelKeyWithPrefix deletes keys with prefix. 53 func DelKeyWithPrefix(rm ekv.RetrieverMutator, prefix ekv.Key) error { 54 var keys []ekv.Key 55 iter, err := rm.Iter(prefix, prefix.PrefixNext()) 56 if err != nil { 57 return errors.Trace(err) 58 } 59 60 defer iter.Close() 61 for { 62 if iter.Valid() && iter.Key().HasPrefix(prefix) { 63 keys = append(keys, iter.Key().Clone()) 64 err = iter.Next() 65 if err != nil { 66 return errors.Trace(err) 67 } 68 } else { 69 break 70 } 71 } 72 73 for _, key := range keys { 74 err := rm.Delete(key) 75 if err != nil { 76 return errors.Trace(err) 77 } 78 } 79 80 return nil 81 } 82 83 // RowKeyPrefixFilter returns a function which checks whether currentKey has decoded rowKeyPrefix as prefix. 84 func RowKeyPrefixFilter(rowKeyPrefix ekv.Key) ekv.FnKeyCmp { 85 return func(currentKey ekv.Key) bool { 86 // Next until key without prefix of this record. 87 return !bytes.HasPrefix(currentKey, rowKeyPrefix) 88 } 89 }