github.com/matrixorigin/matrixone@v0.7.0/pkg/vm/engine/tae/db/gc/droptable.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  )
    22  
    23  type dropTable struct {
    24  	tid    uint64
    25  	drop   bool
    26  	object map[string]*ObjectEntry
    27  }
    28  
    29  func NewDropTable(id uint64) *dropTable {
    30  	return &dropTable{
    31  		tid:    id,
    32  		drop:   false,
    33  		object: make(map[string]*ObjectEntry),
    34  	}
    35  }
    36  
    37  func (d *dropTable) getObject(id common.ID, name string) *ObjectEntry {
    38  	object := d.object[name]
    39  	if object == nil {
    40  		object = NewObjectEntry()
    41  		object.table.tid = id.TableID
    42  	}
    43  	d.object[name] = object
    44  	return object
    45  }
    46  
    47  func (d *dropTable) addBlock(id common.ID, name string) {
    48  	object := d.getObject(id, name)
    49  	object.AddBlock(id)
    50  }
    51  func (d *dropTable) deleteBlock(id common.ID, name string) {
    52  	object := d.getObject(id, name)
    53  	object.DelBlock(id)
    54  }
    55  
    56  func (d *dropTable) merge(dropTable *dropTable) {
    57  	for name, entry := range dropTable.object {
    58  		object := d.object[name]
    59  		if object == nil {
    60  			object = NewObjectEntry()
    61  		}
    62  		object.MergeEntry(entry)
    63  		d.object[name] = object
    64  	}
    65  }
    66  
    67  func (d *dropTable) softGC() []string {
    68  	gc := make([]string, 0)
    69  	for name := range d.object {
    70  		if d.object[name] == nil {
    71  			panic(any("error"))
    72  		}
    73  		if d.drop {
    74  			gc = append(gc, name)
    75  			delete(d.object, name)
    76  			continue
    77  		}
    78  		if d.object[name].AllowGC() {
    79  			gc = append(gc, name)
    80  			delete(d.object, name)
    81  		}
    82  	}
    83  	return gc
    84  }
    85  
    86  func (d *dropTable) Compare(table *dropTable) bool {
    87  	if d.drop != table.drop {
    88  		return false
    89  	}
    90  	if len(d.object) != len(table.object) {
    91  		return false
    92  	}
    93  	for name, entry := range d.object {
    94  		object := table.object[name]
    95  		if object == nil {
    96  			return false
    97  		}
    98  		ok := entry.Compare(object)
    99  		if !ok {
   100  			return ok
   101  		}
   102  
   103  	}
   104  	return true
   105  }
   106  
   107  func (d *dropTable) String() string {
   108  	if len(d.object) == 0 {
   109  		return ""
   110  	}
   111  	var w bytes.Buffer
   112  	_, _ = w.WriteString(" object:[\n")
   113  	for name, entry := range d.object {
   114  		_, _ = w.WriteString(fmt.Sprintf("name: %v ", name))
   115  		_, _ = w.WriteString(entry.String())
   116  	}
   117  	_, _ = w.WriteString("]")
   118  	return w.String()
   119  }