Skip to content

Commit

Permalink
Fix per code review
Browse files Browse the repository at this point in the history
* Fixed bug that doesn’t return NOT_FOUND status when compiling the design doc and there is no design doc or map function defined. Also support the case that there is a native design doc like view defined, then the design doc is queries through the REST API.

* Added isDesignDoc flag to bypass design doc compilation process (check existing and version) if the view is not a design doc view.

#1758
  • Loading branch information
pasin committed Sep 28, 2017
1 parent 6923714 commit a64f5af
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
15 changes: 11 additions & 4 deletions Source/API/CBLView.m
Expand Up @@ -92,7 +92,7 @@ - (instancetype) initWithDatabase: (CBLDatabase*)db name: (NSString*)name create
}


@synthesize name=_name, storage=_storage, collation=_collation;
@synthesize name=_name, storage=_storage, collation=_collation, isDesignDoc=_isDesignDoc;


- (NSString*) description {
Expand Down Expand Up @@ -151,11 +151,18 @@ - (CBLMapBlock) registeredMapBlock {
}

- (CBLMapBlock) mapBlock {
CBLMapBlock map = self.registeredMapBlock;
// Invoke view compiler if it's available:
if (!map && [self respondsToSelector: @selector(compileFromDesignDoc)])
CBLMapBlock map;
if (_isDesignDoc) {
if ([self compileFromDesignDoc] == kCBLStatusOK)
map = self.registeredMapBlock;
} else {
map = self.registeredMapBlock;
// Invoke view compiler if it's available:
if (!map && [self respondsToSelector: @selector(compileFromDesignDoc)]) {
if ([self compileFromDesignDoc] == kCBLStatusOK)
map = self.registeredMapBlock;
}
}
return map;
}

Expand Down
2 changes: 2 additions & 0 deletions Source/CBLView+Internal.h
Expand Up @@ -53,6 +53,8 @@ BOOL CBLQueryRowValueIsEntireDoc(id value);

@property (readonly) NSArray* viewsInGroup;

@property (nonatomic) BOOL isDesignDoc;

/** Updates the view's index (incrementally) if necessary.
If the index is updated, the other views in the viewGroup will be updated as a bonus.
@return 200 if updated, 304 if already up-to-date, else an error code */
Expand Down
15 changes: 9 additions & 6 deletions Source/CBLView+REST.m
Expand Up @@ -16,17 +16,16 @@ @implementation CBLView (REST)


- (CBLStatus) compileFromDesignDoc {
if (!self.isDesignDoc && self.registeredMapBlock) /* Native design doc like view */
return kCBLStatusOK;

// see if there's a design doc with a CouchDB-style view definition we can compile:
NSString* language;
NSDictionary* viewProps = $castIf(NSDictionary, [self.database getDesignDocFunction: self.name
key: @"views"
language: &language]);
if (!viewProps) {
if (self.registeredMapBlock != nil)
return kCBLStatusOK;
else
return kCBLStatusNotFound;
}
if (!viewProps)
return kCBLStatusNotFound;

LogTo(View, @"%@: Attempting to compile %@ from design doc", self.name, language);
if (![CBLView compiler])
Expand Down Expand Up @@ -71,6 +70,10 @@ - (CBLStatus) compileFromProperties: (NSDictionary*)viewProps language: (NSStrin
NSDictionary* options = $castIf(NSDictionary, viewProps[@"options"]);
self.collation = ($equal(options[@"collation"], @"raw")) ? kCBLViewCollationRaw
: kCBLViewCollationUnicode;

// Mark as a design doc view:
self.isDesignDoc = YES;

return kCBLStatusOK;
}

Expand Down
2 changes: 0 additions & 2 deletions Unit-Tests/Router_Tests.m
Expand Up @@ -586,8 +586,6 @@ - (void) test_UpdateMapFunction {
}
}, kCBLStatusCreated, nil);

NSLog(@"%@", result);

// Query view and check the result:
id null = [NSNull null];
Send(self, @"GET", @"/db/_design/design/_view/view", kCBLStatusOK,
Expand Down

0 comments on commit a64f5af

Please sign in to comment.