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 +4 -4
- data/lib/postqueue/item/inserter.rb +38 -10
- data/lib/postqueue/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ad1cc078033356ab3276d5360da442f64c42fc6b
|
4
|
+
data.tar.gz: a79c30543676b7c62679a99d1f9c4380979b9b7a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
#
|
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
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
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
|
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
|
28
|
-
extend
|
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
|
data/lib/postqueue/version.rb
CHANGED