1 /*
2 * Copyright (C) 2010, Google Inc.
3 * and other copyright owners as documented in the project's IP log.
4 *
5 * This program and the accompanying materials are made available
6 * under the terms of the Eclipse Distribution License v1.0 which
7 * accompanies this distribution, is reproduced below, and is
8 * available at http://www.eclipse.org/org/documents/edl-v10.php
9 *
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
15 *
16 * - Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 *
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials provided
22 * with the distribution.
23 *
24 * - Neither the name of the Eclipse Foundation, Inc. nor the
25 * names of its contributors may be used to endorse or promote
26 * products derived from this software without specific prior
27 * written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 */
43
44 package org.eclipse.jgit.internal.storage.pack;
45
46 /**
47 * Supports {@link DeltaIndex} by performing a partial scan of the content.
48 */
49 class DeltaIndexScanner {
50 final int[] table;
51
52 // To save memory the buckets for hash chains are stored in correlated
53 // arrays. This permits us to get 3 values per entry, without paying
54 // the penalty for an object header on each entry.
55
56 final long[] entries;
57
58 final int[] next;
59
60 final int tableMask;
61
62 private int entryCnt;
63
64 DeltaIndexScanner(byte[] raw, int len) {
65 // Clip the length so it falls on a block boundary. We won't
66 // bother to scan the final partial block.
67 //
68 len -= (len % DeltaIndex.BLKSZ);
69
70 final int worstCaseBlockCnt = len / DeltaIndex.BLKSZ;
71 if (worstCaseBlockCnt < 1) {
72 table = new int[] {};
73 tableMask = 0;
74
75 entries = new long[] {};
76 next = new int[] {};
77
78 } else {
79 table = new int[tableSize(worstCaseBlockCnt)];
80 tableMask = table.length - 1;
81
82 // As we insert blocks we preincrement so that 0 is never a
83 // valid entry. Therefore we have to allocate one extra space.
84 //
85 entries = new long[1 + worstCaseBlockCnt];
86 next = new int[entries.length];
87
88 scan(raw, len);
89 }
90 }
91
92 private void scan(byte[] raw, final int end) {
93 // We scan the input backwards, and always insert onto the
94 // front of the chain. This ensures that chains will have lower
95 // offsets at the front of the chain, allowing us to prefer the
96 // earlier match rather than the later match.
97 //
98 int lastHash = 0;
99 int ptr = end - DeltaIndex.BLKSZ;
100 do {
101 final int key = DeltaIndex.hashBlock(raw, ptr);
102 final int tIdx = key & tableMask;
103
104 final int head = table[tIdx];
105 if (head != 0 && lastHash == key) {
106 // Two consecutive blocks have the same content hash,
107 // prefer the earlier block because we want to use the
108 // longest sequence we can during encoding.
109 //
110 entries[head] = (((long) key) << 32) | ptr;
111 } else {
112 final int eIdx = ++entryCnt;
113 entries[eIdx] = (((long) key) << 32) | ptr;
114 next[eIdx] = head;
115 table[tIdx] = eIdx;
116 }
117
118 lastHash = key;
119 ptr -= DeltaIndex.BLKSZ;
120 } while (0 <= ptr);
121 }
122
123 private static int tableSize(final int worstCaseBlockCnt) {
124 int shift = 32 - Integer.numberOfLeadingZeros(worstCaseBlockCnt);
125 int sz = 1 << (shift - 1);
126 if (sz < worstCaseBlockCnt)
127 sz <<= 1;
128 return sz;
129 }
130 }