Top Description Inners Fields Constructors Methods
org.eclipse.jgit.diff

public Class SimilarityIndex

extends Object
Class Inheritance
Imports
java.io.EOFException, .IOException, .InputStream, java.util.Arrays, org.eclipse.jgit.errors.MissingObjectException, org.eclipse.jgit.lib.ObjectLoader, .ObjectStream

Index structure of lines/blocks in one file.

This structure can be used to compute an approximation of the similarity between two files. The index is used by org.eclipse.jgit.diff.SimilarityRenameDetector to compute scores between files.

To save space in memory, this index uses a space efficient encoding which will not exceed 1 MiB per instance. The index starts out at a smaller size (closer to 2 KiB), but may grow as more distinct blocks within the scanned file are discovered.

Since
4.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
SimilarityIndex.TableFullException

Thrown by create() when file is too large.

Field Summary

Modifier and TypeField and Description
private long
hashedCnt

Total amount of bytes hashed into the structure, including \n.

private int
idGrowAt

idSize that triggers idHash to double in size.

private long[]
idHash

Pairings of content keys and counters.

private int
idHashBits

idHash.length == 1 << idHashBits.

private int
idSize

Number of non-zero entries in idHash.

private static final int
KEY_SHIFT

Shift to apply before storing a key.

private static final long
MAX_COUNT

Maximum value of the count field, also mask to extract the count.

public static final SimilarityIndex.TableFullException
TABLE_FULL_OUT_OF_MEMORY

A special TableFullException used in place of OutOfMemoryError.

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
pack-priv void
add(int key, int cnt)

pack-priv long
private static long
private static long
common(long[] srcHash, int srcIdx, long[] dstHash, int dstIdx)

pack-priv long
count(int idx)

private static long
countOf(long v)

public static SimilarityIndex

Returns:

similarity index for this object
create
(ObjectLoader
the object to hash
obj
)

Create a new similarity index for the given object

pack-priv int
findIndex(int key)

private void
grow()

private static int
growAt(int idHashBits)

pack-priv void
pack-priv void
hash(byte[] raw, int ptr, int end)

pack-priv void
hash(InputStream in, long remaining, boolean text)

private void
pack-priv static boolean
pack-priv int
key(int idx)

private static int
keyOf(long v)

private int
packedIndex(int idx)

private static long
pair(int key, long cnt)

public int

Returns:

the similarity score
score
(SimilarityIndex
the other index
dst
,
int
the score representing a 100% match
maxScore
)

Compute the similarity score between this index and another.

pack-priv int
size()

private int
slot(int key)

pack-priv void
sort()

Sort the internal table so it can be used for efficient scoring.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

hashedCntback to summary
private long hashedCnt

Total amount of bytes hashed into the structure, including \n. This is usually the size of the file minus number of CRLF encounters.

idGrowAtback to summary
private int idGrowAt

idSize that triggers idHash to double in size.

idHashback to summary
private long[] idHash

Pairings of content keys and counters.

Slots in the table are actually two ints wedged into a single long. The 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.

idHashBitsback to summary
private int idHashBits

idHash.length == 1 << idHashBits.

idSizeback to summary
private int idSize

Number of non-zero entries in idHash.

KEY_SHIFTback to summary
private static final int KEY_SHIFT

Shift to apply before storing a key.

Within the 64 bit table record space, we leave the highest bit unset so all values are positive. The lower 32 bits to count bytes.

MAX_COUNTback to summary
private static final long MAX_COUNT

Maximum value of the count field, also mask to extract the count.

TABLE_FULL_OUT_OF_MEMORYback to summary
public static final SimilarityIndex.TableFullException TABLE_FULL_OUT_OF_MEMORY

A special TableFullException used in place of OutOfMemoryError.

Constructor Detail

SimilarityIndexback to summary
pack-priv SimilarityIndex()

Method Detail

addback to summary
pack-priv void add(int key, int cnt) throws TableFullException
commonback to summary
pack-priv long common(SimilarityIndex dst)
commonback to summary
private static long common(SimilarityIndex src, SimilarityIndex dst)
commonback to summary
private static long common(long[] srcHash, int srcIdx, long[] dstHash, int dstIdx)
countback to summary
pack-priv long count(int idx)
countOfback to summary
private static long countOf(long v)
createback to summary
public static SimilarityIndex create(ObjectLoader obj) throws IOException, TableFullException

Create a new similarity index for the given object

Parameters
obj:ObjectLoader

the object to hash

Returns:SimilarityIndex

similarity index for this object

Exceptions
IOException:
file contents cannot be read from the repository.
TableFullException:
object hashing overflowed the storage capacity of the SimilarityIndex.
findIndexback to summary
pack-priv int findIndex(int key)
growback to summary
private void grow() throws TableFullException
Annotations
@SuppressWarnings:UnusedException
growAtback to summary
private static int growAt(int idHashBits)
hashback to summary
pack-priv void hash(ObjectLoader obj) throws MissingObjectException, IOException, TableFullException
hashback to summary
pack-priv void hash(byte[] raw, int ptr, int end) throws TableFullException
hashback to summary
pack-priv void hash(InputStream in, long remaining, boolean text) throws IOException, TableFullException
hashLargeObjectback to summary
private void hashLargeObject(ObjectLoader obj) throws IOException, TableFullException
isBinaryback to summary
pack-priv static boolean isBinary(ObjectLoader obj) throws IOException
keyback to summary
pack-priv int key(int idx)
keyOfback to summary
private static int keyOf(long v)
packedIndexback to summary
private int packedIndex(int idx)
pairback to summary
private static long pair(int key, long cnt) throws TableFullException
scoreback to summary
public int score(SimilarityIndex dst, int maxScore)

Compute the similarity score between this index and another.

A region of a file is defined as a line in a text file or a fixed-size block in a binary file. To prepare an index, each region in the file is hashed; the values and counts of hashes are retained in a sorted table. Define the similarity fraction F as the count of matching regions between the two files divided between the maximum count of regions in either file. The similarity score is F multiplied by the maxScore constant, yielding a range [0, maxScore]. It is defined as maxScore for the degenerate case of two empty files.

The similarity score is symmetrical; i.e. a.score(b) == b.score(a).

Parameters
dst:SimilarityIndex

the other index

maxScore:int

the score representing a 100% match

Returns:int

the similarity score

sizeback to summary
pack-priv int size()
slotback to summary
private int slot(int key)
sortback to summary
pack-priv void sort()

Sort the internal table so it can be used for efficient scoring.

Once sorted, additional lines/blocks cannot be added to the index.

org.eclipse.jgit.diff back to summary

public Class SimilarityIndex.TableFullException

extends Exception
Class Inheritance

Thrown by create() when file is too large.

Field Summary

Modifier and TypeField and Description
private static final long

Constructor Summary

AccessConstructor and Description
public

Field Detail

serialVersionUIDback to summary
private static final long serialVersionUID

Hides java.lang.Exception.serialVersionUID.

Constructor Detail

TableFullExceptionback to summary
public TableFullException()