go.temporal.io/server@v1.23.0/common/persistence/sql/sqlplugin/sqlite/namespace.go (about)

     1  // The MIT License
     2  //
     3  // Copyright (c) 2021 Datadog, Inc.
     4  //
     5  // Copyright (c) 2020 Temporal Technologies Inc.  All rights reserved.
     6  //
     7  // Copyright (c) 2020 Uber Technologies, Inc.
     8  //
     9  // Permission is hereby granted, free of charge, to any person obtaining a copy
    10  // of this software and associated documentation files (the "Software"), to deal
    11  // in the Software without restriction, including without limitation the rights
    12  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    13  // copies of the Software, and to permit persons to whom the Software is
    14  // furnished to do so, subject to the following conditions:
    15  //
    16  // The above copyright notice and this permission notice shall be included in
    17  // all copies or substantial portions of the Software.
    18  //
    19  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    20  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    21  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    22  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    23  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    24  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    25  // THE SOFTWARE.
    26  
    27  package sqlite
    28  
    29  import (
    30  	"context"
    31  	"database/sql"
    32  	"errors"
    33  
    34  	"go.temporal.io/api/serviceerror"
    35  
    36  	"go.temporal.io/server/common/persistence/sql/sqlplugin"
    37  )
    38  
    39  const (
    40  	createNamespaceQuery = `INSERT INTO 
    41   namespaces (partition_id, id, name, is_global, data, data_encoding, notification_version)
    42   VALUES(?, ?, ?, ?, ?, ?, ?)`
    43  
    44  	updateNamespaceQuery = `UPDATE namespaces 
    45   SET name = ?, data = ?, data_encoding = ?, is_global = ?, notification_version = ?
    46   WHERE partition_id=54321 AND id = ?`
    47  
    48  	getNamespacePart = `SELECT id, name, is_global, data, data_encoding, notification_version FROM namespaces`
    49  
    50  	getNamespaceByIDQuery   = getNamespacePart + ` WHERE partition_id=? AND id = ?`
    51  	getNamespaceByNameQuery = getNamespacePart + ` WHERE partition_id=? AND name = ?`
    52  
    53  	listNamespacesQuery      = getNamespacePart + ` WHERE partition_id=? ORDER BY id LIMIT ?`
    54  	listNamespacesRangeQuery = getNamespacePart + ` WHERE partition_id=? AND id > ? ORDER BY id LIMIT ?`
    55  
    56  	deleteNamespaceByIDQuery   = `DELETE FROM namespaces WHERE partition_id=? AND id = ?`
    57  	deleteNamespaceByNameQuery = `DELETE FROM namespaces WHERE partition_id=? AND name = ?`
    58  
    59  	getNamespaceMetadataQuery    = `SELECT notification_version FROM namespace_metadata WHERE partition_id = 54321`
    60  	lockNamespaceMetadataQuery   = getNamespaceMetadataQuery
    61  	updateNamespaceMetadataQuery = `UPDATE namespace_metadata SET notification_version = ? WHERE notification_version = ? AND partition_id = 54321`
    62  )
    63  
    64  const (
    65  	partitionID = 54321
    66  )
    67  
    68  var errMissingArgs = errors.New("missing one or more args for API")
    69  
    70  // InsertIntoNamespace inserts a single row into namespaces table
    71  func (mdb *db) InsertIntoNamespace(
    72  	ctx context.Context,
    73  	row *sqlplugin.NamespaceRow,
    74  ) (sql.Result, error) {
    75  	return mdb.conn.ExecContext(ctx,
    76  		createNamespaceQuery,
    77  		partitionID,
    78  		row.ID,
    79  		row.Name,
    80  		row.IsGlobal,
    81  		row.Data,
    82  		row.DataEncoding,
    83  		row.NotificationVersion,
    84  	)
    85  }
    86  
    87  // UpdateNamespace updates a single row in namespaces table
    88  func (mdb *db) UpdateNamespace(
    89  	ctx context.Context,
    90  	row *sqlplugin.NamespaceRow,
    91  ) (sql.Result, error) {
    92  	return mdb.conn.ExecContext(ctx,
    93  		updateNamespaceQuery,
    94  		row.Name,
    95  		row.Data,
    96  		row.DataEncoding,
    97  		row.IsGlobal,
    98  		row.NotificationVersion,
    99  		row.ID,
   100  	)
   101  }
   102  
   103  // SelectFromNamespace reads one or more rows from namespaces table
   104  func (mdb *db) SelectFromNamespace(
   105  	ctx context.Context,
   106  	filter sqlplugin.NamespaceFilter,
   107  ) ([]sqlplugin.NamespaceRow, error) {
   108  	switch {
   109  	case filter.ID != nil || filter.Name != nil:
   110  		if filter.ID != nil && filter.Name != nil {
   111  			return nil, serviceerror.NewInternal("only ID or name filter can be specified for selection")
   112  		}
   113  		return mdb.selectFromNamespace(ctx, filter)
   114  	case filter.PageSize != nil && *filter.PageSize > 0:
   115  		return mdb.selectAllFromNamespace(ctx, filter)
   116  	default:
   117  		return nil, errMissingArgs
   118  	}
   119  }
   120  
   121  func (mdb *db) selectFromNamespace(
   122  	ctx context.Context,
   123  	filter sqlplugin.NamespaceFilter,
   124  ) ([]sqlplugin.NamespaceRow, error) {
   125  	var err error
   126  	var row sqlplugin.NamespaceRow
   127  	switch {
   128  	case filter.ID != nil:
   129  		err = mdb.conn.GetContext(ctx,
   130  			&row,
   131  			getNamespaceByIDQuery,
   132  			partitionID,
   133  			*filter.ID,
   134  		)
   135  	case filter.Name != nil:
   136  		err = mdb.conn.GetContext(ctx,
   137  			&row,
   138  			getNamespaceByNameQuery,
   139  			partitionID,
   140  			*filter.Name,
   141  		)
   142  	}
   143  	if err != nil {
   144  		return nil, err
   145  	}
   146  	return []sqlplugin.NamespaceRow{row}, nil
   147  }
   148  
   149  func (mdb *db) selectAllFromNamespace(
   150  	ctx context.Context,
   151  	filter sqlplugin.NamespaceFilter,
   152  ) ([]sqlplugin.NamespaceRow, error) {
   153  	var err error
   154  	var rows []sqlplugin.NamespaceRow
   155  	switch {
   156  	case filter.GreaterThanID != nil:
   157  		err = mdb.conn.SelectContext(ctx,
   158  			&rows,
   159  			listNamespacesRangeQuery,
   160  			partitionID,
   161  			*filter.GreaterThanID,
   162  			*filter.PageSize,
   163  		)
   164  	default:
   165  		err = mdb.conn.SelectContext(ctx,
   166  			&rows,
   167  			listNamespacesQuery,
   168  			partitionID,
   169  			filter.PageSize,
   170  		)
   171  	}
   172  	return rows, err
   173  }
   174  
   175  // DeleteFromNamespace deletes a single row in namespaces table
   176  func (mdb *db) DeleteFromNamespace(
   177  	ctx context.Context,
   178  	filter sqlplugin.NamespaceFilter,
   179  ) (sql.Result, error) {
   180  	var err error
   181  	var result sql.Result
   182  	switch {
   183  	case filter.ID != nil:
   184  		result, err = mdb.conn.ExecContext(ctx,
   185  			deleteNamespaceByIDQuery,
   186  			partitionID,
   187  			filter.ID,
   188  		)
   189  	default:
   190  		result, err = mdb.conn.ExecContext(ctx,
   191  			deleteNamespaceByNameQuery,
   192  			partitionID,
   193  			filter.Name,
   194  		)
   195  	}
   196  	return result, err
   197  }
   198  
   199  // LockNamespaceMetadata acquires a write lock on a single row in namespace_metadata table
   200  func (mdb *db) LockNamespaceMetadata(
   201  	ctx context.Context,
   202  ) (*sqlplugin.NamespaceMetadataRow, error) {
   203  	var row sqlplugin.NamespaceMetadataRow
   204  	err := mdb.conn.GetContext(ctx,
   205  		&row.NotificationVersion,
   206  		lockNamespaceMetadataQuery,
   207  	)
   208  	if err != nil {
   209  		return nil, err
   210  	}
   211  	return &row, nil
   212  }
   213  
   214  // SelectFromNamespaceMetadata reads a single row in namespace_metadata table
   215  func (mdb *db) SelectFromNamespaceMetadata(
   216  	ctx context.Context,
   217  ) (*sqlplugin.NamespaceMetadataRow, error) {
   218  	var row sqlplugin.NamespaceMetadataRow
   219  	err := mdb.conn.GetContext(ctx,
   220  		&row.NotificationVersion,
   221  		getNamespaceMetadataQuery,
   222  	)
   223  	return &row, err
   224  }
   225  
   226  // UpdateNamespaceMetadata updates a single row in namespace_metadata table
   227  func (mdb *db) UpdateNamespaceMetadata(
   228  	ctx context.Context,
   229  	row *sqlplugin.NamespaceMetadataRow,
   230  ) (sql.Result, error) {
   231  	return mdb.conn.ExecContext(ctx,
   232  		updateNamespaceMetadataQuery,
   233  		row.NotificationVersion+1,
   234  		row.NotificationVersion,
   235  	)
   236  }