/// <summary> /// Creates a new <see cref="CachedOrds"/> from the <see cref="BinaryDocValues"/>. /// Assumes that the <see cref="BinaryDocValues"/> is not <c>null</c>. /// </summary> public CachedOrds(OrdinalsSegmentReader source, int maxDoc) { Offsets = new int[maxDoc + 1]; int[] ords = new int[maxDoc]; // let's assume one ordinal per-document as an initial size // this aggregator is limited to Integer.MAX_VALUE total ordinals. long totOrds = 0; Int32sRef values = new Int32sRef(32); for (int docID = 0; docID < maxDoc; docID++) { Offsets[docID] = (int)totOrds; source.Get(docID, values); long nextLength = totOrds + values.Length; if (nextLength > ords.Length) { if (nextLength > ArrayUtil.MAX_ARRAY_LENGTH) { throw new ThreadStateException("too many ordinals (>= " + nextLength + ") to cache"); } ords = ArrayUtil.Grow(ords, (int)nextLength); } Array.Copy(values.Int32s, 0, ords, (int)totOrds, values.Length); totOrds = nextLength; } Offsets[maxDoc] = (int)totOrds; // if ords array is bigger by more than 10% of what we really need, shrink it if ((double)totOrds / ords.Length < 0.9) { this.Ordinals = new int[(int)totOrds]; Array.Copy(ords, 0, this.Ordinals, 0, (int)totOrds); } else { this.Ordinals = ords; } }
/// <summary> /// Creates a new <seealso cref="CachedOrds"/> from the <seealso cref="BinaryDocValues"/>. /// Assumes that the <seealso cref="BinaryDocValues"/> is not {@code null}. /// </summary> public CachedOrds(OrdinalsSegmentReader source, int maxDoc) { offsets = new int[maxDoc + 1]; int[] ords = new int[maxDoc]; // let's assume one ordinal per-document as an initial size // this aggregator is limited to Integer.MAX_VALUE total ordinals. long totOrds = 0; IntsRef values = new IntsRef(32); for (int docID = 0; docID < maxDoc; docID++) { offsets[docID] = (int)totOrds; source.Get(docID, values); long nextLength = totOrds + values.Length; if (nextLength > ords.Length) { if (nextLength > ArrayUtil.MAX_ARRAY_LENGTH) { throw new ThreadStateException("too many ordinals (>= " + nextLength + ") to cache"); } ords = ArrayUtil.Grow(ords, (int)nextLength); } Array.Copy(values.Ints, 0, ords, (int)totOrds, values.Length); totOrds = nextLength; } offsets[maxDoc] = (int)totOrds; // if ords array is bigger by more than 10% of what we really need, shrink it if ((double)totOrds / ords.Length < 0.9) { this.ordinals = new int[(int)totOrds]; Array.Copy(ords, 0, this.ordinals, 0, (int)totOrds); } else { this.ordinals = ords; } }