DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
Public Member Functions | Protected Member Functions | Properties | Private Member Functions | Private Attributes | List of all members
Deveel.Data.Index.StoreIndex.MappedBlock Class Reference
Inheritance diagram for Deveel.Data.Index.StoreIndex.MappedBlock:
Deveel.Data.Block Deveel.Data.Index.IMappedBlock Deveel.Data.IBlock Deveel.Data.Index.IIndexBlock< int > Deveel.Data.IRequest Deveel.Data.Diagnostics.IEventSource

Public Member Functions

 MappedBlock (StoreIndex index)
 
 MappedBlock (IndexSetStore indexSetStore, long firstEntry, long lastEntry, long blockPointer, int size, byte compactType, int maxBlockSize)
 
long CopyTo (IStore destStore)
 
long Flush ()
 
void Dispose ()
 
- Public Member Functions inherited from Deveel.Data.Block
 Block (IRequest request)
 
void Dispose ()
 
virtual IBlock CreateBlock ()
 
- Public Member Functions inherited from Deveel.Data.Index.IIndexBlock< int >
bool CanContain (int count)
 
void Add (T value)
 Adss an int element to the block. More...
 
RemoveAt (int index)
 Removes the element at the given index from the block. More...
 
int IndexOf (T value)
 
int IndexOf (T value, int startIndex)
 
void Insert (T index, int value)
 Inserts an element to the block at the given index. More...
 
void MoveTo (IIndexBlock< T > destBlock, int destIndex, int count)
 
void CopyTo (IIndexBlock< T > destBlock)
 
int CopyTo (T[] array, int arrayIndex)
 
void Clear ()
 Clears the block of all elements. More...
 
int BinarySearch (object key, IIndexComparer< T > comparer)
 
int SearchFirst (object key, IIndexComparer< T > comparer)
 
int SearchFirst (T value)
 
int SearchLast (object key, IIndexComparer< T > comparer)
 
int SearchLast (T value)
 

Protected Member Functions

override int[] GetArray (bool readOnly)
 
- Protected Member Functions inherited from Deveel.Data.Block
virtual IEnumerable< KeyValuePair< string, object > > GetEventMetadata ()
 
virtual void Dispose (bool disposing)
 
virtual void ExecuteBlock (BlockExecuteContext context)
 

Properties

IndexSetStore IndexSetStore [get, private set]
 
IStore Store [get]
 
long FirstEntry [get, private set]
 
long LastEntry [get, private set]
 
long BlockPointer [get, private set]
 
byte CompactType [get, private set]
 
override int Top [get]
 
override int Bottom [get]
 
override int ArrayLength [get]
 
- Properties inherited from Deveel.Data.Block
IBlock Parent [get, private set]
 
IBlock Next [get, set]
 
IQuery Query [get]
 
IEventSource IEventSource. ParentSource [get]
 
IEnumerable< KeyValuePair< string, object > > IEventSource. Metadata [get]
 
IBlockContext Context [get, private set]
 
IContext IEventSource. Context [get]
 
- Properties inherited from Deveel.Data.IBlock
new IBlockContext Context [get]
 
IBlock Parent [get]
 
IBlock Next [get]
 
- Properties inherited from Deveel.Data.IRequest
IQuery Query [get]
 
- Properties inherited from Deveel.Data.Diagnostics.IEventSource
IContext Context [get]
 
IEventSource ParentSource [get]
 Gets an optional parent source. More...
 
IEnumerable< KeyValuePair< string, object > > Metadata [get]
 Gets the list of metadata associated to the source. More...
 
- Properties inherited from Deveel.Data.Index.IMappedBlock
long FirstEntry [get]
 
long LastEntry [get]
 
long BlockPointer [get]
 
byte CompactType [get]
 
- Properties inherited from Deveel.Data.Index.IIndexBlock< int >
IIndexBlock< T > Next [get, set]
 Gets or sets the next block in the hash. More...
 
IIndexBlock< T > Previous [get, set]
 Gets or sets the previous block in the hash. More...
 
bool HasChanged [get]
 
int Count [get]
 
bool IsFull [get]
 Gets a value indicating if the block is full. More...
 
bool IsEmpty [get]
 Gets a value indicating if the block is empty. More...
 
Top [get]
 Gets the element at the top of the block. More...
 
Bottom [get]
 Gets the element at the bottom of the block. More...
 
this[int index] [get, set]
 Gets or sets the element at the given index within the block. More...
 

Private Member Functions

void PrepareMutate (bool readOnly)
 

Private Attributes

readonly int maxBlockSize
 
object blockLock = new object()
 
bool mutableBlock
 

Detailed Description

Definition at line 94 of file StoreIndex.cs.

Constructor & Destructor Documentation

Deveel.Data.Index.StoreIndex.MappedBlock.MappedBlock ( StoreIndex  index)
inline

Definition at line 107 of file StoreIndex.cs.

108  : base(index.MaxBlockSize) {
110  maxBlockSize = index.MaxBlockSize;
111  BlockPointer = -1;
112  }
IndexSetStore(IDatabaseContext databaseContext, IStore store)
Deveel.Data.Index.StoreIndex.MappedBlock.MappedBlock ( IndexSetStore  indexSetStore,
long  firstEntry,
long  lastEntry,
long  blockPointer,
int  size,
byte  compactType,
int  maxBlockSize 
)
inline

Definition at line 114 of file StoreIndex.cs.

114  {
115  IndexSetStore = indexSetStore;
116  FirstEntry = firstEntry;
117  LastEntry = lastEntry;
118  BlockPointer = blockPointer;
119  CompactType = compactType;
120 
121  this.maxBlockSize = maxBlockSize;
122  Count = size;
123  BaseArray = null;
124  }

Member Function Documentation

long Deveel.Data.Index.StoreIndex.MappedBlock.CopyTo ( IStore  destStore)
inline

Implements Deveel.Data.Index.IMappedBlock.

Definition at line 168 of file StoreIndex.cs.

168  {
169  // The number of bytes per entry
170  int entrySize = CompactType;
171  // The total size of the entry.
172  int areaSize = (Count * entrySize);
173 
174  // Allocate the destination area
175  var dest = destStore.CreateArea(areaSize);
176  long destOffset = dest.Id;
177  Store.GetArea(BlockPointer).CopyTo(dest, areaSize);
178  dest.Flush();
179 
180  return destOffset;
181  }
IArea CreateArea(long size)
Allocates a block of memory in the store of the specified size and returns an IArea object that can b...
void CopyTo(IArea destArea, int size)
Copies the given amount of bytes from the current position of the this area to another one...
IArea GetArea(long id, bool readOnly)
Returns an object that allows for the contents of an area (represented by the id parameter) to be Re...
long Id
Returns the unique identifier that represents this area.
Definition: IArea.cs:31
void Deveel.Data.Index.StoreIndex.MappedBlock.Dispose ( )
inline

Definition at line 284 of file StoreIndex.cs.

284  {
285  IndexSetStore = null;
286  }
long Deveel.Data.Index.StoreIndex.MappedBlock.Flush ( )
inline

Implements Deveel.Data.Index.IMappedBlock.

Definition at line 183 of file StoreIndex.cs.

183  {
184  // Convert the int[] array to a byte[] array.
185 
186  // First determine how we compact this int array into a byte array. If
187  // all the values are < 32768 then we store as shorts
188  long largestVal = 0;
189  for (int i = 0; i < Count; ++i) {
190  long v = BaseArray[i];
191  if (System.Math.Abs(v) > System.Math.Abs(largestVal)) {
192  largestVal = v;
193  }
194  }
195 
196  long lv = largestVal;
197  if (lv >> 7 == 0 || lv >> 7 == -1) {
198  CompactType = 1;
199  } else if (lv >> 15 == 0 || lv >> 15 == -1) {
200  CompactType = 2;
201  } else if (lv >> 23 == 0 || lv >> 23 == -1) {
202  CompactType = 3;
203  }
204  // NOTE: in the future we'll want to determine if we are going to store
205  // as an int or long array.
206  else {
207  CompactType = 4;
208  }
209 
210  // The number of bytes per entry
211  int entrySize = CompactType;
212  // The total size of the entry.
213  int areaSize = (Count * entrySize);
214 
215  // Allocate an array to buffer the block to
216  byte[] arr = new byte[areaSize];
217  // Fill the array
218  int p = 0;
219  for (int i = 0; i < Count; ++i) {
220  int v = BaseArray[i];
221  for (int n = entrySize - 1; n >= 0; --n) {
222  //TODO: check this...
223  arr[p] = (byte)(ByteBuffer.URShift(v, (n * 8)) & 0x0FF);
224  ++p;
225  }
226  }
227 
228  // Create an area to store this
229  var a = Store.CreateArea(areaSize);
230  BlockPointer = a.Id;
231 
232  a.Write(arr, 0, areaSize);
233  a.Flush();
234 
235  // Once written, the block is invalidated
236  blockLock = null;
237 
238  return BlockPointer;
239  }
IArea CreateArea(long size)
Allocates a block of memory in the store of the specified size and returns an IArea object that can b...
static int URShift(int number, int bits)
Operates a shift on the given integer by the number of bits specified.
Definition: ByteBuffer.cs:277
A wrapper for an array of byte.
Definition: ByteBuffer.cs:27
override int [] Deveel.Data.Index.StoreIndex.MappedBlock.GetArray ( bool  readOnly)
inlineprotected

Definition at line 241 of file StoreIndex.cs.

241  {
242  // We must synchronize this entire block because otherwise we could
243  // return a partially loaded array.
244  lock (blockLock) {
245  if (BaseArray != null) {
246  PrepareMutate(readOnly);
247  return BaseArray;
248  }
249 
250  // Create the int array
251  BaseArray = new int[maxBlockSize];
252 
253  // The number of bytes per entry
254  int entrySize = CompactType;
255  // The total size of the entry.
256  int areaSize = (Count * entrySize);
257 
258  // Read in the byte array
259  byte[] buf = new byte[areaSize];
260  try {
261  Store.GetArea(BlockPointer).Read(buf, 0, areaSize);
262  } catch (IOException e) {
263  throw new InvalidOperationException("IO Error: " + e.Message);
264  }
265 
266  // Uncompact it into the int array
267  int p = 0;
268  for (int i = 0; i < Count; ++i) {
269  int v = (((int)buf[p]) << ((entrySize - 1) * 8));
270  ++p;
271  for (int n = entrySize - 2; n >= 0; --n) {
272  v = v | ((((int)buf[p]) & 0x0FF) << (n * 8));
273  ++p;
274  }
275  BaseArray[i] = v;
276  }
277 
278  mutableBlock = false;
279  PrepareMutate(readOnly);
280  return BaseArray;
281  }
282  }
int Read(byte[] buffer, int offset, int length)
Reads an array of bytes from the underlying IArea and advances the position by length ...
IArea GetArea(long id, bool readOnly)
Returns an object that allows for the contents of an area (represented by the id parameter) to be Re...
void Deveel.Data.Index.StoreIndex.MappedBlock.PrepareMutate ( bool  readOnly)
inlineprivate

Definition at line 160 of file StoreIndex.cs.

160  {
161  // If list is to be mutable
162  if (!readOnly && !mutableBlock) {
163  BaseArray = (int[])BaseArray.Clone();
164  mutableBlock = true;
165  }
166  }

Member Data Documentation

object Deveel.Data.Index.StoreIndex.MappedBlock.blockLock = new object()
private

Definition at line 97 of file StoreIndex.cs.

readonly int Deveel.Data.Index.StoreIndex.MappedBlock.maxBlockSize
private

Definition at line 95 of file StoreIndex.cs.

bool Deveel.Data.Index.StoreIndex.MappedBlock.mutableBlock
private

Definition at line 98 of file StoreIndex.cs.

Property Documentation

override int Deveel.Data.Index.StoreIndex.MappedBlock.ArrayLength
getprotected

Definition at line 156 of file StoreIndex.cs.

long Deveel.Data.Index.StoreIndex.MappedBlock.BlockPointer
getprivate set

Definition at line 130 of file StoreIndex.cs.

override int Deveel.Data.Index.StoreIndex.MappedBlock.Bottom
get

Definition at line 145 of file StoreIndex.cs.

byte Deveel.Data.Index.StoreIndex.MappedBlock.CompactType
getprivate set

Definition at line 132 of file StoreIndex.cs.

long Deveel.Data.Index.StoreIndex.MappedBlock.FirstEntry
getprivate set

Definition at line 126 of file StoreIndex.cs.

IndexSetStore Deveel.Data.Index.StoreIndex.MappedBlock.IndexSetStore
getprivate set

Definition at line 101 of file StoreIndex.cs.

long Deveel.Data.Index.StoreIndex.MappedBlock.LastEntry
getprivate set

Definition at line 128 of file StoreIndex.cs.

IStore Deveel.Data.Index.StoreIndex.MappedBlock.Store
getprivate

Definition at line 103 of file StoreIndex.cs.

override int Deveel.Data.Index.StoreIndex.MappedBlock.Top
get

Definition at line 134 of file StoreIndex.cs.


The documentation for this class was generated from the following file: