本文整理汇总了Golang中github.com/cockroachdb/cockroach/internal/client.Batch类的典型用法代码示例。如果您正苦于以下问题:Golang Batch类的具体用法?Golang Batch怎么用?Golang Batch使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Batch类的17个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: deleteRow
// deleteRow adds to the batch the kv operations necessary to delete a table row
// with the given values.
func (rd *rowDeleter) deleteRow(b *client.Batch, values []parser.Datum) error {
if err := rd.fks.checkAll(values); err != nil {
return err
}
primaryIndexKey, secondaryIndexEntries, err := rd.helper.encodeIndexes(rd.fetchColIDtoRowIndex, values)
if err != nil {
return err
}
for _, secondaryIndexEntry := range secondaryIndexEntries {
if log.V(2) {
log.Infof("Del %s", secondaryIndexEntry.Key)
}
b.Del(secondaryIndexEntry.Key)
}
// Delete the row.
rd.startKey = roachpb.Key(primaryIndexKey)
rd.endKey = roachpb.Key(encoding.EncodeNotNullDescending(primaryIndexKey))
if log.V(2) {
log.Infof("DelRange %s - %s", rd.startKey, rd.endKey)
}
b.DelRange(&rd.startKey, &rd.endKey, false)
rd.startKey, rd.endKey = nil, nil
return nil
}
开发者ID:YuleiXiao,项目名称:cockroach,代码行数:30,代码来源:rowwriter.go
示例2: StoreData
// StoreData writes the supplied time series data to the cockroach server.
// Stored data will be sampled at the supplied resolution.
func (db *DB) StoreData(r Resolution, data []tspb.TimeSeriesData) error {
var kvs []roachpb.KeyValue
// Process data collection: data is converted to internal format, and a key
// is generated for each internal message.
for _, d := range data {
idatas, err := d.ToInternal(r.KeyDuration(), r.SampleDuration())
if err != nil {
return err
}
for _, idata := range idatas {
var value roachpb.Value
if err := value.SetProto(&idata); err != nil {
return err
}
kvs = append(kvs, roachpb.KeyValue{
Key: MakeDataKey(d.Name, d.Source, r, idata.StartTimestampNanos),
Value: value,
})
}
}
// Send the individual internal merge requests.
b := client.Batch{}
for _, kv := range kvs {
b.AddRawRequest(&roachpb.MergeRequest{
Span: roachpb.Span{
Key: kv.Key,
},
Value: kv.Value,
})
}
return db.db.Run(&b)
}
开发者ID:CubeLite,项目名称:cockroach,代码行数:37,代码来源:db.go
示例3: insertCPutFn
// insertCPutFn is used by insertRow when conflicts should be respected.
// logValue is used for pretty printing.
func insertCPutFn(b *client.Batch, key *roachpb.Key, value *roachpb.Value) {
// TODO(dan): We want do this V(2) log everywhere in sql. Consider making a
// client.Batch wrapper instead of inlining it everywhere.
if log.V(2) {
log.InfofDepth(1, "CPut %s -> %s", *key, value.PrettyPrint())
}
b.CPut(key, value, nil)
}
开发者ID:YuleiXiao,项目名称:cockroach,代码行数:10,代码来源:rowwriter.go
示例4: createDescriptor
// createDescriptor implements the DescriptorAccessor interface.
func (p *planner) createDescriptor(plainKey sqlbase.DescriptorKey, descriptor sqlbase.DescriptorProto, ifNotExists bool) (bool, error) {
idKey := plainKey.Key()
// Check whether idKey exists.
gr, err := p.txn.Get(idKey)
if err != nil {
return false, err
}
if gr.Exists() {
if ifNotExists {
// Noop.
return false, nil
}
// Key exists, but we don't want it to: error out.
return false, fmt.Errorf("%s %q already exists", descriptor.TypeName(), plainKey.Name())
}
// Increment unique descriptor counter.
if ir, err := p.txn.Inc(keys.DescIDGenerator, 1); err == nil {
descriptor.SetID(sqlbase.ID(ir.ValueInt() - 1))
} else {
return false, err
}
// TODO(pmattis): The error currently returned below is likely going to be
// difficult to interpret.
//
// TODO(pmattis): Need to handle if-not-exists here as well.
//
// TODO(pmattis): This is writing the namespace and descriptor table entries,
// but not going through the normal INSERT logic and not performing a precise
// mimicry. In particular, we're only writing a single key per table, while
// perfect mimicry would involve writing a sentinel key for each row as well.
descKey := sqlbase.MakeDescMetadataKey(descriptor.GetID())
b := client.Batch{}
descID := descriptor.GetID()
descDesc := sqlbase.WrapDescriptor(descriptor)
if log.V(2) {
log.Infof("CPut %s -> %d", idKey, descID)
log.Infof("CPut %s -> %s", descKey, descDesc)
}
b.CPut(idKey, descID, nil)
b.CPut(descKey, descDesc, nil)
p.setTestingVerifyMetadata(func(systemConfig config.SystemConfig) error {
if err := expectDescriptorID(systemConfig, idKey, descID); err != nil {
return err
}
return expectDescriptor(systemConfig, descKey, descDesc)
})
return true, p.txn.Run(&b)
}
开发者ID:CubeLite,项目名称:cockroach,代码行数:55,代码来源:descriptor.go
示例5: truncateTable
// truncateTable truncates the data of a table.
// It deletes a range of data for the table, which includes the PK and all
// indexes.
func truncateTable(tableDesc *sqlbase.TableDescriptor, txn *client.Txn) error {
tablePrefix := keys.MakeTablePrefix(uint32(tableDesc.ID))
// Delete rows and indexes starting with the table's prefix.
tableStartKey := roachpb.Key(tablePrefix)
tableEndKey := tableStartKey.PrefixEnd()
if log.V(2) {
log.Infof("DelRange %s - %s", tableStartKey, tableEndKey)
}
b := client.Batch{}
b.DelRange(tableStartKey, tableEndKey, false)
return txn.Run(&b)
}
开发者ID:CubeLite,项目名称:cockroach,代码行数:16,代码来源:truncate.go
示例6: deleteIndexRow
// deleteIndexRow adds to the batch the kv operations necessary to delete a
// table row from the given index.
func (rd *rowDeleter) deleteIndexRow(
ctx context.Context, b *client.Batch, idx *sqlbase.IndexDescriptor, values []parser.Datum,
) error {
if err := rd.fks.checkAll(values); err != nil {
return err
}
secondaryIndexEntry, err := sqlbase.EncodeSecondaryIndex(
rd.helper.tableDesc, idx, rd.fetchColIDtoRowIndex, values)
if err != nil {
return err
}
if log.V(2) {
log.Infof(ctx, "Del %s", secondaryIndexEntry.Key)
}
b.Del(secondaryIndexEntry.Key)
return nil
}
开发者ID:yangxuanjia,项目名称:cockroach,代码行数:19,代码来源:rowwriter.go
示例7: convertBatchError
func convertBatchError(tableDesc *sqlbase.TableDescriptor, b *client.Batch) error {
origPErr := b.MustPErr()
if origPErr.Index == nil {
return origPErr.GoError()
}
index := origPErr.Index.Index
if index >= int32(len(b.Results)) {
panic(fmt.Sprintf("index %d outside of results: %+v", index, b.Results))
}
result := b.Results[index]
var alloc sqlbase.DatumAlloc
if _, ok := origPErr.GetDetail().(*roachpb.ConditionFailedError); ok {
for _, row := range result.Rows {
// TODO(dan): There's too much internal knowledge of the sql table
// encoding here (and this callsite is the only reason
// DecodeIndexKeyPrefix is exported). Refactor this bit out.
indexID, key, err := sqlbase.DecodeIndexKeyPrefix(&alloc, tableDesc, row.Key)
if err != nil {
return err
}
index, err := tableDesc.FindIndexByID(indexID)
if err != nil {
return err
}
valTypes, err := sqlbase.MakeKeyVals(tableDesc, index.ColumnIDs)
if err != nil {
return err
}
dirs := make([]encoding.Direction, 0, len(index.ColumnIDs))
for _, dir := range index.ColumnDirections {
convertedDir, err := dir.ToEncodingDirection()
if err != nil {
return err
}
dirs = append(dirs, convertedDir)
}
vals := make([]parser.Datum, len(valTypes))
if _, err := sqlbase.DecodeKeyVals(&alloc, valTypes, vals, dirs, key); err != nil {
return err
}
return sqlbase.NewUniquenessConstraintViolationError(index, vals)
}
}
return origPErr.GoError()
}
开发者ID:yangxuanjia,项目名称:cockroach,代码行数:46,代码来源:errors.go
示例8: RenameDatabase
// RenameDatabase renames the database.
// Privileges: security.RootUser user.
// Notes: postgres requires superuser, db owner, or "CREATEDB".
// mysql >= 5.1.23 does not allow database renames.
func (p *planner) RenameDatabase(n *parser.RenameDatabase) (planNode, error) {
if n.Name == "" || n.NewName == "" {
return nil, errEmptyDatabaseName
}
if p.session.User != security.RootUser {
return nil, fmt.Errorf("only %s is allowed to rename databases", security.RootUser)
}
dbDesc, err := p.getDatabaseDesc(string(n.Name))
if err != nil {
return nil, err
}
if dbDesc == nil {
return nil, sqlbase.NewUndefinedDatabaseError(string(n.Name))
}
if n.Name == n.NewName {
// Noop.
return &emptyNode{}, nil
}
// Now update the nameMetadataKey and the descriptor.
descKey := sqlbase.MakeDescMetadataKey(dbDesc.GetID())
dbDesc.SetName(string(n.NewName))
if err := dbDesc.Validate(); err != nil {
return nil, err
}
newKey := databaseKey{string(n.NewName)}.Key()
oldKey := databaseKey{string(n.Name)}.Key()
descID := dbDesc.GetID()
descDesc := sqlbase.WrapDescriptor(dbDesc)
b := client.Batch{}
b.CPut(newKey, descID, nil)
b.Put(descKey, descDesc)
b.Del(oldKey)
if err := p.txn.Run(&b); err != nil {
if _, ok := err.(*roachpb.ConditionFailedError); ok {
return nil, fmt.Errorf("the new database name %q already exists", string(n.NewName))
}
return nil, err
}
p.setTestingVerifyMetadata(func(systemConfig config.SystemConfig) error {
if err := expectDescriptorID(systemConfig, newKey, descID); err != nil {
return err
}
if err := expectDescriptor(systemConfig, descKey, descDesc); err != nil {
return err
}
return expectDeleted(systemConfig, oldKey)
})
return &emptyNode{}, nil
}
开发者ID:CubeLite,项目名称:cockroach,代码行数:63,代码来源:rename.go
示例9: TestAuthentication
// TestAuthentication tests authentication for the KV endpoint.
func TestAuthentication(t *testing.T) {
defer leaktest.AfterTest(t)()
s, _, _ := serverutils.StartServer(t, base.TestServerArgs{})
defer s.Stopper().Stop()
var b1 client.Batch
b1.Put("a", "b")
// Create a node user client and call Run() on it which lets us build our own
// request, specifying the user.
db1 := createTestClientForUser(t, s.Stopper(), s.ServingAddr(), security.NodeUser)
if err := db1.Run(&b1); err != nil {
t.Fatal(err)
}
var b2 client.Batch
b2.Put("c", "d")
// Try again, but this time with certs for a non-node user (even the root
// user has no KV permissions).
db2 := createTestClientForUser(t, s.Stopper(), s.ServingAddr(), security.RootUser)
if err := db2.Run(&b2); !testutils.IsError(err, "is not allowed") {
t.Fatal(err)
}
}
开发者ID:YuleiXiao,项目名称:cockroach,代码行数:26,代码来源:db_test.go
示例10: flush
// flush writes all dirty nodes and the tree to the transaction.
func (tc *treeContext) flush(b *client.Batch) {
if tc.dirty {
b.Put(keys.RangeTreeRoot, tc.tree)
}
for key, cachedNode := range tc.nodes {
if cachedNode.dirty {
if cachedNode.node == nil {
b.Del(keys.RangeTreeNodeKey(roachpb.RKey(key)))
} else {
b.Put(keys.RangeTreeNodeKey(roachpb.RKey(key)), cachedNode.node)
}
}
}
}
开发者ID:CubeLite,项目名称:cockroach,代码行数:15,代码来源:range_tree.go
示例11: delMeta
func delMeta(b *client.Batch, key roachpb.Key, desc *roachpb.RangeDescriptor) {
b.Del(key)
}
开发者ID:CubeLite,项目名称:cockroach,代码行数:3,代码来源:addressing.go
示例12: putMeta
func putMeta(b *client.Batch, key roachpb.Key, desc *roachpb.RangeDescriptor) {
b.Put(key, desc)
}
开发者ID:CubeLite,项目名称:cockroach,代码行数:3,代码来源:addressing.go
示例13: exec
// Execute the entire schema change in steps. startBackfillNotification is
// called before the backfill starts; it can be nil.
func (sc SchemaChanger) exec(
startBackfillNotification func() error,
oldNameNotInUseNotification func(),
) error {
// Acquire lease.
lease, err := sc.AcquireLease()
if err != nil {
return err
}
needRelease := true
// Always try to release lease.
defer func(l *sqlbase.TableDescriptor_SchemaChangeLease) {
// If the schema changer deleted the descriptor, there's no longer a lease to be
// released.
if !needRelease {
return
}
if err := sc.ReleaseLease(*l); err != nil {
log.Warning(err)
}
}(&lease)
// Increment the version and unset tableDescriptor.UpVersion.
desc, err := sc.MaybeIncrementVersion()
if err != nil {
return err
}
if desc.GetTable().Deleted() {
lease, err = sc.ExtendLease(lease)
if err != nil {
return err
}
// Wait for everybody to see the version with the deleted bit set. When
// this returns, nobody has any leases on the table, nor can get new leases,
// so the table will no longer be modified.
if err := sc.waitToUpdateLeases(); err != nil {
return err
}
// Truncate the table and delete the descriptor.
if err := sc.truncateAndDropTable(&lease, desc.GetTable()); err != nil {
return err
}
needRelease = false
return nil
}
if desc.GetTable().Renamed() {
lease, err = sc.ExtendLease(lease)
if err != nil {
return err
}
// Wait for everyone to see the version with the new name. When this
// returns, no new transactions will be using the old name for the table, so
// the old name can now be re-used (by CREATE).
if err := sc.waitToUpdateLeases(); err != nil {
return err
}
if oldNameNotInUseNotification != nil {
oldNameNotInUseNotification()
}
// Free up the old name(s).
err := sc.db.Txn(func(txn *client.Txn) error {
b := client.Batch{}
for _, renameDetails := range desc.GetTable().Renames {
tbKey := tableKey{
sqlbase.ID(renameDetails.OldParentID), renameDetails.OldName}.Key()
b.Del(tbKey)
}
if err := txn.Run(&b); err != nil {
return err
}
return nil
})
if err != nil {
return err
}
// Clean up - clear the descriptor's state.
_, err = sc.leaseMgr.Publish(sc.tableID, func(desc *sqlbase.TableDescriptor) error {
desc.Renames = nil
return nil
}, nil)
if err != nil {
return err
}
}
// Wait for the schema change to propagate to all nodes after this function
// returns, so that the new schema is live everywhere. This is not needed for
// correctness but is done to make the UI experience/tests predictable.
defer func() {
if err := sc.waitToUpdateLeases(); err != nil {
log.Warning(err)
}
}()
//.........这里部分代码省略.........
开发者ID:CubeLite,项目名称:cockroach,代码行数:101,代码来源:schema_changer.go
示例14: updateRow
// updateRow adds to the batch the kv operations necessary to update a table row
// with the given values.
//
// The row corresponding to oldValues is updated with the ones in updateValues.
// Note that updateValues only contains the ones that are changing.
//
// The return value is only good until the next call to UpdateRow.
func (ru *rowUpdater) updateRow(
b *client.Batch,
oldValues []parser.Datum,
updateValues []parser.Datum,
) ([]parser.Datum, error) {
if len(oldValues) != len(ru.fetchCols) {
return nil, errors.Errorf("got %d values but expected %d", len(oldValues), len(ru.fetchCols))
}
if len(updateValues) != len(ru.updateCols) {
return nil, errors.Errorf("got %d values but expected %d", len(updateValues), len(ru.updateCols))
}
primaryIndexKey, secondaryIndexEntries, err := ru.helper.encodeIndexes(ru.fetchColIDtoRowIndex, oldValues)
if err != nil {
return nil, err
}
// The secondary index entries returned by rowHelper.encodeIndexes are only
// valid until the next call to encodeIndexes. We need to copy them so that
// we can compare against the new secondary index entries.
secondaryIndexEntries = append(ru.indexEntriesBuf[:0], secondaryIndexEntries...)
ru.indexEntriesBuf = secondaryIndexEntries
// Check that the new value types match the column types. This needs to
// happen before index encoding because certain datum types (i.e. tuple)
// cannot be used as index values.
for i, val := range updateValues {
if ru.marshalled[i], err = sqlbase.MarshalColumnValue(ru.updateCols[i], val); err != nil {
return nil, err
}
}
// Update the row values.
copy(ru.newValues, oldValues)
for i, updateCol := range ru.updateCols {
ru.newValues[ru.fetchColIDtoRowIndex[updateCol.ID]] = updateValues[i]
}
rowPrimaryKeyChanged := false
var newSecondaryIndexEntries []sqlbase.IndexEntry
if ru.primaryKeyColChange {
var newPrimaryIndexKey []byte
newPrimaryIndexKey, newSecondaryIndexEntries, err =
ru.helper.encodeIndexes(ru.fetchColIDtoRowIndex, ru.newValues)
if err != nil {
return nil, err
}
rowPrimaryKeyChanged = !bytes.Equal(primaryIndexKey, newPrimaryIndexKey)
} else {
newSecondaryIndexEntries, err =
ru.helper.encodeSecondaryIndexes(ru.fetchColIDtoRowIndex, ru.newValues)
if err != nil {
return nil, err
}
}
if rowPrimaryKeyChanged {
if err := ru.fks.checkIdx(ru.helper.tableDesc.PrimaryIndex.ID, oldValues, ru.newValues); err != nil {
return nil, err
}
for i := range newSecondaryIndexEntries {
if !bytes.Equal(newSecondaryIndexEntries[i].Key, secondaryIndexEntries[i].Key) {
if err := ru.fks.checkIdx(ru.helper.indexes[i].ID, oldValues, ru.newValues); err != nil {
return nil, err
}
}
}
if err := ru.rd.deleteRow(b, oldValues); err != nil {
return nil, err
}
if err := ru.ri.insertRow(b, ru.newValues, false); err != nil {
return nil, err
}
return ru.newValues, nil
}
// Add the new values.
// TODO(dan): This has gotten very similar to the loop in insertRow, see if
// they can be DRY'd. Ideally, this would also work for
// truncateAndBackfillColumnsChunk, which is currently abusing rowUdpdater.
for i, family := range ru.helper.tableDesc.Families {
update := false
for _, colID := range family.ColumnIDs {
if _, ok := ru.updateColIDtoRowIndex[colID]; ok {
update = true
break
}
}
if !update {
continue
}
//.........这里部分代码省略.........
开发者ID:YuleiXiao,项目名称:cockroach,代码行数:101,代码来源:rowwriter.go
示例15: insertPutFn
// insertPutFn is used by insertRow when conflicts should be ignored.
// logValue is used for pretty printing.
func insertPutFn(b *client.Batch, key *roachpb.Key, value *roachpb.Value) {
if log.V(2) {
log.InfofDepth(1, "Put %s -> %s", *key, value.PrettyPrint())
}
b.Put(key, value)
}
开发者ID:YuleiXiao,项目名称:cockroach,代码行数:8,代码来源:rowwriter.go
示例16: Query
// Query returns datapoints for the named time series during the supplied time
// span. Data is returned as a series of consecutive data points.
//
// Data is queried only at the Resolution supplied: if data for the named time
// series is not stored at the given resolution, an empty result will be
// returned.
//
// All data stored on the server is downsampled to some degree; the data points
// returned represent the average value within a sample period. Each datapoint's
// timestamp falls in the middle of the sample period it represents.
//
// If data for the named time series was collected from multiple sources, each
// returned datapoint will represent the sum of datapoints from all sources at
// the same time. The returned string slices contains a list of all sources for
// the metric which were aggregated to produce the result.
func (db *DB) Query(query tspb.Query, r Resolution, startNanos, endNanos int64) ([]tspb.TimeSeriesDatapoint, []string, error) {
// Normalize startNanos and endNanos the nearest SampleDuration boundary.
startNanos -= startNanos % r.SampleDuration()
var rows []client.KeyValue
if len(query.Sources) == 0 {
// Based on the supplied timestamps and resolution, construct start and end
// keys for a scan that will return every key with data relevant to the
// query.
startKey := MakeDataKey(query.Name, "" /* source */, r, startNanos)
endKey := MakeDataKey(query.Name, "" /* source */, r, endNanos).PrefixEnd()
var b client.Batch
b.Header.ReadConsistency = roachpb.INCONSISTENT
b.Scan(startKey, endKey, 0)
if err := db.db.Run(&b); err != nil {
return nil, nil, err
}
rows = b.Results[0].Rows
} else {
b := db.db.NewBatch()
b.Header.ReadConsistency = roachpb.INCONSISTENT
// Iterate over all key timestamps which may contain data for the given
// sources, based on the given start/end time and the resolution.
kd := r.KeyDuration()
startKeyNanos := startNanos - (startNanos % kd)
endKeyNanos := endNanos - (endNanos % kd)
for currentTimestamp := startKeyNanos; currentTimestamp <= endKeyNanos; currentTimestamp += kd {
for _, source := range query.Sources {
key := MakeDataKey(query.Name, source, r, currentTimestamp)
b.Get(key)
}
}
err := db.db.Run(b)
if err != nil {
return nil, nil, err
}
for _, result := range b.Results {
row := result.Rows[0]
if row.Value == nil {
continue
}
rows = append(rows, row)
}
}
// Convert the queried source data into a set of data spans, one for each
// source.
sourceSpans, err := makeDataSpans(rows, startNanos)
if err != nil {
return nil, nil, err
}
// Compute a downsample function which will be used to return values from
// each source for each sample period.
downsampler, err := getDownsampleFunction(query.GetDownsampler())
if err != nil {
return nil, nil, err
}
// If we are returning a derivative, iteration needs to start at offset -1
// (in order to correctly compute the rate of change at offset 0).
var startOffset int32
isDerivative := query.GetDerivative() != tspb.TimeSeriesQueryDerivative_NONE
if isDerivative {
startOffset = -1
}
// Create an interpolatingIterator for each dataSpan, adding each iterator
// into a unionIterator collection. This is also where we compute a list of
// all sources with data present in the query.
sources := make([]string, 0, len(sourceSpans))
iters := make(unionIterator, 0, len(sourceSpans))
for name, span := range sourceSpans {
sources = append(sources, name)
iters = append(iters, span.newIterator(startOffset, downsampler))
}
// Choose an aggregation function to use when taking values from the
// unionIterator.
var valueFn func() float64
switch query.GetSourceAggregator() {
case tspb.TimeSeriesQueryAggregator_SUM:
valueFn = iters.sum
case tspb.TimeSeriesQueryAggregator_AVG:
//.........这里部分代码省略.........
开发者ID:CubeLite,项目名称:cockroach,代码行数:101,代码来源:query.go
示例17: RenameTable
// RenameTable renames the table.
// Privileges: DROP on source table, CREATE on destination database.
// Notes: postgres requires the table owner.
// mysql requires ALTER, DROP on the original table, and CREATE, INSERT
// on the new table (and does not copy privileges over).
func (p *planner) RenameTable(n *parser.RenameTable) (planNode, error) {
if err := n.NewName.NormalizeTableName(p.session.Database); err != nil {
return nil, err
}
if n.NewName.Table() == "" {
return nil, errEmptyTableName
}
if err := n.Name.NormalizeTableName(p.session.Database); err != nil {
return nil, err
}
dbDesc, err := p.getDatabaseDesc(n.Name.Database())
if err != nil {
return nil, err
}
if dbDesc == nil {
return nil, sqlbase.NewUndefinedDatabaseError(n.Name.Database())
}
tbKey := tableKey{dbDesc.ID, n.Name.Table()}.Key()
// Check if table exists.
gr, err := p.txn.Get(tbKey)
if err != nil {
return nil, err
}
if !gr.Exists() {
if n.IfExists {
// Noop.
return &emptyNode{}, nil
}
// Key does not exist, but we want it to: error out.
return nil, fmt.Errorf("table %q does not exist", n.Name.Table())
}
targetDbDesc, err := p.getDatabaseDesc(n.NewName.Database())
if err != nil {
return nil, err
}
if targetDbDesc == nil {
return nil, sqlbase.NewUndefinedDatabaseError(n.NewName.Database())
}
if err := p.checkPrivilege(targetDbDesc, privilege.CREATE); err != nil {
return nil, err
}
if n.Name.Database() == n.NewName.Database() && n.Name.Table() == n.NewName.Table() {
// Noop.
return &emptyNode{}, nil
}
tableDesc, err := p.getTableDesc(n.Name)
if err != nil {
return nil, err
}
if tableDesc == nil || tableDesc.State != sqlbase.TableDescriptor_PUBLIC {
return nil, sqlbase.NewUndefinedTableError(n.Name.String())
}
if err := p.checkPrivilege(tableDesc, privilege.DROP); err != nil {
return nil, err
}
tableDesc.SetName(n.NewName.Table())
tableDesc.ParentID = targetDbDesc.ID
descKey := sqlbase.MakeDescMetadataKey(tableDesc.GetID())
newTbKey := tableKey{targetDbDesc.ID, n.NewName.Table()}.Key()
if err := tableDesc.Validate(); err != nil {
return nil, err
}
descID := tableDesc.GetID()
descDesc := sqlbase.WrapDescriptor(tableDesc)
if err := tableDesc.SetUpVersion(); err != nil {
return nil, err
}
renameDetails := sqlbase.TableDescriptor_RenameInfo{
OldParentID: uint32(dbDesc.ID),
OldName: n.Name.Table()}
tableDesc.Renames = append(tableDesc.Renames, renameDetails)
if err := p.writeTableDesc(tableDesc); err != nil {
return nil, err
}
// We update the descriptor to the new name, but also leave the mapping of the
// old name to the id, so that the name is not reused until the schema changer
// has made sure it's not in use any more.
b := client.Batch{}
b.Put(descKey, descDesc)
//.........这里部分代码省略.........
开发者ID:CubeLite,项目名称:cockroach,代码行数:101,代码来源:rename.go
注:本文中的github.com/cockroachdb/cockroach/internal/client.Batch类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论