Project

General

Profile

Statistics
| Revision:

root / DistributedBackupService / src / chunk / FolderManager.java

History | View | Annotate | Download (5.03 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 2 up20130859
42
        public static ArrayList<Chunk> loadChunks(String path, int chunksNumber) throws IOException {
43
                ArrayList<Chunk> chunks = new ArrayList<>();
44 1 up20130859
45 2 up20130859
                for (int i = 0; i <= chunksNumber; i++) {
46
                        byte[] information = loadFile(new File(path + "/" + i));
47
                        Chunk chunk = new Chunk("", i, 1, information);
48
                        chunks.add(chunk);
49
                }
50
51
                return chunks;
52
        }
53
54 1 up20130859
        synchronized public static boolean backupFile(String name, String path, byte[] information) throws IOException {
55
                if (getFreeMemory() < information.length) {
56
                        System.out.println("Without space to backup file");
57
                        return false;
58
                }
59
                FileOutputStream out;
60
                out = new FileOutputStream(path + "/" + name);
61
                out.write(information);
62
                out.close();
63
64
                return FolderManager.incrUsedMemory(information.length);
65
        }
66
67
        synchronized public static byte[] loadFile(File file) throws IOException {
68
                FileInputStream inStream;
69
                byte[] information;
70
71
                inStream = new FileInputStream(file);
72
                information = new byte[(int) file.length()];
73
74
                inStream.read(information);
75
                inStream.close();
76
77
                return information;
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 byte[] loadChunk(String fileID, int chunkNo) {
114
                byte[] chunkinformation = null;
115
                String chunkPath = getChunksPath() + fileID + "/" + chunkNo;
116
117
                try {
118
                        chunkinformation = loadFile(new File(chunkPath));
119
                } catch (IOException e) {
120
                        e.printStackTrace();
121
                }
122
123
                return chunkinformation;
124
        }
125
126
        public void deleteChunk(String fileID, int chunkNo) {
127 2 up20130859
                String chunkPath = getChunksPath() + fileID + "/" + chunkNo;
128 1 up20130859
                File file = new File(chunkPath);
129
130
                long chunkSize = file.length();
131
                file.delete();
132
                decUsedMemory(chunkSize);
133
                fileManager.deleteChunk(fileID, chunkNo);
134
        }
135
136
        private static void decUsedMemory(long n) {
137
                usedMemory -= n;
138
                if (usedMemory < 0) {
139
                        usedMemory = 0;
140
                        System.out.println("Used memory went below 0");
141
                }
142
        }
143
144
        private static boolean incrUsedMemory(long n) {
145
                if (usedMemory + n > maxMemory) {
146
                        System.out.println("Tried to surpass memory restrictions");
147
                        return false;
148
                }
149
                usedMemory += n;
150
                System.out.println("Used memory: " + usedMemory + " / " + maxMemory);
151
                return true;
152
        }
153 2 up20130859
154
        // Getters and setters
155
156
        public static long getMaxMemory() { return maxMemory; }
157
158
        public static void setMaxMemory(int maxMemory) { FolderManager.maxMemory = maxMemory; }
159
160
        public static long getUsedMemory() { return FolderManager.usedMemory; }
161
162
        public static long getFreeMemory() { return maxMemory - usedMemory; }
163
164
        public String getRootPath() { return path; }
165
166
        public String getChunksPath() { return path + Utils.CHUNKS; }
167
168
        public String getRestoresPath() { return path + Utils.RESTORES; }
169
170
        public FileManager getDatabase() { return fileManager; }
171
172
        public Peer getPeer() { return peer; }
173
174
        public void setPeer(Peer peer) { this.peer = peer; }
175
176
        public String getPath() { return path; }
177
178
        public void setPath(String path) { this.path = path; }
179
180
        public FileManager getFileManager() { return fileManager; }
181
182
        public void setFileManager(FileManager fileManager) { this.fileManager = fileManager; }
183
184
        public static int getChunkSizeLimit() {        return chunkSizeLimit; }
185
186
        public static void setChunkSizeLimit(int chunkSizeLimit) { FolderManager.chunkSizeLimit = chunkSizeLimit; }
187
188
        public static void setMaxMemory(long maxMemory) { FolderManager.maxMemory = maxMemory; }
189
190
        public static void setUsedMemory(long usedMemory) {        FolderManager.usedMemory = usedMemory; }
191 1 up20130859
}