Class: GraphQL::AnyCable::PostgreSQLStore::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/anycable/postgresql_store/store.rb

Constant Summary collapse

SUBSCRIPTIONS_PREFIX =
"subscriptions:"

Instance Method Summary collapse

Constructor Details

#initialize(config: PostgreSQLStore.config, graphql_config: GraphQL::AnyCable.config) ⇒ Store

Returns a new instance of Store.



11
12
13
14
15
16
17
18
19
20
# File 'lib/graphql/anycable/postgresql_store/store.rb', line 11

def initialize(config: PostgreSQLStore.config, graphql_config: GraphQL::AnyCable.config)
  load_pg!

  @config = config
  @graphql_config = graphql_config
  @mutex = Mutex.new
  @subscriptions_table = quote_table_name(config.subscriptions_table)
  @events_table = quote_table_name(config.subscription_events_table)
  @channels_table = quote_table_name(config.channel_subscriptions_table)
end

Instance Method Details

#delete_channel_subscriptions(channel_id) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/graphql/anycable/postgresql_store/store.rb', line 136

def delete_channel_subscriptions(channel_id)
  with_connection do |conn|
    transaction(conn) do
      conn.exec_params(<<~SQL, [channel_id])
        DELETE FROM #{subscriptions_table}
        WHERE id IN (
          SELECT subscription_id
          FROM #{channels_table}
          WHERE channel_id = $1
        )
      SQL
      conn.exec_params("DELETE FROM #{channels_table} WHERE channel_id = $1", [channel_id])
    end
  end
end

#delete_subscription(subscription_id) ⇒ Object



152
153
154
155
156
# File 'lib/graphql/anycable/postgresql_store/store.rb', line 152

def delete_subscription(subscription_id)
  with_connection do |conn|
    conn.exec_params("DELETE FROM #{subscriptions_table} WHERE id = $1", [subscription_id])
  end
end

#fingerprints_for_topic(topic) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/graphql/anycable/postgresql_store/store.rb', line 26

def fingerprints_for_topic(topic)
  with_connection do |conn|
    conn.exec_params(<<~SQL, [topic]).map { |row| row.fetch("fingerprint") }
      SELECT events.fingerprint
      FROM #{events_table} events
      INNER JOIN #{subscriptions_table} subscriptions
        ON subscriptions.id = events.subscription_id
      WHERE events.topic = $1
        AND (subscriptions.expires_at IS NULL OR subscriptions.expires_at > CURRENT_TIMESTAMP)
      GROUP BY events.fingerprint
      ORDER BY COUNT(*) ASC, MIN(events.created_at) ASC
    SQL
  end
end

#read_subscription(subscription_id) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/graphql/anycable/postgresql_store/store.rb', line 122

def read_subscription(subscription_id)
  with_connection do |conn|
    result = conn.exec_params(<<~SQL, [subscription_id])
      SELECT query_string, variables, context, operation_name
      FROM #{subscriptions_table}
      WHERE id = $1
        AND (expires_at IS NULL OR expires_at > CURRENT_TIMESTAMP)
    SQL
    return if result.ntuples.zero?

    result.first.transform_keys(&:to_sym)
  end
end

#stats(scan_count:, include_subscriptions: false) ⇒ Object

Raises:

  • (ArgumentError)


158
159
160
161
162
163
164
165
166
167
168
# File 'lib/graphql/anycable/postgresql_store/store.rb', line 158

def stats(scan_count:, include_subscriptions: false)
  # PostgreSQL uses aggregate queries rather than key scans; scan_count
  # is accepted to match graphql-anycable's store stats interface.
  raise ArgumentError, "scan_count must be positive" if scan_count.to_i <= 0

  with_connection do |conn|
    result = {total: total_stats(conn)}
    result[:subscriptions] = subscription_stats(conn) if include_subscriptions
    result
  end
end

#stream_for(fingerprint) ⇒ Object



22
23
24
# File 'lib/graphql/anycable/postgresql_store/store.rb', line 22

def stream_for(fingerprint)
  "#{graphql_config.redis_prefix}-#{SUBSCRIPTIONS_PREFIX}#{fingerprint}"
end

#subscription_exists?(subscription_id) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/graphql/anycable/postgresql_store/store.rb', line 62

def subscription_exists?(subscription_id)
  with_connection do |conn|
    conn.exec_params(<<~SQL, [subscription_id]).getvalue(0, 0) == "t"
      SELECT EXISTS (
        SELECT 1
        FROM #{subscriptions_table}
        WHERE id = $1
          AND (expires_at IS NULL OR expires_at > CURRENT_TIMESTAMP)
      )
    SQL
  end
end

#subscription_ids_for_fingerprints(fingerprints) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/graphql/anycable/postgresql_store/store.rb', line 41

def subscription_ids_for_fingerprints(fingerprints)
  result = fingerprints.to_h { |fingerprint| [fingerprint, []] }
  return result if fingerprints.empty?

  with_connection do |conn|
    conn.exec_params(<<~SQL, fingerprints).each do |row|
      SELECT events.fingerprint, events.subscription_id
      FROM #{events_table} events
      INNER JOIN #{subscriptions_table} subscriptions
        ON subscriptions.id = events.subscription_id
      WHERE events.fingerprint IN (#{placeholders(fingerprints.length)})
        AND (subscriptions.expires_at IS NULL OR subscriptions.expires_at > CURRENT_TIMESTAMP)
      ORDER BY events.fingerprint ASC, events.created_at ASC
    SQL
      result[row.fetch("fingerprint")] << row.fetch("subscription_id")
    end
  end

  result
end

#write_subscription(subscription_id, channel_id:, data:, events:, expiration_seconds:) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/graphql/anycable/postgresql_store/store.rb', line 75

def write_subscription(subscription_id, channel_id:, data:, events:, expiration_seconds:)
  expires_at = expiration_seconds ? (Time.now.utc + expiration_seconds).iso8601(6) : nil
  subscription_params = [
    subscription_id,
    data.fetch(:query_string),
    data.fetch(:variables),
    data.fetch(:context),
    data.fetch(:operation_name),
    data.fetch(:events),
    expires_at
  ]

  with_connection do |conn|
    transaction(conn) do
      conn.exec_params(<<~SQL, subscription_params)
        INSERT INTO #{subscriptions_table}
          (id, query_string, variables, context, operation_name, events, expires_at, created_at, updated_at)
        VALUES ($1, $2, $3, $4, $5, $6, $7, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
        ON CONFLICT (id) DO UPDATE SET
          query_string = EXCLUDED.query_string,
          variables = EXCLUDED.variables,
          context = EXCLUDED.context,
          operation_name = EXCLUDED.operation_name,
          events = EXCLUDED.events,
          expires_at = EXCLUDED.expires_at,
          updated_at = CURRENT_TIMESTAMP
      SQL

      conn.exec_params("DELETE FROM #{events_table} WHERE subscription_id = $1", [subscription_id])
      conn.exec_params("DELETE FROM #{channels_table} WHERE subscription_id = $1", [subscription_id])
      conn.exec_params(<<~SQL, [channel_id, subscription_id])
        INSERT INTO #{channels_table} (channel_id, subscription_id, created_at)
        VALUES ($1, $2, CURRENT_TIMESTAMP)
        ON CONFLICT (channel_id, subscription_id) DO NOTHING
      SQL

      events.each do |event|
        conn.exec_params(<<~SQL, [subscription_id, event.topic, event.fingerprint])
          INSERT INTO #{events_table} (subscription_id, topic, fingerprint, created_at)
          VALUES ($1, $2, $3, CURRENT_TIMESTAMP)
          ON CONFLICT (subscription_id, topic, fingerprint) DO NOTHING
        SQL
      end
    end
  end
end