DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
Public Member Functions | Package Functions | Properties | Private Member Functions | Private Attributes | List of all members
Deveel.Data.Index.IndexSetStore Class Reference

Public Member Functions

 IndexSetStore (IDatabaseContext databaseContext, IStore store)
 
long Create ()
 
void Open (long startPointer)
 
void Close ()
 
void GetAreasUsed (List< long > list)
 
void PrepareIndexLists (int count, int type, int blockSize)
 
IIndexSet GetSnapshotIndex ()
 
void CommitIndexSet (IIndexSet indexSet)
 
void CommitDropIndex (int indexNum)
 

Package Functions

void DeleteAreas (IEnumerable< int > deletedAreas)
 

Properties

IStore Store [get, private set]
 

Private Member Functions

long CreateNewBlock ()
 
void CommitIndexHeader ()
 

Private Attributes

IArea startArea
 
long indexHeaderPointer
 
IArea indexHeaderArea
 
IndexBlock[] indexBlocks
 
const int Magic = 0x0CA90291
 

Detailed Description

Definition at line 27 of file IndexSetStore.cs.

Constructor & Destructor Documentation

Deveel.Data.Index.IndexSetStore.IndexSetStore ( IDatabaseContext  databaseContext,
IStore  store 
)
inline

Definition at line 39 of file IndexSetStore.cs.

39  {
40  Store = store;
41  }

Member Function Documentation

void Deveel.Data.Index.IndexSetStore.Close ( )
inline

Definition at line 150 of file IndexSetStore.cs.

150  {
151  lock (this) {
152  if (Store != null) {
153  for (int i = 0; i < indexBlocks.Length; ++i) {
155  }
156  Store = null;
157  indexBlocks = null;
158  }
159  }
160  }
void Deveel.Data.Index.IndexSetStore.CommitDropIndex ( int  indexNum)
inline

Definition at line 400 of file IndexSetStore.cs.

400  {
401  lock (this) {
402  // The IndexBlock we are dropping
403  var curIndexBlock = indexBlocks[indexNum];
404  int blockSize = curIndexBlock.BlockSize;
405 
406  try {
407  Store.Lock();
408 
409  // Add all the elements to the deleted areas in the block
410  var allBlockPointers = curIndexBlock.GetBlockPointers();
411  foreach (long area in allBlockPointers) {
412  curIndexBlock.AddDeletedArea(area);
413  }
414 
415  // Mark the current block as deleted
416  curIndexBlock.MarkAsDeleted();
417 
418  // Make up a new blank block list for this index set.
419  long blockP = CreateNewBlock();
420 
421  // Now create a new IndexBlock object
422  var newIndexBlock = new IndexBlock(this, indexNum, blockSize, blockP);
423 
424  // Add reference to the new one
425  newIndexBlock.AddReference();
426  // Remove reference to the old
427  curIndexBlock.RemoveReference();
428  // Update the index_blocks list
429  indexBlocks[indexNum] = newIndexBlock;
430 
431  // Commit the new index header (index_blocks)
433  } finally {
434  Store.Unlock();
435  }
436  }
437  }
void Lock()
This method is called before the start of a sequence of Write commands between consistant states of s...
void Unlock()
This method is called after the end of a sequence of Write commands between consistant states of some...
void Deveel.Data.Index.IndexSetStore.CommitIndexHeader ( )
inlineprivate

Definition at line 257 of file IndexSetStore.cs.

257  {
258  lock (this) {
259  // Make a new index header area for the changed set.
260  var a = Store.CreateArea(16 + (indexBlocks.Length * 16));
261  long aOffset = a.Id;
262 
263  a.WriteInt4(1); // version
264  a.WriteInt4(0); // reserved
265  a.WriteInt8(indexBlocks.Length); // count
266 
267  foreach (var indBlock in indexBlocks) {
268  a.WriteInt4(1);
269  a.WriteInt4(indBlock.BlockSize);
270  a.WriteInt8(indBlock.StartOffset);
271  }
272 
273  // Finish creating the updated header
274  a.Flush();
275 
276  // The old index header pointer
277  long oldIndexHeaderP = indexHeaderPointer;
278 
279  // Set the new index header
280  indexHeaderPointer = aOffset;
282 
283  // Write the change to 'startPointer'
284  startArea.Position = 8;
285  startArea.WriteInt8(indexHeaderPointer);
286  startArea.Flush();
287 
288  // Free the old header index
289  Store.DeleteArea(oldIndexHeaderP);
290  }
291  }
IArea CreateArea(long size)
Allocates a block of memory in the store of the specified size and returns an IArea object that can b...
long Position
Returns or sets the current position of the pointer within the area.
Definition: IArea.cs:48
void DeleteArea(long id)
Deletes an area that was previously allocated by the CreateArea method by the area id...
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.IndexSetStore.CommitIndexSet ( IIndexSet  indexSet)
inline

Definition at line 293 of file IndexSetStore.cs.

293  {
294  var removedBlocks = new List<IndexBlock>();
295 
296  lock (this) {
297  var sIndexSet = (SnapshotIndexSet)indexSet;
298  var indices = sIndexSet.AllIndices.ToList();
299 
300  try {
301  try {
302  Store.Lock();
303 
304  // For each Index in the index set,
305  foreach (var index in indices) {
306  int indexNum = index.IndexNumber;
307 
308  // The IndexBlock we are changing
309  var curIndexBlock = indexBlocks[indexNum];
310 
311  // Get all the blocks in the list
312  var blocks = index.AllBlocks.ToList();
313 
314  // Make up a new block list for this index set.
315  var area = Store.CreateArea(16 + (blocks.Count * 28));
316  long blockP = area.Id;
317  area.WriteInt4(1); // version
318  area.WriteInt4(0); // reserved
319  area.WriteInt8(blocks.Count); // block count
320 
321  foreach (var block in blocks) {
322  var mappedBlock = (IMappedBlock)block;
323 
324  long bottomInt = 0;
325  long topInt = 0;
326  int blockSize = mappedBlock.Count;
327  if (blockSize > 0) {
328  bottomInt = mappedBlock.Bottom;
329  topInt = mappedBlock.Top;
330  }
331 
332  long blockPointer = mappedBlock.BlockPointer;
333 
334  // Is the block new or was it changed?
335  if (blockPointer == -1 || mappedBlock.HasChanged) {
336  // If this isn't -1 then write this sector on the list of
337  // sectors to delete during GC.
338  if (blockPointer != -1)
339  curIndexBlock.AddDeletedArea(blockPointer);
340 
341  // This is a new block or a block that's been changed
342  // Write the block to the file system
343  blockPointer = mappedBlock.Flush();
344  }
345 
346  area.WriteInt8(bottomInt);
347  area.WriteInt8(topInt);
348  area.WriteInt8(blockPointer);
349  area.WriteInt4(blockSize | (((int)mappedBlock.CompactType) << 24));
350  }
351 
352  // Finish initializing the area
353  area.Flush();
354 
355  // Add the deleted blocks
356  var deletedBlocks = index.DeletedBlocks.ToArray();
357  for (int i = 0; i < deletedBlocks.Length; ++i) {
358  long delBlockP = deletedBlocks[i].BlockPointer;
359  if (delBlockP != -1)
360  curIndexBlock.AddDeletedArea(delBlockP);
361  }
362 
363  // Mark the current block as deleted
364  curIndexBlock.MarkAsDeleted();
365 
366  // Now create a new IndexBlock object
367  var newIndexBlock = new IndexBlock(this, indexNum, curIndexBlock.BlockSize, blockP);
368  newIndexBlock.Parent = curIndexBlock;
369 
370  // Add reference to the new one
371  newIndexBlock.AddReference();
372 
373  // Update the index_blocks list
374  indexBlocks[indexNum] = newIndexBlock;
375 
376  // We remove this later.
377  removedBlocks.Add(curIndexBlock);
378  }
379 
380  // Commit the new index header (index_blocks)
382  } finally {
383  Store.Unlock();
384  }
385 
386  // Commit finished.
387 
388  } catch (IOException e) {
389  throw new InvalidOperationException("Error while committing index changed", e);
390  }
391 
392  }
393 
394  // Remove all the references for the changed blocks,
395  foreach (var block in removedBlocks) {
396  block.RemoveReference();
397  }
398  }
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 Lock()
This method is called before the start of a sequence of Write commands between consistant states of s...
void Unlock()
This method is called after the end of a sequence of Write commands between consistant states of some...
long Id
Returns the unique identifier that represents this area.
Definition: IArea.cs:31
long Deveel.Data.Index.IndexSetStore.Create ( )
inline

Definition at line 77 of file IndexSetStore.cs.

77  {
78  lock (this) {
79  // Create an empty index header area
80  var a = Store.CreateArea(16);
81  indexHeaderPointer = a.Id;
82  a.WriteInt4(1); // version
83  a.WriteInt4(0); // reserved
84  a.WriteInt8(0); // number of indexes in the set
85  a.Flush();
86 
87  // Set up the local IArea object for the index header
89 
90  indexBlocks = new IndexBlock[0];
91 
92  // Allocate the starting header
93  var sa = Store.CreateArea(32);
94  long startPointer = sa.Id;
95  // The magic
96  sa.WriteInt4(Magic);
97  // The version
98  sa.WriteInt4(1);
99  // Pointer to the index header
100  sa.WriteInt8(indexHeaderPointer);
101  sa.Flush();
102 
103  // Set the 'start_area' value.
104  startArea = Store.GetArea(startPointer);
105 
106  return startPointer;
107  }
108  }
IArea CreateArea(long size)
Allocates a block of memory in the store of the specified size and returns an IArea object that can b...
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
long Deveel.Data.Index.IndexSetStore.CreateNewBlock ( )
inlineprivate

Definition at line 64 of file IndexSetStore.cs.

64  {
65  // Allocate the area
66  var a = Store.CreateArea(16);
67  long indexBlockP = a.Id;
68  // Setup the header
69  a.WriteInt4(1); // version
70  a.WriteInt4(0); // reserved
71  a.WriteInt8(0); // block entries
72  a.Flush();
73 
74  return indexBlockP;
75  }
IArea CreateArea(long size)
Allocates a block of memory in the store of the specified size and returns an IArea object that can b...
long Id
Returns the unique identifier that represents this area.
Definition: IArea.cs:31
void Deveel.Data.Index.IndexSetStore.DeleteAreas ( IEnumerable< int >  deletedAreas)
inlinepackage

Definition at line 45 of file IndexSetStore.cs.

45  {
46  lock (this) {
47  if (Store != null) {
48  try {
49  Store.Lock();
50 
51  foreach (var id in deletedAreas) {
52  Store.DeleteArea(id);
53  }
54 
55  } catch (IOException) {
56  // TODO: Raise the error to the system
57  } finally {
58  Store.Unlock();
59  }
60  }
61  }
62  }
void Lock()
This method is called before the start of a sequence of Write commands between consistant states of s...
void DeleteArea(long id)
Deletes an area that was previously allocated by the CreateArea method by the area id...
void Unlock()
This method is called after the end of a sequence of Write commands between consistant states of some...
void Deveel.Data.Index.IndexSetStore.GetAreasUsed ( List< long >  list)
inline

Definition at line 162 of file IndexSetStore.cs.

162  {
163  list.Add(startArea.Id);
164  list.Add(indexHeaderPointer);
165 
166  foreach (var block in indexBlocks) {
167  list.Add(block.StartOffset);
168  long[] blockPointers = block.GetBlockPointers();
169  list.AddRange(blockPointers);
170  }
171  }
long Id
Returns the unique identifier that represents this area.
Definition: IArea.cs:31
IIndexSet Deveel.Data.Index.IndexSetStore.GetSnapshotIndex ( )
inline

Definition at line 242 of file IndexSetStore.cs.

242  {
243  lock (this) {
244  // Clone the blocks list. This represents the current snapshot of the
245  // index state.
246  var snapshotIndexBlocks = (IndexBlock[])indexBlocks.Clone();
247 
248  // Add this as the reference
249  for (int i = 0; i < snapshotIndexBlocks.Length; ++i) {
250  snapshotIndexBlocks[i].AddReference();
251  }
252 
253  return new SnapshotIndexSet(this, snapshotIndexBlocks);
254  }
255  }
void Deveel.Data.Index.IndexSetStore.Open ( long  startPointer)
inline

Definition at line 110 of file IndexSetStore.cs.

110  {
111  lock (this) {
112  // Set up the start area
113  startArea = Store.GetArea(startPointer);
114 
115  int magic = startArea.ReadInt4();
116  if (magic != Magic)
117  throw new IOException("Magic value for index set does not match.");
118 
119  int version = startArea.ReadInt4();
120  if (version != 1)
121  throw new IOException("Unknown version for index set.");
122 
123  // Setup the index_header area
124  indexHeaderPointer = startArea.ReadInt8();
126 
127  // Read the index header area
128  version = indexHeaderArea.ReadInt4(); // version
129  if (version != 1)
130  throw new IOException("Incorrect version");
131 
132  indexHeaderArea.ReadInt4(); // reserved
133  int indexCount = (int)indexHeaderArea.ReadInt8();
134  indexBlocks = new IndexBlock[indexCount];
135 
136  // Initialize each index block
137  for (int i = 0; i < indexCount; ++i) {
138  int type = indexHeaderArea.ReadInt4();
139  int blockSize = indexHeaderArea.ReadInt4();
140  long indexBlockPointer = indexHeaderArea.ReadInt8();
141  if (type != 1)
142  throw new IOException("Do not understand index type: " + type);
143 
144  indexBlocks[i] = new IndexBlock(this, i, blockSize, indexBlockPointer);
146  }
147  }
148  }
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.IndexSetStore.PrepareIndexLists ( int  count,
int  type,
int  blockSize 
)
inline

Definition at line 173 of file IndexSetStore.cs.

173  {
174  lock (this) {
175  try {
176  Store.Lock();
177 
178  // Allocate a new area for the list
179  int newSize = 16 + ((indexBlocks.Length + count) * 16);
180  var newIndexArea = Store.CreateArea(newSize);
181  long newIndexPointer = newIndexArea.Id;
182  var newIndexBlocks = new IndexBlock[(indexBlocks.Length + count)];
183 
184  // Copy the existing area
186  int version = indexHeaderArea.ReadInt4();
187  int reserved = indexHeaderArea.ReadInt4();
188  long icount = indexHeaderArea.ReadInt8();
189  newIndexArea.WriteInt4(version);
190  newIndexArea.WriteInt4(reserved);
191  newIndexArea.WriteInt8(icount + count);
192 
193  for (int i = 0; i < indexBlocks.Length; ++i) {
194  int itype = indexHeaderArea.ReadInt4();
195  int iblockSize = indexHeaderArea.ReadInt4();
196  long indexBlockP = indexHeaderArea.ReadInt8();
197 
198  newIndexArea.WriteInt4(itype);
199  newIndexArea.WriteInt4(iblockSize);
200  newIndexArea.WriteInt8(indexBlockP);
201 
202  newIndexBlocks[i] = indexBlocks[i];
203  }
204 
205  // Add the new entries
206  for (int i = 0; i < count; ++i) {
207  long newBlankBlockP = CreateNewBlock();
208 
209  newIndexArea.WriteInt4(type);
210  newIndexArea.WriteInt4(blockSize);
211  newIndexArea.WriteInt8(newBlankBlockP);
212 
213  var newBlock = new IndexBlock(this, indexBlocks.Length + i, blockSize, newBlankBlockP);
214  newBlock.AddReference();
215  newIndexBlocks[indexBlocks.Length + i] = newBlock;
216  }
217 
218  // Finished initializing the index.
219  newIndexArea.Flush();
220 
221  // The old index header pointer
222  long oldIndexHeaderP = indexHeaderPointer;
223 
224  // Update the state of this object,
225  indexHeaderPointer = newIndexPointer;
226  indexHeaderArea = Store.GetArea(newIndexPointer);
227  indexBlocks = newIndexBlocks;
228 
229  // Update the start pointer
230  startArea.Position = 8;
231  startArea.WriteInt8(newIndexPointer);
232  startArea.Flush();
233 
234  // Free the old header
235  Store.DeleteArea(oldIndexHeaderP);
236  } finally {
237  Store.Unlock();
238  }
239  }
240  }
IArea CreateArea(long size)
Allocates a block of memory in the store of the specified size and returns an IArea object that can b...
long Position
Returns or sets the current position of the pointer within the area.
Definition: IArea.cs:48
void Lock()
This method is called before the start of a sequence of Write commands between consistant states of s...
void DeleteArea(long id)
Deletes an area that was previously allocated by the CreateArea method by the area id...
void Unlock()
This method is called after the end of a sequence of Write commands between consistant states of some...
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

Member Data Documentation

IndexBlock [] Deveel.Data.Index.IndexSetStore.indexBlocks
private

Definition at line 33 of file IndexSetStore.cs.

IArea Deveel.Data.Index.IndexSetStore.indexHeaderArea
private

Definition at line 31 of file IndexSetStore.cs.

long Deveel.Data.Index.IndexSetStore.indexHeaderPointer
private

Definition at line 30 of file IndexSetStore.cs.

const int Deveel.Data.Index.IndexSetStore.Magic = 0x0CA90291
private

Definition at line 37 of file IndexSetStore.cs.

IArea Deveel.Data.Index.IndexSetStore.startArea
private

Definition at line 28 of file IndexSetStore.cs.

Property Documentation

IStore Deveel.Data.Index.IndexSetStore.Store
getprivate set

Definition at line 43 of file IndexSetStore.cs.


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