2019-05-15 07:11:10 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "LSWriteOptimizer.h"
|
|
|
|
|
2020-10-21 16:17:18 +03:00
|
|
|
#include <new>
|
|
|
|
#include "nsBaseHashtable.h"
|
|
|
|
#include "nsTArray.h"
|
|
|
|
|
2020-11-04 20:04:01 +03:00
|
|
|
namespace mozilla::dom {
|
2019-05-15 07:11:10 +03:00
|
|
|
|
2019-05-15 07:11:11 +03:00
|
|
|
class LSWriteOptimizerBase::WriteInfoComparator {
|
|
|
|
public:
|
|
|
|
bool Equals(const WriteInfo* a, const WriteInfo* b) const {
|
2020-01-10 18:09:09 +03:00
|
|
|
MOZ_ASSERT(a && b);
|
|
|
|
return a->SerialNumber() == b->SerialNumber();
|
2019-05-15 07:11:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool LessThan(const WriteInfo* a, const WriteInfo* b) const {
|
2020-01-10 18:09:09 +03:00
|
|
|
MOZ_ASSERT(a && b);
|
|
|
|
return a->SerialNumber() < b->SerialNumber();
|
2019-05-15 07:11:11 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-05-15 07:11:10 +03:00
|
|
|
void LSWriteOptimizerBase::DeleteItem(const nsAString& aKey, int64_t aDelta) {
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
|
2021-03-01 12:59:31 +03:00
|
|
|
mWriteInfos.WithEntryHandle(aKey, [&](auto&& entry) {
|
|
|
|
if (entry && entry.Data()->GetType() == WriteInfo::InsertItem) {
|
|
|
|
entry.Remove();
|
|
|
|
} else {
|
|
|
|
entry.InsertOrUpdate(
|
|
|
|
MakeUnique<DeleteItemInfo>(NextSerialNumber(), aKey));
|
|
|
|
}
|
|
|
|
});
|
2019-05-15 07:11:10 +03:00
|
|
|
|
|
|
|
mTotalDelta += aDelta;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LSWriteOptimizerBase::Truncate(int64_t aDelta) {
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
|
|
|
|
mWriteInfos.Clear();
|
|
|
|
|
|
|
|
if (!mTruncateInfo) {
|
2020-02-26 02:14:57 +03:00
|
|
|
mTruncateInfo = MakeUnique<TruncateInfo>(NextSerialNumber());
|
2019-05-15 07:11:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
mTotalDelta += aDelta;
|
|
|
|
}
|
|
|
|
|
2019-05-15 07:11:11 +03:00
|
|
|
void LSWriteOptimizerBase::GetSortedWriteInfos(
|
2021-01-21 14:58:47 +03:00
|
|
|
nsTArray<NotNull<WriteInfo*>>& aWriteInfos) {
|
2019-05-15 07:11:11 +03:00
|
|
|
AssertIsOnOwningThread();
|
|
|
|
|
|
|
|
if (mTruncateInfo) {
|
2021-01-21 14:58:47 +03:00
|
|
|
aWriteInfos.InsertElementSorted(WrapNotNullUnchecked(mTruncateInfo.get()),
|
|
|
|
WriteInfoComparator());
|
2019-05-15 07:11:11 +03:00
|
|
|
}
|
|
|
|
|
2021-03-12 19:14:58 +03:00
|
|
|
for (const auto& entry : mWriteInfos) {
|
|
|
|
WriteInfo* writeInfo = entry.GetWeak();
|
2019-05-15 07:11:11 +03:00
|
|
|
|
2021-01-21 14:58:47 +03:00
|
|
|
aWriteInfos.InsertElementSorted(WrapNotNull(writeInfo),
|
|
|
|
WriteInfoComparator());
|
2019-05-15 07:11:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-04 20:04:01 +03:00
|
|
|
} // namespace mozilla::dom
|