Skip to content

Commit

Permalink
refs #6449 removed saveable and replaced it with ISaveable.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
abuts committed Apr 17, 2013
1 parent 6e994c2 commit 34688dc
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 256 deletions.
2 changes: 0 additions & 2 deletions Code/Mantid/Framework/Kernel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ set ( SRC_FILES
src/ReadLock.cpp
src/RebinParamsValidator.cpp
src/RegexStrings.cpp
src/Saveable.cpp
src/SignalChannel.cpp
src/SingletonHolder.cpp
src/SobolSequence.cpp
Expand Down Expand Up @@ -192,7 +191,6 @@ set ( INC_FILES
inc/MantidKernel/RebinParamsValidator.h
inc/MantidKernel/RegexStrings.h
inc/MantidKernel/RegistrationHelper.h
inc/MantidKernel/Saveable.h
inc/MantidKernel/SignalChannel.h
inc/MantidKernel/SingletonHolder.h
inc/MantidKernel/SobolSequence.h
Expand Down
83 changes: 60 additions & 23 deletions Code/Mantid/Framework/Kernel/inc/MantidKernel/ISaveable.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,50 @@ namespace Kernel
/**Return the number of units this block occipies on file */
uint64_t getFileSize()const
{ return m_fileNumEvents; }
//-----------------------------------------------------------------------------------------------
// Saveable functions interface, which controls the logic of working with objects on HDD
virtual bool isBusy()const=0;
virtual bool wasSaved()const=0;
virtual bool isLoaded()const=0;
virtual void setBusy(bool /*On*/)=0;
//protected?
virtual void setLoaded(bool /*Yes*/)=0;
virtual bool isDataChanged()const=0;
virtual void setDataChanged()=0;
virtual void clearDataChanged()=0;

/** Sets the location of the object on HDD */
virtual void setFilePosition(uint64_t newPos,size_t newSize,bool wasSaved)=0;
void setFilePosition(uint64_t newPos,size_t newSize,bool wasSaved);
//-----------------------------------------------------------------------------------------------
// Saveable functions interface, which controls the logic of working with objects on HDD



/** @return true if the object have ever been saved on HDD and knows it place there*/
bool wasSaved()const // for speed it returns this boolean, but for relaibility this should be m_wasSaved&&(m_fileIndexStart!=max())
{ return m_wasSaved; }
/**@return true if the object has been load in the memory -- the load function should call setter, or if the object was constructed in memory it should be loaded too */
bool isLoaded()const
{ return m_isLoaded;}

// protected?
/**@sets the value of the isLoad parameter -- usually only load functiomn should set it to true */
void setLoaded(bool Yes)
{ m_isLoaded = Yes;}

/// @return true if it the data of the object is busy and so cannot be cleared; false if the data was released and can be cleared/written.
bool isBusy() const
{ return m_Busy; }
/// @ set the data busy to prevent from removing them from memory. The process which does that should clean the data when finished with them
void setBusy(bool On)
{ m_Busy=On; }

//protected?

/**@return the state of the parameter, which tells disk buffer to force writing data
* to disk despite the size of the object have not changed (so one have probably done something with object contents. */
bool isDataChanged()const
{return m_dataChanged;}
/** Call this method from the method which changes the object but keeps the object size the same to tell DiskBuffer to write it back
the dataChanged ID is reset after save from the DataBuffer is emptied */
void setDataChanged()
{ if(this->wasSaved())m_dataChanged=true; }
/** this method has to be called if the object has been discarded from memory and is not changed any more.
It expected to be called from clearDataFromMemory. */
void clearDataChanged()
{ m_dataChanged=false; }


//-----------------------------------------------------------------------------------------------
// INTERFACE:

/// Save the data - to be overriden
virtual void save()const =0;
Expand All @@ -94,22 +122,29 @@ namespace Kernel
/// the data size kept in memory
virtual size_t getDataMemorySize()const=0;

protected:
/** Unique, sequential ID of the object/box within the containing workspace.
This ID also relates to boxes order as the boxes with adjacent
ID should usually occupy adjacent places on HDD */
// size_t m_FileId;
/// Start point in the NXS file where the events are located
uint64_t m_fileIndexStart;
/// Number of events saved in the file, after the start index location
uint64_t m_fileNumEvents;
protected:
//--------------
/// a user needs to set this variable to true preventing from deleting data from buffer
bool m_Busy;
/** a user needs to set this variable to true to allow DiskBuffer saving the object to HDD
when it decides it suitable, if the size of iSavable object in cache is unchanged from the previous
save/load operation */
bool m_dataChanged;
// this tracks the history of operations, occuring over the data.
/// this boolean indicates if the data were saved on HDD and have physical representation on it (though this representation may be incorrect as data changed in memory)
mutable bool m_wasSaved;
/// this boolean indicates, if the data have its copy in memory
bool m_isLoaded;

Kernel::Mutex m_setter;
private:
// the iterator which describes the position of this object in the DiskBuffer. Undefined if not placed to buffer
boost::optional< std::list<ISaveable * >::iterator> m_BufPosition;
// the size of the object in the memory buffer, used to calculate the total amount of memory the objects occupy
size_t m_BufMemorySize;
/// Start point in the NXS file where the events are located
uint64_t m_fileIndexStart;
/// Number of events saved in the file, after the start index location
uint64_t m_fileNumEvents;


/// the functions below have to be availible to DiskBuffer and nobody else. To highlight this we make them private
Expand All @@ -131,6 +166,8 @@ namespace Kernel
/// clears the state of the object, and indicate that it is not stored in buffer any more
void clearBufferState();

// the mutex to protect changes in this memory
Kernel::Mutex m_setter;
};


Expand Down
92 changes: 0 additions & 92 deletions Code/Mantid/Framework/Kernel/inc/MantidKernel/Saveable.h

This file was deleted.

141 changes: 84 additions & 57 deletions Code/Mantid/Framework/Kernel/src/ISaveable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,63 +5,90 @@

namespace Mantid
{
namespace Kernel
{
namespace Kernel
{

/** Constructor */
ISaveable::ISaveable():
m_Busy(false),m_dataChanged(false),m_wasSaved(false),m_isLoaded(false),
m_fileIndexStart(std::numeric_limits<uint64_t>::max() ),m_fileNumEvents(0),m_BufMemorySize(0)
{}

//----------------------------------------------------------------------------------------------
/** Copy constructor --> needed for std containers and not to copy mutexes
Note setting isLoaded to false to break connection with the file object which is not copyale */
ISaveable::ISaveable(const ISaveable & other):
m_Busy(other.m_Busy),m_dataChanged(other.m_dataChanged),m_wasSaved(other.m_wasSaved),m_isLoaded(false),
m_fileIndexStart(other.m_fileIndexStart),m_fileNumEvents(other.m_fileNumEvents),
m_BufPosition(other.m_BufPosition),
m_BufMemorySize(other.m_BufMemorySize)
{ }


//---------------------------------------------------------------------------

/** Set the start/end point in the file where the events are located
* @param newPos :: start point,
* @param newSize :: number of events in the file
* @param wasSaved :: flag to mark if the info was saved, by default it does
*/
void ISaveable::setFilePosition(uint64_t newPos, size_t newSize, bool wasSaved)
{
Mutex::ScopedLock (this->m_setter);
this->m_fileIndexStart=newPos;
this->m_fileNumEvents =static_cast<uint64_t>(newSize);
m_wasSaved = wasSaved;
}


// ----------- PRIVATE, only DB availible

/** private function which used by the disk buffer to save the contents of the object
@param newPos -- new position to save object to
@param newSize -- new size of the saveable object
*/
void ISaveable::saveAt(uint64_t newPos, uint64_t newSize)
{

Mutex::ScopedLock _lock(m_setter);

// load old contents if it was there
if(this->wasSaved())
this->load();
// set new position, derived by the disk buffer
m_fileIndexStart= newPos;
m_fileNumEvents = newSize;
// save in the new location
this->save();
this->clearDataFromMemory();
}

/** Method stores the position of the object in Disc buffer and returns the size of this object for disk buffer to store
* @param bufPosition -- the allocator which specifies the position of the object in the list of objects to write
* @returns the size of the object it currently occupies in memory. This size is also stored by the object itself for further references
*/
size_t ISaveable::setBufferPosition(std::list<ISaveable *>::iterator bufPosition)
{
Mutex::ScopedLock _lock(m_setter);

m_BufPosition = boost::optional<std::list<ISaveable *>::iterator >(bufPosition);
m_BufMemorySize = this->getDataMemorySize();

return m_BufMemorySize ;
}


/// clears the state of the object, and indicate that it is not stored in buffer any more
void ISaveable::clearBufferState()
{
Mutex::ScopedLock _lock(m_setter);

m_BufMemorySize=0;
m_BufPosition = boost::optional<std::list<ISaveable *>::iterator>();

}


/** Constructor */
ISaveable::ISaveable():
m_fileIndexStart(std::numeric_limits<uint64_t>::max() ),m_fileNumEvents(0),m_BufMemorySize(0)
{}

//----------------------------------------------------------------------------------------------
/** Copy constructor --> needed for std containers and not to copy mutexes */
ISaveable::ISaveable(const ISaveable & other):
m_fileIndexStart(other.m_fileIndexStart),m_fileNumEvents(other.m_fileNumEvents),
m_BufPosition(other.m_BufPosition),
m_BufMemorySize(other.m_BufMemorySize)
{ }



/** Method stores the position of the object in Disc buffer and returns the size of this object for disk buffer to store
* @param bufPosition -- the allocator which specifies the position of the object in the list of objects to write
* @returns the size of the object it currently occupies in memory. This size is also stored by the object itself for further references
*/
size_t ISaveable::setBufferPosition(std::list<ISaveable *>::iterator bufPosition)
{
Mutex::ScopedLock _lock(m_setter);

m_BufPosition = boost::optional<std::list<ISaveable *>::iterator >(bufPosition);
m_BufMemorySize = this->getDataMemorySize();

return m_BufMemorySize ;
}

/** private function which used by the disk buffer to save the contents of the */
void ISaveable::saveAt(uint64_t newPos, uint64_t newSize)
{

Mutex::ScopedLock _lock(m_setter);

// load old contents if it was there
if(this->wasSaved())
this->load();
// set new position, derived by the disk buffer
m_fileIndexStart= newPos;
m_fileNumEvents = newSize;
// save in the new location
this->save();
this->clearDataFromMemory();
}
/// clears the state of the object, and indicate that it is not stored in buffer any more
void ISaveable::clearBufferState()
{
Mutex::ScopedLock _lock(m_setter);

m_BufMemorySize=0;
m_BufPosition = boost::optional<std::list<ISaveable *>::iterator>();

}
} // namespace Mantid
} // namespace Mantid
} // namespace Kernel

0 comments on commit 34688dc

Please sign in to comment.