FileDocCategorySizeDatePackage
BloomFilterImpl.javaAPI DocAzureus 3.0.3.410049Thu Feb 09 19:42:48 GMT 2006com.aelitis.azureus.core.util.bloom.impl

BloomFilterImpl

public abstract class BloomFilterImpl extends Object implements com.aelitis.azureus.core.util.bloom.BloomFilter

Fields Summary
private static final int
HASH_NUM
private static final int
a2
private static final int
a3
private static final int
a4
private static final int
b2
private static final int
b3
private static final int
b4
private int
max_entries
private BigInteger
bi_max_entries
private int
entry_count
Constructors Summary
public BloomFilterImpl(int _max_entries)

	
	 
	
				 
	
		bi_max_entries	= new BigInteger( ""+(((_max_entries/2)*2)+1));
				
		max_entries	= bi_max_entries.intValue();
	
Methods Summary
public intadd(byte[] value)

		return( add( bytesToInteger( value )));
	
protected intadd(int value)

		int	count = 0xffff;

		for (int i=0;i<HASH_NUM;i++){
			
			int	index = getHash( i, value );
					
				// v = value BEFORE inc
			
			int	v = incValue( index );
			
			if ( v < count ){
				
				count = v;
			}
		}
		
		if ( count == 0 ){
			
			entry_count++;
		}
		
			// count is the smallest val found *before* incrementing
				
		return( trimValue( count + 1 ));
	
protected intbytesToInteger(byte[] data)

		int	res = 0x51f7ac81;
		
		for (int i=0;i<data.length;i++){
			
			//res ^= (data[i]&0xff)<<((i%4)*8);
			
			res = res * 191 + (data[i]&0xff);
		}
		
		return( res );
	
protected booleancontains(int value)

		for (int i=0;i<HASH_NUM;i++){
			
			int	index = getHash( i, value );
			
			int	v = getValue( index );
				
			if ( v == 0 ){
				
				return( false );
			}
		}
		
		return( true );		
	
public booleancontains(byte[] value)

		return( contains( bytesToInteger( value )));
	
public intcount(byte[] value)

		return( count( bytesToInteger( value )));
	
protected intcount(int value)

		int	count = 0xffff;
		
		for (int i=0;i<HASH_NUM;i++){
			
			int	index = getHash( i, value );
			
			int	v = getValue( index );
				
			if ( v < count ){
				
				count	= v;
			}
		}
		
		return( count );	
	
protected abstract intdecValue(int index)

public intgetEntryCount()

		return( entry_count );
	
protected intgetHash(int function, int value)

		long	res;
		
		switch( function ){
			case 0:
			{
					// x mod p
				
				res = value;
				
				break;
			}
			case 1:
			{
				 	// x^2 mod p
				
				res	= value * value;
				
				break;
			}
			case 2:
			{
					// bx + a mod p
				
				res = value *  a2 + b2;
				
				break;
			}
			case 3:
			{
					// cx + d mod p
				
				res = value * a3 + b3;
				
				break;

			}
			case 4:
			{
					// ex + f mod p
				
				res = value * a4 + b4;
				
				break;
			}
			default:
			{
				System.out.println( "**** BloomFilter hash function doesn't exist ****" );
				
				res = 0;
			}
		}
		
		// System.out.println( "hash[" + function + "] " + value + "->" + r );
		
		return( Math.abs( (int)res % max_entries ));
	
protected intgetMaxEntries()

		return( max_entries );
	
protected static byte[]getSerialization(byte[] address, int port)

		    //combine address and port bytes into one
		    byte[] full_address = new byte[ address.length +2 ];
		    System.arraycopy( address, 0, full_address, 0, address.length );
		    full_address[ address.length ] = (byte)(port >> 8);
		    full_address[ address.length +1 ] = (byte)(port & 0xff);
		    return full_address;
	
public intgetSize()

		return( max_entries );
	
protected abstract intgetValue(int index)

protected abstract intincValue(int index)

public static voidmain(java.lang.String[] args)

		Random	rand = new Random();

		/*
		BloomFilter b1 = new BloomFilterAddRemove8Bit(10000);

		for (int i=0;i<260;i++){
			
			System.out.println( b1.add( "parp".getBytes()) + ", count = " + b1.count( "parp".getBytes()) + ", ent = " + b1.getEntryCount());
			
		}
		
		for (int i=0;i<260;i++){
			
			System.out.println( b1.remove( "parp".getBytes())+ ", count = " + b1.count( "parp".getBytes()) + ", ent = " + b1.getEntryCount());
		}
		*/
		
		/*

		BloomFilter b1 = new BloomFilterAddOnly(90*10/3);

		byte[]	key1 = new byte[4];

		for (int i=0;i<200;i++){
			
			
			if ( i%2==0){
				rand.nextBytes( key1 );
			}
			
			b1.add( key1 );
			
			System.out.println( "entries = " + b1.getEntryCount() + ", act = " + i );
		}
		
		System.exit(0);
		*/
		
		
		int	fp_count = 0;
		
		for (int j=0;j<1000;j++){
			
			long	start = System.currentTimeMillis();
			
			BloomFilter b = new BloomFilterAddRemove8Bit(10000);
			//BloomFilter b = new BloomFilterAddOnly(10000);
			
			int	fp = 0;
			
			for (int i=0;i<1000;i++){
				
				//String	key = "" + rand.nextInt();
				
				byte[]	key = new byte[6];
				
				rand.nextBytes( key );
				
				//key  = getSerialization( key, 6881 );
				
				if ( i%2 == 0 ){
					
					b.add( key  );
				
					if ( !b.contains( key )){
						
						System.out.println( "false negative on add!!!!" );
					}
				}else{
					
					if ( b.contains( key )){
						
						fp++;
					}
				}
				
				/*
				if ( i%2 == 0 ){
					
					b.remove( key  );
				
					if ( b.contains( key )){
					
						System.out.println( "false positive" );
					}
				}
				*/
			}	
			
			System.out.println( "" + (System.currentTimeMillis() - start + ", fp = " + fp ));
			
			if ( fp > 0 ){
				
				fp_count++;
			}
		}
		
		System.out.println( fp_count );
	
public intremove(byte[] value)

		return( remove( bytesToInteger( value )));
	
protected intremove(int value)

		int	count = 0xffff;
		
		for (int i=0;i<HASH_NUM;i++){
			
			int	index = getHash( i, value );
				
				// v = value BEFORE dec
			
			int	v = decValue( index );
			
			if ( v < count ){
				
				count = v;
			}
		}		
		
		if ( count == 1 && entry_count > 0 ){
			
			entry_count--;
		}
		
			// count is the value BEFORE dec, decrease one further
		
		return( trimValue( count - 1 ));
	
protected abstract inttrimValue(int value)