Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added support for getting filtered _changes feed, in TDRouter.

  • Loading branch information...
commit 40e53acdad9ad4486bef8c3ccc3dc810c1d1d130 1 parent 06a899b
@snej snej authored
View
6 Demo-Mac/DemoAppController.m
@@ -77,6 +77,12 @@ - (void) applicationDidFinishLaunching: (NSNotification*)n {
}
return YES;
}];
+
+ // And why not a filter, just to allow some simple testing of filtered _changes.
+ // For example, try curl -i 'http://localhost:8888/demo-shopping/_changes?filter=checked'
+ [tdb defineFilter: @"checked" asBlock: ^BOOL(TDRevision *revision) {
+ return [revision.properties objectForKey: @"check"] == $true;
+ }];
CouchQuery* q = [[_database designDocumentWithName: @"default"] queryViewNamed: @"byDate"];
View
5 Source/TDDatabase.h
@@ -40,6 +40,7 @@ typedef BOOL (^TDFilterBlock) (TDRevision* revision);
NSInteger _transactionLevel;
NSMutableDictionary* _views;
NSMutableArray* _validations;
+ NSMutableDictionary* _filters;
TDBlobStore* _attachments;
NSMutableArray* _activeReplicators;
}
@@ -96,6 +97,10 @@ typedef BOOL (^TDFilterBlock) (TDRevision* revision);
options: (const struct TDQueryOptions*)options
filter: (TDFilterBlock)filter;
+/** Define a named filter function. These aren't used directly by TDDatabase, but they're looked up by TDRouter when a _changes request has a ?filter parameter. */
+- (void) defineFilter: (NSString*)filterName asBlock: (TDFilterBlock)filterBlock;
+- (TDFilterBlock) filterNamed: (NSString*)filterName;
+
@end
View
13 Source/TDDatabase.m
@@ -201,6 +201,8 @@ - (void) dealloc {
[_views release];
[_activeReplicators release];
[_validations release];
+ [_filters release];
+ [_attachments release];
[super dealloc];
}
@@ -524,6 +526,17 @@ - (TDRevisionList*) changesSinceSequence: (SequenceNumber)lastSequence
}
+- (void) defineFilter: (NSString*)filterName asBlock: (TDFilterBlock)filterBlock {
+ if (!_filters)
+ _filters = [[NSMutableDictionary alloc] init];
+ [_filters setValue: [[filterBlock copy] autorelease] forKey: filterName];
+}
+
+- (TDFilterBlock) filterNamed: (NSString*)filterName {
+ return [_filters objectForKey: filterName];
+}
+
+
#pragma mark - VIEWS:
View
5 Source/TDRouter.h
@@ -6,8 +6,8 @@
// Copyright (c) 2011 Couchbase, Inc. All rights reserved.
//
-#import <Foundation/Foundation.h>
-@class TDDatabase, TDServer, TDResponse, TDBody;
+#import "TDDatabase.h"
+@class TDServer, TDResponse, TDBody;
extern NSString* const kTDVersionString;
@@ -33,6 +33,7 @@ typedef void (^OnFinishedBlock)();
OnDataAvailableBlock _onDataAvailable;
OnFinishedBlock _onFinished;
BOOL _longpoll;
+ TDFilterBlock _changesFilter;
}
- (id) initWithServer: (TDServer*)server request: (NSURLRequest*)request;
View
14 Source/TDRouter.m
@@ -451,6 +451,9 @@ - (void) sendContinuousChange: (TDRevision*)rev {
- (void) dbChanged: (NSNotification*)n {
TDRevision* rev = [n.userInfo objectForKey: @"rev"];
+
+ if (_changesFilter && !_changesFilter(rev))
+ return;
if (_longpoll) {
Log(@"TDRouter: Sending longpoll response");
@@ -474,7 +477,16 @@ - (TDStatus) do_GET_changes: (TDDatabase*)db {
return 400;
int since = [[self query: @"since"] intValue];
- TDRevisionList* changes = [db changesSinceSequence: since options: &options filter: NULL];
+ NSString* filterName = [self query: @"filter"];
+ if (filterName) {
+ _changesFilter = [[_db filterNamed: filterName] retain];
+ if (!_changesFilter)
+ return 404;
+ }
+
+ TDRevisionList* changes = [db changesSinceSequence: since
+ options: &options
+ filter: _changesFilter];
if (!changes)
return 500;
Please sign in to comment.
Something went wrong with that request. Please try again.