20 #ifndef DEFAULT_DATABASE_CC
21 #define DEFAULT_DATABASE_CC
37 vector<string>* dataDBPaths,
40 this->nodeId = nodeId;
42 this->dbName = dbName;
44 this->logger = logger;
46 this->metaDBPath = metaDBPath;
47 this->dataDBPaths = dataDBPaths;
49 if (this->metaDBPath.compare(
"") != 0) {
50 this->conf->createDir(this->metaDBPath);
52 for (i = 0; i < this->dataDBPaths->size(); i++) {
53 this->conf->createDir(this->dataDBPaths->at(i));
55 this->types =
new map<UserTypeID, TypePtr>();
56 pthread_mutex_init(&this->typeOpLock,
nullptr);
58 this->flushBuffer = flushBuffer;
63 if (this->types !=
nullptr) {
66 pthread_mutex_destroy(&this->typeOpLock);
73 sprintf(buffer,
"%s/%d_%s", dbPath.c_str(), typeId, typeName.c_str());
74 return string(buffer);
79 if (this->types->find(
id) != this->types->end()) {
80 this->logger->writeLn(
"DefaultDatabase: type exists.");
83 string metaTypePath = encodeTypePath(this->metaDBPath,
id, name);
84 vector<string>* dataTypePaths =
new vector<string>();
86 for (i = 0; i < this->dataDBPaths->size(); i++) {
87 string dataTypePath = encodeTypePath(this->dataDBPaths->at(i), id, name);
88 dataTypePaths->push_back(dataTypePath);
90 TypePtr type = make_shared<UserType>(this->nodeId,
108 if (this->types->find(typeId) != this->types->end()) {
109 this->logger->writeLn(
"DefaultDatabase: type exists.");
112 pthread_mutex_lock(&this->typeOpLock);
113 types->insert(pair<UserTypeID, TypePtr>(typeId, type));
114 pthread_mutex_unlock(&this->typeOpLock);
120 this->logger->writeInt(typeId);
121 map<UserTypeID, TypePtr>::iterator it = types->find(typeId);
122 if (it != types->end()) {
123 pthread_mutex_lock(&this->typeOpLock);
124 this->clearType(typeId, it->second->getName());
125 this->types->erase(it);
126 pthread_mutex_unlock(&this->typeOpLock);
129 this->logger->writeLn(
"DefaultDatabase: Type doesn't exist:");
130 this->logger->writeInt(typeId);
138 remove(this->metaDBPath.c_str());
140 for (i = 0; i < this->dataDBPaths->size(); i++) {
141 typePath = encodeTypePath(this->dataDBPaths->at(i), typeId, typeName);
142 boost::filesystem::remove_all(typePath.c_str());
148 map<UserTypeID, TypePtr>::iterator it = this->types->find(typeId);
149 if (it != this->types->end()) {
152 this->logger->writeLn(
"DefaultDatabase: type doesn't exist");
170 using namespace boost::filesystem;
177 if (exists(metaDBDir)) {
178 if (is_directory(metaDBDir)) {
179 vector<path> typeDirs;
180 copy(directory_iterator(metaDBDir), directory_iterator(), back_inserter(typeDirs));
181 vector<path>::iterator iter;
186 for (iter = typeDirs.begin(); iter != typeDirs.end(); iter++) {
187 if (is_directory(*iter)) {
189 path = std::string(iter->c_str());
190 dirName = path.substr(path.find_last_of(
'/') + 1, path.length() - 1);
192 name = dirName.substr(dirName.find(
'_') + 1, dirName.length() - 1);
194 typeId = stoul(dirName.substr(0, dirName.find(
'_')));
197 this->addTypeByPartitionedFiles(name, typeId, path);
201 this->logger->writeLn(
"DefaultDatabase: dbDir doesn't exist:");
202 this->logger->writeLn(metaDBDir.c_str());
216 boost::filesystem::path metaTypeDir) {
217 if (this->types->find(
id) != this->types->end()) {
218 this->logger->writeLn(
"DefaultDatabase: type exists.");
221 vector<string>* dataTypePaths =
new vector<string>();
222 int numDataPaths = this->dataDBPaths->size();
225 for (i = 0; i < numDataPaths; i++) {
226 dataTypePath = this->encodeTypePath(dataDBPaths->at(i), id, name);
227 dataTypePaths->push_back(dataTypePath);
230 TypePtr type = make_shared<UserType>(this->nodeId,
237 string(metaTypeDir.c_str()),
241 if (type ==
nullptr) {
242 this->logger->error(
"Fatal Error: DefaultDatabase: Out of Memory.");
245 type->initializeFromMetaTypeDir(metaTypeDir);
255 if (is_directory(dbDir)) {
256 vector<path> typeDirs;
257 copy(directory_iterator(dbDir), directory_iterator(), back_inserter(typeDirs));
258 vector<path>::iterator iter;
263 for (iter = typeDirs.begin(); iter != typeDirs.end(); iter++) {
264 if (is_directory(*iter)) {
265 this->logger->writeLn(
"DefaultDatabase: find a type at path:");
266 this->logger->writeLn(iter->c_str());
267 path = std::string(iter->c_str());
268 dirName = path.substr(path.find_last_of(
'/') + 1, path.length() - 1);
269 name = dirName.substr(dirName.find(
'_') + 1, dirName.length() - 1);
270 this->logger->writeLn(
"DefaultDatabase: typeName:");
271 this->logger->writeLn(name.c_str());
272 typeId = stoul(dirName.substr(0, dirName.find(
'_')));
273 this->logger->writeLn(
"DefaultDatabase: typeId:");
274 this->logger->writeInt(typeId);
275 cout <<
"DefaultDatabase: detect type at path: " << path <<
"\n";
276 cout <<
"Type name: " << name <<
"\n";
277 cout <<
"Type ID:" << typeId <<
"\n";
278 this->addTypeBySequenceFiles(name, typeId, path);
282 this->logger->writeLn(
"DefaultDatabase: dbDir doesn't exist:");
283 this->logger->writeLn(dbDir.c_str());
297 boost::filesystem::path typeDir) {
298 if (this->types->find(
id) != this->types->end()) {
299 this->logger->writeLn(
"DefaultDatabase: type exists.");
302 vector<string>* dataTypePaths =
new vector<string>();
303 dataTypePaths->push_back(std::string(typeDir.c_str()));
304 TypePtr type = make_shared<UserType>(this->nodeId,
315 if (type ==
nullptr) {
316 this->logger->writeLn(
"Fatal Error: DefaultDatabase: Out of Memory.");
319 type->initializeFromTypeDir(typeDir);
bool addType(TypePtr type)
shared_ptr< UserType > TypePtr
shared_ptr< PageCache > PageCachePtr
bool initializeFromMetaDBDir(boost::filesystem::path metaDBDir)
bool removeType(UserTypeID typeID)
void addTypeBySequenceFiles(string name, UserTypeID id, boost::filesystem::path typeDir)
DatabaseID getDatabaseID()
shared_ptr< SharedMem > SharedMemPtr
void clearType(UserTypeID typeId, string typeName)
string encodeTypePath(string dbPath, UserTypeID typeId, string typeName)
TypePtr getType(UserTypeID typeId)
DefaultDatabase(NodeID nodeId, DatabaseID dbId, string dbName, ConfigurationPtr conf, pdb::PDBLoggerPtr logger, SharedMemPtr shm, string metaDBPath, vector< string > *dataDBPaths, PageCachePtr cache, PageCircularBufferPtr flushBuffer)
shared_ptr< Configuration > ConfigurationPtr
bool initializeFromDBDir(boost::filesystem::path dbDir)
map< UserTypeID, TypePtr > * getTypes()
std::shared_ptr< PDBLogger > PDBLoggerPtr
shared_ptr< PageCircularBuffer > PageCircularBufferPtr
void addTypeByPartitionedFiles(string name, UserTypeID id, boost::filesystem::path metaTypeDir)