Revision 3
Update
ChunkManagement.java | ||
---|---|---|
1 |
package disk; |
|
2 |
|
|
3 |
import java.util.HashMap; |
|
4 |
import java.util.HashSet; |
|
5 |
import java.util.Map; |
|
6 |
import java.util.concurrent.BlockingQueue; |
|
7 |
import java.util.concurrent.LinkedBlockingDeque; |
|
8 |
|
|
9 |
import chunk.Chunk; |
|
10 |
import javafx.util.Pair; |
|
11 |
|
|
12 |
/** |
|
13 |
* ChunkManagement |
|
14 |
*/ |
|
15 |
public class ChunkManagement { |
|
16 |
|
|
17 |
private static ChunkManagement chunkManagementInstance = null; |
|
18 |
|
|
19 |
private Map<String, Map<Integer, Integer>> storesCounter; |
|
20 |
|
|
21 |
private BlockingQueue<Chunk> restoreChunks; |
|
22 |
|
|
23 |
private ChunkManagement() { |
|
24 |
storesCounter = new HashMap<String, Map<Integer, Integer>>(); |
|
25 |
restoreChunks = new LinkedBlockingDeque<Chunk>(); |
|
26 |
} |
|
27 |
|
|
28 |
public static ChunkManagement getInstance() { |
|
29 |
if (chunkManagementInstance == null) { |
|
30 |
chunkManagementInstance = new ChunkManagement(); |
|
31 |
} |
|
32 |
return chunkManagementInstance; |
|
33 |
} |
|
34 |
|
|
35 |
public int getStores(String fileId, int chunkNo) { |
|
36 |
if (!storesCounter.containsKey(fileId)){ |
|
37 |
return 0; |
|
38 |
} |
|
39 |
if (!storesCounter.get(fileId).containsKey(chunkNo)){ |
|
40 |
return 0; |
|
41 |
} |
|
42 |
return storesCounter.get(fileId).get(chunkNo); |
|
43 |
} |
|
44 |
|
|
45 |
public int registerStored(String fileId, int chunkNo) { |
|
46 |
if (!storesCounter.containsKey(fileId)) { |
|
47 |
storesCounter.put(fileId, new HashMap<Integer, Integer>()); |
|
48 |
} |
|
49 |
if (!storesCounter.get(fileId).containsKey(chunkNo)) { |
|
50 |
storesCounter.get(fileId).put(chunkNo, 1); |
|
51 |
} else { |
|
52 |
int nStores = storesCounter.get(fileId).get(chunkNo); |
|
53 |
storesCounter.get(fileId).put(chunkNo, nStores + 1); |
|
54 |
} |
|
55 |
return storesCounter.get(fileId).get(chunkNo); |
|
56 |
} |
|
57 |
|
|
58 |
public int registerRemoved(String fileId, int chunkNo) { |
|
59 |
if (!storesCounter.containsKey(fileId)) { |
|
60 |
return 0; |
|
61 |
} |
|
62 |
if (!storesCounter.get(fileId).containsKey(chunkNo)) { |
|
63 |
return 0; |
|
64 |
} else { |
|
65 |
int nStores = storesCounter.get(fileId).get(chunkNo); |
|
66 |
storesCounter.get(fileId).put(chunkNo, nStores - 1); |
|
67 |
return storesCounter.get(fileId).get(chunkNo); |
|
68 |
} |
|
69 |
} |
|
70 |
|
|
71 |
public void deleteStores(String fileId) { |
|
72 |
storesCounter.remove(fileId); |
|
73 |
} |
|
74 |
|
|
75 |
public void addRestoreChunk(Chunk chunk) { |
|
76 |
restoreChunks.add(chunk); |
|
77 |
} |
|
78 |
|
|
79 |
/** |
|
80 |
* @return the restoreChunks |
|
81 |
*/ |
|
82 |
public BlockingQueue<Chunk> getRestoreChunks() { |
|
83 |
return restoreChunks; |
|
84 |
} |
|
85 |
|
|
86 |
} |
|
1 |
package disk; |
|
2 |
|
|
3 |
import java.util.HashMap; |
|
4 |
import java.util.HashSet; |
|
5 |
import java.util.Map; |
|
6 |
import java.util.concurrent.BlockingQueue; |
|
7 |
import java.util.concurrent.LinkedBlockingDeque; |
|
8 |
|
|
9 |
import chunk.Chunk; |
|
10 |
|
|
11 |
/** |
|
12 |
* ChunkManagement |
|
13 |
*/ |
|
14 |
public class ChunkManagement { |
|
15 |
|
|
16 |
private static ChunkManagement chunkManagementInstance = null; |
|
17 |
|
|
18 |
private Map<String, Map<Integer, Integer>> storesCounter; |
|
19 |
|
|
20 |
private BlockingQueue<Chunk> restoreChunks; |
|
21 |
|
|
22 |
private ChunkManagement() { |
|
23 |
storesCounter = new HashMap<String, Map<Integer, Integer>>(); |
|
24 |
restoreChunks = new LinkedBlockingDeque<Chunk>(); |
|
25 |
} |
|
26 |
|
|
27 |
public static ChunkManagement getInstance() { |
|
28 |
if (chunkManagementInstance == null) { |
|
29 |
chunkManagementInstance = new ChunkManagement(); |
|
30 |
} |
|
31 |
return chunkManagementInstance; |
|
32 |
} |
|
33 |
|
|
34 |
public int getStores(String fileId, int chunkNo) { |
|
35 |
if (!storesCounter.containsKey(fileId)){ |
|
36 |
return 0; |
|
37 |
} |
|
38 |
if (!storesCounter.get(fileId).containsKey(chunkNo)){ |
|
39 |
return 0; |
|
40 |
} |
|
41 |
return storesCounter.get(fileId).get(chunkNo); |
|
42 |
} |
|
43 |
|
|
44 |
public int registerStored(String fileId, int chunkNo) { |
|
45 |
if (!storesCounter.containsKey(fileId)) { |
|
46 |
storesCounter.put(fileId, new HashMap<Integer, Integer>()); |
|
47 |
} |
|
48 |
if (!storesCounter.get(fileId).containsKey(chunkNo)) { |
|
49 |
storesCounter.get(fileId).put(chunkNo, 1); |
|
50 |
} else { |
|
51 |
int nStores = storesCounter.get(fileId).get(chunkNo); |
|
52 |
storesCounter.get(fileId).put(chunkNo, nStores + 1); |
|
53 |
} |
|
54 |
return storesCounter.get(fileId).get(chunkNo); |
|
55 |
} |
|
56 |
|
|
57 |
public int registerRemoved(String fileId, int chunkNo) { |
|
58 |
if (!storesCounter.containsKey(fileId)) { |
|
59 |
return 0; |
|
60 |
} |
|
61 |
if (!storesCounter.get(fileId).containsKey(chunkNo)) { |
|
62 |
return 0; |
|
63 |
} else { |
|
64 |
int nStores = storesCounter.get(fileId).get(chunkNo); |
|
65 |
storesCounter.get(fileId).put(chunkNo, nStores - 1); |
|
66 |
return storesCounter.get(fileId).get(chunkNo); |
|
67 |
} |
|
68 |
} |
|
69 |
|
|
70 |
public void deleteStores(String fileId) { |
|
71 |
storesCounter.remove(fileId); |
|
72 |
} |
|
73 |
|
|
74 |
public void addRestoreChunk(Chunk chunk) { |
|
75 |
restoreChunks.add(chunk); |
|
76 |
} |
|
77 |
|
|
78 |
/** |
|
79 |
* @return the restoreChunks |
|
80 |
*/ |
|
81 |
public BlockingQueue<Chunk> getRestoreChunks() { |
|
82 |
return restoreChunks; |
|
83 |
} |
|
84 |
|
|
85 |
} |
Also available in: Unified diff