packfile: refactor hash search with fanout table
Subsequent patches will introduce file formats that make use of a fanout array and a sorted table containing hashes, just like packfiles. Refactor the hash search in packfile.c into its own function, so that those patches can make use of it as well. Signed-off-by: Jonathan Tan <jonathantanmy@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jonathan Tan committed
Feb 13, 2018 at 10:39 UTC
b4e00f7306a160639f047b3421985e8f3d0c6fb1
3 files changed
+54
-14
packfile.c
+4
-14
@@ -1712,7 +1712,8 @@ off_t find_pack_entry_one(const unsigned char *sha1,
1712
{
1713
const uint32_t *level1_ofs = p->index_data;
1714
const unsigned char *index = p->index_data;
1715
- unsigned hi, lo, stride;
1715
+ unsigned stride;
1716
+ uint32_t result;
1717
1718
if (!index) {
1719
if (open_pack_index(p))
@@ -1725,8 +1726,6 @@ off_t find_pack_entry_one(const unsigned char *sha1,
1726
index += 8;
1727
}
1728
index += 4 * 256;
1728
- hi = ntohl(level1_ofs[*sha1]);
1729
- lo = ((*sha1 == 0x0) ? 0 : ntohl(level1_ofs[*sha1 - 1]));
1729
if (p->index_version > 1) {
1730
stride = 20;
1731
} else {
@@ -1734,17 +1733,8 @@ off_t find_pack_entry_one(const unsigned char *sha1,
1733
index += 4;
1734
}
1735
1737
- while (lo < hi) {
1738
- unsigned mi = lo + (hi - lo) / 2;
1739
- int cmp = hashcmp(index + mi * stride, sha1);
1740
-
1741
- if (!cmp)
1742
- return nth_packed_object_offset(p, mi);
1743
- if (cmp > 0)
1744
- hi = mi;
1745
- else
1746
- lo = mi+1;
1747
- }
1736
+ if (bsearch_hash(sha1, level1_ofs, index, stride, &result))
1737
+ return nth_packed_object_offset(p, result);
1738
return 0;
1739
}
1740
sha1-lookup.c
+28
@@ -99,3 +99,31 @@ int sha1_pos(const unsigned char *sha1, void *table, size_t nr,
99
} while (lo < hi);
100
return -lo-1;
101
}
102
+
103
+int bsearch_hash(const unsigned char *sha1, const uint32_t *fanout_nbo,
104
+ const unsigned char *table, size_t stride, uint32_t *result)
105
+{
106
+ uint32_t hi, lo;
107
+
108
+ hi = ntohl(fanout_nbo[*sha1]);
109
+ lo = ((*sha1 == 0x0) ? 0 : ntohl(fanout_nbo[*sha1 - 1]));
110
+
111
+ while (lo < hi) {
112
+ unsigned mi = lo + (hi - lo) / 2;
113
+ int cmp = hashcmp(table + mi * stride, sha1);
114
+
115
+ if (!cmp) {
116
+ if (result)
117
+ *result = mi;
118
+ return 1;
119
+ }
120
+ if (cmp > 0)
121
+ hi = mi;
122
+ else
123
+ lo = mi + 1;
124
+ }
125
+
126
+ if (result)
127
+ *result = lo;
128
+ return 0;
129
+}
sha1-lookup.h
+22
@@ -7,4 +7,26 @@ extern int sha1_pos(const unsigned char *sha1,
7
void *table,
8
size_t nr,
9
sha1_access_fn fn);
10
+
11
+/*
12
+ * Searches for sha1 in table, using the given fanout table to determine the
13
+ * interval to search, then using binary search. Returns 1 if found, 0 if not.
14
+ *
15
+ * Takes the following parameters:
16
+ *
17
+ * - sha1: the hash to search for
18
+ * - fanout_nbo: a 256-element array of NETWORK-order 32-bit integers; the
19
+ * integer at position i represents the number of elements in table whose
20
+ * first byte is less than or equal to i
21
+ * - table: a sorted list of hashes with optional extra information in between
22
+ * - stride: distance between two consecutive elements in table (should be
23
+ * GIT_MAX_RAWSZ or greater)
24
+ * - result: if not NULL, this function stores the element index of the
25
+ * position found (if the search is successful) or the index of the least
26
+ * element that is greater than sha1 (if the search is not successful)
27
+ *
28
+ * This function does not verify the validity of the fanout table.
29
+ */
30
+int bsearch_hash(const unsigned char *sha1, const uint32_t *fanout_nbo,
31
+ const unsigned char *table, size_t stride, uint32_t *result);
32
#endif