@ -65,8 +65,8 @@ import org.eclipse.jgit.lib.ObjectStream;
* file are discovered .
* /
class SimilarityIndex {
/** The {@link #idHash} table stops growing at {@code 1 << MAX_HASH_BITS} . */
private static final int MAX_HASH_BITS = 17 ;
/** A special {@link TableFullException} used in place of OutOfMemoryError . */
private static final TableFullException TABLE_FULL_OUT_OF_MEMORY = new TableFullException ( ) ;
/ * *
* Shift to apply before storing a key .
@ -76,20 +76,26 @@ class SimilarityIndex {
* /
private static final int KEY_SHIFT = 32 ;
/** Maximum value of the count field, also mask to extract the count. */
private static final long MAX_COUNT = ( 1L < < KEY_SHIFT ) - 1 ;
/** Total size of the file we hashed into the structure. */
private long fileSize ;
/** Number of non-zero entries in {@link #idHash}. */
private int idSize ;
/** {@link #idSize} that triggers {@link #idHash} to double in size. */
private int idGrowAt ;
/ * *
* Pairings of content keys and counters .
* < p >
* Slots in the table are actually two ints wedged into a single long . The
* upper { @link # MAX_HASH_BITS } bits stores the content key , and the
* remaining lower bits stores the number of bytes associated with that key .
* Empty slots are denoted by 0 , which cannot occur because the count cannot
* be 0 . Values can only be positive , which we enforce during key addition .
* upper 32 bits stores the content key , and the remaining lower bits stores
* the number of bytes associated with that key . Empty slots are denoted by
* 0 , which cannot occur because the count cannot be 0 . Values can only be
* positive , which we enforce during key addition .
* /
private long [ ] idHash ;
@ -99,6 +105,7 @@ class SimilarityIndex {
SimilarityIndex ( ) {
idHashBits = 8 ;
idHash = new long [ 1 < < idHashBits ] ;
idGrowAt = growAt ( idHashBits ) ;
}
long getFileSize ( ) {
@ -109,7 +116,8 @@ class SimilarityIndex {
fileSize = size ;
}
void hash ( ObjectLoader obj ) throws MissingObjectException , IOException {
void hash ( ObjectLoader obj ) throws MissingObjectException , IOException ,
TableFullException {
if ( obj . isLarge ( ) ) {
ObjectStream in = obj . openStream ( ) ;
try {
@ -125,7 +133,7 @@ class SimilarityIndex {
}
}
void hash ( byte [ ] raw , int ptr , final int end ) {
void hash ( byte [ ] raw , int ptr , final int end ) throws TableFullException {
while ( ptr < end ) {
int hash = 5381 ;
int start = ptr ;
@ -141,7 +149,8 @@ class SimilarityIndex {
}
}
void hash ( InputStream in , long remaining ) throws IOException {
void hash ( InputStream in , long remaining ) throws IOException ,
TableFullException {
byte [ ] buf = new byte [ 4096 ] ;
int ptr = 0 ;
int cnt = 0 ;
@ -190,11 +199,11 @@ class SimilarityIndex {
return ( int ) ( ( common ( dst ) * maxScore ) / max ) ;
}
int common ( SimilarityIndex dst ) {
long common ( SimilarityIndex dst ) {
return common ( this , dst ) ;
}
private static int common ( SimilarityIndex src , SimilarityIndex dst ) {
private static long common ( SimilarityIndex src , SimilarityIndex dst ) {
int srcIdx = src . packedIndex ( 0 ) ;
int dstIdx = dst . packedIndex ( 0 ) ;
long [ ] srcHash = src . idHash ;
@ -202,12 +211,12 @@ class SimilarityIndex {
return common ( srcHash , srcIdx , dstHash , dstIdx ) ;
}
private static int common ( long [ ] srcHash , int srcIdx , //
private static long common ( long [ ] srcHash , int srcIdx , //
long [ ] dstHash , int dstIdx ) {
if ( srcIdx = = srcHash . length | | dstIdx = = dstHash . length )
return 0 ;
int common = 0 ;
long common = 0 ;
int srcKey = keyOf ( srcHash [ srcIdx ] ) ;
int dstKey = keyOf ( dstHash [ dstIdx ] ) ;
@ -230,8 +239,8 @@ class SimilarityIndex {
break ;
srcKey = keyOf ( srcHash [ srcIdx ] ) ;
} else /* if (srcKey > dst Key) */ {
// Regions of dst which do not appear in dst .
} else /* if (dstKey < src Key) */ {
// Regions of dst which do not appear in src .
if ( + + dstIdx = = dstHash . length )
break ;
dstKey = keyOf ( dstHash [ dstIdx ] ) ;
@ -268,7 +277,7 @@ class SimilarityIndex {
return ( idHash . length - idSize ) + idx ;
}
void add ( int key , int cnt ) {
void add ( int key , int cnt ) throws TableFullException {
key = ( key * 0x9e370001 ) > > > 1 ; // Mix bits and ensure not negative.
int j = slot ( key ) ;
@ -276,18 +285,20 @@ class SimilarityIndex {
long v = idHash [ j ] ;
if ( v = = 0 ) {
// Empty slot in the table, store here.
if ( shouldGrow ( ) ) {
if ( idGrowAt < = idSize ) {
grow ( ) ;
j = slot ( key ) ;
continue ;
}
idHash [ j ] = ( ( ( long ) key ) < < KEY_SHIFT ) | cnt ;
idHash [ j ] = pair ( key , cnt ) ;
idSize + + ;
return ;
} else if ( keyOf ( v ) = = key ) {
// Same key, increment the counter.
idHash [ j ] = v + cnt ;
// Same key, increment the counter. If it overflows, fail
// indexing to prevent the key from being impacted.
//
idHash [ j ] = pair ( key , countOf ( v ) + cnt ) ;
return ;
} else if ( + + j > = idHash . length ) {
@ -296,6 +307,12 @@ class SimilarityIndex {
}
}
private static long pair ( int key , long cnt ) throws TableFullException {
if ( MAX_COUNT < cnt )
throw new TableFullException ( ) ;
return ( ( ( long ) key ) < < KEY_SHIFT ) | cnt ;
}
private int slot ( int key ) {
// We use 31 - idHashBits because the upper bit was already forced
// to be 0 and we want the remaining high bits to be used as the
@ -304,16 +321,26 @@ class SimilarityIndex {
return key > > > ( 31 - idHashBits ) ;
}
private boolean shouldGrow ( ) {
return idHashBits < MAX_HASH_BITS & & idHash . length < = idSize * 2 ;
private static int growAt ( int idHashBits ) {
return ( 1 < < idHashBits ) * ( idHashBits - 3 ) / idHashBits ;
}
private void grow ( ) {
private void grow ( ) throws TableFullException {
if ( idHashBits = = 30 )
throw new TableFullException ( ) ;
long [ ] oldHash = idHash ;
int oldSize = idHash . length ;
idHashBits + + ;
idGrowAt = growAt ( idHashBits ) ;
try {
idHash = new long [ 1 < < idHashBits ] ;
} catch ( OutOfMemoryError noMemory ) {
throw TABLE_FULL_OUT_OF_MEMORY ;
}
for ( int i = 0 ; i < oldSize ; i + + ) {
long v = oldHash [ i ] ;
if ( v ! = 0 ) {
@ -330,7 +357,11 @@ class SimilarityIndex {
return ( int ) ( v > > > KEY_SHIFT ) ;
}
private static int countOf ( long v ) {
return ( int ) v ;
private static long countOf ( long v ) {
return v & MAX_COUNT ;
}
static class TableFullException extends Exception {
private static final long serialVersionUID = 1L ;
}
}