comparison src/org/tmatesoft/hg/internal/RevlogStreamWriter.java @ 530:0f6fa88e2162

Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Wed, 23 Jan 2013 17:46:12 +0100
parents
children 688c1ab113bb
comparison
equal deleted inserted replaced
529:95bdcf75e71e 530:0f6fa88e2162
1 /*
2 * Copyright (c) 2013 TMate Software Ltd
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * For information on how to redistribute this software under
14 * the terms of a license other than GNU General Public License
15 * contact TMate Software at support@hg4j.com
16 */
17 package org.tmatesoft.hg.internal;
18
19 import static org.tmatesoft.hg.internal.Internals.REVLOGV1_RECORD_SIZE;
20
21 import java.io.IOException;
22 import java.io.OutputStream;
23 import java.nio.ByteBuffer;
24
25 import org.tmatesoft.hg.core.Nodeid;
26
27 /**
28 *
29 * @author Artem Tikhomirov
30 * @author TMate Software Ltd.
31 */
32 public class RevlogStreamWriter {
33
34
35 public static class HeaderWriter {
36 private final ByteBuffer header;
37 private final boolean isInline;
38 private long offset;
39 private int length, compressedLength;
40 private int baseRev, linkRev, p1, p2;
41 private Nodeid nodeid;
42
43 public HeaderWriter(boolean inline) {
44 isInline = inline;
45 header = ByteBuffer.allocate(REVLOGV1_RECORD_SIZE);
46 }
47
48 public HeaderWriter offset(long offset) {
49 this.offset = offset;
50 return this;
51 }
52
53 public HeaderWriter baseRevision(int baseRevision) {
54 this.baseRev = baseRevision;
55 return this;
56 }
57
58 public HeaderWriter length(int len, int compressedLen) {
59 this.length = len;
60 this.compressedLength = compressedLen;
61 return this;
62 }
63
64 public HeaderWriter parents(int parent1, int parent2) {
65 p1 = parent1;
66 p2 = parent2;
67 return this;
68 }
69
70 public HeaderWriter linkRevision(int linkRevision) {
71 this.linkRev = linkRevision;
72 return this;
73 }
74
75 public HeaderWriter nodeid(Nodeid n) {
76 this.nodeid = n;
77 return this;
78 }
79
80 public void write(OutputStream out) throws IOException {
81 header.clear();
82 if (offset == 0) {
83 int version = 1 /* RevlogNG */;
84 if (isInline) {
85 final int INLINEDATA = 1 << 16; // FIXME extract constant
86 version |= INLINEDATA;
87 }
88 header.putInt(version);
89 header.putInt(0);
90 } else {
91 header.putLong(offset << 16);
92 }
93 header.putInt(compressedLength);
94 header.putInt(length);
95 header.putInt(baseRev);
96 header.putInt(linkRev);
97 header.putInt(p1);
98 header.putInt(p2);
99 header.put(nodeid.toByteArray());
100 // assume 12 bytes left are zeros
101 out.write(header.array());
102
103 // regardless whether it's inline or separate data,
104 // offset field always represent cumulative compressedLength
105 // (while offset in the index file with inline==true differs by n*sizeof(header), where n is entry's position in the file)
106 offset += compressedLength;
107 }
108 }
109
110 public void addRevision(String text, int baseRevision, int linkRevision, int p1, int p2) {
111 }
112 }