You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ReftableWriter.java 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  1. /*
  2. * Copyright (C) 2017, 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. package org.eclipse.jgit.internal.storage.reftable;
  44. import static java.lang.Math.log;
  45. import static java.nio.charset.StandardCharsets.UTF_8;
  46. import static org.eclipse.jgit.internal.storage.reftable.BlockWriter.padBetweenBlocks;
  47. import static org.eclipse.jgit.internal.storage.reftable.ReftableConstants.FILE_FOOTER_LEN;
  48. import static org.eclipse.jgit.internal.storage.reftable.ReftableConstants.FILE_HEADER_LEN;
  49. import static org.eclipse.jgit.internal.storage.reftable.ReftableConstants.FILE_HEADER_MAGIC;
  50. import static org.eclipse.jgit.internal.storage.reftable.ReftableConstants.INDEX_BLOCK_TYPE;
  51. import static org.eclipse.jgit.internal.storage.reftable.ReftableConstants.LOG_BLOCK_TYPE;
  52. import static org.eclipse.jgit.internal.storage.reftable.ReftableConstants.MAX_BLOCK_SIZE;
  53. import static org.eclipse.jgit.internal.storage.reftable.ReftableConstants.MAX_RESTARTS;
  54. import static org.eclipse.jgit.internal.storage.reftable.ReftableConstants.OBJ_BLOCK_TYPE;
  55. import static org.eclipse.jgit.internal.storage.reftable.ReftableConstants.REF_BLOCK_TYPE;
  56. import static org.eclipse.jgit.internal.storage.reftable.ReftableConstants.VERSION_1;
  57. import static org.eclipse.jgit.lib.Constants.OBJECT_ID_LENGTH;
  58. import java.io.IOException;
  59. import java.io.OutputStream;
  60. import java.text.MessageFormat;
  61. import java.util.ArrayList;
  62. import java.util.Collection;
  63. import java.util.Collections;
  64. import java.util.HashSet;
  65. import java.util.Iterator;
  66. import java.util.List;
  67. import java.util.Set;
  68. import java.util.zip.CRC32;
  69. import org.eclipse.jgit.annotations.Nullable;
  70. import org.eclipse.jgit.internal.JGitText;
  71. import org.eclipse.jgit.internal.storage.reftable.BlockWriter.DeleteLogEntry;
  72. import org.eclipse.jgit.internal.storage.reftable.BlockWriter.Entry;
  73. import org.eclipse.jgit.internal.storage.reftable.BlockWriter.IndexEntry;
  74. import org.eclipse.jgit.internal.storage.reftable.BlockWriter.LogEntry;
  75. import org.eclipse.jgit.internal.storage.reftable.BlockWriter.ObjEntry;
  76. import org.eclipse.jgit.internal.storage.reftable.BlockWriter.RefEntry;
  77. import org.eclipse.jgit.lib.AbbreviatedObjectId;
  78. import org.eclipse.jgit.lib.AnyObjectId;
  79. import org.eclipse.jgit.lib.ObjectId;
  80. import org.eclipse.jgit.lib.ObjectIdOwnerMap;
  81. import org.eclipse.jgit.lib.ObjectIdSubclassMap;
  82. import org.eclipse.jgit.lib.PersonIdent;
  83. import org.eclipse.jgit.lib.Ref;
  84. import org.eclipse.jgit.util.LongList;
  85. import org.eclipse.jgit.util.NB;
  86. /**
  87. * Writes a reftable formatted file.
  88. * <p>
  89. * A reftable can be written in a streaming fashion, provided the caller sorts
  90. * all references. A
  91. * {@link org.eclipse.jgit.internal.storage.reftable.ReftableWriter} is
  92. * single-use, and not thread-safe.
  93. */
  94. public class ReftableWriter {
  95. private ReftableConfig config;
  96. private int refBlockSize;
  97. private int logBlockSize;
  98. private int restartInterval;
  99. private int maxIndexLevels;
  100. private boolean alignBlocks;
  101. private boolean indexObjects;
  102. private long minUpdateIndex;
  103. private long maxUpdateIndex;
  104. private ReftableOutputStream out;
  105. private ObjectIdSubclassMap<RefList> obj2ref;
  106. private BlockWriter.Entry lastRef;
  107. private BlockWriter.Entry lastLog;
  108. private BlockWriter cur;
  109. private Section refs;
  110. private Section objs;
  111. private Section logs;
  112. private int objIdLen;
  113. private Stats stats;
  114. /**
  115. * Initialize a writer with a default configuration.
  116. */
  117. public ReftableWriter() {
  118. this(new ReftableConfig());
  119. lastRef = null;
  120. lastLog = null;
  121. }
  122. /**
  123. * Initialize a writer with a specific configuration.
  124. *
  125. * @param cfg
  126. * configuration for the writer.
  127. */
  128. public ReftableWriter(ReftableConfig cfg) {
  129. config = cfg;
  130. }
  131. /**
  132. * Set configuration for the writer.
  133. *
  134. * @param cfg
  135. * configuration for the writer.
  136. * @return {@code this}
  137. */
  138. public ReftableWriter setConfig(ReftableConfig cfg) {
  139. this.config = cfg != null ? cfg : new ReftableConfig();
  140. return this;
  141. }
  142. /**
  143. * Set the minimum update index for log entries that appear in this
  144. * reftable.
  145. *
  146. * @param min
  147. * the minimum update index for log entries that appear in this
  148. * reftable. This should be 1 higher than the prior reftable's
  149. * {@code maxUpdateIndex} if this table will be used in a stack.
  150. * @return {@code this}
  151. */
  152. public ReftableWriter setMinUpdateIndex(long min) {
  153. minUpdateIndex = min;
  154. return this;
  155. }
  156. /**
  157. * Set the maximum update index for log entries that appear in this
  158. * reftable.
  159. *
  160. * @param max
  161. * the maximum update index for log entries that appear in this
  162. * reftable. This should be at least 1 higher than the prior
  163. * reftable's {@code maxUpdateIndex} if this table will be used
  164. * in a stack.
  165. * @return {@code this}
  166. */
  167. public ReftableWriter setMaxUpdateIndex(long max) {
  168. maxUpdateIndex = max;
  169. return this;
  170. }
  171. /**
  172. * Begin writing the reftable.
  173. *
  174. * @param os
  175. * stream to write the table to. Caller is responsible for
  176. * closing the stream after invoking {@link #finish()}.
  177. * @return {@code this}
  178. * @throws java.io.IOException
  179. * if reftable header cannot be written.
  180. */
  181. public ReftableWriter begin(OutputStream os) throws IOException {
  182. refBlockSize = config.getRefBlockSize();
  183. logBlockSize = config.getLogBlockSize();
  184. restartInterval = config.getRestartInterval();
  185. maxIndexLevels = config.getMaxIndexLevels();
  186. alignBlocks = config.isAlignBlocks();
  187. indexObjects = config.isIndexObjects();
  188. if (refBlockSize <= 0) {
  189. refBlockSize = 4 << 10;
  190. } else if (refBlockSize > MAX_BLOCK_SIZE) {
  191. throw new IllegalArgumentException();
  192. }
  193. if (logBlockSize <= 0) {
  194. logBlockSize = 2 * refBlockSize;
  195. }
  196. if (restartInterval <= 0) {
  197. restartInterval = refBlockSize < (60 << 10) ? 16 : 64;
  198. }
  199. out = new ReftableOutputStream(os, refBlockSize, alignBlocks);
  200. refs = new Section(REF_BLOCK_TYPE);
  201. if (indexObjects) {
  202. obj2ref = new ObjectIdSubclassMap<>();
  203. }
  204. writeFileHeader();
  205. return this;
  206. }
  207. /**
  208. * Sort a collection of references and write them to the reftable.
  209. *
  210. * @param refsToPack
  211. * references to sort and write.
  212. * @return {@code this}
  213. * @throws java.io.IOException
  214. * if reftable cannot be written.
  215. */
  216. public ReftableWriter sortAndWriteRefs(Collection<Ref> refsToPack)
  217. throws IOException {
  218. Iterator<RefEntry> itr = refsToPack.stream()
  219. .map(r -> new RefEntry(r, maxUpdateIndex - minUpdateIndex))
  220. .sorted(Entry::compare)
  221. .iterator();
  222. while (itr.hasNext()) {
  223. RefEntry entry = itr.next();
  224. long blockPos = refs.write(entry);
  225. indexRef(entry.ref, blockPos);
  226. }
  227. return this;
  228. }
  229. /**
  230. * Write one reference to the reftable.
  231. * <p>
  232. * References must be passed in sorted order.
  233. *
  234. * @param ref
  235. * the reference to store.
  236. * @throws java.io.IOException
  237. * if reftable cannot be written.
  238. */
  239. public void writeRef(Ref ref) throws IOException {
  240. writeRef(ref, maxUpdateIndex);
  241. }
  242. /**
  243. * Write one reference to the reftable.
  244. * <p>
  245. * References must be passed in sorted order.
  246. *
  247. * @param ref
  248. * the reference to store.
  249. * @param updateIndex
  250. * the updateIndex that modified this reference. Must be
  251. * {@code >= minUpdateIndex} for this file.
  252. * @throws java.io.IOException
  253. * if reftable cannot be written.
  254. */
  255. public void writeRef(Ref ref, long updateIndex) throws IOException {
  256. if (updateIndex < minUpdateIndex) {
  257. throw new IllegalArgumentException();
  258. }
  259. long d = updateIndex - minUpdateIndex;
  260. RefEntry entry = new RefEntry(ref, d);
  261. if (lastRef != null && Entry.compare(lastRef, entry) >= 0) {
  262. throwIllegalEntry(lastRef, entry);
  263. }
  264. lastRef = entry;
  265. long blockPos = refs.write(entry);
  266. indexRef(ref, blockPos);
  267. }
  268. private void throwIllegalEntry(Entry last, Entry now) {
  269. throw new IllegalArgumentException(MessageFormat.format(
  270. JGitText.get().refTableRecordsMustIncrease,
  271. new String(last.key, UTF_8), new String(now.key, UTF_8)));
  272. }
  273. private void indexRef(Ref ref, long blockPos) {
  274. if (indexObjects && !ref.isSymbolic()) {
  275. indexId(ref.getObjectId(), blockPos);
  276. indexId(ref.getPeeledObjectId(), blockPos);
  277. }
  278. }
  279. private void indexId(ObjectId id, long blockPos) {
  280. if (id != null) {
  281. RefList l = obj2ref.get(id);
  282. if (l == null) {
  283. l = new RefList(id);
  284. obj2ref.add(l);
  285. }
  286. l.addBlock(blockPos);
  287. }
  288. }
  289. /**
  290. * Write one reflog entry to the reftable.
  291. * <p>
  292. * Reflog entries must be written in reference name and descending
  293. * {@code updateIndex} (highest first) order.
  294. *
  295. * @param ref
  296. * name of the reference.
  297. * @param updateIndex
  298. * identifier of the transaction that created the log record. The
  299. * {@code updateIndex} must be unique within the scope of
  300. * {@code ref}, and must be within the bounds defined by
  301. * {@code minUpdateIndex <= updateIndex <= maxUpdateIndex}.
  302. * @param who
  303. * committer of the reflog entry.
  304. * @param oldId
  305. * prior id; pass {@link org.eclipse.jgit.lib.ObjectId#zeroId()}
  306. * for creations.
  307. * @param newId
  308. * new id; pass {@link org.eclipse.jgit.lib.ObjectId#zeroId()}
  309. * for deletions.
  310. * @param message
  311. * optional message (may be null).
  312. * @throws java.io.IOException
  313. * if reftable cannot be written.
  314. */
  315. public void writeLog(String ref, long updateIndex, PersonIdent who,
  316. ObjectId oldId, ObjectId newId, @Nullable String message)
  317. throws IOException {
  318. String msg = message != null ? message : ""; //$NON-NLS-1$
  319. beginLog();
  320. LogEntry entry = new LogEntry(ref, updateIndex, who, oldId, newId, msg);
  321. if (lastLog != null && Entry.compare(lastLog, entry) >= 0) {
  322. throwIllegalEntry(lastLog, entry);
  323. }
  324. lastLog = entry;
  325. logs.write(entry);
  326. }
  327. /**
  328. * Record deletion of one reflog entry in this reftable.
  329. *
  330. * <p>
  331. * The deletion can shadow an entry stored in a lower table in the stack.
  332. * This is useful for {@code refs/stash} and dropping an entry from its
  333. * reflog.
  334. * <p>
  335. * Deletion must be properly interleaved in sorted updateIndex order with
  336. * any other logs written by
  337. * {@link #writeLog(String, long, PersonIdent, ObjectId, ObjectId, String)}.
  338. *
  339. * @param ref
  340. * the ref to delete (hide) a reflog entry from.
  341. * @param updateIndex
  342. * the update index that must be hidden.
  343. * @throws java.io.IOException
  344. * if reftable cannot be written.
  345. */
  346. public void deleteLog(String ref, long updateIndex) throws IOException {
  347. beginLog();
  348. logs.write(new DeleteLogEntry(ref, updateIndex));
  349. }
  350. private void beginLog() throws IOException {
  351. if (logs == null) {
  352. finishRefAndObjSections(); // close prior ref blocks and their index, if present.
  353. out.flushFileHeader();
  354. out.setBlockSize(logBlockSize);
  355. logs = new Section(LOG_BLOCK_TYPE);
  356. }
  357. }
  358. /**
  359. * Get an estimate of the current size in bytes of the reftable
  360. *
  361. * @return an estimate of the current size in bytes of the reftable, if it
  362. * was finished right now. Estimate is only accurate if
  363. * {@link org.eclipse.jgit.internal.storage.reftable.ReftableConfig#setIndexObjects(boolean)}
  364. * is {@code false} and
  365. * {@link org.eclipse.jgit.internal.storage.reftable.ReftableConfig#setMaxIndexLevels(int)}
  366. * is {@code 1}.
  367. */
  368. public long estimateTotalBytes() {
  369. long bytes = out.size();
  370. if (bytes == 0) {
  371. bytes += FILE_HEADER_LEN;
  372. }
  373. if (cur != null) {
  374. long curBlockPos = out.size();
  375. int sz = cur.currentSize();
  376. bytes += sz;
  377. IndexBuilder idx = null;
  378. if (cur.blockType() == REF_BLOCK_TYPE) {
  379. idx = refs.idx;
  380. } else if (cur.blockType() == LOG_BLOCK_TYPE) {
  381. idx = logs.idx;
  382. }
  383. if (idx != null && shouldHaveIndex(idx)) {
  384. if (idx == refs.idx) {
  385. bytes += out.estimatePadBetweenBlocks(sz);
  386. }
  387. bytes += idx.estimateBytes(curBlockPos);
  388. }
  389. }
  390. bytes += FILE_FOOTER_LEN;
  391. return bytes;
  392. }
  393. /**
  394. * Finish writing the reftable by writing its trailer.
  395. *
  396. * @return {@code this}
  397. * @throws java.io.IOException
  398. * if reftable cannot be written.
  399. */
  400. public ReftableWriter finish() throws IOException {
  401. finishRefAndObjSections();
  402. finishLogSection();
  403. writeFileFooter();
  404. out.finishFile();
  405. stats = new Stats(this, out);
  406. out = null;
  407. obj2ref = null;
  408. cur = null;
  409. refs = null;
  410. objs = null;
  411. logs = null;
  412. return this;
  413. }
  414. private void finishRefAndObjSections() throws IOException {
  415. if (cur != null && cur.blockType() == REF_BLOCK_TYPE) {
  416. refs.finishSectionMaybeWriteIndex();
  417. if (indexObjects && !obj2ref.isEmpty() && refs.idx.bytes > 0) {
  418. writeObjBlocks();
  419. }
  420. obj2ref = null;
  421. }
  422. }
  423. private void writeObjBlocks() throws IOException {
  424. List<RefList> sorted = sortById(obj2ref);
  425. obj2ref = null;
  426. objIdLen = shortestUniqueAbbreviation(sorted);
  427. out.padBetweenBlocksToNextBlock();
  428. objs = new Section(OBJ_BLOCK_TYPE);
  429. objs.entryCnt = sorted.size();
  430. for (RefList l : sorted) {
  431. objs.write(new ObjEntry(objIdLen, l, l.blockPos));
  432. }
  433. objs.finishSectionMaybeWriteIndex();
  434. }
  435. private void finishLogSection() throws IOException {
  436. if (cur != null && cur.blockType() == LOG_BLOCK_TYPE) {
  437. logs.finishSectionMaybeWriteIndex();
  438. }
  439. }
  440. private boolean shouldHaveIndex(IndexBuilder idx) {
  441. int threshold;
  442. if (idx == refs.idx && alignBlocks) {
  443. threshold = 4;
  444. } else {
  445. threshold = 1;
  446. }
  447. return idx.entries.size() + (cur != null ? 1 : 0) > threshold;
  448. }
  449. private void writeFileHeader() {
  450. byte[] hdr = new byte[FILE_HEADER_LEN];
  451. encodeHeader(hdr);
  452. out.write(hdr, 0, FILE_HEADER_LEN);
  453. }
  454. private void encodeHeader(byte[] hdr) {
  455. System.arraycopy(FILE_HEADER_MAGIC, 0, hdr, 0, 4);
  456. int bs = alignBlocks ? refBlockSize : 0;
  457. NB.encodeInt32(hdr, 4, (VERSION_1 << 24) | bs);
  458. NB.encodeInt64(hdr, 8, minUpdateIndex);
  459. NB.encodeInt64(hdr, 16, maxUpdateIndex);
  460. }
  461. private void writeFileFooter() {
  462. int ftrLen = FILE_FOOTER_LEN;
  463. byte[] ftr = new byte[ftrLen];
  464. encodeHeader(ftr);
  465. NB.encodeInt64(ftr, 24, indexPosition(refs));
  466. NB.encodeInt64(ftr, 32, (firstBlockPosition(objs) << 5) | objIdLen);
  467. NB.encodeInt64(ftr, 40, indexPosition(objs));
  468. NB.encodeInt64(ftr, 48, firstBlockPosition(logs));
  469. NB.encodeInt64(ftr, 56, indexPosition(logs));
  470. CRC32 crc = new CRC32();
  471. crc.update(ftr, 0, ftrLen - 4);
  472. NB.encodeInt32(ftr, ftrLen - 4, (int) crc.getValue());
  473. out.write(ftr, 0, ftrLen);
  474. }
  475. private static long firstBlockPosition(@Nullable Section s) {
  476. return s != null ? s.firstBlockPosition : 0;
  477. }
  478. private static long indexPosition(@Nullable Section s) {
  479. return s != null && s.idx != null ? s.idx.rootPosition : 0;
  480. }
  481. /**
  482. * Get statistics of the last written reftable.
  483. *
  484. * @return statistics of the last written reftable.
  485. */
  486. public Stats getStats() {
  487. return stats;
  488. }
  489. /** Statistics about a written reftable. */
  490. public static class Stats {
  491. private final int refBlockSize;
  492. private final int logBlockSize;
  493. private final int restartInterval;
  494. private final long minUpdateIndex;
  495. private final long maxUpdateIndex;
  496. private final long refCnt;
  497. private final long objCnt;
  498. private final int objIdLen;
  499. private final long logCnt;
  500. private final long refBytes;
  501. private final long objBytes;
  502. private final long logBytes;
  503. private final long paddingUsed;
  504. private final long totalBytes;
  505. private final int refIndexSize;
  506. private final int refIndexLevels;
  507. private final int objIndexSize;
  508. private final int objIndexLevels;
  509. Stats(ReftableWriter w, ReftableOutputStream o) {
  510. refBlockSize = w.refBlockSize;
  511. logBlockSize = w.logBlockSize;
  512. restartInterval = w.restartInterval;
  513. minUpdateIndex = w.minUpdateIndex;
  514. maxUpdateIndex = w.maxUpdateIndex;
  515. paddingUsed = o.paddingUsed();
  516. totalBytes = o.size();
  517. refCnt = w.refs.entryCnt;
  518. refBytes = w.refs.bytes;
  519. objCnt = w.objs != null ? w.objs.entryCnt : 0;
  520. objBytes = w.objs != null ? w.objs.bytes : 0;
  521. objIdLen = w.objIdLen;
  522. logCnt = w.logs != null ? w.logs.entryCnt : 0;
  523. logBytes = w.logs != null ? w.logs.bytes : 0;
  524. IndexBuilder refIdx = w.refs.idx;
  525. refIndexSize = refIdx.bytes;
  526. refIndexLevels = refIdx.levels;
  527. IndexBuilder objIdx = w.objs != null ? w.objs.idx : null;
  528. objIndexSize = objIdx != null ? objIdx.bytes : 0;
  529. objIndexLevels = objIdx != null ? objIdx.levels : 0;
  530. }
  531. /** @return number of bytes in a ref block. */
  532. public int refBlockSize() {
  533. return refBlockSize;
  534. }
  535. /** @return number of bytes to compress into a log block. */
  536. public int logBlockSize() {
  537. return logBlockSize;
  538. }
  539. /** @return number of references between binary search markers. */
  540. public int restartInterval() {
  541. return restartInterval;
  542. }
  543. /** @return smallest update index contained in this reftable. */
  544. public long minUpdateIndex() {
  545. return minUpdateIndex;
  546. }
  547. /** @return largest update index contained in this reftable. */
  548. public long maxUpdateIndex() {
  549. return maxUpdateIndex;
  550. }
  551. /** @return total number of references in the reftable. */
  552. public long refCount() {
  553. return refCnt;
  554. }
  555. /** @return number of unique objects in the reftable. */
  556. public long objCount() {
  557. return objCnt;
  558. }
  559. /** @return total number of log records in the reftable. */
  560. public long logCount() {
  561. return logCnt;
  562. }
  563. /** @return number of bytes for references, including ref index. */
  564. public long refBytes() {
  565. return refBytes;
  566. }
  567. /** @return number of bytes for objects, including object index. */
  568. public long objBytes() {
  569. return objBytes;
  570. }
  571. /** @return number of bytes for log, including log index. */
  572. public long logBytes() {
  573. return logBytes;
  574. }
  575. /** @return total number of bytes in the reftable. */
  576. public long totalBytes() {
  577. return totalBytes;
  578. }
  579. /** @return bytes of padding used to maintain block alignment. */
  580. public long paddingBytes() {
  581. return paddingUsed;
  582. }
  583. /** @return number of bytes in the ref index; 0 if no index was used. */
  584. public int refIndexSize() {
  585. return refIndexSize;
  586. }
  587. /** @return number of levels in the ref index. */
  588. public int refIndexLevels() {
  589. return refIndexLevels;
  590. }
  591. /** @return number of bytes in the object index; 0 if no index. */
  592. public int objIndexSize() {
  593. return objIndexSize;
  594. }
  595. /** @return number of levels in the object index. */
  596. public int objIndexLevels() {
  597. return objIndexLevels;
  598. }
  599. /**
  600. * @return number of bytes required to uniquely identify all objects in
  601. * the reftable. Unique abbreviations in hex would be
  602. * {@code 2 * objIdLength()}.
  603. */
  604. public int objIdLength() {
  605. return objIdLen;
  606. }
  607. }
  608. private static List<RefList> sortById(ObjectIdSubclassMap<RefList> m) {
  609. List<RefList> s = new ArrayList<>(m.size());
  610. for (RefList l : m) {
  611. s.add(l);
  612. }
  613. Collections.sort(s);
  614. return s;
  615. }
  616. private static int shortestUniqueAbbreviation(List<RefList> in) {
  617. // Estimate minimum number of bytes necessary for unique abbreviations.
  618. int bytes = Math.max(2, (int) (log(in.size()) / log(8)));
  619. Set<AbbreviatedObjectId> tmp = new HashSet<>((int) (in.size() * 0.75f));
  620. retry: for (;;) {
  621. int hexLen = bytes * 2;
  622. for (ObjectId id : in) {
  623. AbbreviatedObjectId a = id.abbreviate(hexLen);
  624. if (!tmp.add(a)) {
  625. if (++bytes >= OBJECT_ID_LENGTH) {
  626. return OBJECT_ID_LENGTH;
  627. }
  628. tmp.clear();
  629. continue retry;
  630. }
  631. }
  632. return bytes;
  633. }
  634. }
  635. private static class RefList extends ObjectIdOwnerMap.Entry {
  636. final LongList blockPos = new LongList(2);
  637. RefList(AnyObjectId id) {
  638. super(id);
  639. }
  640. void addBlock(long pos) {
  641. if (!blockPos.contains(pos)) {
  642. blockPos.add(pos);
  643. }
  644. }
  645. }
  646. private class Section {
  647. final IndexBuilder idx;
  648. final long firstBlockPosition;
  649. long entryCnt;
  650. long bytes;
  651. Section(byte keyType) {
  652. idx = new IndexBuilder(keyType);
  653. firstBlockPosition = out.size();
  654. }
  655. long write(BlockWriter.Entry entry) throws IOException {
  656. if (cur == null) {
  657. beginBlock(entry);
  658. } else if (!cur.tryAdd(entry)) {
  659. flushCurBlock();
  660. if (cur.padBetweenBlocks()) {
  661. out.padBetweenBlocksToNextBlock();
  662. }
  663. beginBlock(entry);
  664. }
  665. entryCnt++;
  666. return out.size();
  667. }
  668. private void beginBlock(BlockWriter.Entry entry)
  669. throws BlockSizeTooSmallException {
  670. byte blockType = entry.blockType();
  671. int bs = out.bytesAvailableInBlock();
  672. cur = new BlockWriter(blockType, idx.keyType, bs, restartInterval);
  673. cur.mustAdd(entry);
  674. }
  675. void flushCurBlock() throws IOException {
  676. idx.entries.add(new IndexEntry(cur.lastKey(), out.size()));
  677. cur.writeTo(out);
  678. }
  679. void finishSectionMaybeWriteIndex() throws IOException {
  680. flushCurBlock();
  681. cur = null;
  682. if (shouldHaveIndex(idx)) {
  683. idx.writeIndex();
  684. }
  685. bytes = out.size() - firstBlockPosition;
  686. }
  687. }
  688. private class IndexBuilder {
  689. final byte keyType;
  690. List<IndexEntry> entries = new ArrayList<>();
  691. long rootPosition;
  692. int bytes;
  693. int levels;
  694. IndexBuilder(byte kt) {
  695. keyType = kt;
  696. }
  697. int estimateBytes(long curBlockPos) {
  698. BlockWriter b = new BlockWriter(
  699. INDEX_BLOCK_TYPE, keyType,
  700. MAX_BLOCK_SIZE,
  701. Math.max(restartInterval, entries.size() / MAX_RESTARTS));
  702. try {
  703. for (Entry e : entries) {
  704. b.mustAdd(e);
  705. }
  706. if (cur != null) {
  707. b.mustAdd(new IndexEntry(cur.lastKey(), curBlockPos));
  708. }
  709. } catch (BlockSizeTooSmallException e) {
  710. return b.currentSize();
  711. }
  712. return b.currentSize();
  713. }
  714. void writeIndex() throws IOException {
  715. if (padBetweenBlocks(keyType)) {
  716. out.padBetweenBlocksToNextBlock();
  717. }
  718. long startPos = out.size();
  719. writeMultiLevelIndex(entries);
  720. bytes = (int) (out.size() - startPos);
  721. entries = null;
  722. }
  723. private void writeMultiLevelIndex(List<IndexEntry> keys)
  724. throws IOException {
  725. levels = 1;
  726. while (maxIndexLevels == 0 || levels < maxIndexLevels) {
  727. keys = writeOneLevel(keys);
  728. if (keys == null) {
  729. return;
  730. }
  731. levels++;
  732. }
  733. // When maxIndexLevels has restricted the writer, write one
  734. // index block with the entire remaining set of keys.
  735. BlockWriter b = new BlockWriter(
  736. INDEX_BLOCK_TYPE, keyType,
  737. MAX_BLOCK_SIZE,
  738. Math.max(restartInterval, keys.size() / MAX_RESTARTS));
  739. for (Entry e : keys) {
  740. b.mustAdd(e);
  741. }
  742. rootPosition = out.size();
  743. b.writeTo(out);
  744. }
  745. private List<IndexEntry> writeOneLevel(List<IndexEntry> keys)
  746. throws IOException {
  747. Section thisLevel = new Section(keyType);
  748. for (Entry e : keys) {
  749. thisLevel.write(e);
  750. }
  751. if (!thisLevel.idx.entries.isEmpty()) {
  752. thisLevel.flushCurBlock();
  753. if (cur.padBetweenBlocks()) {
  754. out.padBetweenBlocksToNextBlock();
  755. }
  756. cur = null;
  757. return thisLevel.idx.entries;
  758. }
  759. // The current block fit entire level; make it the root.
  760. rootPosition = out.size();
  761. cur.writeTo(out);
  762. cur = null;
  763. return null;
  764. }
  765. }
  766. }