Skip to content

Commit

Permalink
Bug 1168606 - Add support for preloading key-only cursors. r=ttung,asuth
Browse files Browse the repository at this point in the history
Differential Revision: https://phabricator.services.mozilla.com/D44009

--HG--
extra : moz-landing-system : lando
  • Loading branch information
sigiesec committed Nov 5, 2019
1 parent 59cd698 commit b09e787
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 68 deletions.
104 changes: 57 additions & 47 deletions dom/indexedDB/ActorsChild.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3227,6 +3227,16 @@ BackgroundCursorChild::CachedResponse::CachedResponse(
mObjectStoreKey{std::move(aObjectStoreKey)},
mCloneInfo{std::move(aCloneInfo)} {}

BackgroundCursorChild::CachedResponse::CachedResponse(Key aKey)
: mKey{std::move(aKey)} {}

BackgroundCursorChild::CachedResponse::CachedResponse(Key aKey,
Key aLocaleAwareKey,
Key aObjectStoreKey)
: mKey{std::move(aKey)},
mLocaleAwareKey{std::move(aLocaleAwareKey)},
mObjectStoreKey{std::move(aObjectStoreKey)} {}

// Does not need to be threadsafe since this only runs on one thread, but
// inheriting from CancelableRunnable is easy.
class BackgroundCursorChild::DelayedActionRunnable final
Expand Down Expand Up @@ -3433,9 +3443,6 @@ void BackgroundCursorChild::CompleteContinueRequestFromCache() {
MOZ_ASSERT(mCursor);
MOZ_ASSERT(mStrongCursor);
MOZ_ASSERT(!mDelayedResponses.empty());
// TODO: Also support the other types.
MOZ_ASSERT(mCursor->GetType() == IDBCursor::Type_ObjectStore ||
mCursor->GetType() == IDBCursor::Type_Index);

RefPtr<IDBCursor> cursor;
mStrongCursor.swap(cursor);
Expand All @@ -3450,8 +3457,14 @@ void BackgroundCursorChild::CompleteContinueRequestFromCache() {
std::move(item.mObjectStoreKey),
std::move(item.mCloneInfo));
break;
case IDBCursor::Type_ObjectStoreKey:
mCursor->Reset(std::move(item.mKey));
break;
case IDBCursor::Type_IndexKey:
mCursor->Reset(std::move(item.mKey), std::move(item.mLocaleAwareKey),
std::move(item.mObjectStoreKey));
break;
default:
// TODO: Also support the other types.
MOZ_CRASH("Should never get here.");
}
mDelayedResponses.pop_front();
Expand Down Expand Up @@ -3629,28 +3642,25 @@ void BackgroundCursorChild::HandleResponse(
}

void BackgroundCursorChild::HandleResponse(
const ObjectStoreKeyCursorResponse& aResponse) {
const nsTArray<ObjectStoreKeyCursorResponse>& aResponses) {
AssertIsOnOwningThread();
MOZ_ASSERT(mRequest);
MOZ_ASSERT(mTransaction);
MOZ_ASSERT(mObjectStore);
MOZ_ASSERT(!mStrongRequest);
MOZ_ASSERT(!mStrongCursor);

// XXX Fix this somehow...
auto& response = const_cast<ObjectStoreKeyCursorResponse&>(aResponse);

RefPtr<IDBCursor> newCursor;

if (mCursor) {
mCursor->Reset(std::move(response.key()));
} else {
newCursor = IDBCursor::Create(this, std::move(response.key()));
mCursor = newCursor;
}
HandleMultipleCursorResponses(
aResponses, [this](ObjectStoreKeyCursorResponse& response) {
RefPtr<IDBCursor> newCursor;

ResultHelper helper(mRequest, mTransaction, mCursor);
DispatchSuccessEvent(&helper);
if (mCursor) {
if (mCursor->IsContinueCalled()) {
mCursor->Reset(std::move(response.key()));
} else {
mCachedResponses.emplace_back(std::move(response.key()));
}
} else {
newCursor = IDBCursor::Create(this, std::move(response.key()));
mCursor = newCursor;
}
});
}

void BackgroundCursorChild::HandleResponse(
Expand Down Expand Up @@ -3688,31 +3698,31 @@ void BackgroundCursorChild::HandleResponse(
}

void BackgroundCursorChild::HandleResponse(
const IndexKeyCursorResponse& aResponse) {
const nsTArray<IndexKeyCursorResponse>& aResponses) {
AssertIsOnOwningThread();
MOZ_ASSERT(mRequest);
MOZ_ASSERT(mTransaction);
MOZ_ASSERT(mIndex);
MOZ_ASSERT(!mStrongRequest);
MOZ_ASSERT(!mStrongCursor);

// XXX Fix this somehow...
auto& response = const_cast<IndexKeyCursorResponse&>(aResponse);

RefPtr<IDBCursor> newCursor;

if (mCursor) {
mCursor->Reset(std::move(response.key()), std::move(response.sortKey()),
std::move(response.objectKey()));
} else {
newCursor = IDBCursor::Create(this, std::move(response.key()),
std::move(response.sortKey()),
std::move(response.objectKey()));
mCursor = newCursor;
}
HandleMultipleCursorResponses(
aResponses, [this](IndexKeyCursorResponse& response) {
RefPtr<IDBCursor> newCursor;

ResultHelper helper(mRequest, mTransaction, mCursor);
DispatchSuccessEvent(&helper);
if (mCursor) {
if (mCursor->IsContinueCalled()) {
mCursor->Reset(std::move(response.key()),
std::move(response.sortKey()),
std::move(response.objectKey()));
} else {
mCachedResponses.emplace_back(std::move(response.key()),
std::move(response.sortKey()),
std::move(response.objectKey()));
}
} else {
newCursor = IDBCursor::Create(this, std::move(response.key()),
std::move(response.sortKey()),
std::move(response.objectKey()));
mCursor = newCursor;
}
});
}

void BackgroundCursorChild::ActorDestroy(ActorDestroyReason aWhy) {
Expand Down Expand Up @@ -3774,16 +3784,16 @@ mozilla::ipc::IPCResult BackgroundCursorChild::RecvResponse(
HandleResponse(aResponse.get_ArrayOfObjectStoreCursorResponse());
break;

case CursorResponse::TObjectStoreKeyCursorResponse:
HandleResponse(aResponse.get_ObjectStoreKeyCursorResponse());
case CursorResponse::TArrayOfObjectStoreKeyCursorResponse:
HandleResponse(aResponse.get_ArrayOfObjectStoreKeyCursorResponse());
break;

case CursorResponse::TArrayOfIndexCursorResponse:
HandleResponse(aResponse.get_ArrayOfIndexCursorResponse());
break;

case CursorResponse::TIndexKeyCursorResponse:
HandleResponse(aResponse.get_IndexKeyCursorResponse());
case CursorResponse::TArrayOfIndexKeyCursorResponse:
HandleResponse(aResponse.get_ArrayOfIndexKeyCursorResponse());
break;

default:
Expand Down
6 changes: 4 additions & 2 deletions dom/indexedDB/ActorsChild.h
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,8 @@ class BackgroundCursorChild final : public PBackgroundIDBCursorChild {
CachedResponse(Key aKey, StructuredCloneReadInfo&& aCloneInfo);
CachedResponse(Key aKey, Key aLocaleAwareKey, Key aObjectStoreKey,
StructuredCloneReadInfo&& aCloneInfo);
explicit CachedResponse(Key aKey);
CachedResponse(Key aKey, Key aLocaleAwareKey, Key aObjectStoreKey);

CachedResponse(CachedResponse&& aOther) = default;
CachedResponse& operator=(CachedResponse&& aOther) = default;
Expand Down Expand Up @@ -717,7 +719,7 @@ class BackgroundCursorChild final : public PBackgroundIDBCursorChild {

void HandleResponse(const nsTArray<ObjectStoreCursorResponse>& aResponses);

void HandleResponse(const ObjectStoreKeyCursorResponse& aResponse);
void HandleResponse(const nsTArray<ObjectStoreKeyCursorResponse>& aResponses);

void HandleResponse(const nsTArray<IndexCursorResponse>& aResponses);

Expand All @@ -728,7 +730,7 @@ class BackgroundCursorChild final : public PBackgroundIDBCursorChild {
void HandleMultipleCursorResponses(const nsTArray<T>& aResponses,
const Func& aHandleRecord);

void HandleResponse(const IndexKeyCursorResponse& aResponse);
void HandleResponse(const nsTArray<IndexKeyCursorResponse>& aResponses);

// IPDL methods are only called by IPDL.
virtual void ActorDestroy(ActorDestroyReason aWhy) override;
Expand Down
48 changes: 31 additions & 17 deletions dom/indexedDB/ActorsParent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15215,8 +15215,9 @@ void Cursor::SendResponseInternal(
MOZ_ASSERT_IF(
aResponse.type() == CursorResponse::Tnsresult ||
aResponse.type() == CursorResponse::Tvoid_t ||
aResponse.type() == CursorResponse::TObjectStoreKeyCursorResponse ||
aResponse.type() == CursorResponse::TIndexKeyCursorResponse,
aResponse.type() ==
CursorResponse::TArrayOfObjectStoreKeyCursorResponse ||
aResponse.type() == CursorResponse::TArrayOfIndexKeyCursorResponse,
aFiles.IsEmpty());
MOZ_ASSERT(!mActorDestroyed);
MOZ_ASSERT(mCurrentlyRunningOp);
Expand Down Expand Up @@ -25717,7 +25718,12 @@ nsresult Cursor::CursorOpBase::PopulateResponseFromStatement(
Transaction()->AssertIsOnConnectionThread();
MOZ_ASSERT(aInitializeResponse ==
(mResponse.type() == CursorResponse::T__None));
MOZ_ASSERT_IF(mFiles.IsEmpty(), aInitializeResponse);
MOZ_ASSERT_IF(
mFiles.IsEmpty() &&
(mResponse.type() ==
CursorResponse::TArrayOfObjectStoreCursorResponse ||
mResponse.type() == CursorResponse::TArrayOfIndexCursorResponse),
aInitializeResponse);

nsresult rv = mCursor->mPosition.SetFromStatement(aStmt, 0);
if (NS_WARN_IF(NS_FAILED(rv))) {
Expand Down Expand Up @@ -25762,8 +25768,16 @@ nsresult Cursor::CursorOpBase::PopulateResponseFromStatement(
}

case OpenCursorParams::TObjectStoreOpenKeyCursorParams: {
MOZ_ASSERT(aInitializeResponse);
mResponse = ObjectStoreKeyCursorResponse(mCursor->mPosition);
if (aInitializeResponse) {
mResponse = nsTArray<ObjectStoreKeyCursorResponse>();
} else {
MOZ_ASSERT(mResponse.type() ==
CursorResponse::TArrayOfObjectStoreKeyCursorResponse);
}

auto& responses = mResponse.get_ArrayOfObjectStoreKeyCursorResponse();
auto& response = *responses.AppendElement();
response.key() = mCursor->mPosition;
break;
}

Expand Down Expand Up @@ -25820,10 +25834,18 @@ nsresult Cursor::CursorOpBase::PopulateResponseFromStatement(
return rv;
}

MOZ_ASSERT(aInitializeResponse);
mResponse = IndexKeyCursorResponse(mCursor->mPosition,
mCursor->mLocaleAwarePosition,
mCursor->mObjectStorePosition);
if (aInitializeResponse) {
mResponse = nsTArray<IndexKeyCursorResponse>();
} else {
MOZ_ASSERT(mResponse.type() ==
CursorResponse::TArrayOfIndexKeyCursorResponse);
}

auto& responses = mResponse.get_ArrayOfIndexKeyCursorResponse();
auto& response = *responses.AppendElement();
response.key() = mCursor->mPosition;
response.sortKey() = mCursor->mLocaleAwarePosition;
response.objectKey() = mCursor->mObjectStorePosition;
break;
}

Expand All @@ -25839,14 +25861,6 @@ nsresult Cursor::CursorOpBase::PopulateExtraResponses(
const nsCString& aOperation) {
AssertIsOnConnectionThread();

if (mCursor->mType != OpenCursorParams::TObjectStoreOpenCursorParams &&
mCursor->mType != OpenCursorParams::TIndexOpenCursorParams) {
IDB_WARNING(
"PRELOAD: Not yet implemented. Extra results were queried, but are "
"discarded for now.");
return NS_OK;
}

// For unique cursors, we need to skip records with the same key. The SQL
// queries currently do not filter these out.
Key previousKey =
Expand Down
4 changes: 2 additions & 2 deletions dom/indexedDB/PBackgroundIDBCursor.ipdl
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ union CursorResponse
void_t;
nsresult;
ObjectStoreCursorResponse[];
ObjectStoreKeyCursorResponse;
ObjectStoreKeyCursorResponse[];
IndexCursorResponse[];
IndexKeyCursorResponse;
IndexKeyCursorResponse[];
};

protocol PBackgroundIDBCursor
Expand Down

0 comments on commit b09e787

Please sign in to comment.