File: feature_tree.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (275 lines) | stat: -rw-r--r-- 9,163 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/feed/core/v2/stream_model/feature_tree.h"

#include <algorithm>
#include <sstream>

#include "base/check.h"

namespace feed {
namespace stream_model {
namespace {
std::string ToAsciiForTesting(const std::string& s) {
  std::string result = s;
  for (size_t i = 0; i < result.size(); ++i) {
    if (result[i] < 32 || result[i] > 126) {
      result[i] = '?';
    }
  }
  return result;
}
}  // namespace

ContentMap::ContentMap(ContentRevision::Generator* revision_generator)
    : revision_generator_(revision_generator) {}

ContentMap::~ContentMap() = default;

ContentTag ContentMap::GetContentTag(const feedwire::ContentId& id) {
  auto iter = mapping_.find(id);
  if (iter != mapping_.end())
    return iter->second;
  ContentTag tag = tag_generator_.GenerateNextId();
  mapping_[id] = tag;
  return tag;
}

const feedstore::Content* ContentMap::FindContent(
    ContentRevision content_revision) {
  const size_t index = content_revision.GetUnsafeValue();
  if (revision_to_content_.size() <= index) {
    return nullptr;
  }
  return revision_to_content_[index];
}

ContentRevision ContentMap::LookupContentRevision(
    const feedstore::Content& content) {
  auto iter = content_.find(content);
  return (iter != content_.end()) ? iter->second : ContentRevision();
}

ContentRevision ContentMap::AddContent(feedstore::Content content) {
  auto result = content_.emplace(std::move(content), ContentRevision());
  // Already exists
  if (!result.second)
    return result.first->second;

  // Newly inserted.
  const ContentRevision new_revision = revision_generator_->GenerateNextId();
  result.first->second = new_revision;
  if (revision_to_content_.size() <= new_revision.GetUnsafeValue()) {
    revision_to_content_.resize(new_revision.GetUnsafeValue() + 1);
  }
  revision_to_content_[new_revision.GetUnsafeValue()] = &result.first->first;
  return new_revision;
}

void ContentMap::Clear() {
  // We don't clear the ID generators, so no IDs are re-used.
  mapping_.clear();
  content_.clear();
  revision_to_content_.clear();
}

StreamNode::StreamNode() = default;
StreamNode::~StreamNode() = default;
StreamNode::StreamNode(const StreamNode&) = default;
StreamNode& StreamNode::operator=(const StreamNode&) = default;

FeatureTree::FeatureTree(ContentMap* content_map) : content_map_(content_map) {}

FeatureTree::FeatureTree(const FeatureTree* base)
    : base_(base),
      content_map_(base->content_map_),
      computed_root_(base->computed_root_),
      root_tag_(base->root_tag_),
      nodes_(base->nodes_) {}
FeatureTree::~FeatureTree() = default;

StreamNode* FeatureTree::GetOrMakeNode(ContentTag id) {
  ResizeNodesIfNeeded(id);
  return &nodes_[id.value()];
}

const StreamNode* FeatureTree::FindNode(ContentTag id) const {
  return const_cast<FeatureTree*>(this)->FindNode(id);
}

StreamNode* FeatureTree::FindNode(ContentTag id) {
  if (!id.is_null() && nodes_.size() > id.value())
    return &nodes_[id.value()];
  return nullptr;
}

const feedstore::Content* FeatureTree::FindContent(ContentRevision id) const {
  return content_map_->FindContent(id);
}

void FeatureTree::ApplyStreamStructure(
    const feedstore::StreamStructure& structure) {
  switch (structure.operation()) {
    case feedstore::StreamStructure::CLEAR_ALL:
      nodes_.clear();
      // Clearing content is not required for correctness, but we can do it to
      // free memory as long as there's no base feature tree that can reference
      // the content.
      if (!base_)
        content_map_->Clear();
      computed_root_ = false;
      break;
    case feedstore::StreamStructure::UPDATE_OR_APPEND: {
      const ContentTag child_id = GetContentTag(structure.content_id());
      const bool is_root = structure.is_root();
      ContentTag parent_id;
      if (structure.has_parent_id()) {
        parent_id = GetContentTag(structure.parent_id());
      }
      ResizeNodesIfNeeded(std::max(child_id, parent_id));
      StreamNode& child = nodes_[child_id.value()];
      StreamNode* parent = FindNode(parent_id);

      // If a node already has a parent, treat this as an update, not an append
      // operation.
      child.tombstoned = false;
      if (root_tag_ == child_id) {
        computed_root_ = false;
      }

      if (parent && !child.has_parent) {
        // The child doesn't yet have a parent, but it should. Link to the
        // parent now. If the child already has a parent, we will never change
        // the parent even if requested by UPDATE_OR_APPEND.
        child.has_parent = true;
        child.previous_sibling = parent->last_child;
        parent->last_child = child_id;
      } else if (is_root || ((!computed_root_ || root_tag_.is_null()) &&
                             !parent && structure.parent_id().id() == 0 &&
                             structure.parent_id().type() == 0)) {
        // For recently produced stream data, there should be a node with
        // 'is_root' set. However, for older cached stream data, we weren't
        // storing this information. In that case, we fallback to pick the first
        // node which doesn't have a parentID as root.
        // TODO(crbug.com/40755948): simplify this once we can depend on
        // receiving is_root.
        computed_root_ = true;
        root_tag_ = child_id;
      }
    } break;
    case feedstore::StreamStructure::REMOVE: {
      // Removal is just unlinking the node from the tree.
      // If it's added back again later, it retains its old children.
      ContentTag tag = GetContentTag(structure.content_id());
      if (root_tag_ == tag) {
        computed_root_ = false;
      }
      GetOrMakeNode(tag)->tombstoned = true;
    } break;
    default:
      break;
  }
}  // namespace stream_model

void FeatureTree::ResizeNodesIfNeeded(ContentTag id) {
  if (nodes_.size() <= id.value())
    nodes_.resize(id.value() + 1);
}

void FeatureTree::AddContent(feedstore::Content content) {
  const ContentTag tag = GetContentTag(content.content_id());
  const ContentRevision revision_id =
      content_map_->AddContent(std::move(content));
  GetOrMakeNode(tag)->content_revision = revision_id;
}

void FeatureTree::CopyAndAddContent(const feedstore::Content& content) {
  ContentRevision revision_id = content_map_->LookupContentRevision(content);
  if (revision_id.is_null()) {
    revision_id = content_map_->AddContent(content);
  }
  const ContentTag tag = GetContentTag(content.content_id());
  GetOrMakeNode(tag)->content_revision = revision_id;
}

void FeatureTree::ResolveRoot() {
  if (computed_root_) {
    DCHECK(!FindNode(root_tag_) || !FindNode(root_tag_)->tombstoned);
    DCHECK(!FindNode(root_tag_) || !FindNode(root_tag_)->has_parent);
    return;
  }
  root_tag_ = ContentTag();
  for (size_t i = 0; i < nodes_.size(); ++i) {
    const StreamNode& node = nodes_[i];
    if (!node.tombstoned && !node.has_parent) {
      root_tag_ = ContentTag(i);
    }
  }
  computed_root_ = true;
}

std::vector<ContentRevision> FeatureTree::GetVisibleContent() {
  ResolveRoot();
  std::vector<ContentRevision> result;
  std::vector<ContentTag> stack;

  // Node: Cycles are impossible here. The root node is guaranteed to
  // not be a child. All other nodes have exactly one parent.
  // It is possible for nodes to cycle, like A->B->A, but in this case there can
  // be no valid root because all nodes have a parent.
  stack.push_back(root_tag_);
  while (!stack.empty()) {
    const ContentTag tag = stack.back();
    stack.pop_back();
    const StreamNode* node = FindNode(tag);
    if (!node || node->tombstoned)
      continue;
    if (!node->last_child.is_null()) {
      for (ContentTag child_id = node->last_child; !child_id.is_null();
           child_id = nodes_[child_id.value()].previous_sibling) {
        stack.push_back(child_id);
      }
    }
    if (!node->content_revision.is_null()) {
      result.push_back(node->content_revision);
    }
  }
  return result;
}

std::string FeatureTree::DumpStateForTesting() {
  std::stringstream ss;
  ss << "FeatureTree{\n";
  ResolveRoot();
  std::vector<std::pair<int, ContentTag>> stack;

  stack.push_back({1, root_tag_});
  while (!stack.empty()) {
    const ContentTag tag = stack.back().second;
    const int depth = stack.back().first;
    stack.pop_back();
    const StreamNode* node = FindNode(tag);
    if (!node || node->tombstoned)
      continue;
    ss << std::string(depth, ' ') << "|-";
    ss << (node->has_parent ? "ROOT" : "node");
    if (!node->last_child.is_null()) {
      for (ContentTag child_id = node->last_child; !child_id.is_null();
           child_id = nodes_[child_id.value()].previous_sibling) {
        stack.push_back({depth + 1, child_id});
      }
    }
    if (!node->content_revision.is_null()) {
      const feedstore::Content* content = FindContent(node->content_revision);
      ss << " content.frame=" << ToAsciiForTesting(content->frame());
    }
    ss << '\n';
  }
  ss << "}FeatureTree\n";
  return ss.str();
}

}  // namespace stream_model
}  // namespace feed