class Racknga::LogDatabase

This is a log database based on groonga. It is used by Racknga::Middleware::Log.

Normally, purge_old_responses is only used for log maintenance.

Public Class Methods

new(database_path) click to toggle source

@param [String] database_path the path for log database.

# File lib/racknga/log_database.rb, line 30
def initialize(database_path)
  @database_path = database_path
  @context = Groonga::Context.new(:encoding => :none)
  ensure_database
end

Public Instance Methods

close_database() click to toggle source
# File lib/racknga/log_database.rb, line 49
def close_database
  @database.close
end
ensure_database() click to toggle source
# File lib/racknga/log_database.rb, line 40
def ensure_database
  if File.exist?(@database_path)
    @database = Groonga::Database.open(@database_path, :context => @context)
  else
    create_database
  end
  ensure_tables
end
entries() click to toggle source
# File lib/racknga/log_database.rb, line 36
def entries
  @entries ||= @context["Entries"]
end
purge_old_entries(base_time=nil) click to toggle source

Purges old responses. To clear old logs, you should call this method. All records created before base_time are removed.

You can call this method by the different process from your Rack application process. (e.g. cron.) It’s multi process safe.

@param [Time] base_time the oldest record time to be removed. The default value is 1 day ago.

# File lib/racknga/log_database.rb, line 63
def purge_old_entries(base_time=nil)
  base_time ||= Time.now - 60 * 60 * 24
  target_entries = entries.select do |record|
    record.time_stamp < base_time
  end
  target_entries.each do |entry|
    entry.key.delete
  end
end

Private Instance Methods

create_database() click to toggle source
# File lib/racknga/log_database.rb, line 115
def create_database
  FileUtils.mkdir_p(File.dirname(@database_path))
  @database = Groonga::Database.create(:path => @database_path,
                                       :context => @context)
end
create_tables() click to toggle source
# File lib/racknga/log_database.rb, line 74
def create_tables
  Groonga::Schema.define(:context => @context) do |schema|
    schema.create_table("Tags",
                        :type => :patricia_trie,
                        :key_type => "ShortText") do |table|
    end

    schema.create_table("Paths",
                        :type => :patricia_trie,
                        :key_type => "ShortText") do |table|
    end

    schema.create_table("UserAgents",
                        :type => :hash,
                        :key_type => "ShortText") do |table|
    end

    schema.create_table("Entries") do |table|
      table.time("time_stamp")
      table.reference("tag", "Tags")
      table.reference("path", "Paths")
      table.reference("user_agent", "UserAgents")
      table.float("runtime")
      table.float("request_time")
      table.short_text("message", :compress => :zlib)
    end

    schema.change_table("Tags") do |table|
      table.index("Entries.tag")
    end

    schema.change_table("Paths") do |table|
      table.index("Entries.path")
    end

    schema.change_table("UserAgents") do |table|
      table.index("Entries.user_agent")
    end
  end
end
ensure_tables() click to toggle source
# File lib/racknga/log_database.rb, line 121
def ensure_tables
  create_tables
end