Merge lp:~rainct/zeitgeist/rebuild-fts-on-new-db into lp:~zeitgeist/zeitgeist/bluebird

Proposed by Siegfried Gevatter
Status: Merged
Merged at revision: 465
Proposed branch: lp:~rainct/zeitgeist/rebuild-fts-on-new-db
Merge into: lp:~zeitgeist/zeitgeist/bluebird
Diff against target: 163 lines (+77/-22)
4 files modified
extensions/fts++/controller.cpp (+4/-0)
extensions/fts++/indexer.cpp (+26/-0)
extensions/fts++/indexer.h (+1/-0)
src/sql-schema.vala (+46/-22)
To merge this branch: bzr merge lp:~rainct/zeitgeist/rebuild-fts-on-new-db
Reviewer Review Type Date Requested Status
Michal Hruby (community) Needs Fixing
Review via email: mp+101024@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Michal Hruby (mhr3) wrote :

114 + var schema_sql = "INSERT INTO schema_version VALUES ('%s', %l64d)"

Let's use int64.FORMAT here.

review: Needs Fixing
Revision history for this message
Michal Hruby (mhr3) wrote :

74 + if (database_creation_date > metadata_date)

Shouldn't we just compare for unequality? (restoring db from backup etc)

Also, shouldn't we insert the schema row also when upgrading a DB from ver 4/5?

Revision history for this message
Michal Hruby (mhr3) wrote :

Also

74 + if (database_creation_date > metadata_date)

&& database_creation_date >= 0 should be added

466. By Siegfried Gevatter

Some bikeshed

Revision history for this message
Siegfried Gevatter (rainct) wrote :

OK, updated.

>> Also, shouldn't we insert the schema row also when upgrading a DB from ver 4/5?
>> && database_creation_date >= 0 should be added

Nope, -1 works just fine and saves us having to bump the schema version again for nothing.

Revision history for this message
Michal Hruby (mhr3) wrote :

24 +#include <sstream>

Remove pls.

50 + if (metadata_date_str == "")

if (metadata_date_str.empty())

52 + else
53 + {
54 + std::stringstream tmpstream;
55 + tmpstream << metadata_date_str;
56 + tmpstream >> metadata_date;
57 + }

metadata_date = g_ascii_strtoll (metadata_date_str.c_str (), NULL, 0);

review: Needs Fixing
467. By Siegfried Gevatter

sstream -> g_ascii_stroll

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'extensions/fts++/controller.cpp'
2--- extensions/fts++/controller.cpp 2012-04-03 18:40:44 +0000
3+++ extensions/fts++/controller.cpp 2012-04-05 22:37:20 +0000
4@@ -81,6 +81,10 @@
5
6 // Set the db metadata key only once we're done
7 PushTask (new MetadataTask ("fts_index_version", INDEX_VERSION));
8+ gint64 zg_creation_date = indexer->GetZeitgeistCreationDate ();
9+ std::string zg_creation_date_str (g_strdup_printf (
10+ "%" G_GINT64_FORMAT, zg_creation_date));
11+ PushTask (new MetadataTask ("zg_db_creation_date", zg_creation_date_str));
12 }
13
14 g_object_unref (time_range);
15
16=== modified file 'extensions/fts++/indexer.cpp'
17--- extensions/fts++/indexer.cpp 2012-04-03 18:40:44 +0000
18+++ extensions/fts++/indexer.cpp 2012-04-05 22:37:20 +0000
19@@ -146,6 +146,14 @@
20 }
21 }
22
23+gint64 Indexer::GetZeitgeistCreationDate ()
24+{
25+ ZeitgeistSQLiteDatabase *database = zeitgeist_db_reader_get_database (
26+ zg_reader);
27+ return zeitgeist_sq_lite_database_schema_get_creation_date (
28+ database->database);
29+}
30+
31 /**
32 * Returns true if and only if the index is good.
33 * Otherwise the index should be rebuild.
34@@ -164,6 +172,24 @@
35 return false;
36 }
37
38+ // Get stored Zeitgeist DB creation date
39+ gint64 metadata_date;
40+ std::string metadata_date_str (db->get_metadata ("zg_db_creation_date"));
41+ if (metadata_date_str.empty ())
42+ metadata_date = -1;
43+ else
44+ metadata_date = g_ascii_strtoll (metadata_date_str.c_str (), NULL, 0);
45+
46+ // In case the Zeitgeist DB is newer than Xapian, we need to re-build.
47+ // This may happen if the Zeitgeist DB gets corrupt and is re-created
48+ // from scratch.
49+ gint64 database_creation_date = GetZeitgeistCreationDate ();
50+ if (database_creation_date != metadata_date)
51+ {
52+ g_message ("Zeitgeist database has been replaced. Doing full rebuild");
53+ return false;
54+ }
55+
56 return true;
57 }
58
59
60=== modified file 'extensions/fts++/indexer.h'
61--- extensions/fts++/indexer.h 2012-04-03 19:01:28 +0000
62+++ extensions/fts++/indexer.h 2012-04-05 22:37:20 +0000
63@@ -80,6 +80,7 @@
64 void IndexEvent (ZeitgeistEvent *event);
65 void DeleteEvent (guint32 event_id);
66 void SetDbMetadata (std::string const& key, std::string const& value);
67+ gint64 GetZeitgeistCreationDate ();
68
69 GPtrArray* Search (const gchar *search,
70 ZeitgeistTimeRange *time_range,
71
72=== modified file 'src/sql-schema.vala'
73--- src/sql-schema.vala 2012-04-05 12:12:49 +0000
74+++ src/sql-schema.vala 2012-04-05 22:37:20 +0000
75@@ -38,6 +38,8 @@
76 public const string CORE_SCHEMA = "core";
77 public const int CORE_SCHEMA_VERSION = 6;
78
79+ private const string DATABASE_CREATION = "database_creation";
80+
81 public static void ensure_schema (Sqlite.Database database)
82 throws EngineError
83 {
84@@ -48,6 +50,12 @@
85 {
86 // most likely a new DB
87 create_schema (database);
88+
89+ // set database creation date
90+ var schema_sql = ("INSERT INTO schema_version VALUES ('%s', %" +
91+ int64.FORMAT + ")").printf (DATABASE_CREATION,
92+ Timestamp.now ());
93+ exec_query (database, schema_sql);
94 }
95 else if (schema_version == 4 || schema_version == 5)
96 {
97@@ -123,28 +131,44 @@
98 public static int get_schema_version (Sqlite.Database database)
99 throws EngineError
100 {
101- var sql = "SELECT version FROM schema_version WHERE schema='core'";
102- int schema_version = -1;
103- database.exec (sql,
104- (n_cols, values, column_names) =>
105- {
106- if (values[0] != null)
107- {
108- schema_version = int.parse (values[0]);
109- }
110- return 0;
111- }, null);
112-
113- // we don't really care about the return value of exec, the result
114- // will be -1 if something went wrong anyway
115- debug ("schema_version is %d", schema_version);
116-
117- if (schema_version < -1)
118- {
119- throw new EngineError.DATABASE_CORRUPT (
120- "Database corruption flag is set.");
121- }
122- return schema_version;
123+ int schema_version = (int) get_schema_metadata (database, CORE_SCHEMA);
124+ debug ("schema_version is %d", schema_version);
125+
126+ if (schema_version < -1)
127+ {
128+ throw new EngineError.DATABASE_CORRUPT (
129+ "Database corruption flag is set.");
130+ }
131+ return schema_version;
132+ }
133+
134+ public static int64 get_creation_date (Sqlite.Database database)
135+ {
136+ return get_schema_metadata (database, DATABASE_CREATION);
137+ }
138+
139+ private static int64 get_schema_metadata (Sqlite.Database database,
140+ string key)
141+ {
142+ var sql = "SELECT version FROM schema_version " +
143+ "WHERE schema='%s'".printf (key);
144+
145+ int64 schema_version = -1;
146+
147+ database.exec (sql,
148+ (n_cols, values, column_names) =>
149+ {
150+ if (values[0] != null)
151+ {
152+ schema_version = int64.parse (values[0]);
153+ }
154+ return 0;
155+ }, null);
156+
157+ // we don't really care about the return value of exec, the result
158+ // will be -1 if something went wrong anyway
159+
160+ return schema_version;
161 }
162
163 public static void set_corruption_flag (Sqlite.Database database)

Subscribers

People subscribed via source and target branches