MCPcopy Create free account
hub / github.com/LadybugDB/ladybug / dropGraph

Method dropGraph

src/main/database_manager.cpp:209–267  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

207}
208
209void DatabaseManager::dropGraph(const std::string& graphName, main::ClientContext* clientContext) {
210 if (StringUtils::caseInsensitiveEquals(graphName, "main")) {
211 throw BinderException{"Cannot drop the main graph."};
212 }
213
214 auto upperCaseName = StringUtils::getUpper(graphName);
215
216 // Check if graph exists in system catalog first
217 auto mainCatalog = clientContext->getDatabase()->getCatalog();
218 auto transaction = TransactionContext::Get(*clientContext)->getActiveTransaction();
219 if (!mainCatalog->containsGraph(transaction, graphName)) {
220 throw RuntimeException{std::format("No graph named {}.", graphName)};
221 }
222
223 // Remove from system catalog
224 mainCatalog->dropGraph(transaction, graphName);
225
226 for (auto it = graphs.begin(); it != graphs.end(); ++it) {
227 auto graphNameUpper = StringUtils::getUpper((*it)->getCatalogName());
228 if (graphNameUpper == upperCaseName) {
229 if (defaultGraph != "" && StringUtils::getUpper(defaultGraph) == upperCaseName) {
230 defaultGraph = "";
231 }
232 auto storageManager = (*it)->getStorageManager();
233 std::string graphPath;
234 if (storageManager != nullptr) {
235 graphPath = storageManager->getDatabasePath();
236 storageManager->closeFileHandle();
237 }
238 if (hasAttachedDatabase(graphName)) {
239 detachDatabase(graphName);
240 }
241 graphs.erase(it);
242
243 // Delete the physical graph files
244 if (!graphPath.empty() &&
245 !DBConfig::isDBPathInMemory(clientContext->getDatabasePath())) {
246 auto vfs = common::VirtualFileSystem::GetUnsafe(*clientContext);
247 vfs->removeFileIfExists(graphPath, clientContext);
248 vfs->removeFileIfExists(storage::StorageUtils::getWALFilePath(graphPath),
249 clientContext);
250 vfs->removeFileIfExists(storage::StorageUtils::getShadowFilePath(graphPath),
251 clientContext);
252 vfs->removeFileIfExists(storage::StorageUtils::getTmpFilePath(graphPath),
253 clientContext);
254 }
255
256 auto dbStorageManager = clientContext->getDatabase()->getStorageManager();
257 auto databaseHeader = dbStorageManager->getOrInitDatabaseHeader(*clientContext);
258 auto newHeader = std::make_unique<storage::DatabaseHeader>(*databaseHeader);
259 newHeader->catalogPageRange.startPageIdx = common::INVALID_PAGE_IDX;
260 newHeader->catalogPageRange.numPages = 0;
261 newHeader->metadataPageRange.startPageIdx = common::INVALID_PAGE_IDX;
262 newHeader->metadataPageRange.numPages = 0;
263 dbStorageManager->setDatabaseHeader(std::move(newHeader));
264 return;
265 }
266 }

Callers

nothing calls this directly

Calls 14

getCatalogMethod · 0.80
getActiveTransactionMethod · 0.80
containsGraphMethod · 0.80
closeFileHandleMethod · 0.80
eraseMethod · 0.80
setDatabaseHeaderMethod · 0.80
getDatabaseMethod · 0.45
beginMethod · 0.45
endMethod · 0.45
getStorageManagerMethod · 0.45
getDatabasePathMethod · 0.45

Tested by

no test coverage detected