Path: blob/main/SignalServiceKit/Storage/TSYapDatabaseObject.m
1 views
//1// Copyright 2017 Signal Messenger, LLC2// SPDX-License-Identifier: AGPL-3.0-only3//45#import "TSYapDatabaseObject.h"6#import <SignalServiceKit/SignalServiceKit-Swift.h>78NS_ASSUME_NONNULL_BEGIN910@interface TSYapDatabaseObject ()1112@property (nonatomic) NSString *uniqueId;13@property (atomic, nullable) NSNumber *grdbId;1415@end1617#pragma mark -1819@implementation TSYapDatabaseObject2021+ (NSString *)generateUniqueId22{23return [[NSUUID UUID] UUIDString];24}2526- (instancetype)init27{28return [self initWithUniqueId:[[self class] generateUniqueId]];29}3031- (instancetype)initWithUniqueId:(NSString *)uniqueId32{33self = [super init];34if (!self) {35return self;36}3738if (uniqueId.length > 0) {39_uniqueId = uniqueId;40} else {41OWSFailDebug(@"Invalid uniqueId.");42_uniqueId = [[self class] generateUniqueId];43}4445return self;46}4748- (instancetype)initWithGrdbId:(int64_t)grdbId uniqueId:(NSString *)uniqueId49{50self = [super init];51if (!self) {52return self;53}5455if (uniqueId.length > 0) {56_uniqueId = uniqueId;57} else {58OWSFailDebug(@"Invalid uniqueId.");59_uniqueId = [[self class] generateUniqueId];60}6162_grdbId = @(grdbId);6364return self;65}6667- (void)encodeIdsWithCoder:(NSCoder *)coder68{69NSNumber *grdbId = self.grdbId;70if (grdbId != nil) {71[coder encodeObject:grdbId forKey:@"grdbId"];72}73NSString *uniqueId = self.uniqueId;74if (uniqueId != nil) {75[coder encodeObject:uniqueId forKey:@"uniqueId"];76}77}7879- (nullable instancetype)initWithCoder:(NSCoder *)coder80{81self = [super init];82if (!self) {83return self;84}85self->_grdbId = [coder decodeObjectOfClass:[NSNumber class] forKey:@"grdbId"];86self->_uniqueId = [coder decodeObjectOfClass:[NSString class] forKey:@"uniqueId"];8788if (_uniqueId.length < 1) {89OWSFailDebug(@"Invalid uniqueId.");90_uniqueId = [[NSUUID UUID] UUIDString];91}9293return self;94}9596- (NSUInteger)hash97{98NSUInteger result = 0;99result ^= self.grdbId.hash;100result ^= self.uniqueId.hash;101return result;102}103104- (BOOL)isEqual:(id)other105{106if (![other isMemberOfClass:self.class]) {107return NO;108}109TSYapDatabaseObject *typedOther = (TSYapDatabaseObject *)other;110if (![NSObject isObject:self.grdbId equalToObject:typedOther.grdbId]) {111return NO;112}113if (![NSObject isObject:self.uniqueId equalToObject:typedOther.uniqueId]) {114return NO;115}116return YES;117}118119- (id)copyAndAssignIdsWithZone:(nullable NSZone *)zone120{121TSYapDatabaseObject *result = [[[self class] allocWithZone:zone] init];122result->_grdbId = self.grdbId;123result->_uniqueId = self.uniqueId;124return result;125}126127#pragma mark -128129- (BOOL)shouldBeSaved130{131return YES;132}133134#pragma mark - Write Hooks135136- (void)anyWillInsertWithTransaction:(DBWriteTransaction *)transaction137{138// Do nothing.139}140141- (void)anyDidInsertWithTransaction:(DBWriteTransaction *)transaction142{143// Do nothing.144}145146- (void)anyWillUpdateWithTransaction:(DBWriteTransaction *)transaction147{148// Do nothing.149}150151- (void)anyDidUpdateWithTransaction:(DBWriteTransaction *)transaction152{153// Do nothing.154}155156- (void)anyWillRemoveWithTransaction:(DBWriteTransaction *)transaction157{158// Do nothing.159}160161- (void)anyDidRemoveWithTransaction:(DBWriteTransaction *)transaction162{163// Do nothing.164}165166#pragma mark - SDSRecordDelegate167168- (void)updateRowId:(int64_t)rowId169{170if (self.grdbId != nil) {171OWSAssertDebug(self.grdbId.longLongValue == rowId);172OWSFailDebug(@"grdbId set more than once.");173}174self.grdbId = @(rowId);175}176177- (void)clearRowId178{179self.grdbId = nil;180}181182- (void)replaceRowId:(int64_t)rowId uniqueId:(NSString *)uniqueId183{184self.grdbId = @(rowId);185self.uniqueId = [uniqueId copy];186}187188@end189190NS_ASSUME_NONNULL_END191192193