@@ -1453,48 +1453,49 @@ def impl(txn: LoggingTransaction) -> Tuple[bool, Optional[int]]:
1453
1453
impl ,
1454
1454
)
1455
1455
1456
- async def delete_old_otks_for_one_user (
1457
- self , after_user_id : str
1458
- ) -> Tuple [Optional [str ], int ]:
1459
- """Deletes old OTKs belonging to one user.
1456
+ async def delete_old_otks_for_next_user_batch (
1457
+ self , after_user_id : str , number_of_users : int
1458
+ ) -> Tuple [List [str ], int ]:
1459
+ """Deletes old OTKs belonging to the next batch of users
1460
1460
1461
1461
Returns:
1462
- `(user , rows)`, where:
1463
- * `user ` is the user ID of the updated user, or None if we are don
1462
+ `(users , rows)`, where:
1463
+ * `users ` is the user IDs of the updated users. An empty list if we are done.
1464
1464
* `rows` is the number of deleted rows
1465
1465
"""
1466
1466
1467
- def impl (txn : LoggingTransaction ) -> Tuple [Optional [str ], int ]:
1468
- # Find the next user
1467
+ def impl (txn : LoggingTransaction ) -> Tuple [List [str ], int ]:
1468
+ # Find a batch of users
1469
1469
txn .execute (
1470
1470
"""
1471
- SELECT user_id FROM e2e_one_time_keys_json WHERE user_id > ? LIMIT 1
1471
+ SELECT DISTINCT(user_id) FROM e2e_one_time_keys_json
1472
+ WHERE user_id > ?
1473
+ ORDER BY user_id
1474
+ LIMIT ?
1472
1475
""" ,
1473
- (after_user_id ,),
1476
+ (after_user_id , number_of_users ),
1474
1477
)
1475
- row = txn .fetchone ()
1476
- if not row :
1477
- # We're done!
1478
- return None , 0
1479
- (user_id ,) = row
1478
+ users = [row [0 ] for row in txn .fetchall ()]
1479
+ if len (users ) == 0 :
1480
+ return users , 0
1480
1481
1481
- # Delete any old OTKs belonging to that user .
1482
+ # Delete any old OTKs belonging to those users .
1482
1483
#
1483
1484
# We only actually consider OTKs whose key ID is 6 characters long. These
1484
1485
# keys were likely made by libolm rather than Vodozemac; libolm only kept
1485
1486
# 100 private OTKs, so was far more vulnerable than Vodozemac to throwing
1486
1487
# away keys prematurely.
1487
- txn .execute (
1488
- """
1488
+ clause , args = make_in_list_sql_clause ( txn .database_engine , 'user_id' , users )
1489
+ sql = f """
1489
1490
DELETE FROM e2e_one_time_keys_json
1490
- WHERE user_id = ? AND ts_added_ms < ? AND length(key_id) = 6
1491
- """ ,
1492
- ( user_id , self ._clock .time_msec () - (7 * 24 * 3600 * 1000 )),
1493
- )
1491
+ WHERE { clause } AND ts_added_ms < ? AND length(key_id) = 6
1492
+ """
1493
+ args . append ( self ._clock .time_msec () - (7 * 24 * 3600 * 1000 ))
1494
+ txn . execute ( sql , args )
1494
1495
1495
- return user_id , txn .rowcount
1496
+ return users , txn .rowcount
1496
1497
1497
- return await self .db_pool .runInteraction ("delete_old_otks_for_one_user " , impl )
1498
+ return await self .db_pool .runInteraction ("delete_old_otks_for_next_user_batch " , impl )
1498
1499
1499
1500
1500
1501
class EndToEndKeyStore (EndToEndKeyWorkerStore , SQLBaseStore ):
0 commit comments