Skip to content

Commit

Permalink
More Small Progress on Reclaim
Browse files Browse the repository at this point in the history
  • Loading branch information
JoaoASousa committed Apr 10, 2021
1 parent fbfb898 commit 9f36f98
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
4 changes: 3 additions & 1 deletion proj1/.idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions proj1/src/FileStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,10 @@ public void removeChunkFromStoredChunkFiles(Chunk chunk) {
storedChunkFiles.remove(chunk);
}

public Set<Chunk> getStoredChunkFiles() {
return storedChunkFiles;
}

public static void saveToDisk(){
try{
FileOutputStream fs = new FileOutputStream(Peer.serviceDirectory + "/" + "State");
Expand Down
7 changes: 4 additions & 3 deletions proj1/src/Peer.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,13 @@ public void delete(String filepath) {
FileStorage.saveToDisk();
}

public void reclaim(long spaceReclaim) {
public void reclaim(long spaceReclaim) { // spaceReclaim -> KB
System.out.println("RECLAIM SERVICE -> DISK SPACE RECLAIM = " + spaceReclaim);

// String messageString = this.protocolVersion + " REMOVED " + peerID + " " + fileParser.getFileID() + " " + "\r\n" + "\r\n";
// byte[] messageBytes = messageString.getBytes();
if (Reclaim.checkIfNewMaxSpaceIsEnough(spaceReclaim)) return;

String generalREMOVEDMessage = this.protocolVersion + " REMOVED " + peerID + " ";
Reclaim.deleteBackups(spaceReclaim, generalREMOVEDMessage);

// FileStorage.saveToDisk();
}
Expand Down
43 changes: 43 additions & 0 deletions proj1/src/Reclaim.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,47 @@
import java.util.HashSet;
import java.util.Set;

public class Reclaim {

public static boolean checkIfNewMaxSpaceIsEnough(long newMaxUsedSpace) {

Set<Chunk> chunks = FileStorage.instance.getStoredChunkFiles();

long spaceCurrentlyUsed = 0;

for (Chunk c : chunks) {
spaceCurrentlyUsed += c.getContent().length;
}

return spaceCurrentlyUsed <= newMaxUsedSpace * 1000;
}

public static void deleteBackups(long maxUsedSpace, String generalREMOVEDMessage) {
long maxUsedSpaceBytes = maxUsedSpace * 1000;

// pick what chunks to delete so as to obey the new max used space
// while () {
//
// }

// delete those chunks

// update do rep degree


// for each chunk send message
Set<Integer> listOfChunkNumbersDeleted = new HashSet<>();
listOfChunkNumbersDeleted.add(1); // temporary. just not to give warning in for loop

for (int i = 0; i < listOfChunkNumbersDeleted.size(); i++) {

// String messageString = generalREMOVEDMessage + fileObject.getFileID() + " " + chunkNr + " " + "\r\n" + "\r\n";
// byte[] messageBytes = messageString.getBytes();
//
// Peer.getMC().sendMessage(messageBytes);
}
}


public static void processPacketREMOVED(String[] splitHeader) {
// FileId + ChunkNr
Expand All @@ -24,4 +66,5 @@ public static void processPacketREMOVED(String[] splitHeader) {
// Abort sending the backup msg if it receives a PUTCHUNK packet
// while in the delay
}

}

0 comments on commit 9f36f98

Please sign in to comment.