// scanCmd reads the values from the db from [key, endKey). func scanCmd(c *cmd, txn *client.Txn, t *testing.T) error { rows, err := txn.Scan(c.getKey(), c.getEndKey(), 0) if err != nil { return err } var vals []string keyPrefix := []byte(fmt.Sprintf("%d.", c.historyIdx)) for _, kv := range rows { key := bytes.TrimPrefix(kv.Key, keyPrefix) c.env[string(key)] = kv.ValueInt() vals = append(vals, fmt.Sprintf("%d", kv.ValueInt())) } c.debug = fmt.Sprintf("[%s]", strings.Join(vals, " ")) return nil }
func allRangeDescriptors(txn *client.Txn) ([]roachpb.RangeDescriptor, error) { // TODO(dan): Iterate with some batch size. rows, err := txn.Scan(keys.Meta2Prefix, keys.MetaMax, 0) if err != nil { return nil, errors.Wrap(err, "unable to scan range descriptors") } rangeDescs := make([]roachpb.RangeDescriptor, len(rows)) for i, row := range rows { if err := row.ValueProto(&rangeDescs[i]); err != nil { return nil, errors.Wrapf(err, "%s: unable to unmarshal range descriptor", row.Key) } } return rangeDescs, nil }
func allSQLDescriptors(txn *client.Txn) ([]sqlbase.Descriptor, error) { startKey := roachpb.Key(keys.MakeTablePrefix(keys.DescriptorTableID)) endKey := startKey.PrefixEnd() // TODO(dan): Iterate with some batch size. rows, err := txn.Scan(startKey, endKey, 0) if err != nil { return nil, errors.Wrap(err, "unable to scan SQL descriptors") } sqlDescs := make([]sqlbase.Descriptor, len(rows)) for i, row := range rows { if err := row.ValueProto(&sqlDescs[i]); err != nil { return nil, errors.Wrapf(err, "%s: unable to unmarshal SQL descriptor", row.Key) } } return sqlDescs, nil }
func restoreTable( ctx context.Context, sst engine.RocksDBSstFileReader, txn *client.Txn, table *sqlbase.TableDescriptor, overwrite bool, ) error { log.Infof(ctx, "Restoring Table %q", table.Name) tableStartKey := roachpb.Key(sqlbase.MakeIndexKeyPrefix(table, table.PrimaryIndex.ID)) tableEndKey := tableStartKey.PrefixEnd() existingDesc, err := txn.Get(sqlbase.MakeDescMetadataKey(table.GetID())) if err != nil { return err } existingData, err := txn.Scan(tableStartKey, tableEndKey, 1) if err != nil { return err } if existingDesc.Value != nil || len(existingData) > 0 { if overwrite { // We're about to Put the descriptor, so don't bother deleting it. if err := txn.DelRange(tableStartKey, tableEndKey); err != nil { return err } } else { return errors.Errorf("table %q already exists", table.Name) } } tableDescKey := sqlbase.MakeDescMetadataKey(table.GetID()) if err := txn.Put(tableDescKey, sqlbase.WrapDescriptor(table)); err != nil { return err } return Import(ctx, sst, txn, engine.MVCCKey{Key: tableStartKey}, engine.MVCCKey{Key: tableEndKey}) }