github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/db/operations.go (about)

     1  // Copyright 2021 - 2022 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 db
    16  
    17  import (
    18  	"context"
    19  	fmt "fmt"
    20  	"time"
    21  
    22  	catalog2 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/catalog"
    23  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/tasks"
    24  
    25  	"github.com/matrixorigin/matrixone/pkg/container/batch"
    26  	"github.com/matrixorigin/matrixone/pkg/container/types"
    27  	apipb "github.com/matrixorigin/matrixone/pkg/pb/api"
    28  	"github.com/matrixorigin/matrixone/pkg/vm/engine"
    29  )
    30  
    31  const (
    32  	OpPreCommit  = uint32(apipb.OpCode_OpPreCommit)
    33  	OpGetLogTail = uint32(apipb.OpCode_OpGetLogTail)
    34  )
    35  
    36  type Request interface {
    37  	CreateDatabaseReq |
    38  		DropDatabaseReq |
    39  		CreateRelationReq |
    40  		DropOrTruncateRelationReq |
    41  		UpdateConstraintReq |
    42  		WriteReq |
    43  		apipb.SyncLogTailReq
    44  }
    45  
    46  type Response interface {
    47  	CreateDatabaseResp |
    48  		DropDatabaseResp |
    49  		CreateRelationResp |
    50  		DropOrTruncateRelationResp |
    51  		UpdateConstraintResp |
    52  		WriteResp |
    53  		apipb.SyncLogTailResp
    54  }
    55  
    56  type RelationType uint8
    57  
    58  const (
    59  	RelationTable RelationType = iota + 1
    60  	RelationView
    61  )
    62  
    63  type AccessInfo struct {
    64  	AccountID uint32
    65  	UserID    uint32
    66  	RoleID    uint32
    67  }
    68  
    69  type CreateDatabaseReq struct {
    70  	AccessInfo AccessInfo
    71  	Name       string
    72  	CreateSql  string
    73  	DatTyp     string
    74  	//Global unique, allocated by CN .
    75  	DatabaseId uint64
    76  }
    77  
    78  type FlushTable struct {
    79  	AccessInfo AccessInfo
    80  	DatabaseID uint64
    81  	TableID    uint64
    82  }
    83  
    84  func (m *FlushTable) MarshalBinary() ([]byte, error) {
    85  	return m.Marshal()
    86  }
    87  
    88  func (m *FlushTable) UnmarshalBinary(data []byte) error {
    89  	return m.Unmarshal(data)
    90  }
    91  
    92  type Checkpoint struct {
    93  	FlushDuration time.Duration
    94  }
    95  
    96  func (m *Checkpoint) MarshalBinary() ([]byte, error) {
    97  	return m.Marshal()
    98  }
    99  
   100  func (m *Checkpoint) UnmarshalBinary(data []byte) error {
   101  	return m.Unmarshal(data)
   102  }
   103  
   104  type InterceptCommit struct {
   105  	TableName string
   106  }
   107  
   108  func (m *InterceptCommit) MarshalBinary() ([]byte, error) {
   109  	return m.Marshal()
   110  }
   111  
   112  func (m *InterceptCommit) UnmarshalBinary(data []byte) error {
   113  	return m.Unmarshal(data)
   114  }
   115  
   116  type InspectTN struct {
   117  	AccessInfo AccessInfo
   118  	Operation  string
   119  }
   120  
   121  func (m *InspectTN) MarshalBinary() ([]byte, error) {
   122  	return m.Marshal()
   123  }
   124  
   125  func (m *InspectTN) UnmarshalBinary(data []byte) error {
   126  	return m.Unmarshal(data)
   127  }
   128  
   129  const (
   130  	EnableFaultInjection  = "enable_fault_injection"
   131  	DisableFaultInjection = "disable_fault_injection"
   132  )
   133  
   134  type FaultPoint struct {
   135  	Name   string
   136  	Freq   string
   137  	Action string
   138  	Iarg   int64
   139  	Sarg   string
   140  }
   141  
   142  func (m *FaultPoint) MarshalBinary() ([]byte, error) {
   143  	return m.Marshal()
   144  }
   145  
   146  func (m *FaultPoint) UnmarshalBinary(data []byte) error {
   147  	return m.Unmarshal(data)
   148  }
   149  
   150  type CreateDatabaseResp struct {
   151  	ID uint64
   152  }
   153  
   154  type DropDatabaseReq struct {
   155  	Name string
   156  	ID   uint64
   157  }
   158  
   159  type DropDatabaseResp struct {
   160  	ID uint64
   161  }
   162  
   163  type CreateRelationReq struct {
   164  	AccessInfo   AccessInfo
   165  	DatabaseID   uint64
   166  	DatabaseName string
   167  	Name         string
   168  	RelationId   uint64
   169  	Type         RelationType
   170  	Defs         []engine.TableDef
   171  }
   172  
   173  func (req *CreateRelationReq) String() string {
   174  	return fmt.Sprintf("%+v, %d-%s:%d-%s",
   175  		req.AccessInfo, req.DatabaseID, req.DatabaseName, req.RelationId, req.Name)
   176  }
   177  
   178  type UpdateConstraintReq struct {
   179  	TableId      uint64
   180  	TableName    string
   181  	DatabaseId   uint64
   182  	DatabaseName string
   183  	Constraint   []byte
   184  }
   185  
   186  type UpdateConstraintResp struct{}
   187  
   188  type CreateRelationResp struct {
   189  	ID uint64
   190  }
   191  
   192  type DropOrTruncateRelationReq struct {
   193  	IsDrop       bool
   194  	DatabaseID   uint64
   195  	DatabaseName string
   196  	Name         string
   197  	ID           uint64
   198  	NewId        uint64
   199  }
   200  
   201  type DropOrTruncateRelationResp struct {
   202  }
   203  
   204  type EntryType int32
   205  
   206  const (
   207  	EntryInsert EntryType = 0
   208  	EntryDelete EntryType = 1
   209  )
   210  
   211  type PKCheckType int32
   212  
   213  const (
   214  	//IncrementalDedup do not check uniqueness of PK before txn's snapshot TS.
   215  	IncrementalDedup PKCheckType = 0
   216  	//FullSkipWorkspaceDedup do not check uniqueness of PK against txn's workspace.
   217  	FullSkipWorkspaceDedup PKCheckType = 1
   218  	FullDedup              PKCheckType = 2
   219  )
   220  
   221  type LocationKey struct{}
   222  
   223  // writeReq responds to entry
   224  type WriteReq struct {
   225  	Type         EntryType
   226  	DatabaseId   uint64
   227  	TableID      uint64
   228  	DatabaseName string
   229  	TableName    string
   230  	Schema       *catalog2.Schema
   231  	Batch        *batch.Batch
   232  	//[IncrementalDedup|FullSkipWorkspaceDedup|FullDedup], default is IncrementalDedup.
   233  	//If incremental-dedup in dn.toml is false, IncrementalDedup will be treated as FullSkipWorkspaceDedup.
   234  	//IncrementalDedup do not check uniqueness of PK before txn's snapshot TS.
   235  	//FullSkipWorkspaceDedup do not check uniqueness of PK against txn's workspace.
   236  	PkCheck PKCheckType
   237  	//S3 object file name
   238  	FileName string
   239  	MetaLocs []string
   240  	//for delete on S3
   241  	DeltaLocs []string
   242  	//tasks for loading primary keys or deleted row ids
   243  	Jobs []*tasks.Job
   244  	//loaded sorted primary keys or deleted row ids.
   245  	JobRes []*tasks.JobResult
   246  	//load context cancel function
   247  	Cancel context.CancelFunc
   248  }
   249  
   250  type WriteResp struct {
   251  }
   252  
   253  type InspectResp struct {
   254  	Typ     int    `json:"-"`
   255  	Message string `json:"msg"`
   256  	Payload []byte `json:"-"`
   257  }
   258  
   259  func (m *InspectResp) MarshalBinary() ([]byte, error) {
   260  	return m.Marshal()
   261  }
   262  
   263  func (m *InspectResp) UnmarshalBinary(data []byte) error {
   264  	return m.Unmarshal(data)
   265  }
   266  
   267  func (m *InspectResp) ConsoleString() string {
   268  	switch m.Typ {
   269  	case InspectNormal:
   270  		return fmt.Sprintf("\nmsg: %s\n\n%v", m.Message, string(m.Payload))
   271  	default:
   272  		return fmt.Sprintf("\nmsg: %s\n\n unhandled resp type %v", m.Message, m.Typ)
   273  	}
   274  }
   275  
   276  const (
   277  	InspectNormal = 0
   278  	InspectCata   = 1
   279  )
   280  
   281  func (m *InspectResp) GetResponse() any {
   282  	switch m.Typ {
   283  	case InspectCata:
   284  		resp := new(CatalogResp)
   285  		types.Decode(m.Payload, resp)
   286  		return resp
   287  	}
   288  	return m
   289  }
   290  
   291  type CatalogResp struct {
   292  	Item string         `json:"Item,omitempty"`
   293  	Sub  []*CatalogResp `json:"Sub,omitempty"`
   294  }
   295  
   296  func (m *CatalogResp) MarshalBinary() ([]byte, error) {
   297  	return m.Marshal()
   298  }
   299  
   300  func (m *CatalogResp) UnmarshalBinary(data []byte) error {
   301  	return m.Unmarshal(data)
   302  }
   303  
   304  type TraceSpan struct {
   305  	Cmd       string
   306  	Spans     string
   307  	Threshold int64
   308  }
   309  
   310  func (t *TraceSpan) MarshalBinary() ([]byte, error) {
   311  	return t.Marshal()
   312  }
   313  
   314  func (t *TraceSpan) UnmarshalBinary(data []byte) error {
   315  	return t.Unmarshal(data)
   316  }
   317  
   318  type StorageUsageReq struct {
   319  	AccIds []int32
   320  }
   321  
   322  func (s *StorageUsageReq) MarshalBinary() ([]byte, error) {
   323  	return s.Marshal()
   324  }
   325  
   326  func (s *StorageUsageReq) UnmarshalBinary(data []byte) error {
   327  	return s.Unmarshal(data)
   328  }
   329  
   330  type BlockMetaInfo struct {
   331  	Info []uint64
   332  }
   333  
   334  func (b *BlockMetaInfo) MarshalBinary() ([]byte, error) {
   335  	return b.Marshal()
   336  }
   337  
   338  func (b *BlockMetaInfo) UnmarshalBinary(data []byte) error {
   339  	return b.Unmarshal(data)
   340  }
   341  
   342  type CkpMetaInfo struct {
   343  	Version  uint32
   344  	Location []byte
   345  }
   346  
   347  func (c *CkpMetaInfo) MarshalBinary() ([]byte, error) {
   348  	return c.Marshal()
   349  }
   350  
   351  func (c *CkpMetaInfo) UnmarshalBinary(data []byte) error {
   352  	return c.Unmarshal(data)
   353  }
   354  
   355  type StorageUsageResp_V0 struct {
   356  	Succeed      bool
   357  	CkpEntries   []*CkpMetaInfo
   358  	BlockEntries []*BlockMetaInfo
   359  }
   360  
   361  type StorageUsageResp struct {
   362  	Succeed bool
   363  	AccIds  []int32
   364  	Sizes   []uint64
   365  	Magic   uint64
   366  }
   367  
   368  func (s *StorageUsageResp) MarshalBinary() ([]byte, error) {
   369  	return s.Marshal()
   370  }
   371  
   372  func (s *StorageUsageResp) UnmarshalBinary(data []byte) error {
   373  	return s.Unmarshal(data)
   374  }