DurableStore.cc

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

Generated on Fri Dec 22 14:47:58 2006 for DTN Reference Implementation by  doxygen 1.5.1