]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/commitdiff
journal: started implementing postgres storage
authorMatthias Hecker <apoc@geekosphere.org>
Sun, 14 Jun 2015 01:01:25 +0000 (03:01 +0200)
committerMatthias Hecker <apoc@geekosphere.org>
Sun, 14 Jun 2015 01:01:25 +0000 (03:01 +0200)
lib/rbot/journal.rb
lib/rbot/journal/postgres.rb [new file with mode: 0644]
test/test_journal.rb

index 09ee7369a5a0acd485796982359fd32a87c1e0d4..27565d061530659bde0b5bff55eeaffbaac41e81 100644 (file)
@@ -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 (file)
index 0000000..85514fb
--- /dev/null
@@ -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
index cee8da0f46b24d121289f28a0a95a6249aa4b4c2..1e55f54b280fc7f36d8958102954b9322912694d 100644 (file)
@@ -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
+