📄 dxdiskhashmap.java
字号:
cache[cacheIndex] = entry; } } } private final void removeElementFromCache( Object key ) { if (cacheMask == 0) { return; } synchronized (cache) { int cacheIndex = key.hashCode() & cacheMask; cache[cacheIndex] = null; } } public void printStatistics() { System.out.println( "DxDiskHastable statistics:" ); System.out.println( " sub-table accesses: " + bufferAccesses + " hits: " + bufferHits + " loads: " + (bufferAccesses - bufferHits) ); System.out.println( " cache accesses: " + cacheAccesses + " hits: " + cacheHits ); } /** * Eine sub-tabelle will nachladen. Wenn der buffer voll ist, * muss eine andere verworfen werden. Die "aelteste" table ist * immer ein blatt, da die zeiten mmer beim rekursiven aufstieg * gesetzt werden. */ protected synchronized void readRequest( DxDiskSubTable subTable ) throws Exception { if (buffer.count() > maxBufferSize) { //die blatt-sub-tabelle mit der aeltesten zugriffszeit //suchen long time = Long.MAX_VALUE; DxIterator it = buffer.iterator(); DxDiskSubTable table; DxDiskSubTable bestMatch = null; while ((table = (DxDiskSubTable)it.next()) != null) { // check access time but do not deactivate rootTable if (table.accessTime < time && table != rootTable) { time = table.accessTime; bestMatch = table; } } // System.out.println (" - " + time); //test ob noch sub-tabels da sind // for (int i=0; i<DxDiskSubTable.SIZE; i++) { // DxDiskHashNode node = bestMatch.table[i]; // if (node != null) { // if (node.element == null && node.subTable.table == null) { // System.out.println ("Node is not a leaf!"); // break; // } // } // } //schreiben, leer-machen und aus buffer loeschen if (isDirtyTable( bestMatch )) { bestMatch.writeTable(); } deleteRequest( bestMatch ); } // subTable.touch(); buffer.add( subTable ); } /** * The specified sub-table was deleted from the tree. So we have * to delete it from the table buffer too. */ public synchronized void deleteRequest( DxDiskSubTable subTable ) { subTable.empty(); buffer.remove( subTable ); } /** * This method is synchronized because sub table filenames have to be unique. */ public synchronized File newSubTableFile() { return new File(baseFileName + "." + String.valueOf( subTableNameCount++ )); } /** * Computes a File object which represents the DxDiskSubTable file denoted by the given filename. * <div> * There are two formats for the given filename: * <ul> * <li> * The long format is a pathname relative to the current working directory of the * Java application which wrote the table referring the DxDiskSubTable in question. * It may also be an absolute pathname. This format is error prone as it does not * allow changing the working directory of Java applications or changing the location * of the table files within the filesystem. That's why the short format is used now. * </li> * <li> * The short format is only the last pathname component of the pathname to the referred DxDiskSubTable file. * The other pathname components (e.g. the directory where the DxDiskSubTable file resides) are determined * by the directory where the root table resides. This is possible because als DxDiskSubTable files reside * in the same directory as the root table file does. * </li> * </ul> * </div> * <div> * For compatibility with old tables, the long format is broken up into pathname components * and only the last pathname component is used then as a directory entry of the directory of the root table file. * </div> */ public File getFileForFilename(String filename) { int index = filename.lastIndexOf(File.separatorChar); if (index!=-1) { filename = filename.substring(index+1); } return new File(tableDirectory,filename); } /** * Delete all the files used by this hashtable. */ public void cleanFiles() { String baseName = new File( baseFileName ).getName(); File path = new File( new File( baseFileName ).getParent() ); String[] fileList = path.list(); for (int i = 0; i < fileList.length; i++) { if (fileList[i].startsWith( baseName )) { new File( path, fileList[i] ).delete(); } } } public synchronized void writeAllTables() throws Exception { DxIterator it = buffer.iterator(); DxDiskSubTable table = null; while ((table = (DxDiskSubTable)it.next()) != null) { table.writeTable(); } } public synchronized boolean addForKey( Object obj, Object key ) { try { if (rootTable.addForKey( obj, key )) { itemCount++; addElementToCache( obj, key, key.hashCode() ); return true; } else { return false; } } catch (Exception e) { e.printStackTrace(); throw new RuntimeException( e.toString() ); } } /** * Gives the element for the specified key.<p> * * * Note: This method is synchronized because the cache of subtables may * change. */ public synchronized Object elementForKey( Object key ) { try { int hashCode = key.hashCode(); Object cacheEntry = cachedElementForKey( key, hashCode ); if (cacheEntry != null) { return cacheEntry; } else { Object answer = rootTable.elementForKey( key, hashCode ); addElementToCache( answer, key, hashCode ); return answer; } } catch (Exception e) { e.printStackTrace(); throw new RuntimeException( e.toString() ); } } protected synchronized void elementDone( DxDiskHashCompatible obj ) { rootTable.elementDone( obj ); } public Object keyForElement( Object obj ) { throw new RuntimeException( "keyForElement() not implemented." ); } public synchronized boolean remove( Object obj ) { throw new RuntimeException( "remove() not implemented." ); } public synchronized Object removeForKey( Object key ) { try { Object obj = rootTable.removeForKey( key ); if (obj != null) { itemCount--; removeElementFromCache( key ); } return obj; } catch (Exception e) { e.printStackTrace(); throw new RuntimeException( e.toString() ); } } public DxIterator iterator() { return new DxDiskHashIterator( this ); } public int count() { return itemCount; } public boolean isEmpty() { return itemCount == 0; } public boolean containsKey( Object key ) { return elementForKey( key ) != null; } public int levelTableBitSize(int depth) { return tableBitSizes[depth]; } public int maxDepth() { return tableBitSizes.length - 1; } public int oldTablesHashMaskShift() { return oldTablesHashMaskShift; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -