From 6779b5cc4399e799f94337839f8fdde82bf02cef Mon Sep 17 00:00:00 2001 From: likeazir Date: Wed, 13 Nov 2024 17:49:30 +0100 Subject: [PATCH] single queries for domains to prevent running into the cursor limit --- .../api/session/AccountSessionManager.java | 61 ++++++++++--------- 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/mastodon/src/main/java/org/joinmastodon/android/api/session/AccountSessionManager.java b/mastodon/src/main/java/org/joinmastodon/android/api/session/AccountSessionManager.java index 00f474be..c5b20678 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/api/session/AccountSessionManager.java +++ b/mastodon/src/main/java/org/joinmastodon/android/api/session/AccountSessionManager.java @@ -382,40 +382,41 @@ public class AccountSessionManager{ } private void readInstanceInfo(SQLiteDatabase db, Set 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]))) { - 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; - case 2 -> InstanceV2.class; - default -> throw new IllegalStateException("Unexpected value: " + version); - }); - StringBuilder emojiSB = new StringBuilder(); - 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})){ - emojiCursor.moveToNext(); - emojiSB.append(emojiCursor.getString(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); + int version=values.getAsInteger("version"); + Instance instance=MastodonAPIController.gson.fromJson(values.getAsString("instance_obj"), switch(version){ + case 1 -> InstanceV1.class; + case 2 -> InstanceV2.class; + default -> throw new IllegalStateException("Unexpected value: "+version); + }); + StringBuilder emojiSB=new StringBuilder(); + 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>maxEmojiLength){ + final int pagesize=1000000; + for(int start=maxEmojiLength; start emojis=MastodonAPIController.gson.fromJson(emojiSB.toString(), new TypeToken>(){}.getType()); + instances.put(domain, instance); + customEmojis.put(domain, groupCustomEmojis(emojis)); + instancesLastUpdated.put(domain, values.getAsLong("last_updated")); } - List emojis=MastodonAPIController.gson.fromJson(emojiSB.toString(), new TypeToken>(){}.getType()); - instances.put(domain, instance); - customEmojis.put(domain, groupCustomEmojis(emojis)); - instancesLastUpdated.put(domain, values.getAsLong("last_updated")); + }catch(Exception ex){ + Log.d(TAG, "readInstanceInfo failed", ex); + return; } - } catch(Exception ex) { - Log.d(TAG, "readInstanceInfo failed", ex); - return; } if(!loadedInstances){ loadedInstances=true;