~zeitgeist/zeitgeist/bluebird

« back to all changes in this revision

Viewing changes to src/db-reader.vala

  • Committer: Siegfried-Angel Gevatter Pujals
  • Date: 2012-03-13 19:12:59 UTC
  • mfrom: (420.1.2 common-where)
  • Revision ID: siegfried@gevatter.com-20120313191259-c5n41ltr474ndzhs
Merge: Split find_event_ids into _for_clause and _for_query

Show diffs side-by-side

added added

removed removed

Lines of Context:
160
160
        return results;
161
161
    }
162
162
 
163
 
    public uint32[] find_event_ids (TimeRange time_range,
164
 
        GenericArray<Event> event_templates,
165
 
        uint storage_state, uint max_events, uint result_type,
166
 
        BusName? sender=null) throws EngineError
 
163
    public uint32[] find_event_ids_for_clause (WhereClause where,
 
164
        uint max_events, uint result_type) throws EngineError
167
165
    {
168
 
 
169
 
        WhereClause where = new WhereClause (WhereClause.Type.AND);
170
 
 
171
 
        /**
172
 
         * We are using the unary operator here to tell SQLite to not use
173
 
         * the index on the timestamp column at the first place. This is a
174
 
         * "fix" for (LP: #672965) based on some benchmarks, which suggest
175
 
         * a performance win, but we might not oversee all implications.
176
 
         * (See http://www.sqlite.org/optoverview.html, section 6.0).
177
 
         *    -- Markus Korn, 29/11/2010
178
 
         */
179
 
        if (time_range.start != 0)
180
 
            where.add (("+timestamp >= %" + int64.FORMAT).printf(
181
 
                time_range.start));
182
 
        if (time_range.end != 0)
183
 
            where.add (("+timestamp <= %" + int64.FORMAT).printf(
184
 
                time_range.end));
185
 
 
186
 
        if (storage_state == StorageState.AVAILABLE ||
187
 
            storage_state == StorageState.NOT_AVAILABLE)
188
 
        {
189
 
            where.add ("(subj_storage_state=? OR subj_storage_state IS NULL)",
190
 
                storage_state.to_string ());
191
 
        }
192
 
        else if (storage_state != StorageState.ANY)
193
 
        {
194
 
            throw new EngineError.INVALID_ARGUMENT(
195
 
                "Unknown storage state '%u'".printf(storage_state));
196
 
        }
197
 
 
198
 
        WhereClause tpl_conditions = get_where_clause_from_event_templates (
199
 
            event_templates);
200
 
        where.extend (tpl_conditions);
201
 
        //if (!where.may_have_results ())
202
 
        //    return new uint32[0];
203
 
 
204
166
        string sql = "SELECT id FROM event_view ";
205
167
        string where_sql = "";
206
168
        if (!where.is_empty ())
352
314
        return event_ids;
353
315
    }
354
316
 
 
317
    public uint32[] find_event_ids (TimeRange time_range,
 
318
        GenericArray<Event> event_templates,
 
319
        uint storage_state, uint max_events, uint result_type,
 
320
        BusName? sender=null) throws EngineError
 
321
    {
 
322
        WhereClause where = get_where_clause_for_query (time_range,
 
323
            event_templates, storage_state);
 
324
 
 
325
        //if (!where.may_have_results ())
 
326
        //    return new uint32[0];
 
327
 
 
328
        return find_event_ids_for_clause (where, max_events, result_type);
 
329
    }
 
330
 
355
331
    public GenericArray<Event?> find_events (TimeRange time_range,
356
332
        GenericArray<Event> event_templates,
357
333
        uint storage_state, uint max_events, uint result_type,
361
337
            storage_state, max_events, result_type));
362
338
    }
363
339
 
 
340
    public WhereClause get_where_clause_for_query (TimeRange time_range,
 
341
        GenericArray<Event> event_templates, uint storage_state) throws EngineError
 
342
    {
 
343
        WhereClause where = new WhereClause (WhereClause.Type.AND);
 
344
 
 
345
        /**
 
346
         * We are using the unary operator here to tell SQLite to not use
 
347
         * the index on the timestamp column at the first place. This is a
 
348
         * "fix" for (LP: #672965) based on some benchmarks, which suggest
 
349
         * a performance win, but we might not oversee all implications.
 
350
         * (See http://www.sqlite.org/optoverview.html, section 6.0).
 
351
         *    -- Markus Korn, 29/11/2010
 
352
         */
 
353
        if (time_range.start != 0)
 
354
            where.add (("+timestamp >= %" + int64.FORMAT).printf(
 
355
                time_range.start));
 
356
        if (time_range.end != 0)
 
357
            where.add (("+timestamp <= %" + int64.FORMAT).printf(
 
358
                time_range.end));
 
359
 
 
360
        if (storage_state == StorageState.AVAILABLE ||
 
361
            storage_state == StorageState.NOT_AVAILABLE)
 
362
        {
 
363
            where.add ("(subj_storage_state=? OR subj_storage_state IS NULL)",
 
364
                storage_state.to_string ());
 
365
        }
 
366
        else if (storage_state != StorageState.ANY)
 
367
        {
 
368
            throw new EngineError.INVALID_ARGUMENT(
 
369
                "Unknown storage state '%u'".printf(storage_state));
 
370
        }
 
371
 
 
372
        WhereClause tpl_conditions = get_where_clause_from_event_templates (
 
373
            event_templates);
 
374
        where.extend (tpl_conditions);
 
375
 
 
376
        return where;
 
377
    }
 
378
 
364
379
    private struct RelatedUri {
365
380
        public uint32 id;
366
381
        public int64 timestamp;
596
611
    }
597
612
 
598
613
    // Used by find_event_ids
599
 
    protected WhereClause get_where_clause_from_event_templates (
 
614
    public WhereClause get_where_clause_from_event_templates (
600
615
        GenericArray<Event> templates) throws EngineError
601
616
    {
602
617
        WhereClause where = new WhereClause (WhereClause.Type.OR);