georgepalmer-couch_foo 0.7.12 → 0.7.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +2 -2
- data/VERSION.yml +1 -1
- data/lib/couch_foo/base.rb +8 -10
- data/lib/couch_foo/database.rb +14 -11
- metadata +6 -2
    
        data/README.rdoc
    CHANGED
    
    | @@ -42,14 +42,14 @@ are a few minor differences to the way CouchDB works.  In particular: | |
| 42 42 | 
             
            * Because there's no SQL there's no SQL finder methods
         | 
| 43 43 | 
             
            * Timezones, aggregations and fixtures are not yet implemented
         | 
| 44 44 | 
             
            * The price of index updating is paid when next accessing the index rather than the point of insertion.  This can be more efficient or less depending on your application.  It may make sense to use an external process to do the updating for you - see CouchFoo#find for more on this
         | 
| 45 | 
            -
            * On that note,  | 
| 45 | 
            +
            * On that note, compacting of CouchDB is required to recover space from old versions of documents and keep performance fast.  This can be kicked off in several ways (see quick start guide)
         | 
| 46 46 |  | 
| 47 47 | 
             
            It is recommend that you read the quick start and performance sections in the rdoc for a full overview of differences and points to be aware of when developing.  The Changelog file shows the differences between gem versions and should be checked when upgrading gem versions.
         | 
| 48 48 |  | 
| 49 49 |  | 
| 50 50 | 
             
            == Getting started
         | 
| 51 51 |  | 
| 52 | 
            -
              CouchFoo::Base.set_database("http://localhost:5984 | 
| 52 | 
            +
              CouchFoo::Base.set_database(:host => "http://localhost:5984", :database => "mydatabase")
         | 
| 53 53 | 
             
              CouchFoo::Base.logger = Rails.logger
         | 
| 54 54 |  | 
| 55 55 | 
             
            If using with Rails you will need to create an initializer to do this (until proper integration is added)
         | 
    
        data/VERSION.yml
    CHANGED
    
    
    
        data/lib/couch_foo/base.rb
    CHANGED
    
    | @@ -129,9 +129,8 @@ module CouchFoo | |
| 129 129 | 
             
              # CouchDB has a concept called bulk saving where mutliple operations can be built up and commited
         | 
| 130 130 | 
             
              # at once.  This has the added advantage of being in a transaction so all or none of the operations
         | 
| 131 131 | 
             
              # will complete.  By default bulk saving is switched off but you can set the 
         | 
| 132 | 
            -
              # CouchFoo::Base.bulk_save_default  | 
| 133 | 
            -
              #  | 
| 134 | 
            -
              # large amounts of documents but beaware it is the developers responsability to commit the work.
         | 
| 132 | 
            +
              # CouchFoo::Base.bulk_save_default setting to true to achieve good performance gains when updating
         | 
| 133 | 
            +
              # large amounts of documents.  Beware it is the developers responsability to commit the work though.
         | 
| 135 134 | 
             
              # For example:
         | 
| 136 135 | 
             
              # 
         | 
| 137 136 | 
             
              #   User.all.size # => 27
         | 
| @@ -148,7 +147,7 @@ module CouchFoo | |
| 148 147 | 
             
              # the document into memory another person or program has altered its contents.  CouchDB detects
         | 
| 149 148 | 
             
              # this through the _rev attribute.  If a conflict occurs a DocumentConflict error is raised. This
         | 
| 150 149 | 
             
              # is effectively the same as ActiveRecord Optimistic locking mechanism.  Exceptions should be
         | 
| 151 | 
            -
              # dealt with in application logic | 
| 150 | 
            +
              # dealt with in application logic if using save! - save will just return false
         | 
| 152 151 | 
             
              #
         | 
| 153 152 | 
             
              # On the finders and associations there are a few options that are no longer relevant - :select,
         | 
| 154 153 | 
             
              # :joins, :having, :group, :from and :lock  Everything else is available although :conditions and
         | 
| @@ -201,16 +200,15 @@ module CouchFoo | |
| 201 200 | 
             
              # 
         | 
| 202 201 | 
             
              # Connecting to the database is done in a way similar to ActiveRecord where the database is
         | 
| 203 202 | 
             
              # inherited by each subclass unless overridden.  In CouchFoo you use the call set_database method
         | 
| 204 | 
            -
              # with a  | 
| 205 | 
            -
              #  | 
| 206 | 
            -
              # applications even use one database per user.
         | 
| 203 | 
            +
              # with a host and database name.  As there's no database connection to be maintained sharding data
         | 
| 204 | 
            +
              # is very efficient and some applications even use one database per user.
         | 
| 207 205 | 
             
              # 
         | 
| 208 206 | 
             
              # If using rails, for now you need to specify your own initializer to make the default database
         | 
| 209 207 | 
             
              # connection.  This will be brought inline with rails in the future, using a couchdb.yml 
         | 
| 210 208 | 
             
              # configuration file or similar.  But for now an initializer file in config/initializers like the 
         | 
| 211 | 
            -
              #  | 
| 209 | 
            +
              # following will do the trick:
         | 
| 212 210 | 
             
              #
         | 
| 213 | 
            -
              #   CouchFoo::Base.set_database("http://localhost:5984 | 
| 211 | 
            +
              #   CouchFoo::Base.set_database(:host => "http://localhost:5984", :database => "mydatabase")
         | 
| 214 212 | 
             
              #   CouchFoo::Base.logger = Rails.logger
         | 
| 215 213 | 
             
              #
         | 
| 216 214 | 
             
              # A few tidbits:
         | 
| @@ -218,7 +216,7 @@ module CouchFoo | |
| 218 216 | 
             
              # * validates_uniqueness_of has had the :case_sensitive option dropped
         | 
| 219 217 | 
             
              # * Because there's no SQL there's no SQL finder methods but equally there's no SQL injection to worry about.  You should still sanitize output when displaying back to the user though because you will still be vunerable to JS attacks etc.
         | 
| 220 218 | 
             
              # * Some operations are more efficient than relational databases, others less so.  See the performance section for more details on this
         | 
| 221 | 
            -
              # * Every so often the database will need compacting to recover space lost to old document revisions.  See http://wiki.apache.org/couchdb/Compaction for more details on this.
         | 
| 219 | 
            +
              # * Every so often the database will need compacting to recover space lost to old document revisions.  More importantly initial indications show un-compacted databases can effect performance.  See http://wiki.apache.org/couchdb/Compaction for more details on this.
         | 
| 222 220 | 
             
              #
         | 
| 223 221 | 
             
              # The following things are not in this implementation (but are in ActiveRecord):
         | 
| 224 222 | 
             
              # * :include - Although the finders and associations allow this option the actual implementation is yet to be written
         | 
    
        data/lib/couch_foo/database.rb
    CHANGED
    
    | @@ -1,17 +1,15 @@ | |
| 1 1 | 
             
            require 'couchrest'
         | 
| 2 2 |  | 
| 3 3 | 
             
            # This class wrappers CouchRest but may ultimately replace it as only parts of the library are used
         | 
| 4 | 
            -
            module CouchFoo | 
| 5 | 
            -
              LATEST_COUCHDB_VERSION = 0.9
         | 
| 6 | 
            -
              
         | 
| 4 | 
            +
            module CouchFoo 
         | 
| 7 5 | 
             
              class DatabaseWrapper
         | 
| 8 6 |  | 
| 9 7 | 
             
                attr_accessor :database, :database_version, :bulk_save_default
         | 
| 10 8 |  | 
| 11 | 
            -
                def initialize( | 
| 12 | 
            -
                  self.database = CouchRest.database( | 
| 9 | 
            +
                def initialize(options, bulk_save_default, *args)
         | 
| 10 | 
            +
                  self.database = CouchRest.database(options[:host] + "/" + options[:database])
         | 
| 13 11 | 
             
                  self.bulk_save_default = bulk_save_default
         | 
| 14 | 
            -
                  self.database_version = version
         | 
| 12 | 
            +
                  self.database_version = (JSON.parse(RestClient.get(options[:host]))["version"]).gsub(/-.+/,"").to_f
         | 
| 15 13 | 
             
                end
         | 
| 16 14 |  | 
| 17 15 | 
             
                def save(doc, bulk_save = bulk_save?)
         | 
| @@ -94,7 +92,8 @@ module CouchFoo | |
| 94 92 | 
             
                def handle_exception(exception)
         | 
| 95 93 | 
             
                  if exception.is_a?(RestClient::ResourceNotFound)
         | 
| 96 94 | 
             
                    raise DocumentNotFound, "Couldn't find document"
         | 
| 97 | 
            -
                  elsif exception.is_a?(RestClient::RequestFailed) && exception. | 
| 95 | 
            +
                  elsif exception.is_a?(RestClient::RequestFailed) && exception.respond_to?(:http_code) && exception.http_code == 412
         | 
| 96 | 
            +
                    
         | 
| 98 97 | 
             
                    raise DocumentConflict, "Document has been updated whilst object loaded"
         | 
| 99 98 | 
             
                  else
         | 
| 100 99 | 
             
                    # We let the rest fall through as normally CouchDB setup error
         | 
| @@ -128,6 +127,10 @@ module CouchFoo | |
| 128 127 | 
             
                  # different databases from their parents.  As such if you only use one database for your
         | 
| 129 128 | 
             
                  # application then only one call is required to CouchFoo::Base for initial setup.
         | 
| 130 129 | 
             
                  #
         | 
| 130 | 
            +
                  # When using a database for the first time a version check is performed on CouchDB so that
         | 
| 131 | 
            +
                  # performance optimisations are run according to your database version.  At time of writing
         | 
| 132 | 
            +
                  # CouchDB 0.9 offers some good performance gains over 0.8
         | 
| 133 | 
            +
                  #
         | 
| 131 134 | 
             
                  # For ultra-scalability and using a different database for each user, perform the set_database
         | 
| 132 135 | 
             
                  # call on the CouchFoo::Base object on a before_filter using the session information to
         | 
| 133 136 | 
             
                  # determine the database to connect to.  For example:
         | 
| @@ -136,7 +139,7 @@ module CouchFoo | |
| 136 139 | 
             
                  #   before_filter :set_user_database
         | 
| 137 140 | 
             
                  #
         | 
| 138 141 | 
             
                  #   def set_user_database
         | 
| 139 | 
            -
                  #     CouchFoo::Base.set_database("http://localhost:5984 | 
| 142 | 
            +
                  #     CouchFoo::Base.set_database(:host => "http://localhost:5984", :database => "user#{session[:user]}")
         | 
| 140 143 | 
             
                  #   end
         | 
| 141 144 | 
             
                  # end
         | 
| 142 145 | 
             
                  #
         | 
| @@ -149,9 +152,9 @@ module CouchFoo | |
| 149 152 | 
             
                  # </ul>
         | 
| 150 153 | 
             
                  #
         | 
| 151 154 | 
             
                  # NOTE: This will work best on domains where there is little overlap between users data (eg basecamp)
         | 
| 152 | 
            -
                  def set_database( | 
| 153 | 
            -
                    @active_database = DatabaseWrapper.new( | 
| 155 | 
            +
                  def set_database(options, bulk_save = bulk_save_default)
         | 
| 156 | 
            +
                    @active_database = DatabaseWrapper.new(options, bulk_save)
         | 
| 154 157 | 
             
                  end
         | 
| 155 158 | 
             
                end # ClassMethods
         | 
| 156 159 | 
             
              end
         | 
| 157 | 
            -
            end
         | 
| 160 | 
            +
            end
         | 
    
        metadata
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification 
         | 
| 2 2 | 
             
            name: georgepalmer-couch_foo
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version 
         | 
| 4 | 
            -
              version: 0.7. | 
| 4 | 
            +
              version: 0.7.13
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors: 
         | 
| 7 7 | 
             
            - George Palmer
         | 
| @@ -9,11 +9,12 @@ autorequire: | |
| 9 9 | 
             
            bindir: bin
         | 
| 10 10 | 
             
            cert_chain: []
         | 
| 11 11 |  | 
| 12 | 
            -
            date: 2009-02- | 
| 12 | 
            +
            date: 2009-02-18 00:00:00 -08:00
         | 
| 13 13 | 
             
            default_executable: 
         | 
| 14 14 | 
             
            dependencies: 
         | 
| 15 15 | 
             
            - !ruby/object:Gem::Dependency 
         | 
| 16 16 | 
             
              name: json
         | 
| 17 | 
            +
              type: :runtime
         | 
| 17 18 | 
             
              version_requirement: 
         | 
| 18 19 | 
             
              version_requirements: !ruby/object:Gem::Requirement 
         | 
| 19 20 | 
             
                requirements: 
         | 
| @@ -23,6 +24,7 @@ dependencies: | |
| 23 24 | 
             
                version: 
         | 
| 24 25 | 
             
            - !ruby/object:Gem::Dependency 
         | 
| 25 26 | 
             
              name: activesupport
         | 
| 27 | 
            +
              type: :runtime
         | 
| 26 28 | 
             
              version_requirement: 
         | 
| 27 29 | 
             
              version_requirements: !ruby/object:Gem::Requirement 
         | 
| 28 30 | 
             
                requirements: 
         | 
| @@ -32,6 +34,7 @@ dependencies: | |
| 32 34 | 
             
                version: 
         | 
| 33 35 | 
             
            - !ruby/object:Gem::Dependency 
         | 
| 34 36 | 
             
              name: jchris-couchrest
         | 
| 37 | 
            +
              type: :runtime
         | 
| 35 38 | 
             
              version_requirement: 
         | 
| 36 39 | 
             
              version_requirements: !ruby/object:Gem::Requirement 
         | 
| 37 40 | 
             
                requirements: 
         | 
| @@ -41,6 +44,7 @@ dependencies: | |
| 41 44 | 
             
                version: 
         | 
| 42 45 | 
             
            - !ruby/object:Gem::Dependency 
         | 
| 43 46 | 
             
              name: uuid
         | 
| 47 | 
            +
              type: :runtime
         | 
| 44 48 | 
             
              version_requirement: 
         | 
| 45 49 | 
             
              version_requirements: !ruby/object:Gem::Requirement 
         | 
| 46 50 | 
             
                requirements: 
         |