View Javadoc
1   /*
2    * Copyright (C) 2016, 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.transport;
12  
13  import org.eclipse.jgit.lib.Constants;
14  
15  /**
16   * Statistics about {@link org.eclipse.jgit.transport.PackParser}.
17   *
18   * @since 4.6
19   */
20  public class ReceivedPackStatistics {
21  	private long numBytesRead;
22  	private long numBytesDuplicated;
23  
24  	private long numWholeCommit;
25  	private long numWholeTree;
26  	private long numWholeBlob;
27  	private long numWholeTag;
28  	private long numOfsDelta;
29  	private long numRefDelta;
30  	private long numObjectsDuplicated;
31  
32  	private long numDeltaCommit;
33  	private long numDeltaTree;
34  	private long numDeltaBlob;
35  	private long numDeltaTag;
36  
37  	/**
38  	 * Get number of bytes read from the input stream
39  	 *
40  	 * @return number of bytes read from the input stream
41  	 */
42  	public long getNumBytesRead() {
43  		return numBytesRead;
44  	}
45  
46  	/**
47  	 * Get number of bytes of objects already in the local database
48  	 *
49  	 * @return number of bytes of objects appeared in both the pack sent by the
50  	 *         client and the local database
51  	 * @since 5.10
52  	 */
53  	public long getNumBytesDuplicated() {
54  		return numBytesDuplicated;
55  	}
56  
57  	/**
58  	 * Get number of whole commit objects in the pack
59  	 *
60  	 * @return number of whole commit objects in the pack
61  	 */
62  	public long getNumWholeCommit() {
63  		return numWholeCommit;
64  	}
65  
66  	/**
67  	 * Get number of whole tree objects in the pack
68  	 *
69  	 * @return number of whole tree objects in the pack
70  	 */
71  	public long getNumWholeTree() {
72  		return numWholeTree;
73  	}
74  
75  	/**
76  	 * Get number of whole blob objects in the pack
77  	 *
78  	 * @return number of whole blob objects in the pack
79  	 */
80  	public long getNumWholeBlob() {
81  		return numWholeBlob;
82  	}
83  
84  	/**
85  	 * Get number of whole tag objects in the pack
86  	 *
87  	 * @return number of whole tag objects in the pack
88  	 */
89  	public long getNumWholeTag() {
90  		return numWholeTag;
91  	}
92  
93  	/**
94  	 * Get number of offset delta objects in the pack
95  	 *
96  	 * @return number of offset delta objects in the pack
97  	 */
98  	public long getNumOfsDelta() {
99  		return numOfsDelta;
100 	}
101 
102 	/**
103 	 * Get number of ref delta objects in the pack
104 	 *
105 	 * @return number of ref delta objects in the pack
106 	 */
107 	public long getNumRefDelta() {
108 		return numRefDelta;
109 	}
110 
111 	/**
112 	 * Get number of objects already in the local database
113 	 *
114 	 * @return number of objects appeared in both the pack sent by the client
115 	 *         and the local database
116 	 * @since 5.10
117 	 */
118 	public long getNumObjectsDuplicated() {
119 		return numObjectsDuplicated;
120 	}
121 
122 	/**
123 	 * Get number of delta commit objects in the pack
124 	 *
125 	 * @return number of delta commit objects in the pack
126 	 */
127 	public long getNumDeltaCommit() {
128 		return numDeltaCommit;
129 	}
130 
131 	/**
132 	 * Get number of delta tree objects in the pack
133 	 *
134 	 * @return number of delta tree objects in the pack
135 	 */
136 	public long getNumDeltaTree() {
137 		return numDeltaTree;
138 	}
139 
140 	/**
141 	 * Get number of delta blob objects in the pack
142 	 *
143 	 * @return number of delta blob objects in the pack
144 	 */
145 	public long getNumDeltaBlob() {
146 		return numDeltaBlob;
147 	}
148 
149 	/**
150 	 * Get number of delta tag objects in the pack
151 	 *
152 	 * @return number of delta tag objects in the pack
153 	 */
154 	public long getNumDeltaTag() {
155 		return numDeltaTag;
156 	}
157 
158 	/** A builder for {@link ReceivedPackStatistics}. */
159 	public static class Builder {
160 		private long numBytesRead;
161 		private long numBytesDuplicated;
162 
163 		private long numWholeCommit;
164 		private long numWholeTree;
165 		private long numWholeBlob;
166 		private long numWholeTag;
167 		private long numOfsDelta;
168 		private long numRefDelta;
169 		private long numObjectsDuplicated;
170 
171 		private long numDeltaCommit;
172 		private long numDeltaTree;
173 		private long numDeltaBlob;
174 		private long numDeltaTag;
175 
176 		/**
177 		 * @param numBytesRead number of bytes read from the input stream
178 		 * @return this
179 		 */
180 		public Builder setNumBytesRead(long numBytesRead) {
181 			this.numBytesRead = numBytesRead;
182 			return this;
183 		}
184 
185 		/**
186 		 * @param size
187 		 *            additional bytes already in the local database
188 		 * @return this
189 		 * @since 5.10
190 		 */
191 		Builder incrementNumBytesDuplicated(long size) {
192 			numBytesDuplicated += size;
193 			return this;
194 		}
195 
196 		/**
197 		 * Increment a whole object count.
198 		 *
199 		 * @param type OBJ_COMMIT, OBJ_TREE, OBJ_BLOB, or OBJ_TAG
200 		 * @return this
201 		 */
202 		public Builder addWholeObject(int type) {
203 			switch (type) {
204 				case Constants.OBJ_COMMIT:
205 					numWholeCommit++;
206 					break;
207 				case Constants.OBJ_TREE:
208 					numWholeTree++;
209 					break;
210 				case Constants.OBJ_BLOB:
211 					numWholeBlob++;
212 					break;
213 				case Constants.OBJ_TAG:
214 					numWholeTag++;
215 					break;
216 				default:
217 					throw new IllegalArgumentException(
218 							type + " cannot be a whole object"); //$NON-NLS-1$
219 			}
220 			return this;
221 		}
222 
223 		/** @return this */
224 		public Builder addOffsetDelta() {
225 			numOfsDelta++;
226 			return this;
227 		}
228 
229 		/** @return this */
230 		public Builder addRefDelta() {
231 			numRefDelta++;
232 			return this;
233 		}
234 
235 		/**
236 		 * Increment the duplicated object count.
237 		 *
238 		 * @return this
239 		 * @since 5.10
240 		 */
241 		Builder incrementObjectsDuplicated() {
242 			numObjectsDuplicated++;
243 			return this;
244 		}
245 
246 		/**
247 		 * Increment a delta object count.
248 		 *
249 		 * @param type OBJ_COMMIT, OBJ_TREE, OBJ_BLOB, or OBJ_TAG
250 		 * @return this
251 		 */
252 		public Builder addDeltaObject(int type) {
253 			switch (type) {
254 				case Constants.OBJ_COMMIT:
255 					numDeltaCommit++;
256 					break;
257 				case Constants.OBJ_TREE:
258 					numDeltaTree++;
259 					break;
260 				case Constants.OBJ_BLOB:
261 					numDeltaBlob++;
262 					break;
263 				case Constants.OBJ_TAG:
264 					numDeltaTag++;
265 					break;
266 				default:
267 					throw new IllegalArgumentException(
268 							"delta should be a delta to a whole object. " + //$NON-NLS-1$
269 							type + " cannot be a whole object"); //$NON-NLS-1$
270 			}
271 			return this;
272 		}
273 
274 		ReceivedPackStatistics build() {
275 			ReceivedPackStatistics s = new ReceivedPackStatistics();
276 			s.numBytesRead = numBytesRead;
277 			s.numBytesDuplicated = numBytesDuplicated;
278 			s.numWholeCommit = numWholeCommit;
279 			s.numWholeTree = numWholeTree;
280 			s.numWholeBlob = numWholeBlob;
281 			s.numWholeTag = numWholeTag;
282 			s.numOfsDelta = numOfsDelta;
283 			s.numRefDelta = numRefDelta;
284 			s.numDeltaCommit = numDeltaCommit;
285 			s.numDeltaTree = numDeltaTree;
286 			s.numDeltaBlob = numDeltaBlob;
287 			s.numDeltaTag = numDeltaTag;
288 			s.numObjectsDuplicated = numObjectsDuplicated;
289 			return s;
290 		}
291 	}
292 }