github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/engine/pkg/meta/model/interfaces.go (about) 1 // Copyright 2022 PingCAP, Inc. 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 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package model 15 16 import ( 17 "context" 18 ) 19 20 // Txn doesn't support nested txn 21 type Txn interface { 22 // Do cache Ops in the Txn 23 // Same op limit with KV Put/Get/Delete interface 24 // Using snapshot isolation 25 Do(ops ...Op) Txn 26 27 // Commit tries to commit the transaction. 28 // Any Op fail will cause entire txn rollback and return error 29 Commit() (*TxnResponse, Error) 30 } 31 32 // KV defines a key value access interface, which is quite similar to etcd KV API 33 type KV interface { 34 // Put puts a key-value pair into metastore. 35 // Note that key,value can be plain bytes array and string is 36 // an immutable representation of that bytes array. 37 // To get a string of bytes, do string([]byte{0x10, 0x20}). 38 // or do nothing on vice verse. 39 // Length of key is restricted to 2KB 40 Put(ctx context.Context, key, val string) (*PutResponse, Error) 41 42 // Get retrieves keys with newest revision. 43 // By default, Get will return the value for "key", if any. 44 // When WithRange(end) is passed, Get will return the keys in the range [key, end). 45 // When WithFromKey() is passed, Get returns keys greater than or equal to key. 46 // When WithPrefix() is passed, Get returns keys with prefix. 47 // WARN: WithRange(), WithFromKey(), WithPrefix() can't be used at the same time 48 Get(ctx context.Context, key string, opts ...OpOption) (*GetResponse, Error) 49 50 // Delete deletes a key, or optionally using WithRange(end), [key, end). 51 // WARN: WithRange(end), WithFromKey(), WithPrefix() can't be used at the same time 52 Delete(ctx context.Context, key string, opts ...OpOption) (*DeleteResponse, Error) 53 54 // Txn creates a transaction. 55 Txn(ctx context.Context) Txn 56 } 57 58 // Error defines the interface used in KV interface 59 type Error interface { 60 error 61 // IsRetryable returns true if this error may be gone if retried. 62 IsRetryable() bool 63 } 64 65 // Client defines some basic method used as a meta client 66 type Client interface { 67 // Close is the method to close the client and release inner resources 68 Close() error 69 70 // GenEpoch generate the increasing epoch for user 71 GenEpoch(ctx context.Context) (int64, error) 72 } 73 74 // KVClient combines Client interface and KV interface 75 type KVClient interface { 76 Client 77 KV 78 } 79 80 // ClientConn is the common method for different connection 81 // HOPE to reuse the common underlying connection pool 82 type ClientConn interface { 83 // StoreType returns the type of connection 84 StoreType() StoreType 85 86 // GetConn gets the underlying connection object 87 // For the fisrt return param if no error happens: 88 // For StoreTypeEtcd, it returns *clientv3.Client 89 // For StoreTypeMySQL/StoreTypeSQLite, it returns *sql.DB 90 GetConn() (interface{}, error) 91 92 // Close closes the underlying connection and releases some resources 93 Close() error 94 }