21 namespace Deveel.Data.Caching {
41 private static readonly
int[] PRIME_LIST =
new int[]
43 3001, 4799, 13999, 15377, 21803, 24247, 35083, 40531, 43669, 44263, 47387,
44 50377, 57059, 57773, 59399, 59999, 75913, 96821, 140551, 149011, 175633,
45 176389, 183299, 205507, 209771, 223099, 240259, 258551, 263909, 270761,
46 274679, 286129, 290531, 296269, 298021, 300961, 306407, 327493, 338851,
47 351037, 365489, 366811, 376769, 385069, 410623, 430709, 433729, 434509,
48 441913, 458531, 464351, 470531, 475207, 479629, 501703, 510709, 516017,
49 522211, 528527, 536311, 539723, 557567, 593587, 596209, 597451, 608897,
50 611069, 642547, 670511, 677827, 679051, 688477, 696743, 717683, 745931,
51 757109, 760813, 763957, 766261, 781559, 785597, 788353, 804493, 813559,
52 836917, 854257, 859973, 883217, 884789, 891493, 902281, 910199, 915199,
53 930847, 939749, 940483, 958609, 963847, 974887, 983849, 984299, 996211,
54 999217, 1007519, 1013329, 1014287, 1032959, 1035829, 1043593, 1046459,
55 1076171, 1078109, 1081027, 1090303, 1095613, 1098847, 1114037, 1124429,
56 1125017, 1130191, 1159393, 1170311, 1180631, 1198609, 1200809, 1212943,
57 1213087, 1226581, 1232851, 1287109, 1289867, 1297123, 1304987, 1318661,
58 1331107, 1343161, 1345471, 1377793, 1385117, 1394681, 1410803, 1411987,
59 1445261, 1460497, 1463981, 1464391, 1481173, 1488943, 1491547, 1492807,
60 1528993, 1539961, 1545001, 1548247, 1549843, 1551001, 1553023, 1571417,
61 1579099, 1600259, 1606153, 1606541, 1639751, 1649587, 1657661, 1662653,
62 1667051, 1675273, 1678837, 1715537, 1718489, 1726343, 1746281, 1749107,
63 1775489, 1781881, 1800157, 1806859, 1809149, 1826753, 1834607, 1846561,
64 1849241, 1851991, 1855033, 1879931, 1891133, 1893737, 1899137, 1909513,
65 1916599, 1917749, 1918549, 1919347, 1925557, 1946489, 1961551, 1965389,
66 2011073, 2033077, 2039761, 2054047, 2060171, 2082503, 2084107, 2095099,
67 2096011, 2112193, 2125601, 2144977, 2150831, 2157401, 2170141, 2221829,
68 2233019, 2269027, 2270771, 2292449, 2299397, 2303867, 2309891, 2312407,
69 2344301, 2348573, 2377007, 2385113, 2386661, 2390051, 2395763, 2422999,
70 2448367, 2500529, 2508203, 2509841, 2513677, 2516197, 2518151, 2518177,
71 2542091, 2547469, 2549951, 2556991, 2563601, 2575543, 2597629, 2599577,
72 2612249, 2620003, 2626363, 2626781, 2636773, 2661557, 2674297, 2691571,
73 2718269, 2725691, 2729381, 2772199, 2774953, 2791363, 2792939, 2804293,
74 2843021, 2844911, 2851313, 2863519, 2880797, 2891821, 2897731, 2904887,
75 2910251, 2928943, 2958341, 2975389
110 this.context = context;
111 this.maxCellSize = maxCellSize;
113 cache =
new DCCache(
this, systemCache, hashSize, maxCacheSize);
117 : this(context, systemCache, maxCacheSize, maxCellSize, 88547) {
124 public long CurrentCacheSize {
127 return currentCacheSize;
142 this.maxCellSize = maxCellSize;
143 cache.SetCacheSize(maxCacheSize);
154 return 16 + cell.SizeOf();
170 int memoryUse = AmountMemory(cell);
171 if (memoryUse <= maxCellSize) {
173 var key =
new DCCacheKey(tableKey, (
short)column, row);
176 var removedCell = (
DataObject)cache.Remove(key);
177 if (removedCell != null) {
178 currentCacheSize -= AmountMemory(removedCell);
181 cache.Set(key, cell);
182 currentCacheSize += memoryUse;
186 Remove(tableKey, row, column);
226 currentCacheSize -= AmountMemory(cell);
236 if (cache.NodeCount == 0 && currentCacheSize != 0) {
237 context.Logger.Error(
this,
"Assertion failed - if nodeCount = 0 then current_cache_size must also be 0.");
239 if (cache.NodeCount != 0) {
241 context.Stats.Increment(StatsDefaultKeys.DataCellCacheTotalWipes);
243 currentCacheSize = 0;
252 currentCacheSize -= val;
264 for (
int i = 0; i < PRIME_LIST.Length; ++i) {
265 if (PRIME_LIST[i] >= value) {
266 return PRIME_LIST[i];
270 return PRIME_LIST[PRIME_LIST.Length - 1];
287 : base(systemCache, cache_hash_size) {
289 maxCacheSize = max_cache_size;
301 maxCacheSize = cache_size;
308 if (cache.CurrentCacheSize >= maxCacheSize) {
310 cache.context.Stats.Set(StatsDefaultKeys.DataCellCacheCurrentSize, (
int) cache.CurrentCacheSize);
314 cache.context.Stats.Increment(StatsDefaultKeys.DataCellCacheClean);
319 return (cache.CurrentCacheSize >= (
int) ((maxCacheSize*100L)/115L));
323 base.OnWipingNode(ob);
327 cache.ReduceCacheSize(AmountMemory(cell));
330 protected override void OnGetWalks(
long totalWalks,
long totalGetOps) {
331 int avg = (int) ((totalWalks*1000000L)/totalGetOps);
332 cache.context.Stats.Set(
"DataCellCache.avg_hash_get_mul_1000000", avg);
333 cache.context.Stats.Set(
"DataCellCache.current_cache_size", (
int) cache.CurrentCacheSize);
334 cache.context.Stats.Set(
"DataCellCache.current_node_count", NodeCount);
352 this.tableId = tableId;
353 this.column = column;
359 return row == destKey.
row &&
360 column == destKey.
column &&
366 return (((
int) column + tableId + (row*189977))*50021) << 4;
static int ClosestPrime(int value)
Returns a prime number from PRIME_LIST that is the closest prime greater or equal to the given value...
long currentCacheSize
The current size of the cache.
override void OnWipingNode(Object ob)
override void CheckClean()
DCCacheKey(int tableId, short column, int row)
readonly SystemContext context
The TransactionSystem that this cache is from.
DataObject Remove(int tableKey, int row, int column)
Removes a cell from the cache.
This is the context of a database system, that handles the configurations and services used by all th...
int maxCacheSize
The maximum size that the cache can grow to in bytes.
void Set(int tableKey, int row, int column, DataObject cell)
Adds a DataObject on the cache for the given row/column of the table.
An implementation of Cache.
static int AmountMemory(DataObject cell)
Returns an approximation of the amount of memory taken by a given DataObject.
void Clear()
Completely wipe the cache of all entries.
DataCellCache(SystemContext context, ICache systemCache, int maxCacheSize, int maxCellSize, int hashSize)
Instantiate the DataCellCache.
readonly DataCellCache cache
override void OnGetWalks(long totalWalks, long totalGetOps)
DataObject Get(int tableKey, int row, int column)
Gets a cell from the cache.
override int GetHashCode()
Represents a cache for accesses to the the data cells within a Table.
Represents a dynamic object that encapsulates a defined SqlType and a compatible constant ISqlObject ...
override bool Equals(Object ob)
readonly DCCache cache
The master cache.
override bool WipeMoreNodes()
int maxCellSize
The maximum size of a DataCell that is allowed to go in the cache.
void ReduceCacheSize(long val)
Evaluate the cache size by the given amount.
Struct that creates an object that hashes nicely over the cache source.
Provides a contract to access a caching system.
void AlterCacheDynamics(int maxCacheSize, int maxCellSize)
Dynamically resizes the data cell cache so it can store more/less data.
DataCellCache(SystemContext context, ICache systemCache, int maxCacheSize, int maxCellSize)
DCCache(DataCellCache cache, ICache systemCache, int cache_hash_size, int max_cache_size)
void SetCacheSize(int cache_size)
Used to dynamically alter the size of the cache.