github.com/matrixorigin/matrixone@v0.7.0/pkg/vm/engine/tae/db/gc/entry.go (about)

     1  // Copyright 2021 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 gc
    16  
    17  import (
    18  	"bytes"
    19  	"fmt"
    20  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/common"
    21  	"sync/atomic"
    22  )
    23  
    24  type TableEntry struct {
    25  	tid    uint64
    26  	blocks []common.ID
    27  	delete []common.ID
    28  }
    29  
    30  type ObjectEntry struct {
    31  	refs  atomic.Int64
    32  	table TableEntry
    33  }
    34  
    35  func NewObjectEntry() *ObjectEntry {
    36  	return &ObjectEntry{
    37  		table: TableEntry{
    38  			blocks: make([]common.ID, 0),
    39  			delete: make([]common.ID, 0),
    40  		},
    41  	}
    42  }
    43  
    44  func (o *ObjectEntry) AddBlock(block common.ID) {
    45  	o.table.tid = block.TableID
    46  	o.table.blocks = append(o.table.blocks, block)
    47  	o.Refs(1)
    48  }
    49  
    50  func (o *ObjectEntry) DelBlock(block common.ID) {
    51  	o.table.tid = block.TableID
    52  	o.table.delete = append(o.table.delete, block)
    53  	o.UnRefs(1)
    54  }
    55  
    56  func (o *ObjectEntry) Refs(n int) {
    57  	o.refs.Add(int64(n))
    58  }
    59  
    60  func (o *ObjectEntry) UnRefs(n int) {
    61  	o.refs.Add(int64(0 - n))
    62  }
    63  
    64  func (o *ObjectEntry) MergeEntry(entry *ObjectEntry) {
    65  	refs := len(entry.table.blocks)
    66  	unRefs := len(entry.table.delete)
    67  	if refs > 0 {
    68  		o.table.blocks = append(o.table.blocks, entry.table.blocks...)
    69  		o.Refs(refs)
    70  	}
    71  
    72  	if unRefs > 0 {
    73  		o.table.delete = append(o.table.delete, entry.table.delete...)
    74  		o.UnRefs(unRefs)
    75  	}
    76  }
    77  
    78  func (o *ObjectEntry) AllowGC() bool {
    79  	if o.refs.Load() < 1 {
    80  		o.table.delete = nil
    81  		o.table.blocks = nil
    82  		return true
    83  	}
    84  	return false
    85  }
    86  
    87  func (o *ObjectEntry) Compare(object *ObjectEntry) bool {
    88  	if o.refs.Load() != object.refs.Load() {
    89  		return false
    90  	}
    91  	if len(o.table.blocks) != len(object.table.blocks) {
    92  		return false
    93  	}
    94  	if len(o.table.delete) != len(object.table.delete) {
    95  		return false
    96  	}
    97  	return true
    98  }
    99  
   100  func (o *ObjectEntry) String() string {
   101  	var w bytes.Buffer
   102  	_, _ = w.WriteString("entry:[")
   103  	_, _ = w.WriteString(fmt.Sprintf("tid: %d, refs: %d ", o.table.tid, o.refs.Load()))
   104  	_, _ = w.WriteString("block:[")
   105  	for _, block := range o.table.blocks {
   106  		_, _ = w.WriteString(fmt.Sprintf(" %v", block.String()))
   107  	}
   108  	_, _ = w.WriteString("]")
   109  	_, _ = w.WriteString("delete:[")
   110  	for _, id := range o.table.delete {
   111  		_, _ = w.WriteString(fmt.Sprintf(" %v", id.String()))
   112  	}
   113  	_, _ = w.WriteString("]")
   114  	_, _ = w.WriteString("]\n")
   115  	return w.String()
   116  }