View Javadoc
1   /*
2    * Copyright (C) 2011, Google Inc. and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  
11  package org.eclipse.jgit.internal.storage.pack;
12  
13  import static org.eclipse.jgit.lib.Constants.OBJ_BLOB;
14  import static org.eclipse.jgit.lib.Constants.OBJ_TREE;
15  
16  import java.io.IOException;
17  import java.util.List;
18  import java.util.Set;
19  
20  import org.eclipse.jgit.errors.IncorrectObjectTypeException;
21  import org.eclipse.jgit.errors.MissingObjectException;
22  import org.eclipse.jgit.lib.AnyObjectId;
23  import org.eclipse.jgit.lib.FileMode;
24  import org.eclipse.jgit.lib.MutableObjectId;
25  import org.eclipse.jgit.lib.ObjectId;
26  import org.eclipse.jgit.lib.ObjectIdOwnerMap;
27  import org.eclipse.jgit.lib.ObjectLoader;
28  import org.eclipse.jgit.lib.ObjectReader;
29  import org.eclipse.jgit.lib.ProgressMonitor;
30  import org.eclipse.jgit.revwalk.RevTree;
31  import org.eclipse.jgit.treewalk.CanonicalTreeParser;
32  
33  class BaseSearch {
34  	private static final int M_BLOB = FileMode.REGULAR_FILE.getBits();
35  
36  	private static final int M_TREE = FileMode.TREE.getBits();
37  
38  	private final ProgressMonitor progress;
39  
40  	private final ObjectReader reader;
41  
42  	private final ObjectId[] baseTrees;
43  
44  	private final ObjectIdOwnerMap<ObjectToPack> objectsMap;
45  
46  	private final List<ObjectToPack> edgeObjects;
47  
48  	private final IntSet alreadyProcessed;
49  
50  	private final ObjectIdOwnerMap<TreeWithData> treeCache;
51  
52  	private final CanonicalTreeParser parser;
53  
54  	private final MutableObjectId idBuf;
55  
56  	BaseSearch(ProgressMonitor countingMonitor, Set<RevTree> bases,
57  			ObjectIdOwnerMap<ObjectToPack> objects,
58  			List<ObjectToPack> edges, ObjectReader or) {
59  		progress = countingMonitor;
60  		reader = or;
61  		baseTrees = bases.toArray(new ObjectId[0]);
62  		objectsMap = objects;
63  		edgeObjects = edges;
64  
65  		alreadyProcessed = new IntSet();
66  		treeCache = new ObjectIdOwnerMap<>();
67  		parser = new CanonicalTreeParser();
68  		idBuf = new MutableObjectId();
69  	}
70  
71  	void addBase(int objectType, byte[] pathBuf, int pathLen, int pathHash)
72  			throws IOException {
73  		final int tailMode = modeForType(objectType);
74  		if (tailMode == 0)
75  			return;
76  
77  		if (!alreadyProcessed.add(pathHash))
78  			return;
79  
80  		if (pathLen == 0) {
81  			for (ObjectId root : baseTrees)
82  				add(root, OBJ_TREE, pathHash);
83  			return;
84  		}
85  
86  		final int firstSlash = nextSlash(pathBuf, 0, pathLen);
87  
88  		CHECK_BASE: for (ObjectId root : baseTrees) {
89  			int ptr = 0;
90  			int end = firstSlash;
91  			int mode = end != pathLen ? M_TREE : tailMode;
92  
93  			parser.reset(readTree(root));
94  			while (!parser.eof()) {
95  				int cmp = parser.pathCompare(pathBuf, ptr, end, mode);
96  
97  				if (cmp < 0) {
98  					parser.next();
99  					continue;
100 				}
101 
102 				if (cmp > 0)
103 					continue CHECK_BASE;
104 
105 				if (end == pathLen) {
106 					if (parser.getEntryFileMode().getObjectType() == objectType) {
107 						idBuf.fromRaw(parser.idBuffer(), parser.idOffset());
108 						add(idBuf, objectType, pathHash);
109 					}
110 					continue CHECK_BASE;
111 				}
112 
113 				if (!FileMode.TREE.equals(parser.getEntryRawMode()))
114 					continue CHECK_BASE;
115 
116 				ptr = end + 1;
117 				end = nextSlash(pathBuf, ptr, pathLen);
118 				mode = end != pathLen ? M_TREE : tailMode;
119 
120 				idBuf.fromRaw(parser.idBuffer(), parser.idOffset());
121 				parser.reset(readTree(idBuf));
122 			}
123 		}
124 	}
125 
126 	private static int modeForType(int typeCode) {
127 		switch (typeCode) {
128 		case OBJ_TREE:
129 			return M_TREE;
130 
131 		case OBJ_BLOB:
132 			return M_BLOB;
133 
134 		default:
135 			return 0;
136 		}
137 	}
138 
139 	private static int nextSlash(byte[] pathBuf, int ptr, int end) {
140 		while (ptr < end && pathBuf[ptr] != '/')
141 			ptr++;
142 		return ptr;
143 	}
144 
145 	private void add(AnyObjectId id, int objectType, int pathHash) {
146 		ObjectToPack obj = new ObjectToPack(id, objectType);
147 		obj.setEdge();
148 		obj.setPathHash(pathHash);
149 
150 		if (objectsMap.addIfAbsent(obj) == obj) {
151 			edgeObjects.add(obj);
152 			progress.update(1);
153 		}
154 	}
155 
156 	private byte[] readTree(AnyObjectId id) throws MissingObjectException,
157 			IncorrectObjectTypeException, IOException {
158 		TreeWithData tree = treeCache.get(id);
159 		if (tree != null)
160 			return tree.buf;
161 
162 		ObjectLoader ldr = reader.open(id, OBJ_TREE);
163 		byte[] buf = ldr.getCachedBytes(Integer.MAX_VALUE);
164 		treeCache.add(new TreeWithData(id, buf));
165 		return buf;
166 	}
167 
168 	private static class TreeWithData extends ObjectIdOwnerMap.Entry {
169 		final byte[] buf;
170 
171 		TreeWithData(AnyObjectId id, byte[] buf) {
172 			super(id);
173 			this.buf = buf;
174 		}
175 	}
176 }