 Chromium Code Reviews
 Chromium Code Reviews Issue 9610006:
  Refactoring, moving and renaming the NetworkActionPredictor.  (Closed) 
  Base URL: http://git.chromium.org/chromium/src.git@master
    
  
    Issue 9610006:
  Refactoring, moving and renaming the NetworkActionPredictor.  (Closed) 
  Base URL: http://git.chromium.org/chromium/src.git@master| Index: chrome/browser/predictors/predictor_database.h | 
| diff --git a/chrome/browser/predictors/predictor_database.h b/chrome/browser/predictors/predictor_database.h | 
| new file mode 100644 | 
| index 0000000000000000000000000000000000000000..e5d71e2d589d3491102706a7aa8dbae90e88ff40 | 
| --- /dev/null | 
| +++ b/chrome/browser/predictors/predictor_database.h | 
| @@ -0,0 +1,148 @@ | 
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved. | 
| +// Use of this source code is governed by a BSD-style license that can be | 
| +// found in the LICENSE file. | 
| + | 
| +#ifndef CHROME_BROWSER_PREDICTORS_PREDICTOR_DATABASE_H_ | 
| +#define CHROME_BROWSER_PREDICTORS_PREDICTOR_DATABASE_H_ | 
| +#pragma once | 
| + | 
| +#include <ostream> | 
| 
dominich
2012/03/06 16:42:13
why is ostream needed?
 
Shishir
2012/03/14 21:14:37
From the earlier file. Removed.
 | 
| +#include <string> | 
| +#include <vector> | 
| + | 
| +#include "base/file_path.h" | 
| +#include "base/memory/ref_counted.h" | 
| +#include "base/string16.h" | 
| +#include "base/synchronization/cancellation_flag.h" | 
| +#include "chrome/browser/profiles/refcounted_profile_keyed_service.h" | 
| +#include "googleurl/src/gurl.h" | 
| +#include "sql/connection.h" | 
| + | 
| +class Profile; | 
| + | 
| +namespace predictors { | 
| + | 
| +// Refcounted as it is created and destroyed in the UI thread, but its database | 
| +// related functions have to be called in the DB thread. | 
| +class PredictorTableBase | 
| 
dominich
2012/03/06 16:42:13
Could be TableBase as we're in a namespace
 
Shishir
2012/03/14 21:14:37
As we discussed, leaving as is. We want the names
 | 
| + : public base::RefCountedThreadSafe<PredictorTableBase> { | 
| + protected: | 
| + PredictorTableBase(); | 
| + virtual ~PredictorTableBase(); | 
| + virtual void CreateTableIfNonExistant() = 0; | 
| 
dominich
2012/03/06 16:42:13
nit: Existent
 
Shishir
2012/03/14 21:14:37
Done.
 | 
| + | 
| + sql::Connection* db_; | 
| + base::CancellationFlag cancelled_; | 
| + | 
| + private: | 
| + friend class PredictorDatabase; | 
| + friend class base::RefCountedThreadSafe<PredictorTableBase>; | 
| + | 
| + void SetCancelled(); | 
| 
dominich
2012/03/06 16:42:13
note for dominic: it's a bit weird to have the fla
 
Shishir
2012/03/14 21:14:37
Yes, a little wierd, maybe move to protected?
 | 
| + void Initialize(sql::Connection* db); | 
| + | 
| + DISALLOW_COPY_AND_ASSIGN(PredictorTableBase); | 
| +}; | 
| + | 
| + | 
| +// This manages the autocomplete predictor table within the SQLite database | 
| +// passed in to the constructor. It expects the following scheme: | 
| +// | 
| +// network_action_predictor | 
| +// id A unique id. | 
| +// user_text What the user typed. | 
| +// url The URL of the entry. | 
| +// number_of_hits Number of times the entry was shown to the user and | 
| +// selected. | 
| +// number_of_misses Number of times the entry was shown to the user but not | 
| +// selected. | 
| +// | 
| +// TODO(dominich): Consider adding this table to one of the history databases. | 
| +// In memory is currently used, but adding to the on-disk visits database | 
| +// would allow DeleteOldEntries to be cheaper through use of a join. | 
| +class AutocompleteActionPredictorTable : public PredictorTableBase { | 
| 
dominich
2012/03/06 16:42:13
could drop 'Predictor' from the class name
 
Shishir
2012/03/14 21:14:37
As discussed we want to keep this.
 | 
| + public: | 
| + struct Row { | 
| + // TODO(dominich): Make this 64-bit integer as an optimization. This | 
| + // requires some investigation into how to make sure the id is unique for | 
| + // each user_text/url pair. | 
| + // http://crbug.com/102020 | 
| + typedef std::string Id; | 
| + | 
| + Row(); | 
| + | 
| + // Only used by unit tests. | 
| + Row(const Id& id, | 
| + const string16& user_text, | 
| + const GURL& url, | 
| + int number_of_hits, | 
| + int number_of_misses); | 
| + | 
| + Row(const Row& row); | 
| + | 
| + Id id; | 
| + string16 user_text; | 
| + GURL url; | 
| + int number_of_hits; | 
| + int number_of_misses; | 
| + }; | 
| + | 
| + void GetRow(const Row::Id& id, Row* row); | 
| + void GetAllRows(std::vector<Row>* row_buffer); | 
| + | 
| + void AddRow(const Row& row); | 
| + void UpdateRow(const Row& row); | 
| + void AddAndUpdateRows(const std::vector<Row>& rows_to_add, | 
| + const std::vector<Row>& rows_to_update); | 
| + void DeleteRow(const Row::Id& id); | 
| + void DeleteRows(const std::vector<Row::Id>& id_list); | 
| + void DeleteAllRows(); | 
| + | 
| + private: | 
| + friend class PredictorDatabase; | 
| + | 
| + AutocompleteActionPredictorTable(); | 
| + virtual ~AutocompleteActionPredictorTable(); | 
| + | 
| + void CreateTableIfNonExistant() OVERRIDE; | 
| 
dominich
2012/03/06 16:42:13
nit: Existent
 
Shishir
2012/03/14 21:14:37
Done.
 | 
| + | 
| + bool StepAndInitializeRow(sql::Statement* statement, Row* row); | 
| + void BindRowToStatement(const Row& row, sql::Statement* statement); | 
| + | 
| + DISALLOW_COPY_AND_ASSIGN(AutocompleteActionPredictorTable); | 
| +}; | 
| + | 
| + | 
| +// RefcountedProfileKeyedService as it is created and destroyed on a different | 
| +// thread to the DB thread that is required for all methods performing database | 
| +// access. | 
| +class PredictorDatabase : public RefcountedProfileKeyedService { | 
| 
dominich
2012/03/06 16:42:13
could drop 'Predictor' from name
 
Shishir
2012/03/14 21:14:37
As we discussed, leaving as is. We want the names
 | 
| + public: | 
| + explicit PredictorDatabase(Profile* profile); | 
| + virtual ~PredictorDatabase(); | 
| + | 
| + // Opens the database file from the profile path. Separated from the | 
| + // constructor to ease construction/destruction of this object on one thread | 
| + // but database access on the DB thread. | 
| + void Initialize(); | 
| + | 
| + AutocompleteActionPredictorTable* autocomplete_table() { | 
| + return autocomplete_table_.get(); | 
| + } | 
| + | 
| + private: | 
| + friend class AutocompleteActionPredictorTableTest; | 
| + | 
| + // RefcountedProfileKeyedService. | 
| + virtual void ShutdownOnUIThread() OVERRIDE; | 
| + | 
| + FilePath db_path_; | 
| + sql::Connection db_; | 
| + scoped_refptr<AutocompleteActionPredictorTable> autocomplete_table_; | 
| + | 
| + DISALLOW_COPY_AND_ASSIGN(PredictorDatabase); | 
| +}; | 
| + | 
| +} // namespace predictors | 
| + | 
| +#endif // CHROME_BROWSER_PREDICTORS_PREDICTOR_DATABASE_H_ |