tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

LSWriteOptimizerImpl.h (2426B)


      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/. */
      6 
      7 #ifndef mozilla_dom_localstorage_LSWriteOptimizerImpl_h
      8 #define mozilla_dom_localstorage_LSWriteOptimizerImpl_h
      9 
     10 #include "LSWriteOptimizer.h"
     11 
     12 namespace mozilla::dom {
     13 
     14 template <typename T, typename U>
     15 void LSWriteOptimizer<T, U>::InsertItem(const nsAString& aKey, const T& aValue,
     16                                        int64_t aDelta) {
     17  AssertIsOnOwningThread();
     18 
     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
     28      // this.
     29 
     30      entry.Update(MakeUnique<UpdateItemInfo>(NextSerialNumber(), aKey, aValue,
     31                                              /* aUpdateWithMove */ true));
     32    } else {
     33      entry.InsertOrUpdate(
     34          MakeUnique<InsertItemInfo>(NextSerialNumber(), aKey, aValue));
     35    }
     36  });
     37 
     38  mTotalDelta += aDelta;
     39 }
     40 
     41 template <typename T, typename U>
     42 void LSWriteOptimizer<T, U>::UpdateItem(const nsAString& aKey, const T& aValue,
     43                                        int64_t aDelta) {
     44  AssertIsOnOwningThread();
     45 
     46  mWriteInfos.WithEntryHandle(aKey, [&](auto&& entry) {
     47    if (entry && entry.Data()->GetType() == WriteInfo::InsertItem) {
     48      entry.Update(
     49          MakeUnique<InsertItemInfo>(NextSerialNumber(), aKey, aValue));
     50    } else {
     51      entry.InsertOrUpdate(
     52          MakeUnique<UpdateItemInfo>(NextSerialNumber(), aKey, aValue,
     53                                     /* aUpdateWithMove */ false));
     54    }
     55  });
     56 
     57  mTotalDelta += aDelta;
     58 }
     59 
     60 }  // namespace mozilla::dom
     61 
     62 #endif  // mozilla_dom_localstorage_LSWriteOptimizerImpl_h