private IEnumerable <IMappedBlock> CreateMappedBlocks() { // Create an area for the index block pointer var indexBlockArea = indexSetStore.Store.GetArea(StartOffset); // First create the list of block entries for this list var blocks = new IMappedBlock[(int)blockEntries]; if (blockEntries != 0) { indexBlockArea.Position = 16; for (int i = 0; i < blockEntries; ++i) { // NOTE: We cast to 'int' here because of internal limitations. var firstEntry = indexBlockArea.ReadInt8(); var lastEntry = indexBlockArea.ReadInt8(); var blockPointer = indexBlockArea.ReadInt8(); var typeSize = indexBlockArea.ReadInt4(); //TODO: check this... // size is the first 24 bits (max size = 16MB) int elementCount = typeSize & 0x0FFF; byte type = (byte)(BytesUtil.URShift(typeSize, 24) & 0x0F); blocks[i] = StoreIndex.NewMappedBlock(indexSetStore, firstEntry, lastEntry, blockPointer, elementCount, type, BlockSize); } } return(blocks); }
public long Flush() { // Convert the int[] array to a byte[] array. // First determine how we compact this int array into a byte array. If // all the values are < 32768 then we store as shorts long largestVal = 0; for (int i = 0; i < Count; ++i) { long v = BaseArray[i]; if (System.Math.Abs(v) > System.Math.Abs(largestVal)) { largestVal = v; } } long lv = largestVal; if (lv >> 7 == 0 || lv >> 7 == -1) { CompactType = 1; } else if (lv >> 15 == 0 || lv >> 15 == -1) { CompactType = 2; } else if (lv >> 23 == 0 || lv >> 23 == -1) { CompactType = 3; } // NOTE: in the future we'll want to determine if we are going to store // as an int or long array. else { CompactType = 4; } // The number of bytes per entry int entrySize = CompactType; // The total size of the entry. int areaSize = (Count * entrySize); // Allocate an array to buffer the block to byte[] arr = new byte[areaSize]; // Fill the array int p = 0; for (int i = 0; i < Count; ++i) { int v = BaseArray[i]; for (int n = entrySize - 1; n >= 0; --n) { //TODO: check this... arr[p] = (byte)(BytesUtil.URShift(v, (n * 8)) & 0x0FF); ++p; } } // Create an area to store this using (var a = Store.CreateArea(areaSize)) { BlockPointer = a.Id; a.Write(arr, 0, areaSize); a.Flush(); } // Once written, the block is invalidated blockLock = null; return(BlockPointer); }