Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
signalapp
GitHub Repository: signalapp/Signal-iOS
Path: blob/main/SignalServiceKit/Storage/TSYapDatabaseObject.m
1 views
1
//
2
// Copyright 2017 Signal Messenger, LLC
3
// SPDX-License-Identifier: AGPL-3.0-only
4
//
5
6
#import "TSYapDatabaseObject.h"
7
#import <SignalServiceKit/SignalServiceKit-Swift.h>
8
9
NS_ASSUME_NONNULL_BEGIN
10
11
@interface TSYapDatabaseObject ()
12
13
@property (nonatomic) NSString *uniqueId;
14
@property (atomic, nullable) NSNumber *grdbId;
15
16
@end
17
18
#pragma mark -
19
20
@implementation TSYapDatabaseObject
21
22
+ (NSString *)generateUniqueId
23
{
24
return [[NSUUID UUID] UUIDString];
25
}
26
27
- (instancetype)init
28
{
29
return [self initWithUniqueId:[[self class] generateUniqueId]];
30
}
31
32
- (instancetype)initWithUniqueId:(NSString *)uniqueId
33
{
34
self = [super init];
35
if (!self) {
36
return self;
37
}
38
39
if (uniqueId.length > 0) {
40
_uniqueId = uniqueId;
41
} else {
42
OWSFailDebug(@"Invalid uniqueId.");
43
_uniqueId = [[self class] generateUniqueId];
44
}
45
46
return self;
47
}
48
49
- (instancetype)initWithGrdbId:(int64_t)grdbId uniqueId:(NSString *)uniqueId
50
{
51
self = [super init];
52
if (!self) {
53
return self;
54
}
55
56
if (uniqueId.length > 0) {
57
_uniqueId = uniqueId;
58
} else {
59
OWSFailDebug(@"Invalid uniqueId.");
60
_uniqueId = [[self class] generateUniqueId];
61
}
62
63
_grdbId = @(grdbId);
64
65
return self;
66
}
67
68
- (void)encodeIdsWithCoder:(NSCoder *)coder
69
{
70
NSNumber *grdbId = self.grdbId;
71
if (grdbId != nil) {
72
[coder encodeObject:grdbId forKey:@"grdbId"];
73
}
74
NSString *uniqueId = self.uniqueId;
75
if (uniqueId != nil) {
76
[coder encodeObject:uniqueId forKey:@"uniqueId"];
77
}
78
}
79
80
- (nullable instancetype)initWithCoder:(NSCoder *)coder
81
{
82
self = [super init];
83
if (!self) {
84
return self;
85
}
86
self->_grdbId = [coder decodeObjectOfClass:[NSNumber class] forKey:@"grdbId"];
87
self->_uniqueId = [coder decodeObjectOfClass:[NSString class] forKey:@"uniqueId"];
88
89
if (_uniqueId.length < 1) {
90
OWSFailDebug(@"Invalid uniqueId.");
91
_uniqueId = [[NSUUID UUID] UUIDString];
92
}
93
94
return self;
95
}
96
97
- (NSUInteger)hash
98
{
99
NSUInteger result = 0;
100
result ^= self.grdbId.hash;
101
result ^= self.uniqueId.hash;
102
return result;
103
}
104
105
- (BOOL)isEqual:(id)other
106
{
107
if (![other isMemberOfClass:self.class]) {
108
return NO;
109
}
110
TSYapDatabaseObject *typedOther = (TSYapDatabaseObject *)other;
111
if (![NSObject isObject:self.grdbId equalToObject:typedOther.grdbId]) {
112
return NO;
113
}
114
if (![NSObject isObject:self.uniqueId equalToObject:typedOther.uniqueId]) {
115
return NO;
116
}
117
return YES;
118
}
119
120
- (id)copyAndAssignIdsWithZone:(nullable NSZone *)zone
121
{
122
TSYapDatabaseObject *result = [[[self class] allocWithZone:zone] init];
123
result->_grdbId = self.grdbId;
124
result->_uniqueId = self.uniqueId;
125
return result;
126
}
127
128
#pragma mark -
129
130
- (BOOL)shouldBeSaved
131
{
132
return YES;
133
}
134
135
#pragma mark - Write Hooks
136
137
- (void)anyWillInsertWithTransaction:(DBWriteTransaction *)transaction
138
{
139
// Do nothing.
140
}
141
142
- (void)anyDidInsertWithTransaction:(DBWriteTransaction *)transaction
143
{
144
// Do nothing.
145
}
146
147
- (void)anyWillUpdateWithTransaction:(DBWriteTransaction *)transaction
148
{
149
// Do nothing.
150
}
151
152
- (void)anyDidUpdateWithTransaction:(DBWriteTransaction *)transaction
153
{
154
// Do nothing.
155
}
156
157
- (void)anyWillRemoveWithTransaction:(DBWriteTransaction *)transaction
158
{
159
// Do nothing.
160
}
161
162
- (void)anyDidRemoveWithTransaction:(DBWriteTransaction *)transaction
163
{
164
// Do nothing.
165
}
166
167
#pragma mark - SDSRecordDelegate
168
169
- (void)updateRowId:(int64_t)rowId
170
{
171
if (self.grdbId != nil) {
172
OWSAssertDebug(self.grdbId.longLongValue == rowId);
173
OWSFailDebug(@"grdbId set more than once.");
174
}
175
self.grdbId = @(rowId);
176
}
177
178
- (void)clearRowId
179
{
180
self.grdbId = nil;
181
}
182
183
- (void)replaceRowId:(int64_t)rowId uniqueId:(NSString *)uniqueId
184
{
185
self.grdbId = @(rowId);
186
self.uniqueId = [uniqueId copy];
187
}
188
189
@end
190
191
NS_ASSUME_NONNULL_END
192
193