root / DistributedBackupService / src / chunk / FileManager.java @ 2
History | View | Annotate | Download (4.59 KB)
1 | 1 | up20130859 | package chunk; |
---|---|---|---|
2 | |||
3 | import java.io.File; |
||
4 | import java.io.FileInputStream; |
||
5 | import java.io.IOException; |
||
6 | import java.io.ObjectInputStream; |
||
7 | import java.io.Serializable; |
||
8 | import java.util.HashMap; |
||
9 | import java.util.Map; |
||
10 | import java.util.Set; |
||
11 | import java.util.concurrent.ConcurrentHashMap; |
||
12 | import java.util.concurrent.ConcurrentMap; |
||
13 | |||
14 | @SuppressWarnings("serial") |
||
15 | public class FileManager implements Serializable { |
||
16 | |||
17 | private ConcurrentMap<String, FileManager> fileRepository; |
||
18 | private ConcurrentMap<String, ConcurrentMap<Integer, Chunk>> chunkRepository; |
||
19 | |||
20 | public FileManager() {
|
||
21 | fileRepository = new ConcurrentHashMap<>(); |
||
22 | chunkRepository = new ConcurrentHashMap<>(); |
||
23 | } |
||
24 | |||
25 | private String fileID; |
||
26 | private String pathName; |
||
27 | private String fileName; |
||
28 | private int numChunks; |
||
29 | private int wantedReplication; |
||
30 | private HashMap<String, Chunk> chunkMap; |
||
31 | |||
32 | public FileManager( File file, String fileID, int replicationDegree, HashMap<String, Chunk> chunksInfo ) { |
||
33 | this.fileID = fileID;
|
||
34 | this.fileName = file.getName();
|
||
35 | this.pathName = file.getPath();
|
||
36 | this.numChunks = chunksInfo.size();
|
||
37 | this.wantedReplication = replicationDegree;
|
||
38 | this.chunkMap = chunksInfo;
|
||
39 | } |
||
40 | |||
41 | synchronized public static FileManager loadDatabase(File file) throws ClassNotFoundException { |
||
42 | FileManager db = null;
|
||
43 | |||
44 | try {
|
||
45 | final ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(file)); |
||
46 | db = (FileManager) inputStream.readObject(); |
||
47 | inputStream.close(); |
||
48 | } catch (final IOException pE) { |
||
49 | pE.printStackTrace(); |
||
50 | } |
||
51 | return db;
|
||
52 | } |
||
53 | |||
54 | public void addFile( String pathName, FileManager fileInfo ) { |
||
55 | fileRepository.put(pathName, fileInfo); |
||
56 | } |
||
57 | |||
58 | public void removeFile( String pathName ) { |
||
59 | fileRepository.remove(pathName); |
||
60 | } |
||
61 | |||
62 | public FileManager getFileInformation( String pathName) |
||
63 | { |
||
64 | return fileRepository.get( pathName);
|
||
65 | } |
||
66 | |||
67 | public void addChunk( Chunk chunk ) |
||
68 | { |
||
69 | |||
70 | String fileID = chunk.getFileID();
|
||
71 | int chunkNo = chunk.getChunkNo();
|
||
72 | |||
73 | ConcurrentMap<Integer, Chunk> fileChunks; |
||
74 | fileChunks = chunkRepository.getOrDefault(fileID, new ConcurrentHashMap<>()); |
||
75 | fileChunks.putIfAbsent(chunkNo, chunk); |
||
76 | |||
77 | chunkRepository.putIfAbsent(fileID, fileChunks); |
||
78 | |||
79 | } |
||
80 | |||
81 | public Chunk getChunkInformation( String fileID, int chunkNo ) |
||
82 | { |
||
83 | Map<Integer, Chunk> fileChunks = chunkRepository.get(fileID); |
||
84 | |||
85 | return fileChunks != null ? fileChunks.get(chunkNo) : null; |
||
86 | } |
||
87 | |||
88 | public void deleteChunk( String fileID, int chunkNo) |
||
89 | { |
||
90 | if (!chunkRepository.containsKey(fileID))
|
||
91 | return;
|
||
92 | |||
93 | chunkRepository.get(fileID).remove(chunkNo); |
||
94 | } |
||
95 | |||
96 | public void deleteFileBackedUp( String fileID) |
||
97 | { |
||
98 | if (!chunkRepository.containsKey( fileID))
|
||
99 | return;
|
||
100 | |||
101 | chunkRepository.remove(fileID); |
||
102 | } |
||
103 | |||
104 | public Integer getRecognizedReplication(String fileID, int chunkNo) |
||
105 | { |
||
106 | int ret;
|
||
107 | try
|
||
108 | { |
||
109 | ret = chunkRepository.get(fileID).get(chunkNo).getNumChunks(); |
||
110 | } catch (NullPointerException e) { |
||
111 | return null; |
||
112 | } |
||
113 | return ret;
|
||
114 | } |
||
115 | |||
116 | 2 | up20130859 | // Getters e Setters
|
117 | |||
118 | 1 | up20130859 | public boolean chunkExists(String fileID) { |
119 | return chunkRepository.containsKey(fileID);
|
||
120 | } |
||
121 | |||
122 | 2 | up20130859 | public ConcurrentMap<String, FileManager> getFileRepository() { |
123 | return fileRepository;
|
||
124 | } |
||
125 | |||
126 | public void setFileRepository(ConcurrentMap<String, FileManager> fileRepository) { |
||
127 | this.fileRepository = fileRepository;
|
||
128 | } |
||
129 | |||
130 | public ConcurrentMap<String, ConcurrentMap<Integer, Chunk>> getChunkRepository() { |
||
131 | return chunkRepository;
|
||
132 | } |
||
133 | |||
134 | public void setChunkRepository(ConcurrentMap<String, ConcurrentMap<Integer, Chunk>> chunkRepository) { |
||
135 | this.chunkRepository = chunkRepository;
|
||
136 | } |
||
137 | |||
138 | public String getPathName() { |
||
139 | return pathName;
|
||
140 | } |
||
141 | |||
142 | public void setPathName(String pathName) { |
||
143 | this.pathName = pathName;
|
||
144 | } |
||
145 | |||
146 | public HashMap<String, Chunk> getChunkMap() { |
||
147 | return chunkMap;
|
||
148 | } |
||
149 | |||
150 | public void setChunkMap(HashMap<String, Chunk> chunkMap) { |
||
151 | this.chunkMap = chunkMap;
|
||
152 | } |
||
153 | |||
154 | public void setFileID(String fileID) { |
||
155 | this.fileID = fileID;
|
||
156 | } |
||
157 | |||
158 | public void setFileName(String fileName) { |
||
159 | this.fileName = fileName;
|
||
160 | } |
||
161 | |||
162 | public void setNumChunks(int numChunks) { |
||
163 | this.numChunks = numChunks;
|
||
164 | } |
||
165 | |||
166 | public void setWantedReplication(int wantedReplication) { |
||
167 | this.wantedReplication = wantedReplication;
|
||
168 | } |
||
169 | |||
170 | 1 | up20130859 | public Set<Integer> getFileChunksKey(String fileID) { |
171 | return chunkRepository.get(fileID).keySet();
|
||
172 | } |
||
173 | 2 | up20130859 | |
174 | public String getFileID() { |
||
175 | return fileID;
|
||
176 | } |
||
177 | 1 | up20130859 | |
178 | 2 | up20130859 | public int getNumChunks() { |
179 | return numChunks;
|
||
180 | } |
||
181 | |||
182 | public String getPathname() { |
||
183 | return pathName;
|
||
184 | } |
||
185 | |||
186 | public String getFileName() { |
||
187 | return fileName;
|
||
188 | } |
||
189 | |||
190 | public int getWantedReplication() { |
||
191 | return wantedReplication;
|
||
192 | } |
||
193 | |||
194 | public HashMap<String, Chunk> getChunks() { |
||
195 | return chunkMap;
|
||
196 | } |
||
197 | |||
198 | public int getNumChunks(String pathname) { |
||
199 | return fileRepository.get(pathname).getNumChunks();
|
||
200 | } |
||
201 | |||
202 | 1 | up20130859 | } |