📄 dbz.java
字号:
/// What's a good table size to hold this many entries?
public int dbzsize( int contents )
{
int n;
if ( contents <= 0 )
return DEFSIZE;
n = ( contents / 2 ) * 3; // try to keep table at most 2/3 full
if ( Utils.even( n ) ) // make it odd
++n;
while ( ! isprime( n ) ) // and look for a prime
n += 2;
return n;
}
private static final int[] smallPrimes = {
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37 };
/// Is a number prime?
private boolean isprime( int x )
{
int div = 0, stop;
// Hit the first few primes quickly to eliminate easy ones.
// This incidentally prevents ridiculously small tables.
for (int i = 0; i < smallPrimes.length; ++i )
{
div = smallPrimes[i];
if ( x % div == 0 )
return false;
}
// Approximate square root of x.
for ( stop = x; x / stop < stop; stop >>= 1 )
;
stop <<= 1;
// Try odd numbers up to stop.
for ( div += 2; div < stop; div += 2 )
if ( x % div == 0 )
return false;
return true;
}
/// Close a database.
public void dbmclose()
{
dbzsync();
try
{
basef.close();
dirf.close();
pagf.close();
}
catch ( IOException ignore ) {}
}
/// Push all in-core data out to disk.
public void dbzsync()
{
if ( ! written )
return;
if ( corepag != null )
putcore( corepag );
if ( ! olddbz )
putconf();
}
/// Cancel writing of in-core data.
// Mostly for use from child process.
public void dbzcancel()
{
written = false;
}
/// Like get(), but assumes the key has already been case-mapped.
public Object fetch( Object key )
{
Object output = null;
prev = null;
String skey = (String) key;
int cmplen = skey.length();
// !!!
return output;
}
/// Like put(), but assumes the key has already been case-mapped.
public Object store( Object key, Object value )
{
if ( readOnly )
throw new RuntimeException( "database is read-only" );
// !!!
return null;
}
/// Control attempts to keep .pag file in core.
public void dbzincore( boolean value )
{
incore = value;
}
/// Get configuration from .dir file.
private void getconf()
{
// !!!
}
/// Write configuration to .dir file.
private void putconf()
{
// !!!
}
/// Try to set up an in-core copy of .pag file.
private int[] getcore()
{
// !!!
return null;
}
/// Try to rewrite an in-core table.
private void putcore( int[] tab )
{
// !!!
}
/// Set up to start or restart a search.
private void start()
{
// !!!
}
/// Conduct part of a searc.
private int search()
{
// !!!
return NOTFOUND;
}
/// Check that a value can be stored.
private boolean okayvalue( int value )
{
if ( HASTAG( value ) )
return false;
return true;
}
/// Store a value into a location previously found by search.
private void set( DbzSearcher s, int value )
{
// !!!
}
/// Determine this machine's byte map.
private void mybytemap( int[] map )
{
// !!!
}
/// Transform an offset from byte ordering map1 to map2.
private int bytemap( int ino, int[] map1, int[] map2 )
{
// !!!
return ino;
}
// This is a simplified version of the pathalias hashing function.
// Thanks to Steve Belovin and Peter Honeyman
//
// Hash a string into a long int. 31 bit crc (from andrew appel).
// The crc table is computed at run time by crcinit() -- we could
// precompute, but it takes 1 clock tick on a 750.
//
// This fast table calculation works only if POLY is a prime polynomial
// in the field of integers modulo 2. Since the coefficients of a
// 32-bit polynomial won't fit in a 32-bit word, the high-order bit is
// implicit. IT MUST ALSO BE THE CASE that the coefficients of orders
// 31 down to 25 are zero. Happily, we have candidates, from
// E. J. Watson, "Primitive Polynomials (Mod 2)", Math. Comp. 16 (1962):
// x^32 + x^7 + x^5 + x^3 + x^2 + x^1 + x^0
// x^31 + x^3 + x^0
//
// We reverse the bits to get:
// 111101010000000000000000000000001 but drop the last 1
// f 5 0 0 0 0 0 0
// 010010000000000000000000000000001 ditto, for 31-bit crc
// 4 8 0 0 0 0 0 0
// 31-bit polynomial (avoids sign problems).
private static final int POLY = 0x48000000;
private int[] crctable = new int[128];
/// Initialize tables for hash function.
private void crcinit()
{
for ( int i = 0; i < crctable.length; ++i )
{
int sum = 0;
for ( int j = 7 - 1; j >= 0; --j )
if ( ( i & ( 1 << j ) ) != 0 )
sum ^= POLY >> j;
crctable[i] = sum;
}
}
/// Honeyman's nice hashing function.
private int hash( String name )
{
int sum = 0;
for ( int i = 0; i < name.length(); ++i )
{
int b = (int) name.charAt( i );
int j = ( sum ^ b ) & 0x7f;
sum = ( sum >> 7 ) ^ crctable[j];
}
return sum;
}
// Case-mapping stuff.
// We exploit the fact that we are dealing only with headers here, and
// headers are limited to the ASCII characters by RFC822. It is barely
// possible that we might be dealing with a translation into another
// character set, but in particular it's very unlikely for a header
// character to be outside -128..255.
// !!!
private String mapcase( String src )
{
// !!!
return src;
}
}
class DbzSearcher
{
public int place; // current location in file
public int tabno; // which table we're in
public int run; // how long we'll stay in this table
public int hash; // the key's hash code (for optimization)
public int tag; // tag we are looking for
public boolean seen; // have we examined current location?
public boolean aborted; // has i/o error aborted search?
}
class DbzKeyEnumerator implements Enumeration
{
public DbzKeyEnumerator()
{
// !!!
}
public boolean hasMoreElements()
{
// !!!
return false;
}
public Object nextElement()
{
// !!!
return null;
}
}
class DbzElementEnumerator implements Enumeration
{
private Dbz dbz;
private Enumeration keys;
public DbzElementEnumerator( Dbz dbz )
{
this.dbz = dbz;
keys = dbz.keys();
}
public boolean hasMoreElements()
{
return keys.hasMoreElements();
}
public Object nextElement()
{
Object key = keys.nextElement();
return dbz.get( key );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -