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

@ -47,11 +47,11 @@ public class SignalAccountFiles {
}
public MultiAccountManager initMultiAccountManager() {
final var managers = getAllLocalAccountNumbers().stream().map(a -> {
final var managers = accountsStore.getAllAccounts().parallelStream().map(a -> {
try {
return initManager(a);
return initManager(a.path());
} 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;
}
}).filter(Objects::nonNull).toList();
@ -61,7 +61,16 @@ public class SignalAccountFiles {
public Manager initManager(String number) throws IOException, NotRegisteredException, AccountCheckException {
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();
}

View file

@ -2,7 +2,7 @@ package org.asamk.signal.manager.storage.accounts;
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()
.map(AccountsStorage.Account::number)
.filter(Objects::nonNull)
.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()
.filter(a -> number.equals(a.number()))
.map(AccountsStorage.Account::path)
@ -56,7 +60,7 @@ public class AccountsStore {
.orElse(null);
}
public String getPathByAci(ACI aci) {
public synchronized String getPathByAci(ACI aci) {
return readAccounts().stream()
.filter(a -> aci.toString().equals(a.uuid()))
.map(AccountsStorage.Account::path)
@ -64,7 +68,7 @@ public class AccountsStore {
.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 -> {
if (path.equals(a.path())) {
return new AccountsStorage.Account(a.path(), number, aci == null ? null : aci.toString());
@ -81,7 +85,7 @@ public class AccountsStore {
}).toList());
}
public String addAccount(String number, ACI aci) {
public synchronized String addAccount(String number, ACI aci) {
final var accountPath = generateNewAccountPath();
final var account = new AccountsStorage.Account(accountPath, number, aci == null ? null : aci.toString());
updateAccounts(accounts -> {