c0bd6e3f1376d5a244a6c333810c16dc5e4be1fb
1 /* ownCloud Android client application
3 * @author David A. Velasco
4 * Copyright (C) 2015 ownCloud Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 package com
.owncloud
.android
.files
.services
;
22 import android
.accounts
.Account
;
23 import android
.util
.Pair
;
25 import com
.owncloud
.android
.datamodel
.OCFile
;
26 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
29 import java
.util
.HashSet
;
30 import java
.util
.Iterator
;
32 import java
.util
.concurrent
.ConcurrentHashMap
;
33 import java
.util
.concurrent
.ConcurrentMap
;
36 * Helper structure to keep the trees of folders containing any file downloading or synchronizing.
38 * A map provides the indexation based in hashing.
40 * A tree is created per account.
42 public class IndexedForest
<V
> {
44 private ConcurrentMap
<String
, Node
<V
>> mMap
= new ConcurrentHashMap
<String
, Node
<V
>>();
46 private class Node
<V
> {
48 Node
<V
> mParent
= null
;
49 Set
<Node
<V
>> mChildren
= new HashSet
<Node
<V
>>(); // TODO be careful with hash()
52 // payload is optional
53 public Node(String key
, V payload
) {
55 throw new IllegalArgumentException("Argument key MUST NOT be null");
61 public Node
<V
> getParent() {
65 public Set
<Node
<V
>> getChildren() {
69 public String
getKey() {
73 public V
getPayload() {
77 public void addChild(Node
<V
> child
) {
79 child
.setParent(this);
82 private void setParent(Node
<V
> parent
) {
86 public boolean hasChildren() {
87 return mChildren
.size() > 0;
90 public void removeChild(Node
<V
> removed
) {
91 mChildren
.remove(removed
);
94 public void clearPayload() {
100 public /* synchronized */ Pair
<String
, String
> putIfAbsent(Account account
, String remotePath
, V value
) {
101 String targetKey
= buildKey(account
, remotePath
);
102 Node
<V
> valuedNode
= new Node(targetKey
, value
);
108 String currentPath
= remotePath
, parentPath
= null
, parentKey
= null
;
109 Node
<V
> currentNode
= valuedNode
, parentNode
= null
;
110 boolean linked
= false
;
111 while (!OCFile
.ROOT_PATH
.equals(currentPath
) && !linked
) {
112 parentPath
= new File(currentPath
).getParent();
113 if (!parentPath
.endsWith(OCFile
.PATH_SEPARATOR
)) {
114 parentPath
+= OCFile
.PATH_SEPARATOR
;
116 parentKey
= buildKey(account
, parentPath
);
117 parentNode
= mMap
.get(parentKey
);
118 if (parentNode
== null
) {
119 parentNode
= new Node(parentKey
, null
);
120 parentNode
.addChild(currentNode
);
121 mMap
.put(parentKey
, parentNode
);
123 parentNode
.addChild(currentNode
);
126 currentPath
= parentPath
;
127 currentNode
= parentNode
;
130 String linkedTo
= OCFile
.ROOT_PATH
;
132 linkedTo
= parentNode
.getKey().substring(account
.name
.length());
134 return new Pair
<String
, String
>(targetKey
, linkedTo
);
138 public Pair
<V
, String
> removePayload(Account account
, String remotePath
) {
139 String targetKey
= buildKey(account
, remotePath
);
140 Node
<V
> target
= mMap
.get(targetKey
);
141 if (target
!= null
) {
142 target
.clearPayload();
143 if (!target
.hasChildren()) {
144 return remove(account
, remotePath
);
147 return new Pair
<V
, String
>(null
, null
);
151 public /* synchronized */ Pair
<V
, String
> remove(Account account
, String remotePath
) {
152 String targetKey
= buildKey(account
, remotePath
);
153 Node
<V
> firstRemoved
= mMap
.remove(targetKey
);
154 String unlinkedFrom
= null
;
156 if (firstRemoved
!= null
) {
158 removeDescendants(firstRemoved
);
160 /// remove ancestors if only here due to firstRemoved
161 Node
<V
> removed
= firstRemoved
;
162 Node
<V
> parent
= removed
.getParent();
163 boolean unlinked
= false
;
164 while (parent
!= null
) {
165 parent
.removeChild(removed
);
166 if (!parent
.hasChildren()) {
167 removed
= mMap
.remove(parent
.getKey());
168 parent
= removed
.getParent();
174 if (parent
!= null
) {
175 unlinkedFrom
= parent
.getKey().substring(account
.name
.length());
178 return new Pair
<V
, String
>(firstRemoved
.getPayload(), unlinkedFrom
);
181 return new Pair
<V
, String
>(null
, null
);
184 private void removeDescendants(Node
<V
> removed
) {
185 Iterator
<Node
<V
>> childrenIt
= removed
.getChildren().iterator();
186 Node
<V
> child
= null
;
187 while (childrenIt
.hasNext()) {
188 child
= childrenIt
.next();
189 mMap
.remove(child
.getKey());
190 removeDescendants(child
);
194 public boolean contains(Account account
, String remotePath
) {
195 String targetKey
= buildKey(account
, remotePath
);
196 return mMap
.containsKey(targetKey
);
199 public /* synchronized */ V
get(String key
) {
200 Node
<V
> node
= mMap
.get(key
);
202 return node
.getPayload();
208 public V
get(Account account
, String remotePath
) {
209 String key
= buildKey(account
, remotePath
);
215 * Remove the elements that contains account as a part of its key
218 public void remove(Account account
){
219 Iterator
<String
> it
= mMap
.keySet().iterator();
220 while (it
.hasNext()) {
221 String key
= it
.next();
222 Log_OC
.d("IndexedForest", "Number of pending downloads= " + mMap
.size());
223 if (key
.startsWith(account
.name
)) {
230 * Builds a key to index files
232 * @param account Account where the file to download is stored
233 * @param remotePath Path of the file in the server
235 private String
buildKey(Account account
, String remotePath
) {
236 return account
.name
+ remotePath
;