root / src / peer / Storage.java @ 1
History | View | Annotate | Download (1.44 KB)
1 | 1 | up20150524 | package peer; |
---|---|---|---|
2 | |||
3 | import files.FileHandler; |
||
4 | |||
5 | import java.io.IOException; |
||
6 | import java.math.BigInteger; |
||
7 | import java.nio.file.Path; |
||
8 | import java.nio.file.Paths; |
||
9 | import java.util.Hashtable; |
||
10 | |||
11 | import chord.ChordManager; |
||
12 | |||
13 | public class Storage { |
||
14 | private long spaceReserved; |
||
15 | private long spaceOcupied; |
||
16 | private Hashtable<BigInteger, String> fileStored; |
||
17 | |||
18 | public Storage(long spaceReserved) throws IOException { |
||
19 | this.spaceReserved = spaceReserved;
|
||
20 | updateSpaceOcupied(); |
||
21 | |||
22 | fileStored = new Hashtable<>(); |
||
23 | } |
||
24 | |||
25 | public void insertHashtable(BigInteger hashFile, String fileName) { |
||
26 | fileStored.put(hashFile, fileName); |
||
27 | } |
||
28 | |||
29 | public long getSpaceReserved() { |
||
30 | return spaceReserved;
|
||
31 | } |
||
32 | |||
33 | public long getSpaceOcupied() { |
||
34 | return spaceOcupied;
|
||
35 | } |
||
36 | |||
37 | public void updateSpaceOcupied() throws IOException { |
||
38 | Path pathPeerFolder = Paths.get("./peerDisk/peer" + Peer.getPeerAccessPoint() + "-" + ChordManager.peerHash); |
||
39 | this.spaceOcupied = FileHandler.getSize(pathPeerFolder) / 1024; |
||
40 | } |
||
41 | |||
42 | |||
43 | public void setSpaceReserved(long spaceReserved) throws IOException { |
||
44 | this.spaceReserved = spaceReserved;
|
||
45 | updateSpaceOcupied(); |
||
46 | if (this.spaceReserved < this.spaceOcupied) { |
||
47 | FileHandler.clearStorageSpace();
|
||
48 | } |
||
49 | } |
||
50 | |||
51 | public Hashtable<BigInteger, String > getHashtable() { |
||
52 | return fileStored;
|
||
53 | } |
||
54 | } |