github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/util/prefix_helper.go (about)

     1  // Copyright 2014 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 2015 PingCAP, 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 util
    19  
    20  import (
    21  	"bytes"
    22  
    23  	"github.com/insionng/yougam/libraries/juju/errors"
    24  	"github.com/insionng/yougam/libraries/pingcap/tidb/kv"
    25  )
    26  
    27  // ScanMetaWithPrefix scans metadata with the prefix.
    28  func ScanMetaWithPrefix(retriever kv.Retriever, prefix kv.Key, filter func(kv.Key, []byte) bool) error {
    29  	iter, err := retriever.Seek(prefix)
    30  	if err != nil {
    31  		return errors.Trace(err)
    32  	}
    33  	defer iter.Close()
    34  
    35  	for {
    36  		if err != nil {
    37  			return errors.Trace(err)
    38  		}
    39  
    40  		if iter.Valid() && iter.Key().HasPrefix(prefix) {
    41  			if !filter(iter.Key(), iter.Value()) {
    42  				break
    43  			}
    44  			err = iter.Next()
    45  			if err != nil {
    46  				return errors.Trace(err)
    47  			}
    48  		} else {
    49  			break
    50  		}
    51  	}
    52  
    53  	return nil
    54  }
    55  
    56  // DelKeyWithPrefix deletes keys with prefix.
    57  func DelKeyWithPrefix(rm kv.RetrieverMutator, prefix kv.Key) error {
    58  	var keys []kv.Key
    59  	iter, err := rm.Seek(prefix)
    60  	if err != nil {
    61  		return errors.Trace(err)
    62  	}
    63  
    64  	defer iter.Close()
    65  	for {
    66  		if err != nil {
    67  			return errors.Trace(err)
    68  		}
    69  
    70  		if iter.Valid() && iter.Key().HasPrefix(prefix) {
    71  			keys = append(keys, iter.Key().Clone())
    72  			err = iter.Next()
    73  			if err != nil {
    74  				return errors.Trace(err)
    75  			}
    76  		} else {
    77  			break
    78  		}
    79  	}
    80  
    81  	for _, key := range keys {
    82  		err := rm.Delete(key)
    83  		if err != nil {
    84  			return errors.Trace(err)
    85  		}
    86  	}
    87  
    88  	return nil
    89  }
    90  
    91  // RowKeyPrefixFilter returns a function which checks whether currentKey has decoded rowKeyPrefix as prefix.
    92  func RowKeyPrefixFilter(rowKeyPrefix kv.Key) kv.FnKeyCmp {
    93  	return func(currentKey kv.Key) bool {
    94  		// Next until key without prefix of this record.
    95  		return !bytes.HasPrefix(currentKey, rowKeyPrefix)
    96  	}
    97  }