no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / dom / localstorage / LSWriteOptimizer.cpp
blob0928dca35ea53931853605d2719352cfe7d479c3
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=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 file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "LSWriteOptimizer.h"
9 #include <new>
10 #include "nsBaseHashtable.h"
11 #include "nsTArray.h"
13 namespace mozilla::dom {
15 class LSWriteOptimizerBase::WriteInfoComparator {
16 public:
17 bool Equals(const WriteInfo* a, const WriteInfo* b) const {
18 MOZ_ASSERT(a && b);
19 return a->SerialNumber() == b->SerialNumber();
22 bool LessThan(const WriteInfo* a, const WriteInfo* b) const {
23 MOZ_ASSERT(a && b);
24 return a->SerialNumber() < b->SerialNumber();
28 void LSWriteOptimizerBase::DeleteItem(const nsAString& aKey, int64_t aDelta) {
29 AssertIsOnOwningThread();
31 mWriteInfos.WithEntryHandle(aKey, [&](auto&& entry) {
32 if (entry && entry.Data()->GetType() == WriteInfo::InsertItem) {
33 entry.Remove();
34 } else {
35 entry.InsertOrUpdate(
36 MakeUnique<DeleteItemInfo>(NextSerialNumber(), aKey));
38 });
40 mTotalDelta += aDelta;
43 void LSWriteOptimizerBase::Truncate(int64_t aDelta) {
44 AssertIsOnOwningThread();
46 mWriteInfos.Clear();
48 if (!mTruncateInfo) {
49 mTruncateInfo = MakeUnique<TruncateInfo>(NextSerialNumber());
52 mTotalDelta += aDelta;
55 void LSWriteOptimizerBase::GetSortedWriteInfos(
56 nsTArray<NotNull<WriteInfo*>>& aWriteInfos) {
57 AssertIsOnOwningThread();
59 if (mTruncateInfo) {
60 aWriteInfos.InsertElementSorted(WrapNotNullUnchecked(mTruncateInfo.get()),
61 WriteInfoComparator());
64 for (const auto& entry : mWriteInfos) {
65 WriteInfo* writeInfo = entry.GetWeak();
67 aWriteInfos.InsertElementSorted(WrapNotNull(writeInfo),
68 WriteInfoComparator());
72 } // namespace mozilla::dom