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.
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 57 |
# File 'lib/atig/db/statuses.rb', line 22 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
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 85 |
# File 'lib/atig/db/statuses.rb', line 59 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
121 122 123 124 125 126 127 128 129 |
# File 'lib/atig/db/statuses.rb', line 121 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
87 88 89 |
# File 'lib/atig/db/statuses.rb', line 87 def find_all(opt={}) find '1', 1, opt end |
#find_by_id(id) ⇒ Object
111 112 113 |
# File 'lib/atig/db/statuses.rb', line 111 def find_by_id(id) find('id', id).first end |
#find_by_screen_name(name, opt = {}) ⇒ Object
91 92 93 |
# File 'lib/atig/db/statuses.rb', line 91 def find_by_screen_name(name, opt={}) find 'screen_name',name, opt end |
#find_by_sid(tid) ⇒ Object
103 104 105 |
# File 'lib/atig/db/statuses.rb', line 103 def find_by_sid(tid) find('sid', tid).first end |
#find_by_status_id(id) ⇒ Object
107 108 109 |
# File 'lib/atig/db/statuses.rb', line 107 def find_by_status_id(id) find('status_id', id).first end |
#find_by_tid(tid) ⇒ Object
99 100 101 |
# File 'lib/atig/db/statuses.rb', line 99 def find_by_tid(tid) find('tid', tid).first end |
#find_by_user(user, opt = {}) ⇒ Object
95 96 97 |
# File 'lib/atig/db/statuses.rb', line 95 def find_by_user(user, opt={}) find 'user_id', user.id, opt end |
#remove_by_id(id) ⇒ Object
115 116 117 118 119 |
# File 'lib/atig/db/statuses.rb', line 115 def remove_by_id(id) @db.execute do|db| db.execute "DELETE FROM status WHERE id = ?",id end end |