Home | History | Annotate | Download | only in predictors
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef CHROME_BROWSER_PREDICTORS_AUTOCOMPLETE_ACTION_PREDICTOR_TABLE_H_
      6 #define CHROME_BROWSER_PREDICTORS_AUTOCOMPLETE_ACTION_PREDICTOR_TABLE_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/strings/string16.h"
     12 #include "chrome/browser/predictors/predictor_table_base.h"
     13 #include "url/gurl.h"
     14 
     15 namespace predictors {
     16 
     17 // This manages the autocomplete predictor table within the SQLite database
     18 // passed in to the constructor. It expects the following scheme:
     19 //
     20 // network_action_predictor
     21 //   id                 A unique id.
     22 //   user_text          What the user typed.
     23 //   url                The URL of the entry.
     24 //   number_of_hits     Number of times the entry was shown to the user and
     25 //                      selected.
     26 //   number_of_misses   Number of times the entry was shown to the user but not
     27 //                      selected.
     28 //
     29 // TODO(dominich): Consider adding this table to one of the history databases.
     30 // In memory is currently used, but adding to the on-disk visits database
     31 // would allow DeleteOldEntries to be cheaper through use of a join.
     32 //
     33 // All the functions apart from constructor and destructor have to be called in
     34 // the DB thread.
     35 class AutocompleteActionPredictorTable : public PredictorTableBase {
     36  public:
     37   struct Row {
     38     // TODO(dominich): Make this 64-bit integer as an optimization. This
     39     // requires some investigation into how to make sure the id is unique for
     40     // each user_text/url pair.
     41     // http://crbug.com/102020
     42     typedef std::string Id;
     43 
     44     Row();
     45 
     46     // Only used by unit tests.
     47     Row(const Id& id,
     48         const string16& user_text,
     49         const GURL& url,
     50         int number_of_hits,
     51         int number_of_misses);
     52 
     53     Row(const Row& row);
     54 
     55     Id id;
     56     string16 user_text;
     57     GURL url;
     58     int number_of_hits;
     59     int number_of_misses;
     60   };
     61 
     62   typedef std::vector<Row> Rows;
     63 
     64   // DB thread functions.
     65   void GetRow(const Row::Id& id, Row* row);
     66   void GetAllRows(Rows* row_buffer);
     67   void AddRow(const Row& row);
     68   void UpdateRow(const Row& row);
     69   void AddAndUpdateRows(const Rows& rows_to_add, const Rows& rows_to_update);
     70   void DeleteRows(const std::vector<Row::Id>& id_list);
     71   void DeleteAllRows();
     72 
     73  private:
     74   friend class PredictorDatabaseInternal;
     75 
     76   AutocompleteActionPredictorTable();
     77   virtual ~AutocompleteActionPredictorTable();
     78 
     79   // PredictorTableBase methods (DB thread).
     80   virtual void CreateTableIfNonExistent() OVERRIDE;
     81   virtual void LogDatabaseStats() OVERRIDE;
     82 
     83   DISALLOW_COPY_AND_ASSIGN(AutocompleteActionPredictorTable);
     84 };
     85 
     86 }  // namespace predictors
     87 
     88 #endif  // CHROME_BROWSER_PREDICTORS_AUTOCOMPLETE_ACTION_PREDICTOR_TABLE_H_
     89