1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_localstorage_LSWriteOptimizerImpl_h
8 #define mozilla_dom_localstorage_LSWriteOptimizerImpl_h
10 #include "LSWriteOptimizer.h"
12 namespace mozilla::dom
{
14 template <typename T
, typename U
>
15 void LSWriteOptimizer
<T
, U
>::InsertItem(const nsAString
& aKey
, const T
& aValue
,
17 AssertIsOnOwningThread();
19 mWriteInfos
.WithEntryHandle(aKey
, [&](auto&& entry
) {
20 if (entry
&& entry
.Data()->GetType() == WriteInfo::DeleteItem
) {
21 // We could just simply replace the deletion with ordinary update, but
22 // that would preserve item's original position/index. Imagine a case when
23 // we have only one existing key k1. Now let's create a new optimizer and
24 // remove k1, add k2 and add k1 back. The final order should be k2, k1
25 // (ordinary update would produce k1, k2). So we need to differentiate
26 // between normal update and "optimized" update which resulted from a
27 // deletion followed by an insertion. We use the UpdateWithMove flag for
30 entry
.Update(MakeUnique
<UpdateItemInfo
>(NextSerialNumber(), aKey
, aValue
,
31 /* aUpdateWithMove */ true));
34 MakeUnique
<InsertItemInfo
>(NextSerialNumber(), aKey
, aValue
));
38 mTotalDelta
+= aDelta
;
41 template <typename T
, typename U
>
42 void LSWriteOptimizer
<T
, U
>::UpdateItem(const nsAString
& aKey
, const T
& aValue
,
44 AssertIsOnOwningThread();
46 mWriteInfos
.WithEntryHandle(aKey
, [&](auto&& entry
) {
47 if (entry
&& entry
.Data()->GetType() == WriteInfo::InsertItem
) {
49 MakeUnique
<InsertItemInfo
>(NextSerialNumber(), aKey
, aValue
));
52 MakeUnique
<UpdateItemInfo
>(NextSerialNumber(), aKey
, aValue
,
53 /* aUpdateWithMove */ false));
57 mTotalDelta
+= aDelta
;
60 } // namespace mozilla::dom
62 #endif // mozilla_dom_localstorage_LSWriteOptimizerImpl_h