Project

General

Profile

Statistics
| Revision:

root / src / disk / ChunkManagement.java @ 3

History | View | Annotate | Download (2.05 KB)

1 3 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
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
}