Created
May 13, 2010 18:19
-
-
Save kovyrin/400178 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Где-нить в initializers/sharding.rb | |
# | |
module DbCharmer | |
module Sharding | |
module Method | |
class MyCoolMethod | |
def initialize(config) | |
# тут можно что-то достать из config[] | |
end | |
def shard_for_key(key) | |
# тут нужно вернуть имя коннекшна из database.yml или хеш с параметрами подключения к базе (если они хранятся где-то еще) | |
end | |
def support_default_shard? | |
# если обьявить этот метод и вернуть из него true, то shard_for_key(:default) будет импользоваться для служебных операций с таблицами (типа show fields) | |
end | |
def shard_connections | |
# этот метод нужно обьявить если есть возможность достать список шардов откуда-нибудь. Если этот метод есть, то можно делать запросы сразу на всех шардах | |
end | |
end | |
end | |
end | |
end | |
DbCharmer::Sharding.register_connection( | |
:name => :my_cool_shards, | |
:method => :my_cool_method, | |
... тут еще всякого можно передать если хочется | |
) | |
#---------------------------------------------------------------------------------------------------------- | |
# | |
# Модель будет выглядеть вот так | |
# | |
class Tweet < ActiveRecord::Base | |
db_magic :sharded => { | |
:key => :user_id, # это пока не используется, но в будущем я хочу использовать его в Model#save и тп местах | |
:sharded_connection => :my_cool_shards # вот тут мы указываем наш класс-шардер | |
} | |
end | |
#---------------------------------------------------------------------------------------------------------- | |
# | |
# После этого доступ к модели выглядит вот так | |
# | |
Tweet.shard_for(some_user_id).find(...) | |
Tweet.shard_for(some_user_id).first(...) | |
Tweet.shard_for(some_user_id).create(...) | |
tweet = Tweet.new | |
... | |
Tweet.shard_for(some_user_id) { tweet.save) # вот это мне не нравится, думаю как сделать лучше | |
... | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment