/* ----------------------------------------------------------------------------
 * Raima Database Manager
 *
 * Copyright (c) 2010 Raima Inc.,  All rights reserved.
 *
 * Use of this software, whether in source code format, or in executable,
 * binary object code form, is governed by the Raima LICENSE which
 * is fully described in the LICENSE.TXT file, included within this
 * distribution of files.
 * ----------------------------------------------------------------------------
 */

/* tfs.h - Contains definitions for the TFS */
#if !defined(TFS_H)
#define TFS_H

#include "rdm.h"
#include "tfs_lm.h"
#include "transport.h"

typedef enum {
    DBS_MIRRORING = 0,
    DBS_CHKPTFREQ,
    DBS_DBFILELOC,
    DBS_SINGLESLAVE_LASTTXID,
    DBS_RESET_TXID,
    DBS_LOGFILE_TRANS,
    DBS_LOGFILE_SIZE,
    DBS_LOGFILES,
    DBS_LOGFILES_OPEN
} DB_SETTING_TYPE;

/* Mirroring Synchronous Mode */
typedef enum {
    MSM_ENABLE = 0,
    MSM_DISABLE,
    MSM_IASYNC
} MSM_TYPE;

typedef enum {
    TX_BEGIN = 0,
    TX_END,
    TX_ABORT
} TX_TYPE;

typedef enum {
    TX_INI_RELEASE = 0,
    TX_INI_SIGNAL,
    TX_INI_REMOVE
} TX_INI_TYPE;

/* Delete By TXID Modes */
typedef enum {
    DBTM_EXCEPT = 0,
    DBTM_LESS,
    DBTM_EQUAL,
    DBTM_ALL
} DBTM_TYPE;

#if defined(__cplusplus)
extern "C" {
#endif

#define TFS_INI "tfs.ini"
RDM_EXPORT int32_t EXTERNAL_FCN tx_discon(TX_USER, void *);
RDM_EXPORT int32_t EXTERNAL_FCN tx_rpc_reqhandler(void *);
RDM_EXPORT int32_t EXTERNAL_FCN tx_setParams(const char *, uint32_t, uint32_t);
RDM_EXPORT int32_t EXTERNAL_FCN tx_tfsinit(const char *, const TFS_PARAMS *, TFS_HANDLE *, TFS_TYPE);
#if !defined(RDM_NO_UNICODE)
RDM_EXPORT int32_t EXTERNAL_FCN tx_tfsinitW(const wchar_t *, const TFS_PARAMSW *, TFS_HANDLE *, TFS_TYPE);
#endif
RDM_EXPORT int32_t EXTERNAL_FCN tx_tfsxapiparams(TFS_HANDLE, const TFS_XAPI_PARAMS *);
RDM_EXPORT int32_t EXTERNAL_FCN tx_tfsterm(TFS_HANDLE);
RDM_EXPORT int32_t EXTERNAL_FCN tx_tfsrun(TFS_HANDLE, DB_BOOLEAN, uint16_t *);
RDM_EXPORT int32_t EXTERNAL_FCN tx_tfsstop(TFS_HANDLE);
RDM_EXPORT int32_t EXTERNAL_FCN tx_tfsdisconnect(TX_USER, const char *);
RDM_EXPORT int32_t EXTERNAL_FCN tx_tfsversion(uint16_t *, uint16_t *, TFS_LIB_TYPE *);
RDM_EXPORT int32_t EXTERNAL_FCN tx_dbSetting(TX_USER, TX_DB, const char *, DB_SETTING_TYPE, const char *);

/***************************************************************************
  User functions
*/
RDM_EXPORT int32_t EXTERNAL_FCN tx_ping(const TX_CONN_CTX *);
RDM_EXPORT int32_t EXTERNAL_FCN tx_login(const TX_CONN_CTX *, const char *, TX_USER *, UUID_VAL *);
RDM_EXPORT int32_t EXTERNAL_FCN tx_logout(TX_USER userID); /*lint -esym(534,tx_logout) */
RDM_EXPORT int32_t EXTERNAL_FCN tx_verify(TX_USER userID);
RDM_EXPORT int32_t EXTERNAL_FCN tx_getDbUserId(TX_USER, char *, uint32_t, uint32_t *);
RDM_EXPORT int32_t EXTERNAL_FCN tx_getDocroot(TX_USER, char *, uint32_t, uint32_t *);
RDM_EXPORT int32_t EXTERNAL_FCN tx_shutdown(const TX_CONN_CTX *);

/***************************************************************************
  INI functions
*/
RDM_EXPORT int32_t EXTERNAL_FCN tx_iniGetInt32(TX_USER, const char*, const char*, const char*, int32_t*);
RDM_EXPORT int32_t EXTERNAL_FCN tx_iniGetUInt64(TX_USER, const char*, const char*, const char*, uint64_t*);
RDM_EXPORT int32_t EXTERNAL_FCN tx_iniGetString(TX_USER, const char*, const char*, const char*, char **, PSP_MEMTAG);
RDM_EXPORT int32_t EXTERNAL_FCN tx_iniSet(TX_USER, const char*, const char*, const char*, const char*);
RDM_EXPORT int32_t EXTERNAL_FCN tx_iniSetDbSetting(TX_USER, const char*, const char*, const char*, const char*);
RDM_EXPORT int32_t EXTERNAL_FCN tx_iniRemoveFile(TX_USER, const char *);
RDM_EXPORT int32_t EXTERNAL_FCN tx_iniReceiveFile(TX_USER, const char *, const char *);
RDM_EXPORT int32_t EXTERNAL_FCN tx_iniSendFile(TX_USER, const char *, const char *);
RDM_EXPORT int32_t EXTERNAL_FCN tx_iniReadFile(TX_USER, const char *, const char **, uint32_t *);
RDM_EXPORT int32_t EXTERNAL_FCN tx_iniWriteFile(TX_USER, const char *, uint32_t, const char *);

/***************************************************************************
  INI file editing functions
*/
RDM_EXPORT int32_t EXTERNAL_FCN tx_iniBeginEdit(TX_USER, const char *);
RDM_EXPORT int32_t EXTERNAL_FCN tx_iniEndEdit(TX_USER, const char *, TX_INI_TYPE);

/***************************************************************************
  INI file change detection functions
*/
RDM_EXPORT int32_t EXTERNAL_FCN tx_iniTimestamps(TX_USER, const char *, uint64_t *, uint64_t *);

/***************************************************************************
  File functions
*/
RDM_EXPORT int32_t EXTERNAL_FCN tx_fileCreate(TX_USER userID, TX_DB dbID, const char *fileName, uint32_t pageSize, uint16_t flags, TX_FILE *file);
RDM_EXPORT int32_t EXTERNAL_FCN tx_fileOpen(TX_USER userID, TX_DB dbID, const char *fileName, uint32_t pageSize, uint16_t flags, TX_FILE *file);
RDM_EXPORT int32_t EXTERNAL_FCN tx_fileClose(TX_USER userID, TX_FILE file);
RDM_EXPORT int32_t EXTERNAL_FCN tx_fileRead(TX_USER userID, TX_FILE file, PSP_FILE_OFFSET offset, uint32_t length, uint32_t *nBytes, void *ptr);
RDM_EXPORT int32_t EXTERNAL_FCN tx_fileWrite(TX_USER userID, TX_FILE file, PSP_FILE_OFFSET offset, uint32_t length, const void *ptr);
RDM_EXPORT int32_t EXTERNAL_FCN tx_fileReadPage(TX_USER userID, TX_FILE file, uint64_t page, uint32_t size, void *ptr, FILE_NO fref, uint32_t *timestamp);
RDM_EXPORT int32_t EXTERNAL_FCN tx_fileLength(TX_USER userID, TX_FILE file, PSP_FILE_OFFSET *length);
RDM_EXPORT int32_t EXTERNAL_FCN tx_fileSetSize(TX_USER userID, TX_FILE file, PSP_FILE_OFFSET length);
RDM_EXPORT int32_t EXTERNAL_FCN tx_fileRename(TX_USER userID, TX_DB dbID, const char *oldName, uint16_t, const char *newName, uint16_t);
RDM_EXPORT int32_t EXTERNAL_FCN tx_fileRemove(TX_USER userID, TX_DB dbID, const char *fileName, uint16_t xflags);
RDM_EXPORT int32_t EXTERNAL_FCN tx_fileCopy(TX_USER userID, TX_DB dbID, const char *oldName, const char *newName);
RDM_EXPORT int32_t EXTERNAL_FCN tx_fileValidate(TX_USER userID, TX_DB dbID, const char *fileName, uint16_t xflags);
RDM_EXPORT int32_t EXTERNAL_FCN tx_fileGetTimestamp(TX_USER userID, FILE_NO fref, uint32_t *timestamp);

/***************************************************************************
  Log file functions
*/
RDM_EXPORT int32_t EXTERNAL_FCN tx_LOGbegin(TX_USER userID, TX_DB dbID);
RDM_EXPORT int32_t EXTERNAL_FCN tx_LOGwrite(TX_USER userID, TX_FILE file, uint64_t page, uint32_t length, const void *pgptr);
RDM_EXPORT int32_t EXTERNAL_FCN tx_LOGend(TX_USER userID, TX_DB dbID);
RDM_EXPORT int32_t EXTERNAL_FCN tx_LOGcommit(TX_USER userID, TX_DB dbID);
RDM_EXPORT int32_t EXTERNAL_FCN tx_LOGprecommit(TX_USER userID, TX_DB dbID);
RDM_EXPORT int32_t EXTERNAL_FCN tx_LOGrollback(TX_USER userID, TX_DB dbID);
RDM_EXPORT int32_t EXTERNAL_FCN tx_LOGinit(TX_USER, TX_FILE);
RDM_EXPORT int32_t EXTERNAL_FCN tx_LOGsynchronous(TX_USER, const char *, MSM_TYPE);
RDM_EXPORT int32_t EXTERNAL_FCN tx_LOGmirSync(TX_USER, TX_DB, TXID);
RDM_EXPORT int32_t EXTERNAL_FCN tx_LOGmirSyncError(TX_USER, TX_DB, int32_t);
RDM_EXPORT int32_t EXTERNAL_FCN tx_LOGisDbSynchronous(TX_USER, const char *, uint16_t *);
RDM_EXPORT int32_t EXTERNAL_FCN tx_LOGisDbReplicated(TX_USER, const char *, uint16_t *);
RDM_EXPORT int32_t EXTERNAL_FCN tx_LOGwaitForCommit(TX_USER, TX_DB, TXID, uint32_t);
RDM_EXPORT int32_t EXTERNAL_FCN tx_LOGstopWaitForCommit(TX_USER, TX_DB);
RDM_EXPORT int32_t EXTERNAL_FCN tx_LOGsetNextTxid(TX_USER, TX_DB, TXID);
RDM_EXPORT int32_t EXTERNAL_FCN tx_LOGsDeleteByTxid(TX_USER, const char *, const char *, TXID, DBTM_TYPE, uint16_t);
RDM_EXPORT int32_t EXTERNAL_FCN tx_LOGendLogify(TX_USER, TX_DB, TXID, const char *, uint16_t);

/***************************************************************************
  MIRRORING functions
*/
RDM_EXPORT int32_t EXTERNAL_FCN tx_MIRactivate(TX_USER, const char *, int16_t);
RDM_EXPORT int32_t EXTERNAL_FCN tx_MIRisTransActive(TX_USER, const char *, uint16_t *);
RDM_EXPORT int32_t EXTERNAL_FCN tx_MIRmarkTrans(TX_USER, TX_TYPE);
RDM_EXPORT int32_t EXTERNAL_FCN tx_MIRcreateLog(TX_USER, TX_DB, TXID);
RDM_EXPORT int32_t EXTERNAL_FCN tx_MIRapplyLog(TX_USER, TX_DB, const char *);

/***************************************************************************
  Read-Only Transaction functions
*/
RDM_EXPORT int32_t EXTERNAL_FCN tx_ROTbegin(TX_USER userID, TX_DB dbID, TXID *txID);
RDM_EXPORT int32_t EXTERNAL_FCN tx_ROTend(TX_USER userID, TX_DB dbID);

/***************************************************************************
  Database functions
*/
RDM_EXPORT int32_t EXTERNAL_FCN tx_dbCreate(TX_USER userID, const char *dbName, TX_DB *db);
RDM_EXPORT int32_t EXTERNAL_FCN tx_dbCreateMir(TX_USER userID, const char *dbName, DFL_TYPE, TX_DB *db);
RDM_EXPORT int32_t EXTERNAL_FCN tx_dbOpen(TX_USER userID, const char *dbName, uint16_t, TX_DB *db);
RDM_EXPORT int32_t EXTERNAL_FCN tx_dbOpenMir(TX_USER userID, const char *dbName, DFL_TYPE, TX_DB *db);
RDM_EXPORT int32_t EXTERNAL_FCN tx_dbClose(TX_USER userID, TX_DB db);
RDM_EXPORT int32_t EXTERNAL_FCN tx_dbFileInfo(TX_USER, TX_DB, const char *, uint16_t, uint16_t *, char (*)[FILENMLEN], uint16_t *, uint16_t *, uint16_t *, uint16_t *, char *, PSP_FILE_OFFSET *);
RDM_EXPORT int32_t EXTERNAL_FCN tx_dbLastCommittedTxid(TX_USER, TX_DB, TXID *);
RDM_EXPORT int32_t EXTERNAL_FCN tx_dbLastTxid(TX_USER, TX_DB, TXID *);
RDM_EXPORT int32_t EXTERNAL_FCN tx_dbNames(TX_USER, uint16_t *, const char ***);
RDM_EXPORT int32_t EXTERNAL_FCN tx_dbDestroy(TX_USER userID, TX_DB db);
RDM_EXPORT int32_t EXTERNAL_FCN tx_dbDumpCache(TX_USER userID, TX_DB dbID);
RDM_EXPORT int32_t EXTERNAL_FCN tx_dbIMFlush(TX_USER userID, TX_DB dbID);
RDM_EXPORT int32_t EXTERNAL_FCN tx_dbSetInMemory(TX_USER userID, TX_DB dbID);
RDM_EXPORT int32_t EXTERNAL_FCN tx_dbGetInMemory(TX_USER userID, TX_DB dbID, uint16_t *);
RDM_EXPORT int32_t EXTERNAL_FCN tx_dbUsers(TX_USER, const char *, uint16_t *, const char ***, TXID **);
RDM_EXPORT int32_t EXTERNAL_FCN tx_dbMove(TX_USER, TX_DB, const char *);
RDM_EXPORT int32_t EXTERNAL_FCN tx_dbGetFilesLike(TX_USER userID, TX_DB dbID, const char *, uint32_t *, const char ***);

/***************************************************************************
  Lock Manager Transaction functions
*/
RDM_EXPORT int32_t EXTERNAL_FCN tx_lmSettime(TX_USER userID, uint32_t timeout);
RDM_EXPORT int32_t EXTERNAL_FCN tx_lmDbOpen(TX_USER userID, uint16_t num_files, RDM_OPEN_MODE mode, const char *const *fnames, uint16_t *fref);
RDM_EXPORT int32_t EXTERNAL_FCN tx_lmDbClose(TX_USER userID, uint16_t num_files, const uint16_t *frefs);
RDM_EXPORT int32_t EXTERNAL_FCN tx_lmFree(TX_USER userID, uint16_t num_files, const uint16_t *frefs, uint16_t *num_timestamps, uint16_t *file_ids, uint32_t *timestamps);
RDM_EXPORT int32_t EXTERNAL_FCN tx_lmLock(TX_USER userID, uint16_t num_files, uint16_t read_lock_secs, uint16_t write_locks_secs, const TFS_LM_LOCKREQ *locks, uint16_t *num_timestamps, uint16_t *file_ids, uint32_t *timestamps);
RDM_EXPORT int32_t EXTERNAL_FCN tx_lmClearUser(TX_USER userID, const char *lm_user);
RDM_EXPORT int32_t EXTERNAL_FCN tx_lmUserStatus(TX_USER userID, const char *lm_username, int16_t *user_status);
RDM_EXPORT int32_t EXTERNAL_FCN tx_lmGeneralStatus(TX_USER userID, TFS_LM_STATUS *lm_status);
RDM_EXPORT int32_t EXTERNAL_FCN tx_lmUserInfo(TX_USER userID, uint16_t lm_userno, TFS_LM_USERINFO *lm_userinfo, uint16_t lm_num_bmbytes, uint8_t *lm_uopen, uint8_t *lm_ulock, uint8_t *lm_ureq, uint16_t lm_num_files, TFS_LM_FILEINFO *lm_files);
RDM_EXPORT int32_t EXTERNAL_FCN tx_lmUserList(TX_USER userID, const char **);
RDM_EXPORT int32_t EXTERNAL_FCN tx_lmEndConnection(TX_USER, RHS_HANDLE, const char *);

/***************************************************************************
  b_log functions
*/
RDM_EXPORT int32_t EXTERNAL_FCN tx_blog_create(TX_USER, uint16_t,  const char *, uint16_t, const char *, size_t, uint64_t, uint64_t, uint64_t, uint64_t, uint16_t, LOG_FILE_CTX **);
RDM_EXPORT int32_t EXTERNAL_FCN tx_blog_open(TX_USER, uint16_t, const char *, uint16_t, char *, size_t, uint16_t, LOG_FILE_CTX **);
RDM_EXPORT int32_t EXTERNAL_FCN tx_blog_close(TX_USER, uint16_t, const LOG_FILE_CTX *);
RDM_EXPORT int32_t EXTERNAL_FCN tx_blog_putNewFile(TX_USER, uint16_t, LOG_FILE_CTX *, const TX_DB_FILE_HDR *, uint64_t *);
RDM_EXPORT int32_t EXTERNAL_FCN tx_blog_putNewPage(TX_USER, uint16_t, LOG_FILE_CTX *, RDM_COMPRESSION_TYPE, uint64_t, uint64_t, uint64_t *, const void *, uint32_t);
RDM_EXPORT int32_t EXTERNAL_FCN tx_blog_findFirstTxidFromDirectory(TX_USER, uint16_t, const char *, uint16_t, TXID *, const char *);
RDM_EXPORT int32_t EXTERNAL_FCN tx_blog_findLastTxidFromDirectory(TX_USER, uint16_t, const char *, uint16_t, TXID *, const char *);
RDM_EXPORT int32_t EXTERNAL_FCN tx_blog_getTransHdr(TX_USER, uint16_t, LOG_FILE_CTX *, TXID, TX_ENTRY_HDR *);
RDM_EXPORT int32_t EXTERNAL_FCN tx_blog_getNextDbPage(TX_USER, uint16_t, LOG_FILE_CTX *, void *, uint32_t, uint32_t *, TX_PAGE_HDR *);
RDM_EXPORT int32_t EXTERNAL_FCN tx_blog_getNextDbFile(TX_USER, uint16_t, LOG_FILE_CTX *, TX_DB_FILE_HDR *, DFL_TYPE);
RDM_EXPORT int32_t EXTERNAL_FCN tx_blog_precommit(TX_USER, uint16_t, LOG_FILE_CTX *, TXID, uint16_t, uint64_t);
RDM_EXPORT int32_t EXTERNAL_FCN tx_blog_commit(TX_USER, uint16_t, LOG_FILE_CTX *, TXID);
RDM_EXPORT int32_t EXTERNAL_FCN tx_blog_transSetStatus(TX_USER, uint16_t, LOG_FILE_CTX *, TXID, TX_ENTRY_STATUS);

#if defined(__cplusplus)
}
#endif

#endif  /* TFS_H */
