DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
TableSourceGC.cs
Go to the documentation of this file.
1 //
2 // Copyright 2010-2015 Deveel
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 using System;
18 using System.IO;
19 
20 using Deveel.Data.Index;
21 
22 namespace Deveel.Data.Sql.Tables {
23  class TableSourceGC {
24  private readonly TableSource tableSource;
26 
27  // private DateTime lastSuccess;
28  // private DateTime? lastTry;
29 
30  private bool fullSweep;
31 
32  public TableSourceGC(TableSource tableSource) {
33  this.tableSource = tableSource;
34  deletedRows = new BlockIndex<int>();
35 
36  // lastSuccess = DateTime.UtcNow;
37  // lastTry = null;
38  }
39 
40  public void DeleteRow(int rowIndex) {
41  if (!fullSweep) {
42  if (!deletedRows.UniqueInsertSort(rowIndex))
43  throw new InvalidOperationException("Row marked twice for deletion.");
44  }
45  }
46 
47  public void Collect(bool force) {
48  try {
49  int checkCount = 0;
50  int deleteCount = 0;
51 
52  // Synchronize over the master data table source so no other threads
53  // can interfere when we collect this information.
54  lock (tableSource) {
55  if (tableSource.IsClosed)
56  return;
57 
58  // If root is locked, or has transaction changes pending, then we
59  // can't delete any rows marked as deleted because they could be
60  // referenced by transactions or result sets.
61  if (force ||
62  (!tableSource.IsRootLocked &&
63  !tableSource.HasChangesPending)) {
64 
65  // lastSuccess = DateTime.Now;
66  // lastTry = null;
67 
68  // Are we due a full sweep?
69  if (fullSweep) {
70  int rawRowCount = tableSource.RawRowCount;
71  for (int i = 0; i < rawRowCount; ++i) {
72  // Synchronized in dataSource.
73  if (tableSource.HardCheckAndReclaimRow(i))
74  ++deleteCount;
75 
76  ++checkCount;
77  }
78 
79  fullSweep = false;
80  } else {
81  // Are there any rows marked as deleted?
82  int size = deletedRows.Count;
83  if (size > 0) {
84  // Go remove all rows marked as deleted.
85  foreach (int rowIndex in deletedRows) {
86  // Synchronized in dataSource.
87  tableSource.HardRemoveRow(rowIndex);
88  ++deleteCount;
89  ++checkCount;
90  }
91  }
92 
93  deletedRows = new BlockIndex<int>();
94  }
95 
96  if (checkCount > 0) {
97  // TODO: Emit the information to the system
98  }
99 
100  } // if not roots locked and not transactions pending
101 
102  } // lock
103  } catch (IOException) {
104  // TODO: Log the error to the system
105  }
106  }
107  }
108 }
TableSourceGC(TableSource tableSource)