Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Publisher list improvements (uplift to 1.9.x) #5314

Merged
merged 3 commits into from
Apr 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions components/brave_rewards/browser/static_values.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const std::vector<std::string> kOnlyAnonWalletCountries = {

const std::map<std::string, uint64_t> kUInt64Options = {
{ledger::kOptionPublisherListRefreshInterval,
7* base::Time::kHoursPerDay * base::Time::kSecondsPerHour}
7* base::Time::kHoursPerDay * base::Time::kSecondsPerHour}};
#else
const std::map<std::string, bool> kBoolOptions = {};

Expand All @@ -48,9 +48,8 @@ const std::vector<std::string> kOnlyAnonWalletCountries = {

const std::map<std::string, uint64_t> kUInt64Options = {
{ledger::kOptionPublisherListRefreshInterval,
3 * base::Time::kHoursPerDay * base::Time::kSecondsPerHour}
3 * base::Time::kHoursPerDay * base::Time::kSecondsPerHour}};
#endif
};

} // namespace brave_rewards

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

#include <map>
#include <utility>
#include <vector>

#include "base/strings/stringprintf.h"
#include "bat/ledger/internal/database/database_server_publisher_amounts.h"
Expand Down Expand Up @@ -173,30 +172,56 @@ bool DatabaseServerPublisherAmounts::MigrateToV15(
return true;
}

void DatabaseServerPublisherAmounts::InsertOrUpdate(
void DatabaseServerPublisherAmounts::InsertOrUpdateList(
ledger::DBTransaction* transaction,
const ledger::PublisherBanner& info) {
const std::vector<ledger::PublisherBanner>& list) {
DCHECK(transaction);

// It's ok if social links are empty
if (info.amounts.empty()) {
if (list.empty()) {
return;
}

for (const auto& amount : info.amounts) {
const std::string query = base::StringPrintf(
"INSERT OR REPLACE INTO %s "
"(publisher_key, amount) VALUES (?, ?)",
const std::string base_query = base::StringPrintf(
"INSERT OR REPLACE INTO %s VALUES ",
kTableName);

auto command = ledger::DBCommand::New();
command->type = ledger::DBCommand::Type::RUN;
command->command = query;
size_t i = 0;
std::string query;
for (const auto& info : list) {
// It's ok if amounts are empty
if (info.amounts.empty()) {
continue;
}

if (i == 0) {
query += base_query;
}

BindString(command.get(), 0, info.publisher_key);
BindDouble(command.get(), 1, amount);
transaction->commands.push_back(std::move(command));
for (const auto& amount : info.amounts) {
if (i == kBatchLimit) {
query += base_query;
i = 0;
}

query += base::StringPrintf(
R"(("%s",%g))",
info.publisher_key.c_str(),
amount);
query += (i == kBatchLimit - 1) ? ";" : ",";
i++;
}
}

if (query.empty()) {
return;
}

query.pop_back();

auto command = ledger::DBCommand::New();
command->type = ledger::DBCommand::Type::EXECUTE;
command->command = query;
transaction->commands.push_back(std::move(command));
}

void DatabaseServerPublisherAmounts::GetRecord(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#define BRAVELEDGER_DATABASE_DATABASE_SERVER_PUBLISHER_AMOUNTS_H_

#include <string>
#include <vector>

#include "bat/ledger/internal/database/database_table.h"

Expand All @@ -19,9 +20,9 @@ class DatabaseServerPublisherAmounts: public DatabaseTable {

bool Migrate(ledger::DBTransaction* transaction, const int target) override;

void InsertOrUpdate(
void InsertOrUpdateList(
ledger::DBTransaction* transaction,
const ledger::PublisherBanner& info);
const std::vector<ledger::PublisherBanner>& list);

void GetRecord(
const std::string& publisher_key,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,9 @@ void DatabaseServerPublisherBanner::InsertOrUpdateList(
"VALUES (?, ?, ?, ?, ?)",
kTableName);

ledger::DBCommandPtr command;
for (const auto& info : list) {
auto command = ledger::DBCommand::New();
command = ledger::DBCommand::New();
command->type = ledger::DBCommand::Type::RUN;
command->command = query;

Expand All @@ -220,11 +221,11 @@ void DatabaseServerPublisherBanner::InsertOrUpdateList(
BindString(command.get(), 4, info.logo);

transaction->commands.push_back(std::move(command));

links_->InsertOrUpdate(transaction.get(), info);
amounts_->InsertOrUpdate(transaction.get(), info);
}

links_->InsertOrUpdateList(transaction.get(), list);
amounts_->InsertOrUpdateList(transaction.get(), list);

auto transaction_callback = std::bind(&OnResultCallback,
_1,
callback);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,26 +129,42 @@ void DatabaseServerPublisherInfo::InsertOrUpdatePartialList(
return;
}

const std::string query = base::StringPrintf(
"INSERT OR REPLACE INTO %s "
"(publisher_key, status, excluded, address) "
"VALUES (?, ?, ?, ?)",
const std::string base_query = base::StringPrintf(
"INSERT OR REPLACE INTO %s VALUES ",
kTableName);

auto transaction = ledger::DBTransaction::New();
size_t i = 0;
std::string main_query = base_query;
for (const auto& info : list) {
auto command = ledger::DBCommand::New();
command->type = ledger::DBCommand::Type::RUN;
command->command = query;
if (i == kBatchLimit) {
main_query += base_query;
i = 0;
}

BindString(command.get(), 0, info.publisher_key);
BindInt(command.get(), 1, static_cast<int>(info.status));
BindBool(command.get(), 2, info.excluded);
BindString(command.get(), 3, info.address);
main_query += base::StringPrintf(
R"(("%s",%d,%d,"%s"))",
info.publisher_key.c_str(),
static_cast<int>(info.status),
info.excluded,
info.address.c_str());
main_query += (i == kBatchLimit - 1) ? ";" : ",";
i++;
}

transaction->commands.push_back(std::move(command));
if (main_query.empty()) {
callback(ledger::Result::LEDGER_ERROR);
return;
}

main_query.pop_back();

auto transaction = ledger::DBTransaction::New();
auto command = ledger::DBCommand::New();
command->type = ledger::DBCommand::Type::EXECUTE;
command->command = main_query;

transaction->commands.push_back(std::move(command));

auto transaction_callback = std::bind(&OnResultCallback,
_1,
callback);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,36 +175,62 @@ bool DatabaseServerPublisherLinks::MigrateToV15(
return true;
}

void DatabaseServerPublisherLinks::InsertOrUpdate(
void DatabaseServerPublisherLinks::InsertOrUpdateList(
ledger::DBTransaction* transaction,
const ledger::PublisherBanner& info) {
const std::vector<ledger::PublisherBanner>& list) {
DCHECK(transaction);

// It's ok if links are empty
if (info.links.empty()) {
if (list.empty()) {
return;
}

for (const auto& link : info.links) {
if (link.second.empty()) {
const std::string base_query = base::StringPrintf(
"INSERT OR REPLACE INTO %s VALUES ",
kTableName);

size_t i = 0;
std::string query;
for (const auto& info : list) {
// It's ok if links are empty
if (info.links.empty()) {
continue;
}

const std::string query = base::StringPrintf(
"INSERT OR REPLACE INTO %s "
"(publisher_key, provider, link) "
"VALUES (?, ?, ?)",
kTableName);

auto command = ledger::DBCommand::New();
command->type = ledger::DBCommand::Type::RUN;
command->command = query;
for (const auto& link : info.links) {
if (link.second.empty()) {
continue;
}

if (i == 0) {
query += base_query;
}

BindString(command.get(), 0, info.publisher_key);
BindString(command.get(), 1, link.first);
BindString(command.get(), 2, link.second);
transaction->commands.push_back(std::move(command));
if (i == kBatchLimit) {
query += base_query;
i = 0;
}

query += base::StringPrintf(
R"(("%s","%s","%s"))",
info.publisher_key.c_str(),
link.first.c_str(),
link.second.c_str());
query += (i == kBatchLimit - 1) ? ";" : ",";
i++;
}
}

if (query.empty()) {
return;
}

query.pop_back();

auto command = ledger::DBCommand::New();
command->type = ledger::DBCommand::Type::EXECUTE;
command->command = query;
transaction->commands.push_back(std::move(command));
}

void DatabaseServerPublisherLinks::GetRecord(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <map>
#include <string>
#include <vector>

#include "bat/ledger/internal/database/database_table.h"

Expand All @@ -20,9 +21,9 @@ class DatabaseServerPublisherLinks: public DatabaseTable {

bool Migrate(ledger::DBTransaction* transaction, const int target) override;

void InsertOrUpdate(
void InsertOrUpdateList(
ledger::DBTransaction* transaction,
const ledger::PublisherBanner& info);
const std::vector<ledger::PublisherBanner>& list);

void GetRecord(
const std::string& publisher_key,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

namespace braveledger_database {

const size_t kBatchLimit = 999;

bool DropTable(
ledger::DBTransaction* transaction,
const std::string& table_name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ void LedgerImpl::OnWalletInitializedInternal(
if (result == ledger::Result::LEDGER_OK ||
result == ledger::Result::WALLET_CREATED) {
initialized_ = true;
bat_publisher_->SetPublisherServerListTimer();
bat_publisher_->SetPublisherServerListTimer(GetRewardsMainEnabled());
bat_contribution_->SetReconcileTimer();
bat_promotion_->Refresh(false);
bat_contribution_->Initialize();
Expand Down Expand Up @@ -602,6 +602,7 @@ void LedgerImpl::GetExcludedList(ledger::PublisherInfoListCallback callback) {

void LedgerImpl::SetRewardsMainEnabled(bool enabled) {
bat_state_->SetRewardsMainEnabled(enabled);
bat_publisher_->SetPublisherServerListTimer(enabled);
}

void LedgerImpl::SetPublisherMinVisitTime(uint64_t duration) { // In seconds
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,12 @@ void Publisher::OnRefreshPublisherServerPublisher(
callback(status);
}

void Publisher::SetPublisherServerListTimer() {
void Publisher::SetPublisherServerListTimer(const bool rewards_enabled) {
if (!rewards_enabled) {
server_list_->ClearTimer();
return;
}

server_list_->SetTimer(false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Publisher {
const std::string& publisher_key,
ledger::OnRefreshPublisherCallback callback);

void SetPublisherServerListTimer();
void SetPublisherServerListTimer(const bool rewards_enabled);

bool loadState(const std::string& data);

Expand Down
Loading