summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Hecker <apoc@geekosphere.org>2015-06-14 03:01:25 +0200
committerMatthias Hecker <apoc@geekosphere.org>2015-06-14 03:01:25 +0200
commit830c8d7d6be5d0bda2df05ff5a2383362e39f8d4 (patch)
tree6c131322968b0ea9f351ac7537f082613a8bbad4
parentd864b0348f25d845fa312cedfd5011b2d25022dc (diff)
journal: started implementing postgres storage
-rw-r--r--lib/rbot/journal.rb47
-rw-r--r--lib/rbot/journal/postgres.rb39
-rw-r--r--test/test_journal.rb13
3 files changed, 99 insertions, 0 deletions
diff --git a/lib/rbot/journal.rb b/lib/rbot/journal.rb
index 09ee7369..27565d06 100644
--- a/lib/rbot/journal.rb
+++ b/lib/rbot/journal.rb
@@ -22,10 +22,21 @@ module Journal
=end
+ Config.register Config::StringValue.new('journal.storage',
+ :default => nil,
+ :requires_restart => true,
+ :desc => 'storage engine used by the journal')
+ Config.register Config::StringValue.new('journal.storage.uri',
+ :default => nil,
+ :requires_restart => true,
+ :desc => 'storage database uri')
+
class InvalidJournalMessage < StandardError
end
class ConsumeInterrupt < StandardError
end
+ class StorageError < StandardError
+ end
class JournalMessage
# a unique identification of this message
@@ -74,6 +85,34 @@ module Journal
end
end
+ module Storage
+ class AbstractStorage
+ # intializes/opens a new storage connection
+ def initialize(opts={})
+ end
+
+ # inserts a message in storage
+ def insert(message)
+ end
+
+ # creates/ensures a index exists on the payload specified by key
+ def ensure_index(key)
+ end
+
+ # returns a array of message instances that match the query
+ def find(query, limit=10, offset=0)
+ end
+
+ # returns the number of messages that match the query
+ def count(query)
+ end
+
+ # delete messages that match the query
+ def delete(query)
+ end
+ end
+ end
+
# Describes a query on journal entries, it is used both to describe
# a subscription aswell as to query persisted messages.
# There two ways to declare a Query instance, using
@@ -226,6 +265,8 @@ module Journal
def initialize(opts={})
# overrides the internal consumer with a block
@consumer = opts[:consumer]
+ # storage backend
+ @storage = opts[:storage]
@queue = Queue.new
# consumer thread:
@thread = Thread.new do
@@ -257,6 +298,12 @@ module Journal
s.block.call(message)
end
end
+
+ @storage.insert(message) if @storage
+ end
+
+ def persists?
+ true if @storage
end
def join
diff --git a/lib/rbot/journal/postgres.rb b/lib/rbot/journal/postgres.rb
new file mode 100644
index 00000000..85514fb2
--- /dev/null
+++ b/lib/rbot/journal/postgres.rb
@@ -0,0 +1,39 @@
+# encoding: UTF-8
+#-- vim:sw=2:et
+#++
+#
+# :title: journal backend for postgresql
+
+require 'pg'
+
+module Irc
+class Bot
+module Journal
+ module Storage
+ class PostgresStorage < AbstractStorage
+ def initialize(opts={})
+ @uri = opts[:uri] || 'postgresql://localhost/rbot_journal'
+ @conn = PG.connect(@uri)
+ @version = @conn.exec('SHOW server_version;')[0]['server_version']
+
+ @version.gsub!(/^(\d+\.\d+)$/, '\1.0')
+ log 'journal storage: postgresql connected to version: ' + @version
+
+ version = @version.split('.')[0,3].join.to_i
+ if version < 930
+ raise StorageError.new(
+ 'PostgreSQL Version too old: %s, supported: >= 9.3' % [@version])
+ end
+ @jsonb = (version >= 940)
+ log 'journal storage: no jsonb support, consider upgrading postgres'
+
+ create_table
+ end
+
+ def create_table
+ end
+ end
+ end
+end # Journal
+end # Bot
+end # Irc
diff --git a/test/test_journal.rb b/test/test_journal.rb
index cee8da0f..1e55f54b 100644
--- a/test/test_journal.rb
+++ b/test/test_journal.rb
@@ -3,6 +3,7 @@ $:.unshift File.join(File.dirname(__FILE__), '../lib')
require 'test/unit'
require 'rbot/ircbot'
require 'rbot/journal'
+require 'rbot/journal/postgres.rb'
class JournalMessageTest < Test::Unit::TestCase
@@ -185,3 +186,15 @@ class JournalBrokerTest < Test::Unit::TestCase
end
+class JournalStorageTest < Test::Unit::TestCase
+
+ include Irc::Bot::Journal
+
+ def test_storage
+ s = Storage::PostgresStorage.new(
+ uri: 'postgresql://apoc:seed42@localhost/rbot_journal')
+ assert_equal(true, true)
+ end
+
+end
+