Files
ubicloud/model/semaphore.rb
Jeremy Evans 106a52cbef Make Semaphore.incr use a single query
Previously, it would usually use 2 queries if inside a transaction,
or 4 queries if outside a transaction.  This replaces the UPDATE
then INSERT query approach with an INSERT SELECT (UPDATE RETURNING)
approach in a single query.  As this is a single query and not
multiple queries, it does not need a transaction.  If the UPDATE
does not update a row, then no rows will be inserted.

This approach also opens up the ability to create multiple
semaphores in a single query, if the first argument is an array
or a dataset.  That will be used in an upcoming commit.

This changes the return value of Semaphore.incr when provided
a single id from returning a Semaphore instance to returning
the Semaphore uuid.  Only SemSnap#incr cares about the return
value, so update that to fetch the Semaphore.
2025-06-04 05:26:37 +09:00

38 lines
944 B
Ruby

# frozen_string_literal: true
require_relative "../model"
class Semaphore < Sequel::Model
plugin ResourceMethods
def self.incr(id, name)
case name
when Symbol
name = name.to_s
when String
# nothing
else
raise "invalid name given to Semaphore.incr: #{name.inspect}"
end
with(:updated_strand,
Strand
.where(id:)
.returning(:id)
.with_sql(:update_sql, schedule: Sequel::CURRENT_TIMESTAMP))
.insert([:id, :strand_id, :name],
DB[:updated_strand].select(Sequel[:gen_timestamp_ubid_uuid].function(820), :id, name))
end
end
# Table: semaphore
# Columns:
# id | uuid | PRIMARY KEY
# strand_id | uuid | NOT NULL
# name | text | NOT NULL
# Indexes:
# semaphore_pkey | PRIMARY KEY btree (id)
# semaphore_strand_id_index | btree (strand_id)
# Foreign key constraints:
# semaphore_strand_id_fkey | (strand_id) REFERENCES strand(id)