postqueue 0.5.6 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dcb53b2cad7afd5da516c06e39f62b7c5f053247
4
- data.tar.gz: 4027f325048b199d891323d8b48c7778d34b7418
3
+ metadata.gz: ad1cc078033356ab3276d5360da442f64c42fc6b
4
+ data.tar.gz: a79c30543676b7c62679a99d1f9c4380979b9b7a
5
5
  SHA512:
6
- metadata.gz: 3ed2e40245f59ccda5b82551ed868dc01d605a8bbfc12238205de689e1c06640751baed2dc334d2f81420be430d9f81fcbeba195037b552f9aa8d76ed17154bb
7
- data.tar.gz: aaaa025a26e7b5e1129a492199cbbc18b9a691c2567e620fcb96f6c9160389e2e7bff98e3a9cb0cd44073f638f33b1b0ce60ec106fa8394092be09ab9de87f19
6
+ metadata.gz: 09b7f4b6c07b3eaf17acef7863ee6f940f0f67906d8f4cb112ffad19a46dd1fba7ea9c678f6c5dc418d1170bfa5be67a0600bff1b77454113788ac913ae23b88
7
+ data.tar.gz: 02b7c4d09582a202a56d12e7fd810da1f1f5334cbd726eb4db2d15cd8fc06b19aeb992c144ae61f050cc50dd46928c901526d9ee40ed5f1e8f804b2925e8bcdf
@@ -2,7 +2,10 @@ require "active_record"
2
2
 
3
3
  module Postqueue
4
4
  #
5
- # An item class.
5
+ # Postqueue::Item inserter modules.
6
+ #
7
+ # This source file provides multiple implementations to insert Postqueue::Items.
8
+ # Which one will be used depends on the "extend XXXInserter" line below.
6
9
  class Item < ActiveRecord::Base
7
10
  module ActiveRecordInserter
8
11
  def insert_item(op:, entity_id:)
@@ -11,20 +14,45 @@ module Postqueue
11
14
  end
12
15
 
13
16
  module RawInserter
14
- def prepared_inserter_statement
15
- @prepared_inserter_statement ||= begin
16
- name = "postqueue-insert-{table_name}-#{Thread.current.object_id}"
17
- connection.raw_connection.prepare(name, "INSERT INTO #{table_name}(op, entity_id) VALUES($1, $2)")
18
- name
19
- end
17
+ def insert_sql
18
+ "INSERT INTO #{table_name}(op, entity_id) VALUES($1, $2)"
19
+ end
20
+
21
+ def insert_item(op:, entity_id:)
22
+ connection.raw_connection.exec_params(insert_sql, [op, entity_id])
23
+ end
24
+ end
25
+
26
+ module PreparedRawInserter
27
+ def insert_sql
28
+ "INSERT INTO #{table_name}(op, entity_id) VALUES($1, $2)"
29
+ end
30
+
31
+ def prepared_inserter_statement(raw_connection)
32
+ @prepared_inserter_statements ||= {}
33
+
34
+ # a prepared connection is PER DATABASE CONNECTION. It is not shared across
35
+ # connections, and it is not per thread, since a Thread might use different
36
+ # connections during its lifetime.
37
+ @prepared_inserter_statements[raw_connection.object_id] ||= create_prepared_inserter_statement(raw_connection)
38
+ end
39
+
40
+ # prepares the INSERT statement, and returns its name
41
+ def create_prepared_inserter_statement(raw_connection)
42
+ name = "postqueue-insert-#{table_name}-#{raw_connection.object_id}"
43
+ raw_connection.prepare(name, insert_sql)
44
+ name
20
45
  end
21
46
 
22
47
  def insert_item(op:, entity_id:)
23
- connection.raw_connection.exec_prepared(prepared_inserter_statement, [op, entity_id])
48
+ raw_connection = connection.raw_connection
49
+ statement_name = prepared_inserter_statement(raw_connection)
50
+ raw_connection.exec_prepared(statement_name, [op, entity_id])
24
51
  end
25
52
  end
26
53
 
27
- # extend RawInserter
28
- extend ActiveRecordInserter
54
+ # extend ActiveRecordInserter # 600µs per item
55
+ extend RawInserter # 100µs per item
56
+ # extend PreparedRawInserter # 50µs per item
29
57
  end
30
58
  end
@@ -1,3 +1,3 @@
1
1
  module Postqueue
2
- VERSION = "0.5.6"
2
+ VERSION = "0.6.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: postqueue
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.6
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - radiospiel