View Javadoc
1   /*
2    * Copyright (C) 2016, 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.transport;
45  
46  import org.eclipse.jgit.lib.Constants;
47  
48  /**
49   * Statistics about {@link PackParser}.
50   *
51   * @since 4.6
52   */
53  public class ReceivedPackStatistics {
54  	private long numBytesRead;
55  
56  	private long numWholeCommit;
57  	private long numWholeTree;
58  	private long numWholeBlob;
59  	private long numWholeTag;
60  	private long numOfsDelta;
61  	private long numRefDelta;
62  
63  	private long numDeltaCommit;
64  	private long numDeltaTree;
65  	private long numDeltaBlob;
66  	private long numDeltaTag;
67  
68  	/** @return number of bytes read from the input stream */
69  	public long getNumBytesRead() {
70  		return numBytesRead;
71  	}
72  
73  	/** @return number of whole commit objects in the pack */
74  	public long getNumWholeCommit() {
75  		return numWholeCommit;
76  	}
77  
78  	/** @return number of whole tree objects in the pack */
79  	public long getNumWholeTree() {
80  		return numWholeTree;
81  	}
82  
83  	/** @return number of whole blob objects in the pack */
84  	public long getNumWholeBlob() {
85  		return numWholeBlob;
86  	}
87  
88  	/** @return number of whole tag objects in the pack */
89  	public long getNumWholeTag() {
90  		return numWholeTag;
91  	}
92  
93  	/** @return number of offset delta objects in the pack */
94  	public long getNumOfsDelta() {
95  		return numOfsDelta;
96  	}
97  
98  	/** @return number of ref delta objects in the pack */
99  	public long getNumRefDelta() {
100 		return numRefDelta;
101 	}
102 
103 	/** @return number of delta commit objects in the pack */
104 	public long getNumDeltaCommit() {
105 		return numDeltaCommit;
106 	}
107 
108 	/** @return number of delta tree objects in the pack */
109 	public long getNumDeltaTree() {
110 		return numDeltaTree;
111 	}
112 
113 	/** @return number of delta blob objects in the pack */
114 	public long getNumDeltaBlob() {
115 		return numDeltaBlob;
116 	}
117 
118 	/** @return number of delta tag objects in the pack */
119 	public long getNumDeltaTag() {
120 		return numDeltaTag;
121 	}
122 
123 	/** A builder for {@link ReceivedPackStatistics}. */
124 	public static class Builder {
125 		private long numBytesRead;
126 
127 		private long numWholeCommit;
128 		private long numWholeTree;
129 		private long numWholeBlob;
130 		private long numWholeTag;
131 		private long numOfsDelta;
132 		private long numRefDelta;
133 
134 		private long numDeltaCommit;
135 		private long numDeltaTree;
136 		private long numDeltaBlob;
137 		private long numDeltaTag;
138 
139 		/**
140 		 * @param numBytesRead number of bytes read from the input stream
141 		 * @return this
142 		 */
143 		public Builder setNumBytesRead(long numBytesRead) {
144 			this.numBytesRead = numBytesRead;
145 			return this;
146 		}
147 
148 		/**
149 		 * Increment a whole object count.
150 		 *
151 		 * @param type OBJ_COMMIT, OBJ_TREE, OBJ_BLOB, or OBJ_TAG
152 		 * @return this
153 		 */
154 		public Builder addWholeObject(int type) {
155 			switch (type) {
156 				case Constants.OBJ_COMMIT:
157 					numWholeCommit++;
158 					break;
159 				case Constants.OBJ_TREE:
160 					numWholeTree++;
161 					break;
162 				case Constants.OBJ_BLOB:
163 					numWholeBlob++;
164 					break;
165 				case Constants.OBJ_TAG:
166 					numWholeTag++;
167 					break;
168 				default:
169 					throw new IllegalArgumentException(
170 							type + " cannot be a whole object"); //$NON-NLS-1$
171 			}
172 			return this;
173 		}
174 
175 		/** @return this */
176 		public Builder addOffsetDelta() {
177 			numOfsDelta++;
178 			return this;
179 		}
180 
181 		/** @return this */
182 		public Builder addRefDelta() {
183 			numRefDelta++;
184 			return this;
185 		}
186 
187 		/**
188 		 * Increment a delta object count.
189 		 *
190 		 * @param type OBJ_COMMIT, OBJ_TREE, OBJ_BLOB, or OBJ_TAG
191 		 * @return this
192 		 */
193 		public Builder addDeltaObject(int type) {
194 			switch (type) {
195 				case Constants.OBJ_COMMIT:
196 					numDeltaCommit++;
197 					break;
198 				case Constants.OBJ_TREE:
199 					numDeltaTree++;
200 					break;
201 				case Constants.OBJ_BLOB:
202 					numDeltaBlob++;
203 					break;
204 				case Constants.OBJ_TAG:
205 					numDeltaTag++;
206 					break;
207 				default:
208 					throw new IllegalArgumentException(
209 							"delta should be a delta to a whole object. " + //$NON-NLS-1$
210 							type + " cannot be a whole object"); //$NON-NLS-1$
211 			}
212 			return this;
213 		}
214 
215 		ReceivedPackStatistics build() {
216 			ReceivedPackStatistics s = new ReceivedPackStatistics();
217 			s.numBytesRead = numBytesRead;
218 			s.numWholeCommit = numWholeCommit;
219 			s.numWholeTree = numWholeTree;
220 			s.numWholeBlob = numWholeBlob;
221 			s.numWholeTag = numWholeTag;
222 			s.numOfsDelta = numOfsDelta;
223 			s.numRefDelta = numRefDelta;
224 			s.numDeltaCommit = numDeltaCommit;
225 			s.numDeltaTree = numDeltaTree;
226 			s.numDeltaBlob = numDeltaBlob;
227 			s.numDeltaTag = numDeltaTag;
228 			return s;
229 		}
230 	}
231 }