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

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

Defined Under Namespace

Classes: Cleaner, Stats

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.



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

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

#cleanerObject



171
172
173
174
175
176
177
178
# File 'lib/graphql/anycable/postgresql_store/store.rb', line 171

def cleaner
  @cleaner ||= Cleaner.new(
    connection_provider: ->(&block) { with_connection(&block) },
    subscriptions_table: subscriptions_table,
    events_table: events_table,
    channels_table: channels_table
  )
end

#delete_channel_subscriptions(channel_id) ⇒ Object



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

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



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

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



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

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



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

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



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

def stats(scan_count:, include_subscriptions: false)
  Stats.new(
    connection_provider: ->(&block) { with_connection(&block) },
    subscriptions_table: subscriptions_table,
    events_table: events_table,
    channels_table: channels_table,
    scan_count: scan_count,
    include_subscriptions: include_subscriptions
  ).collect
end

#stream_for(fingerprint) ⇒ Object



24
25
26
# File 'lib/graphql/anycable/postgresql_store/store.rb', line 24

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

#subscription_exists?(subscription_id) ⇒ Boolean

Returns:

  • (Boolean)


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

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



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

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



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
121
122
# File 'lib/graphql/anycable/postgresql_store/store.rb', line 77

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