forked from content-manager-sdk/Community
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabasePool.cs
More file actions
215 lines (176 loc) · 6.1 KB
/
Copy pathDatabasePool.cs
File metadata and controls
215 lines (176 loc) · 6.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace HP.HPTRIM.SDK.Samples
{
public class DatabasePool : IDisposable
{
static readonly DatabasePool instance = new DatabasePool();
private static Object _poolLock = new Object();
private bool _isDisposed = false;
private const int POOL_SIZE = 500;
// In production code this inforation will probably be stored in a config file.
private const string WG_NAME = "local";
private const int WG_PORT = 1137;
private const string ALT_WG_NAME = "";
private const int ALT_WG_PORT = 0;
static DatabasePool()
{
Database.AllowAccessFromMultipleThreads = true;
}
DatabasePool() { }
public static DatabasePool Instance
{
get
{
return instance;
}
}
private readonly List<DatabasePoolEntry> _pool = new List<DatabasePoolEntry>();
public DatabasePoolEntry AcquirePoolEntry(string dbid, string identity, bool isSingleHop = false, string remoteIp = null)
{
if (_isDisposed) throw new ObjectDisposedException("DatabasePool");
lock (_poolLock)
{
DatabasePoolEntry pooled = GetPoolEntry(identity, dbid);
if (pooled != null)
{
pooled.LastAccessed = DateTime.Now;
return pooled;
}
}
TrimPool();
DatabasePoolEntry mine = new DatabasePoolEntry(identity, WG_NAME, WG_PORT, dbid, ALT_WG_NAME, ALT_WG_PORT, isSingleHop, remoteIp);
lock (_poolLock)
{
_pool.Add(mine);
}
return mine;
}
private DatabasePoolEntry GetPoolEntry(string identity, string dbid)
{
lock (_poolLock)
{
DatabasePoolEntry poolEntry = _pool.Where(
p => p.isAvailable(identity, dbid)).FirstOrDefault();
if (poolEntry != null)
{
poolEntry.IsLocked = true;
}
return poolEntry;
}
}
private static bool _disposing;
private void TrimPool()
{
// We trim the pool in three stages:
// * first mark pool entries as ready for disposal (dirty)
// * next we actually dispose them
// * last we remove the disposed objects from the list.
// it might be fine to simplify this and do it in one loop, this code has built up over the
// years as we have attempted to optimise the pool.
int newPoolSize = POOL_SIZE;
if (_pool.Count >= newPoolSize)
{
bool gotMonitor = false;
try
{
Monitor.TryEnter(_poolLock, ref gotMonitor);
if (gotMonitor)
{
_pool.Sort(delegate (DatabasePoolEntry p1, DatabasePoolEntry p2)
{
return p2.LastAccessed.CompareTo(p1.LastAccessed);
});
for (int counter = _pool.Count; counter >= newPoolSize; counter--)
{
if (!_pool[counter - 1].IsLocked)
{
_pool[counter - 1].Dirty = true;
}
}
}
}
finally
{
if (gotMonitor)
{
Monitor.Exit(_poolLock);
}
}
try
{
if (!_disposing)
{
_disposing = true;
List<DatabasePoolEntry> tempList = null;
lock (_poolLock)
{
tempList = _pool.ToList();
}
foreach (var pe in tempList.Where(pp => pp.Dirty))
{
pe.Dispose();
pe.AwaitingRemoval = true;
}
}
}
finally
{
_disposing = false;
}
if (Monitor.TryEnter(_poolLock))
{
try
{
for (int counter = 0; counter < _pool.Count; counter++)
{
if (_pool[counter].AwaitingRemoval)
{
_pool.RemoveAt(counter);
}
}
}
finally
{
Monitor.Exit(_poolLock);
}
}
}
}
public void ReleasePoolEntry(Guid id)
{
if (_isDisposed) throw new ObjectDisposedException("DatabasePool");
lock (_poolLock)
{
DatabasePoolEntry mine = _pool.Where(p =>
p.Id == id).FirstOrDefault();
if (mine != null)
{
mine.IsLocked = false;
}
}
}
#region IDisposable Members
public void Dispose()
{
if (_pool != null)
{
lock (_poolLock)
{
foreach (DatabasePoolEntry dp in _pool)
{
System.Diagnostics.Debug.Assert(!dp.IsLocked, "Attempt to Dispose Database still in use.");
dp.Dispose();
}
_pool.Clear();
}
}
_isDisposed = true;
}
#endregion
}
}