Revision 14
storage/Storage.java | ||
---|---|---|
1 |
package storage; |
|
2 |
|
|
3 |
import chunk.Chunk; |
|
4 |
import chunk.ChunkFile; |
|
5 |
import chunk.Global; |
|
6 |
import server.Server; |
|
7 |
|
|
8 |
import java.io.File; |
|
9 |
import java.io.Serializable; |
|
10 |
import java.util.ArrayList; |
|
11 |
import java.util.Iterator; |
|
12 |
import java.util.concurrent.ConcurrentHashMap; |
|
13 |
|
|
14 |
public class Storage implements Serializable { |
|
15 |
private ArrayList<ChunkFile> files; |
|
16 |
private ArrayList<Chunk> savedChunks; |
|
17 |
private ArrayList<Chunk> receivedChunks; |
|
18 |
private ConcurrentHashMap<String, Integer> storedTimes; |
|
19 |
private ConcurrentHashMap<String, String> remainingChunks; |
|
20 |
private int totalSpace; |
|
21 |
|
|
22 |
public Storage() { |
|
23 |
this.files = new ArrayList<>(); |
|
24 |
this.savedChunks = new ArrayList<>(); |
|
25 |
this.receivedChunks = new ArrayList<>(); |
|
26 |
this.storedTimes = new ConcurrentHashMap<>(); |
|
27 |
this.remainingChunks = new ConcurrentHashMap<>(); |
|
28 |
this.setTotalSpace(Global.MAX_STORAGE_SIZE); |
|
29 |
} |
|
30 |
|
|
31 |
public ArrayList<ChunkFile> getFiles() { |
|
32 |
return files; |
|
33 |
} |
|
34 |
|
|
35 |
public synchronized void setFiles(ArrayList<ChunkFile> files) { |
|
36 |
this.files = files; |
|
37 |
} |
|
38 |
|
|
39 |
public synchronized void addFile(ChunkFile file) { |
|
40 |
this.files.add(file); |
|
41 |
} |
|
42 |
|
|
43 |
public synchronized boolean addSavedChunk(Chunk chunk) { |
|
44 |
String chunkFileId; |
|
45 |
int chunkNum; |
|
46 |
for (Iterator<Chunk> iter = this.savedChunks.iterator(); iter.hasNext();) { |
|
47 |
chunkFileId = ((Chunk) iter).getfileId(); |
|
48 |
chunkNum = ((Chunk) iter).getNum(); |
|
49 |
if (chunkFileId.equals(chunk.getfileId()) && chunkNum == chunk.getNum()) |
|
50 |
return false; |
|
51 |
} |
|
52 |
this.savedChunks.add(chunk); |
|
53 |
|
|
54 |
return true; |
|
55 |
} |
|
56 |
|
|
57 |
public void deleteStoredChunks(String fileId) { |
|
58 |
int chunkNum; |
|
59 |
|
|
60 |
for (Iterator<Chunk> iter = this.savedChunks.iterator(); iter.hasNext();) { |
|
61 |
Chunk chunk = iter.next(); |
|
62 |
chunkNum = chunk.getNum(); |
|
63 |
if (chunk.getfileId().equals(fileId)) { |
|
64 |
String filename = "database/" + Server.getServerId() + "/" + fileId + "." + chunkNum; |
|
65 |
File file = new File(filename); |
|
66 |
file.delete(); |
|
67 |
removeStoredOccurrencesEntry(fileId, chunkNum); |
|
68 |
incSpaceAvailable(fileId, chunkNum); |
|
69 |
iter.remove(); |
|
70 |
} |
|
71 |
} |
|
72 |
} |
|
73 |
|
|
74 |
public synchronized void incStoredOccurrences(String fileId, int chunkNum) { |
|
75 |
|
|
76 |
String key = fileId + '.' + chunkNum; |
|
77 |
ConcurrentHashMap<String, Integer> StoredTimes = Server.getStorage().getStoredTimes(); |
|
78 |
if (!StoredTimes.containsKey(key)) {// Checks if co |
|
79 |
Server.getStorage().getStoredTimes().put(key, 1); |
|
80 |
} else { |
|
81 |
int total = this.storedTimes.get(key) + 1; |
|
82 |
this.storedTimes.replace(key, total); |
|
83 |
} |
|
84 |
|
|
85 |
} |
|
86 |
|
|
87 |
public synchronized void decStoredOccurrences(String fileId, int chunkNum) { |
|
88 |
String key = fileId + '.' + chunkNum; |
|
89 |
int total = this.storedTimes.get(key) - 1; |
|
90 |
this.storedTimes.replace(key, total); |
|
91 |
} |
|
92 |
|
|
93 |
public synchronized void removeStoredOccurrencesEntry(String fileId, int chunkNum) { |
|
94 |
String key = fileId + '.' + chunkNum; |
|
95 |
this.storedTimes.remove(key); |
|
96 |
} |
|
97 |
|
|
98 |
public void fillCurrRDChunks() { |
|
99 |
String chunkFileId; |
|
100 |
int chunkNum; |
|
101 |
|
|
102 |
for (Iterator<Chunk> iter = this.savedChunks.iterator(); iter.hasNext();) { |
|
103 |
chunkFileId = ((Chunk) iter).getfileId(); |
|
104 |
chunkNum = ((Chunk) iter).getNum(); |
|
105 |
|
|
106 |
String key = chunkFileId + "." + chunkNum; |
|
107 |
((Chunk) iter).setCurrentRepDegree(this.storedTimes.get(key)); |
|
108 |
} |
|
109 |
} |
|
110 |
|
|
111 |
public synchronized void incSpaceAvailable(String fileId, int chunkNum) { |
|
112 |
String chunkFileId; |
|
113 |
int chunkNumber; |
|
114 |
int chunkSize; |
|
115 |
|
|
116 |
for (Iterator<Chunk> iter = this.savedChunks.iterator(); iter.hasNext();) { |
|
117 |
chunkFileId = ((Chunk) iter).getfileId(); |
|
118 |
chunkNumber = ((Chunk) iter).getNum(); |
|
119 |
chunkSize = ((Chunk) iter).getchunkSize(); |
|
120 |
if (chunkFileId.equals(fileId) && chunkNumber == chunkNum) |
|
121 |
this.totalSpace = this.totalSpace + chunkSize; |
|
122 |
} |
|
123 |
} |
|
124 |
|
|
125 |
public ArrayList<Chunk> getSavedChunks() { |
|
126 |
return savedChunks; |
|
127 |
} |
|
128 |
|
|
129 |
public synchronized void setSavedChunks(ArrayList<Chunk> savedChunks) { |
|
130 |
this.savedChunks = savedChunks; |
|
131 |
} |
|
132 |
|
|
133 |
public ArrayList<Chunk> getReceivedChunks() { |
|
134 |
return receivedChunks; |
|
135 |
} |
|
136 |
|
|
137 |
public synchronized void setReceivedChunks(ArrayList<Chunk> receivedChunks) { |
|
138 |
this.receivedChunks = receivedChunks; |
|
139 |
} |
|
140 |
|
|
141 |
public ConcurrentHashMap<String, Integer> getStoredTimes() { |
|
142 |
return storedTimes; |
|
143 |
} |
|
144 |
|
|
145 |
public synchronized void setStoredTimes(ConcurrentHashMap<String, Integer> storedTimes) { |
|
146 |
this.storedTimes = storedTimes; |
|
147 |
} |
|
148 |
|
|
149 |
public ConcurrentHashMap<String, String> getRemainingChunks() { |
|
150 |
return remainingChunks; |
|
151 |
} |
|
152 |
|
|
153 |
public synchronized void setRemainingChunks(ConcurrentHashMap<String, String> remainingChunks) { |
|
154 |
this.remainingChunks = remainingChunks; |
|
155 |
} |
|
156 |
|
|
157 |
public void addRemainingChunks(String fileId, int chunkNum) { |
|
158 |
String key = fileId + '.' + chunkNum; |
|
159 |
this.remainingChunks.put(key, "false"); |
|
160 |
} |
|
161 |
|
|
162 |
public void setWantedChunkReceived(String fileId, int chunkNum) { |
|
163 |
String key = fileId + '.' + chunkNum; |
|
164 |
this.remainingChunks.replace(key, "true"); |
|
165 |
} |
|
166 |
|
|
167 |
public int getTotalSpace() { |
|
168 |
return totalSpace; |
|
169 |
} |
|
170 |
|
|
171 |
public synchronized void setTotalSpace(int totalSpace) { |
|
172 |
this.totalSpace = totalSpace; |
|
173 |
} |
|
174 |
|
|
175 |
public void decSpaceAvailable(int length) { |
|
176 |
totalSpace = totalSpace - length; |
|
177 |
} |
|
178 |
|
|
179 |
public synchronized int getSpaceAvailable() { |
|
180 |
return this.totalSpace; |
|
181 |
} |
|
182 |
|
|
183 |
} |
Also available in: Unified diff