2 * ownCloud Android client application
4 * @author David A. Velasco
5 * Copyright (C) 2015 ownCloud Inc.
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2,
9 * as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 package com
.owncloud
.android
.files
.services
;
23 import android
.accounts
.Account
;
24 import android
.util
.Pair
;
26 import com
.owncloud
.android
.datamodel
.OCFile
;
27 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
30 import java
.util
.HashSet
;
31 import java
.util
.Iterator
;
33 import java
.util
.concurrent
.ConcurrentHashMap
;
34 import java
.util
.concurrent
.ConcurrentMap
;
37 * Helper structure to keep the trees of folders containing any file downloading or synchronizing.
39 * A map provides the indexation based in hashing.
41 * A tree is created per account.
43 public class IndexedForest
<V
> {
45 private ConcurrentMap
<String
, Node
<V
>> mMap
= new ConcurrentHashMap
<String
, Node
<V
>>();
47 private class Node
<V
> {
49 Node
<V
> mParent
= null
;
50 Set
<Node
<V
>> mChildren
= new HashSet
<Node
<V
>>(); // TODO be careful with hash()
53 // payload is optional
54 public Node(String key
, V payload
) {
56 throw new IllegalArgumentException("Argument key MUST NOT be null");
62 public Node
<V
> getParent() {
66 public Set
<Node
<V
>> getChildren() {
70 public String
getKey() {
74 public V
getPayload() {
78 public void addChild(Node
<V
> child
) {
80 child
.setParent(this);
83 private void setParent(Node
<V
> parent
) {
87 public boolean hasChildren() {
88 return mChildren
.size() > 0;
91 public void removeChild(Node
<V
> removed
) {
92 mChildren
.remove(removed
);
95 public void clearPayload() {
101 public /* synchronized */ Pair
<String
, String
> putIfAbsent(Account account
, String remotePath
, V value
) {
102 String targetKey
= buildKey(account
, remotePath
);
103 Node
<V
> valuedNode
= new Node(targetKey
, value
);
109 String currentPath
= remotePath
, parentPath
= null
, parentKey
= null
;
110 Node
<V
> currentNode
= valuedNode
, parentNode
= null
;
111 boolean linked
= false
;
112 while (!OCFile
.ROOT_PATH
.equals(currentPath
) && !linked
) {
113 parentPath
= new File(currentPath
).getParent();
114 if (!parentPath
.endsWith(OCFile
.PATH_SEPARATOR
)) {
115 parentPath
+= OCFile
.PATH_SEPARATOR
;
117 parentKey
= buildKey(account
, parentPath
);
118 parentNode
= mMap
.get(parentKey
);
119 if (parentNode
== null
) {
120 parentNode
= new Node(parentKey
, null
);
121 parentNode
.addChild(currentNode
);
122 mMap
.put(parentKey
, parentNode
);
124 parentNode
.addChild(currentNode
);
127 currentPath
= parentPath
;
128 currentNode
= parentNode
;
131 String linkedTo
= OCFile
.ROOT_PATH
;
133 linkedTo
= parentNode
.getKey().substring(account
.name
.length());
135 return new Pair
<String
, String
>(targetKey
, linkedTo
);
139 public Pair
<V
, String
> removePayload(Account account
, String remotePath
) {
140 String targetKey
= buildKey(account
, remotePath
);
141 Node
<V
> target
= mMap
.get(targetKey
);
142 if (target
!= null
) {
143 target
.clearPayload();
144 if (!target
.hasChildren()) {
145 return remove(account
, remotePath
);
148 return new Pair
<V
, String
>(null
, null
);
152 public /* synchronized */ Pair
<V
, String
> remove(Account account
, String remotePath
) {
153 String targetKey
= buildKey(account
, remotePath
);
154 Node
<V
> firstRemoved
= mMap
.remove(targetKey
);
155 String unlinkedFrom
= null
;
157 if (firstRemoved
!= null
) {
159 removeDescendants(firstRemoved
);
161 /// remove ancestors if only here due to firstRemoved
162 Node
<V
> removed
= firstRemoved
;
163 Node
<V
> parent
= removed
.getParent();
164 boolean unlinked
= false
;
165 while (parent
!= null
) {
166 parent
.removeChild(removed
);
167 if (!parent
.hasChildren()) {
168 removed
= mMap
.remove(parent
.getKey());
169 parent
= removed
.getParent();
175 if (parent
!= null
) {
176 unlinkedFrom
= parent
.getKey().substring(account
.name
.length());
179 return new Pair
<V
, String
>(firstRemoved
.getPayload(), unlinkedFrom
);
182 return new Pair
<V
, String
>(null
, null
);
185 private void removeDescendants(Node
<V
> removed
) {
186 Iterator
<Node
<V
>> childrenIt
= removed
.getChildren().iterator();
187 Node
<V
> child
= null
;
188 while (childrenIt
.hasNext()) {
189 child
= childrenIt
.next();
190 mMap
.remove(child
.getKey());
191 removeDescendants(child
);
195 public boolean contains(Account account
, String remotePath
) {
196 String targetKey
= buildKey(account
, remotePath
);
197 return mMap
.containsKey(targetKey
);
200 public /* synchronized */ V
get(String key
) {
201 Node
<V
> node
= mMap
.get(key
);
203 return node
.getPayload();
209 public V
get(Account account
, String remotePath
) {
210 String key
= buildKey(account
, remotePath
);
216 * Remove the elements that contains account as a part of its key
219 public void remove(Account account
){
220 Iterator
<String
> it
= mMap
.keySet().iterator();
221 while (it
.hasNext()) {
222 String key
= it
.next();
223 Log_OC
.d("IndexedForest", "Number of pending downloads= " + mMap
.size());
224 if (key
.startsWith(account
.name
)) {
231 * Builds a key to index files
233 * @param account Account where the file to download is stored
234 * @param remotePath Path of the file in the server
236 private String
buildKey(Account account
, String remotePath
) {
237 return account
.name
+ remotePath
;