ObjMatrix  Check-in [027eb0e2f8]

Overview
Comment:MTXStorage: Add support for storing joined rooms
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 027eb0e2f8e814fdc47db7a5edc266c4988eff89bb9875e3ff99b2b8f59e3908
User & Date: js on 2020-10-09 22:08:19
Other Links: manifest | tags
Context
2020-10-11
18:34
Track joined rooms check-in: 366f90be1a user: js tags: trunk
2020-10-09
22:08
MTXStorage: Add support for storing joined rooms check-in: 027eb0e2f8 user: js tags: trunk
2020-10-06
20:48
Initial skeleton for handling sync check-in: 4df5567c11 user: js tags: trunk
Changes

Modified src/MTXSQLite3Storage.m from [8d08466130] to [7f2482d98e].

24
25
26
27
28
29
30



31
32
33
34
35
36
37

#import "MTXSQLite3Storage.h"

@implementation MTXSQLite3Storage
{
	SL3Connection *_conn;
	SL3PreparedStatement *_nextBatchSetStatement, *_nextBatchGetStatement;



}

+ (instancetype)storageWithPath: (OFString *)path
{
	return [[[self alloc] initWithPath: path] autorelease];
}








>
>
>







24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

#import "MTXSQLite3Storage.h"

@implementation MTXSQLite3Storage
{
	SL3Connection *_conn;
	SL3PreparedStatement *_nextBatchSetStatement, *_nextBatchGetStatement;
	SL3PreparedStatement *_joinedRoomsAddStatement;
	SL3PreparedStatement *_joinedRoomsRemoveStatement;
	SL3PreparedStatement *_joinedRoomsGetStatement;
}

+ (instancetype)storageWithPath: (OFString *)path
{
	return [[[self alloc] initWithPath: path] autorelease];
}

51
52
53
54
55
56
57












58
59
60
61
62
63
64
65
66
67
68
69
70
71



72
73
74
75
76
77
78
79

80
81
82





83
84
85
86
87
88
89
		    @"    device_id, next_batch\n"
		    @") VALUES (\n"
		    @"    $device_id, $next_batch\n"
		    @")"] retain];
		_nextBatchGetStatement = [[_conn prepareStatement:
		    @"SELECT next_batch FROM next_batch\n"
		    @"WHERE device_id=$device_id"] retain];













		objc_autoreleasePoolPop(pool);
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	[_nextBatchSetStatement release];
	[_nextBatchGetStatement release];



	[_conn release];

	[super dealloc];
}

- (void)createTables
{
	[_conn executeStatement: @"CREATE TABLE IF NOT EXISTS next_batch (\n"

				 @"    device_id TEXT PRIMARY KEY,\n"
				 @"    next_batch TEXT\n"
				 @")"];





}

- (void)transactionWithBlock: (mtx_storage_transaction_block_t)block
{
	[_conn transactionWithBlock: block];
}








>
>
>
>
>
>
>
>
>
>
>
>














>
>
>







|
>
|
|
|
>
>
>
>
>







54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
		    @"    device_id, next_batch\n"
		    @") VALUES (\n"
		    @"    $device_id, $next_batch\n"
		    @")"] retain];
		_nextBatchGetStatement = [[_conn prepareStatement:
		    @"SELECT next_batch FROM next_batch\n"
		    @"WHERE device_id=$device_id"] retain];
		_joinedRoomsAddStatement = [[_conn prepareStatement:
		    @"INSERT INTO joined_rooms (\n"
		    @"    user_id, room_id\n"
		    @") VALUES (\n"
		    @"    $user_id, $room_id\n"
		    @")"] retain];
		_joinedRoomsRemoveStatement = [[_conn prepareStatement:
		    @"DELETE FROM joined_rooms\n"
		    @"WHERE user_id=$user_id AND room_id=$room_id"] retain];
		_joinedRoomsGetStatement = [[_conn prepareStatement:
		    @"SELECT room_id FROM joined_rooms\n"
		    @"WHERE user_id=$user_id"] retain];

		objc_autoreleasePoolPop(pool);
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	[_nextBatchSetStatement release];
	[_nextBatchGetStatement release];
	[_joinedRoomsAddStatement release];
	[_joinedRoomsRemoveStatement release];
	[_joinedRoomsGetStatement release];
	[_conn release];

	[super dealloc];
}

- (void)createTables
{
	[_conn executeStatement:
	    @"CREATE TABLE IF NOT EXISTS next_batch (\n"
	    @"    device_id TEXT PRIMARY KEY,\n"
	    @"    next_batch TEXT\n"
	    @");\n"
	    @"CREATE TABLE IF NOT EXISTS joined_rooms (\n"
	    @"    user_id TEXT,\n"
	    @"    room_id TEXT,\n"
	    @"    PRIMARY KEY (user_id, room_id)\n"
	    @");"];
}

- (void)transactionWithBlock: (mtx_storage_transaction_block_t)block
{
	[_conn transactionWithBlock: block];
}

111
112
113
114
115
116
117
118
119
120
121
122
123

















































124
		@"$device_id": deviceID
	}];

	if (![_nextBatchGetStatement step])
		return nil;

	OFString *nextBatch =
	    [[_nextBatchGetStatement rowDictionary][@"next_batch"] retain];

	objc_autoreleasePoolPop(pool);

	return [nextBatch autorelease];
}

















































@end







|





>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
		@"$device_id": deviceID
	}];

	if (![_nextBatchGetStatement step])
		return nil;

	OFString *nextBatch =
	    [_nextBatchGetStatement.rowDictionary[@"next_batch"] retain];

	objc_autoreleasePoolPop(pool);

	return [nextBatch autorelease];
}

- (void)addJoinedRoom: (OFString *)roomID
	      forUser: (OFString *)userID
{
	void *pool = objc_autoreleasePoolPush();

	[_joinedRoomsAddStatement reset];
	[_joinedRoomsAddStatement bindWithDictionary: @{
		@"$room_id": roomID,
		@"$user_id": userID
	}];
	[_joinedRoomsAddStatement step];

	objc_autoreleasePoolPop(pool);
}

- (void)removeJoinedRoom: (OFString *)roomID
		 forUser: (OFString *)userID
{
	void *pool = objc_autoreleasePoolPush();

	[_joinedRoomsRemoveStatement reset];
	[_joinedRoomsRemoveStatement bindWithDictionary: @{
		@"$room_id": roomID,
		@"$user_id": userID
	}];
	[_joinedRoomsRemoveStatement step];

	objc_autoreleasePoolPop(pool);
}

- (OFArray<OFString *> *)joinedRoomsForUser: (OFString *)userID
{
	OFMutableArray *joinedRooms = [OFMutableArray array];
	void *pool = objc_autoreleasePoolPush();

	[_joinedRoomsGetStatement reset];
	[_joinedRoomsGetStatement bindWithDictionary: @{
		@"$user_id": userID
	}];

	while ([_joinedRoomsGetStatement step])
		[joinedRooms addObject:
		    _joinedRoomsGetStatement.rowDictionary[@"room_id"]];

	objc_autoreleasePoolPop(pool);

	return [joinedRooms autorelease];
}
@end

Modified src/MTXStorage.h from [8e365a3b3e] to [aaa9d2df3c].

54
55
56
57
58
59
60




























61
62
63
 * @brief Returns the next batch for the specified device.
 *
 * @param deviceID The device ID for which to return the next batch
 * @return The next batch for the specified device, or `nil` if none is
 *	   available.
 */
- (nullable OFString *)nextBatchForDeviceID: (OFString *)deviceID;




























@end

OF_ASSUME_NONNULL_END







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
 * @brief Returns the next batch for the specified device.
 *
 * @param deviceID The device ID for which to return the next batch
 * @return The next batch for the specified device, or `nil` if none is
 *	   available.
 */
- (nullable OFString *)nextBatchForDeviceID: (OFString *)deviceID;

/**
 * @brief Adds the specified room ID to the list of joined rooms for the
 *	  specified user ID.
 *
 * @param roomID The room ID to add to the list of joined rooms
 * @param userID The user ID for which to add the room
 */
- (void)addJoinedRoom: (OFString *)roomID
	      forUser: (OFString *)userID;

/**
 * @brief Removes the specified room ID to the list of joined rooms for the
 *	  specified user ID.
 *
 * @param roomID The room ID to add to the list of joined rooms
 * @param userID The user ID for which to add the room
 */
- (void)removeJoinedRoom: (OFString *)roomID
		 forUser: (OFString *)userID;

/**
 * @brief Returns the joined room IDs for the specified user ID.
 *
 * @param userID The user ID for which to return the joined rooms
 * @return The joined room IDs for the specified user ID
 */
- (OFArray<OFString *> *)joinedRoomsForUser: (OFString *)userID;
@end

OF_ASSUME_NONNULL_END