Search in sources :

Example 1 with StorageItem

use of org.whispersystems.signalservice.internal.storage.protos.StorageItem in project Signal-Android by WhisperSystems.

the class SignalServiceAccountManager method readStorageRecords.

public List<SignalStorageRecord> readStorageRecords(StorageKey storageKey, List<StorageId> storageKeys) throws IOException, InvalidKeyException {
    if (storageKeys.isEmpty()) {
        return Collections.emptyList();
    }
    List<SignalStorageRecord> result = new ArrayList<>();
    Map<ByteString, Integer> typeMap = new HashMap<>();
    List<ReadOperation> readOperations = new LinkedList<>();
    ReadOperation.Builder currentOperation = ReadOperation.newBuilder();
    for (StorageId key : storageKeys) {
        typeMap.put(ByteString.copyFrom(key.getRaw()), key.getType());
        if (currentOperation.getReadKeyCount() >= STORAGE_READ_MAX_ITEMS) {
            Log.i(TAG, "Going over max read items. Starting a new read operation.");
            readOperations.add(currentOperation.build());
            currentOperation = ReadOperation.newBuilder();
        }
        if (StorageId.isKnownType(key.getType())) {
            currentOperation.addReadKey(ByteString.copyFrom(key.getRaw()));
        } else {
            result.add(SignalStorageRecord.forUnknown(key));
        }
    }
    if (currentOperation.getReadKeyCount() > 0) {
        readOperations.add(currentOperation.build());
    }
    Log.i(TAG, "Reading " + storageKeys.size() + " items split over " + readOperations.size() + " page(s).");
    String authToken = this.pushServiceSocket.getStorageAuth();
    for (ReadOperation readOperation : readOperations) {
        StorageItems items = this.pushServiceSocket.readStorageItems(authToken, readOperation);
        for (StorageItem item : items.getItemsList()) {
            Integer type = typeMap.get(item.getKey());
            if (type != null) {
                result.add(SignalStorageModels.remoteToLocalStorageRecord(item, type, storageKey));
            } else {
                Log.w(TAG, "No type found! Skipping.");
            }
        }
    }
    return result;
}
Also used : ReadOperation(org.whispersystems.signalservice.internal.storage.protos.ReadOperation) HashMap(java.util.HashMap) ByteString(com.google.protobuf.ByteString) ArrayList(java.util.ArrayList) SignalStorageRecord(org.whispersystems.signalservice.api.storage.SignalStorageRecord) ByteString(com.google.protobuf.ByteString) StorageId(org.whispersystems.signalservice.api.storage.StorageId) LinkedList(java.util.LinkedList) StorageItems(org.whispersystems.signalservice.internal.storage.protos.StorageItems) StorageItem(org.whispersystems.signalservice.internal.storage.protos.StorageItem)

Example 2 with StorageItem

use of org.whispersystems.signalservice.internal.storage.protos.StorageItem in project Signal-Android by signalapp.

the class SignalServiceAccountManager method readStorageRecords.

public List<SignalStorageRecord> readStorageRecords(StorageKey storageKey, List<StorageId> storageKeys) throws IOException, InvalidKeyException {
    if (storageKeys.isEmpty()) {
        return Collections.emptyList();
    }
    List<SignalStorageRecord> result = new ArrayList<>();
    Map<ByteString, Integer> typeMap = new HashMap<>();
    List<ReadOperation> readOperations = new LinkedList<>();
    ReadOperation.Builder currentOperation = ReadOperation.newBuilder();
    for (StorageId key : storageKeys) {
        typeMap.put(ByteString.copyFrom(key.getRaw()), key.getType());
        if (currentOperation.getReadKeyCount() >= STORAGE_READ_MAX_ITEMS) {
            Log.i(TAG, "Going over max read items. Starting a new read operation.");
            readOperations.add(currentOperation.build());
            currentOperation = ReadOperation.newBuilder();
        }
        if (StorageId.isKnownType(key.getType())) {
            currentOperation.addReadKey(ByteString.copyFrom(key.getRaw()));
        } else {
            result.add(SignalStorageRecord.forUnknown(key));
        }
    }
    if (currentOperation.getReadKeyCount() > 0) {
        readOperations.add(currentOperation.build());
    }
    Log.i(TAG, "Reading " + storageKeys.size() + " items split over " + readOperations.size() + " page(s).");
    String authToken = this.pushServiceSocket.getStorageAuth();
    for (ReadOperation readOperation : readOperations) {
        StorageItems items = this.pushServiceSocket.readStorageItems(authToken, readOperation);
        for (StorageItem item : items.getItemsList()) {
            Integer type = typeMap.get(item.getKey());
            if (type != null) {
                result.add(SignalStorageModels.remoteToLocalStorageRecord(item, type, storageKey));
            } else {
                Log.w(TAG, "No type found! Skipping.");
            }
        }
    }
    return result;
}
Also used : ReadOperation(org.whispersystems.signalservice.internal.storage.protos.ReadOperation) HashMap(java.util.HashMap) ByteString(com.google.protobuf.ByteString) ArrayList(java.util.ArrayList) SignalStorageRecord(org.whispersystems.signalservice.api.storage.SignalStorageRecord) ByteString(com.google.protobuf.ByteString) StorageId(org.whispersystems.signalservice.api.storage.StorageId) LinkedList(java.util.LinkedList) StorageItems(org.whispersystems.signalservice.internal.storage.protos.StorageItems) StorageItem(org.whispersystems.signalservice.internal.storage.protos.StorageItem)

Aggregations

ByteString (com.google.protobuf.ByteString)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 LinkedList (java.util.LinkedList)2 SignalStorageRecord (org.whispersystems.signalservice.api.storage.SignalStorageRecord)2 StorageId (org.whispersystems.signalservice.api.storage.StorageId)2 ReadOperation (org.whispersystems.signalservice.internal.storage.protos.ReadOperation)2 StorageItem (org.whispersystems.signalservice.internal.storage.protos.StorageItem)2 StorageItems (org.whispersystems.signalservice.internal.storage.protos.StorageItems)2