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.

BlockWriter.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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.nio.charset.StandardCharsets.UTF_8;
  45. import static org.eclipse.jgit.internal.storage.reftable.ReftableConstants.FILE_HEADER_LEN;
  46. import static org.eclipse.jgit.internal.storage.reftable.ReftableConstants.INDEX_BLOCK_TYPE;
  47. import static org.eclipse.jgit.internal.storage.reftable.ReftableConstants.LOG_BLOCK_TYPE;
  48. import static org.eclipse.jgit.internal.storage.reftable.ReftableConstants.LOG_DATA;
  49. import static org.eclipse.jgit.internal.storage.reftable.ReftableConstants.LOG_NONE;
  50. import static org.eclipse.jgit.internal.storage.reftable.ReftableConstants.MAX_RESTARTS;
  51. import static org.eclipse.jgit.internal.storage.reftable.ReftableConstants.OBJ_BLOCK_TYPE;
  52. import static org.eclipse.jgit.internal.storage.reftable.ReftableConstants.REF_BLOCK_TYPE;
  53. import static org.eclipse.jgit.internal.storage.reftable.ReftableConstants.VALUE_1ID;
  54. import static org.eclipse.jgit.internal.storage.reftable.ReftableConstants.VALUE_2ID;
  55. import static org.eclipse.jgit.internal.storage.reftable.ReftableConstants.VALUE_NONE;
  56. import static org.eclipse.jgit.internal.storage.reftable.ReftableConstants.VALUE_SYMREF;
  57. import static org.eclipse.jgit.internal.storage.reftable.ReftableConstants.VALUE_TYPE_MASK;
  58. import static org.eclipse.jgit.internal.storage.reftable.ReftableConstants.reverseUpdateIndex;
  59. import static org.eclipse.jgit.internal.storage.reftable.ReftableOutputStream.computeVarintSize;
  60. import static org.eclipse.jgit.lib.Constants.OBJECT_ID_LENGTH;
  61. import static org.eclipse.jgit.lib.Ref.Storage.NEW;
  62. import java.io.IOException;
  63. import java.util.ArrayList;
  64. import java.util.Arrays;
  65. import java.util.List;
  66. import org.eclipse.jgit.internal.JGitText;
  67. import org.eclipse.jgit.lib.ObjectId;
  68. import org.eclipse.jgit.lib.PersonIdent;
  69. import org.eclipse.jgit.lib.Ref;
  70. import org.eclipse.jgit.util.IntList;
  71. import org.eclipse.jgit.util.LongList;
  72. import org.eclipse.jgit.util.NB;
  73. /** Formats and writes blocks for {@link ReftableWriter}. */
  74. class BlockWriter {
  75. private final byte blockType;
  76. private final byte keyType;
  77. private final List<Entry> entries;
  78. private final int blockLimitBytes;
  79. private final int restartInterval;
  80. private int entriesSumBytes;
  81. private int restartCnt;
  82. BlockWriter(byte type, byte kt, int bs, int ri) {
  83. blockType = type;
  84. keyType = kt;
  85. blockLimitBytes = bs;
  86. restartInterval = ri;
  87. entries = new ArrayList<>(estimateEntryCount(type, kt, bs));
  88. }
  89. private static int estimateEntryCount(byte blockType, byte keyType,
  90. int blockLimitBytes) {
  91. double avgBytesPerEntry;
  92. switch (blockType) {
  93. case REF_BLOCK_TYPE:
  94. default:
  95. avgBytesPerEntry = 35.31;
  96. break;
  97. case OBJ_BLOCK_TYPE:
  98. avgBytesPerEntry = 4.19;
  99. break;
  100. case LOG_BLOCK_TYPE:
  101. avgBytesPerEntry = 101.14;
  102. break;
  103. case INDEX_BLOCK_TYPE:
  104. switch (keyType) {
  105. case REF_BLOCK_TYPE:
  106. case LOG_BLOCK_TYPE:
  107. default:
  108. avgBytesPerEntry = 27.44;
  109. break;
  110. case OBJ_BLOCK_TYPE:
  111. avgBytesPerEntry = 11.57;
  112. break;
  113. }
  114. }
  115. int cnt = (int) (Math.ceil(blockLimitBytes / avgBytesPerEntry));
  116. return Math.min(cnt, 4096);
  117. }
  118. byte blockType() {
  119. return blockType;
  120. }
  121. boolean padBetweenBlocks() {
  122. return padBetweenBlocks(blockType)
  123. || (blockType == INDEX_BLOCK_TYPE && padBetweenBlocks(keyType));
  124. }
  125. static boolean padBetweenBlocks(byte type) {
  126. return type == REF_BLOCK_TYPE || type == OBJ_BLOCK_TYPE;
  127. }
  128. byte[] lastKey() {
  129. return entries.get(entries.size() - 1).key;
  130. }
  131. int currentSize() {
  132. return computeBlockBytes(0, false);
  133. }
  134. void mustAdd(Entry entry) throws BlockSizeTooSmallException {
  135. if (!tryAdd(entry, true)) {
  136. // Insanely long names need a larger block size.
  137. throw blockSizeTooSmall(entry);
  138. }
  139. }
  140. boolean tryAdd(Entry entry) {
  141. if (entry instanceof ObjEntry
  142. && computeBlockBytes(entry.sizeBytes(), 1) > blockLimitBytes) {
  143. // If the ObjEntry has so many ref block pointers that its
  144. // encoding overflows any block, reconfigure it to tell readers to
  145. // instead scan all refs for this ObjectId. That significantly
  146. // shrinks the entry to a very small size, which may now fit into
  147. // this block.
  148. ((ObjEntry) entry).markScanRequired();
  149. }
  150. if (tryAdd(entry, true)) {
  151. return true;
  152. } else if (nextShouldBeRestart()) {
  153. // It was time for another restart, but the entry doesn't fit
  154. // with its complete key, as the block is nearly full. Try to
  155. // force it to fit with prefix compression rather than waste
  156. // the tail of the block with padding.
  157. return tryAdd(entry, false);
  158. }
  159. return false;
  160. }
  161. private boolean tryAdd(Entry entry, boolean tryRestart) {
  162. byte[] key = entry.key;
  163. int prefixLen = 0;
  164. boolean restart = tryRestart && nextShouldBeRestart();
  165. if (!restart) {
  166. Entry priorEntry = entries.get(entries.size() - 1);
  167. byte[] prior = priorEntry.key;
  168. prefixLen = commonPrefix(prior, prior.length, key);
  169. if (prefixLen <= 5 /* "refs/" */ && keyType == REF_BLOCK_TYPE) {
  170. // Force restart points at transitions between namespaces
  171. // such as "refs/heads/" to "refs/tags/".
  172. restart = true;
  173. prefixLen = 0;
  174. } else if (prefixLen == 0) {
  175. restart = true;
  176. }
  177. }
  178. entry.restart = restart;
  179. entry.prefixLen = prefixLen;
  180. int entryBytes = entry.sizeBytes();
  181. if (computeBlockBytes(entryBytes, restart) > blockLimitBytes) {
  182. return false;
  183. }
  184. entriesSumBytes += entryBytes;
  185. entries.add(entry);
  186. if (restart) {
  187. restartCnt++;
  188. }
  189. return true;
  190. }
  191. private boolean nextShouldBeRestart() {
  192. int cnt = entries.size();
  193. return (cnt == 0 || ((cnt + 1) % restartInterval) == 0)
  194. && restartCnt < MAX_RESTARTS;
  195. }
  196. private int computeBlockBytes(int entryBytes, boolean restart) {
  197. return computeBlockBytes(
  198. entriesSumBytes + entryBytes,
  199. restartCnt + (restart ? 1 : 0));
  200. }
  201. private static int computeBlockBytes(int entryBytes, int restartCnt) {
  202. return 4 // 4-byte block header
  203. + entryBytes
  204. + restartCnt * 3 // restart_offset
  205. + 2; // 2-byte restart_count
  206. }
  207. void writeTo(ReftableOutputStream os) throws IOException {
  208. os.beginBlock(blockType);
  209. IntList restarts = new IntList(restartCnt);
  210. for (Entry entry : entries) {
  211. if (entry.restart) {
  212. restarts.add(os.bytesWrittenInBlock());
  213. }
  214. entry.writeKey(os);
  215. entry.writeValue(os);
  216. }
  217. if (restarts.size() == 0 || restarts.size() > MAX_RESTARTS) {
  218. throw new IllegalStateException();
  219. }
  220. for (int i = 0; i < restarts.size(); i++) {
  221. os.writeInt24(restarts.get(i));
  222. }
  223. os.writeInt16(restarts.size());
  224. os.flushBlock();
  225. }
  226. private BlockSizeTooSmallException blockSizeTooSmall(Entry entry) {
  227. // Compute size required to fit this entry by itself.
  228. int min = FILE_HEADER_LEN + computeBlockBytes(entry.sizeBytes(), 1);
  229. return new BlockSizeTooSmallException(min);
  230. }
  231. static int commonPrefix(byte[] a, int n, byte[] b) {
  232. int len = Math.min(n, Math.min(a.length, b.length));
  233. for (int i = 0; i < len; i++) {
  234. if (a[i] != b[i]) {
  235. return i;
  236. }
  237. }
  238. return len;
  239. }
  240. static int encodeSuffixAndType(int sfx, int valueType) {
  241. return (sfx << 3) | valueType;
  242. }
  243. static int compare(
  244. byte[] a, int ai, int aLen,
  245. byte[] b, int bi, int bLen) {
  246. int aEnd = ai + aLen;
  247. int bEnd = bi + bLen;
  248. while (ai < aEnd && bi < bEnd) {
  249. int c = (a[ai++] & 0xff) - (b[bi++] & 0xff);
  250. if (c != 0) {
  251. return c;
  252. }
  253. }
  254. return aLen - bLen;
  255. }
  256. static abstract class Entry {
  257. static int compare(Entry ea, Entry eb) {
  258. byte[] a = ea.key;
  259. byte[] b = eb.key;
  260. return BlockWriter.compare(a, 0, a.length, b, 0, b.length);
  261. }
  262. final byte[] key;
  263. int prefixLen;
  264. boolean restart;
  265. Entry(byte[] key) {
  266. this.key = key;
  267. }
  268. void writeKey(ReftableOutputStream os) {
  269. int sfxLen = key.length - prefixLen;
  270. os.writeVarint(prefixLen);
  271. os.writeVarint(encodeSuffixAndType(sfxLen, valueType()));
  272. os.write(key, prefixLen, sfxLen);
  273. }
  274. int sizeBytes() {
  275. int sfxLen = key.length - prefixLen;
  276. int sfx = encodeSuffixAndType(sfxLen, valueType());
  277. return computeVarintSize(prefixLen)
  278. + computeVarintSize(sfx)
  279. + sfxLen
  280. + valueSize();
  281. }
  282. abstract byte blockType();
  283. abstract int valueType();
  284. abstract int valueSize();
  285. abstract void writeValue(ReftableOutputStream os) throws IOException;
  286. }
  287. static class IndexEntry extends Entry {
  288. private final long blockPosition;
  289. IndexEntry(byte[] key, long blockPosition) {
  290. super(key);
  291. this.blockPosition = blockPosition;
  292. }
  293. @Override
  294. byte blockType() {
  295. return INDEX_BLOCK_TYPE;
  296. }
  297. @Override
  298. int valueType() {
  299. return 0;
  300. }
  301. @Override
  302. int valueSize() {
  303. return computeVarintSize(blockPosition);
  304. }
  305. @Override
  306. void writeValue(ReftableOutputStream os) {
  307. os.writeVarint(blockPosition);
  308. }
  309. }
  310. static class RefEntry extends Entry {
  311. final Ref ref;
  312. final long updateIndexDelta;
  313. RefEntry(Ref ref, long updateIndexDelta) {
  314. super(nameUtf8(ref));
  315. this.ref = ref;
  316. this.updateIndexDelta = updateIndexDelta;
  317. }
  318. @Override
  319. byte blockType() {
  320. return REF_BLOCK_TYPE;
  321. }
  322. @Override
  323. int valueType() {
  324. if (ref.isSymbolic()) {
  325. return VALUE_SYMREF;
  326. } else if (ref.getStorage() == NEW && ref.getObjectId() == null) {
  327. return VALUE_NONE;
  328. } else if (ref.getPeeledObjectId() != null) {
  329. return VALUE_2ID;
  330. } else {
  331. return VALUE_1ID;
  332. }
  333. }
  334. @Override
  335. int valueSize() {
  336. int n = computeVarintSize(updateIndexDelta);
  337. switch (valueType()) {
  338. case VALUE_NONE:
  339. return n;
  340. case VALUE_1ID:
  341. return n + OBJECT_ID_LENGTH;
  342. case VALUE_2ID:
  343. return n + 2 * OBJECT_ID_LENGTH;
  344. case VALUE_SYMREF:
  345. if (ref.isSymbolic()) {
  346. int nameLen = nameUtf8(ref.getTarget()).length;
  347. return n + computeVarintSize(nameLen) + nameLen;
  348. }
  349. }
  350. throw new IllegalStateException();
  351. }
  352. @Override
  353. void writeValue(ReftableOutputStream os) throws IOException {
  354. os.writeVarint(updateIndexDelta);
  355. switch (valueType()) {
  356. case VALUE_NONE:
  357. return;
  358. case VALUE_1ID: {
  359. ObjectId id1 = ref.getObjectId();
  360. if (!ref.isPeeled()) {
  361. throw new IOException(JGitText.get().peeledRefIsRequired);
  362. } else if (id1 == null) {
  363. throw new IOException(JGitText.get().invalidId0);
  364. }
  365. os.writeId(id1);
  366. return;
  367. }
  368. case VALUE_2ID: {
  369. ObjectId id1 = ref.getObjectId();
  370. ObjectId id2 = ref.getPeeledObjectId();
  371. if (!ref.isPeeled()) {
  372. throw new IOException(JGitText.get().peeledRefIsRequired);
  373. } else if (id1 == null || id2 == null) {
  374. throw new IOException(JGitText.get().invalidId0);
  375. }
  376. os.writeId(id1);
  377. os.writeId(id2);
  378. return;
  379. }
  380. case VALUE_SYMREF:
  381. if (ref.isSymbolic()) {
  382. os.writeVarintString(ref.getTarget().getName());
  383. return;
  384. }
  385. }
  386. throw new IllegalStateException();
  387. }
  388. private static byte[] nameUtf8(Ref ref) {
  389. return ref.getName().getBytes(UTF_8);
  390. }
  391. }
  392. static class ObjEntry extends Entry {
  393. final LongList blockPos;
  394. ObjEntry(int idLen, ObjectId id, LongList blockPos) {
  395. super(key(idLen, id));
  396. this.blockPos = blockPos;
  397. }
  398. private static byte[] key(int idLen, ObjectId id) {
  399. byte[] key = new byte[OBJECT_ID_LENGTH];
  400. id.copyRawTo(key, 0);
  401. if (idLen < OBJECT_ID_LENGTH) {
  402. return Arrays.copyOf(key, idLen);
  403. }
  404. return key;
  405. }
  406. void markScanRequired() {
  407. blockPos.clear();
  408. }
  409. @Override
  410. byte blockType() {
  411. return OBJ_BLOCK_TYPE;
  412. }
  413. @Override
  414. int valueType() {
  415. int cnt = blockPos.size();
  416. return cnt != 0 && cnt <= VALUE_TYPE_MASK ? cnt : 0;
  417. }
  418. @Override
  419. int valueSize() {
  420. int cnt = blockPos.size();
  421. if (cnt == 0) {
  422. return computeVarintSize(0);
  423. }
  424. int n = 0;
  425. if (cnt > VALUE_TYPE_MASK) {
  426. n += computeVarintSize(cnt);
  427. }
  428. n += computeVarintSize(blockPos.get(0));
  429. for (int j = 1; j < cnt; j++) {
  430. long prior = blockPos.get(j - 1);
  431. long b = blockPos.get(j);
  432. n += computeVarintSize(b - prior);
  433. }
  434. return n;
  435. }
  436. @Override
  437. void writeValue(ReftableOutputStream os) throws IOException {
  438. int cnt = blockPos.size();
  439. if (cnt == 0) {
  440. os.writeVarint(0);
  441. return;
  442. }
  443. if (cnt > VALUE_TYPE_MASK) {
  444. os.writeVarint(cnt);
  445. }
  446. os.writeVarint(blockPos.get(0));
  447. for (int j = 1; j < cnt; j++) {
  448. long prior = blockPos.get(j - 1);
  449. long b = blockPos.get(j);
  450. os.writeVarint(b - prior);
  451. }
  452. }
  453. }
  454. static class DeleteLogEntry extends Entry {
  455. DeleteLogEntry(String refName, long updateIndex) {
  456. super(LogEntry.key(refName, updateIndex));
  457. }
  458. @Override
  459. byte blockType() {
  460. return LOG_BLOCK_TYPE;
  461. }
  462. @Override
  463. int valueType() {
  464. return LOG_NONE;
  465. }
  466. @Override
  467. int valueSize() {
  468. return 0;
  469. }
  470. @Override
  471. void writeValue(ReftableOutputStream os) {
  472. // Nothing in a delete log record.
  473. }
  474. }
  475. static class LogEntry extends Entry {
  476. final ObjectId oldId;
  477. final ObjectId newId;
  478. final long timeSecs;
  479. final short tz;
  480. final byte[] name;
  481. final byte[] email;
  482. final byte[] msg;
  483. LogEntry(String refName, long updateIndex, PersonIdent who,
  484. ObjectId oldId, ObjectId newId, String message) {
  485. super(key(refName, updateIndex));
  486. this.oldId = oldId;
  487. this.newId = newId;
  488. this.timeSecs = who.getWhen().getTime() / 1000L;
  489. this.tz = (short) who.getTimeZoneOffset();
  490. this.name = who.getName().getBytes(UTF_8);
  491. this.email = who.getEmailAddress().getBytes(UTF_8);
  492. this.msg = message.getBytes(UTF_8);
  493. }
  494. static byte[] key(String ref, long index) {
  495. byte[] name = ref.getBytes(UTF_8);
  496. byte[] key = Arrays.copyOf(name, name.length + 1 + 8);
  497. NB.encodeInt64(key, key.length - 8, reverseUpdateIndex(index));
  498. return key;
  499. }
  500. @Override
  501. byte blockType() {
  502. return LOG_BLOCK_TYPE;
  503. }
  504. @Override
  505. int valueType() {
  506. return LOG_DATA;
  507. }
  508. @Override
  509. int valueSize() {
  510. return 2 * OBJECT_ID_LENGTH
  511. + computeVarintSize(name.length) + name.length
  512. + computeVarintSize(email.length) + email.length
  513. + computeVarintSize(timeSecs)
  514. + 2 // tz
  515. + computeVarintSize(msg.length) + msg.length;
  516. }
  517. @Override
  518. void writeValue(ReftableOutputStream os) {
  519. os.writeId(oldId);
  520. os.writeId(newId);
  521. os.writeVarintString(name);
  522. os.writeVarintString(email);
  523. os.writeVarint(timeSecs);
  524. os.writeInt16(tz);
  525. os.writeVarintString(msg);
  526. }
  527. }
  528. }