本文整理汇总了Golang中github.com/cockroachdb/cockroach/internal/client.NewTxn函数的典型用法代码示例。如果您正苦于以下问题:Golang NewTxn函数的具体用法?Golang NewTxn怎么用?Golang NewTxn使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewTxn函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: TestTxnCoordSenderCleanupOnAborted
// TestTxnCoordSenderCleanupOnAborted verifies that if a txn receives a
// TransactionAbortedError, the coordinator cleans up the transaction.
func TestTxnCoordSenderCleanupOnAborted(t *testing.T) {
defer leaktest.AfterTest(t)()
s, sender := createTestDB(t)
defer s.Stop()
// Create a transaction with intent at "a".
key := roachpb.Key("a")
txn1 := client.NewTxn(context.Background(), *s.DB)
txn1.InternalSetPriority(1)
if err := txn1.Put(key, []byte("value")); err != nil {
t.Fatal(err)
}
// Push the transaction (by writing key "a" with higher priority) to abort it.
txn2 := client.NewTxn(context.Background(), *s.DB)
txn2.InternalSetPriority(2)
if err := txn2.Put(key, []byte("value2")); err != nil {
t.Fatal(err)
}
// Now end the transaction and verify we've cleanup up, even though
// end transaction failed.
err := txn1.CommitOrCleanup()
assertTransactionAbortedError(t, err)
if err := txn2.CommitOrCleanup(); err != nil {
t.Fatal(err)
}
verifyCleanup(key, sender, s.Eng, t)
}
开发者ID:CubeLite,项目名称:cockroach,代码行数:31,代码来源:txn_coord_sender_test.go
示例2: TestTxnCoordSenderBeginTransaction
// TestTxnCoordSenderBeginTransaction verifies that a command sent with a
// not-nil Txn with empty ID gets a new transaction initialized.
func TestTxnCoordSenderBeginTransaction(t *testing.T) {
defer leaktest.AfterTest(t)()
s, sender := createTestDB(t)
defer s.Stop()
defer teardownHeartbeats(sender)
txn := client.NewTxn(context.Background(), *s.DB)
// Put request will create a new transaction.
key := roachpb.Key("key")
txn.InternalSetPriority(10)
txn.Proto.Isolation = enginepb.SNAPSHOT
txn.Proto.Name = "test txn"
if err := txn.Put(key, []byte("value")); err != nil {
t.Fatal(err)
}
if txn.Proto.Name != "test txn" {
t.Errorf("expected txn name to be %q; got %q", "test txn", txn.Proto.Name)
}
if txn.Proto.Priority != 10 {
t.Errorf("expected txn priority 10; got %d", txn.Proto.Priority)
}
if !bytes.Equal(txn.Proto.Key, key) {
t.Errorf("expected txn Key to match %q != %q", key, txn.Proto.Key)
}
if txn.Proto.Isolation != enginepb.SNAPSHOT {
t.Errorf("expected txn isolation to be SNAPSHOT; got %s", txn.Proto.Isolation)
}
}
开发者ID:CubeLite,项目名称:cockroach,代码行数:31,代码来源:txn_coord_sender_test.go
示例3: TestTxnCoordSenderAddIntentOnError
// TestTxnCoordSenderAddIntentOnError verifies that intents are tracked if
// the transaction is, even on error.
func TestTxnCoordSenderAddIntentOnError(t *testing.T) {
defer leaktest.AfterTest(t)()
s, sender := createTestDB(t)
defer s.Stop()
// Create a transaction with intent at "a".
key := roachpb.Key("x")
txn := client.NewTxn(context.Background(), *s.DB)
// Write so that the coordinator begins tracking this txn.
if err := txn.Put("x", "y"); err != nil {
t.Fatal(err)
}
err, ok := txn.CPut(key, []byte("x"), []byte("born to fail")).(*roachpb.ConditionFailedError)
if !ok {
t.Fatal(err)
}
sender.Lock()
txnID := *txn.Proto.ID
roachpb.MergeSpans(&sender.txns[txnID].keys)
intentSpans := sender.txns[txnID].keys
expSpans := []roachpb.Span{{Key: key, EndKey: []byte("")}}
equal := !reflect.DeepEqual(intentSpans, expSpans)
sender.Unlock()
if err := txn.Rollback(); err != nil {
t.Fatal(err)
}
if !equal {
t.Fatalf("expected stored intents %v, got %v", expSpans, intentSpans)
}
}
开发者ID:CubeLite,项目名称:cockroach,代码行数:32,代码来源:txn_coord_sender_test.go
示例4: TestTxnCoordIdempotentCleanup
// TestTxnCoordIdempotentCleanup verifies that cleanupTxnLocked is idempotent.
func TestTxnCoordIdempotentCleanup(t *testing.T) {
defer leaktest.AfterTest(t)()
s, sender := createTestDB(t)
defer s.Stop()
defer teardownHeartbeats(sender)
txn := client.NewTxn(context.Background(), *s.DB)
ba := txn.NewBatch()
ba.Put(roachpb.Key("a"), []byte("value"))
if err := txn.Run(ba); err != nil {
t.Fatal(err)
}
sender.Lock()
// Clean up twice successively.
sender.cleanupTxnLocked(context.Background(), txn.Proto)
sender.cleanupTxnLocked(context.Background(), txn.Proto)
sender.Unlock()
// For good measure, try to commit (which cleans up once more if it
// succeeds, which it may not if the previous cleanup has already
// terminated the heartbeat goroutine)
ba = txn.NewBatch()
ba.AddRawRequest(&roachpb.EndTransactionRequest{})
err := txn.Run(ba)
if err != nil && !testutils.IsError(err, errNoState.Error()) {
t.Fatal(err)
}
}
开发者ID:CubeLite,项目名称:cockroach,代码行数:30,代码来源:txn_coord_sender_test.go
示例5: TestTxnInitialTimestamp
// TestTxnInitialTimestamp verifies that the timestamp requested
// before the Txn is created is honored.
func TestTxnInitialTimestamp(t *testing.T) {
defer leaktest.AfterTest(t)()
s, sender := createTestDB(t)
defer s.Stop()
defer teardownHeartbeats(sender)
txn := client.NewTxn(context.Background(), *s.DB)
// Request a specific timestamp.
refTimestamp := hlc.Timestamp{WallTime: 42, Logical: 69}
txn.Proto.OrigTimestamp = refTimestamp
// Put request will create a new transaction.
key := roachpb.Key("key")
txn.InternalSetPriority(10)
txn.Proto.Isolation = enginepb.SNAPSHOT
txn.Proto.Name = "test txn"
if err := txn.Put(key, []byte("value")); err != nil {
t.Fatal(err)
}
if txn.Proto.OrigTimestamp != refTimestamp {
t.Errorf("expected txn orig ts to be %s; got %s", refTimestamp, txn.Proto.OrigTimestamp)
}
if txn.Proto.Timestamp != refTimestamp {
t.Errorf("expected txn ts to be %s; got %s", refTimestamp, txn.Proto.Timestamp)
}
}
开发者ID:CubeLite,项目名称:cockroach,代码行数:29,代码来源:txn_coord_sender_test.go
示例6: TestTxnCoordSenderGCTimeout
// TestTxnCoordSenderGCTimeout verifies that the coordinator cleans up extant
// transactions and intents after the lastUpdateNanos exceeds the timeout.
func TestTxnCoordSenderGCTimeout(t *testing.T) {
defer leaktest.AfterTest(t)()
s, sender := createTestDB(t)
defer s.Stop()
// Set heartbeat interval to 1ms for testing.
sender.heartbeatInterval = 1 * time.Millisecond
txn := client.NewTxn(context.Background(), *s.DB)
key := roachpb.Key("a")
if err := txn.Put(key, []byte("value")); err != nil {
t.Fatal(err)
}
// Now, advance clock past the default client timeout.
// Locking the TxnCoordSender to prevent a data race.
sender.Lock()
s.Manual.Set(defaultClientTimeout.Nanoseconds() + 1)
sender.Unlock()
txnID := *txn.Proto.ID
util.SucceedsSoon(t, func() error {
// Locking the TxnCoordSender to prevent a data race.
sender.Lock()
_, ok := sender.txns[txnID]
sender.Unlock()
if ok {
return errors.Errorf("expected garbage collection")
}
return nil
})
verifyCleanup(key, sender, s.Eng, t)
}
开发者ID:CubeLite,项目名称:cockroach,代码行数:37,代码来源:txn_coord_sender_test.go
示例7: reset
// reset creates a new Txn and initializes it using the session defaults.
func (ts *txnState) reset(ctx context.Context, e *Executor, s *Session) {
*ts = txnState{}
ts.txn = client.NewTxn(ctx, *e.ctx.DB)
ts.txn.Proto.Isolation = s.DefaultIsolationLevel
ts.tr = s.Trace
// Discard the old schemaChangers, if any.
ts.schemaChangers = schemaChangerCollection{}
}
开发者ID:CubeLite,项目名称:cockroach,代码行数:9,代码来源:session.go
示例8: setupTxn
func (ds *ServerImpl) setupTxn(
ctx context.Context,
txnProto *roachpb.Transaction,
) *client.Txn {
txn := client.NewTxn(ctx, *ds.ctx.DB)
// TODO(radu): we should sanity check some of these fields
txn.Proto = *txnProto
return txn
}
开发者ID:CubeLite,项目名称:cockroach,代码行数:9,代码来源:server.go
示例9: Prepare
// Prepare returns the result types of the given statement. pinfo may
// contain partial type information for placeholders. Prepare will
// populate the missing types. The column result types are returned (or
// nil if there are no results).
func (e *Executor) Prepare(
query string,
session *Session,
pinfo parser.PlaceholderTypes,
) ([]ResultColumn, error) {
if log.V(2) {
log.Infof(session.Ctx(), "preparing: %s", query)
} else if traceSQL {
log.Tracef(session.Ctx(), "preparing: %s", query)
}
stmt, err := parser.ParseOne(query, parser.Syntax(session.Syntax))
if err != nil {
return nil, err
}
if err = pinfo.ProcessPlaceholderAnnotations(stmt); err != nil {
return nil, err
}
protoTS, err := isAsOf(&session.planner, stmt, e.ctx.Clock.Now())
if err != nil {
return nil, err
}
session.planner.resetForBatch(e)
session.planner.semaCtx.Placeholders.SetTypes(pinfo)
session.planner.evalCtx.PrepareOnly = true
// Prepare needs a transaction because it needs to retrieve db/table
// descriptors for type checking.
txn := client.NewTxn(session.Ctx(), *e.ctx.DB)
txn.Proto.Isolation = session.DefaultIsolationLevel
session.planner.setTxn(txn)
defer session.planner.setTxn(nil)
if protoTS != nil {
session.planner.asOf = true
defer func() {
session.planner.asOf = false
}()
setTxnTimestamps(txn, *protoTS)
}
plan, err := session.planner.prepare(stmt)
if err != nil {
return nil, err
}
if plan == nil {
return nil, nil
}
cols := plan.Columns()
for _, c := range cols {
if err := checkResultDatum(c.Typ); err != nil {
return nil, err
}
}
return cols, nil
}
开发者ID:yangxuanjia,项目名称:cockroach,代码行数:61,代码来源:executor.go
示例10: TestTxnCoordSenderMultipleTxns
// TestTxnCoordSenderMultipleTxns verifies correct operation with
// multiple outstanding transactions.
func TestTxnCoordSenderMultipleTxns(t *testing.T) {
defer leaktest.AfterTest(t)()
s, sender := createTestDB(t)
defer s.Stop()
defer teardownHeartbeats(sender)
txn1 := client.NewTxn(context.Background(), *s.DB)
txn2 := client.NewTxn(context.Background(), *s.DB)
if err := txn1.Put(roachpb.Key("a"), []byte("value")); err != nil {
t.Fatal(err)
}
if err := txn2.Put(roachpb.Key("b"), []byte("value")); err != nil {
t.Fatal(err)
}
if len(sender.txns) != 2 {
t.Errorf("expected length of transactions map to be 2; got %d", len(sender.txns))
}
}
开发者ID:CubeLite,项目名称:cockroach,代码行数:22,代码来源:txn_coord_sender_test.go
示例11: Restore
// Restore imports a SQL table (or all user SQL tables) from a set of
// non-overlapping sstable files.
func Restore(ctx context.Context, db client.DB, base string, table string, overwrite bool) error {
sst, err := engine.MakeRocksDBSstFileReader()
if err != nil {
return err
}
defer sst.Close()
descBytes, err := ioutil.ReadFile(filepath.Join(base, backupDescriptorName))
if err != nil {
return err
}
var backupDesc sqlbase.BackupDescriptor
if err := backupDesc.Unmarshal(descBytes); err != nil {
return err
}
for _, r := range backupDesc.Ranges {
if len(r.Path) == 0 {
continue
}
if err := sst.AddFile(r.Path); err != nil {
return err
}
}
// TODO(dan): This uses one giant transaction for the entire restore, which
// works for small datasets, but not for big ones.
txn := client.NewTxn(ctx, db)
if len(table) > 0 {
found := false
for _, desc := range backupDesc.SQL {
if t := desc.GetTable(); t != nil && t.Name == table {
if err := restoreTable(ctx, sst, txn, t, overwrite); err != nil {
return err
}
found = true
break
}
}
if !found {
return errors.Errorf("table not found: %s", table)
}
} else {
for _, desc := range backupDesc.SQL {
if t := desc.GetTable(); t != nil && t.ParentID != keys.SystemDatabaseID {
if err := restoreTable(ctx, sst, txn, t, overwrite); err != nil {
return err
}
}
}
}
return txn.CommitOrCleanup()
}
开发者ID:yaojingguo,项目名称:cockroach,代码行数:54,代码来源:backup.go
示例12: TestTxnCoordSenderKeyRanges
// TestTxnCoordSenderKeyRanges verifies that multiple requests to same or
// overlapping key ranges causes the coordinator to keep track only of
// the minimum number of ranges.
func TestTxnCoordSenderKeyRanges(t *testing.T) {
defer leaktest.AfterTest(t)()
ranges := []struct {
start, end roachpb.Key
}{
{roachpb.Key("a"), roachpb.Key(nil)},
{roachpb.Key("a"), roachpb.Key(nil)},
{roachpb.Key("aa"), roachpb.Key(nil)},
{roachpb.Key("b"), roachpb.Key(nil)},
{roachpb.Key("aa"), roachpb.Key("c")},
{roachpb.Key("b"), roachpb.Key("c")},
}
s, sender := createTestDB(t)
defer s.Stop()
defer teardownHeartbeats(sender)
txn := client.NewTxn(context.Background(), *s.DB)
for _, rng := range ranges {
if rng.end != nil {
if err := txn.DelRange(rng.start, rng.end); err != nil {
t.Fatal(err)
}
} else {
if err := txn.Put(rng.start, []byte("value")); err != nil {
t.Fatal(err)
}
}
}
txnID := *txn.Proto.ID
// Verify that the transaction metadata contains only two entries
// in its "keys" range group. "a" and range "aa"-"c".
txnMeta, ok := sender.txns[txnID]
if !ok {
t.Fatalf("expected a transaction to be created on coordinator")
}
roachpb.MergeSpans(&txnMeta.keys)
keys := txnMeta.keys
if len(keys) != 2 {
t.Errorf("expected 2 entries in keys range group; got %v", keys)
}
}
开发者ID:CubeLite,项目名称:cockroach,代码行数:47,代码来源:txn_coord_sender_test.go
示例13: TestTxnCoordSenderReleaseTxnMeta
// TestTxnCoordSenderReleaseTxnMeta verifies that TxnCoordSender releases the
// txnMetadata after the txn has committed successfully.
func TestTxnCoordSenderReleaseTxnMeta(t *testing.T) {
defer leaktest.AfterTest(t)()
s, sender := createTestDB(t)
defer s.Stop()
defer teardownHeartbeats(sender)
txn := client.NewTxn(context.Background(), *s.DB)
ba := txn.NewBatch()
ba.Put(roachpb.Key("a"), []byte("value"))
ba.Put(roachpb.Key("b"), []byte("value"))
if err := txn.CommitInBatch(ba); err != nil {
t.Fatal(err)
}
txnID := *txn.Proto.ID
if _, ok := sender.txns[txnID]; ok {
t.Fatal("expected TxnCoordSender has released the txn")
}
}
开发者ID:CubeLite,项目名称:cockroach,代码行数:22,代码来源:txn_coord_sender_test.go
示例14: TestTxnCoordSenderBeginTransactionMinPriority
// TestTxnCoordSenderBeginTransactionMinPriority verifies that when starting
// a new transaction, a non-zero priority is treated as a minimum value.
func TestTxnCoordSenderBeginTransactionMinPriority(t *testing.T) {
defer leaktest.AfterTest(t)()
s, sender := createTestDB(t)
defer s.Stop()
defer teardownHeartbeats(sender)
txn := client.NewTxn(context.Background(), *s.DB)
// Put request will create a new transaction.
key := roachpb.Key("key")
txn.InternalSetPriority(10)
txn.Proto.Isolation = enginepb.SNAPSHOT
txn.Proto.Priority = 11
if err := txn.Put(key, []byte("value")); err != nil {
t.Fatal(err)
}
if prio := txn.Proto.Priority; prio != 11 {
t.Errorf("expected txn priority 11; got %d", prio)
}
}
开发者ID:CubeLite,项目名称:cockroach,代码行数:22,代码来源:txn_coord_sender_test.go
示例15: reset
// reset creates a new Txn and initializes it using the session defaults.
func (ts *txnState) reset(ctx context.Context, e *Executor, s *Session) {
*ts = txnState{}
ts.txn = client.NewTxn(ctx, *e.ctx.DB)
ts.txn.Context = s.context
ts.txn.Proto.Isolation = s.DefaultIsolationLevel
ts.tr = s.Trace
// Discard the old schemaChangers, if any.
ts.schemaChangers = schemaChangerCollection{}
if traceSQL {
sp, err := tracing.JoinOrNewSnowball("coordinator", nil, func(sp basictracer.RawSpan) {
ts.txn.CollectedSpans = append(ts.txn.CollectedSpans, sp)
})
if err != nil {
log.Warningf(ctx, "unable to create snowball tracer: %s", err)
return
}
ts.txn.Context = opentracing.ContextWithSpan(ts.txn.Context, sp)
ts.sp = sp
}
}
开发者ID:yangxuanjia,项目名称:cockroach,代码行数:22,代码来源:session.go
示例16: TestReadOnlyTxnObeysDeadline
// TestReadOnlyTxnObeysDeadline tests that read-only transactions obey the
// deadline.
func TestReadOnlyTxnObeysDeadline(t *testing.T) {
defer leaktest.AfterTest(t)()
s, _, _ := serverutils.StartServer(t, base.TestServerArgs{})
defer s.Stopper().Stop()
db := createTestClient(t, s.Stopper(), s.ServingAddr())
if err := db.Put("k", "v"); err != nil {
t.Fatal(err)
}
// Use txn.Exec instead of db.Txn to disable auto retry.
txn := client.NewTxn(context.TODO(), *db)
if err := txn.Exec(client.TxnExecOptions{AutoRetry: false, AutoCommit: true}, func(txn *client.Txn, _ *client.TxnExecOptions) error {
// Set deadline to sometime in the past.
txn.UpdateDeadlineMaybe(hlc.Timestamp{WallTime: timeutil.Now().Add(-time.Second).UnixNano()})
_, err := txn.Get("k")
return err
}); !testutils.IsError(err, "txn aborted") {
t.Fatal(err)
}
}
开发者ID:yangxuanjia,项目名称:cockroach,代码行数:23,代码来源:client_test.go
示例17: TestTxnCoordSenderCancel
func TestTxnCoordSenderCancel(t *testing.T) {
defer leaktest.AfterTest(t)()
s, sender := createTestDB(t)
defer s.Stop()
ctx, cancel := context.WithCancel(context.Background())
origSender := sender.wrapped
sender.wrapped = client.SenderFunc(
func(ctx context.Context, args roachpb.BatchRequest) (*roachpb.BatchResponse, *roachpb.Error) {
if _, hasET := args.GetArg(roachpb.EndTransaction); hasET {
// Cancel the transaction while also sending it along. This tickled a
// data race in TxnCoordSender.tryAsyncAbort. See #7726.
cancel()
}
return origSender.Send(ctx, args)
})
// Create a transaction with bunch of intents.
txn := client.NewTxn(ctx, *s.DB)
batch := txn.NewBatch()
for i := 0; i < 100; i++ {
key := roachpb.Key(fmt.Sprintf("%d", i))
batch.Put(key, []byte("value"))
}
if err := txn.Run(batch); err != nil {
t.Fatal(err)
}
// Commit the transaction. Note that we cancel the transaction when the
// commit is sent which stresses the TxnCoordSender.tryAsyncAbort code
// path. We'll either succeed, get a "does not exist" error, or get a
// context canceled error. Anything else is unexpected.
err := txn.CommitOrCleanup()
if err != nil && err.Error() != context.Canceled.Error() &&
!testutils.IsError(err, "does not exist") {
t.Fatal(err)
}
}
开发者ID:yangxuanjia,项目名称:cockroach,代码行数:39,代码来源:txn_coord_sender_test.go
示例18: TestTxnCoordSenderAddRequest
// TestTxnCoordSenderAddRequest verifies adding a request creates a
// transaction metadata and adding multiple requests with same
// transaction ID updates the last update timestamp.
func TestTxnCoordSenderAddRequest(t *testing.T) {
defer leaktest.AfterTest(t)()
s, sender := createTestDB(t)
defer s.Stop()
defer teardownHeartbeats(sender)
txn := client.NewTxn(context.Background(), *s.DB)
// Put request will create a new transaction.
if err := txn.Put(roachpb.Key("a"), []byte("value")); err != nil {
t.Fatal(err)
}
txnID := *txn.Proto.ID
txnMeta, ok := sender.txns[txnID]
if !ok {
t.Fatal("expected a transaction to be created on coordinator")
}
if !txn.Proto.Writing {
t.Fatal("txn is not marked as writing")
}
ts := atomic.LoadInt64(&txnMeta.lastUpdateNanos)
// Advance time and send another put request. Lock the coordinator
// to prevent a data race.
sender.Lock()
s.Manual.Set(1)
sender.Unlock()
if err := txn.Put(roachpb.Key("a"), []byte("value")); err != nil {
t.Fatal(err)
}
if len(sender.txns) != 1 {
t.Errorf("expected length of transactions map to be 1; got %d", len(sender.txns))
}
txnMeta = sender.txns[txnID]
if lu := atomic.LoadInt64(&txnMeta.lastUpdateNanos); ts >= lu || lu != s.Manual.UnixNano() {
t.Errorf("expected last update time to advance; got %d", lu)
}
}
开发者ID:CubeLite,项目名称:cockroach,代码行数:41,代码来源:txn_coord_sender_test.go
示例19: TestTxnRestartCount
func TestTxnRestartCount(t *testing.T) {
defer leaktest.AfterTest(t)()
_, sender, cleanupFn := setupMetricsTest(t)
defer cleanupFn()
key := []byte("key-restart")
value := []byte("value")
db := client.NewDB(sender)
// Start a transaction and do a GET. This forces a timestamp to be chosen for the transaction.
txn := client.NewTxn(context.Background(), *db)
if _, err := txn.Get(key); err != nil {
t.Fatal(err)
}
// Outside of the transaction, read the same key as was read within the transaction. This
// means that future attempts to write will increase the timestamp.
if _, err := db.Get(key); err != nil {
t.Fatal(err)
}
// This put will lay down an intent, txn timestamp will increase beyond original.
if err := txn.Put(key, value); err != nil {
t.Fatal(err)
}
if !txn.Proto.OrigTimestamp.Less(txn.Proto.Timestamp) {
t.Errorf("expected timestamp to increase: %s", txn.Proto)
}
// Commit (should cause restart metric to increase).
err := txn.CommitOrCleanup()
assertTransactionRetryError(t, err)
teardownHeartbeats(sender)
checkTxnMetrics(t, sender, "restart txn", 0, 0, 0, 1, 1)
}
开发者ID:CubeLite,项目名称:cockroach,代码行数:36,代码来源:txn_coord_sender_test.go
示例20: TestValidateCrossTableReferences
//.........这里部分代码省略.........
}},
},
},
referenced: []TableDescriptor{{
ID: 52,
Name: "baz",
}},
},
{
err: `missing interleave back reference to foo.bar from baz.qux`,
desc: TableDescriptor{
ID: 51,
Name: "foo",
PrimaryIndex: IndexDescriptor{
ID: 1,
Name: "bar",
Interleave: InterleaveDescriptor{Ancestors: []InterleaveDescriptor_Ancestor{
{TableID: 52, IndexID: 2},
}},
},
},
referenced: []TableDescriptor{{
ID: 52,
Name: "baz",
PrimaryIndex: IndexDescriptor{
ID: 2,
Name: "qux",
},
}},
},
{
err: `invalid interleave backreference table=52 index=2: descriptor not found`,
desc: TableDescriptor{
ID: 51,
PrimaryIndex: IndexDescriptor{
ID: 1,
InterleavedBy: []ForeignKeyReference{{Table: 52, Index: 2}},
},
},
},
{
err: `invalid interleave backreference table=baz index=2: index-id "2" does not exist`,
desc: TableDescriptor{
ID: 51,
PrimaryIndex: IndexDescriptor{
ID: 1,
InterleavedBy: []ForeignKeyReference{{Table: 52, Index: 2}},
},
},
referenced: []TableDescriptor{{
ID: 52,
Name: "baz",
}},
},
{
err: `broken interleave backward reference from foo.bar to baz.qux`,
desc: TableDescriptor{
ID: 51,
Name: "foo",
PrimaryIndex: IndexDescriptor{
ID: 1,
Name: "bar",
InterleavedBy: []ForeignKeyReference{{Table: 52, Index: 2}},
},
},
referenced: []TableDescriptor{{
ID: 52,
Name: "baz",
PrimaryIndex: IndexDescriptor{
ID: 2,
Name: "qux",
},
}},
},
}
for i, test := range tests {
for _, referencedDesc := range test.referenced {
var v roachpb.Value
desc := &Descriptor{Union: &Descriptor_Table{Table: &referencedDesc}}
if err := v.SetProto(desc); err != nil {
t.Fatal(err)
}
if err := kvDB.Put(MakeDescMetadataKey(referencedDesc.ID), &v); err != nil {
t.Fatal(err)
}
}
txn := client.NewTxn(context.Background(), *kvDB)
if err := test.desc.validateCrossReferences(txn); err == nil {
t.Errorf("%d: expected \"%s\", but found success: %+v", i, test.err, test.desc)
} else if test.err != err.Error() {
t.Errorf("%d: expected \"%s\", but found \"%s\"", i, test.err, err.Error())
}
for _, referencedDesc := range test.referenced {
if err := kvDB.Del(MakeDescMetadataKey(referencedDesc.ID)); err != nil {
t.Fatal(err)
}
}
}
}
开发者ID:yangxuanjia,项目名称:cockroach,代码行数:101,代码来源:structured_test.go
注:本文中的github.com/cockroachdb/cockroach/internal/client.NewTxn函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论