现在的位置: 首页 > 综合 > 正文

FastDb client-server模式

2018年10月19日 ⁄ 综合 ⁄ 共 7458字 ⁄ 字号 评论关闭

转自:http://blog.csdn.net/fuyun10036/article/details/8620816感谢。

就个人理解而言,fastdb client-server模式,只是在client和server之间添加了一个socket通信,其实操作都是在server端完成的。

但是client-server也有很多好处,其中一个就是可以同一个进程可以同时运用fastdb的无盘模式,和磁盘模式。

当然其中一个模式只是client(比如这个进程开启时需要用无盘模式,将数据全部存入内存,以方便读取,但是记录日志时希望用fastdb记录到库中,这时候就可以另外开始一个server进程,用磁盘模式打开数据库,client连接到server进行insert操作。)

server端代码:

  1. // Sev_log.cpp : 定义控制台应用程序的入口点。  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. #include "fastdb.h"  
  6.   
  7. class CLog  
  8. {  
  9.     public:  
  10.         char const* sdatetime;  
  11.         int4        ProcessNo;  
  12.         int4        level;  
  13.         char const* logtext;  
  14.         char const* ifwrited;  
  15.   
  16.     TYPE_DESCRIPTOR((FIELD(sdatetime),  
  17.                      FIELD(logtext),  
  18.                      FIELD(ProcessNo),  
  19.                      FIELD(level),  
  20.                      FIELD(ifwrited)  
  21.                      ));  
  22. };  
  23.   
  24. #pragma comment(lib, "fastdb.lib")  
  25. #pragma comment(lib, "wsock32.lib")  
  26.   
  27. #if THREADS_SUPPORTED && !defined(NO_SOCKETS)  
  28. #define SERVER_SUPPORTED 1  
  29. #include "server.h"  
  30. #else  
  31. #define SERVER_SUPPORTED 0  
  32. #endif  
  33.   
  34. USE_FASTDB_NAMESPACE  
  35.   
  36. REGISTER(CLog);  
  37.   
  38. int _tmain(int argc, _TCHAR* argv[])  
  39. {     
  40.     //由于不关闭数据库的话,内存占用会越来越大,所以定时重启数据库  
  41.     while(1)  
  42.     {  
  43.         dbDatabase* m_pdb;  
  44.         m_pdb = NULL;  
  45.   
  46.         size_t dbInitSize = 16 * 1024 * 1024;           // 16M  指定了数据库文件的初始大小   缺省值为4MB  
  47.         size_t dbExtensionQuantum = 1 * 1024 * 1024;    <span style="white-space:pre">  </span>//  1M   内存分配位图的扩展量子     缺省值为4MB  
  48.         size_t dbInitIndexSize = 2 * 1024 * 1024;       //  2M  指定了初始的索引大小          64K个对象标识符  
  49.   
  50.         m_pdb = new dbDatabase(dbDatabase::dbAllAccess, dbExtensionQuantum, dbInitIndexSize);  
  51.       
  52.         if(m_pdb->open("test"))  
  53.         {  
  54.             m_pdb->scheduleBackup("backup.fdb",2); //定时备份 2s备份一次  
  55.             char serverURL[64];  
  56.             strcpy(serverURL, "localhost:2100");  
  57.             dbServer* server = dbServer::find(serverURL);  
  58.   
  59.             if (server == NULL){  
  60.                 /* 
  61.                 dbServer::dbServer(dbDatabase* db, 
  62.                     char const* serverURL,  
  63.                     int optimalNumberOfThreads,  
  64.                     int connectionQueueLen) 
  65.                 */  
  66.                 server = new dbServer(m_pdb, serverURL, 8);  
  67.                 if (server != NULL){  
  68.                     server->start();  
  69.                     printf("Server started for URL %s\n", serverURL);  
  70.                 }  
  71.             }  
  72.   
  73.             Sleep(60000);  
  74.   
  75.             if (server != NULL) {   
  76.                 server->stop();  
  77.                 delete server;  
  78.                 server = NULL;  
  79.                 printf("Server stopped for URL %s\n", serverURL);  
  80.             }   
  81.   
  82.             m_pdb->close();  
  83.             printf("fastdb close!\n");  
  84.         }  
  85.   
  86.         delete m_pdb;  
  87.         m_pdb=NULL;  
  88.     }  
  89.     return 0;  
  90. }  

client端代码:

  1. // cli_writelog.cpp : 定义控制台应用程序的入口点。  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. #include "cli.h"  
  6. #include <string.h>  
  7. #include <process.h> //windows  
  8. //#include "unistd.h"  //linux  
  9.   
  10. #include "fastdb.h"  
  11.   
  12. class CLog  
  13. {  
  14.     public:  
  15.         char const* sdatetime;  
  16.         int4        ProcessNo;  
  17.         int4        level;  
  18.         char const* logtext;  
  19.         char const* ifwrited;  
  20.   
  21.     TYPE_DESCRIPTOR((FIELD(sdatetime),  
  22.                      FIELD(logtext),  
  23.                      FIELD(ProcessNo),  
  24.                      FIELD(level),  
  25.                      FIELD(ifwrited)  
  26.                      ));  
  27. };  
  28.   
  29. #pragma comment(lib, "cli.lib")  
  30. #pragma comment(lib, "wsock32.lib")  
  31.   
  32. USE_FASTDB_NAMESPACE;  
  33.   
  34. #define SUCCESS         1  
  35. #define CREATE_STATEMENT_ERROR -1  
  36. #define BIND_COLUMN_ERROR      -2  
  37. #define INSERT_ERROR           -3  
  38. #define PRECOMMIT_ERROR        -4  
  39. #define FREE_STATEMENT_ERROR   -5  
  40.   
  41.   
  42. int WriteLog(char *format,...)  
  43. {  
  44.     FILE * fp;  
  45.     va_list args;  
  46.     char sLogFile[100];  
  47.       
  48.     memset(sLogFile,0,sizeof(sLogFile));  
  49.       
  50.     strcpy(sLogFile,"log.txt");  
  51.       
  52.     if((fp=fopen(sLogFile,"a+"))==NULL)  
  53.     {  
  54.         //printf("open file happened errors!\n");  
  55.         return -1;  
  56.     }  
  57.   
  58.     va_start(args,format);  
  59.     vfprintf(fp,format,args);  
  60.     va_end(args);  
  61.       
  62.     fclose(fp);  
  63.     return 0;  
  64. }  
  65.   
  66. int WriteLog(const char* log , const int level , const int session)  
  67. {  
  68.     const int MAXSTRLEN = 256;  
  69.     int inst_level,inst_processno;  
  70.     char inst_sdatetime[MAXSTRLEN];  
  71.     char inst_logs[MAXSTRLEN];  
  72.   
  73.     int StatementToInst = cli_statement(session, "insert into CLog");  
  74.   
  75.     if (StatementToInst < 0) {   
  76.         WriteLog("cli_statement failed with code %d\n", StatementToInst);  
  77.         return CREATE_STATEMENT_ERROR;  
  78.     }  
  79.   
  80.     int rc;//用于返回错误代码  
  81.     if ((rc = cli_column(StatementToInst, "sdatetime",cli_asciiz, NULL, &inst_sdatetime)) != cli_ok ||  
  82.         (rc = cli_column(StatementToInst, "logtext", cli_asciiz, NULL, &inst_logs)) != cli_ok       ||  
  83.         (rc = cli_column(StatementToInst, "ProcessNo", cli_int4, NULL, &inst_processno)) != cli_ok   ||  
  84.         (rc = cli_column(StatementToInst, "level", cli_int4, NULL, &inst_level)) != cli_ok )  
  85.       
  86.     {  
  87.         WriteLog("cli_column failed with code %d\n", rc);  
  88.         return BIND_COLUMN_ERROR;  
  89.     }  
  90.   
  91.     char buf[MAXSTRLEN];//用于临时存储时间  
  92.   
  93.     strcpy(inst_sdatetime,dbDateTime::current().asString(buf,sizeof buf,"%Y-%m-%d %H:%M:%S"));  
  94.     strcpy(inst_logs,log);  
  95.     inst_processno = (int)getpid();  
  96.     inst_level = level;  
  97.   
  98.     if((rc = cli_insert(StatementToInst,NULL))!= cli_ok)  
  99.     {  
  100.         WriteLog("cli_insert failed with code %d\n", rc);  
  101.         return INSERT_ERROR;  
  102.     }  
  103.   
  104.     /* 
  105.     if((rc = cli_commit(session)) != cli_ok)       //直接commit非常慢! 
  106.     { 
  107.         printf("cli_commit failed with code %d\n", rc); 
  108.         return PRECOMMIT_ERROR; 
  109.     } 
  110.     */  
  111.       
  112.       
  113.       
  114.     if((rc = cli_precommit(session)) != cli_ok)  
  115.     {  
  116.         WriteLog("cli_precommit failed with code %d\n", rc);  
  117.         return PRECOMMIT_ERROR;  
  118.     }  
  119.       
  120.       
  121.     if((rc = cli_free(StatementToInst)) != cli_ok){  
  122.         WriteLog("cli_free failed with code %d\n", rc);  
  123.         return FREE_STATEMENT_ERROR;  
  124.     }  
  125.   
  126.     return SUCCESS;  
  127. }  
  128.   
  129.   
  130. int _tmain(int argc, _TCHAR* argv[])  
  131. {  
  132.     charconst serverURL = "127.0.0.1:2100";  
  133.   
  134.     int session = cli_open(serverURL,10,1);//重连次数10,重连间隔1s  
  135.     if (session < 0) {   
  136.         printf("cli_open failed with code %d\n", session);  
  137.         return -1;  
  138.     }  
  139.       
  140.     DWORD dstart = GetTickCount();  
  141.   
  142.     for(int i=0;i < 10;i++)  
  143.         WriteLog("this is a log test",i%10,session);  
  144.   
  145.     printf("time is %d\n",GetTickCount()-dstart);  
  146.   
  147.     int rc;  
  148.     /* 
  149.     if((rc = cli_commit(session)) != cli_ok)  //批量提交速度最快  但是在提交之前,这部分日志无法取出 
  150.     { 
  151.         printf("cli_commit failed with code %d\n", rc); 
  152.         return PRECOMMIT_ERROR; 
  153.     } 
  154.     */  
  155.   
  156.     //precommit最好加上定时备份日志  
  157.     /*FastDB为数据库提供了precommit的接口,用于完成除sync到磁盘文件外的所有事物操作 
  158.     ,如释放mutex资源等。同时提供了backup接口,用来完成内存数据到磁盘文件的备份, 
  159.     甚至支持打开数据库时同时指定定时备份到磁盘文件的间隔,但是cli这个功能还没完善*/  
  160.   
  161.   
  162.     if ((rc = cli_close(session)) != 0) {   
  163.         printf("cli_close failed with code %d\n", rc);  
  164.         return -1;    
  165.     }  
  166.     printf("fastdb insert successfully!\n");  
  167.   
  168.     dstart = GetTickCount();  
  169.     for(int i=0;i < 100;i++)  
  170.         WriteLog("this is a log test\n",i%10);  
  171.   
  172.     printf("time is %d\n",GetTickCount()-dstart);                                                                    
  173.     printf("file insert successfully!\n");  
  174.   
  175.   
  176.     getchar();  
  177.   
  178.     return 0;  
  179. }  

抱歉!评论已关闭.