Skip to content

Commit

Permalink
[Feed] Create JournalOperation and JournalMutation
Browse files Browse the repository at this point in the history
Create JournalOperation and JournalMutation on C++ side.

Bug: 875143
Change-Id: Ia473ef0f3f528ffe46372f2c8963ebff8ad5bf65
Reviewed-on: https://chromium-review.googlesource.com/1179380
Commit-Queue: Gang Wu <gangwu@chromium.org>
Reviewed-by: Sky Malice <skym@chromium.org>
Cr-Commit-Position: refs/heads/master@{#584600}
  • Loading branch information
Gang Wu authored and Commit Bot committed Aug 21, 2018
1 parent 0a26fad commit 58ef920
Show file tree
Hide file tree
Showing 13 changed files with 440 additions and 76 deletions.
6 changes: 6 additions & 0 deletions components/feed/core/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ source_set("feed_core") {
"feed_image_database.h",
"feed_image_manager.cc",
"feed_image_manager.h",
"feed_journal_mutation.cc",
"feed_journal_mutation.h",
"feed_journal_operation.cc",
"feed_journal_operation.h",
"feed_networking_host.cc",
"feed_networking_host.h",
"feed_scheduler_host.cc",
Expand Down Expand Up @@ -73,8 +77,10 @@ source_set("core_unit_tests") {
testonly = true
sources = [
"feed_content_database_unittest.cc",
"feed_content_mutation_unittest.cc",
"feed_image_database_unittest.cc",
"feed_image_manager_unittest.cc",
"feed_journal_mutation_unittest.cc",
"feed_networking_host_unittest.cc",
"feed_scheduler_host_unittest.cc",
"feed_storage_database_unittest.cc",
Expand Down
62 changes: 32 additions & 30 deletions components/feed/core/feed_content_database.cc
Original file line number Diff line number Diff line change
Expand Up @@ -117,36 +117,35 @@ void FeedContentDatabase::CommitContentMutation(
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(content_mutation);

PerformNextOperation(content_mutation->TakeOperations(), std::move(callback));
PerformNextOperation(std::move(content_mutation), std::move(callback));
}

void FeedContentDatabase::PerformNextOperation(
ContentOperationList operations_list,
std::unique_ptr<ContentMutation> content_mutation,
ConfirmationCallback callback) {
DCHECK(!operations_list.empty());
DCHECK(!content_mutation->Empty());

ContentOperation operation(operations_list.front());
operations_list.pop_front();
ContentOperation operation = content_mutation->TakeFristOperation();

switch (operation.type()) {
case ContentOperation::CONTENT_DELETE:
// TODO(gangwu): If deletes are continuous, we should combine them into
// one commit.
DeleteContent(std::move(operation), std::move(operations_list),
DeleteContent(std::move(operation), std::move(content_mutation),
std::move(callback));
break;
case ContentOperation::CONTENT_DELETE_BY_PREFIX:
DeleteContentByPrefix(std::move(operation), std::move(operations_list),
DeleteContentByPrefix(std::move(operation), std::move(content_mutation),
std::move(callback));
break;
case ContentOperation::CONTENT_UPSERT:
// TODO(gangwu): If upserts are continuous, we should combine them into
// one commit.
UpsertContent(std::move(operation), std::move(operations_list),
UpsertContent(std::move(operation), std::move(content_mutation),
std::move(callback));
break;
case ContentOperation::CONTENT_DELETE_ALL:
DeleteAllContent(std::move(operation), std::move(operations_list),
DeleteAllContent(std::move(operation), std::move(content_mutation),
std::move(callback));
break;
default:
Expand All @@ -156,9 +155,10 @@ void FeedContentDatabase::PerformNextOperation(
}
}

void FeedContentDatabase::UpsertContent(ContentOperation operation,
ContentOperationList operations_list,
ConfirmationCallback callback) {
void FeedContentDatabase::UpsertContent(
ContentOperation operation,
std::unique_ptr<ContentMutation> content_mutation,
ConfirmationCallback callback) {
DCHECK_EQ(operation.type(), ContentOperation::CONTENT_UPSERT);

auto contents_to_save = std::make_unique<StorageEntryVector>();
Expand All @@ -170,13 +170,14 @@ void FeedContentDatabase::UpsertContent(ContentOperation operation,
storage_database_->UpdateEntries(
std::move(contents_to_save), std::make_unique<std::vector<std::string>>(),
base::BindOnce(&FeedContentDatabase::OnOperationCommitted,
weak_ptr_factory_.GetWeakPtr(), std::move(operations_list),
std::move(callback)));
weak_ptr_factory_.GetWeakPtr(),
std::move(content_mutation), std::move(callback)));
}

void FeedContentDatabase::DeleteContent(ContentOperation operation,
ContentOperationList operations_list,
ConfirmationCallback callback) {
void FeedContentDatabase::DeleteContent(
ContentOperation operation,
std::unique_ptr<ContentMutation> content_mutation,
ConfirmationCallback callback) {
DCHECK_EQ(operation.type(), ContentOperation::CONTENT_DELETE);

auto content_to_delete = std::make_unique<std::vector<std::string>>(
Expand All @@ -185,36 +186,37 @@ void FeedContentDatabase::DeleteContent(ContentOperation operation,
storage_database_->UpdateEntries(
std::make_unique<StorageEntryVector>(), std::move(content_to_delete),
base::BindOnce(&FeedContentDatabase::OnOperationCommitted,
weak_ptr_factory_.GetWeakPtr(), std::move(operations_list),
std::move(callback)));
weak_ptr_factory_.GetWeakPtr(),
std::move(content_mutation), std::move(callback)));
}

void FeedContentDatabase::DeleteContentByPrefix(
ContentOperation operation,
ContentOperationList operations_list,
std::unique_ptr<ContentMutation> content_mutation,
ConfirmationCallback callback) {
DCHECK_EQ(operation.type(), ContentOperation::CONTENT_DELETE_BY_PREFIX);

storage_database_->UpdateEntriesWithRemoveFilter(
std::make_unique<StorageEntryVector>(),
base::BindRepeating(&DatabasePrefixFilter, operation.prefix()),
base::BindOnce(&FeedContentDatabase::OnOperationCommitted,
weak_ptr_factory_.GetWeakPtr(), std::move(operations_list),
std::move(callback)));
weak_ptr_factory_.GetWeakPtr(),
std::move(content_mutation), std::move(callback)));
}

void FeedContentDatabase::DeleteAllContent(ContentOperation operation,
ContentOperationList operations_list,
ConfirmationCallback callback) {
void FeedContentDatabase::DeleteAllContent(
ContentOperation operation,
std::unique_ptr<ContentMutation> content_mutation,
ConfirmationCallback callback) {
DCHECK_EQ(operation.type(), ContentOperation::CONTENT_DELETE_ALL);

std::string key_prefix = "";
storage_database_->UpdateEntriesWithRemoveFilter(
std::make_unique<StorageEntryVector>(),
base::BindRepeating(&DatabasePrefixFilter, std::move(key_prefix)),
base::BindOnce(&FeedContentDatabase::OnOperationCommitted,
weak_ptr_factory_.GetWeakPtr(), std::move(operations_list),
std::move(callback)));
weak_ptr_factory_.GetWeakPtr(),
std::move(content_mutation), std::move(callback)));
}

void FeedContentDatabase::OnDatabaseInitialized(bool success) {
Expand Down Expand Up @@ -268,7 +270,7 @@ void FeedContentDatabase::OnLoadKeysForLoadAllContentKeys(
}

void FeedContentDatabase::OnOperationCommitted(
ContentOperationList operations_list,
std::unique_ptr<ContentMutation> content_mutation,
ConfirmationCallback callback,
bool success) {
// Commit is unsuccessful, skip processing the other operations since
Expand All @@ -281,12 +283,12 @@ void FeedContentDatabase::OnOperationCommitted(
}

// All operations were committed successfully, call |callback|.
if (operations_list.empty()) {
if (content_mutation->Empty()) {
std::move(callback).Run(success);
return;
}

PerformNextOperation(std::move(operations_list), std::move(callback));
PerformNextOperation(std::move(content_mutation), std::move(callback));
}

} // namespace feed
12 changes: 6 additions & 6 deletions components/feed/core/feed_content_database.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,19 @@ class FeedContentDatabase {
// These methods work with |CommitContentMutation|. They process
// |ContentOperation| in |ContentMutation| which is passed to
// |PerformNextOperation| by |CommitContentMutation|.
void PerformNextOperation(ContentOperationList operations_list,
void PerformNextOperation(std::unique_ptr<ContentMutation> content_mutation,
ConfirmationCallback callback);
void UpsertContent(ContentOperation operation,
ContentOperationList operations_list,
std::unique_ptr<ContentMutation> content_mutation,
ConfirmationCallback callback);
void DeleteContent(ContentOperation operation,
ContentOperationList operations_list,
std::unique_ptr<ContentMutation> content_mutation,
ConfirmationCallback callback);
void DeleteContentByPrefix(ContentOperation operation,
ContentOperationList operations_list,
std::unique_ptr<ContentMutation> content_mutation,
ConfirmationCallback callback);
void DeleteAllContent(ContentOperation operation,
ContentOperationList operations_list,
std::unique_ptr<ContentMutation> content_mutation,
ConfirmationCallback callback);

// Callback methods given to |storage_database_| for async responses.
Expand All @@ -110,7 +110,7 @@ class FeedContentDatabase {
ContentKeyCallback callback,
bool success,
std::unique_ptr<std::vector<std::string>> keys);
void OnOperationCommitted(ContentOperationList operations_list,
void OnOperationCommitted(std::unique_ptr<ContentMutation> content_mutation,
ConfirmationCallback callback,
bool success);

Expand Down
32 changes: 20 additions & 12 deletions components/feed/core/feed_content_mutation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,43 @@

#include <utility>

#include "base/logging.h"
#include "components/feed/core/feed_content_operation.h"

namespace feed {

ContentMutation::ContentMutation() {}
ContentMutation::ContentMutation() = default;

ContentMutation::~ContentMutation() {}
ContentMutation::~ContentMutation() = default;

void ContentMutation::AppendDeleteOperation(const std::string& key) {
operations_list_.emplace_back(ContentOperation::CreateDeleteOperation(key));
void ContentMutation::AppendDeleteOperation(std::string key) {
operations_list_.emplace_back(
ContentOperation::CreateDeleteOperation(std::move(key)));
}

void ContentMutation::AppendDeleteAllOperation() {
operations_list_.emplace_back(ContentOperation::CreateDeleteAllOperation());
}

void ContentMutation::AppendDeleteByPrefixOperation(const std::string& prefix) {
void ContentMutation::AppendDeleteByPrefixOperation(std::string prefix) {
operations_list_.emplace_back(
ContentOperation::CreateDeleteByPrefixOperation(prefix));
ContentOperation::CreateDeleteByPrefixOperation(std::move(prefix)));
}

void ContentMutation::AppendUpsertOperation(const std::string& key,
const std::string& value) {
operations_list_.emplace_back(
ContentOperation::CreateUpsertOperation(key, value));
void ContentMutation::AppendUpsertOperation(std::string key,
std::string value) {
operations_list_.emplace_back(ContentOperation::CreateUpsertOperation(
std::move(key), std::move(value)));
}

bool ContentMutation::Empty() {
return operations_list_.empty();
}

ContentOperationList ContentMutation::TakeOperations() {
return std::move(operations_list_);
ContentOperation ContentMutation::TakeFristOperation() {
ContentOperation operation = std::move(operations_list_.front());
operations_list_.pop_front();
return operation;
}

} // namespace feed
17 changes: 10 additions & 7 deletions components/feed/core/feed_content_mutation.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
namespace feed {

class ContentOperation;
using ContentOperationList = std::list<ContentOperation>;

// Native counterpart of ContentMutation.java.
// To commit a set of ContentOperation into FeedContentDatabase, user need to
Expand All @@ -25,16 +24,20 @@ class ContentMutation {
ContentMutation();
~ContentMutation();

void AppendDeleteOperation(const std::string& key);
void AppendDeleteOperation(std::string key);
void AppendDeleteAllOperation();
void AppendDeleteByPrefixOperation(const std::string& prefix);
void AppendUpsertOperation(const std::string& key, const std::string& value);
void AppendDeleteByPrefixOperation(std::string prefix);
void AppendUpsertOperation(std::string key, std::string value);

// This will std::move the |operations_list_| out.
ContentOperationList TakeOperations();
// Check if mutation has ContentOperation left.
bool Empty();

// This will remove the first ContentOperation in |operations_list_| and
// return it to caller.
ContentOperation TakeFristOperation();

private:
ContentOperationList operations_list_;
std::list<ContentOperation> operations_list_;

DISALLOW_COPY_AND_ASSIGN(ContentMutation);
};
Expand Down
80 changes: 80 additions & 0 deletions components/feed/core/feed_content_mutation_unittest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/feed/core/feed_content_mutation.h"

#include "components/feed/core/feed_content_operation.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace feed {

namespace {

const char kContentkey[] = "ContentKey";
const char kContentValue[] = "Value";
const char kContentPrefix[] = "Content";

} // namespace

class FeedContentMutationTest : public testing::Test {
public:
FeedContentMutationTest() = default;

private:
DISALLOW_COPY_AND_ASSIGN(FeedContentMutationTest);
};

TEST_F(FeedContentMutationTest, AppendDeleteOperation) {
ContentMutation mutation;
EXPECT_TRUE(mutation.Empty());

mutation.AppendDeleteOperation(kContentkey);
EXPECT_FALSE(mutation.Empty());

ContentOperation operation = mutation.TakeFristOperation();
EXPECT_TRUE(mutation.Empty());
EXPECT_EQ(operation.type(), ContentOperation::CONTENT_DELETE);
EXPECT_EQ(operation.key(), kContentkey);
}

TEST_F(FeedContentMutationTest, AppendDeleteAllOperation) {
ContentMutation mutation;
EXPECT_TRUE(mutation.Empty());

mutation.AppendDeleteAllOperation();
EXPECT_FALSE(mutation.Empty());

ContentOperation operation = mutation.TakeFristOperation();
EXPECT_TRUE(mutation.Empty());
EXPECT_EQ(operation.type(), ContentOperation::CONTENT_DELETE_ALL);
}

TEST_F(FeedContentMutationTest, AppendDeleteByPrefixOperation) {
ContentMutation mutation;
EXPECT_TRUE(mutation.Empty());

mutation.AppendDeleteByPrefixOperation(kContentPrefix);
EXPECT_FALSE(mutation.Empty());

ContentOperation operation = mutation.TakeFristOperation();
EXPECT_TRUE(mutation.Empty());
EXPECT_EQ(operation.type(), ContentOperation::CONTENT_DELETE_BY_PREFIX);
EXPECT_EQ(operation.prefix(), kContentPrefix);
}

TEST_F(FeedContentMutationTest, AppendUpsertOperation) {
ContentMutation mutation;
EXPECT_TRUE(mutation.Empty());

mutation.AppendUpsertOperation(kContentkey, kContentValue);
EXPECT_FALSE(mutation.Empty());

ContentOperation operation = mutation.TakeFristOperation();
EXPECT_TRUE(mutation.Empty());
EXPECT_EQ(operation.type(), ContentOperation::CONTENT_UPSERT);
EXPECT_EQ(operation.key(), kContentkey);
EXPECT_EQ(operation.value(), kContentValue);
}

} // namespace feed
Loading

0 comments on commit 58ef920

Please sign in to comment.