DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
TableCellCache.old.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 
19 using Deveel.Data.DbSystem;
20 
21 namespace Deveel.Data.Caching {
22  public sealed class TableCellCache {
23  private Cache cache;
24  private long size;
25 
26  public const int DefaultHashSize = 88547;
27 
28  public TableCellCache(IDatabaseContext context, ICache baseCase, int maxSize, int maxCellSize)
29  : this(context, baseCase, maxSize, maxCellSize, DefaultHashSize) {
30  }
31 
32  public TableCellCache(IDatabaseContext context, ICache baseCase, int maxSize, int maxCellSize, int hashSize) {
33  Context = context;
34  MaxCellSize = maxCellSize;
35 
36  cache = new Cache(this, baseCase, hashSize, maxSize);
37  }
38 
39  public long Size {
40  get {
41  lock (this) {
42  return size;
43  }
44  }
45  }
46 
47  public int MaxCellSize { get; private set; }
48 
49  public IDatabaseContext Context { get; private set; }
50 
51  private void ReduceCacheSize(long value) {
52  size -= value;
53  }
54 
55  public void Clear() {
56  lock (this) {
57  if (cache.NodeCount == 0 && Size != 0) {
58  // TODO: Raise an error
59  }
60  if (cache.NodeCount != 0) {
61  cache.Clear();
62  // TODO: Register the statistics
63  }
64 
65  size = 0;
66  }
67  }
68 
69  public void AlterCacheDynamics(int maxCacheSize, int maxCellSize) {
70  lock (this) {
71  MaxCellSize = maxCellSize;
72  cache.ChangeSize(maxCacheSize);
73  }
74  }
75 
76  public void Set(int tableKey, int row, int column, DataObject value) {
77  if (!value.IsCacheable)
78  throw new ArgumentException(String.Format("A value of type '{0}' cannot be stored in cache.", value.Type));
79 
80  lock (this) {
81  int memoryUse = AmountMemory(value);
82  if (memoryUse <= MaxCellSize) {
83  // Generate the key
84  var key = new CacheKey(tableKey, row, (short)column);
85 
86  // If there is an existing object here, remove it from the cache and
87  // update the current_cache_size.
88  var removedCell = (DataObject) cache.Remove(key);
89  if (!Equals(removedCell, null)) {
90  size -= AmountMemory(removedCell);
91  }
92 
93  // Put the new entry in the cache
94  cache.Set(key, value);
95  size += memoryUse;
96  } else {
97  // If the object is larger than the minimum object size that can be
98  // cached, remove any existing entry (possibly smaller) from the cache.
99  Remove(tableKey, row, column);
100  }
101  }
102  }
103 
104  private static int AmountMemory(DataObject value) {
105  return 16 + value.CacheUsage;
106  }
107 
108  public DataObject Get(int tableKey, int row, int column) {
109  lock (this) {
110  return (DataObject)cache.Get(new CacheKey(tableKey, row, (short)column));
111  }
112  }
113 
114  public DataObject Remove(int tableKey, int row, int column) {
115  lock (this) {
116  var cell = (DataObject)cache.Remove(new CacheKey(tableKey, row, (short)column));
117  if (cell != null)
118  size -= AmountMemory(cell);
119 
120  return cell;
121  }
122  }
123 
124  #region Cache
125 
126  class Cache : CacheAdapter {
127  private readonly TableCellCache tableCache;
128  private int hashSize;
129 
130  public Cache(TableCellCache tableCache, ICache baseCache, int hashSize, int maxSize)
131  : base(baseCache, maxSize) {
132  this.tableCache = tableCache;
133  this.hashSize = hashSize;
134  }
135 
136  public void ChangeSize(int newSize) {
137  hashSize = newSize;
138  CheckClean();
139  }
140 
141  protected override void CheckClean() {
142  if (tableCache.Size >= hashSize) {
143  // TODO: Register the statistics
144 
145  Clean();
146 
147  //TODO: Register the statistics
148  }
149  }
150 
151  protected override bool WipeMoreNodes() {
152  return (tableCache.Size >= (int)((hashSize * 100L) / 115L));
153  }
154 
155  protected override void OnWipingNode(object ob) {
156  base.OnWipingNode(ob);
157 
158  // Update our memory indicator accordingly.
159  var value = (DataObject)ob;
160  tableCache.ReduceCacheSize(AmountMemory(value));
161  }
162 
163  protected override void OnGetWalks(long totalWalks, long totalGetOps) {
164  // TODO: Register the statistics ...
165  base.OnGetWalks(totalWalks, totalGetOps);
166  }
167  }
168 
169  #endregion
170 
171  #region CacheKey
172 
173  class CacheKey : IEquatable<CacheKey> {
174  private readonly short column;
175  private readonly int row;
176  private readonly int tableId;
177 
178  public CacheKey(int tableId, int row, short column) {
179  this.tableId = tableId;
180  this.row = row;
181  this.column = column;
182  }
183 
184  public override bool Equals(object obj) {
185  return Equals((CacheKey) obj);
186  }
187 
188  public override int GetHashCode() {
189  // Yicks - this one is the best by far!
190  return (((int)column + tableId + (row * 189977)) * 50021) << 4;
191  }
192 
193  public bool Equals(CacheKey other) {
194  return row == other.row &&
195  column == other.column &&
196  tableId == other.tableId;
197  }
198  }
199 
200  #endregion
201  }
202 }
SqlType Type
Gets the SqlType that defines the object properties
Definition: DataObject.cs:78
override void OnGetWalks(long totalWalks, long totalGetOps)
Notifies that some statistical information about the hash map has updated.
The context of a single database within a system.
override void OnWipingNode(object ob)
Notifies that the given object has been wiped from the cache by the clean up procedure.
TableCellCache(IDatabaseContext context, ICache baseCase, int maxSize, int maxCellSize, int hashSize)
Represents a dynamic object that encapsulates a defined SqlType and a compatible constant ISqlObject ...
Definition: DataObject.cs:35
void AlterCacheDynamics(int maxCacheSize, int maxCellSize)
TableCellCache(IDatabaseContext context, ICache baseCase, int maxSize, int maxCellSize)
Cache(TableCellCache tableCache, ICache baseCache, int hashSize, int maxSize)
Represents a cache of Objects. /summary>
Definition: Cache.cs:25
Provides a contract to access a caching system.
Definition: ICache.cs:25
override void CheckClean()
This is called whenever at object is put into the cache.
CacheKey(int tableId, int row, short column)
override bool WipeMoreNodes()
Checks if the clean-up method should clean up more elements from the cache.
DataObject Remove(int tableKey, int row, int column)
void Set(int tableKey, int row, int column, DataObject value)
static int AmountMemory(DataObject value)
DataObject Get(int tableKey, int row, int column)