go.temporal.io/server@v1.23.0/common/persistence/sql/sqlplugin/sqlite/shard.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  
    33  	"go.temporal.io/server/common/persistence/sql/sqlplugin"
    34  )
    35  
    36  const (
    37  	createShardQry = `INSERT INTO
    38   shards (shard_id, range_id, data, data_encoding) VALUES (?, ?, ?, ?)`
    39  
    40  	getShardQry = `SELECT
    41   shard_id, range_id, data, data_encoding
    42   FROM shards WHERE shard_id = ?`
    43  
    44  	updateShardQry = `UPDATE shards 
    45   SET range_id = ?, data = ?, data_encoding = ? 
    46   WHERE shard_id = ?`
    47  
    48  	lockShardQry     = `SELECT range_id FROM shards WHERE shard_id = ?`
    49  	readLockShardQry = lockShardQry
    50  )
    51  
    52  // InsertIntoShards inserts one or more rows into shards table
    53  func (mdb *db) InsertIntoShards(
    54  	ctx context.Context,
    55  	row *sqlplugin.ShardsRow,
    56  ) (sql.Result, error) {
    57  	return mdb.conn.ExecContext(ctx,
    58  		createShardQry,
    59  		row.ShardID,
    60  		row.RangeID,
    61  		row.Data,
    62  		row.DataEncoding,
    63  	)
    64  }
    65  
    66  // UpdateShards updates one or more rows into shards table
    67  func (mdb *db) UpdateShards(
    68  	ctx context.Context,
    69  	row *sqlplugin.ShardsRow,
    70  ) (sql.Result, error) {
    71  	return mdb.conn.ExecContext(ctx,
    72  		updateShardQry,
    73  		row.RangeID,
    74  		row.Data,
    75  		row.DataEncoding,
    76  		row.ShardID,
    77  	)
    78  }
    79  
    80  // SelectFromShards reads one or more rows from shards table
    81  func (mdb *db) SelectFromShards(
    82  	ctx context.Context,
    83  	filter sqlplugin.ShardsFilter,
    84  ) (*sqlplugin.ShardsRow, error) {
    85  	var row sqlplugin.ShardsRow
    86  	err := mdb.conn.GetContext(ctx,
    87  		&row,
    88  		getShardQry,
    89  		filter.ShardID,
    90  	)
    91  	if err != nil {
    92  		return nil, err
    93  	}
    94  	return &row, err
    95  }
    96  
    97  // ReadLockShards acquires a read lock on a single row in shards table
    98  func (mdb *db) ReadLockShards(
    99  	ctx context.Context,
   100  	filter sqlplugin.ShardsFilter,
   101  ) (int64, error) {
   102  	var rangeID int64
   103  	err := mdb.conn.GetContext(ctx,
   104  		&rangeID,
   105  		readLockShardQry,
   106  		filter.ShardID,
   107  	)
   108  	return rangeID, err
   109  }
   110  
   111  // WriteLockShards acquires a write lock on a single row in shards table
   112  func (mdb *db) WriteLockShards(
   113  	ctx context.Context,
   114  	filter sqlplugin.ShardsFilter,
   115  ) (int64, error) {
   116  	var rangeID int64
   117  	err := mdb.conn.GetContext(ctx,
   118  		&rangeID,
   119  		lockShardQry,
   120  		filter.ShardID,
   121  	)
   122  	return rangeID, err
   123  }