[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-multimedia
Subject:    PATCH: less frequent trader cache updates
From:       Stefan Westerfeld <stefan () space ! twc ! de>
Date:       2001-07-26 20:32:29
[Download RAW message or body]

Hi!

The following patch only updates ~/.mcop/trader-cache if there were really
some changes. This should have a positive effect on artsd startup time. You
can observe the effect when running artsd -l0 with and without the patch.

   Cu... Stefan
-- 
  -* Stefan Westerfeld, stefan@space.twc.de (PGP!), Hamburg/Germany
     KDE Developer, project infos at http://space.twc.de/~stefan/kde *-         

["20010726-cache-updating.diff" (text/plain)]

cvs server: Diffing .
Index: soundserverv2_impl.cc
===================================================================
RCS file: /home/kde/kdelibs/arts/soundserver/soundserverv2_impl.cc,v
retrieving revision 1.16
diff -u -r1.16 soundserverv2_impl.cc
--- soundserverv2_impl.cc	2001/05/14 20:42:00	1.16
+++ soundserverv2_impl.cc	2001/07/26 20:25:56
@@ -37,6 +37,9 @@
 #include <sys/stat.h>
 #include <unistd.h>
 #include <dirent.h>
+#include <errno.h>
+#include <fstream>
+#include <set>
 
 #include "config.h"
 
@@ -222,9 +225,10 @@
 	closedir(dir);
 }
 
-static void doTypeIndex(string prefix, ModuleDef& module) /* copied from mcopidl */
+/* copied from mcopidl */
+static void doTypeIndex(string dir, string prefix, ModuleDef& module)
 {
-	FILE *typeIndex = fopen((prefix+".mcopclass").c_str(),"w");
+	FILE *typeIndex = fopen((dir+"/"+prefix+".mcopclass").c_str(),"w");
 
 	vector<string> supportedTypes;
 
@@ -258,21 +262,72 @@
 	arts_return_if_fail(home != 0);
 
 	string dir = home + string("/.mcop/trader-cache");
+	string dataVersionFileName = dir + "/cache-data-version";
 
 	mkdir(home,0755);
 	mkdir((home+string("/.mcop")).c_str(),0755);
-	mkdir(dir.c_str(),0755);
-
-	int chdirOk = chdir(dir.c_str());
-	arts_return_if_fail(chdirOk == 0);
+	if(mkdir(dir.c_str(),0755) != 0)
+	{
+		string why = strerror(errno);
 
-	clearDirectory(dir);
+		struct stat st;
+		stat(dir.c_str(),&st);
+		if(!S_ISDIR(st.st_mode))
+		{
+			arts_warning("can't create directory %s to fill it with"
+						 " trader data (%s)", dir.c_str(), why.c_str());
+			return;
+		}
+	}
 
 	TraderQuery query;
 	query.supports("Interface", "Arts::Loader");
 	vector<TraderOffer> *offers = query.query();
 	vector<TraderOffer>::iterator i;
 
+	set<string> newDataVersion, cacheDataVersion;
+	for(i = offers->begin(); i != offers->end(); i++)
+	{
+		// TODO: error checking?
+		Arts::Loader loader = SubClass(i->interfaceName());
+		newDataVersion.insert(loader.dataVersion());
+	}
+
+	/* change this line if you change the cache update code */
+	newDataVersion.insert("Cache-Update-Code-Version:1.0");
+
+	/* load cache-data-version file */
+	{
+		ifstream infile(dataVersionFileName.c_str());
+
+		string line;
+		while(infile >> line)
+			cacheDataVersion.insert(line);
+	}
+
+	/* if it differs, rebuild trader cache */
+	if(cacheDataVersion != newDataVersion)
+	{
+		clearDirectory(dir);
+
+		/* save new cache-data-version file */
+		{
+			ofstream out(dataVersionFileName.c_str());
+
+			set<string>::iterator i;
+			for(i = newDataVersion.begin(); i != newDataVersion.end(); i++)
+				out << *i << endl;
+		}
+		rebuildTraderCache(dir, offers);
+	}
+	delete offers;
+}
+
+void SoundServerV2_impl::rebuildTraderCache(const string& directory,
+											vector<TraderOffer> *offers)
+{
+	vector<TraderOffer>::iterator i;
+
 	for(i = offers->begin(); i != offers->end(); i++)
 	{
 		// TODO: error checking?
@@ -285,7 +340,7 @@
 		{
 			const TraderEntry& entry = *ei;
 
-			FILE *traderFile = fopen((dir+"/"+entry.interfaceName+".mcopclass").c_str(),"w");
+			FILE *traderFile = fopen((directory+"/"+entry.interfaceName+".mcopclass").c_str(),"w");
 			fprintf(traderFile, "# this file was generated by artsd - do not edit\n");
 			vector<string>::const_iterator li;
 			for(li = entry.lines.begin(); li != entry.lines.end(); li++)
@@ -305,17 +360,15 @@
 			Buffer b;
 			module.writeType(b);
 
-			FILE *typeFile = fopen((module.moduleName+".arts.mcoptype").c_str(),"w");
+			FILE *typeFile = fopen((directory + "/" + module.moduleName+".arts.mcoptype").c_str(),"w");
 			unsigned long towrite = b.size();
 			fwrite(b.read(towrite),1,towrite,typeFile);
 			fclose(typeFile);
 
-			doTypeIndex(module.moduleName+".arts",module);
+			doTypeIndex(directory,module.moduleName+".arts",module);
 		}
 		delete modules;
 	}
-	delete offers;
-
 	Dispatcher::the()->reloadTraderData();
 }
 
Index: soundserverv2_impl.h
===================================================================
RCS file: /home/kde/kdelibs/arts/soundserver/soundserverv2_impl.h,v
retrieving revision 1.12
diff -u -r1.12 soundserverv2_impl.h
--- soundserverv2_impl.h	2001/04/28 18:15:10	1.12
+++ soundserverv2_impl.h	2001/07/26 20:25:56
@@ -37,6 +37,10 @@
 	{
 		protected:
 			SampleStorage _sampleStorage;
+
+			void rebuildTraderCache(const std::string& directory,
+									std::vector<TraderOffer> *offers);
+
 		public:
 			SoundServerV2_impl();
 

_______________________________________________
Kde-multimedia mailing list
Kde-multimedia@master.kde.org
http://master.kde.org/mailman/listinfo/kde-multimedia


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic