SecondaryIndex

  • Undocumented

    See more

    Declaration

    Objective-C

    @interface YapDatabaseSecondaryIndexConnection : YapDatabaseExtensionConnection
    
    /**
     * Returns the parent instance.
     */
    @property (nonatomic, strong, readonly) YapDatabaseSecondaryIndex *secondaryIndex;
    
    /**
     * The queryCache speeds up the transaction methods. (enumerateXMatchingQuery:usingBlock:)
     *
     * In order for a query to be executed, it first has to be compiled by SQLite into an executable routine.
     * The queryCache stores these compiled reusable routines, so that repeated queries can be executed faster.
     *
     * Please note that, in terms of caching, only the queryString matters. The queryParameters do not.
     * That is, if you use the same queryString over and over, but with different parameters,
     * you will get a nice benefit from caching as it will be able to recyle the compiled routine,
     * and simply bind the different parameters each time.
     *
     * By default the queryCache is enabled and has a limit of 10.
     *
     * To disable the cache entirely, set queryCacheEnabled to NO.
     * To use an inifinite cache size, set the queryCacheLimit to ZERO.
     */
    @property (atomic, assign, readwrite) BOOL queryCacheEnabled;
    @property (atomic, assign, readwrite) NSUInteger queryCacheLimit;
    
    @end

    Swift

    class YapDatabaseSecondaryIndexConnection : YapDatabaseExtensionConnection
  • Undocumented

    See more

    Declaration

    Objective-C

    @interface YapDatabaseSecondaryIndexTransaction : YapDatabaseExtensionTransaction
    
    /**
     * These methods allow you to enumerates matches from the secondary index(es) using a given query.
     *
     * The query that you input is an SQL style query (appropriate for SQLite semantics),
     * excluding the "SELECT ... FROM 'tableName'" component.
     * 
     * For example:
     * 
     * query = [YapDatabaseQuery queryWithFormat:@"WHERE age >= 62"];
     * [[transaction ext:@"idx"] enumerateKeysMatchingQuery:query usingBlock:^(NSString *key, BOOL *stop) {
     * 
     *     // ...
     * }];
     *
     * You can also pass parameters to the query using the standard SQLite placeholder:
     * 
     * query = [YapDatabaseQuery queryWithFormat:@"WHERE age >= ? AND state == ?", @(age), state];
     * [[transaction ext:@"idx"] enumerateKeysMatchingQuery:query usingBlock:^(NSString *key, BOOL *stop) {
     *
     *     // ...
     * }];
     *
     * For more information, and more examples, please see YapDatabaseQuery.
     * 
     * @return NO if there was a problem with the given query. YES otherwise.
     * 
     * @see YapDatabaseQuery
     */
    
    - (BOOL)enumerateKeysMatchingQuery:(YapDatabaseQuery *)query
                            usingBlock:(void (NS_NOESCAPE^)(NSString *collection, NSString *key, BOOL *stop))block;
    
    - (BOOL)enumerateKeysAndMetadataMatchingQuery:(YapDatabaseQuery *)query
                                       usingBlock:
                                (void (NS_NOESCAPE^)(NSString *collection, NSString *key, __nullable id metadata, BOOL *stop))block;
    
    - (BOOL)enumerateKeysAndObjectsMatchingQuery:(YapDatabaseQuery *)query
                                      usingBlock:
                                (void (NS_NOESCAPE^)(NSString *collection, NSString *key, id object, BOOL *stop))block;
    
    - (BOOL)enumerateRowsMatchingQuery:(YapDatabaseQuery *)query
                            usingBlock:
                                (void (NS_NOESCAPE^)(NSString *collection, NSString *key, id object, __nullable id metadata, BOOL *stop))block;
    
    - (BOOL)enumerateIndexedValuesInColumn:(NSString *)column matchingQuery:(YapDatabaseQuery *)query usingBlock:(void(NS_NOESCAPE^)(id indexedValue, BOOL *stop))block;
    
    /**
     * Skips the enumeration process, and just gives you the count of matching rows.
     */
    - (BOOL)getNumberOfRows:(NSUInteger *)count matchingQuery:(YapDatabaseQuery *)query;
    
    /**
     * Aggregate Queries.
     * 
     * E.g.: avg, max, min, sum
     * 
     * For more inforation, see the sqlite docs on "Aggregate Functions":
     * https://www.sqlite.org/lang_aggfunc.html
     */
    - (nullable id)performAggregateQuery:(YapDatabaseQuery *)query;
    
    /**
     * This method assists in performing a query over a subset of rows,
     * where the subset is a known set of keys.
     * 
     * For example:
     * 
     * Say you have a bunch of tracks & playlist objects in the database.
     * And you've added a secondary index on track.duration.
     * Now you want to quickly figure out the duration of an entire playlist.
     * 
     * NSArray *keys = [self trackKeysInPlaylist:playlist];
     * NSArray *rowids = [[[transaction ext:@"idx"] rowidsForKeys:keys inCollection:@"tracks"] allValues];
     *
     * YapDatabaseQuery *query =
     *   [YapDatabaseQuery queryWithAggregateFunction:@"SUM(duration)" format:@"WHERE rowid IN (?)", rowids];
     */
    - (NSDictionary<NSString*, NSNumber*> *)rowidsForKeys:(NSArray<NSString *> *)keys
                                             inCollection:(nullable NSString *)collection;
    
    @end

    Swift

    class YapDatabaseSecondaryIndexTransaction : YapDatabaseExtensionTransaction
  • The handler block handles extracting the column values for the secondary indexes.

    When you add or update rows in the databse the block is invoked. Your block can inspect the row and determine if it contains any values that should be added to the secondary indexes. If not, the block can simply return. Otherwise the block should extract any values and add them to the given dictionary.

    After the block returns, the dictionary parameter will be inspected, and any set values will be automatically inserted/updated within the sqlite indexes.

    You should choose a block type that takes the minimum number of required parameters. The extension can make various optimizations based on required parameters of the block. For example, if metadata isn’t required, then the extension can ignore metadata-only updates.

    See more

    Declaration

    Objective-C

    @interface YapDatabaseSecondaryIndexHandler : NSObject

    Swift

    class YapDatabaseSecondaryIndexHandler : NSObject
  • Undocumented

    See more

    Declaration

    Objective-C

    @interface YapDatabaseSecondaryIndexSetup : NSObject <NSCopying, NSFastEnumeration>
    
    - (id)init;
    - (id)initWithCapacity:(NSUInteger)capacity;
    
    - (void)addColumn:(NSString *)name withType:(YapDatabaseSecondaryIndexType)type;
    
    - (NSUInteger)count;
    - (nullable YapDatabaseSecondaryIndexColumn *)columnAtIndex:(NSUInteger)index;
    
    - (NSArray<NSString *> *)columnNames;
    
    @end

    Swift

    class YapDatabaseSecondaryIndexSetup : NSObject, NSCopying, NSFastEnumeration