Project

General

Profile

Revision 2

Final delivery

View differences:

StorageSystem.java
7 7
import java.nio.file.Files;
8 8
import java.nio.file.Paths;
9 9
import java.util.ArrayList;
10
import java.util.Collections;
10 11

  
12

  
11 13
public class StorageSystem{
12 14

  
13 15
	private long storage_capacity = 100000000; 
......
26 28
	public long getSpace_available() {
27 29
		return space_available;
28 30
	}
29

  
31
	
32
	public void updateStorage() {
33
		space_available = storage_capacity - used_storage;
34
	}
30 35
	private ArrayList<Chunk> chunks;
31 36
	private ArrayList<FileInfo> fileinfo;
32 37
	private ArrayList<BackUpInfo> backupinfo;
......
47 52
	
48 53
	}
49 54
	
50
	public boolean deleteChunk(Chunk c) {
51
		 
52
        if(isStored(c)) {
55
	public void increaseChunkRepDeg(String fileID, int chunkn) {
56
		
57
	 for(Chunk chunk: chunks) {
58
    	if(chunk.getFileID().equals(fileID) && chunk.getChunkN()==chunkn) 
59
    		chunk.increaseRepDeg();
53 60
 
54
            this.chunks.remove(c);
55
            used_storage= used_storage - c.getsize();
56
            return true;       
61
     	}
62
	}
63
	
64
	
65
	public int[] getChunkInfo(){
66
		int[] a = new int[2];		
67
		return a;
68
	}
69
	
70
	public boolean deleteChunkByFileID(String fileID) {
71
		 boolean found = false;
72
		 ArrayList<Chunk> aux = new ArrayList<Chunk>();
73
        for(Chunk chunk: chunks) {
74
        	if(chunk.getFileID().equals(fileID)) {
75
        		aux.add(chunk);
76
        		found = true;
77
        		 used_storage= used_storage - chunk.getsize();
78
        		 
79

  
80
        	}
57 81
 
82
            
83
                 
84
 
58 85
        }
59
        System.out.println("Chunk doesn't exist");
60
        return false;
86
        
87
        chunks.removeAll(aux);
88
        updateStorage();
89
        if(!found)
90
        System.out.println("File isn't saved in this peer");
91
        return found;
61 92
 
62 93
    }
94
	
95
	public boolean deleteChunkByFileIDAndChunkN(String fileID, int chunkN) {
96
		 boolean found = false;
97
		 ArrayList<Chunk> aux = new ArrayList<Chunk>();
98
       for(Chunk chunk: chunks) {
99
       	if(chunk.getFileID().equals(fileID) && chunk.getChunkN()==chunkN) {
100
       		aux.add(chunk);
101
       		found = true;
102
       		 used_storage= used_storage - chunk.getsize();
103
       		 
104

  
105
       	}
106

  
107
           
108
                
109

  
110
       }
111
       
112
       chunks.removeAll(aux);
113
       updateStorage();
114
       if(!found)
115
       System.out.println("File isn't saved in this peer");
116
       return found;
117

  
118
   }
63 119
 
64 120
	
65 121
	public boolean addChunk(Chunk c) {
......
68 124
		
69 125
				if(!isStored(c)) {
70 126
					chunks.add(c);
127
					updateStorage();
71 128
					return true;
72 129
				}
73 130
		
......
99 156
		return isEqualChunk;
100 157
	}
101 158
	
102
	public void addFile(String filename, long data, String hash) {
103
		getFileInfo().add(new FileInfo(hash, data, filename, this.peerID));
159
	public void addFile(String filename, long data, String hash, int repdeg) {
160
		getFileInfo().add(new FileInfo(hash, data, filename, this.peerID, repdeg));
104 161
	}
105 162
	
106 163

  
107 164
	   
108
	   public boolean moreRecent(FileInfo a, FileInfo b) {
109
	        if (a.getDateModified() > b.getDateModified())
110
	            return true; // highest value first
111
	        else  return false;
112
	    }
165
	
166
    public boolean compare(FileInfo e1, FileInfo e2) {
167
        return (e1.getDateModified() - e2.getDateModified()>0);
168
    }
113 169
	   
114 170
	   public String lookUp(String filename) {
115 171
		   ArrayList<FileInfo> samename = new ArrayList<FileInfo>();
116 172
			String aux = null;
117
			FileInfo auxFile = null;
118 173
			
174
			
119 175
			for(int i = 0; i < getFileInfo().size(); i++) {
120 176
				
121
				
122
			
123 177
			if(getFileInfo().get(i).getFilename().equals(filename))
124 178
				samename.add(getFileInfo().get(i));
125 179
				
126 180
			}
127 181

  
128
			if(samename.size() == 1) {
129
			aux = samename.get(0).getFileID();
182
			Collections.sort(samename,(a,b)->a.getDateModified() - b.getDateModified());
130 183
			
131
			}
132 184
			
133
			else {
134
				for(int j = 0; j < samename.size(); j++) {
135
					if(moreRecent(samename.get(j), auxFile)){
136
						auxFile = samename.get(j);
137
						aux = samename.get(j).getFileID();
138
						}
139
						}
140
					}
141 185
			
142 186
			
143
			return aux;
187
			return samename.get(0).getFileID();
144 188
	   }
145 189
	   
146 190
	   
......
170 214
			
171 215
		}
172 216
		
217
	
173 218
		
219
		int perceivedRepDeg(String fileID, int ChunkN) {
220
			int repdeg = 0;
221
			for(BackUpInfo chunk: backupinfo) {
222
				if(chunk.getChunkN() == ChunkN && chunk.getFileID().equals(fileID))
223
					repdeg++;
224
			}
225
			return repdeg;
226
		}
227
		
228
		
174 229
		public void loadFileInfo() throws IOException {
175 230
			
176 231
			if(Files.exists(Paths.get("/fileInfo/Peer"+peerID+"/"))) {
......
205 260
		public void setFileInfo(ArrayList<FileInfo> fileinfo) {
206 261
			this.fileinfo = fileinfo;
207 262
		}
263

  
264
		public boolean lowerRepDeg(String fileID, int chunkNo) {
265
			
266
			for(Chunk chunk: chunks) {
267
				if(chunk.getFileID().equals(fileID) && chunk.getChunkN() == chunkNo)
268
				{
269
					chunk.decreaseRepDeg();
270
					if(chunk.getGoalRepDeg() > chunk.getRepDeg()) {
271
						return true;
272
					}
273
				}
274
			}
275
			return false;
276
		}
208 277
	  
209 278
}
210 279

  

Also available in: Unified diff