github.com/nutsdb/nutsdb@v1.0.4/index.go (about)

     1  // Copyright 2022 The nutsdb Author. All rights reserved.
     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 nutsdb
    16  
    17  type IdxType interface {
    18  	BTree | Set | SortedSet | List
    19  }
    20  
    21  type defaultOp[T IdxType] struct {
    22  	idx map[BucketId]*T
    23  }
    24  
    25  func (op *defaultOp[T]) computeIfAbsent(id BucketId, f func() *T) *T {
    26  	if i, isExist := op.idx[id]; isExist {
    27  		return i
    28  	}
    29  	i := f()
    30  	op.idx[id] = i
    31  	return i
    32  }
    33  
    34  func (op *defaultOp[T]) delete(id BucketId) {
    35  	delete(op.idx, id)
    36  }
    37  
    38  func (op *defaultOp[T]) exist(id BucketId) (*T, bool) {
    39  	i, isExist := op.idx[id]
    40  	return i, isExist
    41  }
    42  
    43  func (op *defaultOp[T]) getIdxLen() int {
    44  	return len(op.idx)
    45  }
    46  
    47  func (op *defaultOp[T]) rangeIdx(f func(elem *T)) {
    48  	for _, t := range op.idx {
    49  		f(t)
    50  	}
    51  }
    52  
    53  type ListIdx struct {
    54  	*defaultOp[List]
    55  }
    56  
    57  func (idx ListIdx) getWithDefault(id BucketId) *List {
    58  	return idx.defaultOp.computeIfAbsent(id, func() *List {
    59  		return NewList()
    60  	})
    61  }
    62  
    63  type BTreeIdx struct {
    64  	*defaultOp[BTree]
    65  }
    66  
    67  func (idx BTreeIdx) getWithDefault(id BucketId) *BTree {
    68  	return idx.defaultOp.computeIfAbsent(id, func() *BTree {
    69  		return NewBTree()
    70  	})
    71  }
    72  
    73  type SetIdx struct {
    74  	*defaultOp[Set]
    75  }
    76  
    77  func (idx SetIdx) getWithDefault(id BucketId) *Set {
    78  	return idx.defaultOp.computeIfAbsent(id, func() *Set {
    79  		return NewSet()
    80  	})
    81  }
    82  
    83  type SortedSetIdx struct {
    84  	*defaultOp[SortedSet]
    85  }
    86  
    87  func (idx SortedSetIdx) getWithDefault(id BucketId, db *DB) *SortedSet {
    88  	return idx.defaultOp.computeIfAbsent(id, func() *SortedSet {
    89  		return NewSortedSet(db)
    90  	})
    91  }
    92  
    93  type index struct {
    94  	list      ListIdx
    95  	bTree     BTreeIdx
    96  	set       SetIdx
    97  	sortedSet SortedSetIdx
    98  }
    99  
   100  func newIndex() *index {
   101  	i := new(index)
   102  	i.list = ListIdx{&defaultOp[List]{idx: map[BucketId]*List{}}}
   103  	i.bTree = BTreeIdx{&defaultOp[BTree]{idx: map[BucketId]*BTree{}}}
   104  	i.set = SetIdx{&defaultOp[Set]{idx: map[BucketId]*Set{}}}
   105  	i.sortedSet = SortedSetIdx{&defaultOp[SortedSet]{idx: map[BucketId]*SortedSet{}}}
   106  	return i
   107  }