#ifndef _CEGODISTMANAGER_H_INCLUDED_
#define _CEGODISTMANAGER_H_INCLUDED_
///////////////////////////////////////////////////////////////////////////////
//                                                         
// CegoDistManager.h
// -----------------
// Cego distribution manager class definition
// 
// Design and Implementation by Bjoern Lemke
//
// (C)opyright 2000-2010 Bjoern Lemke
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2, or (at your option)
// any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; see the file COPYING.  If not, write to
// the Free Software Foundation, 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.
//
// INTERFACE MODULE
//
// Class: CegoDistManager
// 
// Description: 
//
// Status: QG-2.6
//
///////////////////////////////////////////////////////////////////////////////

#include <lfc/Chain.h>
#include <lfc/ListT.h>

#include "CegoField.h"
#include "CegoPredDesc.h"
#include "CegoTableManager.h"
#include "CegoDistDbHandler.h"
#include "CegoLockHandler.h"
#include "CegoDatabaseManager.h"

class CegoDistManager : public CegoTableManager {

public:
    
    CegoDistManager(CegoDatabaseManager *pDBMng);
    ~CegoDistManager();

    void startDistTableSet(const Chain& tableSet, const Chain& secondary, bool cleanIt);
    void startTableSet(const Chain& tableSet, const Chain& secondary, bool cleanIt);
    void registerObjects(const Chain& tableSet);

    void stopDistTableSet(const Chain& tableSet, bool archComplete);

    bool distObjectExists( const Chain& tableSet, const Chain& objName, CegoObject::ObjectType objType);

    void getDistObject(const Chain& tableSet, const Chain& objName, CegoObject::ObjectType type, CegoDecodableObject& oe);
    void getLocalObject(int tabSetId, const Chain& objName, CegoObject::ObjectType type, CegoDecodableObject& oe);

    void createDistDataTable(const Chain& tableSet, const Chain& tableName, CegoObject::ObjectType type, ListT<CegoField>& fldList, ListT<CegoField>& idxList, bool useColumnId = false);
    void createLocalDataTable(int tabSetId, const Chain& tableName, CegoObject::ObjectType type, ListT<CegoField>& fldList, ListT<CegoField>& idxList, bool useColumnId = false);

    void createDistForeignKey( const Chain& tableSet, const Chain& fkey, const Chain& tableName, const ListT<CegoField>& keyList, const Chain& refTable, const ListT<CegoField>& refList);
    void createLocalForeignKey(int tabSetId, const Chain& fkey, const Chain& tableName, const ListT<CegoField>& keyList, const Chain& refTable, const ListT<CegoField>& refList);    

    void createDistCheck( const Chain& tableSet, const Chain& checkName, const Chain& tableName, CegoPredDesc *pPredDesc);
    void createLocalCheck(int tabSetId, const Chain& checkName, const Chain& tableName, CegoPredDesc *pPredDesc);

    void reorgDistObject(const Chain& tableSet, const Chain& tableName, CegoObject::ObjectType type);

    void createDistIndexTable( const Chain& tableSet, const Chain& indexName, const Chain& tableName, ListT<CegoField>& schema, CegoObject::ObjectType type);
    void createLocalIndexTable(int tabSetId, const Chain& indexName, const Chain& tableName, CegoObject::ObjectType type, ListT<CegoField>& idxList);

    void insertDistDataTable(CegoTableObject& oe, ListT<CegoField>& fvl);
    void insertLocalDataTable(CegoTableObject& oe, ListT<CegoField>& fvl);

    long deleteDistDataTable(CegoTableObject& oe, CegoPredDesc* pPred, CegoProcBlock* pBlock = 0);
    long deleteLocalDataTable(CegoTableObject& oe, CegoPredDesc* pPred, CegoProcBlock* pBlock = 0);

    long updateDistDataTable(CegoTableObject& oe, CegoPredDesc* pPred, ListT<CegoField>& updSchema, ListT<CegoExpr*>& exprList, CegoProcBlock* pBlock = 0);

    long updateLocalDataTable(CegoTableObject& oe, CegoPredDesc* pPred, ListT<CegoField>& updSchema, ListT<CegoExpr*>& exprList, CegoProcBlock* pBlock = 0);

    void alterDistDataTable(CegoTableObject& oe, const ListT<CegoAlterDesc>& alterList);
    
    void renameDistObject(const Chain& tableSet, const Chain& objName, CegoObject::ObjectType type,  const Chain& newObjName);
    void renameLocalObject(int tabSetId, const Chain& objName, CegoObject::ObjectType type,  const Chain& newObjName);
    void createDistView(const Chain& tableSet, const Chain& viewName, const ListT<CegoField>& schema, const Chain& viewText);
    void createLocalView(int tabSetId, const Chain& viewName, const ListT<CegoField>& schema, const Chain& viewText);

    void createDistProc(const Chain& tableSet, const Chain& procName, const Chain& procText);

    void createLocalProc(int tabSetId, const Chain& procName, const Chain& procText);

    void dropDistObject(const Chain& tableName, const Chain& tableSet, CegoObject::ObjectType type);
    void dropLocalObject(int tabSetId, const Chain& tableName, CegoObject::ObjectType type);

    void getDistObjectList(const Chain& tableSet, CegoObject::ObjectType type, ListT<Chain> &objList);
    void getLocalObjectList(int tabSetId, CegoObject::ObjectType type, ListT<Chain> &objList);

    void getDistObjectByTableList(const Chain& tableSet, const Chain& tabName, ListT<CegoTableObject> &idxList, ListT<CegoBTreeObject> &btreeList, ListT<CegoKeyObject>& keyList, ListT<CegoCheckObject>& checkList, bool ignoreInvalid = true);

    CegoAttrCond::IndexMatch distIndexExists(int tabSetId, const Chain& tableName, CegoAttrCond& ac);

    int getDistPageCount(const Chain& tableSet, const Chain& tabName, CegoObject::ObjectType type);

    void startDistTransaction( const Chain& tableSet);
    long endDistTransaction( const Chain& tableSet);
    long rollbackDistTransaction( const Chain& tableSet);

    int getDistTid( const Chain& tableSet);
    int getLocalTid(int tabSetId);


    void setThreadId(long tid);
    long getThreadId() const;

    void setAllocatedSortArea(long n);
    long getAllocatedSortArea() const;


    void syncDistTableSet(const Chain& tableSet, const Chain& msg, const Chain& escCmd, int timeout);

    void enableAuth();
    void disableAuth();

    void setActiveUser(const Chain& tableSet, const Chain& user, const Chain& passwd);

    CegoLockHandler* getLockHandle();
    
    const Chain& getUser() const;

    void getActiveUser(const Chain& tableSet, Chain& user, Chain& password);

    Element* verifyTable(const Chain& tableSet, const Chain& tableName);
    Element* verifyView(const Chain& tableSet, const Chain& viewName);
    Element* verifyProcedure(const Chain& tableSet, const Chain& procName);

    Element* correctTable(const Chain& tableSet, const Chain& tableName);

    void getObjectDesc(const Chain& tableSet, const Chain& objName, CegoObject::ObjectType type, 
		       ListT<CegoField>& schema, ListT< ListT<CegoFieldValue> > &fa);


    CegoView* getView(int tabSetId, const Chain& viewName);
    CegoProcedure* getProcedure(int tabSetId, const Chain& procName);

private:

    void reloadView(int tabSetId, const Chain& viewName);
    void reloadProcedure(int tabSetId, const Chain& procName);

    bool checkKey(CegoKeyObject *pKey);
    
    class ActiveTSUser {
	
    public:
        
        ActiveTSUser();
        ActiveTSUser(const Chain& tableSet);
        ActiveTSUser (const Chain& tableSet, const Chain& userName, const Chain& passwd );
        ~ActiveTSUser();
	
        const Chain& getUserName() const;
        const Chain& getPasswd() const;

        ActiveTSUser& operator = (const ActiveTSUser& u); 
	bool operator == (const ActiveTSUser& t) const;
        
    private:
        
	Chain _tableSet;
        Chain _userName;
	Chain _passwd;
	
    };

    ListT<ActiveTSUser> _userList;

    Chain _authUser;
    bool _authEnabled;

    long _threadId;
    long _numAllocated;

    unsigned long _modId;

};

#endif
