vitess.io/vitess@v0.16.2/go/vt/topo/metadata.go (about)

     1  /*
     2  Copyright 2019 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 topo
    18  
    19  import (
    20  	"context"
    21  	"path"
    22  
    23  	"vitess.io/vitess/go/vt/sqlparser"
    24  
    25  	"vitess.io/vitess/go/event"
    26  	"vitess.io/vitess/go/vt/topo/events"
    27  )
    28  
    29  // UpsertMetadata sets the key/value in the metadata if it doesn't exist, otherwise it updates the content
    30  func (ts *Server) UpsertMetadata(ctx context.Context, key string, val string) error {
    31  	keyPath := path.Join(MetadataPath, key)
    32  
    33  	_, _, err := ts.globalCell.Get(ctx, keyPath)
    34  	status := "updated"
    35  
    36  	if err != nil {
    37  		if !IsErrType(err, NoNode) {
    38  			return err
    39  		}
    40  
    41  		status = "created"
    42  	}
    43  
    44  	// nil version means that it will insert if keyPath does not exist
    45  	if _, err := ts.globalCell.Update(ctx, keyPath, []byte(val), nil); err != nil {
    46  		return err
    47  	}
    48  
    49  	dispatchEvent(keyPath, status)
    50  	return nil
    51  }
    52  
    53  // GetMetadata retrieves all metadata value that matches the given key regular expression. If empty all values are returned.
    54  func (ts *Server) GetMetadata(ctx context.Context, keyFilter string) (map[string]string, error) {
    55  	keys, err := ts.globalCell.ListDir(ctx, MetadataPath, false)
    56  	if err != nil {
    57  		return nil, err
    58  	}
    59  
    60  	re := sqlparser.LikeToRegexp(keyFilter)
    61  
    62  	result := make(map[string]string)
    63  	for _, k := range keys {
    64  		if !re.MatchString(k.Name) {
    65  			continue
    66  		}
    67  
    68  		val, err := ts.getMetadata(ctx, k.Name)
    69  		if err != nil {
    70  			return nil, err
    71  		}
    72  		result[k.Name] = val
    73  	}
    74  
    75  	return result, nil
    76  }
    77  
    78  // DeleteMetadata deletes the key in the metadata
    79  func (ts *Server) DeleteMetadata(ctx context.Context, key string) error {
    80  	keyPath := path.Join(MetadataPath, key)
    81  
    82  	// nil version means that it will insert if keyPath does not exist
    83  	if err := ts.globalCell.Delete(ctx, keyPath, nil); err != nil {
    84  		return err
    85  	}
    86  
    87  	dispatchEvent(keyPath, "deleted")
    88  	return nil
    89  }
    90  
    91  func (ts *Server) getMetadata(ctx context.Context, key string) (string, error) {
    92  	keyPath := path.Join(MetadataPath, key)
    93  	contents, _, err := ts.globalCell.Get(ctx, keyPath)
    94  	if err != nil {
    95  		return "", err
    96  	}
    97  
    98  	return string(contents), nil
    99  }
   100  
   101  func dispatchEvent(key string, status string) {
   102  	event.Dispatch(&events.MetadataChange{
   103  		Key:    key,
   104  		Status: status,
   105  	})
   106  }