tikhomirov@17: /* tikhomirov@74: * Copyright (c) 2010-2011 TMate Software Ltd tikhomirov@74: * tikhomirov@74: * This program is free software; you can redistribute it and/or modify tikhomirov@74: * it under the terms of the GNU General Public License as published by tikhomirov@74: * the Free Software Foundation; version 2 of the License. tikhomirov@74: * tikhomirov@74: * This program is distributed in the hope that it will be useful, tikhomirov@74: * but WITHOUT ANY WARRANTY; without even the implied warranty of tikhomirov@74: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the tikhomirov@74: * GNU General Public License for more details. tikhomirov@74: * tikhomirov@74: * For information on how to redistribute this software under tikhomirov@74: * the terms of a license other than GNU General Public License tikhomirov@102: * contact TMate Software at support@hg4j.com tikhomirov@2: */ tikhomirov@74: package org.tmatesoft.hg.repo; tikhomirov@2: tikhomirov@74: import static org.tmatesoft.hg.repo.HgRepository.TIP; tikhomirov@74: tikhomirov@115: import java.nio.ByteBuffer; tikhomirov@78: import java.util.ArrayList; tikhomirov@78: import java.util.Collection; tikhomirov@79: import java.util.TreeMap; tikhomirov@78: tikhomirov@74: import org.tmatesoft.hg.core.Nodeid; tikhomirov@74: import org.tmatesoft.hg.core.Path; tikhomirov@121: import org.tmatesoft.hg.internal.FilterByteChannel; tikhomirov@77: import org.tmatesoft.hg.internal.RevlogStream; tikhomirov@115: import org.tmatesoft.hg.util.ByteChannel; tikhomirov@74: tikhomirov@5: tikhomirov@17: tikhomirov@2: /** tikhomirov@2: * ? name:HgFileNode? tikhomirov@74: * tikhomirov@74: * @author Artem Tikhomirov tikhomirov@74: * @author TMate Software Ltd. tikhomirov@2: */ tikhomirov@2: public class HgDataFile extends Revlog { tikhomirov@2: tikhomirov@3: // absolute from repo root? tikhomirov@3: // slashes, unix-style? tikhomirov@3: // repo location agnostic, just to give info to user, not to access real storage tikhomirov@74: private final Path path; tikhomirov@78: private Metadata metadata; tikhomirov@2: tikhomirov@115: /*package-local*/HgDataFile(HgRepository hgRepo, Path filePath, RevlogStream content) { tikhomirov@21: super(hgRepo, content); tikhomirov@115: path = filePath; tikhomirov@3: } tikhomirov@115: tikhomirov@115: /*package-local*/HgDataFile(HgRepository hgRepo, Path filePath) { tikhomirov@115: super(hgRepo); tikhomirov@115: path = filePath; tikhomirov@115: } tikhomirov@115: tikhomirov@115: // exists is not the best name possible. now it means no file with such name was ever known to the repo. tikhomirov@115: // it might be confused with files existed before but lately removed. tikhomirov@3: public boolean exists() { tikhomirov@3: return content != null; // XXX need better impl tikhomirov@2: } tikhomirov@2: tikhomirov@77: // human-readable (i.e. "COPYING", not "store/data/_c_o_p_y_i_n_g.i") tikhomirov@74: public Path getPath() { tikhomirov@2: return path; // hgRepo.backresolve(this) -> name? tikhomirov@2: } tikhomirov@2: tikhomirov@22: public int length(Nodeid nodeid) { tikhomirov@88: return content.dataLength(getLocalRevision(nodeid)); tikhomirov@22: } tikhomirov@22: tikhomirov@2: public byte[] content() { tikhomirov@2: return content(TIP); tikhomirov@2: } tikhomirov@115: tikhomirov@121: /*XXX not sure applyFilters is the best way to do, perhaps, callers shall add filters themselves?*/ tikhomirov@121: public void content(int revision, ByteChannel sink, boolean applyFilters) throws /*TODO typed*/Exception { tikhomirov@115: byte[] content = content(revision); tikhomirov@115: ByteBuffer buf = ByteBuffer.allocate(512); tikhomirov@115: int left = content.length; tikhomirov@115: int offset = 0; tikhomirov@121: ByteChannel _sink = applyFilters ? new FilterByteChannel(sink, getRepo().getFiltersFromRepoToWorkingDir(getPath())) : sink; tikhomirov@115: do { tikhomirov@115: buf.put(content, offset, Math.min(left, buf.remaining())); tikhomirov@115: buf.flip(); tikhomirov@121: // XXX I may not rely on returned number of bytes but track change in buf position instead. tikhomirov@121: int consumed = _sink.write(buf); tikhomirov@115: buf.compact(); tikhomirov@115: offset += consumed; tikhomirov@115: left -= consumed; tikhomirov@115: } while (left > 0); tikhomirov@115: } tikhomirov@22: tikhomirov@78: // for data files need to check heading of the file content for possible metadata tikhomirov@78: // @see http://mercurial.selenic.com/wiki/FileFormats#data.2BAC8- tikhomirov@78: @Override tikhomirov@78: public byte[] content(int revision) { tikhomirov@78: if (revision == TIP) { tikhomirov@78: revision = content.revisionCount() - 1; // FIXME maxRevision. tikhomirov@78: } tikhomirov@78: byte[] data = super.content(revision); tikhomirov@78: if (data.length < 4 || (data[0] != 1 && data[1] != 10)) { tikhomirov@78: return data; tikhomirov@78: } tikhomirov@78: int toSkip = 0; tikhomirov@78: if (metadata == null || !metadata.known(revision)) { tikhomirov@78: int lastEntryStart = 2; tikhomirov@78: int lastColon = -1; tikhomirov@78: ArrayList _metadata = new ArrayList(); tikhomirov@78: String key = null, value = null; tikhomirov@78: for (int i = 2; i < data.length; i++) { tikhomirov@78: if (data[i] == (int) ':') { tikhomirov@78: key = new String(data, lastEntryStart, i - lastEntryStart); tikhomirov@78: lastColon = i; tikhomirov@78: } else if (data[i] == '\n') { tikhomirov@78: if (key == null || lastColon == -1 || i <= lastColon) { tikhomirov@78: throw new IllegalStateException(); // FIXME log instead and record null key in the metadata. Ex just to fail fast during dev tikhomirov@78: } tikhomirov@78: value = new String(data, lastColon + 1, i - lastColon - 1).trim(); tikhomirov@78: _metadata.add(new MetadataEntry(key, value)); tikhomirov@78: key = value = null; tikhomirov@78: lastColon = -1; tikhomirov@78: lastEntryStart = i+1; tikhomirov@78: } else if (data[i] == 1 && i + 1 < data.length && data[i+1] == 10) { tikhomirov@78: if (key != null && lastColon != -1 && i > lastColon) { tikhomirov@78: // just in case last entry didn't end with newline tikhomirov@78: value = new String(data, lastColon + 1, i - lastColon - 1); tikhomirov@78: _metadata.add(new MetadataEntry(key, value)); tikhomirov@78: } tikhomirov@78: lastEntryStart = i+1; tikhomirov@78: break; tikhomirov@78: } tikhomirov@78: } tikhomirov@78: _metadata.trimToSize(); tikhomirov@78: if (metadata == null) { tikhomirov@78: metadata = new Metadata(); tikhomirov@78: } tikhomirov@78: metadata.add(revision, lastEntryStart, _metadata); tikhomirov@78: toSkip = lastEntryStart; tikhomirov@78: } else { tikhomirov@78: toSkip = metadata.dataOffset(revision); tikhomirov@78: } tikhomirov@78: // XXX copy of an array may be memory-hostile, a wrapper with baseOffsetShift(lastEntryStart) would be more convenient tikhomirov@78: byte[] rv = new byte[data.length - toSkip]; tikhomirov@78: System.arraycopy(data, toSkip, rv, 0, rv.length); tikhomirov@78: return rv; tikhomirov@78: } tikhomirov@78: tikhomirov@3: public void history(Changeset.Inspector inspector) { tikhomirov@48: history(0, content.revisionCount() - 1, inspector); tikhomirov@48: } tikhomirov@48: tikhomirov@48: public void history(int start, int end, Changeset.Inspector inspector) { tikhomirov@3: if (!exists()) { tikhomirov@3: throw new IllegalStateException("Can't get history of invalid repository file node"); tikhomirov@3: } tikhomirov@77: final int last = content.revisionCount() - 1; tikhomirov@77: if (start < 0 || start > last) { tikhomirov@77: throw new IllegalArgumentException(); tikhomirov@77: } tikhomirov@77: if (end == TIP) { tikhomirov@77: end = last; tikhomirov@77: } else if (end < start || end > last) { tikhomirov@77: throw new IllegalArgumentException(); tikhomirov@77: } tikhomirov@48: final int[] commitRevisions = new int[end - start + 1]; tikhomirov@77: RevlogStream.Inspector insp = new RevlogStream.Inspector() { tikhomirov@3: int count = 0; tikhomirov@3: tikhomirov@3: public void next(int revisionNumber, int actualLen, int baseRevision, int linkRevision, int parent1Revision, int parent2Revision, byte[] nodeid, byte[] data) { tikhomirov@3: commitRevisions[count++] = linkRevision; tikhomirov@3: } tikhomirov@3: }; tikhomirov@48: content.iterate(start, end, false, insp); tikhomirov@3: getRepo().getChangelog().range(inspector, commitRevisions); tikhomirov@3: } tikhomirov@88: tikhomirov@88: // for a given local revision of the file, find out local revision in the changelog tikhomirov@88: public int getChangesetLocalRevision(int revision) { tikhomirov@88: return content.linkRevision(revision); tikhomirov@88: } tikhomirov@88: tikhomirov@88: public Nodeid getChangesetRevision(Nodeid nid) { tikhomirov@88: int changelogRevision = getChangesetLocalRevision(getLocalRevision(nid)); tikhomirov@88: return getRepo().getChangelog().getRevision(changelogRevision); tikhomirov@88: } tikhomirov@78: tikhomirov@78: public boolean isCopy() { tikhomirov@78: if (metadata == null) { tikhomirov@78: content(0); // FIXME expensive way to find out metadata, distinct RevlogStream.Iterator would be better. tikhomirov@78: } tikhomirov@78: if (metadata == null || !metadata.known(0)) { tikhomirov@78: return false; tikhomirov@78: } tikhomirov@78: return metadata.find(0, "copy") != null; tikhomirov@78: } tikhomirov@78: tikhomirov@78: public Path getCopySourceName() { tikhomirov@78: if (isCopy()) { tikhomirov@78: return Path.create(metadata.find(0, "copy")); tikhomirov@78: } tikhomirov@78: throw new UnsupportedOperationException(); // XXX REVISIT, think over if Exception is good (clients would check isCopy() anyway, perhaps null is sufficient?) tikhomirov@78: } tikhomirov@78: tikhomirov@78: public Nodeid getCopySourceRevision() { tikhomirov@78: if (isCopy()) { tikhomirov@78: return Nodeid.fromAscii(metadata.find(0, "copyrev")); // XXX reuse/cache Nodeid tikhomirov@78: } tikhomirov@78: throw new UnsupportedOperationException(); tikhomirov@78: } tikhomirov@88: tikhomirov@88: @Override tikhomirov@88: public String toString() { tikhomirov@88: StringBuilder sb = new StringBuilder(getClass().getSimpleName()); tikhomirov@88: sb.append('('); tikhomirov@88: sb.append(getPath()); tikhomirov@88: sb.append(')'); tikhomirov@88: return sb.toString(); tikhomirov@88: } tikhomirov@78: tikhomirov@88: private static final class MetadataEntry { tikhomirov@78: private final String entry; tikhomirov@78: private final int valueStart; tikhomirov@78: /*package-local*/MetadataEntry(String key, String value) { tikhomirov@78: entry = key + value; tikhomirov@78: valueStart = key.length(); tikhomirov@78: } tikhomirov@78: /*package-local*/boolean matchKey(String key) { tikhomirov@78: return key.length() == valueStart && entry.startsWith(key); tikhomirov@78: } tikhomirov@78: public String key() { tikhomirov@78: return entry.substring(0, valueStart); tikhomirov@78: } tikhomirov@78: public String value() { tikhomirov@78: return entry.substring(valueStart); tikhomirov@78: } tikhomirov@78: } tikhomirov@78: tikhomirov@78: private static class Metadata { tikhomirov@78: // XXX sparse array needed tikhomirov@79: private final TreeMap offsets = new TreeMap(); tikhomirov@79: private final TreeMap entries = new TreeMap(); tikhomirov@78: boolean known(int revision) { tikhomirov@78: return offsets.containsKey(revision); tikhomirov@78: } tikhomirov@78: // since this is internal class, callers are supposed to ensure arg correctness (i.e. ask known() before) tikhomirov@78: int dataOffset(int revision) { tikhomirov@78: return offsets.get(revision); tikhomirov@78: } tikhomirov@78: void add(int revision, int dataOffset, Collection e) { tikhomirov@78: offsets.put(revision, dataOffset); tikhomirov@78: entries.put(revision, e.toArray(new MetadataEntry[e.size()])); tikhomirov@78: } tikhomirov@78: String find(int revision, String key) { tikhomirov@78: for (MetadataEntry me : entries.get(revision)) { tikhomirov@78: if (me.matchKey(key)) { tikhomirov@78: return me.value(); tikhomirov@78: } tikhomirov@78: } tikhomirov@78: return null; tikhomirov@78: } tikhomirov@78: } tikhomirov@2: }