MAX_STORAGE_SIZE_SHIFT = shift;
ArrayList<int[]> storageArrays = new ArrayList<int[]>();
TIntArrayList curStorageArray = new TIntArrayList(MAX_STORAGE_SIZE);
int curStorageArrayIndex = 0;
// Two things about this:
// 1) First array must start with 1 as 0 pointer means no value.
// 2) Always points to the next usable place
int curOffsetWithInStorage = 1;
curStorageArray.add(Integer.MIN_VALUE); // first place is wasted.
int maxDoc = ordinalsNoPerDoc.length;
firstLevel = new int[maxDoc];
for (int curDoc = 0; curDoc < maxDoc; curDoc++) {
int curOrdinalNoForDoc = ordinalsNoPerDoc[curDoc];
switch (curOrdinalNoForDoc) {
case 0:
case 1:
break; // nothing to do ordinals will fit in the firstLevel array
default:
if ((curOffsetWithInStorage + curOrdinalNoForDoc) > MAX_STORAGE_SIZE) {
if (curOrdinalNoForDoc > MAX_STORAGE_SIZE - 1) {
throw new ElasticSearchException(
String.format("Number of values for doc %s has a exceeded the maximum allowed " +
"(got %s values, max %s)",
curDoc, curOrdinalNoForDoc, MAX_STORAGE_SIZE - 1));
}
curStorageArrayIndex++;
logger.debug("Allocating a new storage array. {} so far.", curStorageArrayIndex);
storageArrays.add(curStorageArray.toArray());
curOffsetWithInStorage = 1; // for pointer consistency waste a slot.
curStorageArray.clear(MAX_STORAGE_SIZE);
curStorageArray.add(Integer.MIN_VALUE); // first place is wasted.
}
for (int i=0;i< curOrdinalNoForDoc; i++) curStorageArray.add(0); // reserve space.
firstLevel[curDoc] = -((curStorageArrayIndex << MAX_STORAGE_SIZE_SHIFT) + curOffsetWithInStorage);
curOffsetWithInStorage += curOrdinalNoForDoc; // make space for the ordinals.
}
}
// all done. populate final storage space
this.storageArrays = new int[storageArrays.size() + 1][];
for (int i = 0; i < storageArrays.size(); i++) {
this.storageArrays[i] = storageArrays.get(i);
}
this.storageArrays[storageArrays.size()] = curStorageArray.toArray();
logger.debug("Ordinal array loaded. {} docs, {} secondary storage arrays. Memory signature: {}KB",
this.firstLevel.length, this.storageArrays.length, computeSizeInBytes() / 1024);
}