beagle r4458 - trunk/beagle/beagled



Author: dbera
Date: Sun Feb  3 18:51:14 2008
New Revision: 4458
URL: http://svn.gnome.org/viewvc/beagle?rev=4458&view=rev

Log:
Remove a whole lot of code now redundant due to the keyword-mapping.xml changes. Move all the logic in PropertyKeywordFu. Remove some of the unnecessary classes in PropertyKeywordFu and remove the mappings hardcoded in the code; they are now in the external mappings file.

Modified:
   trunk/beagle/beagled/PropertyKeywordFu.cs
   trunk/beagle/beagled/QueryDriver.cs
   trunk/beagle/beagled/QueryStringParser.cs

Modified: trunk/beagle/beagled/PropertyKeywordFu.cs
==============================================================================
--- trunk/beagle/beagled/PropertyKeywordFu.cs	(original)
+++ trunk/beagle/beagled/PropertyKeywordFu.cs	Sun Feb  3 18:51:14 2008
@@ -29,46 +29,14 @@
 using System.Collections.Generic;
 using System.IO;
 using System.Text;
+using System.Xml;
+using System.Xml.Serialization;
 
 using Beagle;
 using Beagle.Util;
-using System.Xml.Serialization;
-	
-namespace Beagle.Daemon {
-
-	public class PropertyDetail {
-		private PropertyType type;
-		private string property_name;
-		// a short description, so that frontends can get the description from here
-		// and they dont need to guess the meaning of a query keyword (being too nice ?!)
-		private string short_desc;
-		
-		public PropertyType Type { 
-			get { return type;} 
-		}
-
-		public string PropertyName { 
-			get { return property_name;} 
-		}
 
-		public string Description { 
-			get { return short_desc; } 
-		}
-
-		public PropertyDetail (PropertyType type, string property_name) {
-		    this.type = type;
-		    this.property_name = property_name;
-		    this.short_desc = "";
-		}
-		
-		public PropertyDetail (PropertyType type, string property_name, string desc) {
-		    this.type = type;
-		    this.property_name = property_name;
-		    this.short_desc = desc;
-		}
+namespace Beagle.Daemon {
 
-	}
-	
 	public class PropertyKeywordFu {
 		// mapping
 		private static Hashtable property_table;
@@ -76,10 +44,6 @@
 		// static class
 		private PropertyKeywordFu () { }
 
-		static PropertyKeywordFu () {
-			PopulatePropertyTable ();
-		}
-
 		public static IEnumerable Keys {
 			get { return property_table.Keys; }
 		}
@@ -89,80 +53,88 @@
 				yield break;
 
 			object o = property_table [keyword];
-			if (o is PropertyDetail)
+			if (o is QueryKeywordMapping)
 				yield return o;
 			else if (o is ArrayList) {
-				foreach (PropertyDetail detail in ((ArrayList) o))
-					yield return detail;
+				foreach (QueryKeywordMapping mapping in ((ArrayList) o))
+					yield return mapping;
 			}
 		}
 
-		// FIXME handle i18n issues... user might use a i18n-ised string for "title"
-		private static void PopulatePropertyTable () {
+		////////////////////////////////////////////////////////
+
+		public static void ReadKeywordMappings ()
+		{
 			property_table = new Hashtable ();
-			
-			// Mapping between human query keywords and beagle index property keywords
-			// These are some of the standard mapping which is available to all backends and filters.
-			
-			property_table.Add ("title",
-					    new PropertyDetail (PropertyType.Text, "dc:title", "Title"));
-			
-			property_table.Add ("creator",
-					    new PropertyDetail (PropertyType.Text, "dc:creator", "Creator of the content"));
-
-			property_table.Add ("author",
-					    new PropertyDetail (PropertyType.Text, "dc:author", "Author of the content"));
-
-			property_table.Add ("summary",
-					    new PropertyDetail (PropertyType.Text, "dc:subject", "Brief description of the content"));
-
-			property_table.Add ("source",
-					    new PropertyDetail (PropertyType.Keyword, "beagle:Source", "Name of the backend"));
-
-			property_table.Add ("type",
-					    new PropertyDetail (PropertyType.Keyword, "beagle:HitType", "Hittype of the content e.g. File, IMLog, MailMessage"));
-
-			property_table.Add ("mimetype",
-					    new PropertyDetail (PropertyType.Keyword, "beagle:MimeType", "Mimetype of the content"));
-
-			property_table.Add ("filetype",
-					    new PropertyDetail (PropertyType.Keyword, "beagle:FileType", "Type of content for HitType File"));
-					    
-			
+
+			// FIXME: No need for a SerializerFactory here, since we need the serializer
+			// only once
+			XmlSerializerFactory xsf = new XmlSerializerFactory();
+			XmlSerializer xs = xsf.CreateSerializer (typeof (QueryMapping), new Type[] { typeof (QueryKeywordMapping)});
+
+			QueryMapping query_mapping = null;
+
+			// <keyword name, can override>
+			Dictionary<string, bool> mapping_override = new Dictionary<string, bool> ();
+
+			using (Stream s = File.OpenRead (Path.Combine (PathFinder.ConfigDataDir, "keyword-mapping.xml"))) {
+				try {			
+					query_mapping = (QueryMapping) xs.Deserialize (s);
+					foreach (QueryKeywordMapping mapping in query_mapping.Mappings) {
+						PropertyKeywordFu.RegisterMapping (mapping);
+						mapping_override [mapping.Keyword] = true;
+					}
+				} catch (XmlException e) {
+					Logger.Log.Error (e, "Unable to parse global keyword-mapping.xml");
+				}
+			}
+
+			// Override global mappings by local mappings
+
+			if (! File.Exists (Path.Combine (PathFinder.StorageDir, "keyword-mapping.xml")))
+				return;
+
+			using (Stream s = File.OpenRead (Path.Combine (PathFinder.StorageDir, "keyword-mapping.xml"))) {
+				try {			
+					query_mapping = (QueryMapping) xs.Deserialize (s);
+					foreach (QueryKeywordMapping mapping in query_mapping.Mappings) {
+						if (mapping_override.ContainsKey (mapping.Keyword)) {
+							property_table.Remove (mapping.Keyword);
+							mapping_override [mapping.Keyword] = false;
+						}
+
+						PropertyKeywordFu.RegisterMapping (mapping);
+					}
+				} catch (XmlException e) {
+					Logger.Log.Error (e, "Unable to parse local keyword-mapping.xml");
+				}
+			}
 		}
 
-		public static void RegisterMapping (PropertyKeywordMapping mapping)
+		private static void RegisterMapping (QueryKeywordMapping mapping)
 		{
 			// If multiple mapping as registered, create an OR query for them
 			// Store the multiple matchings in a list
 			if (property_table.Contains (mapping.Keyword)) {
 				object o = property_table [mapping.Keyword];
 				if (o is ArrayList) {
-					((ArrayList)o).Add (new PropertyDetail ( 
-						mapping.IsKeyword ? PropertyType.Keyword : PropertyType.Text,
-						mapping.PropertyName, 
-						mapping.Description));
-				} else if (o is PropertyDetail) {
+					((ArrayList)o).Add (mapping);
+				} else if (o is QueryKeywordMapping) {
 					ArrayList list = new ArrayList (2);
 					list.Add (o);
-					list.Add (new PropertyDetail ( 
-						mapping.IsKeyword ? PropertyType.Keyword : PropertyType.Text,
-						mapping.PropertyName, 
-						mapping.Description));
+					list.Add (mapping);
 					property_table [mapping.Keyword] = list;
 				}
 				return;
 			}
 
-			property_table.Add (mapping.Keyword,
-					    new PropertyDetail ( 
-						mapping.IsKeyword ? PropertyType.Keyword : PropertyType.Text,
-						mapping.PropertyName, 
-						mapping.Description));
+			property_table.Add (mapping.Keyword, mapping);
 		}
 
+		////////////////////////////////////////////////////////
+
 		// return false if property not found!
-		public static bool GetPropertyDetails (string keyword, out int num, out string[] name, out PropertyType[] type) {
+		public static bool GetMapping (string keyword, out int num, out string[] name, out PropertyType[] type) {
 			num = 0;
 			name = null;
 			type = null;
@@ -178,116 +150,32 @@
 				type = new PropertyType [num];
 
 				for (int i = 0; i < num; ++i) {
-					PropertyDetail detail = (PropertyDetail) (list [i]);
-					name [i] = detail.PropertyName;
-					type [i] = detail.Type;
+					QueryKeywordMapping mapping = (QueryKeywordMapping) (list [i]);
+					name [i] = mapping.PropertyName;
+					type [i] = (mapping.IsKeyword ? PropertyType.Keyword : PropertyType.Text);
 				}
-			} else if (o is PropertyDetail) {
+			} else if (o is QueryKeywordMapping) {
 				num = 1;
 				name = new string [num];
 				type = new PropertyType [num];
-				name [0] = ((PropertyDetail) o).PropertyName;
-				type [0] = ((PropertyDetail) o).Type;
+				name [0] = ((QueryKeywordMapping) o).PropertyName;
+				type [0] = (((QueryKeywordMapping) o).IsKeyword ? PropertyType.Keyword : PropertyType.Text);
 			}
 			return true;
 		}
-		
-		public static void AddToCache (string keyword, PropertyDetail details) {
-			
-			if (property_table.Contains (keyword)) {
-					object o = PropertyKeywordFu.property_table [keyword];
-					if (o is ArrayList) {
-						((ArrayList)o).Add ( details );
-					} else if (o is PropertyDetail) {
-						ArrayList list = new ArrayList (2);
-						list.Add (o);
-						list.Add ( details );
-						PropertyKeywordFu.property_table [keyword] = list;
-					}
-				} else {
-					
-					property_table.Add (keyword , details);
-					                                      
-				}
-		}
 	}
-	public class KeywordMappingStore
+
+	public class QueryMapping
 	{
-		private List<PropertyInfo> property_info = new List<PropertyInfo>();
-		
+		private List<QueryKeywordMapping> property_mappings = new List<QueryKeywordMapping> ();
+
 		[XmlArray]
-		[XmlArrayItem (ElementName="Properties", Type=typeof(PropertyInfo))]
-		public List<PropertyInfo> PropertyInfo {
-			get {
-				return property_info;
-			}
-			set {
-				property_info = value;
-			}
-		}
-		
-		public KeywordMappingStore (){
-		}
-		
-		
-	}
-	
-	public class PropertyInfo
-	{
-		private string keyword;
-		private string propertyname;
-		private PropertyType prop_type;
-		private string description;
-		
-		public PropertyInfo () {
-			
-		}
-		
-		public PropertyInfo (string k, string prop, PropertyType keyword, string descript) {
-			this.keyword = k;
-			this.propertyname = prop;
-			this.prop_type = keyword;
-			this.description = descript;
-		}
-		
-		[XmlAttribute ("Keyword")]
-		public string Keyword {
-			get {
-				return keyword;
-			}
-			set {
-				keyword = value;
-			}
-		}
-		[XmlAttribute ("PropertyName")]
-		public string Propertyname {
-			get {
-				return propertyname;
-			}
-			set {
-				propertyname = value;
-			}
+		[XmlArrayItem (ElementName="Mapping", Type=typeof (QueryKeywordMapping))]
+		public List<QueryKeywordMapping> Mappings {
+			get { return property_mappings; }
+			set { property_mappings = value; }
 		}
 
-		[XmlText]
-		public string Description {
-			get {
-				return description;
-			}
-			set {
-				description = value;
-			}
-		}
-		
-		[XmlAttribute  (AttributeName = "PropType",Type = typeof(PropertyType))]
-		public PropertyType PropType {
-			get {
-				return prop_type;
-			} set {
-				prop_type = value;
-			}
-		}
-		
+		public QueryMapping () { }
 	}
-	
 }

Modified: trunk/beagle/beagled/QueryDriver.cs
==============================================================================
--- trunk/beagle/beagled/QueryDriver.cs	(original)
+++ trunk/beagle/beagled/QueryDriver.cs	Sun Feb  3 18:51:14 2008
@@ -150,7 +150,6 @@
 			int count = 0;
 
 			foreach (Type type in ReflectionFu.GetTypesFromAssemblyAttribute (assembly, typeof (IQueryableTypesAttribute))) {
-				bool type_accepted = false;
 				foreach (QueryableFlavor flavor in ReflectionFu.ScanTypeForAttribute (type, typeof (QueryableFlavor))) {
 					if (! UseQueryable (flavor.Name))
 						continue;
@@ -176,98 +175,15 @@
 						Queryable q = new Queryable (flavor, iq);
 						iqueryable_to_queryable [iq] = q;
 						++count;
-						type_accepted = true;
 						break;
 					}
 				}
-
-				if (! type_accepted)
-					continue;
-
-				object[] attributes = type.GetCustomAttributes (false);
-				foreach (object attribute in attributes) {
-					PropertyKeywordMapping mapping = attribute as PropertyKeywordMapping;
-					if (mapping == null)
-						continue;
-					//Logger.Log.Debug (mapping.Keyword + " => " 
-					//		+ mapping.PropertyName + 
-					//		+ " is-keyword=" + mapping.IsKeyword + " (" 
-					//		+ mapping.Description + ") "
-					//		+ "(" + type.FullName + ")");
-					PropertyKeywordFu.RegisterMapping (mapping);
-				}
-					
 			}
 			Logger.Log.Debug ("Found {0} backends in {1}", count, assembly.Location);
 		}
 
 		////////////////////////////////////////////////////////
 
-		public static void ReadKeywordMappings ()
-		{
-			XmlSerializerFactory xsf = new XmlSerializerFactory();
-			XmlSerializer xs = xsf.CreateSerializer (typeof(KeywordMappingStore),new Type[] {typeof(PropertyInfo),typeof(Beagle.PropertyType)});
-			Stream s = null;
-			KeywordMappingStore kms = null; 			
-			try {			
-				if (System.Environment.GetEnvironmentVariable ("$BEAGLE_LOAD_KEYWORDS_FROM") != null){
-					s =File.OpenRead (System.Environment.GetEnvironmentVariable ("$BEAGLE_LOAD_KEYWORDS_FROM"));
-				} else {
-					Logger.Log.Debug ("Reading mapping from filters");
-					s =File.OpenRead (Path.Combine (PathFinder.GlobalConfigDir,"../keyword-mapping.xml"));
-				}
-				kms = (KeywordMappingStore) xs.Deserialize (s);
-				s.Flush();
-				s.Close();
-				foreach (PropertyInfo pi in kms.PropertyInfo){
-					PropertyKeywordFu.AddToCache ( pi.Keyword, (
-							   new PropertyDetail ( 
-								pi.PropType ,
-								pi.Propertyname, 
-								pi.Description))); 
-				}
-			} catch (IOException e) {
-				Logger.Log.Error (e);
-			}
-				
-			try {
-
-			
-				if (File.Exists ( Path.Combine (PathFinder.StorageDir, "keyword-mapping.xml"))){
-					s =File.Open ( Path.Combine (PathFinder.StorageDir, "keyword-mapping.xml") , FileMode.Open);
-					kms = (KeywordMappingStore) xs.Deserialize (s);
-					s.Flush();
-					s.Close();
-					foreach (PropertyInfo pi in kms.PropertyInfo){
-						PropertyKeywordFu.AddToCache ( pi.Keyword, (
-								   new PropertyDetail ( 
-									pi.PropType ,
-									pi.Propertyname, 
-									pi.Description))); 
-					}
-				}
-			} catch (IOException e) {
-				Logger.Log.Error (e);
-			}
-			
-			//To Generate a starting file, the original reflection was used. I've kept the code, should we need to recreate
-			//the initial file.
-//			KeywordMappingStore kms = new KeywordMappingStore();
-//			foreach (String s1  in PropertyKeywordFu.Keys ) {
-//				foreach (PropertyDetail pd  in PropertyKeywordFu.Properties (s1) ) {
-//					kms.PropertyInfo.Add ( new PropertyInfo (s1,pd.PropertyName,pd.Type, pd.Description));
-//					System.Console.WriteLine(new PropertyInfo (s1,pd.PropertyName,pd.Type, pd.Description).Propertyname);
-//				}
-//			}
-//			
-//			XmlSerializerFactory xsf = new XmlSerializerFactory();
-//			XmlSerializer xs = xsf.CreateSerializer (typeof(KeywordMappingStore),new Type[] {typeof(PropertyInfo),typeof(Beagle.PropertyType)});
-//			xs.Serialize (Console.Out,kms);
-			
-		}
-
-		////////////////////////////////////////////////////////
-
 		// Scans PathFinder.SystemIndexesDir after available 
 		// system-wide indexes.
 		static void LoadSystemIndexes () 
@@ -402,7 +318,7 @@
 			
 			assemblies = null;
 
-			ReadKeywordMappings ();
+			PropertyKeywordFu.ReadKeywordMappings ();
 
 			LoadSystemIndexes ();
 			LoadStaticQueryables ();

Modified: trunk/beagle/beagled/QueryStringParser.cs
==============================================================================
--- trunk/beagle/beagled/QueryStringParser.cs	(original)
+++ trunk/beagle/beagled/QueryStringParser.cs	Sun Feb  3 18:51:14 2008
@@ -277,7 +277,7 @@
 			PropertyType[] prop_type;
 			int num;
 
-			is_present = PropertyKeywordFu.GetPropertyDetails (key, out num, out prop_string, out prop_type);
+			is_present = PropertyKeywordFu.GetMapping (key, out num, out prop_string, out prop_type);
 			// if key is not present in the mapping, assume the query is a text query
 			// i.e. if token is foo:bar and there is no mappable property named foo,
 			// assume "foo:bar" as text query



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]