Project

General

Profile

Statistics
| Revision:

root / src / disk / ChunkManagement.java @ 2

History | View | Annotate | Download (2.16 KB)

1 2 up20150644
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
}