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