single queries for domains to prevent running into the cursor limit

This commit is contained in:
likeazir 2024-11-13 17:49:30 +01:00
parent 86e369201a
commit 6779b5cc43
1 changed files with 31 additions and 30 deletions

View File

@ -382,13 +382,13 @@ public class AccountSessionManager{
}
private void readInstanceInfo(SQLiteDatabase db, Set<String> domains){
//limit emoji string to 0.5MB characters to stay under 1MB cursor limit
try(Cursor cursor=db.rawQuery("SELECT domain, instance_obj, substring(emojis,0,500000) AS emojis, length(emojis) AS emojiLength, last_updated, version FROM instances WHERE `domain` IN ("
+ String.join(", ", "?".repeat(domains.size())) + ")", domains.toArray(new String[0]))) {
for(String domain : domains){
final int maxEmojiLength=500000;
try(Cursor cursor=db.rawQuery("SELECT domain, instance_obj, substring(emojis,0,?) AS emojis, length(emojis) AS emojiLength, last_updated, version FROM instances WHERE `domain` = ?",
new String[]{String.valueOf(maxEmojiLength) , domain})) {
ContentValues values=new ContentValues();
while(cursor.moveToNext()){
DatabaseUtils.cursorRowToContentValues(cursor, values);
String domain=values.getAsString("domain");
int version=values.getAsInteger("version");
Instance instance=MastodonAPIController.gson.fromJson(values.getAsString("instance_obj"), switch(version){
case 1 -> InstanceV1.class;
@ -399,10 +399,10 @@ public class AccountSessionManager{
emojiSB.append(values.getAsString("emojis"));
//get emoji in chunks of 1MB if it didn't fit in the first query
int emojiStringLength=values.getAsInteger("emojiLength");
if(emojiStringLength > 500000){
int pagesize=1000000;
for(int start = 500000; start < emojiStringLength; start += pagesize){
try(Cursor emojiCursor=db.rawQuery("select substr(emojis,?, ?) from instances where `domain` = ?", new String[]{String.valueOf(start), String.valueOf(pagesize), domain})){
if(emojiStringLength>maxEmojiLength){
final int pagesize=1000000;
for(int start=maxEmojiLength; start<emojiStringLength; start+=pagesize){
try(Cursor emojiCursor=db.rawQuery("SELECT substr(emojis,?, ?) FROM instances WHERE `domain` = ?", new String[]{String.valueOf(start), String.valueOf(pagesize), domain})){
emojiCursor.moveToNext();
emojiSB.append(emojiCursor.getString(0));
}
@ -417,6 +417,7 @@ public class AccountSessionManager{
Log.d(TAG, "readInstanceInfo failed", ex);
return;
}
}
if(!loadedInstances){
loadedInstances=true;
MastodonAPIController.runInBackground(()->maybeUpdateInstanceInfo(domains));