root / DistributedBackupService / src / chunk / FolderManager.java @ 1
History | View | Annotate | Download (4.43 KB)
1 | 1 | up20130859 | package chunk; |
---|---|---|---|
2 | |||
3 | import java.io.*; |
||
4 | import java.util.ArrayList; |
||
5 | import chunk.FileManager; |
||
6 | import server.Peer; |
||
7 | import utils.Utils; |
||
8 | |||
9 | import static java.util.Arrays.copyOfRange; |
||
10 | |||
11 | public class FolderManager { |
||
12 | private static long maxMemory; |
||
13 | private static long usedMemory; |
||
14 | private Peer peer;
|
||
15 | private String path; |
||
16 | private FileManager fileManager;
|
||
17 | private static int chunkSizeLimit; |
||
18 | |||
19 | public FolderManager(Peer peer, long maxMemory) throws ClassNotFoundException { |
||
20 | this.peer = peer;
|
||
21 | FolderManager.maxMemory = maxMemory; |
||
22 | |||
23 | chunkSizeLimit = 64000;
|
||
24 | usedMemory = 0;
|
||
25 | path = "results/peer_" + peer.getID() + "/"; |
||
26 | |||
27 | File db = new File(path + "db"); |
||
28 | |||
29 | if (db.exists()) {
|
||
30 | fileManager = FileManager.loadDatabase(db); |
||
31 | } else {
|
||
32 | fileManager = new FileManager();
|
||
33 | } |
||
34 | makeDir(path + Utils.CHUNKS); |
||
35 | } |
||
36 | |||
37 | public static void makeDir(String name) { |
||
38 | File file = new File(name); |
||
39 | file.mkdirs(); |
||
40 | } |
||
41 | |||
42 | synchronized public static boolean backupFile(String name, String path, byte[] information) throws IOException { |
||
43 | if (getFreeMemory() < information.length) {
|
||
44 | System.out.println("Without space to backup file"); |
||
45 | return false; |
||
46 | } |
||
47 | FileOutputStream out;
|
||
48 | out = new FileOutputStream(path + "/" + name); |
||
49 | out.write(information); |
||
50 | out.close(); |
||
51 | |||
52 | return FolderManager.incrUsedMemory(information.length);
|
||
53 | } |
||
54 | |||
55 | synchronized public static byte[] loadFile(File file) throws IOException { |
||
56 | FileInputStream inStream;
|
||
57 | byte[] information; |
||
58 | |||
59 | inStream = new FileInputStream(file); |
||
60 | information = new byte[(int) file.length()]; |
||
61 | |||
62 | inStream.read(information); |
||
63 | inStream.close(); |
||
64 | |||
65 | return information;
|
||
66 | } |
||
67 | |||
68 | public static ArrayList<Chunk> loadChunks(String path, int chunksNumber) throws IOException { |
||
69 | ArrayList<Chunk> chunks = new ArrayList<>(); |
||
70 | |||
71 | for (int i = 0; i <= chunksNumber; i++) { |
||
72 | byte[] information = loadFile(new File(path + "/" + i)); |
||
73 | Chunk chunk = new Chunk("", i, 1, information); |
||
74 | chunks.add(chunk); |
||
75 | } |
||
76 | |||
77 | return chunks;
|
||
78 | } |
||
79 | |||
80 | public static ArrayList<Chunk> fileSplit(byte[] fileInformation, String fileID, int replication) { |
||
81 | ArrayList<Chunk> chunks;
|
||
82 | int numChunks;
|
||
83 | |||
84 | numChunks = fileInformation.length / chunkSizeLimit + 1;
|
||
85 | chunks = new ArrayList<>(); |
||
86 | |||
87 | for (int i = 0; i < numChunks; i++) |
||
88 | { |
||
89 | byte[] chunkinformation; |
||
90 | |||
91 | if (i == numChunks - 1) |
||
92 | { |
||
93 | int leftOverBytes = fileInformation.length - (i * chunkSizeLimit);
|
||
94 | chunkinformation = copyOfRange(fileInformation, i * chunkSizeLimit, i * chunkSizeLimit + leftOverBytes); |
||
95 | } |
||
96 | else if (i == numChunks - 1 && fileInformation.length % chunkSizeLimit == 0) |
||
97 | { |
||
98 | chunkinformation = new byte[0]; |
||
99 | } |
||
100 | else
|
||
101 | { |
||
102 | chunkinformation = copyOfRange(fileInformation, i * chunkSizeLimit, i * chunkSizeLimit + chunkSizeLimit); |
||
103 | } |
||
104 | |||
105 | Chunk chunk = new Chunk(fileID, i, replication, chunkinformation);
|
||
106 | chunks.add(chunk); |
||
107 | } |
||
108 | |||
109 | return chunks;
|
||
110 | } |
||
111 | |||
112 | |||
113 | public String getChunkPath(String fileID, int chunkNo) { |
||
114 | return getChunksPath() + fileID + "/" + chunkNo; |
||
115 | } |
||
116 | |||
117 | public byte[] loadChunk(String fileID, int chunkNo) { |
||
118 | byte[] chunkinformation = null; |
||
119 | String chunkPath = getChunksPath() + fileID + "/" + chunkNo; |
||
120 | |||
121 | try {
|
||
122 | chunkinformation = loadFile(new File(chunkPath)); |
||
123 | } catch (IOException e) { |
||
124 | e.printStackTrace(); |
||
125 | } |
||
126 | |||
127 | return chunkinformation;
|
||
128 | } |
||
129 | |||
130 | |||
131 | public String getRootPath() { |
||
132 | return path;
|
||
133 | } |
||
134 | |||
135 | public String getChunksPath() { |
||
136 | return path + Utils.CHUNKS;
|
||
137 | } |
||
138 | |||
139 | public String getRestoresPath() { |
||
140 | return path + Utils.RESTORES;
|
||
141 | } |
||
142 | |||
143 | public FileManager getDatabase() {
|
||
144 | return fileManager;
|
||
145 | } |
||
146 | |||
147 | public void deleteChunk(String fileID, int chunkNo) { |
||
148 | String chunkPath = getChunkPath(fileID, chunkNo);
|
||
149 | File file = new File(chunkPath); |
||
150 | |||
151 | long chunkSize = file.length();
|
||
152 | file.delete(); |
||
153 | decUsedMemory(chunkSize); |
||
154 | fileManager.deleteChunk(fileID, chunkNo); |
||
155 | } |
||
156 | |||
157 | public static long getMaxMemory() { |
||
158 | return maxMemory;
|
||
159 | } |
||
160 | |||
161 | public static void setMaxMemory(int maxMemory) { |
||
162 | FolderManager.maxMemory = maxMemory; |
||
163 | } |
||
164 | |||
165 | public static long getUsedMemory() { |
||
166 | return FolderManager.usedMemory;
|
||
167 | } |
||
168 | |||
169 | public static long getFreeMemory() { |
||
170 | return maxMemory - usedMemory;
|
||
171 | } |
||
172 | |||
173 | private static void decUsedMemory(long n) { |
||
174 | usedMemory -= n; |
||
175 | if (usedMemory < 0) { |
||
176 | usedMemory = 0;
|
||
177 | System.out.println("Used memory went below 0"); |
||
178 | } |
||
179 | } |
||
180 | |||
181 | private static boolean incrUsedMemory(long n) { |
||
182 | if (usedMemory + n > maxMemory) {
|
||
183 | System.out.println("Tried to surpass memory restrictions"); |
||
184 | return false; |
||
185 | } |
||
186 | usedMemory += n; |
||
187 | System.out.println("Used memory: " + usedMemory + " / " + maxMemory); |
||
188 | return true; |
||
189 | } |
||
190 | } |