annotate src/org/tmatesoft/hg/internal/Metadata.java @ 642:9ef1fab9f5e3

Added tag v1.1.0 for changeset 2f33f102a8fa
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Tue, 11 Jun 2013 16:32:12 +0200
parents e3717fc7d26f
children 7efabe0cddcf
rev   line source
602
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
1 /*
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
2 * Copyright (c) 2013 TMate Software Ltd
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
3 *
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
4 * This program is free software; you can redistribute it and/or modify
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
5 * it under the terms of the GNU General Public License as published by
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
6 * the Free Software Foundation; version 2 of the License.
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
7 *
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
8 * This program is distributed in the hope that it will be useful,
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
11 * GNU General Public License for more details.
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
12 *
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
13 * For information on how to redistribute this software under
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
14 * the terms of a license other than GNU General Public License
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
15 * contact TMate Software at support@hg4j.com
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
16 */
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
17 package org.tmatesoft.hg.internal;
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
18
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
19 import static org.tmatesoft.hg.util.LogFacility.Severity.Error;
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
20
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
21 import java.io.ByteArrayOutputStream;
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
22 import java.io.IOException;
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
23 import java.util.ArrayList;
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
24 import java.util.Collection;
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
25
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
26 import org.tmatesoft.hg.core.SessionContext;
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
27 import org.tmatesoft.hg.repo.HgInvalidControlFileException;
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
28 import org.tmatesoft.hg.repo.HgInvalidStateException;
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
29 import org.tmatesoft.hg.util.LogFacility;
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
30
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
31 /**
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
32 * Container for metadata recorded as part of file revisions
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
33 *
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
34 * @author Artem Tikhomirov
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
35 * @author TMate Software Ltd.
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
36 */
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
37 public final class Metadata {
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
38 private static class Record {
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
39 public final int offset;
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
40 public final MetadataEntry[] entries;
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
41
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
42 public Record(int off, MetadataEntry[] entr) {
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
43 offset = off;
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
44 entries = entr;
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
45 }
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
46 }
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
47 // XXX sparse array needed
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
48 private final IntMap<Metadata.Record> entries = new IntMap<Metadata.Record>(5);
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
49
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
50 private final Metadata.Record NONE = new Record(-1, null); // don't want statics
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
51
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
52 private final LogFacility log;
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
53
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
54 public Metadata(SessionContext.Source sessionCtx) {
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
55 log = sessionCtx.getSessionContext().getLog();
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
56 }
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
57
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
58 // true when there's metadata for given revision
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
59 public boolean known(int revision) {
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
60 Metadata.Record i = entries.get(revision);
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
61 return i != null && NONE != i;
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
62 }
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
63
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
64 // true when revision has been checked for metadata presence.
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
65 public boolean checked(int revision) {
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
66 return entries.containsKey(revision);
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
67 }
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
68
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
69 // true when revision has been checked and found not having any metadata
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
70 public boolean none(int revision) {
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
71 Metadata.Record i = entries.get(revision);
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
72 return i == NONE;
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
73 }
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
74
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
75 // mark revision as having no metadata.
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
76 void recordNone(int revision) {
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
77 Metadata.Record i = entries.get(revision);
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
78 if (i == NONE) {
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
79 return; // already there
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
80 }
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
81 if (i != null) {
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
82 throw new HgInvalidStateException(String.format("Trying to override Metadata state for revision %d (known offset: %d)", revision, i));
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
83 }
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
84 entries.put(revision, NONE);
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
85 }
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
86
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
87 // since this is internal class, callers are supposed to ensure arg correctness (i.e. ask known() before)
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
88 public int dataOffset(int revision) {
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
89 return entries.get(revision).offset;
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
90 }
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
91 void add(int revision, int dataOffset, Collection<MetadataEntry> e) {
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
92 assert !entries.containsKey(revision);
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
93 entries.put(revision, new Record(dataOffset, e.toArray(new MetadataEntry[e.size()])));
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
94 }
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
95
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
96 /**
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
97 * @return <code>true</code> if metadata has been found
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
98 */
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
99 public boolean tryRead(int revisionNumber, DataAccess data) throws IOException, HgInvalidControlFileException {
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
100 final int daLength = data.length();
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
101 if (daLength < 4 || data.readByte() != 1 || data.readByte() != 10) {
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
102 recordNone(revisionNumber);
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
103 return false;
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
104 } else {
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
105 ArrayList<MetadataEntry> _metadata = new ArrayList<MetadataEntry>();
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
106 int offset = parseMetadata(data, daLength, _metadata);
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
107 add(revisionNumber, offset, _metadata);
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
108 return true;
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
109 }
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
110 }
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
111
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
112 public String find(int revision, String key) {
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
113 for (MetadataEntry me : entries.get(revision).entries) {
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
114 if (me.matchKey(key)) {
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
115 return me.value();
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
116 }
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
117 }
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
118 return null;
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
119 }
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
120
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
121 private int parseMetadata(DataAccess data, final int daLength, ArrayList<MetadataEntry> _metadata) throws IOException, HgInvalidControlFileException {
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
122 int lastEntryStart = 2;
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
123 int lastColon = -1;
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
124 // XXX in fact, need smth like ByteArrayBuilder, similar to StringBuilder,
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
125 // which can't be used here because we can't convert bytes to chars as we read them
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
126 // (there might be multi-byte encoding), and we need to collect all bytes before converting to string
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
127 ByteArrayOutputStream bos = new ByteArrayOutputStream();
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
128 String key = null, value = null;
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
129 boolean byteOne = false;
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
130 boolean metadataIsComplete = false;
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
131 for (int i = 2; i < daLength; i++) {
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
132 byte b = data.readByte();
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
133 if (b == '\n') {
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
134 if (byteOne) { // i.e. \n follows 1
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
135 lastEntryStart = i+1;
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
136 metadataIsComplete = true;
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
137 // XXX is it possible to have here incomplete key/value (i.e. if last pair didn't end with \n)
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
138 // if yes, need to set metadataIsComplete to true in that case as well
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
139 break;
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
140 }
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
141 if (key == null || lastColon == -1 || i <= lastColon) {
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
142 log.dump(getClass(), Error, "Missing key in file revision metadata at index %d", i);
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
143 }
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
144 value = new String(bos.toByteArray()).trim();
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
145 bos.reset();
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
146 _metadata.add(new MetadataEntry(key, value));
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
147 key = value = null;
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
148 lastColon = -1;
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
149 lastEntryStart = i+1;
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
150 continue;
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
151 }
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
152 // byteOne has to be consumed up to this line, if not yet, consume it
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
153 if (byteOne) {
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
154 // insert 1 we've read on previous step into the byte builder
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
155 bos.write(1);
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
156 byteOne = false;
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
157 // fall-through to consume current byte
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
158 }
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
159 if (b == (int) ':') {
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
160 assert value == null;
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
161 key = new String(bos.toByteArray());
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
162 bos.reset();
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
163 lastColon = i;
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
164 } else if (b == 1) {
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
165 byteOne = true;
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
166 } else {
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
167 bos.write(b);
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
168 }
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
169 }
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
170 // data.isEmpty is not reliable, renamed files of size==0 keep only metadata
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
171 if (!metadataIsComplete) {
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
172 // XXX perhaps, worth a testcase (empty file, renamed, read or ask ifCopy
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
173 throw new HgInvalidControlFileException("Metadata is not closed properly", null, null);
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
174 }
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
175 return lastEntryStart;
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
176 }
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
177
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
178 /**
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
179 * There may be several entries of metadata per single revision, this class captures single entry
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
180 */
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
181 private static class MetadataEntry {
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
182 private final String entry;
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
183 private final int valueStart;
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
184
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
185 // key may be null
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
186 /* package-local */MetadataEntry(String key, String value) {
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
187 if (key == null) {
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
188 entry = value;
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
189 valueStart = -1; // not 0 to tell between key == null and key == ""
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
190 } else {
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
191 entry = key + value;
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
192 valueStart = key.length();
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
193 }
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
194 }
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
195
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
196 /* package-local */boolean matchKey(String key) {
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
197 return key == null ? valueStart == -1 : key.length() == valueStart && entry.startsWith(key);
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
198 }
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
199
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
200 // uncomment once/if needed
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
201 // public String key() {
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
202 // return entry.substring(0, valueStart);
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
203 // }
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
204
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
205 public String value() {
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
206 return valueStart == -1 ? entry : entry.substring(valueStart);
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
207 }
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
208 }
e3717fc7d26f Refactor metadata parsing in HgDataFile, moved to standalone class
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
209 }