File: file_index.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; 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,806; 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 (189 lines) | stat: -rw-r--r-- 6,221 bytes parent folder | download | duplicates (2)
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
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/ash/components/file_manager/indexing/file_index.h"

#include "base/time/time.h"

namespace ash::file_manager {

FileIndex::FileIndex(std::unique_ptr<IndexStorage> storage)
    : storage_(std::move(storage)) {}
FileIndex::~FileIndex() = default;

OpResults FileIndex::Init() {
  return storage_->Init() ? OpResults::kSuccess : OpResults::kUninitialized;
}

OpResults FileIndex::PutFileInfo(const FileInfo& file_info) {
  return storage_->PutFileInfo(file_info) == -1 ? OpResults::kGenericError
                                                : OpResults::kSuccess;
}

OpResults FileIndex::SetTerms(const std::vector<Term>& terms, const GURL& url) {
  if (terms.empty()) {
    return OpResults::kArgumentError;
  }
  // Arrange terms by field and remove duplicates and convert to internal IDs.
  int64_t url_id = storage_->GetUrlId(url);
  if (url_id == -1) {
    return OpResults::kFileMissing;
  }
  std::set<int64_t> term_id_set = ConvertToTermIds(terms);

  // If the given url_id already had some terms associated with it, remove terms
  // not specified in terms vector. Say, if url_id had terms {t1, t3, t8}
  // associated with it, and terms was {t1, t2}, we would compute {t3, t8} as
  // the difference between two collections and remove those.
  std::set<int64_t> url_term_ids = storage_->GetTermIdsForUrl(url_id);
  if (!url_term_ids.empty()) {
    std::set<int64_t> to_remove_terms;
    std::set_difference(
        url_term_ids.begin(), url_term_ids.end(), term_id_set.begin(),
        term_id_set.end(),
        std::inserter(to_remove_terms, to_remove_terms.begin()));
    storage_->DeleteTermIdsForUrl(to_remove_terms, url_id);
  }
  storage_->AddTermIdsForUrl(term_id_set, url_id);
  return OpResults::kSuccess;
}

OpResults FileIndex::MoveFile(const GURL& old_url, const GURL& new_url) {
  DCHECK(old_url.is_valid());
  DCHECK(new_url.is_valid());
  // Check for no-op.
  if (old_url == new_url) {
    return OpResults::kSuccess;
  }
  // Phase 1: Run some diagnostics; not strictly necessary but it gives more
  // accurate error reporting.
  int64_t old_url_id = storage_->GetUrlId(old_url);
  if (old_url_id < 0) {
    return OpResults::kFileMissing;
  }
  int64_t new_url_id = storage_->GetUrlId(new_url);
  if (new_url_id != -1) {
    return OpResults::kFileExists;
  }
  std::optional<FileInfo> file_info = storage_->GetFileInfo(old_url_id);
  if (!file_info.has_value()) {
    return OpResults::kFileMissing;
  }

  // Phase 2: Just make the move by updating URL.
  return storage_->MoveUrl(old_url, new_url) == -1 ? OpResults::kGenericError
                                                   : OpResults::kSuccess;
}

OpResults FileIndex::RemoveFile(const GURL& url) {
  int64_t url_id = storage_->GetUrlId(url);
  if (url_id < 0) {
    return OpResults::kSuccess;
  }
  const std::set<int64_t>& url_term_ids = storage_->GetTermIdsForUrl(url_id);
  for (int64_t term_id : url_term_ids) {
    storage_->DeleteFromPostingList(term_id, url_id);
  }
  storage_->DeleteFileInfo(url_id);
  storage_->DeleteUrl(url);
  return OpResults::kSuccess;
}

OpResults FileIndex::RemoveTerms(const std::vector<Term>& terms,
                                 const GURL& url) {
  int64_t url_id = storage_->GetUrlId(url);
  if (url_id < 0) {
    return OpResults::kSuccess;
  }
  std::set<int64_t> term_ids;
  for (const Term& t : terms) {
    int64_t id_with_field = storage_->GetTermId(t);
    if (id_with_field != -1) {
      term_ids.emplace(id_with_field);
    }
    int64_t global_id = storage_->GetTermId(Term("", t.token()));
    if (global_id != -1) {
      term_ids.emplace(global_id);
    }
  }
  for (int64_t term_id : term_ids) {
    storage_->DeleteFromPostingList(term_id, url_id);
  }
  return OpResults::kSuccess;
}

OpResults FileIndex::AddTerms(const std::vector<Term>& terms, const GURL& url) {
  if (terms.empty()) {
    return OpResults::kSuccess;
  }

  int64_t url_id = storage_->GetUrlId(url);
  if (url_id == -1) {
    return OpResults::kFileMissing;
  }

  std::set<int64_t> term_id_set = ConvertToTermIds(terms);
  storage_->AddTermIdsForUrl(term_id_set, url_id);
  return OpResults::kSuccess;
}

// Searches the index for file info matching the specified query.
SearchResults FileIndex::Search(const Query& query) {
  const std::vector<Term>& terms = query.terms();
  SearchResults results;
  if (terms.empty()) {
    // Technically, an empty query matches every file, but we treat this
    // as empty match.
    return results;
  }
  std::set<int64_t> matched_url_ids;
  bool first = true;
  for (const Term& term : terms) {
    int64_t term_id = storage_->GetTermId(term);
    if (term_id == -1) {
      return results;
    }
    const std::set<int64_t> url_ids = storage_->GetUrlIdsForTermId(term_id);
    if (url_ids.empty()) {
      return results;
    }
    if (first) {
      matched_url_ids = url_ids;
      first = false;
    } else {
      std::set<int64_t> intersection;
      std::set_intersection(matched_url_ids.begin(), matched_url_ids.end(),
                            url_ids.begin(), url_ids.end(),
                            std::inserter(intersection, intersection.begin()));
      matched_url_ids = intersection;
    }
    if (matched_url_ids.empty()) {
      break;
    }
  }
  if (matched_url_ids.empty()) {
    return results;
  }
  for (const int64_t url_id : matched_url_ids) {
    std::optional<FileInfo> file_info = storage_->GetFileInfo(url_id);
    DCHECK(file_info.has_value());
    // TODO(b:327535200): Add true score.
    results.matches.emplace_back(Match(1, file_info.value()));
  }
  // TODO(b:327535200): Correctly compute total_matches.
  results.total_matches = results.matches.size();
  return results;
}

std::set<int64_t> FileIndex::ConvertToTermIds(const std::vector<Term>& terms) {
  std::set<int64_t> term_ids;
  for (const Term& term : terms) {
    DCHECK(!term.field().empty());
    term_ids.emplace(storage_->GetOrCreateTermId(term));
    term_ids.emplace(storage_->GetOrCreateTermId(Term("", term.token())));
  }
  return term_ids;
}

}  // namespace ash::file_manager