00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include <sys/types.h>
00018 #include <sys/stat.h>
00019 #include <fcntl.h>
00020
00021 #include "config.h"
00022
00023 #include "DurableStore.h"
00024 #include "BerkeleyDBStore.h"
00025 #include "FileSystemStore.h"
00026 #include "MemoryStore.h"
00027 #include "StorageConfig.h"
00028
00029 namespace oasys {
00030
00031 DurableStore::~DurableStore()
00032 {
00033 delete impl_;
00034 impl_ = 0;
00035
00036 if (clean_shutdown_file_ != "") {
00037
00038 unlink(clean_shutdown_file_.c_str());
00039
00040 int fd = creat(clean_shutdown_file_.c_str(), S_IRUSR);
00041 if (fd < 0) {
00042 log_err("error creating shutdown file '%s': %s",
00043 clean_shutdown_file_.c_str(), strerror(errno));
00044 } else {
00045 log_debug("successfully created clean shutdown file '%s'",
00046 clean_shutdown_file_.c_str());
00047 close(fd);
00048 }
00049 }
00050 }
00051
00052 int
00053 DurableStore::create_store(const StorageConfig& config,
00054 bool* clean_shutdown)
00055 {
00056 ASSERT(impl_ == NULL);
00057
00058 if (0) {}
00059
00060
00061 else if (config.type_ == "filesysdb")
00062 {
00063 impl_ = new FileSystemStore(logpath_);
00064 }
00065
00066
00067 else if (config.type_ == "memorydb")
00068 {
00069 impl_ = new MemoryStore(logpath_);
00070 }
00071
00072 #if LIBDB_ENABLED
00073
00074 else if (config.type_ == "berkeleydb")
00075 {
00076 impl_ = new BerkeleyDBStore(logpath_);
00077 }
00078 #endif
00079
00080 #if MYSQL_ENABLED
00081 #error Mysql support not yet added to oasys
00082 #endif // MYSQL_ENABLED
00083
00084 #if POSTGRES_ENABLED
00085 #error Postgres support not yet added to oasys
00086 #endif // POSTGRES_ENABLED
00087
00088 else
00089 {
00090 log_crit("configured storage type '%s' not implemented, exiting...",
00091 config.type_.c_str());
00092 exit(1);
00093 }
00094
00095 int err = impl_->init(config);
00096 if (err != 0)
00097 {
00098 log_err("can't initialize %s %d",
00099 config.type_.c_str(), err);
00100 return DS_ERR;
00101 }
00102
00103 if (config.leave_clean_file_) {
00104 clean_shutdown_file_ = config.dbdir_;
00105 clean_shutdown_file_ += "/.ds_clean";
00106
00107
00108 err = unlink(clean_shutdown_file_.c_str());
00109 if ((err == 0) ||
00110 (errno == ENOENT && config.init_ == true))
00111 {
00112 log_info("datastore %s was cleanly shut down",
00113 config.dbdir_.c_str());
00114 if (clean_shutdown) {
00115 *clean_shutdown = true;
00116 }
00117 } else {
00118 log_info("datastore %s was not cleanly shut down",
00119 config.dbdir_.c_str());
00120 if (clean_shutdown) {
00121 *clean_shutdown = false;
00122 }
00123 }
00124 }
00125
00126 return 0;
00127 }
00128
00129
00130 int
00131 DurableStore::get_table(StaticTypedDurableTable** table,
00132 std::string table_name,
00133 int flags,
00134 DurableObjectCache<SerializableObject>* cache)
00135 {
00136 ASSERT(cache == 0);
00137
00138
00139
00140 PrototypeVector prototypes;
00141
00142 DurableTableImpl* table_impl;
00143 int err = impl_->get_table(&table_impl, table_name, flags, prototypes);
00144 if (err != 0) {
00145 return err;
00146 }
00147
00148 *table = new StaticTypedDurableTable(table_impl, table_name);
00149 return 0;
00150 }
00151
00152
00153 int
00154 DurableStore::get_table_names(StringVector* table_names)
00155 {
00156 int err = impl_->get_table_names(table_names);
00157 return err;
00158 }
00159
00160
00161 std::string
00162 DurableStore::get_info() const
00163 {
00164 return impl_->get_info();
00165 }
00166
00167 }