X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/265e32dee6f3d92dfde7ede23e3baa2d831923b3..00482b6dcb1e98da39df54374f7098b51d66bee5:/src/com/owncloud/android/files/services/IndexedForest.java diff --git a/src/com/owncloud/android/files/services/IndexedForest.java b/src/com/owncloud/android/files/services/IndexedForest.java index 52482993..4c1ac7bd 100644 --- a/src/com/owncloud/android/files/services/IndexedForest.java +++ b/src/com/owncloud/android/files/services/IndexedForest.java @@ -1,4 +1,7 @@ -/* ownCloud Android client application +/** + * ownCloud Android client application + * + * @author David A. Velasco * Copyright (C) 2015 ownCloud Inc. * * This program is free software: you can redistribute it and/or modify @@ -18,8 +21,10 @@ package com.owncloud.android.files.services; import android.accounts.Account; +import android.util.Pair; import com.owncloud.android.datamodel.OCFile; +import com.owncloud.android.lib.common.utils.Log_OC; import java.io.File; import java.util.HashSet; @@ -34,8 +39,6 @@ import java.util.concurrent.ConcurrentMap; * A map provides the indexation based in hashing. * * A tree is created per account. - * - * @author David A. Velasco */ public class IndexedForest { @@ -88,10 +91,14 @@ public class IndexedForest { public void removeChild(Node removed) { mChildren.remove(removed); } + + public void clearPayload() { + mPayload = null; + } } - public /* synchronized */ String putIfAbsent(Account account, String remotePath, V value) { + public /* synchronized */ Pair putIfAbsent(Account account, String remotePath, V value) { String targetKey = buildKey(account, remotePath); Node valuedNode = new Node(targetKey, value); mMap.putIfAbsent( @@ -121,12 +128,31 @@ public class IndexedForest { currentNode = parentNode; } - return targetKey; + String linkedTo = OCFile.ROOT_PATH; + if (linked) { + linkedTo = parentNode.getKey().substring(account.name.length()); + } + return new Pair(targetKey, linkedTo); }; - public /* synchronized */ V remove(Account account, String remotePath) { + + public Pair removePayload(Account account, String remotePath) { + String targetKey = buildKey(account, remotePath); + Node target = mMap.get(targetKey); + if (target != null) { + target.clearPayload(); + if (!target.hasChildren()) { + return remove(account, remotePath); + } + } + return new Pair(null, null); + } + + + public /* synchronized */ Pair remove(Account account, String remotePath) { String targetKey = buildKey(account, remotePath); Node firstRemoved = mMap.remove(targetKey); + String unlinkedFrom = null; if (firstRemoved != null) { /// remove children @@ -135,23 +161,25 @@ public class IndexedForest { /// remove ancestors if only here due to firstRemoved Node removed = firstRemoved; Node parent = removed.getParent(); + boolean unlinked = false; while (parent != null) { parent.removeChild(removed); if (!parent.hasChildren()) { removed = mMap.remove(parent.getKey()); parent = removed.getParent(); } else { - parent = null; + break; } } - } - if (firstRemoved != null) { - return firstRemoved.getPayload(); - } else { - return null; + if (parent != null) { + unlinkedFrom = parent.getKey().substring(account.name.length()); + } + + return new Pair(firstRemoved.getPayload(), unlinkedFrom); } + return new Pair(null, null); } private void removeDescendants(Node removed) { @@ -178,6 +206,26 @@ public class IndexedForest { } } + public V get(Account account, String remotePath) { + String key = buildKey(account, remotePath); + return get(key); + } + + + /** + * Remove the elements that contains account as a part of its key + * @param account + */ + public void remove(Account account){ + Iterator it = mMap.keySet().iterator(); + while (it.hasNext()) { + String key = it.next(); + Log_OC.d("IndexedForest", "Number of pending downloads= " + mMap.size()); + if (key.startsWith(account.name)) { + mMap.remove(key); + } + } + } /** * Builds a key to index files @@ -189,6 +237,4 @@ public class IndexedForest { return account.name + remotePath; } - - }