/[svn]/linuxsampler/trunk/src/db/InstrumentsDb.cpp
ViewVC logotype

Diff of /linuxsampler/trunk/src/db/InstrumentsDb.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1727 by iliev, Tue Apr 29 15:44:09 2008 UTC revision 1881 by iliev, Mon Mar 30 16:56:41 2009 UTC
# Line 1  Line 1 
1  /***************************************************************************  /***************************************************************************
2   *                                                                         *   *                                                                         *
3   *   Copyright (C) 2007, 2008 Grigor Iliev                                 *   *   Copyright (C) 2007-2009 Grigor Iliev                                 *
4   *                                                                         *   *                                                                         *
5   *   This program is free software; you can redistribute it and/or modify  *   *   This program is free software; you can redistribute it and/or modify  *
6   *   it under the terms of the GNU General Public License as published by  *   *   it under the terms of the GNU General Public License as published by  *
# Line 21  Line 21 
21  #include "InstrumentsDb.h"  #include "InstrumentsDb.h"
22    
23  #include "../common/File.h"  #include "../common/File.h"
24    #include "../common/Path.h"
25  #include "../common/global_private.h"  #include "../common/global_private.h"
26    
27  #include <iostream>  #include <iostream>
# Line 641  namespace LinuxSampler { Line 642  namespace LinuxSampler {
642          FireDirectoryInfoChanged(Dir);          FireDirectoryInfoChanged(Dir);
643      }      }
644    
645      int InstrumentsDb::AddInstruments(ScanMode Mode, String DbDir, String FsDir, bool bBackground) {      int InstrumentsDb::AddInstruments(ScanMode Mode, String DbDir, String FsDir, bool bBackground, bool insDir) {
646          dmsg(2,("InstrumentsDb: AddInstruments(Mode=%d,DbDir=%s,FsDir=%s,bBackground=%d)\n", Mode, DbDir.c_str(), FsDir.c_str(), bBackground));          dmsg(2,("InstrumentsDb: AddInstruments(Mode=%d,DbDir=%s,FsDir=%s,bBackground=%d,insDir=%d)\n", Mode, DbDir.c_str(), FsDir.c_str(), bBackground, insDir));
647          if(!bBackground) {          if(!bBackground) {
648              switch (Mode) {              switch (Mode) {
649                  case NON_RECURSIVE:                  case NON_RECURSIVE:
650                      AddInstrumentsNonrecursive(DbDir, FsDir);                      AddInstrumentsNonrecursive(DbDir, FsDir, insDir);
651                      break;                      break;
652                  case RECURSIVE:                  case RECURSIVE:
653                      AddInstrumentsRecursive(DbDir, FsDir);                      AddInstrumentsRecursive(DbDir, FsDir, false, insDir);
654                      break;                      break;
655                  case FLAT:                  case FLAT:
656                      AddInstrumentsRecursive(DbDir, FsDir, true);                      AddInstrumentsRecursive(DbDir, FsDir, true, insDir);
657                      break;                      break;
658                  default:                  default:
659                      throw Exception("Unknown scan mode");                      throw Exception("Unknown scan mode");
# Line 663  namespace LinuxSampler { Line 664  namespace LinuxSampler {
664    
665          ScanJob job;          ScanJob job;
666          int jobId = Jobs.AddJob(job);          int jobId = Jobs.AddJob(job);
667          InstrumentsDbThread.Execute(new AddInstrumentsJob(jobId, Mode, DbDir, FsDir));          InstrumentsDbThread.Execute(new AddInstrumentsJob(jobId, Mode, DbDir, FsDir, insDir));
668    
669          return jobId;          return jobId;
670      }      }
# Line 671  namespace LinuxSampler { Line 672  namespace LinuxSampler {
672      int InstrumentsDb::AddInstruments(String DbDir, String FilePath, int Index, bool bBackground) {      int InstrumentsDb::AddInstruments(String DbDir, String FilePath, int Index, bool bBackground) {
673          dmsg(2,("InstrumentsDb: AddInstruments(DbDir=%s,FilePath=%s,Index=%d,bBackground=%d)\n", DbDir.c_str(), FilePath.c_str(), Index, bBackground));          dmsg(2,("InstrumentsDb: AddInstruments(DbDir=%s,FilePath=%s,Index=%d,bBackground=%d)\n", DbDir.c_str(), FilePath.c_str(), Index, bBackground));
674          if(!bBackground) {          if(!bBackground) {
675              AddInstruments(DbDir, FilePath, Index);              AddInstruments(DbDir, false, FilePath, Index);
676              return -1;              return -1;
677          }          }
678    
679          ScanJob job;          ScanJob job;
680          int jobId = Jobs.AddJob(job);          int jobId = Jobs.AddJob(job);
681          InstrumentsDbThread.Execute(new AddInstrumentsFromFileJob(jobId, DbDir, FilePath, Index));          InstrumentsDbThread.Execute(new AddInstrumentsFromFileJob(jobId, DbDir, FilePath, Index, false));
682    
683          return jobId;          return jobId;
684      }      }
685    
686      void InstrumentsDb::AddInstruments(String DbDir, String FilePath, int Index, ScanProgress* pProgress) {      void InstrumentsDb::AddInstruments(String DbDir, bool insDir, String FilePath, int Index, ScanProgress* pProgress) {
687          dmsg(2,("InstrumentsDb: AddInstruments(DbDir=%s,FilePath=%s,Index=%d)\n", DbDir.c_str(), FilePath.c_str(), Index));          dmsg(2,("InstrumentsDb: AddInstruments(DbDir=%s,insDir=%d,FilePath=%s,Index=%d)\n", DbDir.c_str(), insDir, FilePath.c_str(), Index));
688          if (DbDir.empty() || FilePath.empty()) return;          if (DbDir.empty() || FilePath.empty()) return;
689                    
690          DbInstrumentsMutex.Lock();          DbInstrumentsMutex.Lock();
# Line 704  namespace LinuxSampler { Line 705  namespace LinuxSampler {
705                  throw Exception(ss.str());                  throw Exception(ss.str());
706              }              }
707    
708              AddInstrumentsFromFile(DbDir, FilePath, Index, pProgress);              String dir = insDir ? PrepareSubdirectory(DbDir, FilePath) : DbDir;
709                AddInstrumentsFromFile(dir, FilePath, Index, pProgress);
710          } catch (Exception e) {          } catch (Exception e) {
711              DbInstrumentsMutex.Unlock();              DbInstrumentsMutex.Unlock();
712              throw e;              throw e;
# Line 713  namespace LinuxSampler { Line 715  namespace LinuxSampler {
715          DbInstrumentsMutex.Unlock();          DbInstrumentsMutex.Unlock();
716      }      }
717    
718      void InstrumentsDb::AddInstrumentsNonrecursive(String DbDir, String FsDir, ScanProgress* pProgress) {      void InstrumentsDb::AddInstrumentsNonrecursive(String DbDir, String FsDir, bool insDir, ScanProgress* pProgress) {
719          dmsg(2,("InstrumentsDb: AddInstrumentsNonrecursive(DbDir=%s,FsDir=%s)\n", DbDir.c_str(), FsDir.c_str()));          dmsg(2,("InstrumentsDb: AddInstrumentsNonrecursive(DbDir=%s,FsDir=%s,insDir=%d)\n", DbDir.c_str(), FsDir.c_str(), insDir));
720          if (DbDir.empty() || FsDir.empty()) return;          if (DbDir.empty() || FsDir.empty()) return;
721                    
722          DbInstrumentsMutex.Lock();          DbInstrumentsMutex.Lock();
# Line 740  namespace LinuxSampler { Line 742  namespace LinuxSampler {
742              try {              try {
743                  FileListPtr fileList = File::GetFiles(FsDir);                  FileListPtr fileList = File::GetFiles(FsDir);
744                  for (int i = 0; i < fileList->size(); i++) {                  for (int i = 0; i < fileList->size(); i++) {
745                      AddInstrumentsFromFile(DbDir, FsDir + fileList->at(i), -1, pProgress);                      String dir = insDir ? PrepareSubdirectory(DbDir, fileList->at(i)) : DbDir;
746                                            AddInstrumentsFromFile(dir, FsDir + fileList->at(i), -1, pProgress);
747                  }                  }
748              } catch(Exception e) {              } catch(Exception e) {
749                  e.PrintMessage();                  e.PrintMessage();
# Line 755  namespace LinuxSampler { Line 758  namespace LinuxSampler {
758          DbInstrumentsMutex.Unlock();          DbInstrumentsMutex.Unlock();
759      }      }
760    
761      void InstrumentsDb::AddInstrumentsRecursive(String DbDir, String FsDir, bool Flat, ScanProgress* pProgress) {      void InstrumentsDb::AddInstrumentsRecursive(String DbDir, String FsDir, bool Flat, bool insDir, ScanProgress* pProgress) {
762          dmsg(2,("InstrumentsDb: AddInstrumentsRecursive(DbDir=%s,FsDir=%s,Flat=%d)\n", DbDir.c_str(), FsDir.c_str(), Flat));          dmsg(2,("InstrumentsDb: AddInstrumentsRecursive(DbDir=%s,FsDir=%s,Flat=%d,insDir=%d)\n", DbDir.c_str(), FsDir.c_str(), Flat, insDir));
763          if (pProgress != NULL) {          if (pProgress != NULL) {
764              InstrumentFileCounter c;              InstrumentFileCounter c;
765              pProgress->SetTotalFileCount(c.Count(FsDir));              pProgress->SetTotalFileCount(c.Count(FsDir));
766          }          }
767    
768          DirectoryScanner d;          DirectoryScanner d;
769          d.Scan(DbDir, FsDir, Flat, pProgress);          d.Scan(DbDir, FsDir, Flat, insDir, pProgress);
770      }      }
771    
772      int InstrumentsDb::GetInstrumentCount(int DirId) {      int InstrumentsDb::GetInstrumentCount(int DirId) {
# Line 1174  namespace LinuxSampler { Line 1177  namespace LinuxSampler {
1177              throw Exception(ss.str());              throw Exception(ss.str());
1178          }          }
1179    
1180            bool unlocked = false;
1181          RIFF::File* riff = NULL;          RIFF::File* riff = NULL;
1182          gig::File* gig = NULL;          gig::File* gig = NULL;
1183          try {          try {
# Line 1202  namespace LinuxSampler { Line 1206  namespace LinuxSampler {
1206    
1207              if (Index == -1) {              if (Index == -1) {
1208                  int instrIndex = 0;                  int instrIndex = 0;
1209                    // Assume that it's locked and should be unlocked at this point
1210                    // to be able to use the database from another threads
1211                    if (!InTransaction) {
1212                        DbInstrumentsMutex.Unlock();
1213                        unlocked = true;
1214                    } else {
1215                        std::cerr << "Shouldn't be in transaction when adding instruments." << std::endl;
1216                    }
1217    
1218                  if (pProgress != NULL) gig->GetInstrument(0, &(pProgress->GigFileProgress)); // TODO: this workaround should be fixed                  if (pProgress != NULL) gig->GetInstrument(0, &(pProgress->GigFileProgress)); // TODO: this workaround should be fixed
1219                  gig::Instrument* pInstrument = gig->GetFirstInstrument();                  gig::Instrument* pInstrument = gig->GetFirstInstrument();
1220    
1221                    if (!InTransaction) DbInstrumentsMutex.Lock();
1222                  while (pInstrument) {                  while (pInstrument) {
1223                      BindTextParam(pStmt, 7, gig->pInfo->Product);                      BindTextParam(pStmt, 7, gig->pInfo->Product);
1224                      BindTextParam(pStmt, 8, gig->pInfo->Artists);                      BindTextParam(pStmt, 8, gig->pInfo->Artists);
# Line 1231  namespace LinuxSampler { Line 1246  namespace LinuxSampler {
1246          } catch (RIFF::Exception e) {          } catch (RIFF::Exception e) {
1247              if (gig != NULL) delete gig;              if (gig != NULL) delete gig;
1248              if (riff != NULL) delete riff;              if (riff != NULL) delete riff;
1249                if (unlocked) DbInstrumentsMutex.Lock();
1250              std::stringstream ss;              std::stringstream ss;
1251              ss << "Failed to scan `" << FilePath << "`: " << e.Message;              ss << "Failed to scan `" << FilePath << "`: " << e.Message;
1252                            
# Line 1238  namespace LinuxSampler { Line 1254  namespace LinuxSampler {
1254          } catch (Exception e) {          } catch (Exception e) {
1255              if (gig != NULL) delete gig;              if (gig != NULL) delete gig;
1256              if (riff != NULL) delete riff;              if (riff != NULL) delete riff;
1257                if (unlocked) DbInstrumentsMutex.Lock();
1258              throw e;              throw e;
1259          } catch (...) {          } catch (...) {
1260              if (gig != NULL) delete gig;              if (gig != NULL) delete gig;
1261              if (riff != NULL) delete riff;              if (riff != NULL) delete riff;
1262                if (unlocked) DbInstrumentsMutex.Lock();
1263              throw Exception("Failed to scan `" + FilePath + "`");              throw Exception("Failed to scan `" + FilePath + "`");
1264          }          }
1265      }      }
# Line 1250  namespace LinuxSampler { Line 1268  namespace LinuxSampler {
1268          dmsg(2,("InstrumentsDb: AddGigInstrument(DbDir=%s,DirId=%d,File=%s,Index=%d)\n", DbDir.c_str(), DirId, File.c_str(), Index));          dmsg(2,("InstrumentsDb: AddGigInstrument(DbDir=%s,DirId=%d,File=%s,Index=%d)\n", DbDir.c_str(), DirId, File.c_str(), Index));
1269          String name = pInstrument->pInfo->Name;          String name = pInstrument->pInfo->Name;
1270          if (name == "") return;          if (name == "") return;
1271          name = GetUniqueInstrumentName(DirId, name);          name = GetUniqueName(DirId, name);
1272                    
1273          std::stringstream sql2;          std::stringstream sql2;
1274          sql2 << "SELECT COUNT(*) FROM instruments WHERE instr_file=? AND ";          sql2 << "SELECT COUNT(*) FROM instruments WHERE instr_file=? AND ";
# Line 1724  namespace LinuxSampler { Line 1742  namespace LinuxSampler {
1742          if (File.empty()) throw Exception("Invalid file name: " + File);          if (File.empty()) throw Exception("Invalid file name: " + File);
1743      }      }
1744    
1745      String InstrumentsDb::GetUniqueInstrumentName(int DirId, String Name) {      String InstrumentsDb::GetUniqueName(int DirId, String Name) {
1746          dmsg(2,("InstrumentsDb: GetUniqueInstrumentName(DirId=%d,Name=%s)\n", DirId, Name.c_str()));          dmsg(2,("InstrumentsDb: GetUniqueInstrumentName(DirId=%d,Name=%s)\n", DirId, Name.c_str()));
1747    
1748          if (GetInstrumentId(DirId, Name) == -1 && GetDirectoryId(DirId, Name) == -1) return Name;          if (GetInstrumentId(DirId, Name) == -1 && GetDirectoryId(DirId, Name) == -1) return Name;
# Line 1739  namespace LinuxSampler { Line 1757  namespace LinuxSampler {
1757    
1758          throw Exception("Unable to find an unique name: " + Name);          throw Exception("Unable to find an unique name: " + Name);
1759      }      }
1760        
1761        String InstrumentsDb::PrepareSubdirectory(String DbDir, String FsPath) {
1762            std::string dir = Path::getBaseName(FsPath);
1763            dir = toAbstractName(dir);
1764            if(dir.empty()) dir = "New Directory";
1765            dir = GetUniqueName(GetDirectoryId(DbDir), dir);
1766            dir = AppendNode(DbDir, dir);
1767            AddDirectory(dir);
1768            return dir;
1769        }
1770    
1771        String InstrumentsDb::AppendNode(String DbDir, String Node) {
1772            if(DbDir.length() == 1 && DbDir.at(0) == '/') return DbDir + Node;
1773            if(DbDir.at(DbDir.length() - 1) == '/') return DbDir + Node;
1774            return DbDir + "/" + Node;
1775        }
1776    
1777      String InstrumentsDb::toDbName(String AbstractName) {      String InstrumentsDb::toDbName(String AbstractName) {
1778          for (int i = 0; i < AbstractName.length(); i++) {          for (int i = 0; i < AbstractName.length(); i++) {

Legend:
Removed from v.1727  
changed lines
  Added in v.1881

  ViewVC Help
Powered by ViewVC