/*

Copyright (c) 2002-2005, Yauheni Akhotnikau
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

- Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

- Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.

- The name of the author may not be used to endorse or promote products derived
from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.

*/

/*
	ObjESSty-1
*/
#include <iostream>
#include <cstring>

#include <oess_1/db/storage/impl/h/mem_dirty_page_storage.hpp>

namespace oess_1 {

namespace db {

namespace storage {

namespace impl {

//
// mem_dirty_page_storage_t
//

mem_dirty_page_storage_t::mem_dirty_page_storage_t()
	{}

mem_dirty_page_storage_t::~mem_dirty_page_storage_t()
	{}

void
mem_dirty_page_storage_t::put(
	const ref_loaded_page_t & page )
	{
		pages_map_t::iterator it = m_pages.find( page->ordinal() );
		if( it == m_pages.end() )
			m_pages[ page->ordinal() ] = page;
	}

bool
mem_dirty_page_storage_t::find(
	oess_1::uint_t page_ordinal,
	ref_loaded_page_t & receiver ) const
	{
		pages_map_t::const_iterator it = m_pages.find( page_ordinal );
		if( it != m_pages.end() )
			{
				receiver = it->second;

				return true;
			}

		return false;
	}

void
mem_dirty_page_storage_t::for_each( dirty_page_actor_t & actor ) const
	{
		for( pages_map_t::const_iterator
			it = m_pages.begin(), it_end = m_pages.end();
			it != it_end; ++it )
			{
				const ref_loaded_page_t & v = it->second;
				const oess_1::char_t * source =
						v->has_previous() ? v->previous() : 0;
				actor( it->first, v->current(), source, v->page_size() );
			}
	}

void
mem_dirty_page_storage_t::clear()
	{
		m_pages.clear();
	}

oess_1::uint_t
mem_dirty_page_storage_t::size() const
	{
		return m_pages.size();
	}

} /* namespace impl */

//
// create_std_dirty_page_storage
//

std::auto_ptr< dirty_page_storage_t >
create_std_dirty_page_storage()
	{
		return std::auto_ptr< dirty_page_storage_t >(
				new impl::mem_dirty_page_storage_t() );
	}

} /* namespace storage */

} /* namespace db */

} /* namespace oess */

