/** * Here we manage the registered peers: - set crt as first in loaded_peers it may have been part * of the list before but we assume that its message was accounted in current_space * * @param crt */ private static void register_loaded(D_DirectoryEntry crt) { ArrayList<D_DirectoryEntry> rem = new ArrayList<D_DirectoryEntry>(); // crt.encoder = crt.getEncoder(); // if(crt.loaded_globals) crt.component_node.message = crt.encode(); //crt.encoder.getBytes(); synchronized (loaded_peers) { if (DEBUG) System.out.println("DSCache:register_loaded registering " + crt); if (crt.get_DDP2P_DoubleLinkedList_Node() == null) { boolean result = loaded_peers.offerFirst(crt); if (DEBUG) System.out.println("DSCache:register_loaded registered " + result); if (DEBUG) System.out.println("DSCache:register_loaded " + loaded_peers); // loaded_peer_By_LocalID.put(new Long(crt._peer_ID), crt); loaded_peer_By_GID.put(crt.globalID, crt); loaded_peer_By_GIDhash.put(crt.globalIDhash, crt); if (crt.component_node.message != null) { current_space += crt.component_node.message.length; } } else { loaded_peers.moveToFront(crt); } while ((loaded_peers.size() > MAX_LOADED_PEERS) || (current_space > MAX_PEERS_RAM)) { if (loaded_peers.size() <= MIN_PEERS_RAM) break; // at least _crt_peer and _myself // D_DirectoryEntry candidate = loaded_peers.getTail(); // if((candidate == D_DirectoryEntry._crt_peer)||(candidate == // D_DirectoryEntry._myself)){ // setRecent(candidate); // continue; // } D_DirectoryEntry removed = loaded_peers.removeTail(); // remove(loaded_peers.size()-1); // loaded_peer_By_LocalID.remove(new Long(removed._peer_ID)); loaded_peer_By_GID.remove(removed.globalID); loaded_peer_By_GIDhash.remove(removed.globalIDhash); if (removed.component_node.message != null) current_space -= removed.component_node.message.length; if (D_Directory_Storage.need_saving_contains(removed.globalIDhash, removed.instance)) rem.add(removed); } } if (DEBUG) System.out.println("DSCache:register_loaded cleaned " + loaded_peers); for (D_DirectoryEntry removed : rem) { try { synchronized (D_Directory_Storage.saver_thread_monitor) { if (D_Directory_Storage.need_saving_contains(removed.globalIDhash, removed.instance)) removed.update(); } } catch (P2PDDSQLException e) { e.printStackTrace(); } } if (DEBUG) System.out.println("DSCache:register_loaded exit " + loaded_peers); }
/** * Add a new instance to an existing peer cache. * * <p>Since the peer is assumed preloaded in the cache, instance not needed in global * hashtables. * * @param crt */ public static void register_loaded_instance(D_DirectoryEntry crt) { ArrayList<D_DirectoryEntry> rem = new ArrayList<D_DirectoryEntry>(); synchronized (loaded_peers) { loaded_peers.offerFirst(crt); if (crt.component_node.message != null) current_space += crt.component_node.message.length; /** Disconnect overflowing instances from list, set them in "removed" */ while ((loaded_peers.size() > MAX_LOADED_PEERS) || (current_space > MAX_PEERS_RAM)) { if (loaded_peers.size() <= MIN_PEERS_RAM) break; // at least _crt_peer and _myself D_DirectoryEntry removed = loaded_peers.removeTail(); // remove(loaded_peers.size()-1); if (removed.component_node.message != null) current_space -= removed.component_node.message.length; if (D_Directory_Storage.need_saving_contains(removed.globalIDhash, removed.instance)) rem.add(removed); else { for (D_DirectoryEntry a : removed.instances.values()) { if (D_Directory_Storage.need_saving_contains(a.globalIDhash, a.instance)) rem.add(a); } } } } /** Do the saving */ for (D_DirectoryEntry removed : rem) { try { synchronized (D_Directory_Storage.saver_thread_monitor) { if (D_Directory_Storage.need_saving_contains(removed.globalIDhash, removed.instance)) removed.update(); for (D_DirectoryEntry a : removed.instances.values()) { if (D_Directory_Storage.need_saving_contains(a.globalIDhash, a.instance)) a.update(); } } } catch (P2PDDSQLException e) { e.printStackTrace(); } } }
/** * Move this to the front of the list of items (tail being trimmed) * * @param crt */ private static void setRecent(D_DirectoryEntry crt) { loaded_peers.moveToFront(crt); }
/** @return */ private static String dumpDirCache() { String s = "["; s += loaded_peers.toString(); s += "]"; return s; }