View Javadoc
1   /*
2    * Copyright (C) 2011, 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  
44  package org.eclipse.jgit.internal.storage.pack;
45  
46  import static org.eclipse.jgit.lib.Constants.OBJ_BLOB;
47  import static org.eclipse.jgit.lib.Constants.OBJ_TREE;
48  
49  import java.io.IOException;
50  import java.util.List;
51  import java.util.Set;
52  
53  import org.eclipse.jgit.errors.IncorrectObjectTypeException;
54  import org.eclipse.jgit.errors.MissingObjectException;
55  import org.eclipse.jgit.lib.AnyObjectId;
56  import org.eclipse.jgit.lib.FileMode;
57  import org.eclipse.jgit.lib.MutableObjectId;
58  import org.eclipse.jgit.lib.ObjectId;
59  import org.eclipse.jgit.lib.ObjectIdOwnerMap;
60  import org.eclipse.jgit.lib.ObjectLoader;
61  import org.eclipse.jgit.lib.ObjectReader;
62  import org.eclipse.jgit.lib.ProgressMonitor;
63  import org.eclipse.jgit.revwalk.RevTree;
64  import org.eclipse.jgit.treewalk.CanonicalTreeParser;
65  
66  class BaseSearch {
67  	private static final int M_BLOB = FileMode.REGULAR_FILE.getBits();
68  
69  	private static final int M_TREE = FileMode.TREE.getBits();
70  
71  	private final ProgressMonitor progress;
72  
73  	private final ObjectReader reader;
74  
75  	private final ObjectId[] baseTrees;
76  
77  	private final ObjectIdOwnerMap<ObjectToPack> objectsMap;
78  
79  	private final List<ObjectToPack> edgeObjects;
80  
81  	private final IntSet alreadyProcessed;
82  
83  	private final ObjectIdOwnerMap<TreeWithData> treeCache;
84  
85  	private final CanonicalTreeParser parser;
86  
87  	private final MutableObjectId idBuf;
88  
89  	BaseSearch(ProgressMonitor countingMonitor, Set<RevTree> bases,
90  			ObjectIdOwnerMap<ObjectToPack> objects,
91  			List<ObjectToPack> edges, ObjectReader or) {
92  		progress = countingMonitor;
93  		reader = or;
94  		baseTrees = bases.toArray(new ObjectId[bases.size()]);
95  		objectsMap = objects;
96  		edgeObjects = edges;
97  
98  		alreadyProcessed = new IntSet();
99  		treeCache = new ObjectIdOwnerMap<TreeWithData>();
100 		parser = new CanonicalTreeParser();
101 		idBuf = new MutableObjectId();
102 	}
103 
104 	void addBase(int objectType, byte[] pathBuf, int pathLen, int pathHash)
105 			throws IOException {
106 		final int tailMode = modeForType(objectType);
107 		if (tailMode == 0)
108 			return;
109 
110 		if (!alreadyProcessed.add(pathHash))
111 			return;
112 
113 		if (pathLen == 0) {
114 			for (ObjectId root : baseTrees)
115 				add(root, OBJ_TREE, pathHash);
116 			return;
117 		}
118 
119 		final int firstSlash = nextSlash(pathBuf, 0, pathLen);
120 
121 		CHECK_BASE: for (ObjectId root : baseTrees) {
122 			int ptr = 0;
123 			int end = firstSlash;
124 			int mode = end != pathLen ? M_TREE : tailMode;
125 
126 			parser.reset(readTree(root));
127 			while (!parser.eof()) {
128 				int cmp = parser.pathCompare(pathBuf, ptr, end, mode);
129 
130 				if (cmp < 0) {
131 					parser.next();
132 					continue;
133 				}
134 
135 				if (cmp > 0)
136 					continue CHECK_BASE;
137 
138 				if (end == pathLen) {
139 					if (parser.getEntryFileMode().getObjectType() == objectType) {
140 						idBuf.fromRaw(parser.idBuffer(), parser.idOffset());
141 						add(idBuf, objectType, pathHash);
142 					}
143 					continue CHECK_BASE;
144 				}
145 
146 				if (!FileMode.TREE.equals(parser.getEntryRawMode()))
147 					continue CHECK_BASE;
148 
149 				ptr = end + 1;
150 				end = nextSlash(pathBuf, ptr, pathLen);
151 				mode = end != pathLen ? M_TREE : tailMode;
152 
153 				idBuf.fromRaw(parser.idBuffer(), parser.idOffset());
154 				parser.reset(readTree(idBuf));
155 			}
156 		}
157 	}
158 
159 	private static int modeForType(int typeCode) {
160 		switch (typeCode) {
161 		case OBJ_TREE:
162 			return M_TREE;
163 
164 		case OBJ_BLOB:
165 			return M_BLOB;
166 
167 		default:
168 			return 0;
169 		}
170 	}
171 
172 	private static int nextSlash(byte[] pathBuf, int ptr, int end) {
173 		while (ptr < end && pathBuf[ptr] != '/')
174 			ptr++;
175 		return ptr;
176 	}
177 
178 	private void add(AnyObjectId id, int objectType, int pathHash) {
179 		ObjectToPack obj = new ObjectToPack(id, objectType);
180 		obj.setEdge();
181 		obj.setPathHash(pathHash);
182 
183 		if (objectsMap.addIfAbsent(obj) == obj) {
184 			edgeObjects.add(obj);
185 			progress.update(1);
186 		}
187 	}
188 
189 	private byte[] readTree(AnyObjectId id) throws MissingObjectException,
190 			IncorrectObjectTypeException, IOException {
191 		TreeWithData tree = treeCache.get(id);
192 		if (tree != null)
193 			return tree.buf;
194 
195 		ObjectLoader ldr = reader.open(id, OBJ_TREE);
196 		byte[] buf = ldr.getCachedBytes(Integer.MAX_VALUE);
197 		treeCache.add(new TreeWithData(id, buf));
198 		return buf;
199 	}
200 
201 	private static class TreeWithData extends ObjectIdOwnerMap.Entry {
202 		final byte[] buf;
203 
204 		TreeWithData(AnyObjectId id, byte[] buf) {
205 			super(id);
206 			this.buf = buf;
207 		}
208 	}
209 }