vitess.io/vitess@v0.16.2/go/vt/vtorc/inst/keyspace_dao.go (about)

     1  /*
     2  Copyright 2022 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 inst
    18  
    19  import (
    20  	"errors"
    21  
    22  	"vitess.io/vitess/go/vt/external/golib/sqlutils"
    23  	topodatapb "vitess.io/vitess/go/vt/proto/topodata"
    24  	"vitess.io/vitess/go/vt/topo"
    25  	"vitess.io/vitess/go/vt/vtorc/db"
    26  )
    27  
    28  // ErrKeyspaceNotFound is a fixed error message used when a keyspace is not found in the database.
    29  var ErrKeyspaceNotFound = errors.New("keyspace not found")
    30  
    31  // ReadKeyspace reads the vitess keyspace record.
    32  func ReadKeyspace(keyspaceName string) (*topo.KeyspaceInfo, error) {
    33  	if err := topo.ValidateKeyspaceName(keyspaceName); err != nil {
    34  		return nil, err
    35  	}
    36  
    37  	query := `
    38  		select
    39  			keyspace_type,
    40  			durability_policy
    41  		from
    42  			vitess_keyspace
    43  		where keyspace=?
    44  		`
    45  	args := sqlutils.Args(keyspaceName)
    46  	keyspace := &topo.KeyspaceInfo{
    47  		Keyspace: &topodatapb.Keyspace{},
    48  	}
    49  	err := db.QueryVTOrc(query, args, func(row sqlutils.RowMap) error {
    50  		keyspace.KeyspaceType = topodatapb.KeyspaceType(row.GetInt("keyspace_type"))
    51  		keyspace.DurabilityPolicy = row.GetString("durability_policy")
    52  		keyspace.SetKeyspaceName(keyspaceName)
    53  		return nil
    54  	})
    55  	if err != nil {
    56  		return nil, err
    57  	}
    58  	if keyspace.KeyspaceName() == "" {
    59  		return nil, ErrKeyspaceNotFound
    60  	}
    61  	return keyspace, nil
    62  }
    63  
    64  // SaveKeyspace saves the keyspace record against the keyspace name.
    65  func SaveKeyspace(keyspace *topo.KeyspaceInfo) error {
    66  	_, err := db.ExecVTOrc(`
    67  		replace
    68  			into vitess_keyspace (
    69  				keyspace, keyspace_type, durability_policy
    70  			) values (
    71  				?, ?, ?
    72  			)
    73  		`,
    74  		keyspace.KeyspaceName(),
    75  		int(keyspace.KeyspaceType),
    76  		keyspace.GetDurabilityPolicy(),
    77  	)
    78  	return err
    79  }