vitess.io/vitess@v0.16.2/go/vt/vttablet/tabletserver/schema/schema.go (about)

     1  /*
     2  Copyright 2019 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package schema
    18  
    19  import (
    20  	"sync"
    21  	"time"
    22  
    23  	"vitess.io/vitess/go/vt/sqlparser"
    24  
    25  	querypb "vitess.io/vitess/go/vt/proto/query"
    26  )
    27  
    28  // Table types
    29  const (
    30  	NoType = iota
    31  	Sequence
    32  	Message
    33  )
    34  
    35  // TypeNames allows to fetch a the type name for a table.
    36  // Count must match the number of table types.
    37  var TypeNames = []string{
    38  	"none",
    39  	"sequence",
    40  	"message",
    41  }
    42  
    43  // Table contains info about a table.
    44  type Table struct {
    45  	Name      sqlparser.IdentifierCS
    46  	Fields    []*querypb.Field
    47  	PKColumns []int
    48  	Type      int
    49  
    50  	// SequenceInfo contains info for sequence tables.
    51  	SequenceInfo *SequenceInfo
    52  
    53  	// MessageInfo contains info for message tables.
    54  	MessageInfo *MessageInfo
    55  
    56  	CreateTime    int64
    57  	FileSize      uint64
    58  	AllocatedSize uint64
    59  }
    60  
    61  // SequenceInfo contains info specific to sequence tabels.
    62  // It must be locked before accessing the values inside.
    63  // If CurVal==LastVal, we have to cache new values.
    64  // When the schema is first loaded, the values are all 0,
    65  // which will trigger caching on first use.
    66  type SequenceInfo struct {
    67  	sync.Mutex
    68  	NextVal int64
    69  	LastVal int64
    70  }
    71  
    72  // MessageInfo contains info specific to message tables.
    73  type MessageInfo struct {
    74  	// Fields stores the field info to be
    75  	// returned for subscribers.
    76  	Fields []*querypb.Field
    77  
    78  	// AckWaitDuration specifies how long to wait after
    79  	// the message was first sent. The back-off doubles
    80  	// every attempt.
    81  	AckWaitDuration time.Duration
    82  
    83  	// PurgeAfterDuration specifies the time after which
    84  	// a successfully acked message can be deleted.
    85  	PurgeAfterDuration time.Duration
    86  
    87  	// BatchSize specifies the max number of events to
    88  	// send per response.
    89  	BatchSize int
    90  
    91  	// CacheSize specifies the number of messages to keep
    92  	// in cache. Anything that cannot fit in the cache
    93  	// is sent as best effort.
    94  	CacheSize int
    95  
    96  	// PollInterval specifies the polling frequency to
    97  	// look for messages to be sent.
    98  	PollInterval time.Duration
    99  
   100  	// MinBackoff specifies the shortest duration message manager
   101  	// should wait before rescheduling a message
   102  	MinBackoff time.Duration
   103  
   104  	// MaxBackoff specifies the longest duration message manager
   105  	// should wait before rescheduling a message
   106  	MaxBackoff time.Duration
   107  }
   108  
   109  // NewTable creates a new Table.
   110  func NewTable(name string) *Table {
   111  	return &Table{
   112  		Name: sqlparser.NewIdentifierCS(name),
   113  	}
   114  }
   115  
   116  // FindColumn finds a column in the table. It returns the index if found.
   117  // Otherwise, it returns -1.
   118  func (ta *Table) FindColumn(name sqlparser.IdentifierCI) int {
   119  	for i, col := range ta.Fields {
   120  		if name.EqualString(col.Name) {
   121  			return i
   122  		}
   123  	}
   124  	return -1
   125  }
   126  
   127  // GetPKColumn returns the pk column specified by the index.
   128  func (ta *Table) GetPKColumn(index int) *querypb.Field {
   129  	return ta.Fields[ta.PKColumns[index]]
   130  }
   131  
   132  // HasPrimary returns true if the table has a primary key.
   133  func (ta *Table) HasPrimary() bool {
   134  	return len(ta.PKColumns) != 0
   135  }