/*

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.

*/

/*!
	\file
	\brief Тип оглавления хранилища потоков.
*/

#include <oess_1/defs/h/ex.hpp>
#include <oess_1/defs/log/h/out.hpp>

#include <oess_1/stdsn/h/inout_templ.hpp>

#include <oess_1/db/storage/h/errno.hpp>

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

namespace oess_1 {

namespace db {

namespace storage {

namespace impl {

//
// stream_storage_dir_item_t
//

stream_storage_dir_item_t::stream_storage_dir_item_t()
	:	m_id( 0 )
	,	m_description_chain( invalid_chain_id )
	{}

stream_storage_dir_item_t::stream_storage_dir_item_t(
	const oess_1::uint_t & id,
	const chain_id_t & description_chain )
	:	m_id( id )
	,	m_description_chain( description_chain )
	{}

stream_storage_dir_item_t::stream_storage_dir_item_t(
	const stream_storage_dir_item_t & o )
	{
		(*this) = o;
	}

stream_storage_dir_item_t::~stream_storage_dir_item_t()
	{}

stream_storage_dir_item_t &
stream_storage_dir_item_t::operator=(
	const stream_storage_dir_item_t & o )
	{
		if( &o != this )
			{
				base_type_t::operator=( o );

				m_id = o.m_id;
				m_description_chain = o.m_description_chain;
			}

		return *this;
	}

oess_1::uint_t
stream_storage_dir_item_t::id() const
	{
		return m_id;
	}

const chain_id_t &
stream_storage_dir_item_t::description_chain() const
	{
		return m_description_chain;
	}

//
// stream_storage_dir_t
//

stream_storage_dir_t::stream_storage_dir_t()
	:	m_id( 0 )
	{}

stream_storage_dir_t::stream_storage_dir_t(
	const stream_storage_dir_t & o )
	{
		(*this) = o;
	}
	
stream_storage_dir_t::~stream_storage_dir_t()
	{}

stream_storage_dir_t &
stream_storage_dir_t::operator=(
	const stream_storage_dir_t & o )
	{
		if( &o != this )
			{
				base_type_t::operator=( o );

				m_dir = o.m_dir;
				m_id = o.m_id;
			}

		return *this;
	}

oess_1::uint_t
stream_storage_dir_t::allocate_id()
	{
		return ++m_id;
	}

void
stream_storage_dir_t::add(
	const std::string & name,
	const oess_1::uint_t & id,
	const chain_id_t & description_chain )
	{
		if( m_dir.find( name ) != m_dir.end() )
			OESS_THROW_LOGIC(
					oess_1::db::storage::err::c_stream_name_not_unique,
					name << ": stream is already in storage directory" )

		m_dir[ name ] = stream_storage_dir_item_t(
				id, description_chain );
	}

void
stream_storage_dir_t::destroy(
	const std::string & name )
	{
		m_dir.erase( locate_existent_item( name ) );
	}

oess_1::uint_t
stream_storage_dir_t::find_id(
	const std::string & name ) const
	{
		return locate_existent_item( name )->second.id();
	}

bool
stream_storage_dir_t::is_exists(
	const std::string & name ) const
	{
		return ( m_dir.find( name ) != m_dir.end() );
	}

std::set< std::string >
stream_storage_dir_t::names() const
	{
		std::set< std::string > keys;
		for( dir_t::const_iterator it = m_dir.begin(), it_end = m_dir.end();
				it != it_end; ++it )
			keys.insert( it->first );

		return keys;
	}

const stream_storage_dir_item_t &
stream_storage_dir_t::item(
	const std::string & name ) const
	{
		return locate_existent_item( name )->second;
	}

stream_storage_dir_t::dir_t::iterator
stream_storage_dir_t::locate_existent_item(
	const std::string & name )
	{
		dir_t::iterator it = m_dir.find( name );
		if( it == m_dir.end() )
			OESS_THROW_LOGIC(
					oess_1::db::storage::err::c_stream_is_not_in_directory,
					name << ": stream not found in directory" )

		return it;
	}

stream_storage_dir_t::dir_t::const_iterator
stream_storage_dir_t::locate_existent_item(
	const std::string & name ) const
	{
		dir_t::const_iterator it = m_dir.find( name );
		if( it == m_dir.end() )
			OESS_THROW_LOGIC(
					oess_1::db::storage::err::c_stream_is_not_in_directory,
					name << ": stream not found in directory" )

		return it;
	}

#include "stream_storage_dir.ddl.cpp"

} /* namespace impl */

} /* namespace storage */

} /* namespace db */

} /* namespace oess_1 */
