github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/parsers/tree/snapshot.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 tree
    16  
    17  import (
    18  	"github.com/matrixorigin/matrixone/pkg/common/reuse"
    19  )
    20  
    21  func init() {
    22  	reuse.CreatePool[DropSnapShot](
    23  		func() *DropSnapShot { return &DropSnapShot{} },
    24  		func(d *DropSnapShot) { d.reset() },
    25  		reuse.DefaultOptions[DropSnapShot](), //.
    26  	) //WithEnableChecker()
    27  }
    28  
    29  type SnapshotLevel int
    30  
    31  const (
    32  	SNAPSHOTLEVELCLUSTER SnapshotLevel = iota
    33  	SNAPSHOTLEVELACCOUNT
    34  	SNAPSHOTLEVELDATABASE
    35  	SNAPSHOTLEVELTABLE
    36  )
    37  
    38  func (s SnapshotLevel) String() string {
    39  	switch s {
    40  	case SNAPSHOTLEVELCLUSTER:
    41  		return "cluster"
    42  	case SNAPSHOTLEVELACCOUNT:
    43  		return "account"
    44  	case SNAPSHOTLEVELDATABASE:
    45  		return "database"
    46  	case SNAPSHOTLEVELTABLE:
    47  		return "table"
    48  	}
    49  	return "unknown"
    50  }
    51  
    52  type SnapshotLevelType struct {
    53  	Level SnapshotLevel
    54  }
    55  
    56  func (node *SnapshotLevelType) Format(ctx *FmtCtx) {
    57  	ctx.WriteString(node.Level.String())
    58  }
    59  
    60  type ObjectInfo struct {
    61  	SLevel  SnapshotLevelType // snapshot level
    62  	ObjName Identifier        // object name
    63  }
    64  
    65  func (node *ObjectInfo) Format(ctx *FmtCtx) {
    66  	node.SLevel.Format(ctx)
    67  	ctx.WriteString(" ")
    68  	node.ObjName.Format(ctx)
    69  }
    70  
    71  type CreateSnapShot struct {
    72  	statementImpl
    73  
    74  	IfNotExists bool
    75  	Name        Identifier // snapshot name
    76  	Object      ObjectInfo
    77  }
    78  
    79  func (node *CreateSnapShot) Format(ctx *FmtCtx) {
    80  	ctx.WriteString("create snapshot ")
    81  	if node.IfNotExists {
    82  		ctx.WriteString("if not exists ")
    83  	}
    84  	node.Name.Format(ctx)
    85  	ctx.WriteString(" for ")
    86  	node.Object.Format(ctx)
    87  }
    88  
    89  func (node *CreateSnapShot) GetStatementType() string { return "Create Snapshot" }
    90  
    91  func (node *CreateSnapShot) GetQueryType() string { return QueryTypeOth }
    92  
    93  type DropSnapShot struct {
    94  	statementImpl
    95  
    96  	IfExists bool
    97  	Name     Identifier // snapshot name
    98  }
    99  
   100  func (node *DropSnapShot) Free() { reuse.Free[DropSnapShot](node, nil) }
   101  
   102  func (node *DropSnapShot) reset() { *node = DropSnapShot{} }
   103  
   104  func (node DropSnapShot) TypeName() string { return "tree.DropSnapShot" }
   105  
   106  func NewDropSnapShot(ifExists bool, Name Identifier) *DropSnapShot {
   107  	drop := reuse.Alloc[DropSnapShot](nil)
   108  	drop.IfExists = ifExists
   109  	drop.Name = Name
   110  	return drop
   111  }
   112  
   113  func (node *DropSnapShot) Format(ctx *FmtCtx) {
   114  	ctx.WriteString("drop snapshot ")
   115  
   116  	if node.IfExists {
   117  		ctx.WriteString("if exists ")
   118  	}
   119  
   120  	node.Name.Format(ctx)
   121  }
   122  
   123  func (node *DropSnapShot) GetStatementType() string { return "Drop Snapshot" }
   124  
   125  func (node *DropSnapShot) GetQueryType() string { return QueryTypeOth }
   126  
   127  type ShowSnapShots struct {
   128  	statementImpl
   129  	Where *Where
   130  }
   131  
   132  func (node *ShowSnapShots) Format(ctx *FmtCtx) {
   133  	ctx.WriteString("show snapshots")
   134  	if node.Where != nil {
   135  		ctx.WriteString(" ")
   136  		node.Where.Format(ctx)
   137  	}
   138  }
   139  
   140  func (node *ShowSnapShots) GetStatementType() string { return "Show Snapshot" }
   141  
   142  func (node *ShowSnapShots) GetQueryType() string { return QueryTypeDQL }
   143  
   144  type RestoreLevel int
   145  
   146  const (
   147  	RESTORELEVELCLUSTER RestoreLevel = iota
   148  	RESTORELEVELACCOUNT
   149  	RESTORELEVELDATABASE
   150  	RESTORELEVELTABLE
   151  )
   152  
   153  func (s RestoreLevel) String() string {
   154  	switch s {
   155  	case RESTORELEVELCLUSTER:
   156  		return "cluster"
   157  	case RESTORELEVELACCOUNT:
   158  		return "account"
   159  	case RESTORELEVELDATABASE:
   160  		return "database"
   161  	case RESTORELEVELTABLE:
   162  		return "table"
   163  	}
   164  	return "unknown"
   165  }
   166  
   167  type RestoreSnapShot struct {
   168  	statementImpl
   169  
   170  	Level         RestoreLevel
   171  	AccountName   Identifier // account name
   172  	DatabaseName  Identifier // database name
   173  	TableName     Identifier // table name
   174  	SnapShotName  Identifier // snapshot name
   175  	ToAccountName Identifier // to account name
   176  }
   177  
   178  func (node *RestoreSnapShot) Format(ctx *FmtCtx) {
   179  	ctx.WriteString("restore ")
   180  	switch node.Level {
   181  	case RESTORELEVELCLUSTER:
   182  		ctx.WriteString("cluster")
   183  	case RESTORELEVELACCOUNT:
   184  		ctx.WriteString("account ")
   185  		node.AccountName.Format(ctx)
   186  	case RESTORELEVELDATABASE:
   187  		ctx.WriteString("account ")
   188  		node.AccountName.Format(ctx)
   189  		ctx.WriteString(" database ")
   190  		node.DatabaseName.Format(ctx)
   191  	case RESTORELEVELTABLE:
   192  		ctx.WriteString("account ")
   193  		node.AccountName.Format(ctx)
   194  		ctx.WriteString(" database ")
   195  		node.DatabaseName.Format(ctx)
   196  		ctx.WriteString(" table ")
   197  		node.TableName.Format(ctx)
   198  	}
   199  
   200  	ctx.WriteString(" from snapshot ")
   201  	node.SnapShotName.Format(ctx)
   202  
   203  	if len(node.ToAccountName) > 0 {
   204  		ctx.WriteString(" to account ")
   205  		node.ToAccountName.Format(ctx)
   206  	}
   207  }
   208  
   209  func (node *RestoreSnapShot) GetStatementType() string { return "Restore Snapshot" }
   210  
   211  func (node *RestoreSnapShot) GetQueryType() string { return QueryTypeOth }