Skip to content

SecondaryIndex does not support “partial” or “conditional” datasets #104

@shpakovski

Description

@shpakovski

Hello, I got a use case:

You may want to index only archived objects to restore them later by name. So one would expect something like this to work:

YapDatabaseSecondaryIndexWithObjectBlock block = ^(NSMutableDictionary *dict, NSString *collection, NSString *key, MYCategory *category) {
    if ([category isKindOfClass:[MYCategory class]] && category.archived) {
        dict[MYCategoriesIndexKeyDisplayName] = category.displayName;
    }
};

Later, you query archived objects by MYCategoriesIndexKeyDisplayName, restore them by setting archived to NO, and automatically delete from the index. Unfortunately, this is impossible with current implementation.


Here is why:

The most likely you had set archived property later than the object was stored into the YapDatabase. As result, the SecondaryIndex only updates the row literally generating an UPDATE statement for SQLite without any effect in the table. Here is a workaround:

YapDatabaseSecondaryIndexWithObjectBlock block = ^(NSMutableDictionary *dict, NSString *collection, NSString *key, MYCategory *category) {
    if ([category isKindOfClass:MYCategory.class] && category.archived) {
        dict[MYCategoriesIndexKeyArchived] = @(category.archived);
        dict[MYCategoriesIndexKeyDisplayName] = category.displayName;
    }
};

The problem is that you have to keep an index for all categories potentially increasing search time. I believe it would be more effective to allow “partial” indexes and, as result, to reduce the number of rows in the index database table.


Not sure if this is a bug or a feature, so I updated the Wiki page.

Thanks,
Vadim

P. S. Maybe there is another way to solve this use-case?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions