Content deletion from a node that is no longer one of the K-Closest is working!

This commit is contained in:
Joshua Kissoon 2014-03-22 15:52:37 +05:30
parent d9fdcc57fd
commit 219e6073ed
4 changed files with 84 additions and 3 deletions

View File

@ -12,6 +12,7 @@ import java.util.NoSuchElementException;
import kademlia.core.Configuration; import kademlia.core.Configuration;
import kademlia.core.GetParameter; import kademlia.core.GetParameter;
import kademlia.exceptions.ContentExistException; import kademlia.exceptions.ContentExistException;
import kademlia.exceptions.ContentNotFoundException;
import kademlia.node.NodeId; import kademlia.node.NodeId;
import kademlia.serializer.JsonSerializer; import kademlia.serializer.JsonSerializer;
@ -156,6 +157,36 @@ public class DHT
throw new NoSuchElementException(); throw new NoSuchElementException();
} }
/**
* Delete a content from local storage
*
* @param content The Content to Remove
*
*
* @throws kademlia.exceptions.ContentNotFoundException
*/
public void remove(KadContent content) throws ContentNotFoundException
{
this.remove(new StorageEntry(content));
}
public void remove(StorageEntry entry) throws ContentNotFoundException
{
String folder = this.getContentStorageFolderName(entry.getKey());
File file = new File(folder + File.separator + entry.hashCode() + ".kct");
entriesManager.remove(entry);
if (file.exists())
{
file.delete();
}
else
{
throw new ContentNotFoundException();
}
}
/** /**
* Get the name of the folder for which a content should be stored * Get the name of the folder for which a content should be stored
* *

View File

@ -7,6 +7,7 @@ import java.util.Map;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import kademlia.core.GetParameter; import kademlia.core.GetParameter;
import kademlia.exceptions.ContentExistException; import kademlia.exceptions.ContentExistException;
import kademlia.exceptions.ContentNotFoundException;
import kademlia.node.NodeId; import kademlia.node.NodeId;
/** /**
@ -159,6 +160,23 @@ class StorageEntryManager
return entriesRet; return entriesRet;
} }
public void remove(KadContent content) throws ContentNotFoundException
{
this.remove(new StorageEntry(content));
}
public void remove(StorageEntry entry) throws ContentNotFoundException
{
if (contains(entry))
{
this.entries.get(entry.getKey()).remove(entry);
}
else
{
throw new ContentNotFoundException("This content does not exist in the Storage Entries");
}
}
@Override @Override
public String toString() public String toString()
{ {

View File

@ -0,0 +1,21 @@
package kademlia.exceptions;
/**
* An exception used to indicate that a content does not exist on the DHT
*
* @author Joshua Kissoon
* @created 20140322
*/
public class ContentNotFoundException extends Exception
{
public ContentNotFoundException()
{
super();
}
public ContentNotFoundException(String message)
{
super(message);
}
}

View File

@ -6,6 +6,7 @@ import kademlia.core.Configuration;
import kademlia.core.KadServer; import kademlia.core.KadServer;
import kademlia.dht.DHT; import kademlia.dht.DHT;
import kademlia.dht.StorageEntry; import kademlia.dht.StorageEntry;
import kademlia.exceptions.ContentNotFoundException;
import kademlia.message.Message; import kademlia.message.Message;
import kademlia.message.StoreContentMessage; import kademlia.message.StoreContentMessage;
import kademlia.node.Node; import kademlia.node.Node;
@ -69,9 +70,19 @@ public class ContentRefreshOperation implements Operation
} }
} }
/** /* Delete any content on this node that this node is not one of the K-Closest nodes to */
* @todo Delete any content on this node that this node is not one of the K-Closest nodes to try
*/ {
if (!closestNodes.contains(this.localNode))
{
this.dht.remove(e);
}
}
catch (ContentNotFoundException cnfe)
{
/* It would be weird if the content is not found here */
System.err.println("ContentRefreshOperation: Removing content from local node, content not found... Message: " + cnfe.getMessage());
}
} }
} }