/***************************************************************************
                Database Connection Pool
                -----------------------------------------
	begin      : 2012/05/13
	copyright  : (C) 2011 SolutionBox Inc.
	author     : Service 1 Team
	email      : svc1@solbox.com
	version    : 3.1.0

    CopyRight(C) 2011 SolutionBox Inc. All Rights reserved.
    Redistribution and use in source and binary forms, with or with out
    modification, are not permitted in outside of SolutionBox Inc.
***************************************************************************/
#ifndef __DATABASE_CONNECTION_POOL__
#define __DATABASE_CONNECTION_POOL__

class DataBase;

class conndbpool
{
public:
	enum POOL_STATUSE
	{
		POOL_ERR	=	-1,
		POOL_FREE	=	0,
		POOL_USE	=	1
	};
public:
	conndbpool(databaseinfo info);
	virtual ~conndbpool();

	int	CreatePool( int poolcnt = 4 );
	int DestroyPool();
	DataBase* GetConnFromPool(int timeout = 10);
	void ReleaseConnToPool(DataBase * t, bool success = true);
	int SetKeepAliveTimeout(int sec);

	void printstatus(string prefixed = "");

	int GetFreePoolCnt();

	bool IsExit() { return m_exitpool; }
	bool IsTimeoutAlive();
	bool SendAliveMsg();

	void			SetDatabaseInfo(databaseinfo info)	{ m_dbinfo = info; }
	databaseinfo   GetDatabaseInfo()	{ return m_dbinfo; }
	size_t GetPoolSize()	{ return m_poolmap.size(); }

	// 2014.06.06 dadamin
	// 현재 DB 형상 체크
	bool	SetTargetTableType(DataBase * d);
	// 작업할 메타 테이블명
	string	GetMetaTableName(const char * tranid);
	// display_name 컬럼 사용 유무
	bool	UseDisplayname() { return (m_tabletype == 1); }

private:
	static void* KeepPoolAlive(void*);

private:
	databaseinfo			m_dbinfo;	
	map<DataBase*, short>	m_poolmap;
	pthread_mutex_t			m_mutex;
	bool					m_exitpool;
	DataBase*				m_lastset;
	
	int						m_alivetime;
	pthread_t				m_keepalive;
	pthread_cond_t			m_alivecond;
	pthread_mutex_t			m_alivemutex;

	// 2014.06.04 dadamin
	// -1: unset, 0: t_dav_resource, 1: t_meat_[sp_svc_tran_id]
	int						m_tabletype;
};

class masterdbpool : public conndbpool
{
public:
	static	void			init(databaseinfo info);
	static	masterdbpool*	getInstance();
	static	int				release();

private:
	masterdbpool(databaseinfo info);
	~masterdbpool();

private:
	static masterdbpool*		m_inst;
};

class slavedbpool : public conndbpool
{
public:
	static	void			init(databaseinfo info);
	static	slavedbpool*	getInstance();
	static	int				release();

private:
	slavedbpool(databaseinfo info);
	~slavedbpool();

private:
	static slavedbpool*		m_inst;
};

#endif // __DATABASE_CONNECTION_POOL__
