Welcome to mirror list, hosted at ThFree Co, Russian Federation.

diff_manager.cpp « diff_scheme « storage - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ce5fe7995aaafcab694569afd56b9cd963b17126 (plain)
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
#include "storage/diff_scheme/diff_manager.hpp"
#include "storage/diff_scheme/diff_scheme_checker.hpp"

#include "generator/mwm_diff/diff.hpp"

#include "platform/platform.hpp"

#include "coding/internal/file_data.hpp"

#include "base/assert.hpp"

namespace storage
{
namespace diffs
{
void Manager::Load(LocalMapsInfo && info)
{
  LocalMapsInfo localMapsInfo = info;
  {
    std::lock_guard<std::mutex> lock(m_mutex);
    m_localMapsInfo = std::move(info);
  }

  m_workerThread.Push([this, localMapsInfo]
  {
    NameFileInfoMap const diffs = Checker::Check(localMapsInfo);

    std::lock_guard<std::mutex> lock(m_mutex);

    m_diffs = diffs;
    if (diffs.empty())
    {
      m_status = Status::NotAvailable;

      GetPlatform().GetMarketingService().SendMarketingEvent(marketing::kDiffSchemeFallback, {});
    }
    else
    {
      m_status = Status::Available;
    }

    auto & observers = m_observers;
    auto status = m_status;
    GetPlatform().RunOnGuiThread([observers, status]() mutable {
      observers.ForEach(&Observer::OnDiffStatusReceived, status);
    });
  });
}

void Manager::ApplyDiff(ApplyDiffParams && p, std::function<void(bool const result)> const & task)
{
  m_workerThread.Push([this, p, task]
  {
    CHECK(p.m_diffFile, ("No diff at path:", p.m_diffFile->GetPath(MapOptions::Diff)));
    CHECK(p.m_oldMwmFile, ("No old mwm at path:", p.m_oldMwmFile->GetPath(MapOptions::Map)));

    auto & diffReadyPath = p.m_diffReadyPath;
    auto & diffFile = p.m_diffFile;
    auto const diffPath = diffFile->GetPath(MapOptions::Diff);
    bool result = false;

    diffFile->SyncWithDisk();

    auto const isOnDisk = diffFile->OnDisk(MapOptions::Diff);
    auto const isFilePrepared = isOnDisk || my::RenameFileX(diffReadyPath, diffPath);

    if (isFilePrepared)
    {
      // Sync with disk after renaming.
      if (!isOnDisk)
        diffFile->SyncWithDisk();

      string const oldMwmPath = p.m_oldMwmFile->GetPath(MapOptions::Map);
      string const newMwmPath = diffFile->GetPath(MapOptions::Map);
      string const diffApplyingInProgressPath = newMwmPath + DIFF_APPLYING_FILE_EXTENSION;
      result = generator::mwm_diff::ApplyDiff(oldMwmPath, diffApplyingInProgressPath, diffPath) &&
              my::RenameFileX(diffApplyingInProgressPath, newMwmPath);
    }

    diffFile->DeleteFromDisk(MapOptions::Diff);

    if (result)
    {
      std::lock_guard<std::mutex> lock(m_mutex);
      m_diffs.erase(diffFile->GetCountryName());
      if (m_diffs.empty())
        m_status = Status::NotAvailable;
    }
    else
    {
      std::lock_guard<std::mutex> lock(m_mutex);
      m_status = Status::NotAvailable;

      GetPlatform().GetMarketingService().SendMarketingEvent(
          marketing::kDiffSchemeError,
          {{"type", "patching"},
           {"error", isFilePrepared ? "Cannot apply diff" : "Cannot prepare file"}});
    }

    task(result);
  });
}

Status Manager::GetStatus() const
{
  std::lock_guard<std::mutex> lock(m_mutex);
  return m_status;
}

void Manager::SetStatus(Status status)
{
  std::lock_guard<std::mutex> lock(m_mutex);
  m_status = status;
}

FileInfo const & Manager::InfoFor(storage::TCountryId const & countryId) const
{
  std::lock_guard<std::mutex> lock(m_mutex);
  ASSERT(HasDiffForUnsafe(countryId), ());
  return m_diffs.at(countryId);
}

bool Manager::HasDiffFor(storage::TCountryId const & countryId) const
{
  std::lock_guard<std::mutex> lock(m_mutex);
  return HasDiffForUnsafe(countryId);
}

bool Manager::HasDiffForUnsafe(storage::TCountryId const & countryId) const
{
  if (m_status != diffs::Status::Available)
    return false;
  return m_diffs.find(countryId) != m_diffs.end();
}

bool Manager::IsPossibleToAutoupdate() const
{
  std::lock_guard<std::mutex> lock(m_mutex);

  if (m_status != Status::Available)
    return false;

  for (auto const & nameVersion : m_localMapsInfo.m_localMaps)
  {
    if (m_diffs.find(nameVersion.first) == m_diffs.end())
      return false;
  }
  return true;
}
  
std::string DebugPrint(Status status)
{
  switch (status)
  {
  case Status::Undefined:
    return "Undefined";
  case Status::Available:
    return "Available";
  case Status::NotAvailable:
    return "NotAvailable";
  }
}
}  // namespace diffs
}  // namespace storage