Load multiple accounts in parallel

This commit is contained in:
AsamK 2022-02-13 20:07:08 +01:00
parent cf0cc50e32
commit 90c787f8e2
4 changed files with 26 additions and 11 deletions

View file

@ -1083,6 +1083,7 @@
"queryAllDeclaredMethods":true, "queryAllDeclaredMethods":true,
"queryAllDeclaredConstructors":true, "queryAllDeclaredConstructors":true,
"methods":[ "methods":[
{"name":"<init>","parameterTypes":["java.lang.String","java.lang.String","org.asamk.signal.manager.storage.stickerPacks.JsonStickerPack$JsonSticker","java.util.List"] },
{"name":"author","parameterTypes":[] }, {"name":"author","parameterTypes":[] },
{"name":"cover","parameterTypes":[] }, {"name":"cover","parameterTypes":[] },
{"name":"stickers","parameterTypes":[] }, {"name":"stickers","parameterTypes":[] },
@ -1095,6 +1096,7 @@
"queryAllDeclaredMethods":true, "queryAllDeclaredMethods":true,
"queryAllDeclaredConstructors":true, "queryAllDeclaredConstructors":true,
"methods":[ "methods":[
{"name":"<init>","parameterTypes":["java.lang.Integer","java.lang.String","java.lang.String","java.lang.String"] },
{"name":"contentType","parameterTypes":[] }, {"name":"contentType","parameterTypes":[] },
{"name":"emoji","parameterTypes":[] }, {"name":"emoji","parameterTypes":[] },
{"name":"file","parameterTypes":[] }, {"name":"file","parameterTypes":[] },

View file

@ -47,11 +47,11 @@ public class SignalAccountFiles {
} }
public MultiAccountManager initMultiAccountManager() { public MultiAccountManager initMultiAccountManager() {
final var managers = getAllLocalAccountNumbers().stream().map(a -> { final var managers = accountsStore.getAllAccounts().parallelStream().map(a -> {
try { try {
return initManager(a); return initManager(a.path());
} catch (NotRegisteredException | IOException | AccountCheckException e) { } catch (NotRegisteredException | IOException | AccountCheckException e) {
logger.warn("Ignoring {}: {} ({})", a, e.getMessage(), e.getClass().getSimpleName()); logger.warn("Ignoring {}: {} ({})", a.number(), e.getMessage(), e.getClass().getSimpleName());
return null; return null;
} }
}).filter(Objects::nonNull).toList(); }).filter(Objects::nonNull).toList();
@ -61,7 +61,16 @@ public class SignalAccountFiles {
public Manager initManager(String number) throws IOException, NotRegisteredException, AccountCheckException { public Manager initManager(String number) throws IOException, NotRegisteredException, AccountCheckException {
final var accountPath = accountsStore.getPathByNumber(number); final var accountPath = accountsStore.getPathByNumber(number);
if (accountPath == null || !SignalAccount.accountFileExists(pathConfig.dataPath(), accountPath)) { return this.initManager(number, accountPath);
}
private Manager initManager(
String number, String accountPath
) throws IOException, NotRegisteredException, AccountCheckException {
if (accountPath == null) {
throw new NotRegisteredException();
}
if (!SignalAccount.accountFileExists(pathConfig.dataPath(), accountPath)) {
throw new NotRegisteredException(); throw new NotRegisteredException();
} }

View file

@ -2,7 +2,7 @@ package org.asamk.signal.manager.storage.accounts;
import java.util.List; import java.util.List;
record AccountsStorage(List<Account> accounts) { public record AccountsStorage(List<Account> accounts) {
record Account(String path, String number, String uuid) {} public record Account(String path, String number, String uuid) {}
} }

View file

@ -41,14 +41,18 @@ public class AccountsStore {
} }
} }
public Set<String> getAllNumbers() { public synchronized Set<String> getAllNumbers() {
return readAccounts().stream() return readAccounts().stream()
.map(AccountsStorage.Account::number) .map(AccountsStorage.Account::number)
.filter(Objects::nonNull) .filter(Objects::nonNull)
.collect(Collectors.toSet()); .collect(Collectors.toSet());
} }
public String getPathByNumber(String number) { public synchronized Set<AccountsStorage.Account> getAllAccounts() {
return readAccounts().stream().filter(a -> a.number() != null).collect(Collectors.toSet());
}
public synchronized String getPathByNumber(String number) {
return readAccounts().stream() return readAccounts().stream()
.filter(a -> number.equals(a.number())) .filter(a -> number.equals(a.number()))
.map(AccountsStorage.Account::path) .map(AccountsStorage.Account::path)
@ -56,7 +60,7 @@ public class AccountsStore {
.orElse(null); .orElse(null);
} }
public String getPathByAci(ACI aci) { public synchronized String getPathByAci(ACI aci) {
return readAccounts().stream() return readAccounts().stream()
.filter(a -> aci.toString().equals(a.uuid())) .filter(a -> aci.toString().equals(a.uuid()))
.map(AccountsStorage.Account::path) .map(AccountsStorage.Account::path)
@ -64,7 +68,7 @@ public class AccountsStore {
.orElse(null); .orElse(null);
} }
public void updateAccount(String path, String number, ACI aci) { public synchronized void updateAccount(String path, String number, ACI aci) {
updateAccounts(accounts -> accounts.stream().map(a -> { updateAccounts(accounts -> accounts.stream().map(a -> {
if (path.equals(a.path())) { if (path.equals(a.path())) {
return new AccountsStorage.Account(a.path(), number, aci == null ? null : aci.toString()); return new AccountsStorage.Account(a.path(), number, aci == null ? null : aci.toString());
@ -81,7 +85,7 @@ public class AccountsStore {
}).toList()); }).toList());
} }
public String addAccount(String number, ACI aci) { public synchronized String addAccount(String number, ACI aci) {
final var accountPath = generateNewAccountPath(); final var accountPath = generateNewAccountPath();
final var account = new AccountsStorage.Account(accountPath, number, aci == null ? null : aci.toString()); final var account = new AccountsStorage.Account(accountPath, number, aci == null ? null : aci.toString());
updateAccounts(accounts -> { updateAccounts(accounts -> {