class Feedcellar::GroongaDatabase

Public Class Methods

new() click to toggle source
# File lib/feedcellar/groonga_database.rb, line 24
def initialize
  @database = nil
end

Public Instance Methods

add(resource_key, title, link, description, date) click to toggle source
# File lib/feedcellar/groonga_database.rb, line 51
def add(resource_key, title, link, description, date)
  columns = {
    :resource    => resources[resource_key],
    :title       => title,
    :link        => link,
    :description => description,
    :date        => date,
  }
  if date
    if feeds.have_column?(:year)
      columns[:year] = date.year
    end
    if feeds.have_column?(:month)
      columns[:month] = date.month
    end
    if feeds.have_column?(:day)
      columns[:day] = date.day
    end
    if feeds.have_column?(:wday)
      columns[:wday] = Date.new(date.year, date.month, date.day).wday
    end
  end
  feeds.add(link, columns)
end
close() click to toggle source
# File lib/feedcellar/groonga_database.rb, line 111
def close
  @database.close
  @database = nil
end
closed?() click to toggle source
# File lib/feedcellar/groonga_database.rb, line 116
def closed?
  @database.nil? or @database.closed?
end
delete(id_or_key_or_conditions) click to toggle source
# File lib/feedcellar/groonga_database.rb, line 76
def delete(id_or_key_or_conditions)
  if id_or_key_or_conditions.is_a?(Integer)
    id = id_or_key_or_conditions
    feeds.delete(id, :id => true)
  elsif id_or_key_or_conditions.is_a?(String)
    key = id_or_key_or_conditions
    feeds.delete(key)
  elsif id_or_key_or_conditions.is_a?(Hash)
    conditions = id_or_key_or_conditions
    feeds.delete do |record|
      expression_builder = nil
      conditions.each do |key, value|
        case key
        when :resource_key
          record &= (record.resource._key == value)
        else
          raise ArgumentError,
                "Not supported condition: <#{key}>"
        end
      end
      record
    end
  else
    raise ArgumentError,
          "Not supported type: <#{id_or_conditions.class}>"
  end
end
dump() click to toggle source
# File lib/feedcellar/groonga_database.rb, line 128
def dump
  Groonga::DatabaseDumper.dump
end
feeds() click to toggle source
# File lib/feedcellar/groonga_database.rb, line 124
def feeds
  @feeds ||= Groonga["Feeds"]
end
open(base_path, encoding=:utf8) { |self| ... } click to toggle source
# File lib/feedcellar/groonga_database.rb, line 28
def open(base_path, encoding=:utf8)
  reset_context(encoding)
  path = File.join(base_path, "feedcellar.db")
  if File.exist?(path)
    @database = Groonga::Database.open(path)
    populate_schema
  else
    FileUtils.mkdir_p(base_path)
    populate(path)
  end
  if block_given?
    begin
      yield(self)
    ensure
      close unless closed?
    end
  end
end
register(key, attributes) click to toggle source
# File lib/feedcellar/groonga_database.rb, line 47
def register(key, attributes)
  resources.add(key, attributes)
end
resources() click to toggle source
# File lib/feedcellar/groonga_database.rb, line 120
def resources
  @resources ||= Groonga["Resources"]
end
unregister(title_or_url) click to toggle source
# File lib/feedcellar/groonga_database.rb, line 104
def unregister(title_or_url)
  resources.delete do |record|
    (record.title == title_or_url) |
    (record.xmlUrl == title_or_url)
  end
end

Private Instance Methods

populate(path) click to toggle source
# File lib/feedcellar/groonga_database.rb, line 138
def populate(path)
  @database = Groonga::Database.create(:path => path)
  populate_schema
end
populate_schema() click to toggle source
# File lib/feedcellar/groonga_database.rb, line 143
def populate_schema
  Groonga::Schema.define do |schema|
    schema.create_table("Resources", :type => :hash) do |table|
      table.text("text")
      table.short_text("isComment")
      table.short_text("isBreakpoint")
      table.short_text("created")
      table.short_text("category")
      table.text("description")
      table.short_text("url")
      table.short_text("htmlUrl")
      table.short_text("xmlUrl")
      table.short_text("title")
      table.short_text("version")
      table.short_text("language")
    end

    schema.create_table("Feeds", :type => :hash) do |table|
      table.reference("resource", "Resources")
      table.short_text("title")
      table.short_text("link")
      table.text("description")
      table.integer16("year")
      table.integer8("month")
      table.integer8("day")
      table.integer8("wday")
      table.time("date")
    end

    schema.create_table("Terms",
                        :type => :patricia_trie,
                        :normalizer => "NormalizerAuto",
                        :default_tokenizer => "TokenBigram") do |table|
      table.index("Feeds.title")
      table.index("Feeds.description")
    end
  end
end
reset_context(encoding) click to toggle source
# File lib/feedcellar/groonga_database.rb, line 133
def reset_context(encoding)
  Groonga::Context.default_options = {:encoding => encoding}
  Groonga::Context.default = nil
end