DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
Classes | Public Member Functions | Package Functions | Properties | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
Deveel.Data.Sql.Tables.TableSource Class Reference
Inheritance diagram for Deveel.Data.Sql.Tables.TableSource:
Deveel.Data.Sql.Tables.ITableSource

Classes

class  MinimalTable
 
class  TypeResolver
 

Public Member Functions

bool Exists ()
 
void Close (bool dropPending)
 
bool Drop ()
 
void Open ()
 
IIndexSet CreateIndexSet ()
 
void AddIndex (IndexInfo indexInfo)
 
void Create (TableInfo tableInfo)
 
long GetNextUniqueId ()
 
void SetUniqueId (long value)
 
IMutableTable CreateTableAtCommit (ITransaction transaction)
 
IMutableTable CreateTableAtCommit (ITransaction transaction, TableEventRegistry registry)
 
RecordState WriteRecordState (int rowNumber, RecordState state)
 
RecordState ReadRecordState (int rowNumber)
 
bool IsRecordDeleted (int rowNumber)
 
int AddRow (Row row)
 
void BuildIndexes ()
 
void CopyFrom (int tableId, TableSource destSource, IIndexSet indexSet)
 
void AddLock ()
 
void RemoveLock ()
 
DataObject GetValue (int rowIndex, int columnOffset)
 
IEnumerable< TableEventRegistryFindChangesSinceCmmit (long commitId)
 
void RollbackTransactionChange (TableEventRegistry registry)
 
void MergeChanges (long commitId)
 

Package Functions

 TableSource (TableSourceComposite composite, IStoreSystem storeSystem, IObjectStore objStore, int tableId, string sourceName)
 
void CommitTransactionChange (int commitId, TableEventRegistry change, IIndexSet indexSet)
 
void HardRemoveRow (int rowIndex)
 
bool HardCheckAndReclaimRow (int recordIndex)
 
ColumnIndex CreateColumnIndex (IIndexSet indexSet, ITable table, int columnOffset)
 

Properties

TableSourceComposite Composite [get, private set]
 
IDatabaseContext DatabaseContext [get]
 
IDatabase Database [get]
 
ISystemContext SystemContext [get]
 
IStoreSystem StoreSystem [get, set]
 
int TableId [get, private set]
 
ObjectName TableName [get]
 
string SourceName [get, private set]
 
bool IsRootLocked [get]
 
TableInfo TableInfo [get, private set]
 
int ColumnCount [get]
 
int RawRowCount [get]
 
long CurrentUniqueId [get]
 
bool CanCompact [get]
 
bool IsReadOnly [get]
 
IndexSetInfo IndexSetInfo [get, private set]
 
bool IsClosed [get, protected set]
 
bool HasChangesPending [get]
 
string StoreIdentity [get, private set]
 
IStore Store [get, private set]
 
IObjectStore ObjectStore [get, private set]
 
TableSourceGC GC [get, private set]
 
bool HasShutdown [get, private set]
 
ITableCellCache CellCache [get, set]
 
bool CellCaching [get]
 
- Properties inherited from Deveel.Data.Sql.Tables.ITableSource
int TableId [get]
 
TableInfo TableInfo [get]
 
bool CanCompact [get]
 

Private Member Functions

void ClearLocks ()
 
void ReleaseObjects ()
 
void DoOpeningScan ()
 
void ScanForLeaks ()
 
bool OpenTable ()
 
void CommitIndexSet (IIndexSet indexSet)
 
void SetTableInfo (TableInfo info)
 
void SetIndexSetInfo ()
 
void LoadInternal ()
 
void CreateTable ()
 
void SetupInitialStore ()
 
void ReadStoreHeaders ()
 
void DoHardRowRemove (int rowNumber)
 
void OnDeleteRow (int rowIndex)
 
void RemoveRowFromCache (int rowIndex)
 
void ReleaseRowObjects (long recordPointer)
 
long AddToRecordList (long recordPointer)
 
void GrowRecordList ()
 
int OnAddRow (Row row)
 
long WriteRecord (Row data)
 
void BuildIndex (int indexNumber)
 
ColumnIndex CreateIndexAt (IIndexSet indexSet, ITable table, int indexI)
 
void CheckForCleanup ()
 

Static Private Member Functions

static string MakeSourceIdentity (ISystemContext context, int tableId, string tableName)
 

Private Attributes

IndexSetStore indexSetStore
 
VersionedTableIndexList tableIndices
 
FixedRecordList recordList
 
long indexHeaderOffset
 
long listHeaderOffset
 
IArea headerArea
 
long firstDeleteChainRecord
 
long sequenceId
 
bool isClosed
 
int rootLock
 

Detailed Description

Definition at line 34 of file TableSource.cs.

Constructor & Destructor Documentation

Deveel.Data.Sql.Tables.TableSource.TableSource ( TableSourceComposite  composite,
IStoreSystem  storeSystem,
IObjectStore  objStore,
int  tableId,
string  sourceName 
)
inlinepackage

Definition at line 49 of file TableSource.cs.

49  {
50  if (composite == null)
51  throw new ArgumentNullException("composite");
52 
53  Composite = composite;
54  StoreSystem = storeSystem;
55  ObjectStore = objStore;
56  TableId = tableId;
57  SourceName = sourceName;
58 
59  GC = new TableSourceGC(this);
60 
61  CellCache = composite.DatabaseContext.ResolveService<ITableCellCache>();
62 
63  // Generate the name of the store file name.
64  StoreIdentity = MakeSourceIdentity(composite.DatabaseContext.SystemContext, tableId, sourceName);
65  }
static string MakeSourceIdentity(ISystemContext context, int tableId, string tableName)
Definition: TableSource.cs:465
TableSourceComposite Composite
Definition: TableSource.cs:67

Member Function Documentation

void Deveel.Data.Sql.Tables.TableSource.AddIndex ( IndexInfo  indexInfo)
inline

Definition at line 397 of file TableSource.cs.

397  {
398  lock (this) {
399  // TODO: are there other checks to be done here?
400 
401  IndexSetInfo.AddIndex(indexInfo);
402  }
403  }
void AddIndex(IndexInfo indexInfo)
Definition: IndexSetInfo.cs:58
void Deveel.Data.Sql.Tables.TableSource.AddLock ( )
inline

Definition at line 1264 of file TableSource.cs.

1264  {
1265  lock (this) {
1266  // TODO: Emit the stat to the system
1267  ++rootLock;
1268  }
1269  }
int Deveel.Data.Sql.Tables.TableSource.AddRow ( Row  row)
inline

Implements Deveel.Data.Sql.Tables.ITableSource.

Definition at line 939 of file TableSource.cs.

939  {
940  int rowNumber;
941 
942  lock (this) {
943  rowNumber = OnAddRow(row);
944 
945  } // lock
946 
947  // Update stats
948  // TODO:
949 
950  // Return the record index of the new data in the table
951  return rowNumber;
952  }
long Deveel.Data.Sql.Tables.TableSource.AddToRecordList ( long  recordPointer)
inlineprivate

Definition at line 955 of file TableSource.cs.

955  {
956  lock (recordList) {
957  if (HasShutdown)
958  throw new IOException("IO operation while shutting down.");
959 
960  // If there are no free deleted records input the delete chain,
961  if (firstDeleteChainRecord == -1)
962  // Grow the fixed structure to allow more nodes,
963  GrowRecordList();
964 
965  // Pull free block from the delete chain and recycle it.
966  long recycledRecord = firstDeleteChainRecord;
967  var block = recordList.GetRecord(recycledRecord);
968  var recPos = block.Position;
969 
970  // Status of the recycled block
971  var status = (RecordState) block.ReadInt4();
972  if (status != RecordState.Deleted)
973  throw new InvalidOperationException(String.Format("Record {0} is not deleted. ({1})", recPos, status));
974 
975  // The pointer to the next input the chain.
976  long nextChain = block.ReadInt8();
977  firstDeleteChainRecord = nextChain;
978 
979  try {
980  Store.Lock();
981 
982  // Update the first_delete_chain_record field input the header
984 
985  // Update the block
986  block.Position = recPos;
987  block.WriteInt4((int)RecordState.Uncommitted);
988  block.WriteInt8(recordPointer);
989  block.Flush();
990  } finally {
991  Store.Unlock();
992  }
993 
994  return recycledRecord;
995  }
996  }
A long string in the system.
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...
RecordState
An enumeration that represents the various states of a record.
Definition: RecordState.cs:23
void Unlock()
This method is called after the end of a sequence of Write commands between consistant states of some...
IArea GetRecord(long recordNumber)
void Deveel.Data.Sql.Tables.TableSource.BuildIndex ( int  indexNumber)
inlineprivate

Definition at line 1188 of file TableSource.cs.

1188  {
1189  lock (this) {
1190  var indexSet = CreateIndexSet();
1191 
1192  // Master index is always on index position 0
1193  var masterIndex = indexSet.GetIndex(0);
1194 
1195  // A minimal ITable for constructing the indexes
1196  var minTable = new MinimalTable(this, masterIndex);
1197 
1198  // Set up schemes for the index,
1199  var index = CreateColumnIndex(indexSet, minTable, indexNumber);
1200 
1201  // Rebuild the entire index
1202  int rowCount = RawRowCount;
1203  for (int rowIndex = 0; rowIndex < rowCount; ++rowIndex) {
1204  // If this row isn't deleted, set the index information for it,
1205  if (!IsRecordDeleted(rowIndex))
1206  index.Insert(rowIndex);
1207  }
1208 
1209  // Commit the index
1210  CommitIndexSet(indexSet);
1211  }
1212  }
void CommitIndexSet(IIndexSet indexSet)
Definition: TableSource.cs:405
ColumnIndex CreateColumnIndex(IIndexSet indexSet, ITable table, int columnOffset)
bool IsRecordDeleted(int rowNumber)
Definition: TableSource.cs:803
void Deveel.Data.Sql.Tables.TableSource.BuildIndexes ( )
inline

Implements Deveel.Data.Sql.Tables.ITableSource.

Definition at line 1156 of file TableSource.cs.

1156  {
1157  lock (this) {
1158  var indexSet = CreateIndexSet();
1159 
1160  var indexSetInfo = IndexSetInfo;
1161 
1162  int rowCount = RawRowCount;
1163 
1164  // Master index is always on index position 0
1165  IIndex masterIndex = indexSet.GetIndex(0);
1166 
1167  // First, update the master index
1168  for (int rowIndex = 0; rowIndex < rowCount; ++rowIndex) {
1169  // If this row isn't deleted, set the index information for it,
1170  if (!IsRecordDeleted(rowIndex)) {
1171  // First add to master inde
1172  if (!masterIndex.UniqueInsertSort(rowIndex))
1173  throw new Exception("Master index entry was duplicated.");
1174  }
1175  }
1176 
1177  // Commit the master index
1178  CommitIndexSet(indexSet);
1179 
1180  // Now go ahead and build each index in this table
1181  int indexCount = indexSetInfo.IndexCount;
1182  for (int i = 0; i < indexCount; ++i) {
1183  BuildIndex(i);
1184  }
1185  }
1186  }
void CommitIndexSet(IIndexSet indexSet)
Definition: TableSource.cs:405
An interface for querying and accessing an index of primitive integers.
Definition: IIndex.cs:37
void BuildIndex(int indexNumber)
bool IsRecordDeleted(int rowNumber)
Definition: TableSource.cs:803
void Deveel.Data.Sql.Tables.TableSource.CheckForCleanup ( )
inlineprivate

Definition at line 1288 of file TableSource.cs.

1288  {
1289  lock (this) {
1290  GC.Collect(false);
1291  }
1292  }
void Deveel.Data.Sql.Tables.TableSource.ClearLocks ( )
inlineprivate

Definition at line 178 of file TableSource.cs.

178  {
179  lock (this) {
180  rootLock = 0;
181  }
182  }
void Deveel.Data.Sql.Tables.TableSource.Close ( bool  dropPending)
inline

Definition at line 184 of file TableSource.cs.

184  {
185  if (IsClosed)
186  return;
187 
188  lock (this) {
189  // NOTE: This method MUST be synchronized over the table to prevent
190  // establishing a root Lock on this table. If a root Lock is established
191  // then the collection event could fail.
192 
193  lock (recordList) {
194  // If we are root locked, we must become un root locked.
195  ClearLocks();
196 
197  try {
198  try {
199  Store.Lock();
200 
201  // Force a garbage collection event.
202  if (!IsReadOnly)
203  GC.Collect(true);
204 
205  // If we are closing pending a drop, we need to remove all blob
206  // references input the table.
207  // NOTE: This must only happen after the above collection event.
208  if (dropPending) {
209  // Scan and remove all blob references for this dropped table.
210  ReleaseObjects();
211  }
212  } finally {
213  Store.Unlock();
214  }
215  } catch (Exception) {
216  // TODO: Register the error to the logs
217  }
218 
219  // Synchronize the store
221 
222  // Close the store input the store system.
224 
225  TableInfo = null;
226  IsClosed = true;
227  }
228  }
229  }
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...
bool CloseStore(IStore store)
Closes a store that has been either created or opened with the CreateStore or OpenStore methods...
void Deveel.Data.Sql.Tables.TableSource.CommitIndexSet ( IIndexSet  indexSet)
inlineprivate

Definition at line 405 of file TableSource.cs.

405  {
406  indexSetStore.CommitIndexSet(indexSet);
407  indexSet.Dispose();
408  }
void CommitIndexSet(IIndexSet indexSet)
void Deveel.Data.Sql.Tables.TableSource.CommitTransactionChange ( int  commitId,
TableEventRegistry  change,
IIndexSet  indexSet 
)
inlinepackage

Definition at line 696 of file TableSource.cs.

696  {
697  lock (this) {
698  // ASSERT: Can't do this if source is Read only.
699  if (IsReadOnly)
700  throw new InvalidOperationException("Can't commit transaction journal, table is Read only.");
701 
702  change.CommitId = commitId;
703 
704  try {
705  // Add this registry to the multi version table indices log
706  tableIndices.AddRegistry(change);
707 
708  // Write the modified index set to the index store
709  // (Updates the index file)
710  CommitIndexSet(indexSet);
711 
712  // Update the state of the committed added data to the file system.
713  // (Updates data to the allocation file)
714  //
715  // ISSUE: This can add up to a lot of changes to the allocation file and
716  // the runtime could potentially be terminated in the middle of
717  // the update. If an interruption happens the allocation information
718  // may be incorrectly flagged. The type of corruption this would
719  // result in would be;
720  // + From an 'update' the updated record may disappear.
721  // + From a 'delete' the deleted record may not delete.
722  // + From an 'insert' the inserted record may not insert.
723  //
724  // Note, the possibility of this type of corruption occuring has been
725  // minimized as best as possible given the current architecture.
726  // Also note that is not possible for a table file to become corrupted
727  // beyond recovery from this issue.
728 
729  foreach (var entry in change) {
730  if (entry is TableRowEvent) {
731  var rowEvent = (TableRowEvent) entry;
732  var rowIndex = rowEvent.RowNumber;
733 
734  if (rowEvent.EventType == TableRowEventType.Add) {
735  // Record commit added
736  var oldType = WriteRecordState(rowIndex, RecordState.CommittedAdded);
737 
738  // Check the record was in an uncommitted state before we changed
739  // it.
740  if (oldType != RecordState.Uncommitted) {
741  WriteRecordState(rowIndex, oldType);
742  throw new InvalidOperationException(String.Format("Record {0} of table {1} was not in an uncommitted state!",
743  rowIndex, TableName));
744  }
745  } else if (rowEvent.EventType == TableRowEventType.Remove) {
746  // Record commit removed
747  var oldType = WriteRecordState(rowIndex, RecordState.CommittedRemoved);
748 
749  // Check the record was in an added state before we removed it.
750  if (oldType != RecordState.CommittedAdded) {
751  WriteRecordState(rowIndex, oldType);
752  throw new InvalidOperationException(String.Format("Record {0} of table {1} was not in an added state!", rowIndex,
753  TableName));
754  }
755 
756  // Notify collector that this row has been marked as deleted.
757  GC.DeleteRow(rowIndex);
758  }
759  }
760  }
761  } catch (IOException e) {
762  throw new InvalidOperationException("IO Error: " + e.Message, e);
763  }
764 
765  }
766  }
A long string in the system.
void CommitIndexSet(IIndexSet indexSet)
Definition: TableSource.cs:405
RecordState WriteRecordState(int rowNumber, RecordState state)
Definition: TableSource.cs:768
RecordState
An enumeration that represents the various states of a record.
Definition: RecordState.cs:23
VersionedTableIndexList tableIndices
Definition: TableSource.cs:36
TableRowEventType
The kind of events that can happen on a table row during the life-time of a transaction.
void Deveel.Data.Sql.Tables.TableSource.CopyFrom ( int  tableId,
TableSource  destSource,
IIndexSet  indexSet 
)
inline

Definition at line 1260 of file TableSource.cs.

1260  {
1261  throw new NotImplementedException();
1262  }
void Deveel.Data.Sql.Tables.TableSource.Create ( TableInfo  tableInfo)
inline

Definition at line 452 of file TableSource.cs.

452  {
453  // Set the data table info object
454  SetTableInfo(tableInfo);
455 
456  // Load internal state
457  LoadInternal();
458 
459  // Set up internal state of this object
460  //TableId = tableInfo.Id;
461 
462  CreateTable();
463  }
void SetTableInfo(TableInfo info)
Definition: TableSource.cs:410
ColumnIndex Deveel.Data.Sql.Tables.TableSource.CreateColumnIndex ( IIndexSet  indexSet,
ITable  table,
int  columnOffset 
)
inlinepackage

Definition at line 1214 of file TableSource.cs.

1214  {
1215  lock (this) {
1216  var column = TableInfo[columnOffset];
1217  if (!column.IsIndexable ||
1218  (String.IsNullOrEmpty(column.IndexType) ||
1219  column.IndexType.Equals(DefaultIndexTypes.BlindSearch)))
1220  return new BlindSearchIndex(table, columnOffset);
1221 
1222  var indexI = IndexSetInfo.FindIndexForColumns(new[] {column.ColumnName});
1223  return CreateIndexAt(indexSet, table, indexI);
1224  }
1225  }
A long string in the system.
int FindIndexForColumns(string[] columnNames)
Definition: IndexSetInfo.cs:99
ColumnIndex CreateIndexAt(IIndexSet indexSet, ITable table, int indexI)
ColumnIndex Deveel.Data.Sql.Tables.TableSource.CreateIndexAt ( IIndexSet  indexSet,
ITable  table,
int  indexI 
)
inlineprivate

Definition at line 1227 of file TableSource.cs.

1227  {
1228  lock (this) {
1229  try {
1230  // Get the IndexDef object
1231  var indexInfo = IndexSetInfo.GetIndex(indexI);
1232 
1233  if (indexInfo == null)
1234  return null;
1235 
1236  string[] cols = indexInfo.ColumnNames;
1237  var tableInfo = TableInfo;
1238  if (cols.Length != 1)
1239  throw new Exception("Multi-column indexes not supported at this time.");
1240 
1241  // If a single column
1242  var colIndex = tableInfo.IndexOfColumn(cols[0]);
1243 
1244  if (indexInfo.IndexType.Equals(DefaultIndexTypes.InsertSearch)) {
1245  // Get the index from the index set and set up the new InsertSearch
1246  // scheme.
1247  var indexList = indexSet.GetIndex(indexInfo.Offset);
1248  return new InsertSearchIndex(table, colIndex, indexList);
1249  }
1250 
1251  // TODO: support metadata from input
1252  return SystemContext.CreateColumnIndex(indexInfo.IndexType, table, colIndex);
1253  } catch (Exception ex) {
1254  throw new InvalidOperationException(
1255  String.Format("An error occurred while creating a colummn for table {0}", TableName), ex);
1256  }
1257  }
1258  }
A long string in the system.
IndexInfo GetIndex(int offset)
Definition: IndexSetInfo.cs:65
IIndex GetIndex(int index)
Gets a mutable implementation of IIndex for the given index number in this set of indices...
IIndexSet Deveel.Data.Sql.Tables.TableSource.CreateIndexSet ( )
inline

Implements Deveel.Data.Sql.Tables.ITableSource.

Definition at line 393 of file TableSource.cs.

393  {
395  }
void Deveel.Data.Sql.Tables.TableSource.CreateTable ( )
inlineprivate

Definition at line 487 of file TableSource.cs.

487  {
488  // Initially set the table sequence_id to 1
489  sequenceId = 1;
490 
491  // Create and open the store.
493 
494  try {
495  Store.Lock();
496 
497  // Setup the list structure
498  recordList = new FixedRecordList(Store, 12);
499  } finally {
500  Store.Unlock();
501  }
502 
503  // Initialize the store to an empty state,
506  }
IStore CreateStore(String name)
Creates and returns a new persistent Store object given the unique name of the store.
void PrepareIndexLists(int count, int type, int blockSize)
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...
int ColumnCount
Gets a count of the columns defined by this object.
Definition: TableInfo.cs:159
IMutableTable Deveel.Data.Sql.Tables.TableSource.CreateTableAtCommit ( ITransaction  transaction)
inline

Implements Deveel.Data.Sql.Tables.ITableSource.

Definition at line 688 of file TableSource.cs.

688  {
689  return CreateTableAtCommit(transaction, new TableEventRegistry(this));
690  }
IMutableTable CreateTableAtCommit(ITransaction transaction)
Definition: TableSource.cs:688
IMutableTable Deveel.Data.Sql.Tables.TableSource.CreateTableAtCommit ( ITransaction  transaction,
TableEventRegistry  registry 
)
inline

Definition at line 692 of file TableSource.cs.

692  {
693  return new TransactionTable(transaction, this, registry);
694  }
void Deveel.Data.Sql.Tables.TableSource.DoHardRowRemove ( int  rowNumber)
inlineprivate

Definition at line 808 of file TableSource.cs.

808  {
809  lock (this) {
810  // Internally delete the row,
811  OnDeleteRow(rowNumber);
812 
813  // Update stats
814  //TODO:
815  }
816  }
void Deveel.Data.Sql.Tables.TableSource.DoOpeningScan ( )
inlineprivate

Definition at line 267 of file TableSource.cs.

267  {
268  lock (this) {
269  // ASSERTION: No root locks and no pending transaction changes,
270  // VERY important we assert there's no pending transactions.
272  // This shouldn't happen if we are calling from 'open'.
273  throw new Exception("Odd, we are root locked or have pending journal changes.");
274 
275  // This is pointless if we are in Read only mode.
276  if (!IsReadOnly) {
277  // Get the master index of rows in this table
278  var indexSet = CreateIndexSet();
279  var masterIndex = indexSet.GetIndex(0);
280 
281  // NOTE: We assume the index information is correct and that the
282  // allocation information is potentially bad.
283 
284  int rowCount = RawRowCount;
285  for (int rowNumber = 0; rowNumber < rowCount; ++rowNumber) {
286  // Is this record marked as deleted?
287  if (!IsRecordDeleted(rowNumber)) {
288  // Get the type flags for this record.
289  var type = ReadRecordState(rowNumber);
290 
291  // Check if this record is marked as committed removed, or is an
292  // uncommitted record.
293  if (type == RecordState.CommittedRemoved ||
294  type == RecordState.Uncommitted) {
295  // Check it's not in the master index...
296  if (!masterIndex.Contains(rowNumber)) {
297  // Delete it.
298  DoHardRowRemove(rowNumber);
299  } else {
300  // Mark the row as committed added because it is in the index.
301  WriteRecordState(rowNumber, RecordState.CommittedAdded);
302 
303  }
304  } else {
305  // Must be committed added. Check it's indexed.
306  if (!masterIndex.Contains(rowNumber)) {
307  // Not indexed, so data is inconsistant.
308 
309  // Mark the row as committed removed because it is not in the
310  // index.
311  WriteRecordState(rowNumber, RecordState.CommittedRemoved);
312 
313  }
314  }
315  } else {
316  // if deleted
317  // Check this record isn't in the master index.
318  if (masterIndex.Contains(rowNumber)) {
319  // It's in the master index which is wrong! We should remake the
320  // indices.
321 
322  // Mark the row as committed added because it is in the index.
323  WriteRecordState(rowNumber, RecordState.CommittedAdded);
324 
325  }
326  }
327  } // for (int i = 0 ; i < row_count; ++i)
328 
329  // Dispose the index set
330  indexSet.Dispose();
331  }
332 
333  ScanForLeaks();
334  }
335  }
RecordState WriteRecordState(int rowNumber, RecordState state)
Definition: TableSource.cs:768
RecordState
An enumeration that represents the various states of a record.
Definition: RecordState.cs:23
void DoHardRowRemove(int rowNumber)
Definition: TableSource.cs:808
bool IsRecordDeleted(int rowNumber)
Definition: TableSource.cs:803
RecordState ReadRecordState(int rowNumber)
Definition: TableSource.cs:794
bool Deveel.Data.Sql.Tables.TableSource.Drop ( )
inline

Definition at line 247 of file TableSource.cs.

247  {
248  throw new NotImplementedException();
249  }
bool Deveel.Data.Sql.Tables.TableSource.Exists ( )
inline

Definition at line 174 of file TableSource.cs.

174  {
176  }
bool StoreExists(String name)
Returns true if the store with the given name exists within the system, or false otherwise.
IEnumerable<TableEventRegistry> Deveel.Data.Sql.Tables.TableSource.FindChangesSinceCmmit ( long  commitId)
inline

Definition at line 1502 of file TableSource.cs.

1502  {
1503  lock (this) {
1504  return tableIndices.FindSinceCommit(commitId);
1505  }
1506  }
VersionedTableIndexList tableIndices
Definition: TableSource.cs:36
IEnumerable< TableEventRegistry > FindSinceCommit(long commitId)
long Deveel.Data.Sql.Tables.TableSource.GetNextUniqueId ( )
inline

Implements Deveel.Data.Sql.Tables.ITableSource.

Definition at line 643 of file TableSource.cs.

643  {
644  lock (recordList) {
645  long v = sequenceId;
646  ++sequenceId;
647  if (HasShutdown)
648  throw new Exception("IO operation while shutting down.");
649 
650  try {
651  try {
652  Store.Lock();
653  headerArea.Position = 4 + 4;
654  headerArea.WriteInt8(sequenceId);
655  headerArea.Flush();
656  } finally {
657  Store.Unlock();
658  }
659  } catch (IOException e) {
660  throw new InvalidOperationException("IO Error: " + e.Message);
661  }
662 
663  return v;
664  }
665  }
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 Unlock()
This method is called after the end of a sequence of Write commands between consistant states of some...
DataObject Deveel.Data.Sql.Tables.TableSource.GetValue ( int  rowIndex,
int  columnOffset 
)
inline

Definition at line 1294 of file TableSource.cs.

1294  {
1295  // NOTES:
1296  // This is called *A LOT*. It's a key part of the 20% of the program
1297  // that's run 80% of the time.
1298  // This performs very nicely for rows that are completely contained within
1299  // 1 sector. However, rows that contain large cells (eg. a large binary
1300  // or a large string) and spans many sectors will not be utilizing memory
1301  // as well as it could.
1302  // The reason is because all the data for a row is Read from the store even
1303  // if only 1 cell of the column is requested. This will have a big
1304  // impact on column scans and searches. The cell cache takes some of this
1305  // performance bottleneck away.
1306  // However, a better implementation of this method is made difficult by
1307  // the fact that sector spans can be compressed. We should perhaps
1308  // revise the low level data storage so only sectors can be compressed.
1309 
1310  // First check if this is within the cache before we continue.
1311  DataObject cell;
1312  if (CellCaching) {
1313  if (CellCache.TryGetValue(Database.Name, TableId, rowIndex, columnOffset, out cell))
1314  return cell;
1315  }
1316 
1317  // We maintain a cache of byte[] arrays that contain the rows Read input
1318  // from the file. If consecutive reads are made to the same row, then
1319  // this will cause lots of fast cache hits.
1320 
1321  long recordPointer = -1;
1322  try {
1323  lock (recordList) {
1324  // Increment the file hits counter
1325  //TODO:
1326  //++sRunFileHits;
1327 
1328  //if (sRunFileHits >= 100) {
1329  // // TODO: Register the stats
1330  // sRunFileHits = 0;
1331  //}
1332 
1333  // Get the node for the record
1334  var listBlock = recordList.GetRecord(rowIndex);
1335  var status = (RecordState) listBlock.ReadInt4();
1336  // Check it's not deleted
1337  if (status == RecordState.Deleted)
1338  throw new InvalidOperationException(String.Format("Record {0} was deleted: unable to read.", rowIndex));
1339 
1340  // Get the pointer to the record we are reading
1341  recordPointer = listBlock.ReadInt8();
1342  }
1343 
1344  // Open a stream to the record
1345  using (var stream = Store.GetAreaInputStream(recordPointer)) {
1346  var reader = new BinaryReader(stream);
1347 
1348  stream.Seek(4 + (columnOffset * 8), SeekOrigin.Current);
1349 
1350  int cellType = reader.ReadInt32();
1351  int cellOffset = reader.ReadInt32();
1352 
1353  int curAt = 8 + 4 + (columnOffset * 8);
1354  int beAt = 4 + (ColumnCount * 8);
1355  int skipAmount = (beAt - curAt) + cellOffset;
1356 
1357  stream.Seek(skipAmount, SeekOrigin.Current);
1358 
1359  // Get the TType for this column
1360  // NOTE: It's possible this call may need optimizing?
1361  var type = TableInfo[columnOffset].ColumnType;
1362 
1363  ISqlObject ob;
1364  if (cellType == 1) {
1365  // If standard object type
1366  ob = type.DeserializeObject(stream);
1367  } else if (cellType == 2) {
1368  // If reference to a blob input the BlobStore
1369  int fType = reader.ReadInt32();
1370  int fReserved = reader.ReadInt32();
1371  long refId = reader.ReadInt64();
1372 
1373  if (fType == 0) {
1374  // Resolve the reference
1375  var objRef = ObjectStore.GetObject(refId);
1376  ob = type.CreateFromLargeObject(objRef);
1377  } else if (fType == 1) {
1378  ob = null;
1379  } else {
1380  throw new Exception("Unknown blob type.");
1381  }
1382  } else {
1383  throw new Exception("Unrecognised cell type input data.");
1384  }
1385 
1386  // Wrap it around a TObject
1387  cell = new DataObject(type, ob);
1388 
1389  // And close the reader.
1390 #if PCL
1391  reader.Dispose();
1392 #else
1393  reader.Close();
1394 #endif
1395  }
1396  } catch (IOException e) {
1397  throw new Exception(String.Format("Error getting cell at ({0}, {1}) pointer = " + recordPointer + ".", rowIndex,
1398  columnOffset), e);
1399  }
1400 
1401  // And WriteByte input the cache and return it.
1402  if (CellCaching) {
1403  CellCache.Set(Database.Name, TableId, rowIndex, columnOffset, cell);
1404  }
1405 
1406  return cell;
1407  }
bool TryGetValue(CellKey key, out DataObject value)
A long string in the system.
ILargeObject GetObject(ObjectId id)
Gets an object that was previously created for the given unique identifier.
Definition: ObjectStore.cs:490
string Name
Gets the name of the database.
Definition: IDatabase.cs:44
RecordState
An enumeration that represents the various states of a record.
Definition: RecordState.cs:23
Defines the contract for a valid SQL Object
Definition: ISqlObject.cs:23
IArea GetRecord(long recordNumber)
void Deveel.Data.Sql.Tables.TableSource.GrowRecordList ( )
inlineprivate

Definition at line 998 of file TableSource.cs.

998  {
999  try {
1000  Store.Lock();
1001 
1002  // Increase the size of the list structure.
1004 
1005  // The start record of the new size
1006  int newBlockNumber = recordList.BlockCount - 1;
1007  long startIndex = recordList.BlockFirstPosition(newBlockNumber);
1008  long sizeOfBlock = recordList.BlockNodeCount(newBlockNumber);
1009 
1010  // The IArea object for the new position
1011  var a = recordList.GetRecord(startIndex);
1012 
1013  // Set the rest of the block as deleted records
1014  for (long n = 0; n < sizeOfBlock - 1; ++n) {
1015  a.WriteInt4((int)RecordState.Deleted);
1016  a.WriteInt8(startIndex + n + 1);
1017  }
1018 
1019  // The last block is end of delete chain.
1020  a.WriteInt4((int)RecordState.Deleted);
1021  a.WriteInt8(firstDeleteChainRecord);
1022  a.Flush();
1023 
1024  // And set the new delete chain
1025  firstDeleteChainRecord = startIndex;
1026 
1027  // Set the reserved area
1029  } finally {
1030  Store.Unlock();
1031  }
1032  }
long BlockFirstPosition(int blockNumber)
void Lock()
This method is called before the start of a sequence of Write commands between consistant states of s...
RecordState
An enumeration that represents the various states of a record.
Definition: RecordState.cs:23
void Unlock()
This method is called after the end of a sequence of Write commands between consistant states of some...
long BlockNodeCount(int blockNumber)
IArea GetRecord(long recordNumber)
bool Deveel.Data.Sql.Tables.TableSource.HardCheckAndReclaimRow ( int  recordIndex)
inlinepackage

Definition at line 833 of file TableSource.cs.

833  {
834  lock (this) {
835  // ASSERTION: We are not under a root Lock.
836  if (IsRootLocked)
837  throw new InvalidOperationException("Assertion failed: Can't remove row, table is under a root Lock.");
838 
839  // Row already deleted?
840  if (IsRecordDeleted(recordIndex))
841  return false;
842 
843  var typeKey = ReadRecordState(recordIndex);
844 
845  // Check this record is marked as committed removed.
846  if (typeKey != RecordState.CommittedRemoved)
847  return false;
848 
849  DoHardRowRemove(recordIndex);
850  return true;
851  }
852  }
RecordState
An enumeration that represents the various states of a record.
Definition: RecordState.cs:23
void DoHardRowRemove(int rowNumber)
Definition: TableSource.cs:808
bool IsRecordDeleted(int rowNumber)
Definition: TableSource.cs:803
RecordState ReadRecordState(int rowNumber)
Definition: TableSource.cs:794
void Deveel.Data.Sql.Tables.TableSource.HardRemoveRow ( int  rowIndex)
inlinepackage

Definition at line 818 of file TableSource.cs.

818  {
819  lock (this) {
820  // ASSERTION: We are not under a root Lock.
821  if (IsRootLocked)
822  throw new InvalidOperationException("Cannot remove row, table is locked");
823 
824  var typeKey = ReadRecordState(rowIndex);
825  // Check this record is marked as committed removed.
826  if (typeKey != RecordState.CommittedRemoved)
827  throw new InvalidOperationException(String.Format("The row {0} is not marked as committed removed", rowIndex));
828 
829  DoHardRowRemove(rowIndex);
830  }
831  }
A long string in the system.
RecordState
An enumeration that represents the various states of a record.
Definition: RecordState.cs:23
void DoHardRowRemove(int rowNumber)
Definition: TableSource.cs:808
RecordState ReadRecordState(int rowNumber)
Definition: TableSource.cs:794
bool Deveel.Data.Sql.Tables.TableSource.IsRecordDeleted ( int  rowNumber)
inline

Definition at line 803 of file TableSource.cs.

803  {
804  var state = ReadRecordState(rowNumber);
805  return state == RecordState.Deleted;
806  }
RecordState
An enumeration that represents the various states of a record.
Definition: RecordState.cs:23
RecordState ReadRecordState(int rowNumber)
Definition: TableSource.cs:794
void Deveel.Data.Sql.Tables.TableSource.LoadInternal ( )
inlineprivate

Definition at line 444 of file TableSource.cs.

444  {
445  lock (this) {
446  // Set up the stat keys.
447  // TODO:
448  isClosed = false;
449  }
450  }
static string Deveel.Data.Sql.Tables.TableSource.MakeSourceIdentity ( ISystemContext  context,
int  tableId,
string  tableName 
)
inlinestaticprivate

Definition at line 465 of file TableSource.cs.

465  {
466  string str = tableName.Replace('.', '_').ToLower();
467 
468  // Go through each character and remove each non a-z,A-Z,0-9,_ character.
469  // This ensure there are no strange characters in the file name that the
470  // underlying OS may not like.
471  StringBuilder osifiedName = new StringBuilder();
472  int count = 0;
473  for (int i = 0; i < str.Length || count > 64; ++i) {
474  char c = str[i];
475  if ((c >= 'a' && c <= 'z') ||
476  (c >= 'A' && c <= 'Z') ||
477  (c >= '0' && c <= '9') ||
478  c == '_') {
479  osifiedName.Append(c);
480  ++count;
481  }
482  }
483 
484  return String.Format("{0}_{1}", tableId, osifiedName);
485  }
A long string in the system.
void Deveel.Data.Sql.Tables.TableSource.MergeChanges ( long  commitId)
inline

Definition at line 1540 of file TableSource.cs.

1540  {
1541  lock (this) {
1542  bool allMerged = tableIndices.MergeChanges(commitId);
1543  // If all journal entries merged then schedule deleted row collection.
1544  if (allMerged && !IsReadOnly) {
1545  CheckForCleanup();
1546  }
1547  }
1548  }
VersionedTableIndexList tableIndices
Definition: TableSource.cs:36
int Deveel.Data.Sql.Tables.TableSource.OnAddRow ( Row  row)
inlineprivate

Definition at line 1034 of file TableSource.cs.

1034  {
1035  long rowNumber;
1036  int intRowNumber;
1037 
1038  // Write the record to the store.
1039  lock (recordList) {
1040  long recordPointer = WriteRecord(row);
1041 
1042  // Now add this record into the record block list,
1043  rowNumber = AddToRecordList(recordPointer);
1044  intRowNumber = (int)rowNumber;
1045  }
1046 
1047  // Update the cell cache as appropriate
1048  if (CellCaching && row.CanBeCached) {
1049  int rowCells = row.ColumnCount;
1050  for (int i = 0; i < rowCells; ++i) {
1051  // Put the row/column/TObject into the cache.
1052  CellCache.Set(Database.Name, TableId, intRowNumber, i, row.GetValue(i));
1053  }
1054  }
1055 
1056  // Return the record index of the new data input the table
1057  // NOTE: We are casting this from a long to int which means we are limited
1058  // to ~2 billion record references.
1059  return (int)rowNumber;
1060  }
string Name
Gets the name of the database.
Definition: IDatabase.cs:44
long AddToRecordList(long recordPointer)
Definition: TableSource.cs:955
void Deveel.Data.Sql.Tables.TableSource.OnDeleteRow ( int  rowIndex)
inlineprivate

Definition at line 854 of file TableSource.cs.

854  {
855  lock (recordList) {
856  if (HasShutdown)
857  throw new IOException("IO operation while VM shutting down.");
858 
859  // Find the record entry input the block list.
860  var blockArea = recordList.GetRecord(rowIndex);
861  var p = blockArea.Position;
862  var status = (RecordState) blockArea.ReadInt4();
863 
864  // Check it is not already deleted
865  if (status == RecordState.Deleted)
866  throw new IOException("Record is already marked as deleted.");
867 
868  long recordPointer = blockArea.ReadInt8();
869 
870  // Update the status record.
871  try {
872  Store.Lock();
873 
874  blockArea.Position = p;
875  blockArea.WriteInt4((int)RecordState.Deleted);
876  blockArea.WriteInt8(firstDeleteChainRecord);
877  blockArea.Flush();
878  firstDeleteChainRecord = rowIndex;
879 
880  // Update the first_delete_chain_record field input the header
882 
883  // If the record contains any references to blobs, remove the reference
884  // here.
885  ReleaseRowObjects(recordPointer);
886 
887  // Free the record from the store
888  Store.DeleteArea(recordPointer);
889  } finally {
890  RemoveRowFromCache(rowIndex);
891  Store.Unlock();
892  }
893  }
894  }
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...
RecordState
An enumeration that represents the various states of a record.
Definition: RecordState.cs:23
void Unlock()
This method is called after the end of a sequence of Write commands between consistant states of some...
void ReleaseRowObjects(long recordPointer)
Definition: TableSource.cs:905
void RemoveRowFromCache(int rowIndex)
Definition: TableSource.cs:896
IArea GetRecord(long recordNumber)
void Deveel.Data.Sql.Tables.TableSource.Open ( )
inline

Definition at line 251 of file TableSource.cs.

251  {
252  bool needsCheck = OpenTable();
253 
254  // Create table indices
255  tableIndices = new VersionedTableIndexList(this);
256 
257  // Load internal state
258  LoadInternal();
259 
260  if (needsCheck) {
261  // Do an opening scan of the table. Any records that are uncommited
262  // must be marked as deleted.
263  DoOpeningScan();
264  }
265  }
VersionedTableIndexList tableIndices
Definition: TableSource.cs:36
bool Deveel.Data.Sql.Tables.TableSource.OpenTable ( )
inlineprivate

Definition at line 379 of file TableSource.cs.

379  {
380  // Open the store.
382  bool needCheck = !Store.ClosedClean;
383 
384  // Setup the list structure
385  recordList = new FixedRecordList(Store, 12);
386 
387  // Read and setup the pointers
389 
390  return needCheck;
391  }
bool ClosedClean
Indicates if the store was closed cleanly last time was accessed.
Definition: IStore.cs:187
IStore OpenStore(String name)
Opens an existing persistent Store object in the system and returns the IStore object that contains i...
RecordState Deveel.Data.Sql.Tables.TableSource.ReadRecordState ( int  rowNumber)
inline

Definition at line 794 of file TableSource.cs.

794  {
795  lock (recordList) {
796  // Find the record entry input the block list.
797  var blockArea = recordList.GetRecord(rowNumber);
798  // Get the status.
799  return (RecordState) blockArea.ReadInt4();
800  }
801  }
RecordState
An enumeration that represents the various states of a record.
Definition: RecordState.cs:23
IArea GetRecord(long recordNumber)
void Deveel.Data.Sql.Tables.TableSource.ReadStoreHeaders ( )
inlineprivate

Definition at line 580 of file TableSource.cs.

580  {
581  // Read the fixed header
582  var fixedArea = Store.GetArea(-1);
583 
584  // Set the header area
585  headerArea = Store.GetArea(fixedArea.ReadInt8());
586 
587  // Open a stream to the header
588  var version = headerArea.ReadInt4(); // version
589  if (version != 1)
590  throw new IOException("Incorrect version identifier.");
591 
592  TableId = headerArea.ReadInt4(); // table_id
593  sequenceId = headerArea.ReadInt8(); // sequence id
594  long infoPointer = headerArea.ReadInt8(); // pointer to DataTableInfo
595  long indexInfoPointer = headerArea.ReadInt8(); // pointer to DataIndexSetInfo
596  indexHeaderOffset = headerArea.ReadInt8(); // pointer to index header
597  listHeaderOffset = headerArea.ReadInt8(); // pointer to list header
598 
599  // Read the table info
600  using (var stream = Store.GetAreaInputStream(infoPointer)) {
601  var reader = new BinaryReader(stream, Encoding.Unicode);
602  version = reader.ReadInt32();
603  if (version != 1)
604  throw new IOException("Incorrect TableInfo version identifier.");
605 
606  var userTypeResolver = new TypeResolver(Database);
607  TableInfo = TableInfo.Deserialize(stream, userTypeResolver);
609  }
610 
611  // Read the data index set info
612  using (var stream = Store.GetAreaInputStream(indexInfoPointer)) {
613  var reader = new BinaryReader(stream, Encoding.Unicode);
614  version = reader.ReadInt32();
615  if (version != 1)
616  throw new IOException("Incorrect IndexSetInfo version identifier.");
617 
618  IndexSetInfo = Sql.IndexSetInfo.DeserializeFrom(stream);
619  }
620 
621  // Read the list header
624 
625  // Init the index store
627  try {
629  } catch (IOException) {
630  // If this failed try writing output a new empty index set.
631  // ISSUE: Should this occur here? This is really an attempt at repairing
632  // the index store.
636  headerArea.Position = 32;
637  headerArea.WriteInt8(indexHeaderOffset);
638  headerArea.Position = 0;
639  headerArea.Flush();
640  }
641  }
static TableInfo Deserialize(Stream stream, ITypeResolver typeResolver)
Definition: TableInfo.cs:411
void PrepareIndexLists(int count, int type, int blockSize)
long Position
Returns or sets the current position of the pointer within the area.
Definition: IArea.cs:48
IndexSetInfo(ObjectName tableName, IEnumerable< IndexInfo > indexes, bool readOnly)
Definition: IndexSetInfo.cs:28
void Open(long startPointer)
int ColumnCount
Gets a count of the columns defined by this object.
Definition: TableInfo.cs:159
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.Sql.Tables.TableSource.ReleaseObjects ( )
inlineprivate

Definition at line 231 of file TableSource.cs.

231  {
232  lock (recordList) {
233  long elements = recordList.NodeCount;
234  for (long rowNumber = 0; rowNumber < elements; ++rowNumber) {
235  var a = recordList.GetRecord(rowNumber);
236  var status = (RecordState) a.ReadInt4();
237  // Is the record not deleted?
238  if (status != RecordState.Deleted) {
239  // Get the record pointer
240  long recordPointer = a.ReadInt8();
241  ReleaseRowObjects(recordPointer);
242  }
243  }
244  }
245  }
RecordState
An enumeration that represents the various states of a record.
Definition: RecordState.cs:23
void ReleaseRowObjects(long recordPointer)
Definition: TableSource.cs:905
IArea GetRecord(long recordNumber)
void Deveel.Data.Sql.Tables.TableSource.ReleaseRowObjects ( long  recordPointer)
inlineprivate

Definition at line 905 of file TableSource.cs.

905  {
906  // NOTE: Does this need to be optimized?
907  IArea recordArea = Store.GetArea(recordPointer);
908  recordArea.ReadInt4(); // reserved
909 
910  // Look for any blob references input the row
911  for (int i = 0; i < ColumnCount; ++i) {
912  int ctype = recordArea.ReadInt4();
913  int cellOffset = recordArea.ReadInt4();
914 
915  if (ctype == 1) {
916  // Type 1 is not a large object
917  } else if (ctype == 2) {
918  var curP = recordArea.Position;
919  recordArea.Position = cellOffset + 4 + (ColumnCount * 8);
920 
921  int btype = recordArea.ReadInt4();
922  recordArea.ReadInt4(); // (reserved)
923 
924  if (btype == 0) {
925  long blobRefId = recordArea.ReadInt8();
926 
927  // Release this reference
928  ObjectStore.ReleaseObject(blobRefId);
929  }
930 
931  // Revert the area pointer
932  recordArea.Position = curP;
933  } else {
934  throw new Exception("Unrecognised type.");
935  }
936  }
937  }
long Position
Returns or sets the current position of the pointer within the area.
Definition: IArea.cs:48
An interface for access the contents of an area of a store.
Definition: IArea.cs:23
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.Sql.Tables.TableSource.RemoveLock ( )
inline

Definition at line 1271 of file TableSource.cs.

1271  {
1272  lock (this) {
1273  if (!isClosed) {
1274  // TODO: Emit the event to the system
1275 
1276  if (rootLock == 0)
1277  throw new InvalidOperationException("Too many root locks removed!");
1278 
1279  --rootLock;
1280 
1281  // If the last Lock is removed, schedule a possible collection.
1282  if (rootLock == 0)
1283  CheckForCleanup();
1284  }
1285  }
1286  }
void Deveel.Data.Sql.Tables.TableSource.RemoveRowFromCache ( int  rowIndex)
inlineprivate

Definition at line 896 of file TableSource.cs.

896  {
897  if (CellCaching) {
898  var colCount = TableInfo.ColumnCount;
899  for (int i = 0; i < colCount; i++) {
900  CellCache.Remove(DatabaseContext.DatabaseName(), TableId, rowIndex, i);
901  }
902  }
903  }
int ColumnCount
Gets a count of the columns defined by this object.
Definition: TableInfo.cs:159
void Deveel.Data.Sql.Tables.TableSource.RollbackTransactionChange ( TableEventRegistry  registry)
inline

Definition at line 1508 of file TableSource.cs.

1508  {
1509  lock (this) {
1510  // ASSERT: Can't do this is source is Read only.
1511  if (IsReadOnly)
1512  throw new InvalidOperationException("Can't rollback transaction journal, table is Read only.");
1513 
1514  // Any rows added in the journal are marked as committed deleted and the
1515  // journal is then discarded.
1516 
1517  try {
1518  // Mark all rows in the data_store as appropriate to the changes.
1519  foreach (var tableEvent in registry) {
1520  if (tableEvent is TableRowEvent) {
1521  var rowEvent = (TableRowEvent) tableEvent;
1522  if (rowEvent.EventType == TableRowEventType.Add) {
1523  var oldState = WriteRecordState(rowEvent.RowNumber, RecordState.CommittedRemoved);
1524  if (oldState != RecordState.Uncommitted) {
1525  WriteRecordState(rowEvent.RowNumber, oldState);
1526  throw new InvalidOperationException(String.Format("Record {0} was not in an uncommitted state.",
1527  rowEvent.RowNumber));
1528  }
1529 
1530  GC.DeleteRow(rowEvent.RowNumber);
1531  }
1532  }
1533  }
1534  } catch (IOException e) {
1535  throw new InvalidOperationException("IO Error: " + e.Message, e);
1536  }
1537  }
1538  }
A long string in the system.
RecordState WriteRecordState(int rowNumber, RecordState state)
Definition: TableSource.cs:768
RecordState
An enumeration that represents the various states of a record.
Definition: RecordState.cs:23
TableRowEventType
The kind of events that can happen on a table row during the life-time of a transaction.
void Deveel.Data.Sql.Tables.TableSource.ScanForLeaks ( )
inlineprivate

Definition at line 337 of file TableSource.cs.

337  {
338  lock (recordList) {
339  // The list of pointers to areas (as Long).
340  var usedAreas = new List<long>();
341 
342  usedAreas.Add(headerArea.Id);
343 
344  headerArea.Position = 16;
345  // Add the DataTableInfo and DataIndexSetInfo objects
346  usedAreas.Add(headerArea.ReadInt8());
347  usedAreas.Add(headerArea.ReadInt8());
348 
349  // Add all the used areas input the list_structure itself.
350  recordList.GetAreasUsed(usedAreas);
351 
352  // Adds all the user areas input the index store.
353  indexSetStore.GetAreasUsed(usedAreas);
354 
355  // Search the list structure for all areas
356  long elements = recordList.NodeCount;
357  for (long i = 0; i < elements; ++i) {
358  var area = recordList.GetRecord(i);
359  var status = (RecordState) area.ReadInt4();
360  if (status != RecordState.Deleted) {
361  usedAreas.Add(area.ReadInt8());
362  }
363  }
364 
365  // Following depends on store implementation
366  if (Store is StoreBase) {
367  var aStore = (StoreBase)Store;
368  var leakedAreas = aStore.FindAllocatedAreasNotIn(usedAreas).ToList();
369  if (leakedAreas.Count == 0) {
370  } else {
371  foreach (long areaPointer in leakedAreas) {
372  Store.DeleteArea(areaPointer);
373  }
374  }
375  }
376  }
377  }
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...
void GetAreasUsed(List< long > list)
RecordState
An enumeration that represents the various states of a record.
Definition: RecordState.cs:23
void GetAreasUsed(IList< long > usedAreas)
long Id
Returns the unique identifier that represents this area.
Definition: IArea.cs:31
IArea GetRecord(long recordNumber)
void Deveel.Data.Sql.Tables.TableSource.SetIndexSetInfo ( )
inlineprivate

Definition at line 427 of file TableSource.cs.

427  {
428  lock (this) {
429  // Create the initial DataIndexSetInfo object.
431  foreach (var colInfo in TableInfo) {
432  if (colInfo.IsIndexable) {
433  var indexName = String.Format("IDX_{0}", colInfo.ColumnName);
434  var indexType = colInfo.IndexType;
435  if (String.IsNullOrEmpty(indexType))
436  indexType = DefaultIndexTypes.InsertSearch;
437 
438  IndexSetInfo.AddIndex(new IndexInfo(indexName, indexType, new[] {colInfo.ColumnName}, false));
439  }
440  }
441  }
442  }
A long string in the system.
ObjectName TableName
Gets the fully qualified name of the table that is ensured to be unique within the system...
Definition: TableInfo.cs:97
void AddIndex(IndexInfo indexInfo)
Definition: IndexSetInfo.cs:58
void Deveel.Data.Sql.Tables.TableSource.SetTableInfo ( TableInfo  info)
inlineprivate

Definition at line 410 of file TableSource.cs.

410  {
411  lock (this) {
412  // Check table_id isn't too large.
413  if ((TableId & 0x0F0000000) != 0)
414  throw new InvalidOperationException("'table_id' exceeds maximum possible keys.");
415 
416  info.Establish(TableId);
417  TableInfo = info;
418 
419  // Create table indices
420  tableIndices = new VersionedTableIndexList(this);
421 
422  // Setup the DataIndexSetInfo
423  SetIndexSetInfo();
424  }
425  }
VersionedTableIndexList tableIndices
Definition: TableSource.cs:36
void Deveel.Data.Sql.Tables.TableSource.SetUniqueId ( long  value)
inline

Implements Deveel.Data.Sql.Tables.ITableSource.

Definition at line 667 of file TableSource.cs.

667  {
668  lock (recordList) {
669  sequenceId = value;
670  if (HasShutdown)
671  throw new Exception("IO operation while shutting down.");
672 
673  try {
674  try {
675  Store.Lock();
676  headerArea.Position = 4 + 4;
677  headerArea.WriteInt8(sequenceId);
678  headerArea.Flush();
679  } finally {
680  Store.Unlock();
681  }
682  } catch (IOException e) {
683  throw new InvalidOperationException("IO Error: " + e.Message, e);
684  }
685  }
686  }
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 Unlock()
This method is called after the end of a sequence of Write commands between consistant states of some...
void Deveel.Data.Sql.Tables.TableSource.SetupInitialStore ( )
inlineprivate

Definition at line 508 of file TableSource.cs.

508  {
509  byte[] tableInfoBuf;
510  using (var stream = new MemoryStream()) {
511  var writer = new BinaryWriter(stream, Encoding.Unicode);
512  writer.Write(1);
513  TableInfo.Serialize(TableInfo, writer);
514 
515  tableInfoBuf = stream.ToArray();
516  }
517 
518  byte[] indexSetInfoBuf;
519  using (var stream = new MemoryStream()) {
520  var writer = new BinaryWriter(stream, Encoding.Unicode);
521  writer.Write(1);
522 
523  IndexSetInfo.SerialiazeTo(stream);
524 
525  indexSetInfoBuf = stream.ToArray();
526  }
527 
528  try {
529  Store.Lock();
530 
531  // Allocate an 80 byte header
532  var headerWriter = Store.CreateArea(80);
533  long headerPointer = headerWriter.Id;
534  // Allocate space to store the DataTableInfo serialization
535  var dataTableDefWriter = Store.CreateArea(tableInfoBuf.Length);
536  long tableInfoOffset = dataTableDefWriter.Id;
537  // Allocate space to store the DataIndexSetInfo serialization
538  var indexSetWriter = Store.CreateArea(indexSetInfoBuf.Length);
539  long indexSetInfoPointer = indexSetWriter.Id;
540 
541  // Allocate space for the list header
545 
546  // Create the index store
549 
550  // Write the main header
551  headerWriter.WriteInt4(1); // Version
552  headerWriter.WriteInt4(TableId); // table id
553  headerWriter.WriteInt8(sequenceId); // initial sequence id
554  headerWriter.WriteInt8(tableInfoOffset); // pointer to DataTableInfo
555  headerWriter.WriteInt8(indexSetInfoPointer); // pointer to DataIndexSetInfo
556  headerWriter.WriteInt8(indexHeaderOffset); // index header pointer
557  headerWriter.WriteInt8(listHeaderOffset); // list header pointer
558  headerWriter.Flush();
559 
560  // Write the table info
561  dataTableDefWriter.Write(tableInfoBuf, 0, tableInfoBuf.Length);
562  dataTableDefWriter.Flush();
563 
564  // Write the index set info
565  indexSetWriter.Write(indexSetInfoBuf, 0, indexSetInfoBuf.Length);
566  indexSetWriter.Flush();
567 
568  // Set the pointer to the header input the reserved area.
569  var fixedArea = Store.GetArea(-1);
570  fixedArea.WriteInt8(headerPointer);
571  fixedArea.Flush();
572 
573  // Set the header area
574  headerArea = Store.GetArea(headerPointer);
575  } finally {
576  Store.Unlock();
577  }
578  }
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...
void SerialiazeTo(Stream stream)
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
static void Serialize(TableInfo tableInfo, Stream stream)
Definition: TableInfo.cs:387
long Deveel.Data.Sql.Tables.TableSource.WriteRecord ( Row  data)
inlineprivate

Definition at line 1062 of file TableSource.cs.

1062  {
1063  // Calculate how much space this record will use
1064  int rowCells = data.ColumnCount;
1065 
1066  int[] cellSizes = new int[rowCells];
1067  int[] cellTypes = new int[rowCells];
1068 
1069  try {
1070  Store.Lock();
1071 
1072  // Establish a reference to any blobs input the record
1073  int allRecordsSize = 0;
1074  for (int i = 0; i < rowCells; ++i) {
1075  var cell = data.GetValue(i);
1076  int cellSize;
1077  int cellType;
1078 
1079  if (cell.Value is IObjectRef) {
1080  var largeObjectRef = (IObjectRef)cell.Value;
1081 
1082  cellSize = 16;
1083  cellType = 2;
1084  if (largeObjectRef != null) {
1085  // Tell the blob store interface that we've made a static reference
1086  // to this blob.
1087  ObjectStore.EstablishObject(largeObjectRef.ObjectId.Id);
1088  }
1089  } else {
1090  cellSize = cell.Size;
1091  cellType = 1;
1092  }
1093 
1094  cellSizes[i] = cellSize;
1095  cellTypes[i] = cellType;
1096  allRecordsSize += cellSize;
1097  }
1098 
1099  // Allocate space for the record,
1100  var area = Store.CreateArea(allRecordsSize + (rowCells * 8) + 4);
1101  long recordPointer = area.Id;
1102 
1103  // The record output stream
1104  using (var areaStream = new AreaStream(area)) {
1105  var writer = new BinaryWriter(areaStream);
1106 
1107  // Write the record header first,
1108  writer.Write(0); // reserved for future use
1109  int cellSkip = 0;
1110  for (int i = 0; i < rowCells; ++i) {
1111  writer.Write(cellTypes[i]);
1112  writer.Write(cellSkip);
1113  cellSkip += cellSizes[i];
1114  }
1115 
1116  // Now Write a serialization of the cells themselves,
1117  for (int i = 0; i < rowCells; ++i) {
1118  var obj = data.GetValue(i);
1119  int cellType = cellTypes[i];
1120  if (cellType == 1) {
1121  // Regular object
1122  obj.SerializeValueTo(areaStream, SystemContext);
1123  } else if (cellType == 2) {
1124  // This is a binary large object and must be represented as a ref
1125  // to a blob input the BlobStore.
1126  var largeObjectRef = (IObjectRef)obj.Value;
1127  if (largeObjectRef == null) {
1128  // null value
1129  writer.Write(1);
1130  writer.Write(0); // Reserved for future use
1131  writer.Write(-1L);
1132  } else {
1133  writer.Write(0);
1134  writer.Write(0); // Reserved for future use
1135  writer.Write(largeObjectRef.ObjectId.Id);
1136  }
1137  } else {
1138  throw new IOException("Unrecognised cell type.");
1139  }
1140  }
1141 
1142  // Flush the output
1143  writer.Flush();
1144  }
1145 
1146  // Finish the record
1147  area.Flush();
1148 
1149  // Return the record
1150  return recordPointer;
1151  } finally {
1152  Store.Unlock();
1153  }
1154  }
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
RecordState Deveel.Data.Sql.Tables.TableSource.WriteRecordState ( int  rowNumber,
RecordState  state 
)
inline

Implements Deveel.Data.Sql.Tables.ITableSource.

Definition at line 768 of file TableSource.cs.

768  {
769  lock (recordList) {
770  if (HasShutdown)
771  throw new IOException("IO operation while shutting down.");
772 
773  // Find the record entry input the block list.
774  var blockArea = recordList.GetRecord(rowNumber);
775  var pos = blockArea.Position;
776  // Get the status.
777  var oldStatus = (RecordState) blockArea.ReadInt4();
778 
779  // Write the new status
780  try {
781  Store.Lock();
782 
783  blockArea.Position = pos;
784  blockArea.WriteInt4((int)state);
785  blockArea.Flush();
786  } finally {
787  Store.Unlock();
788  }
789 
790  return oldStatus;
791  }
792  }
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...
RecordState
An enumeration that represents the various states of a record.
Definition: RecordState.cs:23
void Unlock()
This method is called after the end of a sequence of Write commands between consistant states of some...
IArea GetRecord(long recordNumber)

Member Data Documentation

long Deveel.Data.Sql.Tables.TableSource.firstDeleteChainRecord
private

Definition at line 42 of file TableSource.cs.

IArea Deveel.Data.Sql.Tables.TableSource.headerArea
private

Definition at line 41 of file TableSource.cs.

long Deveel.Data.Sql.Tables.TableSource.indexHeaderOffset
private

Definition at line 39 of file TableSource.cs.

IndexSetStore Deveel.Data.Sql.Tables.TableSource.indexSetStore
private

Definition at line 35 of file TableSource.cs.

bool Deveel.Data.Sql.Tables.TableSource.isClosed
private

Definition at line 46 of file TableSource.cs.

long Deveel.Data.Sql.Tables.TableSource.listHeaderOffset
private

Definition at line 40 of file TableSource.cs.

FixedRecordList Deveel.Data.Sql.Tables.TableSource.recordList
private

Definition at line 38 of file TableSource.cs.

int Deveel.Data.Sql.Tables.TableSource.rootLock
private

Definition at line 47 of file TableSource.cs.

long Deveel.Data.Sql.Tables.TableSource.sequenceId
private

Definition at line 44 of file TableSource.cs.

VersionedTableIndexList Deveel.Data.Sql.Tables.TableSource.tableIndices
private

Definition at line 36 of file TableSource.cs.

Property Documentation

bool Deveel.Data.Sql.Tables.TableSource.CanCompact
get

Definition at line 123 of file TableSource.cs.

ITableCellCache Deveel.Data.Sql.Tables.TableSource.CellCache
getsetprivate

Definition at line 168 of file TableSource.cs.

bool Deveel.Data.Sql.Tables.TableSource.CellCaching
get

Definition at line 170 of file TableSource.cs.

int Deveel.Data.Sql.Tables.TableSource.ColumnCount
get

Definition at line 101 of file TableSource.cs.

TableSourceComposite Deveel.Data.Sql.Tables.TableSource.Composite
getprivate set

Definition at line 67 of file TableSource.cs.

long Deveel.Data.Sql.Tables.TableSource.CurrentUniqueId
get

Definition at line 115 of file TableSource.cs.

IDatabase Deveel.Data.Sql.Tables.TableSource.Database
get

Definition at line 73 of file TableSource.cs.

IDatabaseContext Deveel.Data.Sql.Tables.TableSource.DatabaseContext
get

Definition at line 69 of file TableSource.cs.

TableSourceGC Deveel.Data.Sql.Tables.TableSource.GC
getprivate set

Definition at line 164 of file TableSource.cs.

bool Deveel.Data.Sql.Tables.TableSource.HasChangesPending
get

Definition at line 150 of file TableSource.cs.

bool Deveel.Data.Sql.Tables.TableSource.HasShutdown
getprivate set

Definition at line 166 of file TableSource.cs.

IndexSetInfo Deveel.Data.Sql.Tables.TableSource.IndexSetInfo
getprivate set

Definition at line 135 of file TableSource.cs.

bool Deveel.Data.Sql.Tables.TableSource.IsClosed
getprotected set

Definition at line 137 of file TableSource.cs.

bool Deveel.Data.Sql.Tables.TableSource.IsReadOnly
get

Definition at line 131 of file TableSource.cs.

bool Deveel.Data.Sql.Tables.TableSource.IsRootLocked
get

Definition at line 91 of file TableSource.cs.

IObjectStore Deveel.Data.Sql.Tables.TableSource.ObjectStore
getprivate set

Definition at line 162 of file TableSource.cs.

int Deveel.Data.Sql.Tables.TableSource.RawRowCount
get

Definition at line 105 of file TableSource.cs.

string Deveel.Data.Sql.Tables.TableSource.SourceName
getprivate set

Definition at line 89 of file TableSource.cs.

IStore Deveel.Data.Sql.Tables.TableSource.Store
getprivate set

Definition at line 160 of file TableSource.cs.

string Deveel.Data.Sql.Tables.TableSource.StoreIdentity
getprivate set

Definition at line 158 of file TableSource.cs.

IStoreSystem Deveel.Data.Sql.Tables.TableSource.StoreSystem
getsetprivate

Definition at line 81 of file TableSource.cs.

ISystemContext Deveel.Data.Sql.Tables.TableSource.SystemContext
get

Definition at line 77 of file TableSource.cs.

int Deveel.Data.Sql.Tables.TableSource.TableId
getprivate set

Definition at line 83 of file TableSource.cs.

TableInfo Deveel.Data.Sql.Tables.TableSource.TableInfo
getprivate set

Definition at line 99 of file TableSource.cs.

ObjectName Deveel.Data.Sql.Tables.TableSource.TableName
get

Definition at line 85 of file TableSource.cs.


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