ihoka-friendly 0.6.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +35 -0
- data/CONTRIBUTORS.md +7 -0
- data/LICENSE +20 -0
- data/README.md +288 -0
- data/lib/friendly.rb +52 -0
- data/lib/friendly/associations.rb +7 -0
- data/lib/friendly/associations/association.rb +34 -0
- data/lib/friendly/associations/set.rb +37 -0
- data/lib/friendly/attribute.rb +98 -0
- data/lib/friendly/boolean.rb +10 -0
- data/lib/friendly/cache.rb +24 -0
- data/lib/friendly/cache/by_id.rb +33 -0
- data/lib/friendly/data_store.rb +73 -0
- data/lib/friendly/document.rb +70 -0
- data/lib/friendly/document/associations.rb +50 -0
- data/lib/friendly/document/attributes.rb +114 -0
- data/lib/friendly/document/convenience.rb +41 -0
- data/lib/friendly/document/mixin.rb +15 -0
- data/lib/friendly/document/scoping.rb +66 -0
- data/lib/friendly/document/storage.rb +63 -0
- data/lib/friendly/document_table.rb +56 -0
- data/lib/friendly/index.rb +73 -0
- data/lib/friendly/indexer.rb +50 -0
- data/lib/friendly/memcached.rb +48 -0
- data/lib/friendly/newrelic.rb +6 -0
- data/lib/friendly/query.rb +42 -0
- data/lib/friendly/scope.rb +100 -0
- data/lib/friendly/scope_proxy.rb +43 -0
- data/lib/friendly/sequel_monkey_patches.rb +34 -0
- data/lib/friendly/storage.rb +31 -0
- data/lib/friendly/storage_factory.rb +24 -0
- data/lib/friendly/storage_proxy.rb +111 -0
- data/lib/friendly/table.rb +15 -0
- data/lib/friendly/table_creator.rb +48 -0
- data/lib/friendly/time.rb +14 -0
- data/lib/friendly/translator.rb +35 -0
- data/lib/friendly/uuid.rb +149 -0
- data/rails/init.rb +3 -0
- metadata +522 -0
data/CHANGELOG.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
Changelog
|
2
|
+
=========
|
3
|
+
|
4
|
+
### 0.6.2
|
5
|
+
* (ihoka) renamed gem to ihoka-friendly
|
6
|
+
|
7
|
+
### 0.6.1
|
8
|
+
* (ihoka) upgraded to Jeweler 1.5
|
9
|
+
* (ihoka) updated gem dependencies
|
10
|
+
|
11
|
+
### 0.5
|
12
|
+
|
13
|
+
* (jamesgolick) Add offline index building.
|
14
|
+
|
15
|
+
### 0.4.4
|
16
|
+
|
17
|
+
* (jamesgolick) Make it possible to query with order, but no conditions.
|
18
|
+
* (jamesgolick) Add change tracking. This is mostly to facilitate arbitrary caches.
|
19
|
+
|
20
|
+
### 0.4.2
|
21
|
+
|
22
|
+
* (nullstyle) convert UUID to SQL::Blob so that Sequel can properly escape it in databases that don't treat binary strings like regular strings.
|
23
|
+
|
24
|
+
### 0.4.1
|
25
|
+
|
26
|
+
* (jamesgolick) Fix for ruby 1.9.1.
|
27
|
+
|
28
|
+
### 0.4.0
|
29
|
+
|
30
|
+
* (jamesgolick) Add scope chaining. See the README and the docs for Friendly::Scope.
|
31
|
+
* (jamesgolick) Add has_many association.
|
32
|
+
* (jamesgolick) Add ad-hoc scopes. See the docs for Document.scope.
|
33
|
+
* (jamesgolick) Add named_scope functionality. See the docs for Document.named_scope.
|
34
|
+
* (jeffrafter + jamesgolick) DDL generation now supports custom types. Previously, it would only work with types that Sequel already supported. Now, if you want to register a custom type, use Friendly::Attribute.register_type(klass, sql_type, &conversion_method).
|
35
|
+
|
data/CONTRIBUTORS.md
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
Contributors to Friendly
|
2
|
+
========================
|
3
|
+
|
4
|
+
* James Golick - http://jamesgolick.com (github: jamesgolick)
|
5
|
+
* Jonathan Palardy - http://technotales.wordpress.com (github: jpalardy)
|
6
|
+
* Jeff Rafter - http://socialorange.com (github: jeffrafter)
|
7
|
+
* Scott Fleckenstein - http://nullstyle.com (github: nullstyle)
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 James Golick
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,288 @@
|
|
1
|
+
Friendly
|
2
|
+
========
|
3
|
+
|
4
|
+
### Short Version
|
5
|
+
|
6
|
+
This is an implementation of the ideas found in [this article](http://bret.appspot.com/entry/how-friendfeed-uses-mysql) about how FriendFeed uses MySQL. You should read that article for all the details.
|
7
|
+
|
8
|
+
### Long Version
|
9
|
+
|
10
|
+
Turn MySQL in to a document db!
|
11
|
+
|
12
|
+
Why? Everybody is super excited about NoSQL. Aside from the ridiculous rumour that removing SQL makes things magically scalable, there's a lot of reason to look forward to these new data storage solutions.
|
13
|
+
|
14
|
+
One of the biggest improvements is where schema / index changes are concerned. When you have a ton of data, migrating MySQL tables takes forever and locks the table during the process. Document dbs like mongo and couch, on the other hand, are schemaless. You just add and remove fields as you need them.
|
15
|
+
|
16
|
+
But, the available document oriented solutions are still young. While many of them show great promise, they've all got their quirks. For all its flaws, MySQL is a rock. It's pretty fast, and battle-hardened. We *never* have problems with MySQL in production.
|
17
|
+
|
18
|
+
Fortunately, with a little extra work on the client-side, we can get the flexibility of a doc db in MySQL!
|
19
|
+
|
20
|
+
### How it Works
|
21
|
+
|
22
|
+
Let's say we had a user model.
|
23
|
+
|
24
|
+
class User
|
25
|
+
include Friendly::Document
|
26
|
+
|
27
|
+
attribute :name, String
|
28
|
+
attribute :age, Integer
|
29
|
+
end
|
30
|
+
|
31
|
+
Friendly always stores your documents in a table with the same schema:
|
32
|
+
|
33
|
+
CREATE TABLE users (
|
34
|
+
added_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
|
35
|
+
id BINARY(16) NOT NULL,
|
36
|
+
created_at DATETIME NOT NULL,
|
37
|
+
updated_at DATETIME NOT NULL,
|
38
|
+
attributes TEXT,
|
39
|
+
UNIQUE KEY (id),
|
40
|
+
) ENGINE=InnoDB;
|
41
|
+
|
42
|
+
- added_id is there because InnoDB stores records on disk in sequential primary key order. Having recently inserted objects together on disk is usually a win.
|
43
|
+
- id is a UUID (instance of Friendly::UUID).
|
44
|
+
- created_at and updated_at are exactly what they sound like - automatically managed by Friendly.
|
45
|
+
- attributes is where all the attributes of your object are stored. They get serialized to json and stored in there.
|
46
|
+
|
47
|
+
We can instantiate and save our model like an ActiveRecord object.
|
48
|
+
|
49
|
+
@user = User.new :name => "James"
|
50
|
+
@user.save
|
51
|
+
|
52
|
+
As is, our user model only supports queries by id.
|
53
|
+
|
54
|
+
User.find(id)
|
55
|
+
User.first(:id => id)
|
56
|
+
User.all(:id => [1,2,3])
|
57
|
+
|
58
|
+
Not great. We'd probably want to be able to query by name, at the very least.
|
59
|
+
|
60
|
+
Indexes
|
61
|
+
=======
|
62
|
+
|
63
|
+
To support richer queries, Friendly maintains its own indexes in separate tables. To index our user model on name, we'd create a table like this:
|
64
|
+
|
65
|
+
CREATE TABLE index_users_on_name (
|
66
|
+
name varchar(256) NOT NULL,
|
67
|
+
id binary(16) NOT NULL,
|
68
|
+
PRIMARY KEY (name, id)
|
69
|
+
UNIQUE KEY unique_index_on_id (id)
|
70
|
+
) ENGINE=InnoDB DEFAULT CHARSET=latin1
|
71
|
+
|
72
|
+
Then, we'd tell friendly to maintain that index for us:
|
73
|
+
|
74
|
+
class User
|
75
|
+
# ... snip ...
|
76
|
+
|
77
|
+
indexes :name
|
78
|
+
end
|
79
|
+
|
80
|
+
Any time friendly saves a user object, it will update the index as well. That way, we can query by name:
|
81
|
+
|
82
|
+
User.first(:name => "James")
|
83
|
+
User.all(:name => ["James", "John", "Jonathan"])
|
84
|
+
|
85
|
+
One of the big advantages to this approach is that indexes can be built offline. If you need a new index, you can write a script to generate it in the background without affecting the running application. Then, once it's ready, you can start querying it.
|
86
|
+
|
87
|
+
Caching
|
88
|
+
=======
|
89
|
+
|
90
|
+
Friendly has built-in support for write-through caching.
|
91
|
+
|
92
|
+
First, install the memcached gem:
|
93
|
+
|
94
|
+
sudo gem install memcached
|
95
|
+
|
96
|
+
Then, configure your cache:
|
97
|
+
|
98
|
+
$cache = Memcached.new # you'll probably want to pass some params here.
|
99
|
+
Friendly.cache = Friendly::Memcached.new($cache)
|
100
|
+
|
101
|
+
Finally, declare the cache in your model:
|
102
|
+
|
103
|
+
class User
|
104
|
+
# ... snip ...
|
105
|
+
|
106
|
+
caches_by :id
|
107
|
+
end
|
108
|
+
|
109
|
+
This tells Friendly to automatically write through to cache on save, and read through to the cache if it needs to hit the database on a query.
|
110
|
+
|
111
|
+
Currently, only caching by id is supported, but caching of arbitrary indexes is planned.
|
112
|
+
|
113
|
+
__We're seeing a 99.8% cache hit rate in production with this code.__
|
114
|
+
|
115
|
+
Scopes
|
116
|
+
======
|
117
|
+
|
118
|
+
### Named Scopes
|
119
|
+
|
120
|
+
It's possible to create a scope that you can refer to by name:
|
121
|
+
|
122
|
+
class Post
|
123
|
+
attribute :author, String
|
124
|
+
|
125
|
+
named_scope :by_james, :author => "James"
|
126
|
+
end
|
127
|
+
|
128
|
+
Calling the scope will provide you with a Friendly:Scope object:
|
129
|
+
|
130
|
+
Post.by_james #=> #<Friendly::Scope>
|
131
|
+
|
132
|
+
That scope object supports a variety of methods. Calling any of the methods is the equivalent of calling those methods directly on Document with the scope's parameters.
|
133
|
+
|
134
|
+
e.g.
|
135
|
+
|
136
|
+
Post.by_james.all == Post.all(:author => "James")
|
137
|
+
Post.by_james.first == Post.first(:author => "James")
|
138
|
+
Post.by_james.paginate # => #<WillPaginate::Collection>
|
139
|
+
Post.by_james.build.name == "James"
|
140
|
+
@post = Post.by_james.create
|
141
|
+
@post.new_record? # => false
|
142
|
+
@post.name # => "James"
|
143
|
+
|
144
|
+
Each of the methods also accepts override parameters. The APIs are the same as on Document.
|
145
|
+
|
146
|
+
Post.by_james.all(:author => "Steve") == Post.all(:author => "Steve")
|
147
|
+
|
148
|
+
You can also compose arbitrary combinations of scopes with simple chaining.
|
149
|
+
|
150
|
+
class Post
|
151
|
+
# ... snip ...
|
152
|
+
named_scope :recent, :order! => :created_at.desc, :limit! => 4
|
153
|
+
indexes :name, :created_at
|
154
|
+
end
|
155
|
+
|
156
|
+
Post.by_james.recent == Post.all(:name => "James",
|
157
|
+
:order! => :created_at.desc,
|
158
|
+
:limit! => 4)
|
159
|
+
|
160
|
+
If two parameters conflict, the right-most scope takes precedence.
|
161
|
+
|
162
|
+
### Ad-hoc Scopes
|
163
|
+
|
164
|
+
You can also create a scope object on the fly:
|
165
|
+
|
166
|
+
Post.scope(:author => "Steve")
|
167
|
+
|
168
|
+
The object you get is identical to the one you get from a named_scope. So, see above for the API.
|
169
|
+
|
170
|
+
Associations
|
171
|
+
============
|
172
|
+
|
173
|
+
Friendly currently only supports has\_many associations.
|
174
|
+
|
175
|
+
Creating a has\_many is as simple as setting up the necessary association and foreign key.
|
176
|
+
|
177
|
+
_Note: Make sure that the target model is indexed on the foreign key. If it isn't, querying the association will raise Friendly::MissingIndex._
|
178
|
+
|
179
|
+
e.g.
|
180
|
+
|
181
|
+
class Post
|
182
|
+
attribute :user_id, Friendly::UUID
|
183
|
+
indexes :user_id
|
184
|
+
end
|
185
|
+
|
186
|
+
class User
|
187
|
+
has_many :posts
|
188
|
+
end
|
189
|
+
|
190
|
+
@user = User.create
|
191
|
+
@post = @user.posts.create
|
192
|
+
@user.posts.all == [@post] # => true
|
193
|
+
|
194
|
+
Friendly defaults the foreign key to class_name_id just like ActiveRecord. It also converts the name of the association to the name of the target class just like ActiveRecord does.
|
195
|
+
|
196
|
+
The biggest difference in semantics between Friendly's has\_many and active\_record's is that Friendly's just returns a Friendly::Scope object. If you want all the associated objects, you have to call #all to get them.
|
197
|
+
|
198
|
+
You can also use any other Friendly::Scope method like scope chaining.
|
199
|
+
|
200
|
+
# note: the Post.recent scope is defined in the above section
|
201
|
+
@user.posts.recent == Post.all(:user_id => @user.id,
|
202
|
+
:order! => :created_at.desc,
|
203
|
+
:limit! => 4)
|
204
|
+
|
205
|
+
See the section above or the Friendly::Scope docs for more details.
|
206
|
+
|
207
|
+
Offline Indexing
|
208
|
+
================
|
209
|
+
|
210
|
+
Friendly includes support for building an index in the background, without taking your app offline.
|
211
|
+
|
212
|
+
All you have to do is declare the index in your model. If we wanted to add an index on :name, :created_at in a User model, we'd do it like this:
|
213
|
+
|
214
|
+
class User
|
215
|
+
# ...snip...
|
216
|
+
|
217
|
+
indexes :name, :created_at
|
218
|
+
end
|
219
|
+
|
220
|
+
Then, make sure to run Friendly.create_tables! to create the table in the database. This won't overwrite any of your existing tables, so don't worry.
|
221
|
+
|
222
|
+
>> Friendly.create_tables!
|
223
|
+
|
224
|
+
Now that the the new table has been created, you need to copy the .rake file included with Friendly (lib/tasks/friendly.rake) to somewhere that will get picked up by your main Rakefile (lib/tasks if it's a rails project). Then, run:
|
225
|
+
|
226
|
+
$ KLASS=User FIELDS=name,created_at rake friendly:build_index
|
227
|
+
|
228
|
+
If you're running this in production, you'll probably want to fire up GNU screen so that it'll keep running even if you lose your SSH connection. When this task completes, the index is populated and ready to go!
|
229
|
+
|
230
|
+
Installation
|
231
|
+
============
|
232
|
+
|
233
|
+
Friendly is available as a gem. Get it with:
|
234
|
+
|
235
|
+
sudo gem install friendly
|
236
|
+
|
237
|
+
Setup
|
238
|
+
=====
|
239
|
+
|
240
|
+
All you have to do is supply Friendly with some information about your database:
|
241
|
+
|
242
|
+
Friendly.configure :adapter => "mysql",
|
243
|
+
:host => "localhost",
|
244
|
+
:user => "root",
|
245
|
+
:password => "swordfish",
|
246
|
+
:database => "friendly_development"
|
247
|
+
|
248
|
+
Now, you're ready to rock.
|
249
|
+
|
250
|
+
If you're using rails, set friendly as a gem dependency:
|
251
|
+
|
252
|
+
config.gem "friendly"
|
253
|
+
|
254
|
+
...and drop something like this in config/friendly.yml (an example of such a config exists in examples/friendly.yml):
|
255
|
+
|
256
|
+
development:
|
257
|
+
:adapter: "mysql"
|
258
|
+
:host: "localhost"
|
259
|
+
:user: "root"
|
260
|
+
:password: "swordfish"
|
261
|
+
:database: "friendly_development"
|
262
|
+
|
263
|
+
Of course, you'll want to swap out these values for your own, fill in additional environments, and so forth.
|
264
|
+
|
265
|
+
Then, create some models, and run:
|
266
|
+
|
267
|
+
Friendly.create_tables!
|
268
|
+
|
269
|
+
That'll create all the necessary tables as best it can. This has worked well enough for me, but it's possible that certain table configurations will fail. It won't attempt to create any tables that already exist, so it's safe to run in an initializer or something.
|
270
|
+
|
271
|
+
TODO
|
272
|
+
====
|
273
|
+
|
274
|
+
- Online migrations. Add a version column to each model and a DSL to update schema from one version to another on read. This facilitates data transformations on the fly. If you want to transform the whole table at once, just iterate over all the objects, and save.
|
275
|
+
- Associations
|
276
|
+
- Offline indexer
|
277
|
+
- Caching of arbitrary indexes
|
278
|
+
- A lot more documentation
|
279
|
+
|
280
|
+
Credits
|
281
|
+
=======
|
282
|
+
|
283
|
+
Friendly was developed by James Golick & Jonathan Palardy at FetLife (nsfw).
|
284
|
+
|
285
|
+
Copyright (c) 2009 James Golick. See LICENSE for details.
|
286
|
+
|
287
|
+
Except for friendly/uuid.rb which is copyright Evan Weaver and Apache Licensed. See APACHE-LICENSE for details.
|
288
|
+
|
data/lib/friendly.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'friendly/associations'
|
2
|
+
require 'friendly/attribute'
|
3
|
+
require 'friendly/boolean'
|
4
|
+
require 'friendly/cache'
|
5
|
+
require 'friendly/cache/by_id'
|
6
|
+
require 'friendly/data_store'
|
7
|
+
require 'friendly/document'
|
8
|
+
require 'friendly/document_table'
|
9
|
+
require 'friendly/index'
|
10
|
+
require 'friendly/indexer'
|
11
|
+
require 'friendly/memcached'
|
12
|
+
require 'friendly/query'
|
13
|
+
require 'friendly/sequel_monkey_patches'
|
14
|
+
require 'friendly/scope'
|
15
|
+
require 'friendly/scope_proxy'
|
16
|
+
require 'friendly/storage_factory'
|
17
|
+
require 'friendly/storage_proxy'
|
18
|
+
require 'friendly/translator'
|
19
|
+
require 'friendly/uuid'
|
20
|
+
|
21
|
+
require 'will_paginate/collection'
|
22
|
+
|
23
|
+
module Friendly
|
24
|
+
class << self
|
25
|
+
attr_accessor :datastore, :db, :cache
|
26
|
+
|
27
|
+
def configure(config)
|
28
|
+
self.db = Sequel.connect(config)
|
29
|
+
self.datastore = DataStore.new(db)
|
30
|
+
end
|
31
|
+
|
32
|
+
def batch
|
33
|
+
begin
|
34
|
+
datastore.start_batch
|
35
|
+
yield
|
36
|
+
datastore.flush_batch
|
37
|
+
ensure
|
38
|
+
datastore.reset_batch
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def create_tables!
|
43
|
+
Document.create_tables!
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
class Error < RuntimeError; end
|
48
|
+
class RecordNotFound < Error; end
|
49
|
+
class MissingIndex < Error; end
|
50
|
+
class NoConverterExists < Friendly::Error; end
|
51
|
+
class NotSupported < Friendly::Error; end
|
52
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Friendly
|
2
|
+
module Associations
|
3
|
+
class Association
|
4
|
+
attr_reader :owner_klass, :name
|
5
|
+
|
6
|
+
def initialize(owner_klass, name, options = {})
|
7
|
+
@owner_klass = owner_klass
|
8
|
+
@name = name
|
9
|
+
@class_name = options[:class_name]
|
10
|
+
@foreign_key = options[:foreign_key]
|
11
|
+
end
|
12
|
+
|
13
|
+
def klass
|
14
|
+
@klass ||= class_name.constantize
|
15
|
+
end
|
16
|
+
|
17
|
+
def foreign_key
|
18
|
+
@foreign_key ||= [owner_klass_name, :id].join("_").to_sym
|
19
|
+
end
|
20
|
+
|
21
|
+
def class_name
|
22
|
+
@class_name ||= name.to_s.classify
|
23
|
+
end
|
24
|
+
|
25
|
+
def owner_klass_name
|
26
|
+
owner_klass.name.to_s.underscore.singularize
|
27
|
+
end
|
28
|
+
|
29
|
+
def scope(document)
|
30
|
+
klass.scope(foreign_key => document.id)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|