’»°´ ¾²‚°ˆ — Yap Database

  • View
    444

  • Download
    2

Embed Size (px)

Text of ’»°´ ¾²‚°ˆ — Yap Database

  • YapDatabaseKey-Value store and Much More

    Built atop SQLite for iOS & Mac

  • API,

  • Views - UITableViewDatasource

    Secondary Indexes - SQL

    Full Text Search - FTS SQLite

    Relationships - , deletion rules CoreData

    CloudKit - CloudKit (beta)

    Whole Database Encryption - SQLCipher

  • YapDatabase

    ,

  • YapDatabase

  • collection key data metadata

    movies movie_56757 BLOB BLOB

    actors actor_546 BLOB BLOB

    genres genre_13 BLOB BLOB

    movies movie_95456 BLOB BLOB

    primary key - collection, key

  • @{

    @"collection_1": @{

    @"key1": @[object, metadata],

    @"key2": @[object, metadata],

    //...

    },

    @"collection_2": @{

    @"key1": @[object, metadata],

    @"key2": @[object, metadata],

    //...

    }

    //...

    };

    NSDictionary

  • - YapDatabase

    - YapDatabaseConnection

    - YapDatabaseTransaction

  • YapDatabase *database = [[YapDatabase alloc] initWithPath:@"yap_db.sqlite"];

    (YapDatabase) YapDatabase .

  • YapDatabaseConnection *connection = [database newConnection];

    (YapDatabaseConnection) .

  • : YapDatabaseReadTransaction YapDatabaseReadWriteTransaction

    SQLite + snaphot & changeset

    ACID

  • Snapshot & Changeset

    Snapshot - 64- ,

    Changeset - , ,

  • . .

  • //YapDatabaseReadTransaction -

    [connection readWithBlock:^(YapDatabaseReadTransaction *transaction){

    object1 = [transaction objectForKey:object1Key

    inCollection:collection];

    object2 = [transaction objectForKey:object2Key

    inCollection:collection];

    }];

    //YapDatabaseReadWriteTransaction -

    [connection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction){

    object = [transaction objectForKey:objectKey

    inCollection:collection];

    [transaction setObject:newObject

    forKey:newObjectKey inCollection:collection];

    [transaction replaceObject:updatedObject

    forKey:updatedObjectKey inCollection:collection];

    [transaction removeObjectForKey:removedObjectKey inCollection:collection];

    }];

    CRUD

  • NSCoding

  • typedef NSData* (^YapDatabaseSerializer)(NSString *collection, NSString *key, id object);

    typedef id (^YapDatabaseDeserializer)(NSString *collection, NSString *key, NSData *data);

    - (instancetype)initWithPath:(NSString *)path

    serializer:(YapDatabaseSerializer)serializer

    deserializer:(YapDatabaseDeserializer)deserializer;

    - (instancetype)initWithPath:(NSString *)path

    objectSerializer:(YapDatabaseSerializer)objectSerializer

    objectDeserializer:(YapDatabaseDeserializer)objectDeserializer

    metadataSerializer:(YapDatabaseSerializer)metadataSerializer

    metadataDeserializer:(YapDatabaseDeserializer)metadataDeserializer;

  • - Mantle, JSONModel,

    Protobuf, Thrift, Fastcoding, etc

    - lz4, Snappy

  • LRU

  • YapDatabasePolicyContainment -

    YapDatabasePolicyCopy - NSCopying

    YapDatabasePolicyShare

  • Extensions

  • Views

  • @{

    @"books": @[ @[@"fiction", @"key24"],

    @[@"fantasy", @"key7"],

    @[@"mystery", @"key11"] ],

    @"magazines": @[ @[@"gossip", @"key60"],

    @[@"science", @"key49"],

    @[@"travel", @"key82"] ]

    };

    // SQL-

    //WHERE ... (filter)

    //GROUP BY ... (group)

    //ORDER BY ... (sort)

    NSDictionary Collection, Key

  • View ()

  • YapDatabaseViewGrouping *grouping = nil;

    YapDatabaseViewSorting *sotring = nil;

    grouping = [YapDatabaseViewGrouping withRowBlock:groupingBlock];

    sotring = [YapDatabaseViewSorting withRowBlock:sortingBlock];

    YapDatabaseView *myView = [[YapDatabaseView alloc] initWithGrouping:grouping

    sorting:sotring];

    View /

    .

  • groupingBlock = ^NSString *(NSString *collection, NSString *key,

    id object, id metadata){

    if ([object isKindOfClass:[Book class]])

    return @"books";

    if ([object isKindOfClass:[Magazine class]])

    return @"magazines";

    return nil; // exclude from view

    };

    sortingBlock = ^NSComparisonResult (NSString *group,

    NSString *collection1, NSString *key1,

    id obj1, id meta1,

    NSString *collection2, NSString *key2,

    id obj2, id meta2){

    if ([group isEqualToString:@"books"])

    return [obj1 compareBookByTitleThenAuthor:obj2];

    else

    return [obj1 compareMagazineByMonthThenTitle:obj2];

    };

    .

    , .

  • __block id objectForRow = nil;

    [connection readWithBlock:^(YapDatabaseReadTransaction *transaction){

    objectForRow = [[transaction ext:@"myViewName"] objectAtIndex:indexPath.row

    inGroup:@"books"];

    }];

    // View

    (NSUInteger)numberOfGroups;

    (NSArray *)allGroups;

    (NSUInteger)numberOfItemsInGroup:(NSString *)group;

    (NSUInteger)numberOfItemsInAllGroups;

    (id)objectAtIndex:(NSUInteger)keyIndex inGroup:(NSString *)group;

    View

  • View

    YapDatabaseModifiedNotification

    Long-Lived Read Transactions

    Mappings

  • - (void)viewDidLoad

    {

    // ...

    [connection beginLongLivedReadTransaction];

    [[NSNotificationCenter defaultCenter] addObserver:self

    selector:@selector(yapDatabaseModified:)

    name:YapDatabaseModifiedNotification

    object:connection.database];

    // ...

    }

    - (void)yapDatabaseModified:(NSNotification *)notification

    {

    YapDatabaseConnection *connection = nil;

    NSArray *notifications = [connection beginLongLivedReadTransaction];

    }

    YapDatabaseModifiedNotification

    Long-Lived Read Transactions

  • //View@{ @"bond movies": @[ @[@"movies", @"abc123"], @[@"movies", @"xyz123"] ],

    @"star wars": @[ @[@"movies", @"def456"] ],

    @"batman movies": @[ @[@"movies", @"zxc567"], @[@"movies", @"asd567"] ] };

    //Mapping

    //Section 0@[ @[ @[@"movies", @"def456"] ], //Section 1 @[ @[@"movies", @"zxc567"], @[@"movies", @"asd567"] ], //Section 2 @[ @[@"movies", @"abc123"], @[@"movies", @"xyz123"] ] ];

    Mappings (YapDatabaseViewMappings) group-in-a-view section-in-a-table

  • - (NSUInteger)numberOfSections;

    - (NSUInteger)numberOfItemsInSection:(NSUInteger)section;

    - (NSString *)groupForSection:(NSUInteger)section

    //

    - (id)objectAtIndexPath:(NSIndexPath *)indexPath

    withMappings:(YapDatabaseViewMappings *)mappings;

    - (id)objectAtRow:(NSUInteger)row

    inSection:(NSUInteger)section

    withMappings:(YapDatabaseViewMappings *)mappings;

    YapDatabaseViewMappings

  • - (void)yapDatabaseModified:(NSNotification *)notification{ NSArray *notifications = [connection beginLongLivedReadTransaction]; NSArray *sectionChanges = nil, *rowChanges = nil;

    [[connection ext:@"movies"] getSectionChanges:&sectionChanges rowChanges:&rowChanges forNotifications:notifications withMappings:mappings];

    if ([sectionChanges count] == 0 & [rowChanges count] == 0){ // Nothing has changed that affects our tableView return; } // Animate tableView updates ...}

    YapDatabaseViewMappings ?

    , View c mapping-a.

    .

  • Mapping-

    -

    - ,

  • //

    staticMappings = [YapDatabaseViewMappings mappingsWithGroups:@[ @"bond movies",

    @"batman movies" ]

    view:@"movies"];

    //

    [staticMappings setIsReversed:YES forGroup:@"bond movies"];

    //

    YapDatabaseViewMappingGroupFilter filterBlock = nil;

    YapDatabaseViewMappingGroupSort sortBlock = nil;

    filterBlock = ^(NSString *group, YapDatabaseReadTransaction *transaction){

    return YES; // include all movies groups

    };

    sortBlock = ^(NSString *group1, NSString *group2,

    YapDatabaseReadTransaction *transaction){

    return [group1 compare:group2]; // sorted by movie group name

    };

    dynamicMappings = [[YapDatabaseViewMappings alloc] initWithGroupFilterBlock:filterBlock