EnvExample.cpp
上传用户:tsgydb
上传日期:2007-04-14
资源大小:10674k
文件大小:3k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /*-
  2.  * See the file LICENSE for redistribution information.
  3.  *
  4.  * Copyright (c) 1997, 1998, 1999, 2000
  5.  * Sleepycat Software.  All rights reserved.
  6.  *
  7.  * $Id: EnvExample.cpp,v 11.12 2000/10/27 20:32:00 dda Exp $
  8.  */
  9. #include "db_config.h"
  10. #ifndef NO_SYSTEM_INCLUDES
  11. #include <sys/types.h>
  12. #include <errno.h>
  13. #include <iostream.h>
  14. #include <stddef.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <unistd.h>
  19. #endif
  20. #include <db_cxx.h>
  21. #ifdef macintosh
  22. #define DATABASE_HOME ":database"
  23. #define CONFIG_DATA_DIR ":database"
  24. #else
  25. #ifdef DB_WIN32
  26. #define DATABASE_HOME "\tmp\database"
  27. #define CONFIG_DATA_DIR "\database\files"
  28. #else
  29. #define DATABASE_HOME "/tmp/database"
  30. #define CONFIG_DATA_DIR "/database/files"
  31. #endif
  32. #endif
  33. void db_setup(char *, char *, ostream&);
  34. void db_teardown(char *, char *, ostream&);
  35. char *progname = "EnvExample"; /* Program name. */
  36. //
  37. // An example of a program creating/configuring a Berkeley DB environment.
  38. //
  39. int
  40. main(int, char **)
  41. {
  42. //
  43. // Note: it may be easiest to put all Berkeley DB operations in a
  44. // try block, as seen here.  Alternatively, you can change the
  45. // ErrorModel in the DbEnv so that exceptions are never thrown
  46. // and check error returns from all methods.
  47. //
  48. try {
  49. char *data_dir, *home;
  50. //
  51. // All of the shared database files live in /home/database,
  52. // but data files live in /database.
  53. //
  54. home = DATABASE_HOME;
  55. data_dir = CONFIG_DATA_DIR;
  56. cout << "Setup envn";
  57. db_setup(DATABASE_HOME, data_dir, cerr);
  58. cout << "Teardown envn";
  59. db_teardown(DATABASE_HOME, data_dir, cerr);
  60. return 0;
  61. }
  62. catch (DbException &dbe) {
  63. cerr << "AccessExample: " << dbe.what() << "n";
  64. return 1;
  65. }
  66. }
  67. // Note that any of the db calls can throw DbException
  68. void
  69. db_setup(char *home, char *data_dir, ostream& err_stream)
  70. {
  71. //
  72. // Create an environment object and initialize it for error
  73. // reporting.
  74. //
  75. DbEnv *dbenv = new DbEnv(0);
  76. dbenv->set_error_stream(&err_stream);
  77. dbenv->set_errpfx(progname);
  78. //
  79. // We want to specify the shared memory buffer pool cachesize,
  80. // but everything else is the default.
  81. //
  82. dbenv->set_cachesize(0, 64 * 1024, 0);
  83. // Databases are in a subdirectory.
  84. (void)dbenv->set_data_dir(data_dir);
  85. // Open the environment with full transactional support.
  86. dbenv->open(DATABASE_HOME,
  87.     DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN, 0);
  88. // Do something interesting...
  89. // Close the handle.
  90. dbenv->close(0);
  91. }
  92. void
  93. db_teardown(char *home, char *data_dir, ostream& err_stream)
  94. {
  95. // Remove the shared database regions.
  96. DbEnv *dbenv = new DbEnv(0);
  97. dbenv->set_error_stream(&err_stream);
  98. dbenv->set_errpfx(progname);
  99. (void)dbenv->set_data_dir(data_dir);
  100. dbenv->remove(home, 0);
  101. delete dbenv;
  102. }