DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
Classes | Public Member Functions | Package Functions | Static Package Functions | Properties | Private Member Functions | Static Private Member Functions | Private Attributes | Static Private Attributes | List of all members
Deveel.Data.Caching.DataCellCache Class Reference

Represents a cache for accesses to the the data cells within a Table. More...

Classes

class  DCCache
 An implementation of Cache. More...
 
struct  DCCacheKey
 Struct that creates an object that hashes nicely over the cache source. More...
 

Public Member Functions

void AlterCacheDynamics (int maxCacheSize, int maxCellSize)
 Dynamically resizes the data cell cache so it can store more/less data. More...
 
void Set (int tableKey, int row, int column, DataObject cell)
 Adds a DataObject on the cache for the given row/column of the table. More...
 
DataObject Get (int tableKey, int row, int column)
 Gets a cell from the cache. More...
 
DataObject Remove (int tableKey, int row, int column)
 Removes a cell from the cache. More...
 
void Clear ()
 Completely wipe the cache of all entries. More...
 

Package Functions

 DataCellCache (SystemContext context, ICache systemCache, int maxCacheSize, int maxCellSize, int hashSize)
 Instantiate the DataCellCache. More...
 
 DataCellCache (SystemContext context, ICache systemCache, int maxCacheSize, int maxCellSize)
 

Static Package Functions

static int ClosestPrime (int value)
 Returns a prime number from PRIME_LIST that is the closest prime greater or equal to the given value. More...
 

Properties

long CurrentCacheSize [get]
 Returns an estimation of the current cache size in bytes. More...
 

Private Member Functions

void ReduceCacheSize (long val)
 Evaluate the cache size by the given amount. More...
 

Static Private Member Functions

static int AmountMemory (DataObject cell)
 Returns an approximation of the amount of memory taken by a given DataObject. More...
 

Private Attributes

readonly DCCache cache
 The master cache. More...
 
readonly SystemContext context
 The TransactionSystem that this cache is from. More...
 
long currentCacheSize
 The current size of the cache. More...
 
int maxCellSize
 The maximum size of a DataCell that is allowed to go in the cache. More...
 

Static Private Attributes

static readonly int[] PRIME_LIST
 A list of primes ordered from lowest to highest. More...
 

Detailed Description

Represents a cache for accesses to the the data cells within a Table.

Whenever a column/row index to a cell is accessed, the cache is first checked. If the cell is not in the cache then it may go ahead and read the cell from the file.

Issue We may need to keep track of memory used. Since a String may use up much memory, we may need a cap on the maximum size the cache can grow to. For example, we wouldn't want to cache a large document. This could be handled at a higher level?

Definition at line 37 of file DataCellCache.cs.

Constructor & Destructor Documentation

Deveel.Data.Caching.DataCellCache.DataCellCache ( SystemContext  context,
ICache  systemCache,
int  maxCacheSize,
int  maxCellSize,
int  hashSize 
)
inlinepackage

Instantiate the DataCellCache.

<param name="context></param> <param name="systemCache"></param> <param name="maxCacheSize">The maximum size in bytes that the cache is allowed to grow to (eg. 4000000).

Parameters
maxCellSizeThe maximum size of an object that can be stored in the cache.
hashSizeThe number of elements in the hash (should be a prime number).

Definition at line 109 of file DataCellCache.cs.

109  {
110  this.context = context;
111  this.maxCellSize = maxCellSize;
112 
113  cache = new DCCache(this, systemCache, hashSize, maxCacheSize);
114  }
readonly SystemContext context
The TransactionSystem that this cache is from.
readonly DCCache cache
The master cache.
int maxCellSize
The maximum size of a DataCell that is allowed to go in the cache.
Deveel.Data.Caching.DataCellCache.DataCellCache ( SystemContext  context,
ICache  systemCache,
int  maxCacheSize,
int  maxCellSize 
)
inlinepackage

Definition at line 116 of file DataCellCache.cs.

117  : this(context, systemCache, maxCacheSize, maxCellSize, 88547) {
118  // Good prime number hash size
119  }
readonly SystemContext context
The TransactionSystem that this cache is from.
int maxCellSize
The maximum size of a DataCell that is allowed to go in the cache.

Member Function Documentation

void Deveel.Data.Caching.DataCellCache.AlterCacheDynamics ( int  maxCacheSize,
int  maxCellSize 
)
inline

Dynamically resizes the data cell cache so it can store more/less data.

Parameters
maxCacheSize
maxCellSize

This is used to change cache dynamics at runtime.

Definition at line 140 of file DataCellCache.cs.

140  {
141  lock (this) {
142  this.maxCellSize = maxCellSize;
143  cache.SetCacheSize(maxCacheSize);
144  }
145  }
readonly DCCache cache
The master cache.
int maxCellSize
The maximum size of a DataCell that is allowed to go in the cache.
void SetCacheSize(int cache_size)
Used to dynamically alter the size of the cache.
static int Deveel.Data.Caching.DataCellCache.AmountMemory ( DataObject  cell)
inlinestaticprivate

Returns an approximation of the amount of memory taken by a given DataObject.

Parameters
cell
Returns

Definition at line 153 of file DataCellCache.cs.

153  {
154  return 16 + cell.SizeOf();
155  }
void Deveel.Data.Caching.DataCellCache.Clear ( )
inline

Completely wipe the cache of all entries.

Definition at line 234 of file DataCellCache.cs.

234  {
235  lock (this) {
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.");
238  }
239  if (cache.NodeCount != 0) {
240  cache.Clear();
241  context.Stats.Increment(StatsDefaultKeys.DataCellCacheTotalWipes);
242  }
243  currentCacheSize = 0;
244  }
245  }
long currentCacheSize
The current size of the cache.
readonly SystemContext context
The TransactionSystem that this cache is from.
readonly DCCache cache
The master cache.
static int Deveel.Data.Caching.DataCellCache.ClosestPrime ( int  value)
inlinestaticpackage

Returns a prime number from PRIME_LIST that is the closest prime greater or equal to the given value.

Parameters
value
Returns

Definition at line 263 of file DataCellCache.cs.

263  {
264  for (int i = 0; i < PRIME_LIST.Length; ++i) {
265  if (PRIME_LIST[i] >= value) {
266  return PRIME_LIST[i];
267  }
268  }
269  // Return the last prime
270  return PRIME_LIST[PRIME_LIST.Length - 1];
271  }
static readonly int[] PRIME_LIST
A list of primes ordered from lowest to highest.
DataObject Deveel.Data.Caching.DataCellCache.Get ( int  tableKey,
int  row,
int  column 
)
inline

Gets a cell from the cache.

Parameters
tableKey
row
column
Returns
Returns the value of the cell at the given coordinates, or null if the row/column is not in the cache.

Definition at line 201 of file DataCellCache.cs.

201  {
202  lock (this) {
203  return (DataObject) cache.Get(new DCCacheKey(tableKey, (short) column, row));
204  }
205  }
readonly DCCache cache
The master cache.
void Deveel.Data.Caching.DataCellCache.ReduceCacheSize ( long  val)
inlineprivate

Evaluate the cache size by the given amount.

Parameters
val

Definition at line 251 of file DataCellCache.cs.

251  {
252  currentCacheSize -= val;
253  }
long currentCacheSize
The current size of the cache.
DataObject Deveel.Data.Caching.DataCellCache.Remove ( int  tableKey,
int  row,
int  column 
)
inline

Removes a cell from the cache.

Parameters
tableKey
row
column

This is used when we need to notify the cache that an object has become outdated. This should be used when the cell has been removed or changed.

Returns
Returns the cell that was removed, or null if there was no cell at the given location.

Definition at line 222 of file DataCellCache.cs.

222  {
223  lock (this) {
224  var cell = (DataObject)cache.Remove(new DCCacheKey(tableKey, (short) column, row));
225  if (cell != null)
227  return cell;
228  }
229  }
long currentCacheSize
The current size of the cache.
static int AmountMemory(DataObject cell)
Returns an approximation of the amount of memory taken by a given DataObject.
readonly DCCache cache
The master cache.
void Deveel.Data.Caching.DataCellCache.Set ( int  tableKey,
int  row,
int  column,
DataObject  cell 
)
inline

Adds a DataObject on the cache for the given row/column of the table.

Parameters
tableKey
row
column
cell

Ignores any cells that are larger than the maximum size.

Definition at line 168 of file DataCellCache.cs.

168  {
169  lock (this) {
170  int memoryUse = AmountMemory(cell);
171  if (memoryUse <= maxCellSize) {
172  // Generate the key
173  var key = new DCCacheKey(tableKey, (short)column, row);
174  // If there is an existing object here, remove it from the cache and
175  // update the current_cache_size.
176  var removedCell = (DataObject)cache.Remove(key);
177  if (removedCell != null) {
178  currentCacheSize -= AmountMemory(removedCell);
179  }
180  // Put the new entry in the cache
181  cache.Set(key, cell);
182  currentCacheSize += memoryUse;
183  } else {
184  // If the object is larger than the minimum object size that can be
185  // cached, remove any existing entry (possibly smaller) from the cache.
186  Remove(tableKey, row, column);
187  }
188  }
189  }
long currentCacheSize
The current size of the cache.
DataObject Remove(int tableKey, int row, int column)
Removes a cell from the cache.
static int AmountMemory(DataObject cell)
Returns an approximation of the amount of memory taken by a given DataObject.
readonly DCCache cache
The master cache.
int maxCellSize
The maximum size of a DataCell that is allowed to go in the cache.

Member Data Documentation

readonly DCCache Deveel.Data.Caching.DataCellCache.cache
private

The master cache.

Definition at line 81 of file DataCellCache.cs.

readonly SystemContext Deveel.Data.Caching.DataCellCache.context
private

The TransactionSystem that this cache is from.

Definition at line 86 of file DataCellCache.cs.

long Deveel.Data.Caching.DataCellCache.currentCacheSize
private

The current size of the cache.

Definition at line 91 of file DataCellCache.cs.

int Deveel.Data.Caching.DataCellCache.maxCellSize
private

The maximum size of a DataCell that is allowed to go in the cache.

Definition at line 96 of file DataCellCache.cs.

readonly int [] Deveel.Data.Caching.DataCellCache.PRIME_LIST
staticprivate

A list of primes ordered from lowest to highest.

Definition at line 41 of file DataCellCache.cs.

Property Documentation

long Deveel.Data.Caching.DataCellCache.CurrentCacheSize
get

Returns an estimation of the current cache size in bytes.

Definition at line 124 of file DataCellCache.cs.


The documentation for this class was generated from the following file: