[tracker/subqueries] libtracker-data: Add support for tracker:secondaryIndex



commit 68b47231e7bac3d848cdc8fd7640ac378420397c
Author: Jürg Billeter <j bitron ch>
Date:   Mon Dec 7 17:48:45 2009 +0100

    libtracker-data: Add support for tracker:secondaryIndex
    
    This will create a two-column index in SQLite.

 data/ontologies/11-rdf.ontology            |    5 +++
 data/ontologies/34-nmo.ontology            |    1 +
 src/libtracker-common/tracker-property.c   |   55 ++++++++++++++++++++++++++++
 src/libtracker-common/tracker-property.h   |    3 ++
 src/libtracker-data/tracker-data-manager.c |   53 +++++++++++++++++++++------
 5 files changed, 105 insertions(+), 12 deletions(-)
---
diff --git a/data/ontologies/11-rdf.ontology b/data/ontologies/11-rdf.ontology
index 4b0d051..1972265 100644
--- a/data/ontologies/11-rdf.ontology
+++ b/data/ontologies/11-rdf.ontology
@@ -75,6 +75,11 @@ tracker:indexed a rdf:Property ;
 	rdfs:domain rdf:Property ;
 	rdfs:range xsd:boolean .
 
+tracker:secondaryIndex a rdf:Property ;
+	nrl:maxCardinality 1 ;
+	rdfs:domain rdf:Property ;
+	rdfs:range rdf:Property .
+
 tracker:fulltextIndexed a rdf:Property ;
 	nrl:maxCardinality 1 ;
 	rdfs:domain rdf:Property ;
diff --git a/data/ontologies/34-nmo.ontology b/data/ontologies/34-nmo.ontology
index 4803f90..dd2cf51 100644
--- a/data/ontologies/34-nmo.ontology
+++ b/data/ontologies/34-nmo.ontology
@@ -232,6 +232,7 @@ nmo:communicationChannel a rdf:Property ;
 
 nmo:messageChannel a rdf:Property ;
 	tracker:indexed true ;
+	tracker:secondaryIndex nmo:sentDate ;
 	nrl:maxCardinality 1 ;
 	rdfs:domain nmo:Message ;
 	rdfs:range nmo:CommunicationChannel .
diff --git a/src/libtracker-common/tracker-property.c b/src/libtracker-common/tracker-property.c
index 39cf82f..0649962 100644
--- a/src/libtracker-common/tracker-property.c
+++ b/src/libtracker-common/tracker-property.c
@@ -49,6 +49,7 @@ struct _TrackerPropertyPriv {
 	TrackerClass   *range;
 	gint	       weight;
 	gboolean       indexed;
+	TrackerProperty *secondary_index;
 	gboolean       fulltext_indexed;
 	gboolean       embedded;
 	gboolean       multiple_values;
@@ -78,6 +79,7 @@ enum {
 	PROP_RANGE,
 	PROP_WEIGHT,
 	PROP_INDEXED,
+	PROP_SECONDARY_INDEX,
 	PROP_FULLTEXT_INDEXED,
 	PROP_EMBEDDED,
 	PROP_MULTIPLE_VALUES,
@@ -208,6 +210,13 @@ tracker_property_class_init (TrackerPropertyClass *klass)
 							       TRUE,
 							       G_PARAM_READWRITE));
 	g_object_class_install_property (object_class,
+					 PROP_SECONDARY_INDEX,
+					 g_param_spec_object ("secondary-index",
+							      "secondary-index",
+							      "Secondary index property",
+							      TRACKER_TYPE_PROPERTY,
+							      G_PARAM_READWRITE));
+	g_object_class_install_property (object_class,
 					 PROP_FULLTEXT_INDEXED,
 					 g_param_spec_boolean ("fulltext-indexed",
 							       "fulltext-indexed",
@@ -287,6 +296,10 @@ property_finalize (GObject *object)
 		g_object_unref (priv->range);
 	}
 
+	if (priv->secondary_index) {
+		g_object_unref (priv->secondary_index);
+	}
+
 	g_array_free (priv->super_properties, TRUE);
 
 	(G_OBJECT_CLASS (tracker_property_parent_class)->finalize) (object);
@@ -324,6 +337,9 @@ property_get_property (GObject    *object,
 	case PROP_INDEXED:
 		g_value_set_boolean (value, priv->indexed);
 		break;
+	case PROP_SECONDARY_INDEX:
+		g_value_set_object (value, priv->secondary_index);
+		break;
 	case PROP_FULLTEXT_INDEXED:
 		g_value_set_boolean (value, priv->fulltext_indexed);
 		break;
@@ -379,6 +395,10 @@ property_set_property (GObject	    *object,
 		tracker_property_set_indexed (TRACKER_PROPERTY (object),
 					   g_value_get_boolean (value));
 		break;
+	case PROP_SECONDARY_INDEX:
+		tracker_property_set_secondary_index (TRACKER_PROPERTY (object),
+					              g_value_get_object (value));
+		break;
 	case PROP_FULLTEXT_INDEXED:
 		tracker_property_set_fulltext_indexed (TRACKER_PROPERTY (object),
 						    g_value_get_boolean (value));
@@ -549,6 +569,19 @@ tracker_property_get_indexed (TrackerProperty *field)
 }
 
 
+TrackerProperty *
+tracker_property_get_secondary_index (TrackerProperty *field)
+{
+	TrackerPropertyPriv *priv;
+
+	g_return_val_if_fail (TRACKER_IS_PROPERTY (field), NULL);
+
+	priv = GET_PRIV (field);
+
+	return priv->secondary_index;
+}
+
+
 gboolean
 tracker_property_get_fulltext_indexed (TrackerProperty *field)
 {
@@ -788,6 +821,28 @@ tracker_property_set_indexed (TrackerProperty *field,
 }
 
 void
+tracker_property_set_secondary_index (TrackerProperty *field,
+			              TrackerProperty    *value)
+{
+	TrackerPropertyPriv *priv;
+
+	g_return_if_fail (TRACKER_IS_PROPERTY (field));
+
+	priv = GET_PRIV (field);
+
+	if (priv->secondary_index) {
+		g_object_unref (priv->secondary_index);
+		priv->secondary_index = NULL;
+	}
+
+	if (value) {
+		priv->secondary_index = g_object_ref (value);
+	}
+
+	g_object_notify (G_OBJECT (field), "secondary-index");
+}
+
+void
 tracker_property_set_fulltext_indexed (TrackerProperty *field,
 				       gboolean	        value)
 {
diff --git a/src/libtracker-common/tracker-property.h b/src/libtracker-common/tracker-property.h
index e6264b7..23bd455 100644
--- a/src/libtracker-common/tracker-property.h
+++ b/src/libtracker-common/tracker-property.h
@@ -81,6 +81,7 @@ TrackerClass *      tracker_property_get_domain           (TrackerProperty
 TrackerClass *      tracker_property_get_range            (TrackerProperty      *property);
 gint                tracker_property_get_weight           (TrackerProperty      *property);
 gboolean            tracker_property_get_indexed          (TrackerProperty      *property);
+TrackerProperty *   tracker_property_get_secondary_index  (TrackerProperty      *property);
 gboolean            tracker_property_get_fulltext_indexed (TrackerProperty      *property);
 gboolean            tracker_property_get_embedded         (TrackerProperty      *property);
 gboolean            tracker_property_get_multiple_values  (TrackerProperty      *property);
@@ -101,6 +102,8 @@ void                tracker_property_set_weight           (TrackerProperty
 							   gint                  value);
 void                tracker_property_set_indexed          (TrackerProperty      *property,
 							   gboolean              value);
+void                tracker_property_set_secondary_index  (TrackerProperty      *property,
+							   TrackerProperty      *value);
 void                tracker_property_set_fulltext_indexed (TrackerProperty      *property,
 							   gboolean              value);
 void                tracker_property_set_embedded         (TrackerProperty      *property,
diff --git a/src/libtracker-data/tracker-data-manager.c b/src/libtracker-data/tracker-data-manager.c
index 525d921..5eeabaa 100644
--- a/src/libtracker-data/tracker-data-manager.c
+++ b/src/libtracker-data/tracker-data-manager.c
@@ -222,6 +222,22 @@ load_ontology_file_from_path (const gchar	 *ontology_file)
 			if (strcmp (object, "true") == 0) {
 				tracker_property_set_indexed (property, TRUE);
 			}
+		} else if (g_strcmp0 (predicate, TRACKER_PREFIX "secondaryIndex") == 0) {
+			TrackerProperty *property, *secondary_index;
+
+			property = tracker_ontology_get_property_by_uri (subject);
+			if (property == NULL) {
+				g_critical ("%s: Unknown property %s", ontology_file, subject);
+				continue;
+			}
+
+			secondary_index = tracker_ontology_get_property_by_uri (object);
+			if (secondary_index == NULL) {
+				g_critical ("%s: Unknown property %s", ontology_file, object);
+				continue;
+			}
+
+			tracker_property_set_secondary_index (property, secondary_index);
 		} else if (g_strcmp0 (predicate, TRACKER_PREFIX "transient") == 0) {
 			TrackerProperty *property;
 
@@ -443,6 +459,7 @@ db_get_static_data (TrackerDBInterface *iface)
 						      "(SELECT Uri FROM \"rdfs:Resource\" WHERE ID = \"rdfs:range\"), "
 						      "\"nrl:maxCardinality\", "
 						      "\"tracker:indexed\", "
+						      "(SELECT Uri FROM \"rdfs:Resource\" WHERE ID = \"tracker:secondaryIndex\"), "
 						      "\"tracker:fulltextIndexed\", "
 						      "\"tracker:transient\", "
 						      "\"tracker:isAnnotation\", "
@@ -456,7 +473,7 @@ db_get_static_data (TrackerDBInterface *iface)
 
 		while (tracker_db_cursor_iter_next (cursor)) {
 			GValue value = { 0 };
-			TrackerProperty *property;
+			TrackerProperty *property, *secondary_index;
 			const gchar     *uri, *domain_uri, *range_uri;
 			gboolean         multi_valued, indexed, fulltext_indexed;
 			gboolean         transient, annotation, is_inverse_functional_property;
@@ -490,7 +507,7 @@ db_get_static_data (TrackerDBInterface *iface)
 				indexed = FALSE;
 			}
 
-			tracker_db_cursor_get_value (cursor, 6, &value);
+			tracker_db_cursor_get_value (cursor, 7, &value);
 
 			if (G_VALUE_TYPE (&value) != 0) {
 				fulltext_indexed = (g_value_get_int (&value) == 1);
@@ -500,7 +517,7 @@ db_get_static_data (TrackerDBInterface *iface)
 				fulltext_indexed = FALSE;
 			}
 
-			tracker_db_cursor_get_value (cursor, 7, &value);
+			tracker_db_cursor_get_value (cursor, 8, &value);
 
 			if (G_VALUE_TYPE (&value) != 0) {
 				transient = (g_value_get_int (&value) == 1);
@@ -510,7 +527,7 @@ db_get_static_data (TrackerDBInterface *iface)
 				transient = FALSE;
 			}
 
-			tracker_db_cursor_get_value (cursor, 8, &value);
+			tracker_db_cursor_get_value (cursor, 9, &value);
 
 			if (G_VALUE_TYPE (&value) != 0) {
 				annotation = (g_value_get_int (&value) == 1);
@@ -520,7 +537,7 @@ db_get_static_data (TrackerDBInterface *iface)
 				annotation = FALSE;
 			}
 
-			tracker_db_cursor_get_value (cursor, 9, &value);
+			tracker_db_cursor_get_value (cursor, 10, &value);
 
 			if (G_VALUE_TYPE (&value) != 0) {
 				is_inverse_functional_property = TRUE;
@@ -536,6 +553,7 @@ db_get_static_data (TrackerDBInterface *iface)
 			tracker_property_set_range (property, tracker_ontology_get_class_by_uri (range_uri));
 			tracker_property_set_multiple_values (property, multi_valued);
 			tracker_property_set_indexed (property, indexed);
+			tracker_property_set_secondary_index (property, tracker_ontology_get_property_by_uri (secondary_index));
 			tracker_property_set_fulltext_indexed (property, fulltext_indexed);
 			tracker_property_set_embedded (property, !annotation);
 			tracker_property_set_is_inverse_functional_property (property, is_inverse_functional_property);
@@ -725,7 +743,7 @@ create_decomposed_metadata_tables (TrackerDBInterface *iface,
 
 	/* create index for single-valued fields */
 	for (field_it = class_properties; field_it != NULL; field_it = field_it->next) {
-		TrackerProperty *field;
+		TrackerProperty *field, *secondary_index;
 		const char   *field_name;
 
 		field = field_it->data;
@@ -733,12 +751,23 @@ create_decomposed_metadata_tables (TrackerDBInterface *iface,
 		if (!tracker_property_get_multiple_values (field)
 		    && tracker_property_get_indexed (field)) {
 			field_name = tracker_property_get_name (field);
-			tracker_db_interface_execute_query (iface, NULL,
-							    "CREATE INDEX \"%s_%s\" ON \"%s\" (\"%s\")",
-							    service_name,
-							    field_name,
-							    service_name,
-							    field_name);
+			secondary_index = tracker_property_get_secondary_index (field);
+			if (secondary_index == NULL) {
+				tracker_db_interface_execute_query (iface, NULL,
+								    "CREATE INDEX \"%s_%s\" ON \"%s\" (\"%s\")",
+								    service_name,
+								    field_name,
+								    service_name,
+								    field_name);
+			} else {
+				tracker_db_interface_execute_query (iface, NULL,
+								    "CREATE INDEX \"%s_%s\" ON \"%s\" (\"%s\", \"%s\")",
+								    service_name,
+								    field_name,
+								    service_name,
+								    field_name,
+								    tracker_property_get_name (secondary_index));
+			}
 		}
 	}
 



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