| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 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 #include "services/catalog/entry_cache.h" |
| 6 |
| 7 #include "services/catalog/entry.h" |
| 8 |
| 9 namespace catalog { |
| 10 |
| 11 EntryCache::EntryCache() {} |
| 12 |
| 13 EntryCache::~EntryCache() {} |
| 14 |
| 15 bool EntryCache::AddRootEntry(std::unique_ptr<Entry> entry) { |
| 16 DCHECK(entry); |
| 17 const std::string& name = entry->name(); |
| 18 if (!AddEntry(entry.get())) |
| 19 return false; |
| 20 root_entries_.insert(std::make_pair(name, std::move(entry))); |
| 21 return true; |
| 22 } |
| 23 |
| 24 const Entry* EntryCache::GetEntry(const std::string& name) { |
| 25 auto iter = entries_.find(name); |
| 26 if (iter == entries_.end()) |
| 27 return nullptr; |
| 28 return iter->second; |
| 29 } |
| 30 |
| 31 bool EntryCache::AddEntry(const Entry* entry) { |
| 32 auto root_iter = root_entries_.find(entry->name()); |
| 33 if (root_iter != root_entries_.end()) { |
| 34 RemoveEntry(root_iter->second.get()); |
| 35 root_entries_.erase(root_iter); |
| 36 } else { |
| 37 auto entry_iter = entries_.find(entry->name()); |
| 38 if (entry_iter != entries_.end()) { |
| 39 // There's already a non-root entry for this name, so we change nothing. |
| 40 return false; |
| 41 } |
| 42 } |
| 43 |
| 44 entries_.insert({ entry->name(), entry }); |
| 45 for (const auto& child : entry->children()) |
| 46 AddEntry(child.get()); |
| 47 return true; |
| 48 } |
| 49 |
| 50 void EntryCache::RemoveEntry(const Entry* entry) { |
| 51 auto iter = entries_.find(entry->name()); |
| 52 if (iter->second == entry) |
| 53 entries_.erase(iter); |
| 54 for (const auto& child : entry->children()) |
| 55 RemoveEntry(child.get()); |
| 56 } |
| 57 |
| 58 } // namespace catalog |
| OLD | NEW |