/** * Puts a DHTObject into the index database. Internally, the expiration timeout of the data item * is set to be {@link KademliaConfig#DATA_EXPIRATION_TIME} from the current simulation time * ("now"). * * <p>Adding an item does <i>not</i> overwrite an existing value if it is stored under the same * key. It is furthermore assumed that this method is called each time a data item is republished, * even if the initiator of that republish is the local node. * * @param key the KademliaOverlayKey belonging to the DHTObject. * @param newValue the DHTObject that represents the data item to be inserted. */ public final void put(final KademliaOverlayKey key, final DHTObject newValue) { KademliaIndexEntry indexEntry; indexEntry = index.get(key); if (indexEntry == null) { indexEntry = new KademliaIndexEntry(newValue, config); index.put(key, indexEntry); } // set last republish time indexEntry.updateLastRepublish(); }
/** * Looks up the DHTObject associated with <code>key</code> in the local database. * * @param key the KademliaOverlayKey of the data item to be looked up. * @return the DHTObject associated with <code>key</code>. If no such object exists (or it has * expired), <code>null</code> is returned. */ public final DHTObject get(final KademliaOverlayKey key) { final KademliaIndexEntry indexEntry = index.get(key); if (indexEntry == null) { // no entry known return null; } else if (indexEntry.hasExpired()) { // old entry expired index.remove(key); return null; } return indexEntry.getValue(); }