DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
TableCommitCallback.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.Collections.Generic;
19 
20 using Deveel.Data;
21 using Deveel.Data.Sql;
22 
23 namespace Deveel.Data.Transactions {
24  public abstract class TableCommitCallback {
25  private readonly List<int> addedList;
26  private readonly List<int> removedList;
27 
28  protected TableCommitCallback(ObjectName tableName) {
29  if (tableName == null)
30  throw new ArgumentNullException("tableName");
31 
32  TableName = tableName;
33 
34  addedList = new List<int>();
35  removedList = new List<int>();
36  }
37 
38  public ObjectName TableName { get; private set; }
39 
40  protected bool IsInTransaction { get; private set; }
41 
42  internal void OnTransactionStarted() {
43  IsInTransaction = true;
44  Act();
45  }
46 
47  internal void OnTransactionEnd() {
48  IsInTransaction = false;
49  Act();
50  }
51 
52  public void AttachTo(ITransaction transaction) {
53  transaction.RegisterOnCommit(OnCommit);
54 
55  if (transaction is ICallbackHandler)
56  ((ICallbackHandler)transaction).OnCallbackAttached(this);
57  }
58 
59  public void DetachFrom(ITransaction transaction) {
60  transaction.UnregisterOnCommit(OnCommit);
61 
62  if (transaction is ICallbackHandler)
63  ((ICallbackHandler)transaction).OnCallbackDetached(this);
64  }
65 
66  private void Act() {
67  IList<int> add, remove;
68  lock (removedList) {
69  add = new List<int>(addedList);
70  remove = new List<int>(removedList);
71 
72  addedList.Clear();
73  removedList.Clear();
74  }
75 
76  OnAction(add, remove);
77  }
78 
79  private void OnCommit(TableCommitInfo commitInfo) {
80  if (TableName.Equals(commitInfo.TableName)) {
81  addedList.AddRange(commitInfo.AddedRows);
82  removedList.AddRange(commitInfo.RemovedRows);
83  }
84  }
85 
86  protected abstract void OnAction(IEnumerable<int> addedRows, IEnumerable<int> removedRows);
87  }
88 }
void UnregisterOnCommit(Action< TableCommitInfo > action)
Describes the name of an object within a database.
Definition: ObjectName.cs:44
void RegisterOnCommit(Action< TableCommitInfo > action)
void OnCommit(TableCommitInfo commitInfo)
The simplest implementation of a transaction.
Definition: ITransaction.cs:30