Class: Atig::Db::Followings

Inherits:
Object
  • Object
show all
Includes:
Listenable, Transaction
Defined in:
lib/atig/db/followings.rb

Constant Summary

Constants included from Listenable

Listenable::SingleThread

Instance Method Summary collapse

Methods included from Transaction

#debug, #init, #transaction

Methods included from ExceptionUtil

daemon, safe

Methods included from Listenable

#listen

Constructor Details

#initialize(name) ⇒ Followings

Returns a new instance of Followings.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/atig/db/followings.rb', line 13

def initialize(name)
  @db = Sql.new name

  unless File.exist? name then
    @db.execute do|db|
      db.execute %{create table users (
                    id integer primary key,
                    screen_name text,
                    user_id text,
                    protected bool,
                    only bool,
                    data blob);}
      db.execute %{
        create index users_screen on users (screen_name);
      }
    end
  end

  @users = []
  @on_invalidated = lambda{}
end

Instance Method Details

#bool(b) ⇒ Object



73
74
75
# File 'lib/atig/db/followings.rb', line 73

def bool(b)
  if b then 1 else 0 end
end

#empty?Boolean

Returns:

  • (Boolean)


41
42
43
44
45
# File 'lib/atig/db/followings.rb', line 41

def empty?
  @db.execute do|db|
    db.get_first_value('SELECT * FROM users LIMIT 1') == nil
  end
end

#exists?(db, templ, *args) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/atig/db/followings.rb', line 63

def exists?(db, templ, *args)
  db.get_first_value("SELECT * FROM users WHERE #{templ} LIMIT 1",*args) != nil
end

#find_by_screen_name(name) ⇒ Object



122
123
124
125
126
# File 'lib/atig/db/followings.rb', line 122

def find_by_screen_name(name)
  @db.execute do|db|
    @db.load db.get_first_value('SELECT data FROM users WHERE screen_name = ? LIMIT 1', name)
  end
end

#include?(user) ⇒ Boolean

Returns:

  • (Boolean)


128
129
130
131
132
# File 'lib/atig/db/followings.rb', line 128

def include?(user)
  @db.execute do|db|
    exists? db,'user_id = ?', user.id
  end
end

#invalidateObject



47
48
49
# File 'lib/atig/db/followings.rb', line 47

def invalidate
  @on_invalidated.call
end

#may_notify(mode, xs) ⇒ Object



67
68
69
70
71
# File 'lib/atig/db/followings.rb', line 67

def may_notify(mode, xs)
  unless xs.empty? then
    notify mode, xs
  end
end

#on_invalidated(&f) ⇒ Object



51
52
53
# File 'lib/atig/db/followings.rb', line 51

def on_invalidated(&f)
  @on_invalidated = f
end

#sizeObject



35
36
37
38
39
# File 'lib/atig/db/followings.rb', line 35

def size
  @db.execute do|db|
    db.get_first_value('SELECT COUNT(*) FROM users').to_i
  end
end

#update(users) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/atig/db/followings.rb', line 77

def update(users)
  @db.execute do|db|
    may_notify :join, users.select{|u|
      not exists?(db,
                  "screen_name = ?",
                  u.screen_name)
    }

    names = users.map{|u| u.screen_name.inspect }.join(",")
    parts =
    may_notify :part, db.execute(%{SELECT screen_name,data FROM users
                                   WHERE screen_name NOT IN (#{names})}).map{|_,data|
      @db.load(data)
    }
    db.execute(%{DELETE FROM users
                 WHERE screen_name NOT IN (#{names})})

    may_notify :mode, users.select{|u|
      exists?(db,
              "screen_name = ? AND (protected != ? OR only != ?)",
              u.screen_name, bool(u.protected), bool(u.only))
    }

    users.each do|user|
      id = db.get_first_value('SELECT id FROM users WHERE user_id = ? LIMIT 1', user.id)
      if id then
        db.execute("UPDATE users SET screen_name = ?, protected = ?, only = ?, data = ? WHERE id = ?",
                   user.screen_name,
                   bool(user.protected),
                   bool(user.only),
                   @db.dump(user),
                   id)
      else
        db.execute("INSERT INTO users
                    VALUES(NULL, :screen_name, :user_id, :protected, :only, :data)",
                   :screen_name => user.screen_name,
                   :user_id     => user.id,
                   :protected   => bool(user.protected),
                   :only        => bool(user.only),
                   :data        => @db.dump(user))
      end
    end
  end
end

#usersObject



55
56
57
58
59
60
61
# File 'lib/atig/db/followings.rb', line 55

def users
  @db.execute{|db|
    db.execute("SELECT data FROM users").map{|data|
      @db.load data[0]
    }
  }
end