Class: Atig::Db::Statuses
- Inherits:
-
Object
- Object
- Atig::Db::Statuses
- Includes:
- Listenable, Transaction
- Defined in:
- lib/atig/db/statuses.rb
Constant Summary collapse
- Size =
400
Constants included from Listenable
Instance Method Summary collapse
- #add(opt) ⇒ Object
- #cleanup ⇒ Object
- #find_all(opt = {}) ⇒ Object
- #find_by_id(id) ⇒ Object
- #find_by_screen_name(name, opt = {}) ⇒ Object
- #find_by_sid(tid) ⇒ Object
- #find_by_status_id(id) ⇒ Object
- #find_by_tid(tid) ⇒ Object
- #find_by_user(user, opt = {}) ⇒ Object
-
#initialize(name) ⇒ Statuses
constructor
A new instance of Statuses.
- #remove_by_id(id) ⇒ Object
Methods included from Transaction
Methods included from ExceptionUtil
Methods included from Listenable
Constructor Details
#initialize(name) ⇒ Statuses
Returns a new instance of Statuses.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/atig/db/statuses.rb', line 21 def initialize(name) @db = Sql.new name @roman = Roman.new unless File.exist? name then @db.execute do|db| db.execute %{create table status ( id integer primary key, status_id text, tid text, sid text, screen_name text, user_id text, created_at integer, data blob);} db.execute %{create table id ( id integer primary key, screen_name text, count integer);} # thx to @L_star # http://d.hatena.ne.jp/mzp/20100407#c db.execute_batch %{ create index status_createdat on status(created_at); create index status_sid on status(sid); create index status_statusid on status(status_id); create index status_tid on status(tid); create index status_userid on status(user_id); create index status_id on status(id); create index id_id on id(id); } end end end |
Instance Method Details
#add(opt) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/atig/db/statuses.rb', line 58 def add(opt) @db.execute do|db| id = opt[:status].id return unless db.execute(%{SELECT id FROM status WHERE status_id = ?}, id).empty? screen_name = opt[:user].screen_name sum = db.get_first_value("SELECT sum(count) FROM id").to_i count = db.get_first_value("SELECT count FROM id WHERE screen_name = ?", screen_name).to_i entry = OpenStruct.new opt.merge(:tid => @roman.make(sum), :sid => "#{screen_name}:#{@roman.make(count)}") db.execute(%{INSERT INTO status VALUES(NULL, :id, :tid, :sid, :screen_name, :user_id, :created_at, :data)}, :id => id, :tid => entry.tid, :sid => entry.sid, :screen_name => screen_name, :user_id => opt[:user].id, :created_at => Time.parse(opt[:status].created_at).to_i, :data => @db.dump(entry)) if count == 0 then db.execute("INSERT INTO id VALUES(NULL,?,?)", screen_name, 1) else db.execute("UPDATE id SET count = ? WHERE screen_name = ?", count + 1, screen_name) end notify entry end end |
#cleanup ⇒ Object
120 121 122 123 124 125 126 127 128 |
# File 'lib/atig/db/statuses.rb', line 120 def cleanup @db.execute do|db| created_at = db.execute("SELECT created_at FROM status ORDER BY created_at DESC LIMIT 1 OFFSET ?", Size-1) unless created_at.empty? then db.execute "DELETE FROM status WHERE created_at < ?", created_at.first end db.execute "VACUUM status" end end |
#find_all(opt = {}) ⇒ Object
86 87 88 |
# File 'lib/atig/db/statuses.rb', line 86 def find_all(opt={}) find '1', 1, opt end |
#find_by_id(id) ⇒ Object
110 111 112 |
# File 'lib/atig/db/statuses.rb', line 110 def find_by_id(id) find('id', id).first end |
#find_by_screen_name(name, opt = {}) ⇒ Object
90 91 92 |
# File 'lib/atig/db/statuses.rb', line 90 def find_by_screen_name(name, opt={}) find 'screen_name',name, opt end |
#find_by_sid(tid) ⇒ Object
102 103 104 |
# File 'lib/atig/db/statuses.rb', line 102 def find_by_sid(tid) find('sid', tid).first end |
#find_by_status_id(id) ⇒ Object
106 107 108 |
# File 'lib/atig/db/statuses.rb', line 106 def find_by_status_id(id) find('status_id', id).first end |
#find_by_tid(tid) ⇒ Object
98 99 100 |
# File 'lib/atig/db/statuses.rb', line 98 def find_by_tid(tid) find('tid', tid).first end |
#find_by_user(user, opt = {}) ⇒ Object
94 95 96 |
# File 'lib/atig/db/statuses.rb', line 94 def find_by_user(user, opt={}) find 'user_id', user.id, opt end |
#remove_by_id(id) ⇒ Object
114 115 116 117 118 |
# File 'lib/atig/db/statuses.rb', line 114 def remove_by_id(id) @db.execute do|db| db.execute "DELETE FROM status WHERE id = ?",id end end |