diff options
author | lipalani@chromium.org <lipalani@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-04-06 18:34:43 +0000 |
---|---|---|
committer | lipalani@chromium.org <lipalani@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-04-06 18:34:43 +0000 |
commit | d2b21e8d4c93fb3c95693eed3ceae02a910e33b5 (patch) | |
tree | f7430a34d4b4403389a5528c21590afa7a88e918 | |
parent | 19bebd0304fabb04866125d296399a0c68a1955f (diff) | |
download | chromium_src-d2b21e8d4c93fb3c95693eed3ceae02a910e33b5.zip chromium_src-d2b21e8d4c93fb3c95693eed3ceae02a910e33b5.tar.gz chromium_src-d2b21e8d4c93fb3c95693eed3ceae02a910e33b5.tar.bz2 |
Implement search feature backend. This does a DFS and returns the matching node ids in a vector.
This searchs the text in entityspecifics of a node. This still does ot search ids.
BUG=76812
TEST=
Review URL: http://codereview.chromium.org/6693049
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@80665 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/sync/engine/syncapi.cc | 89 | ||||
-rw-r--r-- | chrome/browser/sync/engine/syncapi.h | 4 |
2 files changed, 93 insertions, 0 deletions
diff --git a/chrome/browser/sync/engine/syncapi.cc b/chrome/browser/sync/engine/syncapi.cc index f26cf9a..4303720 100644 --- a/chrome/browser/sync/engine/syncapi.cc +++ b/chrome/browser/sync/engine/syncapi.cc @@ -1077,6 +1077,21 @@ DictionaryValue* SyncManager::ChangeRecord::ToValue( return value; } +bool BaseNode::ContainsString(const std::string& lowercase_query) const { + DCHECK(GetEntry()); + // TODO(lipalani) - figure out what to do if the node is encrypted. + const sync_pb::EntitySpecifics& specifics = GetEntry()->Get(SPECIFICS); + std::string temp; + // The protobuf serialized string contains the original strings. So + // we will just serialize it and search it. + specifics.SerializeToString(&temp); + + // Now convert to lower case. + StringToLowerASCII(&temp); + + return temp.find(lowercase_query) != std::string::npos; +} + SyncManager::ExtraPasswordChangeRecordData::ExtraPasswordChangeRecordData() {} SyncManager::ExtraPasswordChangeRecordData::ExtraPasswordChangeRecordData( @@ -1349,6 +1364,8 @@ class SyncManager::SyncInternal const browser_sync::JsArgList& args, const browser_sync::JsEventHandler* sender); + ListValue* FindNodesContainingString(const std::string& query); + private: // Helper to call OnAuthError when no authentication credentials are // available. @@ -1465,6 +1482,9 @@ class SyncManager::SyncInternal browser_sync::JsArgList ProcessGetNodeByIdMessage( const browser_sync::JsArgList& args); + browser_sync::JsArgList ProcessFindNodesContainingString( + const browser_sync::JsArgList& args); + // We couple the DirectoryManager and username together in a UserShare member // so we can return a handle to share_ to clients of the API for use when // constructing any transaction type. @@ -2036,6 +2056,52 @@ void SyncManager::SyncInternal::EncryptDataTypes( return; } +namespace { + +void FindChildNodesContainingString(const std::string& lowercase_query, + const ReadNode& parent_node, + sync_api::ReadTransaction* trans, + ListValue* result) { + int64 child_id = parent_node.GetFirstChildId(); + while (child_id != kInvalidId) { + ReadNode node(trans); + if (node.InitByIdLookup(child_id)) { + if (node.ContainsString(lowercase_query)) { + result->Append(new StringValue(base::Int64ToString(child_id))); + } + FindChildNodesContainingString(lowercase_query, node, trans, result); + child_id = node.GetSuccessorId(); + } else { + LOG(WARNING) << "Lookup of node failed. Id: " << child_id; + return; + } + } +} +} // namespace + +// Returned pointer owned by the caller. +ListValue* SyncManager::SyncInternal::FindNodesContainingString( + const std::string& query) { + // Convert the query string to lower case to perform case insensitive + // searches. + std::string lowercase_query = query; + StringToLowerASCII(&lowercase_query); + ReadTransaction trans(GetUserShare()); + ReadNode root(&trans); + root.InitByRootLookup(); + + ListValue* result = new ListValue(); + + base::Time start_time = base::Time::Now(); + FindChildNodesContainingString(lowercase_query, root, &trans, result); + base::Time end_time = base::Time::Now(); + + base::TimeDelta delta = end_time - start_time; + VLOG(1) << "Time taken in milliseconds to search " << delta.InMilliseconds(); + + return result; +} + void SyncManager::SyncInternal::ReEncryptEverything(WriteTransaction* trans) { syncable::ModelTypeSet encrypted_types = GetEncryptedDataTypes(trans->GetWrappedTrans()); @@ -2602,6 +2668,14 @@ void SyncManager::SyncInternal::ProcessMessage( } parent_router_->RouteJsEvent( "onGetNodeByIdFinished", ProcessGetNodeByIdMessage(args), sender); + } else if (name == "findNodesContainingString") { + if (!parent_router_) { + LogNoRouter(name, args); + return; + } + parent_router_->RouteJsEvent( + "onFindNodesContainingStringFinished", + ProcessFindNodesContainingString(args), sender); } else { VLOG(1) << "Dropping unknown message " << name << " with args " << args.ToString(); @@ -2634,6 +2708,21 @@ browser_sync::JsArgList SyncManager::SyncInternal::ProcessGetNodeByIdMessage( return browser_sync::JsArgList(return_args); } +browser_sync::JsArgList SyncManager::SyncInternal:: + ProcessFindNodesContainingString( + const browser_sync::JsArgList& args) { + std::string query; + ListValue return_args; + if (!args.Get().GetString(0, &query)) { + return_args.Append(new ListValue()); + return browser_sync::JsArgList(return_args); + } + + ListValue* result = FindNodesContainingString(query); + return_args.Append(result); + return browser_sync::JsArgList(return_args); +} + void SyncManager::SyncInternal::OnNotificationStateChange( bool notifications_enabled) { VLOG(1) << "P2P: Notifications enabled = " diff --git a/chrome/browser/sync/engine/syncapi.h b/chrome/browser/sync/engine/syncapi.h index 18da106..2ec06a3 100644 --- a/chrome/browser/sync/engine/syncapi.h +++ b/chrome/browser/sync/engine/syncapi.h @@ -257,6 +257,10 @@ class BaseNode { // Transfers ownership of the DictionaryValue to the caller. DictionaryValue* ToValue() const; + // Does a case in-sensitive search for a given string, which must be + // lower case. + bool ContainsString(const std::string& lowercase_query) const; + protected: BaseNode(); virtual ~BaseNode(); |