--- linuxsampler/trunk/src/db/InstrumentsDb.cpp 2008/04/29 15:44:09 1727 +++ linuxsampler/trunk/src/db/InstrumentsDb.cpp 2009/03/30 16:56:41 1881 @@ -1,6 +1,6 @@ /*************************************************************************** * * - * Copyright (C) 2007, 2008 Grigor Iliev * + * Copyright (C) 2007-2009 Grigor Iliev * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * @@ -21,6 +21,7 @@ #include "InstrumentsDb.h" #include "../common/File.h" +#include "../common/Path.h" #include "../common/global_private.h" #include @@ -641,18 +642,18 @@ FireDirectoryInfoChanged(Dir); } - int InstrumentsDb::AddInstruments(ScanMode Mode, String DbDir, String FsDir, bool bBackground) { - dmsg(2,("InstrumentsDb: AddInstruments(Mode=%d,DbDir=%s,FsDir=%s,bBackground=%d)\n", Mode, DbDir.c_str(), FsDir.c_str(), bBackground)); + int InstrumentsDb::AddInstruments(ScanMode Mode, String DbDir, String FsDir, bool bBackground, bool insDir) { + dmsg(2,("InstrumentsDb: AddInstruments(Mode=%d,DbDir=%s,FsDir=%s,bBackground=%d,insDir=%d)\n", Mode, DbDir.c_str(), FsDir.c_str(), bBackground, insDir)); if(!bBackground) { switch (Mode) { case NON_RECURSIVE: - AddInstrumentsNonrecursive(DbDir, FsDir); + AddInstrumentsNonrecursive(DbDir, FsDir, insDir); break; case RECURSIVE: - AddInstrumentsRecursive(DbDir, FsDir); + AddInstrumentsRecursive(DbDir, FsDir, false, insDir); break; case FLAT: - AddInstrumentsRecursive(DbDir, FsDir, true); + AddInstrumentsRecursive(DbDir, FsDir, true, insDir); break; default: throw Exception("Unknown scan mode"); @@ -663,7 +664,7 @@ ScanJob job; int jobId = Jobs.AddJob(job); - InstrumentsDbThread.Execute(new AddInstrumentsJob(jobId, Mode, DbDir, FsDir)); + InstrumentsDbThread.Execute(new AddInstrumentsJob(jobId, Mode, DbDir, FsDir, insDir)); return jobId; } @@ -671,19 +672,19 @@ int InstrumentsDb::AddInstruments(String DbDir, String FilePath, int Index, bool bBackground) { dmsg(2,("InstrumentsDb: AddInstruments(DbDir=%s,FilePath=%s,Index=%d,bBackground=%d)\n", DbDir.c_str(), FilePath.c_str(), Index, bBackground)); if(!bBackground) { - AddInstruments(DbDir, FilePath, Index); + AddInstruments(DbDir, false, FilePath, Index); return -1; } ScanJob job; int jobId = Jobs.AddJob(job); - InstrumentsDbThread.Execute(new AddInstrumentsFromFileJob(jobId, DbDir, FilePath, Index)); + InstrumentsDbThread.Execute(new AddInstrumentsFromFileJob(jobId, DbDir, FilePath, Index, false)); return jobId; - } + } - void InstrumentsDb::AddInstruments(String DbDir, String FilePath, int Index, ScanProgress* pProgress) { - dmsg(2,("InstrumentsDb: AddInstruments(DbDir=%s,FilePath=%s,Index=%d)\n", DbDir.c_str(), FilePath.c_str(), Index)); + void InstrumentsDb::AddInstruments(String DbDir, bool insDir, String FilePath, int Index, ScanProgress* pProgress) { + dmsg(2,("InstrumentsDb: AddInstruments(DbDir=%s,insDir=%d,FilePath=%s,Index=%d)\n", DbDir.c_str(), insDir, FilePath.c_str(), Index)); if (DbDir.empty() || FilePath.empty()) return; DbInstrumentsMutex.Lock(); @@ -704,7 +705,8 @@ throw Exception(ss.str()); } - AddInstrumentsFromFile(DbDir, FilePath, Index, pProgress); + String dir = insDir ? PrepareSubdirectory(DbDir, FilePath) : DbDir; + AddInstrumentsFromFile(dir, FilePath, Index, pProgress); } catch (Exception e) { DbInstrumentsMutex.Unlock(); throw e; @@ -713,8 +715,8 @@ DbInstrumentsMutex.Unlock(); } - void InstrumentsDb::AddInstrumentsNonrecursive(String DbDir, String FsDir, ScanProgress* pProgress) { - dmsg(2,("InstrumentsDb: AddInstrumentsNonrecursive(DbDir=%s,FsDir=%s)\n", DbDir.c_str(), FsDir.c_str())); + void InstrumentsDb::AddInstrumentsNonrecursive(String DbDir, String FsDir, bool insDir, ScanProgress* pProgress) { + dmsg(2,("InstrumentsDb: AddInstrumentsNonrecursive(DbDir=%s,FsDir=%s,insDir=%d)\n", DbDir.c_str(), FsDir.c_str(), insDir)); if (DbDir.empty() || FsDir.empty()) return; DbInstrumentsMutex.Lock(); @@ -740,7 +742,8 @@ try { FileListPtr fileList = File::GetFiles(FsDir); for (int i = 0; i < fileList->size(); i++) { - AddInstrumentsFromFile(DbDir, FsDir + fileList->at(i), -1, pProgress); + String dir = insDir ? PrepareSubdirectory(DbDir, fileList->at(i)) : DbDir; + AddInstrumentsFromFile(dir, FsDir + fileList->at(i), -1, pProgress); } } catch(Exception e) { e.PrintMessage(); @@ -755,15 +758,15 @@ DbInstrumentsMutex.Unlock(); } - void InstrumentsDb::AddInstrumentsRecursive(String DbDir, String FsDir, bool Flat, ScanProgress* pProgress) { - dmsg(2,("InstrumentsDb: AddInstrumentsRecursive(DbDir=%s,FsDir=%s,Flat=%d)\n", DbDir.c_str(), FsDir.c_str(), Flat)); + void InstrumentsDb::AddInstrumentsRecursive(String DbDir, String FsDir, bool Flat, bool insDir, ScanProgress* pProgress) { + dmsg(2,("InstrumentsDb: AddInstrumentsRecursive(DbDir=%s,FsDir=%s,Flat=%d,insDir=%d)\n", DbDir.c_str(), FsDir.c_str(), Flat, insDir)); if (pProgress != NULL) { InstrumentFileCounter c; pProgress->SetTotalFileCount(c.Count(FsDir)); } DirectoryScanner d; - d.Scan(DbDir, FsDir, Flat, pProgress); + d.Scan(DbDir, FsDir, Flat, insDir, pProgress); } int InstrumentsDb::GetInstrumentCount(int DirId) { @@ -1174,6 +1177,7 @@ throw Exception(ss.str()); } + bool unlocked = false; RIFF::File* riff = NULL; gig::File* gig = NULL; try { @@ -1202,8 +1206,19 @@ if (Index == -1) { int instrIndex = 0; + // Assume that it's locked and should be unlocked at this point + // to be able to use the database from another threads + if (!InTransaction) { + DbInstrumentsMutex.Unlock(); + unlocked = true; + } else { + std::cerr << "Shouldn't be in transaction when adding instruments." << std::endl; + } + if (pProgress != NULL) gig->GetInstrument(0, &(pProgress->GigFileProgress)); // TODO: this workaround should be fixed gig::Instrument* pInstrument = gig->GetFirstInstrument(); + + if (!InTransaction) DbInstrumentsMutex.Lock(); while (pInstrument) { BindTextParam(pStmt, 7, gig->pInfo->Product); BindTextParam(pStmt, 8, gig->pInfo->Artists); @@ -1231,6 +1246,7 @@ } catch (RIFF::Exception e) { if (gig != NULL) delete gig; if (riff != NULL) delete riff; + if (unlocked) DbInstrumentsMutex.Lock(); std::stringstream ss; ss << "Failed to scan `" << FilePath << "`: " << e.Message; @@ -1238,10 +1254,12 @@ } catch (Exception e) { if (gig != NULL) delete gig; if (riff != NULL) delete riff; + if (unlocked) DbInstrumentsMutex.Lock(); throw e; } catch (...) { if (gig != NULL) delete gig; if (riff != NULL) delete riff; + if (unlocked) DbInstrumentsMutex.Lock(); throw Exception("Failed to scan `" + FilePath + "`"); } } @@ -1250,7 +1268,7 @@ dmsg(2,("InstrumentsDb: AddGigInstrument(DbDir=%s,DirId=%d,File=%s,Index=%d)\n", DbDir.c_str(), DirId, File.c_str(), Index)); String name = pInstrument->pInfo->Name; if (name == "") return; - name = GetUniqueInstrumentName(DirId, name); + name = GetUniqueName(DirId, name); std::stringstream sql2; sql2 << "SELECT COUNT(*) FROM instruments WHERE instr_file=? AND "; @@ -1724,7 +1742,7 @@ if (File.empty()) throw Exception("Invalid file name: " + File); } - String InstrumentsDb::GetUniqueInstrumentName(int DirId, String Name) { + String InstrumentsDb::GetUniqueName(int DirId, String Name) { dmsg(2,("InstrumentsDb: GetUniqueInstrumentName(DirId=%d,Name=%s)\n", DirId, Name.c_str())); if (GetInstrumentId(DirId, Name) == -1 && GetDirectoryId(DirId, Name) == -1) return Name; @@ -1739,6 +1757,22 @@ throw Exception("Unable to find an unique name: " + Name); } + + String InstrumentsDb::PrepareSubdirectory(String DbDir, String FsPath) { + std::string dir = Path::getBaseName(FsPath); + dir = toAbstractName(dir); + if(dir.empty()) dir = "New Directory"; + dir = GetUniqueName(GetDirectoryId(DbDir), dir); + dir = AppendNode(DbDir, dir); + AddDirectory(dir); + return dir; + } + + String InstrumentsDb::AppendNode(String DbDir, String Node) { + if(DbDir.length() == 1 && DbDir.at(0) == '/') return DbDir + Node; + if(DbDir.at(DbDir.length() - 1) == '/') return DbDir + Node; + return DbDir + "/" + Node; + } String InstrumentsDb::toDbName(String AbstractName) { for (int i = 0; i < AbstractName.length(); i++) {