Space Class Reference

Class containing the logical namespace of the cache. More...

#include <Space.h>

List of all members.

Public Types

typedef __gnu_cxx::hash_map
< LightString, HashTabElement
SpaceHashTab
 Type of the hash-table.
typedef std::vector< SpaceHashTabSpaceHashTabs
 Type of the structure that holds every sub-hash-table.
typedef std::list< LRUListElementSpaceLRUList
 Type of space's LRU List.
typedef std::vector< SpaceLRUListSpaceLRULists
 Type of vector of space's LRU Lists.
typedef std::vector
< pthread_rwlock_t * > 
Mutexes
 Type of structure holding hash-table mutexes.
typedef std::map< int, char > Indexes
 Type of structure that maps ints to chars (for convieniency).

Public Member Functions

 Space (const std::string &name, unsigned long long softQuota, unsigned long long hardQuota, Cache *memcache, User *user)
 Constructs the Space.
Value get (const LightString &key, unsigned timeout=0)
 Retrieves the value of the cached chunk assigned to the given key.
bool get (const LightString &key, Value &value, unsigned timeout=0)
 Retrieves the value of the cached chunk assigned to the given key.
SpaceResult set (const LightString &key, const Value &value, unsigned timeout, uint64_t *new_cas_id=NULL)
 Assigns the given value to the given key, sets the expiry time and marks the value with the next available cas_id.
SpaceResult cas (const LightString &key, const Value &value, unsigned timeout, uint64_t cas_id, uint64_t *new_cas_id=NULL)
 Similar to set but this method checks whether current cas_id is equal to the one passed as parameter and sets the new value if and only if they match.
SpaceResult add (const LightString &key, const Value &value, unsigned timeout, uint64_t cas_id=0, uint64_t *new_cas_id=NULL)
 Similar to set, but sets only if there's no such key.
SpaceResult replace (const LightString &key, const Value &value, unsigned timeout, uint64_t cas_id=0, uint64_t *new_cas_id=NULL)
 Similar to set, but sets only if the key already exists in the space.
SpaceResult append (const LightString &key, const Value &value, uint64_t *new_cas_id=NULL)
 Appends the give value to the value assigned that is already assigned to given key.
SpaceResult prepend (const LightString &key, const Value &value, uint64_t *new_cas_id=NULL)
 Prepends the give value to the value assigned that is already assigned to given key.
SpaceResult increment (const LightString &key, uint64_t step, uint64_t *out, unsigned timeout=0, bool force=0, uint64_t initial=0)
 Increments integer value that was previously stored.
SpaceResult decrement (const LightString &key, uint64_t step, uint64_t *out, unsigned timeout=0, bool force=0, uint64_t initial=0)
 Decrements integer value that was previously stored.
bool saveSpace (const char *fileName, unsigned minTimeout=0)
 Dumps the space to a file.
void cleanSpace ()
 Cleans the space.
void destroySpace ()
 Destroys the space.
Stats getStats ()
 Retrieves space stats.
void clearStats ()
 Clears all the statistics e.g.
void setStatus (unsigned nUsedQuota, unsigned nGetsCount, unsigned nSetsCount, unsigned nBytesWritten, unsigned nBytesRead, unsigned nMissCount, unsigned nHitCount, unsigned long long nItemsCount)
 Used while loading space from disk to restore saved status and stats.
std::string getStatsStr ()
 Generates a textual description of the current space state.

Public Attributes

Mutexes mapMutexes
 Dispersed hash-table mutexes.
pthread_mutex_t globalMutex
 Mutex to guard access to space stats and LRUList.
std::string name
 Textual name of the space.
Stats stats
 Space's statistical data.
StatsfragmentedStats
 Space's statistical data (dispersed into many sub-stats to boost GET requests).
SpaceHashTabs hashTab
 sub-spaces' hash tables
SpaceLRULists LRUList
 sub-spaces' LRU lists

Static Public Attributes

static unsigned int NUMERIC_OBJECT_SIZE = UINT_MAX / 2
 If a numeric value is stored it has size of NUMERIC_OBJECT_SIZE.
static unsigned int INCR_MAX_STORAGE_LEN = 24
 Maximal digit count in a numeric value.
static const int MUTEXES_COUNT = 32
 Space is dispersed to MUTEXES_COUNT sub-spaces.

Classes

class  HashTabElement
 Auxiliary class for keeping elements in the hash table, containing information about the cached value and iterator to LRUList. More...
class  LRUListElement
 Auxiliary class for keeping elements in the LRUList, containing the key to hash table and expiry time of the cached value. More...


Detailed Description

Class containing the logical namespace of the cache.

It manages the mapping textual keys to the binary values that can be stored and loaded by user.

Definition at line 245 of file Space.h.


Constructor & Destructor Documentation

Space::Space ( const std::string &  name,
unsigned long long  softQuota,
unsigned long long  hardQuota,
Cache memcache,
User user 
)

Constructs the Space.

Parameters:
name the name of the Space
softQuota size of the soft quota in bytes
hardQuota size of the soft quota in bytes
memcache pointer to the Cache object to which constructed Space belongs
user pointer to the User object to whom constructed Space belongs

Definition at line 31 of file Space.cpp.


Member Function Documentation

Value Space::get ( const LightString key,
unsigned  timeout = 0 
)

Retrieves the value of the cached chunk assigned to the given key.

Optionally modifies the expiry time.

Parameters:
key key of which value will be retrieved
timeout interval in seconds of the time of life of the value or 0 to leave it unmodified
Returns:
Retrieved value. If there was no assigned value (or it was expired) to the given key then method isNull() on the returned objects returns true else returns false.

Definition at line 139 of file Space.cpp.

bool Space::get ( const LightString key,
Value value,
unsigned  timeout = 0 
)

Retrieves the value of the cached chunk assigned to the given key.

Optionally modifies the expiry time.

if value.memoryPointer is NULL a chunk of memory of exact size as the value in cache will be malloced. if value.memoryPointer is not NULL value.size will be checked whether the value from cache would fit the memory pointed by memoryPointer. If value.size is smaller then size of the one in cache this method does nothind and returns false.

Parameters:
key key of which value will be retrieved
value value reference to which new data will be copied
timeout interval in seconds of the time of life of the value or 0 to leave it unmodified
Returns:
true on success

Definition at line 67 of file Space.cpp.

SpaceResult Space::set ( const LightString key,
const Value value,
unsigned  timeout,
uint64_t *  new_cas_id = NULL 
)

Assigns the given value to the given key, sets the expiry time and marks the value with the next available cas_id.

NOTE: cas_id member of value parameter is ignored by this method.

See also:
cas()
Parameters:
key key at which the value will be stored
value cached value
timeout interval in seconds of the time of life of the value
new_cas_id pointer to where the new cas_id will be stored if not NULL
Returns:
true if the value was cached and false in case of error

Definition at line 260 of file Space.cpp.

SpaceResult Space::cas ( const LightString key,
const Value value,
unsigned  timeout,
uint64_t  cas_id,
uint64_t *  new_cas_id = NULL 
)

Similar to set but this method checks whether current cas_id is equal to the one passed as parameter and sets the new value if and only if they match.

cas_id of a value can be obtained by get().

NOTE: cas_id member of value parameter is ignored by this method.

Parameters:
key key at which the value will be stored
value cached value
timeout interval in seconds of the time of life of the value
cas_id id that has to be equal to the value's current one
new_cas_id pointer to where the new cas_id will be stored if not NULL
Returns:
true if the value was cached and false if cas_id's do not match or in case of error

Definition at line 280 of file Space.cpp.

SpaceResult Space::add ( const LightString key,
const Value value,
unsigned  timeout,
uint64_t  cas_id = 0,
uint64_t *  new_cas_id = NULL 
)

Similar to set, but sets only if there's no such key.

Parameters:
key key at which the value will be stored
value cached value
timeout interval in seconds of the time of life of the value
cas_id if it's > 0 then overwritten cas_id will be matched and nothing will be done if match fails
new_cas_id pointer to where the new cas_id will be stored if not NULL
Returns:
true if the value was cached and false if the size exceeds the Space size or there's already set some value at that key

Definition at line 301 of file Space.cpp.

SpaceResult Space::replace ( const LightString key,
const Value value,
unsigned  timeout,
uint64_t  cas_id = 0,
uint64_t *  new_cas_id = NULL 
)

Similar to set, but sets only if the key already exists in the space.

Parameters:
key key at which the value will be stored
value cached value
timeout interval in seconds of the time of life of the value
cas_id if it's > 0 then overwritten cas_id will be matched and nothing will be done if match fails
new_cas_id pointer to where the new cas_id will be stored if not NULL
Returns:
true if the value was cached and false if the size exceeds the Space size or there is not set any value at that key

Definition at line 325 of file Space.cpp.

SpaceResult Space::append ( const LightString key,
const Value value,
uint64_t *  new_cas_id = NULL 
)

Appends the give value to the value assigned that is already assigned to given key.

Parameters:
key key at which the value will appended
value appended value
new_cas_id pointer to where the new cas_id will be stored if not NULL
Returns:
true if the value was appended and false if the size exceeds the Space size or there is not set any value at that key

Definition at line 348 of file Space.cpp.

SpaceResult Space::prepend ( const LightString key,
const Value value,
uint64_t *  new_cas_id = NULL 
)

Prepends the give value to the value assigned that is already assigned to given key.

Parameters:
key key at which the value will prepended
value prepended value
new_cas_id pointer to where the new cas_id will be stored if not NULL
Returns:
true if the value was prepended and false if the size exceeds the Space size or there is not set any value at that key

Definition at line 352 of file Space.cpp.

SpaceResult Space::increment ( const LightString key,
uint64_t  step,
uint64_t *  out,
unsigned  timeout = 0,
bool  force = 0,
uint64_t  initial = 0 
)

Increments integer value that was previously stored.

You have to provide that the value was initialized using setNumeric otherwise the result is undetermined.

Parameters:
key key at which the value is stored
step the value will be equal to current_value+step
out pointer to where the new value will be stored
timeout interval in seconds of the time of life of the value or 0 to leave it unmodified
force force to store the initial value in case it doesn't exist yet
initial the initial value; ignored if force is false
Returns:
the value after the incrementation

Definition at line 451 of file Space.cpp.

SpaceResult Space::decrement ( const LightString key,
uint64_t  step,
uint64_t *  out,
unsigned  timeout = 0,
bool  force = 0,
uint64_t  initial = 0 
)

Decrements integer value that was previously stored.

You have to provide that the value was initialized using setNumeric otherwise the result is undetermined.

Parameters:
key key at which the value is stored
step the value will be equal to current_value-step
out pointer to where the new value will be stored
timeout interval in seconds of the time of life of the value or 0 to leave it unmodified
force force to store the initial value in case it doesn't exist yet
initial the initial value; ignored if force is false
Returns:
the value after the decrementation

Definition at line 458 of file Space.cpp.

bool Space::saveSpace ( const char *  fileName,
unsigned  minTimeout = 0 
)

Dumps the space to a file.

It dumps only these values which timeout is equal or greater than given timout.

Parameters:
fileName the name of the file in which the space will be saved
minTimeout the minimal timeout for filtering out values which timeout is less than it
Returns:
true if space was succesfuly saved or false otherwise

Definition at line 539 of file Space.cpp.

Stats Space::getStats (  ) 

Retrieves space stats.

Returns:
space stats

Definition at line 606 of file Space.cpp.

void Space::clearStats (  ) 

Clears all the statistics e.g.

sets them to zero. These are: bytesRead bytesWritten getsCount setsCount hitCount missCount

Definition at line 629 of file Space.cpp.

std::string Space::getStatsStr (  )  [inline]

Generates a textual description of the current space state.

Returns:
string with the description of the current space state

Definition at line 625 of file Space.h.


Member Data Documentation

Dispersed hash-table mutexes.

(Normally there should be one global mutex which would be locked before findind in hashTable. We keep vector of globalMutexes to spread waiting at least a little.)

Definition at line 355 of file Space.h.


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

Generated on Fri Jan 8 10:38:13 2010 for dreamcache by  doxygen 1.5.5