github.com/zuoyebang/bitalosdb@v1.1.1-0.20240516111551-79a8c4d8ce20/internal/sortedkv/kv.go (about)

     1  // Copyright 2021 The Bitalosdb author(hustxrb@163.com) and other contributors.
     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 sortedkv
    16  
    17  import (
    18  	"bytes"
    19  	"sort"
    20  	"strconv"
    21  	"strings"
    22  
    23  	"github.com/zuoyebang/bitalosdb/internal/base"
    24  	"github.com/zuoyebang/bitalosdb/internal/utils"
    25  )
    26  
    27  const sortedKeyPrefix = "sorted_key_prefix_"
    28  
    29  type SortedKVItem struct {
    30  	Key   *base.InternalKey
    31  	Value []byte
    32  }
    33  type SortedKVList []SortedKVItem
    34  
    35  func (x SortedKVList) Len() int { return len(x) }
    36  func (x SortedKVList) Less(i, j int) bool {
    37  	return bytes.Compare(x[i].Key.UserKey, x[j].Key.UserKey) == -1
    38  }
    39  func (x SortedKVList) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
    40  
    41  func ParseSortedKeyInt(k []byte) int {
    42  	strs := strings.Split(string(k), "_")
    43  	n, _ := strconv.Atoi(strs[len(strs)-1])
    44  	return n
    45  }
    46  
    47  func MakeSortedKVListForBitrie(start, end int, seqNum uint64, vsize int) SortedKVList {
    48  	var kvList SortedKVList
    49  	for i := start; i < end; i++ {
    50  		key := MakeSortedKeyForBitrie(i)
    51  		ikey := base.MakeInternalKey(key, seqNum, base.InternalKeyKindSet)
    52  		kvList = append(kvList, SortedKVItem{
    53  			Key:   &ikey,
    54  			Value: utils.FuncRandBytes(vsize),
    55  		})
    56  		seqNum++
    57  	}
    58  
    59  	sort.Sort(kvList)
    60  	return kvList
    61  }
    62  
    63  func MakeSortedKVList(start, end int, seqNum uint64, vsize int) SortedKVList {
    64  	var kvList SortedKVList
    65  	for i := start; i < end; i++ {
    66  		key := MakeSortedKey(i)
    67  		ikey := base.MakeInternalKey(key, seqNum, base.InternalKeyKindSet)
    68  		kvList = append(kvList, SortedKVItem{
    69  			Key:   &ikey,
    70  			Value: utils.FuncRandBytes(vsize),
    71  		})
    72  		seqNum++
    73  	}
    74  
    75  	sort.Sort(kvList)
    76  	return kvList
    77  }
    78  
    79  func MakeSortedKV2List(start, end int, seqNum uint64, vsize int) SortedKVList {
    80  	var kvList SortedKVList
    81  	for i := start; i < end; i++ {
    82  		version := uint64(i/10 + 100)
    83  		slotId := uint16(version % 65535)
    84  		key := MakeKey2([]byte(sortedKeyPrefix+strconv.Itoa(i)), slotId, version)
    85  		ikey := base.MakeInternalKey(key, seqNum, base.InternalKeyKindSet)
    86  		kvList = append(kvList, SortedKVItem{
    87  			Key:   &ikey,
    88  			Value: utils.FuncRandBytes(vsize),
    89  		})
    90  		seqNum++
    91  	}
    92  
    93  	sort.Sort(kvList)
    94  	return kvList
    95  }
    96  
    97  func MakeSlotSortedKVList(start, end int, seqNum uint64, vsize int, slotId uint16) SortedKVList {
    98  	var kvList SortedKVList
    99  	for i := start; i < end; i++ {
   100  		key := MakeSlotKey([]byte(sortedKeyPrefix+strconv.Itoa(i)), slotId)
   101  		ikey := base.MakeInternalKey(key, seqNum, base.InternalKeyKindSet)
   102  		kvList = append(kvList, SortedKVItem{
   103  			Key:   &ikey,
   104  			Value: utils.FuncRandBytes(vsize),
   105  		})
   106  		seqNum++
   107  	}
   108  
   109  	sort.Sort(kvList)
   110  	return kvList
   111  }
   112  
   113  func MakeSlotSortedKVList2(start, end int, seqNum uint64, vsize int, slotId uint16) SortedKVList {
   114  	var kvList SortedKVList
   115  	for i := start; i < end; i++ {
   116  		version := uint64(i/10 + 100)
   117  		key := MakeKey2([]byte(sortedKeyPrefix+strconv.Itoa(i)), slotId, version)
   118  		ikey := base.MakeInternalKey(key, seqNum, base.InternalKeyKindSet)
   119  		kvList = append(kvList, SortedKVItem{
   120  			Key:   &ikey,
   121  			Value: utils.FuncRandBytes(vsize),
   122  		})
   123  		seqNum++
   124  	}
   125  
   126  	sort.Sort(kvList)
   127  	return kvList
   128  }
   129  
   130  func MakeSortedSamePrefixDeleteKVList(start, end int, seqNum uint64, vsize int, slotId uint16) SortedKVList {
   131  	var kvList SortedKVList
   132  	for i := start; i < end; i++ {
   133  		version := uint64(i/10 + 100)
   134  		var ikey base.InternalKey
   135  		if version%2 == 0 && IsLastVersionKey(i) {
   136  			key := MakeKey2(nil, slotId, version)
   137  			ikey = base.MakeInternalKey(key, seqNum, base.InternalKeyKindPrefixDelete)
   138  		} else {
   139  			key := MakeKey2([]byte(sortedKeyPrefix+strconv.Itoa(i)), slotId, version)
   140  			ikey = base.MakeInternalKey(key, seqNum, base.InternalKeyKindSet)
   141  		}
   142  		kvList = append(kvList, SortedKVItem{
   143  			Key:   &ikey,
   144  			Value: utils.FuncRandBytes(vsize),
   145  		})
   146  		seqNum++
   147  	}
   148  
   149  	sort.Sort(kvList)
   150  	return kvList
   151  }
   152  
   153  func MakeSortedSamePrefixDeleteKVList2(start, end int, seqNum uint64, vsize int, slotId uint16, versionNum int) SortedKVList {
   154  	var kvList SortedKVList
   155  	for i := start; i < end; i++ {
   156  		version := uint64(i/versionNum + 100)
   157  		key := MakeKey2([]byte(sortedKeyPrefix+strconv.Itoa(i)), slotId, version)
   158  		ikey := base.MakeInternalKey(key, seqNum, base.InternalKeyKindSet)
   159  		kvList = append(kvList, SortedKVItem{
   160  			Key:   &ikey,
   161  			Value: utils.FuncRandBytes(vsize),
   162  		})
   163  		seqNum++
   164  	}
   165  
   166  	sort.Sort(kvList)
   167  	return kvList
   168  }
   169  
   170  func MakeSortedSameVersionKVList(start, end int, seqNum uint64, version uint64, vsize int, slotId uint16) SortedKVList {
   171  	var kvList SortedKVList
   172  	for i := start; i < end; i++ {
   173  		key := MakeKey2([]byte(sortedKeyPrefix+strconv.Itoa(i)), slotId, version)
   174  		ikey := base.MakeInternalKey(key, seqNum, base.InternalKeyKindSet)
   175  		kvList = append(kvList, SortedKVItem{
   176  			Key:   &ikey,
   177  			Value: utils.FuncRandBytes(vsize),
   178  		})
   179  		seqNum++
   180  	}
   181  
   182  	sort.Sort(kvList)
   183  	return kvList
   184  }
   185  
   186  func IsPrefixDeleteKey(version uint64) bool {
   187  	return version%2 == 0
   188  }
   189  
   190  func IsLastVersionKey(i int) bool {
   191  	return i%10 == 9
   192  }