Permalink
Browse files

Moved to TouchDB, but launch is dying on an assertion failure

  • Loading branch information...
1 parent e02f9dd commit 5316c049242497518e0e8a973f6ed10209bb61ed Deepwinter committed May 17, 2012
Showing 521 changed files with 340 additions and 9,059 deletions.
Binary file not shown.
@@ -18,7 +18,7 @@
/** A binary attachment to a document.
- Actually a CouchAttachment is a child of a CouchRevision, since attachments (like all document contents) are versioned. This means that each instance represents an attachment immutably as it appeared in one revision of its document. So if you PUT a change to an attachment, the updated attachment will have a new CouchAttachment object. */
+ Actually a CouchAttachment may be a child of either a CouchDocument or a CouchRevision. The latter represents an attachment immutably as it appeared in one revision of its document. So if you PUT a change to an attachment, the updated attachment will have a new CouchAttachment object. */
@interface CouchAttachment : CouchResource
{
@private
@@ -23,7 +23,7 @@
#import "CouchQuery.h"
#import "CouchRevision.h"
#import "CouchServer.h"
-#import "CouchEmbeddedServer.h"
+#import "CouchTouchDBServer.h"
/** @mainpage About CouchCocoa
@@ -15,8 +15,10 @@
#import "CouchResource.h"
#import "CouchReplication.h"
-@class RESTCache, CouchChangeTracker, CouchDocument, CouchDesignDocument, CouchPersistentReplication, CouchQuery, CouchServer;
+@class RESTCache, CouchChangeTracker, CouchDocument, CouchDesignDocument, CouchModelFactory,
+ CouchPersistentReplication, CouchQuery, CouchServer;
+typedef NSString* (^CouchDocumentPathMap)(NSString* documentID);
/** A CouchDB database; contains CouchDocuments.
The CouchServer is the factory object for CouchDatabases. */
@@ -30,6 +32,8 @@
BOOL _lastSequenceNumberKnown;
id _onChangeBlock;
NSMutableArray* _deferredChanges;
+ CouchDocumentPathMap _documentPathMap;
+ CouchModelFactory* _modelFactory;
}
/** A convenience to instantiate a CouchDatabase directly from a URL, without having to first instantiate a CouchServer.
@@ -43,6 +47,10 @@
@property (readonly) CouchServer* server;
+/** Allows retrieving documents from the CouchDatabase using a different path from the documentID. This is useful for
+ accessing a document using a Couch rewrite. The result of the CouchDocumentPathMap is the path relative to the database. */
+@property (copy) CouchDocumentPathMap documentPathMap;
+
/** Creates the database on the server.
Fails with an HTTP status 412 (Conflict) if a database with this name already exists. */
- (RESTOperation*) create;
@@ -77,7 +85,7 @@
/** Bulk-writes multiple documents in one HTTP call.
@param properties An array specifying the new properties of each item in revisions. Each item must be an NSDictionary, or an NSNull object which means to delete the corresponding document.
- @param revisions A parallel array to 'properties', containing each CouchRevision to be updated. Can be nil, in which case the method acts as described in the docs for -putChanges:. */
+ @param revisions A parallel array to 'properties', containing each CouchRevision or CouchDocument to be updated. Can be nil, in which case the method acts as described in the docs for -putChanges:. */
- (RESTOperation*) putChanges: (NSArray*)properties toRevisions: (NSArray*)revisions;
/** Bulk-writes multiple documents in one HTTP call.
@@ -133,17 +141,13 @@
/** Triggers replication from a source database, to this database.
@param sourceURL The URL of the database to replicate from.
- @param options Zero or more option flags affecting the replication.
- @return The CouchReplication object managing the replication. It will already have been started. */
-- (CouchReplication*) pullFromDatabaseAtURL: (NSURL*)sourceURL
- options: (CouchReplicationOptions)options;
+ @return The CouchReplication object managing the replication. You have a chance to customize its properties (like .continuous and .filter) before it starts. */
+- (CouchReplication*) pullFromDatabaseAtURL: (NSURL*)sourceURL;
/** Triggers replication from this database to a target database.
@param targetURL The URL of the database to replicate to.
- @param options Zero or more option flags affecting the replication.
- @return The CouchReplication object managing the replication. It will already have been started. */
-- (CouchReplication*) pushToDatabaseAtURL: (NSURL*)targetURL
- options: (CouchReplicationOptions)options;
+ @return The CouchReplication object managing the replication. You have a chance to customize its properties (like .continuous and .filter) before it starts.*/
+- (CouchReplication*) pushToDatabaseAtURL: (NSURL*)targetURL;
/** Configures this database to replicate bidirectionally (sync to and from) a database at the given URL.
@param otherURL The URL of the other database, or nil to indicate no replication.
@@ -31,9 +31,11 @@ extern NSString* const kCouchLanguageErlang;
NSString* _language;
NSMutableDictionary* _views;
NSString* _validation;
+ NSMutableDictionary* _viewOptions;
NSString* _viewsRevisionID;
BOOL _changed;
BOOL _changedValidation;
+ BOOL _changedViewOptions;
RESTOperation* _savingOp;
}
@@ -79,6 +81,11 @@ extern NSString* const kCouchLanguageErlang;
/** The validation function, a JavaScript function that validates document contents. */
@property (copy) NSString* validation;
+/** Should view query results include the document local sequence number in the index?
+ Setting this to YES sets the 'local_seq' property of the design document's 'options' property to 'true'.
+ This affects every view in this design document. */
+@property (nonatomic, assign) BOOL includeLocalSequence;
+
/** Have the contents of the design document been changed in-memory but not yet saved? */
@property (readonly) BOOL changed;
@@ -8,21 +8,21 @@
#import "CouchDesignDocument.h"
#ifdef COUCHCOCOA_IMPL
-#import "CouchbaseCallbacks.h"
-#elif TARGET_OS_IPHONE
-#import <Couchbase/CouchbaseCallbacks.h>
+typedef id TDMapBlock;
+typedef id TDReduceBlock;
+typedef id TDFilterBlock;
+typedef id TDValidationBlock;
#else
-#import <CouchbaseMac/CouchbaseCallbacks.h>
+#import <TouchDB/TDDatabase+Insertion.h>
+#import <TouchDB/TDView.h>
#endif
-extern NSString* const kCouchLanguageObjectiveC;
-
-
#define MAPBLOCK(BLOCK) ^(NSDictionary* doc, void (^emit)(id key, id value)){BLOCK}
#define REDUCEBLOCK(BLOCK) ^id(NSArray* keys, NSArray* values, BOOL rereduce){BLOCK}
-#define VALIDATIONBLOCK(BLOCK) ^BOOL(NSDictionary* doc, id<CouchbaseValidationContext> context)\
+#define VALIDATIONBLOCK(BLOCK) ^BOOL(TDRevision* newRevision, id<TDValidationContext> context)\
{BLOCK}
+#define FILTERBLOCK(BLOCK) ^BOOL(TDRevision* revision) {BLOCK}
/** Optional support for native Objective-C map/reduce functions.
@@ -35,15 +35,20 @@ extern NSString* const kCouchLanguageObjectiveC;
It is very important that this block be a law-abiding map function! As in other languages, it must be a "pure" function, with no side effects, that always emits the same values given the same input document. That means that it should not access or change any external state; be careful, since blocks make that so easy that you might do it inadvertently!
The block may be called on any thread, or on multiple threads simultaneously. This won't be a problem if the code is "pure" as described above, since it will as a consequence also be thread-safe. */
- (void) defineViewNamed: (NSString*)viewName
- mapBlock: (CouchMapBlock)mapBlock;
+ mapBlock: (TDMapBlock)mapBlock
+ version: (NSString*)version;
/** Defines or deletes a native view with both a map and a reduce function.
For details, read the documentation of the -defineViewNamed:mapBlock: method.*/
- (void) defineViewNamed: (NSString*)viewName
- mapBlock: (CouchMapBlock)mapBlock
- reduceBlock: (CouchReduceBlock)reduceBlock;
+ mapBlock: (TDMapBlock)mapBlock
+ reduceBlock: (TDReduceBlock)reduceBlock
+ version: (NSString*)version;
+
+- (void) defineFilterNamed: (NSString*)filterName
+ block: (TDFilterBlock)filterBlock;
-/** An Objective-C block that can validate any document being added/updated to this database. */
-@property (copy) CouchValidateUpdateBlock validationBlock;
+/** An Objective-C block that can validate any document being added/updated/deleted in this database. */
+- (void) setValidationBlock: (TDValidationBlock)validationBlock;
@end
@@ -26,6 +26,7 @@
BOOL _isDeleted;
NSString* _currentRevisionID;
CouchRevision* _currentRevision;
+ NSString *_documentID;
}
/** The unique ID of this document; its key in the database. */
@@ -14,6 +14,9 @@
The dynamic accessors will be bridged to calls to -getValueOfProperty: and setValue:ofProperty:, allowing you to easily store property values in an NSDictionary or other container. */
@interface CouchDynamicObject : NSObject
+/** Returns the names of all properties defined in this class and superclasses up to CouchDynamicObject. */
++ (NSSet*) propertyNames;
+
/** Returns the value of a named property.
This method will only be called for properties that have been declared in the class's @interface using @property.
You must override this method -- the base implementation just raises an exception.*/
@@ -27,6 +30,15 @@
- (BOOL) setValue: (id)value ofProperty: (NSString*)property;
+// FOR SUBCLASSES TO CALL:
+
+/** Given the name of an object-valued property, returns the class of the property's value.
+ Returns nil if the property doesn't exist, or if its type isn't an object pointer or is 'id'. */
++ (Class) classOfProperty: (NSString*)propertyName;
+
++ (NSString*) getterKey: (SEL)sel;
++ (NSString*) setterKey: (SEL)sel;
+
// ADVANCED STUFF FOR SUBCLASSES TO OVERRIDE:
+ (IMP) impForGetterOfClass: (Class)propertyClass;
@@ -1,50 +0,0 @@
-//
-// CouchEmbeddedServer.h
-// CouchCocoa
-//
-// Created by Jens Alfke on 10/13/11.
-// Copyright (c) 2011 Couchbase, Inc. All rights reserved.
-//
-
-#import "CouchServer.h"
-@class CouchbaseMobile;
-
-/** A convenience class that glues Couchbase Mobile into CouchCocoa.
- On creation, starts up an instance of Couchbase (or CouchbaseMobile).
- This object will have a placeholder URL until the embedded server has started up, so you can't access it (i.e. creating any databases) until then. */
-@interface CouchEmbeddedServer : CouchServer
-{
- @private
- CouchbaseMobile* _couchbase;
- NSError* _error;
- void(^_onStartBlock)();
-}
-
-/** A shared per-process instance. Remember that CouchCocoa is not thread-safe so you can't
- use this shared instance among multiple threads. */
-+ (CouchEmbeddedServer*) sharedInstance;
-
-/** Preferred initializer. Starts up an in-process server. */
-- (id) init;
-
-/** Inherited initializer, if you want to connect to a remote server for debugging purposes.
- (If you call -start:, the block will still be called.) */
-- (id) initWithURL:(NSURL *)url;
-
-/** Starts the server, asynchronously.
- @param onStartBlock A block to be called when the server finishes starting up (or fails to). At that point you can start to access databases, etc.
- @return YES if startup began, NO if a fatal error occurred. */
-- (BOOL) start: (void(^)())onStartBlock;
-
-/** Is the embedded Couchbase server running? */
-@property (readonly) BOOL running;
-
-/** If the server fails to start up, this will give the reason why. */
-@property (readonly, retain) NSError* error;
-
-@end
-
-
-extern NSString* const CouchEmbeddedServerWillSuspendNotification;
-extern NSString* const CouchEmbeddedServerDidStartNotification;
-extern NSString* const CouchEmbeddedServerDidRestartNotification;
@@ -14,7 +14,8 @@
There's a 1::1 mapping between these and CouchDocuments; call +modelForDocument: to get (or create) a model object for a document, and .document to get the document of a model.
You should subclass this and declare properties in the subclass's @interface. As with NSManagedObject, you don't need to implement their accessor methods or declare instance variables; simply note them as '@dynamic' in the @implementation. The property value will automatically be fetched from or stored to the document, using the same name.
Supported scalar types are bool, char, short, int, double. These map to JSON numbers, except 'bool' which maps to JSON 'true' and 'false'. (Use bool instead of BOOL.)
- Supported object types are NSString, NSNumber, NSData, NSDate, NSArray, NSDictionary. (NSData and NSDate are not native JSON; they will be automatically converted to/from strings in base64 and ISO date formats, respectively.) */
+ Supported object types are NSString, NSNumber, NSData, NSDate, NSArray, NSDictionary. (NSData and NSDate are not native JSON; they will be automatically converted to/from strings in base64 and ISO date formats, respectively.)
+ Additionally, a property's type can be a pointer to a CouchModel subclass. This provides references between model objects. The raw property value in the document must be a string whose value is interpreted as a document ID. */
@interface CouchModel : CouchDynamicObject
{
@private
@@ -30,7 +31,9 @@
}
/** Returns the CouchModel associated with a CouchDocument, or creates & assigns one if necessary.
- Don't call this on CouchModel itself, rather on the subclass you want to instantiate for that document, e.g. [MyWidgetModel modelForDocument: doc]. It always returns an instance of the class it's called on. */
+ If the CouchDocument already has an associated model, it's returned. Otherwise a new one is instantiated.
+ If you call this on CouchModel itself, it'll delegate to the CouchModelFactory to decide what class to instantiate; this lets you map different classes to different "type" property values, for instance.
+ If you call this method on a CouchModel subclass, it will always instantiate an instance of that class; e.g. [MyWidgetModel modelForDocument: doc] always creates a MyWidgetModel. */
+ (id) modelForDocument: (CouchDocument*)document;
/** Creates a new "untitled" model with a new unsaved document.
@@ -77,6 +80,14 @@
This value can be used to highlight recently-changed objects in the UI. */
@property (readonly) NSTimeInterval timeSinceExternallyChanged;
+/** Bulk-saves changes to multiple model objects (which must all be in the same database).
+ This invokes -[CouchDatabase putChanges:], which sends a single request to _bulk_docs.
+ Any unchanged models in the array are ignored.
+ @param models An array of CouchModel objects, which must all be in the same database.
+ @return A RESTOperation that saves all changes, or nil if none of the models need saving. */
++ (RESTOperation*) saveModels: (NSArray*)models;
+
+/** Resets the timeSinceExternallyChanged property to zero. */
- (void) markExternallyChanged;
#pragma mark - PROPERTIES & ATTACHMENTS:
@@ -126,4 +137,8 @@
This happens both when initialized from a document, and after an external change. */
- (void) didLoadFromDocument;
+/** Returns the database in which to look up the document ID of a model-valued property.
+ Defaults to the same database as the receiver's document. You should override this if a document property contains the ID of a document in a different database. */
+- (CouchDatabase*) databaseForModelProperty: (NSString*)propertyName;
+
@end
@@ -7,6 +7,7 @@
//
#import "CouchModel.h"
+#import "CouchReplication.h"
/** Possible current states of a replication. */
@@ -27,6 +28,8 @@ typedef enum {
CouchReplicationState _state;
unsigned _completed, _total;
NSString* _statusString;
+ NSError* _error;
+ CouchReplicationMode _mode;
}
/** The source URL for the replication.
@@ -37,6 +40,8 @@ typedef enum {
This will be either a complete HTTP(s) URL or the name of a database on this server. */
@property (readonly, copy) NSString* target;
+@property (readonly) NSURL* remoteURL;
+
/** Should the target database be created if it doesn't already exist? (Defaults to NO). */
@property bool create_target;
@@ -52,6 +57,23 @@ typedef enum {
Should be a JSON-compatible dictionary. */
@property (copy) NSDictionary* query_params;
+/** Sets the documents to specify as part of the replication. */
+@property (copy) NSArray *doc_ids;
+
+/** Sets the "user_ctx" property of the replication, which identifies what privileges it will run with when accessing the local server. To replicate design documents, this should be set to a value with "_admin" in the list of roles.
+ The server will not let you specify privileges you don't have, so the request to create the replication must be made with credentials that match what you're setting here, unless the server is in no-authentication "admin party" mode.
+ See <https://gist.github.com/832610>, section 8, for details.
+ If both 'user' and 'roles' are nil, the user_ctx will be cleared.
+ @param user A server username, or nil
+ @param roles An array of CouchDB role name strings, or nil */
+- (void) actAsUser: (NSString*)username withRoles: (NSArray*)roles;
+
+/** A convenience that calls -actAsUser:withRoles: to specify the _admin role. */
+- (void) actAsAdmin;
+
+/** Restarts a replication; this is most useful to make a non-continuous replication run again after it's stopped. */
+- (void) restart;
+
/** The current state of replication activity. */
@property (readonly) CouchReplicationState state;
@@ -61,4 +83,8 @@ typedef enum {
/** The total number of changes to be processed, if the task is active, else 0 (observable). */
@property (nonatomic, readonly) unsigned total;
+@property (nonatomic, readonly, retain) NSError* error;
+
+@property (nonatomic, readonly) CouchReplicationMode mode;
+
@end
Oops, something went wrong.

0 comments on commit 5316c04

Please sign in to comment.