Project

General

Profile

Statistics
| Revision:

root / DistributedBackupService / src / chunk / FileManager.java @ 1

History | View | Annotate | Download (3.49 KB)

1
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
        public String getFileID() {
42
                return fileID;
43
        }
44

    
45
        public int getNumChunks() {
46
                return numChunks;
47
        }
48

    
49
        public String getPathname() {
50
                return pathName;
51
        }
52

    
53
        public String getFileName() {
54
                return fileName;
55
        }
56

    
57
        public int getWantedReplication() {
58
                return wantedReplication;
59
        }
60

    
61
        public HashMap<String, Chunk> getChunks() {
62
                return chunkMap;
63
        }
64

    
65
        synchronized public static FileManager loadDatabase(File file) throws ClassNotFoundException {
66
                FileManager db = null;
67

    
68
                try {
69
                        final ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(file));
70
                        db = (FileManager) inputStream.readObject();
71
                        inputStream.close();
72
                } catch (final IOException pE) {
73
                        pE.printStackTrace();
74
                }
75
                return db;
76
        }
77

    
78
        public void addFile( String pathName, FileManager fileInfo ) {
79
                fileRepository.put(pathName, fileInfo);
80
        }
81

    
82
        public void removeFile( String pathName ) {
83
                fileRepository.remove(pathName);
84
        }
85

    
86
        public FileManager getFileInformation( String pathName) 
87
        {
88
                return fileRepository.get( pathName);
89
        }
90

    
91
        public void addChunk( Chunk chunk ) 
92
        {
93
                
94
                String fileID = chunk.getFileID();
95
                int chunkNo = chunk.getChunkNo();
96

    
97
                ConcurrentMap<Integer, Chunk> fileChunks;
98
                fileChunks = chunkRepository.getOrDefault(fileID, new ConcurrentHashMap<>());
99
                fileChunks.putIfAbsent(chunkNo, chunk);
100

    
101
                chunkRepository.putIfAbsent(fileID, fileChunks);
102

    
103
        }
104
 
105
        public Chunk getChunkInformation( String fileID, int chunkNo ) 
106
        {
107
                Map<Integer, Chunk> fileChunks = chunkRepository.get(fileID);
108

    
109
                return fileChunks != null ? fileChunks.get(chunkNo) : null;
110
        }
111

    
112
        public void deleteChunk( String fileID, int chunkNo) 
113
        {
114
                if (!chunkRepository.containsKey(fileID))
115
                        return;
116

    
117
                chunkRepository.get(fileID).remove(chunkNo);
118
        }
119

    
120
        public void deleteFileBackedUp( String fileID) 
121
        {
122
                if (!chunkRepository.containsKey( fileID))
123
                        return;
124

    
125
                chunkRepository.remove(fileID);
126
        }
127

    
128
        public int getNumChunks(String pathname) {
129
                return fileRepository.get(pathname).getNumChunks();
130
        }
131

    
132
        public Integer getRecognizedReplication(String fileID, int chunkNo) 
133
        {
134
                int ret;
135
                try 
136
                {        
137
                        ret = chunkRepository.get(fileID).get(chunkNo).getNumChunks();
138
                } catch (NullPointerException e) {
139
                        return null;
140
                }
141
                return ret;
142
        }
143

    
144
        public boolean chunkExists(String fileID) {
145
                return chunkRepository.containsKey(fileID);
146
        }
147

    
148
        public Set<Integer> getFileChunksKey(String fileID) {
149
                return chunkRepository.get(fileID).keySet();
150
        }
151

    
152
}