DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
TransactionCollection.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;
19 using System.Collections.Generic;
20 using System.Linq;
21 
22 namespace Deveel.Data.Transactions {
23  public sealed class TransactionCollection : IEnumerable<ITransaction> {
24  private readonly List<ITransaction> transactions;
25  private long minCommitId;
26  private long maxCommitId;
27 
29  transactions = new List<ITransaction>();
30  minCommitId = Int64.MaxValue;
31  maxCommitId = 0;
32  }
33 
34  public int Count {
35  get {
36  lock (this) {
37  return transactions.Count;
38  }
39  }
40  }
41 
42  public void AddTransaction(ITransaction transaction) {
43  lock (this) {
44  long currentCommitId = transaction.CommitId;
45  if (currentCommitId < maxCommitId)
46  throw new InvalidOperationException("Added a transaction with a lower than maximum commit id");
47 
48  transactions.Add(transaction);
49  //TODO: SystemContext.Stats.Increment(StatsDefaultKeys.OpenTransactionsCount);
50  maxCommitId = currentCommitId;
51  }
52  }
53 
54  public void RemoveTransaction(ITransaction transaction) {
55  lock (this) {
56  int size = transactions.Count;
57  int i = transactions.IndexOf(transaction);
58  if (i == 0) {
59  // First in list.
60  if (i == size - 1) {
61  // And last.
62  minCommitId = Int32.MaxValue;
63  maxCommitId = 0;
64  } else {
65  minCommitId = transactions[i + 1].CommitId;
66  }
67  } else if (i == transactions.Count - 1) {
68  // Last in list.
69  maxCommitId = transactions[i - 1].CommitId;
70  } else if (i == -1) {
71  throw new InvalidOperationException("Unable to find transaction in the list.");
72  }
73 
74  transactions.RemoveAt(i);
75  //TODO: SystemContext.Stats.Decrement(StatsDefaultKeys.OpenTransactionsCount);
76  }
77  }
78 
79  public long MinimumCommitId(ITransaction transaction) {
80  lock (this) {
81  long commitId = Int64.MaxValue;
82  if (transactions.Count > 0) {
83  // If the bottom transaction is this transaction, then go to the
84  // next up from the bottom (we don't count this transaction as the
85  // minimum commit_id).
86  var testTransaction = transactions[0];
87  if (testTransaction != transaction) {
88  commitId = testTransaction.CommitId;
89  } else if (transactions.Count > 1) {
90  commitId = transactions[1].CommitId;
91  }
92  }
93 
94  return commitId;
95  }
96  }
97 
98  public IEnumerator<ITransaction> GetEnumerator() {
99  lock (this) {
100  return transactions.GetEnumerator();
101  }
102  }
103 
104  IEnumerator IEnumerable.GetEnumerator() {
105  return GetEnumerator();
106  }
107 
108  public ITransaction FindById(int commitId) {
109  lock (this) {
110  return transactions.FirstOrDefault(x => x.CommitId == commitId);
111  }
112  }
113  }
114 }
int CommitId
Gets a number uniquely identifying a transaction within a database context.
Definition: ITransaction.cs:37
The simplest implementation of a transaction.
Definition: ITransaction.cs:30