-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodels.rb
293 lines (228 loc) · 7.1 KB
/
models.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
57
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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
require 'rubygems'
require 'redis'
require 'singleton'
BOOK_STRING_ATTRS = %w(title isbn description price pages)
class RedisHub < Redis
include Singleton
def initialize
super
end
end
class Book
attr_reader :id
attr_accessor :title, :isbn, :description, :price, :pages, :authors, :topics
def initialize(options = {})
@hub = RedisHub.instance
@id = options[:id].nil? ? @hub.incr('book:next_id') : options[:id]
@title = options[:title] unless options[:title].nil?
@isbn = options[:isbn] unless options[:isbn].nil?
@description = options[:description] unless options[:description].nil?
@price = options[:price] unless options[:price].nil?
@pages = options[:pages] unless options[:pages].nil?
@authors = options[:authors] unless options[:authors].nil?
@topics = options[:topics] unless options[:topics].nil?
end
# given a book_id, returns the corresponding book object with all
# the attributes loaded from Redis; if the book_id does not exists,
# it returns nil.
def self.get(id)
hub = RedisHub.instance
if hub.get("book:#{id}:title")
book = Book.new(:id => id)
else
return nil
end
book.authors = Book.get_book_authors(id)
book.topics = Book.get_book_topics(id)
BOOK_STRING_ATTRS.each do |attr|
book.send "#{attr}=".to_sym, hub.get("book:#{id}:#{attr}")
end
book
end
# returns an array of book objects corresponding to all the book_id
# available in our Redis book:list key
def self.get_all
books = []
RedisHub.instance.lrange('book:list', 0, -1).each do |book_id|
books.push(Book.get(book_id))
end
books
end
# save the book object in Redis
def save
# add the id to the book list
@hub.lpush('book:list', @id)
# creates the couples key-values where we store our data
BOOK_STRING_ATTRS.each do |attr|
@hub.set("book:#{@id}:#{attr}", self.send(attr.to_sym))
end
# creates a set containing all the topic id associated to our book
# and add the book id to the set of all the books belonging to the
# same topic
@topics.each do |topic|
tid = (topic.is_a?(Book)) ? topic.id : topic
@hub.sadd("book:#{@id}:topics", tid)
Topic.add_book(tid, id)
end
# create a set containing all the authors id of our book and add
# the book id to the set of all the books belonging to the same
# author
@authors.each do |author|
aid = (author.is_a?(Author)) ? author.id : author
@hub.sadd("book:#{@id}:authors", author)
Author.add_book(author, id)
end
end
# given a book_id, delete all keys associated with it from the Redis
# database
def self.remove(id)
hub = RedisHub.instance
BOOK_STRING_ATTRS.each do |attr|
hub.del("book:#{id}:#{attr}")
end
Book.get_book_topics(id).each do |topic|
Topic.remove_book(topic.id, id)
end
hub.del("book:#{id}:topics")
Book.get_book_authors(id).each do |author|
Author.remove_book(author.id, id)
end
hub.del("book:#{id}:authors")
hub.lrem('book:list', 1, id)
end
# returns an array with all the book objects available in the
# book:list key between start and stop
def self.get_books_range(start, stop)
books = []
RedisHub.instance.lrange('book:list', start, stop).each do |book_id|
books.push(Book.get(book_id))
end
books
end
# returns an array with all the topic objects relative to the given
# book_id
def self.get_book_topics(id)
topics = []
RedisHub.instance.smembers("book:#{id}:topics").each do |topic_id|
topics.push(Topic.get(topic_id))
end
topics
end
# returns an array with all the author objects relative to the given
# book_id
def self.get_book_authors(id)
authors = []
RedisHub.instance.smembers("book:#{id}:authors").each do |author_id|
authors.push(Author.get(author_id))
end
authors
end
end
class Topic
attr_reader :id
attr_accessor :name, :description, :books
def initialize(options = {})
@hub = RedisHub.instance
@id = options[:id].nil? ? @hub.incr('topic:next_id') : options[:id]
@name = options[:name] unless options[:name].nil?
@description = options[:description] unless options[:description].nil?
@books = options[:books] || []
end
def self.get(id)
hub = RedisHub.instance
if hub.get("topic:#{id}:name")
topic = Topic.new(:id => id)
else
return nil
end
topic.name = hub.get("topic:#{id}:name")
topic.description = hub.get("topic:#{id}:description")
topic.books = hub.smembers("topic:#{id}:books")
topic
end
def self.get_all
topics = []
RedisHub.instance.lrange('topic:list', 0, -1).each do |topic_id|
topics.push(Topic.get(topic_id))
end
topics
end
def save
@hub.lpush('topic:list', @id)
@hub.set("topic:#{@id}:name", @name)
@hub.set("topic:#{@id}:description", @description)
@books.each do |book|
@hub.sadd("topic:#{id}:books", book)
end
end
def self.add_book(topic_id, book_id)
RedisHub.instance.sadd("topic:#{topic_id}:books", book_id)
end
def self.remove_book(topic_id, book_id)
RedisHub.instance.srem("topic:#{topic_id}:books", book_id)
end
def self.remove(id)
hub = RedisHub.instance
hub.del("topic:#{id}:name")
hub.del("topic:#{id}:description")
hub.smembers("topic:#{id}:books").each do |book_id|
hub.srem("book:#{book_id}:topics", id)
end
hub.del("topic:#{id}:books")
hub.lrem('topic:list', 1, id)
end
end
class Author
attr_reader :id
attr_accessor :name, :surname, :books
def initialize(options = {})
@hub = RedisHub.instance
@id = options[:id].nil? ? @hub.incr('author:next_id') : options[:id]
@name = options[:name] unless options[:name].nil?
@surname = options[:surname] unless options[:surname].nil?
@books = options[:books] || []
end
def self.get(id)
hub = RedisHub.instance
if hub.get("author:#{id}:name")
author = Author.new(:id => id)
else
return nil
end
author.name = hub.get("author:#{id}:name")
author.surname = hub.get("author:#{id}:surname")
author.books = hub.smembers("author:#{id}:books")
author
end
def self.get_all
authors = []
RedisHub.instance.lrange('author:list', 0, -1).each do |author_id|
authors.push(Author.get(author_id))
end
authors
end
def save
@hub.lpush('author:list', @id)
@hub.set("author:#{@id}:name", @name)
@hub.set("author:#{@id}:surname", @surname)
@books.each do |book|
@hub.sadd("author:#{@id}:books", book)
end
end
def self.add_book(author_id, book_id)
RedisHub.instance.sadd("author:#{author_id}:books", book_id)
end
def self.remove_book(author_id, book_id)
RedisHub.instance.srem("author:#{author_id}:books", book_id)
end
def self.remove(id)
hub = RedisHub.instance
hub.del("author:#{id}:name")
hub.del("author:#{id}:surname")
hub.smembers("author:#{id}:books").each do |book_id|
hub.srem("book:#{book_id}:authors", id)
end
hub.del("author:#{id}:books")
hub.lrem('author:list', 1, id)
end
end