1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 using System;
19 
20 namespace Lucene.Net.Index
21 {
22 
23 	sealed class IntBlockPool
24 	{
InitBlock()25 		private void  InitBlock()
26 		{
27 			intUpto = DocumentsWriter.INT_BLOCK_SIZE;
28 		}
29 
30 		public int[][] buffers = new int[10][];
31 
32 		internal int bufferUpto = - 1; // Which buffer we are upto
33 		public int intUpto; // Where we are in head buffer
34 
35 		public int[] buffer; // Current head buffer
36 		public int intOffset = - DocumentsWriter.INT_BLOCK_SIZE; // Current head offset
37 
38 		private DocumentsWriter docWriter;
39 		internal bool trackAllocations;
40 
IntBlockPool(DocumentsWriter docWriter, bool trackAllocations)41 		public IntBlockPool(DocumentsWriter docWriter, bool trackAllocations)
42 		{
43 			InitBlock();
44 			this.docWriter = docWriter;
45 			this.trackAllocations = trackAllocations;
46 		}
47 
Reset()48 		public void  Reset()
49 		{
50 			if (bufferUpto != - 1)
51 			{
52 				if (bufferUpto > 0)
53 				// Recycle all but the first buffer
54 					docWriter.RecycleIntBlocks(buffers, 1, 1 + bufferUpto);
55 
56 				// Reuse first buffer
57 				bufferUpto = 0;
58 				intUpto = 0;
59 				intOffset = 0;
60 				buffer = buffers[0];
61 			}
62 		}
63 
NextBuffer()64 		public void  NextBuffer()
65 		{
66 			if (1 + bufferUpto == buffers.Length)
67 			{
68 				int[][] newBuffers = new int[(int) (buffers.Length * 1.5)][];
69 				Array.Copy(buffers, 0, newBuffers, 0, buffers.Length);
70 				buffers = newBuffers;
71 			}
72 			buffer = buffers[1 + bufferUpto] = docWriter.GetIntBlock(trackAllocations);
73 			bufferUpto++;
74 
75 			intUpto = 0;
76 			intOffset += DocumentsWriter.INT_BLOCK_SIZE;
77 		}
78 	}
79 }