[evolution-data-server/openismus-work-master: 15/76] EBookBackendSqliteDB: Pull out the code which keeps separate collators per summarized field.



commit b60fb77f082491500738d033f7a88fa7a6f7bf30
Author: Tristan Van Berkom <tristanvb openismus com>
Date:   Fri May 17 20:58:05 2013 +0900

    EBookBackendSqliteDB: Pull out the code which keeps separate collators per summarized field.
    
    For now let's keep a single ECollator, it seems that we don't want to configure
    collation rules but rather have a single collator API which maintains a
    knowledge base of what tailoring to choose in which locale.

 .../libedata-book/e-book-backend-sqlitedb.c        |  284 ++------------------
 1 files changed, 26 insertions(+), 258 deletions(-)
---
diff --git a/addressbook/libedata-book/e-book-backend-sqlitedb.c 
b/addressbook/libedata-book/e-book-backend-sqlitedb.c
index 8a28470..d6990ab 100644
--- a/addressbook/libedata-book/e-book-backend-sqlitedb.c
+++ b/addressbook/libedata-book/e-book-backend-sqlitedb.c
@@ -72,7 +72,6 @@ typedef struct {
        GType         type;    /* The GType (only support string or gboolean) */
        const gchar  *dbname;  /* The key for this field in the sqlite3 table */
        IndexFlags    index;   /* Whether this summary field should have an index in the SQLite DB */
-       ECollator    *collator;/* The ECollator to create sort keys for this field */
 } SummaryField;
 
 struct _EBookBackendSqliteDBPrivate {
@@ -90,6 +89,8 @@ struct _EBookBackendSqliteDBPrivate {
        gint            n_summary_fields;
        guint           have_attr_list : 1;
        IndexFlags      attr_list_indexes;
+
+       ECollator    *collator;/* The ECollator to create sort keys for all fields */
 };
 
 G_DEFINE_TYPE (EBookBackendSqliteDB, e_book_backend_sqlitedb, G_TYPE_OBJECT)
@@ -140,15 +141,6 @@ static gboolean upgrade_contacts_table (EBookBackendSqliteDB  *ebsdb,
                                         const gchar           *region,
                                         GError               **error);
 
-static gboolean summary_fields_add_collations (SummaryField   *summary_fields,
-                                              gint            n_summary_fields,
-                                              EContactField  *fields,
-                                              gchar         **collations,
-                                              gint            n_fields,
-                                              GError        **error);
-static void clear_collations (SummaryField *fields,
-                             gint          n_fields);
-
 
 static const gchar *
 get_current_collation_locale (void)
@@ -252,10 +244,10 @@ e_book_backend_sqlitedb_finalize (GObject *object)
        sqlite3_close (priv->db);
 
        g_free (priv->path);
-
-       clear_collations (priv->summary_fields, priv->n_summary_fields);
        g_free (priv->summary_fields);
 
+       e_collator_unref (priv->collator);
+
        g_mutex_clear (&priv->lock);
        g_mutex_clear (&priv->updates_lock);
 
@@ -521,7 +513,6 @@ collect_versions_cb (gpointer ref,
 typedef struct {
        gboolean has_countrycode;
        gboolean has_lc_collate;
-       gboolean has_collations;
 } LocaleColumns;
 
 static gint
@@ -538,8 +529,6 @@ find_locale_columns (gpointer data,
                        columns->has_countrycode = TRUE;
                else if (g_strcmp0 (cols[i], "lc_collate") == 0)
                        columns->has_lc_collate = TRUE;
-               else if (g_strcmp0 (cols[i], "collations") == 0)
-                       columns->has_collations = TRUE;
        }
 
        return 0;
@@ -552,7 +541,7 @@ create_folders_table (EBookBackendSqliteDB *ebsdb,
 {
        gboolean success;
        gint version = 0;
-       LocaleColumns locale_columns = { FALSE, FALSE, FALSE };
+       LocaleColumns locale_columns = { FALSE, FALSE };
 
        /* sync_data points to syncronization data, it could be last_modified
         * time or a sequence number or some text depending on the backend.
@@ -695,14 +684,6 @@ create_folders_table (EBookBackendSqliteDB *ebsdb,
                        goto rollback;
        }
 
-       if (!locale_columns.has_collations) {
-               stmt = "ALTER TABLE folders ADD COLUMN collations TEXT";
-               success = book_backend_sql_exec (ebsdb->priv->db, stmt, NULL, NULL, error);
-
-               if (!success)
-                       goto rollback;
-       }
-
        /* Remember the schema version for later use and finish the transaction. */
        *previous_schema = version;
        return book_backend_sqlitedb_commit_transaction (ebsdb, error);
@@ -767,71 +748,6 @@ get_count_cb (gpointer ref,
 }
 
 static gboolean
-add_collations_from_introspection (SummaryField *summary_fields,
-                                  gint          n_summary_fields,
-                                  const gchar  *collation_desc)
-{
-
-       gchar **fields;
-       GArray *coll_fields;
-       GArray *collations;
-       gboolean success = TRUE;
-       gchar *collation;
-       gint i;
-
-       /* Construct an array of collation fields and names */
-       coll_fields = g_array_new (FALSE, FALSE, sizeof (EContactField));
-       collations = g_array_new (TRUE, FALSE, sizeof (gchar *));
-
-       fields = g_strsplit (collation_desc, ":", 0);
-       for (i = 0; success && fields[i] != NULL; i++) {
-               EContactField  field;
-               gchar **rule;
-
-               rule = g_strsplit (fields[i], "=", 0);
-               /* Sanity check */
-               if (!rule[0] || !rule[1]) {
-                       success = FALSE;
-               } else {
-                       /* 0 is the invalid EContactField id */
-                       field = e_contact_field_id (rule[0]);
-                       if (field == 0)
-                               success = FALSE;
-               }
-
-               if (success == FALSE) {
-                       g_warning ("Malformed collation rule '%s'", fields[i]);
-                       g_strfreev (rule);
-                       break;
-               }
-               /* Add a value to the arrays */
-               collation = g_strdup (rule[1]);
-               g_array_append_val (coll_fields, field);
-               g_array_append_val (collations, collation);
-               
-               g_strfreev (rule);
-       }
-       g_strfreev (fields);
-
-       if (success)
-               success = summary_fields_add_collations (summary_fields,
-                                                        n_summary_fields,
-                                                        (EContactField *)coll_fields->data,
-                                                        (gchar **)collations->data,
-                                                        coll_fields->len,
-                                                        NULL);
-
-       for (i = 0; i < collations->len; i++) {
-               collation = g_array_index (collations, gchar *, i);
-               g_free (collation);
-       }
-       g_array_free (collations, TRUE);
-       g_array_free (coll_fields, TRUE);
-
-       return success;
-}
-
-static gboolean
 check_folderid_exists (EBookBackendSqliteDB *ebsdb,
                        const gchar *folderid,
                        gboolean *exists,
@@ -855,7 +771,6 @@ static gboolean
 add_folder_into_db (EBookBackendSqliteDB *ebsdb,
                     const gchar *folderid,
                     const gchar *folder_name,
-                   const gchar *initial_collations,
                    gboolean *already_exists,
                     GError **error)
 {
@@ -876,9 +791,9 @@ add_folder_into_db (EBookBackendSqliteDB *ebsdb,
 
                stmt = sqlite3_mprintf (
                        "INSERT OR IGNORE INTO "
-                       "folders ( folder_id, folder_name, version, multivalues, collations ) "
-                       "VALUES ( %Q, %Q, %d, %Q, %Q ) ",
-                       folderid, folder_name, FOLDER_VERSION, multivalues, initial_collations);
+                       "folders ( folder_id, folder_name, version, multivalues ) "
+                       "VALUES ( %Q, %Q, %d, %Q ) ",
+                       folderid, folder_name, FOLDER_VERSION, multivalues);
                success = book_backend_sql_exec (
                        ebsdb->priv->db, stmt, NULL, NULL, error);
                sqlite3_free (stmt);
@@ -937,7 +852,6 @@ introspect_summary (EBookBackendSqliteDB *ebsdb,
        GList *summary_columns = NULL, *l;
        GArray *summary_fields = NULL;
        gchar *multivalues = NULL;
-       gchar *collations = NULL;
        gint i, j;
 
        stmt = sqlite3_mprintf ("PRAGMA table_info (%Q);", folderid);
@@ -1053,19 +967,6 @@ introspect_summary (EBookBackendSqliteDB *ebsdb,
                g_strfreev (fields);
        }
 
-       /* Introspect custom collation fields, here we setup the custom ECollators
-        * on the SummaryFields if we need to.
-        */
-       stmt = sqlite3_mprintf (
-               "SELECT collations FROM folders WHERE folder_id = %Q", folderid);
-       success = book_backend_sql_exec (
-               ebsdb->priv->db, stmt, get_string_cb, &collations, error);
-       sqlite3_free (stmt);
-
-       success = add_collations_from_introspection ((SummaryField *)summary_fields->data,
-                                                    summary_fields->len,
-                                                    collations);
-
  introspect_summary_finish:
 
        g_list_free_full (summary_columns, (GDestroyNotify) g_free);
@@ -1073,13 +974,10 @@ introspect_summary (EBookBackendSqliteDB *ebsdb,
 
        /* Apply the introspected summary fields */
        if (success) {
-               clear_collations (ebsdb->priv->summary_fields, ebsdb->priv->n_summary_fields);
                g_free (ebsdb->priv->summary_fields);
-
                ebsdb->priv->n_summary_fields = summary_fields->len;
                ebsdb->priv->summary_fields = (SummaryField *) g_array_free (summary_fields, FALSE);
        } else if (summary_fields) {
-               clear_collations ((SummaryField *)summary_fields->data, summary_fields->len);
                g_array_free (summary_fields, TRUE);
        }
 
@@ -1670,13 +1568,14 @@ e_book_backend_sqlitedb_new_internal (const gchar *path,
                                       gint n_fields,
                                       gboolean have_attr_list,
                                       IndexFlags attr_list_indexes,
-                                     const gchar *initial_collations,
                                       GError **error)
 {
        EBookBackendSqliteDB *ebsdb;
        gchar *hash_key, *filename;
        gint previous_schema = 0;
        gboolean already_exists = FALSE;
+       const gchar *lc_collate;
+       ECollator *collator;
 
        g_return_val_if_fail (path != NULL, NULL);
        g_return_val_if_fail (emailid != NULL, NULL);
@@ -1696,6 +1595,13 @@ e_book_backend_sqlitedb_new_internal (const gchar *path,
                }
        }
 
+       lc_collate = get_current_collation_locale ();
+       collator = e_collator_new (lc_collate, error);
+       if (!collator) {
+               g_mutex_unlock (&dbcon_lock);
+               return NULL;
+       }
+
        ebsdb = g_object_new (E_TYPE_BOOK_BACKEND_SQLITEDB, NULL);
        ebsdb->priv->path = g_strdup (path);
        ebsdb->priv->summary_fields = fields;
@@ -1703,6 +1609,7 @@ e_book_backend_sqlitedb_new_internal (const gchar *path,
        ebsdb->priv->have_attr_list = have_attr_list;
        ebsdb->priv->attr_list_indexes = attr_list_indexes;
        ebsdb->priv->store_vcard = store_vcard;
+       ebsdb->priv->collator = collator;
 
        if (g_mkdir_with_parents (path, 0777) < 0) {
                g_mutex_unlock (&dbcon_lock);
@@ -1738,7 +1645,7 @@ e_book_backend_sqlitedb_new_internal (const gchar *path,
        LOCK_MUTEX (&ebsdb->priv->lock);
        g_mutex_unlock (&dbcon_lock);
 
-       if (!add_folder_into_db (ebsdb, folderid, folder_name, initial_collations,
+       if (!add_folder_into_db (ebsdb, folderid, folder_name,
                                 &already_exists, error)) {
                UNLOCK_MUTEX (&ebsdb->priv->lock);
                g_object_unref (ebsdb);
@@ -1864,116 +1771,6 @@ summary_fields_add_indexes (GArray *array,
        }
 }
 
-static gboolean
-ensure_collation (SummaryField  *field,
-                 GHashTable    *col_hash,
-                 const gchar   *collation_name,
-                 GError       **error)
-{
-       ECollator *collator;
-       const gchar *lc_collate;
-
-       collator = g_hash_table_lookup (col_hash, collation_name);
-
-       if (!collator) {
-               lc_collate = get_current_collation_locale ();
-
-               collator = e_collator_new (lc_collate, collation_name, error);
-               if (collator)
-                       g_hash_table_insert (col_hash,
-                                            g_strdup (collation_name),
-                                            collator);
-       }
-
-       if (collator)
-               field->collator = e_collator_ref (collator);
-
-       return (collator != NULL);
-}
-
-static gboolean
-summary_fields_add_collations (SummaryField   *summary_fields,
-                              gint            n_summary_fields,
-                              EContactField  *fields,
-                              gchar         **collations,
-                              gint            n_fields,
-                              GError        **error)
-{
-       GHashTable *col_hash;
-       gboolean  success = TRUE;
-       gint i, j;
-       ECollator *collator;
-       const gchar *lc_collate;
-
-       col_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
-                                         (GDestroyNotify)g_free,
-                                         (GDestroyNotify)e_collator_unref);
-
-       lc_collate = get_current_collation_locale ();
-       collator = e_collator_new (lc_collate, NULL, error);
-
-       if (!collator)
-               success = FALSE;
-
-       for (i = 0; success && i < n_summary_fields; i++) {
-               for (j = 0; j < n_fields; j++) {
-
-                       if (summary_fields[i].field == fields[j]) {
-                               success = ensure_collation (&(summary_fields[i]),
-                                                           col_hash,
-                                                           collations[j],
-                                                           error);
-                               break;
-                       }
-               }
-       }
-
-       /* Fill any gaps with the default collator */
-       for (i = 0; success && i < n_summary_fields; i++) {
-
-               if (summary_fields[i].collator == NULL)
-                       summary_fields[i].collator = e_collator_ref (collator);
-       }
-
-       g_hash_table_destroy (col_hash);
-       if (collator)
-               e_collator_unref (collator);
-
-       return success;
-}
-
-static void
-clear_collations (SummaryField *fields,
-                 gint          n_fields)
-{
-       gint i;
-
-       for (i = 0; i < n_fields; i++) {
-               if (fields[i].collator)
-                       e_collator_unref (fields[i].collator);
-       }
-}
-
-static gchar *
-format_collations (EContactField *coll_fields,
-                  gchar        **collations,
-                  gint           n_fields)
-{
-       GString *string = g_string_new ("");
-       gint i;
-
-       for (i = 0; i < n_fields; i++) {
-               if (i > 0)
-                       g_string_append_c (string, ':');
-
-               g_string_append_printf (string, "%s=%s",
-                                       e_contact_field_name (coll_fields[i]),
-                                       collations[i]);
-       }
-
-       return g_string_free (string, FALSE);
-}
-
 /**
  * e_book_backend_sqlitedb_new_full:
  * @path: location where the db would be created
@@ -2011,18 +1808,15 @@ e_book_backend_sqlitedb_new_full (const gchar *path,
        EBookBackendSqliteDB *ebsdb = NULL;
        EContactField *fields;
        EContactField *indexed_fields;
-       EContactField *coll_fields;
        EBookIndexType *index_types = NULL;
-       gchar **collations = NULL, *initial_collations;
        gboolean have_attr_list = FALSE;
        IndexFlags attr_list_indexes = 0;
        gboolean had_error = FALSE;
        GArray *summary_fields;
-       gint n_fields = 0, n_indexed_fields = 0, n_coll_fields = 0, i;
+       gint n_fields = 0, n_indexed_fields = 0, i;
 
        fields         = e_source_backend_summary_setup_get_summary_fields (setup, &n_fields);
        indexed_fields = e_source_backend_summary_setup_get_indexed_fields (setup, &index_types, 
&n_indexed_fields);
-       coll_fields    = e_source_backend_summary_setup_get_collations     (setup, &collations, 
&n_coll_fields);
 
        /* No specified summary fields indicates the default summary configuration should be used */
        if (n_fields <= 0) {
@@ -2047,18 +1841,7 @@ e_book_backend_sqlitedb_new_full (const gchar *path,
                }
        }
 
-       /* Add the specialized collation types, populate the ECollators */
-       if (!had_error)
-               had_error = !summary_fields_add_collations ((SummaryField *)summary_fields->data,
-                                                           summary_fields->len,
-                                                           coll_fields,
-                                                           collations,
-                                                           n_coll_fields,
-                                                           error);
-
        if (had_error) {
-
-               clear_collations ((SummaryField *)summary_fields->data, summary_fields->len);
                g_array_free (summary_fields, TRUE);
                g_free (fields);
                g_free (index_types);
@@ -2071,8 +1854,6 @@ e_book_backend_sqlitedb_new_full (const gchar *path,
                summary_fields, indexed_fields, index_types, n_indexed_fields,
                &attr_list_indexes);
 
-       initial_collations = format_collations (coll_fields, collations, n_coll_fields);
-
        ebsdb = e_book_backend_sqlitedb_new_internal (
                path, emailid, folderid, folder_name,
                store_vcard,
@@ -2080,15 +1861,11 @@ e_book_backend_sqlitedb_new_full (const gchar *path,
                summary_fields->len,
                have_attr_list,
                attr_list_indexes,
-               initial_collations,
                error);
 
        g_free (fields);
        g_free (index_types);
        g_free (indexed_fields);
-       g_free (initial_collations);
-       g_free (coll_fields);
-       g_strfreev (collations);
        g_array_free (summary_fields, FALSE);
 
        return ebsdb;
@@ -2137,10 +1914,6 @@ e_book_backend_sqlitedb_new (const gchar *path,
                G_N_ELEMENTS (default_indexed_fields),
                &attr_list_indexes);
 
-       /* Add the default collator to all columns */
-       summary_fields_add_collations ((SummaryField *)summary_fields->data,
-                                      summary_fields->len, NULL, NULL, 0, NULL);
-
        ebsdb = e_book_backend_sqlitedb_new_internal (
                path, emailid, folderid, folder_name,
                store_vcard,
@@ -2148,7 +1921,7 @@ e_book_backend_sqlitedb_new (const gchar *path,
                summary_fields->len,
                have_attr_list,
                attr_list_indexes,
-               "", error);
+               error);
 
        g_array_free (summary_fields, FALSE);
 
@@ -2344,7 +2117,7 @@ insert_stmt_from_contact (EBookBackendSqliteDB *ebsdb,
                                normal = e_util_utf8_normalize (val);
 
                                if (val)
-                                       localized = e_collator_generate_key 
(ebsdb->priv->summary_fields[i].collator, val, NULL);
+                                       localized = e_collator_generate_key (ebsdb->priv->collator, val, 
NULL);
                                else
                                        localized = g_strdup ("");
                        } else
@@ -5440,18 +5213,16 @@ ebsdb_cursor_set_state_from_contact (EBookBackendSqliteDB *ebsdb,
                                     EbSdbCursor          *cursor,
                                     EContact             *contact)
 {
-       gint i, index;
+       gint i;
 
        ebsdb_cursor_clear_state (cursor);
 
        for (i = 0; i < cursor->n_sort_fields; i++) {
                const gchar *string = e_contact_get_const (contact, cursor->sort_fields[i]);
 
-               index = summary_index_from_field (ebsdb, cursor->sort_fields[i]);
-
                if (string)
                        cursor->values[i] =
-                               e_collator_generate_key (ebsdb->priv->summary_fields[index].collator,
+                               e_collator_generate_key (ebsdb->priv->collator,
                                                         string, NULL);
                else
                        cursor->values[i] = g_strdup ("");
@@ -5931,7 +5702,7 @@ e_book_backend_sqlitedb_cursor_set_targetv (EBookBackendSqliteDB *ebsdb,
                                            const gchar         **values,
                                            gint                  n_values)
 {
-       gint i, index;
+       gint i;
 
        g_return_if_fail (E_IS_BOOK_BACKEND_SQLITEDB (ebsdb));
        g_return_if_fail (cursor != NULL);
@@ -5940,10 +5711,7 @@ e_book_backend_sqlitedb_cursor_set_targetv (EBookBackendSqliteDB *ebsdb,
        ebsdb_cursor_clear_state (cursor);
 
        for (i = 0; i < MIN (cursor->n_sort_fields, n_values); i++) {
-
-               index = summary_index_from_field (ebsdb, cursor->sort_fields[i]);
-
-               cursor->values[i] = e_collator_generate_key (ebsdb->priv->summary_fields[index].collator, 
values[i], NULL);
+               cursor->values[i] = e_collator_generate_key (ebsdb->priv->collator, values[i], NULL);
        }
 
        if (n_values > cursor->n_sort_fields)


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