go.temporal.io/server@v1.23.0/common/persistence/sql/sqlplugin/sqlite/queue_v2.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 templateEnqueueMessageQueryV2 = `INSERT INTO queue_messages (queue_type, queue_name, queue_partition, message_id, message_payload, message_encoding) VALUES(:queue_type, :queue_name, :queue_partition, :message_id, :message_payload, :message_encoding)` 38 templateGetMessagesQueryV2 = `SELECT message_id, message_payload, message_encoding FROM queue_messages WHERE queue_type = ? and queue_name = ? and queue_partition = ? and message_id >= ? ORDER BY message_id ASC LIMIT ?` 39 templateRangeDeleteMessagesQueryV2 = `DELETE FROM queue_messages WHERE queue_type = ? and queue_name = ? and queue_partition = ? and message_id >= ? and message_id <= ?` 40 templateGetLastMessageIDQueryV2 = `SELECT message_id FROM queue_messages WHERE queue_type=? and queue_name=? and queue_partition=? ORDER BY message_id DESC LIMIT 1` 41 templateCreateQueueMetadataQueryV2 = `INSERT INTO queues (queue_type, queue_name, metadata_payload, metadata_encoding) VALUES(:queue_type, :queue_name, :metadata_payload, :metadata_encoding)` 42 templateUpdateQueueMetadataQueryV2 = `UPDATE queues SET metadata_payload = :metadata_payload, metadata_encoding = :metadata_encoding WHERE queue_type = :queue_type and queue_name = :queue_name` 43 templateGetQueueMetadataQueryV2 = `SELECT metadata_payload, metadata_encoding from queues WHERE queue_type=? and queue_name=?` 44 templateGetNameFromQueueMetadataV2 = `SELECT queue_type, queue_name, metadata_payload, metadata_encoding from queues WHERE queue_type=? LIMIT ? OFFSET ?` 45 ) 46 47 func (sdb *db) InsertIntoQueueV2Metadata(ctx context.Context, row *sqlplugin.QueueV2MetadataRow) (sql.Result, error) { 48 return sdb.conn.NamedExecContext(ctx, 49 templateCreateQueueMetadataQueryV2, 50 row, 51 ) 52 } 53 func (sdb *db) UpdateQueueV2Metadata(ctx context.Context, row *sqlplugin.QueueV2MetadataRow) (sql.Result, error) { 54 return sdb.conn.NamedExecContext(ctx, 55 templateUpdateQueueMetadataQueryV2, 56 row, 57 ) 58 } 59 func (sdb *db) SelectFromQueueV2Metadata(ctx context.Context, filter sqlplugin.QueueV2MetadataFilter) (*sqlplugin.QueueV2MetadataRow, error) { 60 var row sqlplugin.QueueV2MetadataRow 61 err := sdb.conn.GetContext(ctx, 62 &row, 63 templateGetQueueMetadataQueryV2, 64 filter.QueueType, 65 filter.QueueName, 66 ) 67 if err != nil { 68 return nil, err 69 } 70 return &row, nil 71 } 72 func (sdb *db) SelectFromQueueV2MetadataForUpdate(ctx context.Context, filter sqlplugin.QueueV2MetadataFilter) (*sqlplugin.QueueV2MetadataRow, error) { 73 // sqlite does not have FOR UPDATE clause. Calling SelectFromQueueV2Metadata() itself. 74 return sdb.SelectFromQueueV2Metadata(ctx, filter) 75 } 76 func (sdb *db) SelectNameFromQueueV2Metadata(ctx context.Context, filter sqlplugin.QueueV2MetadataTypeFilter) ([]sqlplugin.QueueV2MetadataRow, error) { 77 var rows []sqlplugin.QueueV2MetadataRow 78 err := sdb.conn.SelectContext(ctx, 79 &rows, 80 templateGetNameFromQueueMetadataV2, 81 filter.QueueType, 82 filter.PageSize, 83 filter.PageOffset, 84 ) 85 if err != nil { 86 return nil, err 87 } 88 return rows, nil 89 } 90 func (sdb *db) InsertIntoQueueV2Messages(ctx context.Context, row []sqlplugin.QueueV2MessageRow) (sql.Result, error) { 91 return sdb.conn.NamedExecContext(ctx, 92 templateEnqueueMessageQueryV2, 93 row, 94 ) 95 } 96 func (sdb *db) RangeSelectFromQueueV2Messages(ctx context.Context, filter sqlplugin.QueueV2MessagesFilter) ([]sqlplugin.QueueV2MessageRow, error) { 97 var rows []sqlplugin.QueueV2MessageRow 98 err := sdb.conn.SelectContext(ctx, 99 &rows, 100 templateGetMessagesQueryV2, 101 filter.QueueType, 102 filter.QueueName, 103 filter.Partition, 104 filter.MinMessageID, 105 filter.PageSize, 106 ) 107 return rows, err 108 } 109 func (sdb *db) RangeDeleteFromQueueV2Messages(ctx context.Context, filter sqlplugin.QueueV2MessagesFilter) (sql.Result, error) { 110 return sdb.conn.ExecContext(ctx, 111 templateRangeDeleteMessagesQueryV2, 112 filter.QueueType, 113 filter.QueueName, 114 filter.Partition, 115 filter.MinMessageID, 116 filter.MaxMessageID, 117 ) 118 } 119 120 func (sdb *db) GetLastEnqueuedMessageIDForUpdateV2(ctx context.Context, filter sqlplugin.QueueV2Filter) (int64, error) { 121 var lastMessageID int64 122 err := sdb.conn.GetContext(ctx, 123 &lastMessageID, 124 templateGetLastMessageIDQueryV2, 125 filter.QueueType, 126 filter.QueueName, 127 filter.Partition, 128 ) 129 return lastMessageID, err 130 }