DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (31ec81b5d7bb)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
/* -*- 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/. */

#ifndef mozilla_dom_quota_patternmatcher_h__
#define mozilla_dom_quota_patternmatcher_h__

#include "mozilla/dom/quota/QuotaCommon.h"

#include "ArrayCluster.h"
#include "Utilities.h"

BEGIN_QUOTA_NAMESPACE

template <class ValueType, class BaseType = ArrayCluster<nsIOfflineStorage*> >
class StorageMatcher : public ValueType
{
  typedef StorageMatcher<ValueType, BaseType> SelfType;

  struct Closure
  {
    Closure(SelfType& aSelf)
    : mSelf(aSelf), mPattern(EmptyCString()), mIndexes(nullptr)
    { }

    Closure(SelfType& aSelf, const nsACString& aPattern)
    : mSelf(aSelf), mPattern(aPattern), mIndexes(nullptr)
    { }

    Closure(SelfType& aSelf, const nsTArray<uint32_t>* aIndexes)
    : mSelf(aSelf), mPattern(EmptyCString()), mIndexes(aIndexes)
    { }

    Closure(SelfType& aSelf, const nsACString& aPattern,
            const nsTArray<uint32_t>* aIndexes)
    : mSelf(aSelf), mPattern(aPattern), mIndexes(aIndexes)
    { }

    SelfType& mSelf;
    const nsACString& mPattern;
    const nsTArray<uint32_t>* mIndexes;
  };

public:
  template <class T, class U, class V>
  void
  Find(const nsBaseHashtable<T, U, V>& aHashtable,
       const nsACString& aPattern)
  {
    SelfType::Clear();

    Closure closure(*this, aPattern);
    aHashtable.EnumerateRead(SelfType::MatchPattern, &closure);
  }

  template <class T, class U, class V>
  void
  Find(const nsBaseHashtable<T, U, V>& aHashtable,
       const nsTArray<uint32_t>* aIndexes)
  {
    SelfType::Clear();

    Closure closure(*this, aIndexes);
    aHashtable.EnumerateRead(SelfType::MatchIndexes, &closure);
  }

  template <class T, class U, class V>
  void
  Find(const nsBaseHashtable<T, U, V>& aHashtable,
       uint32_t aIndex)
  {
    nsAutoTArray<uint32_t, 1> indexes;
    indexes.AppendElement(aIndex);

    Find(aHashtable, &indexes);
  }

  template <class T, class U, class V>
  void
  Find(const nsBaseHashtable<T, U, V>& aHashtable,
       const nsACString& aPattern,
       const nsTArray<uint32_t>* aIndexes)
  {
    SelfType::Clear();

    Closure closure(*this, aPattern, aIndexes);
    aHashtable.EnumerateRead(SelfType::MatchPatternAndIndexes, &closure);
  }

  template <class T, class U, class V>
  void
  Find(const nsBaseHashtable<T, U, V>& aHashtable,
       const nsACString& aPattern,
       uint32_t aIndex)
  {
    nsAutoTArray<uint32_t, 1> indexes;
    indexes.AppendElement(aIndex);

    Find(aHashtable, aPattern, &indexes);
  }

  template <class T, class U, class V>
  void
  Find(const nsBaseHashtable<T, U, V>& aHashtable)
  {
    SelfType::Clear();

    Closure closure(*this);
    aHashtable.EnumerateRead(SelfType::MatchAll, &closure);
  }

private:
  static PLDHashOperator
  MatchPattern(const nsACString& aKey,
               BaseType* aValue,
               void* aUserArg)
  {
    MOZ_ASSERT(!aKey.IsEmpty(), "Empty key!");
    MOZ_ASSERT(aValue, "Null pointer!");
    MOZ_ASSERT(aUserArg, "Null pointer!");

    Closure* closure = static_cast<Closure*>(aUserArg);

    if (PatternMatchesOrigin(closure->mPattern, aKey)) {
      aValue->AppendElementsTo(closure->mSelf);
    }

    return PL_DHASH_NEXT;
  }

  static PLDHashOperator
  MatchIndexes(const nsACString& aKey,
               BaseType* aValue,
               void* aUserArg)
  {
    MOZ_ASSERT(!aKey.IsEmpty(), "Empty key!");
    MOZ_ASSERT(aValue, "Null pointer!");
    MOZ_ASSERT(aUserArg, "Null pointer!");

    Closure* closure = static_cast<Closure*>(aUserArg);

    for (uint32_t index = 0; index < closure->mIndexes->Length(); index++) {
      aValue->AppendElementsTo(closure->mIndexes->ElementAt(index),
                               closure->mSelf);
    }

    return PL_DHASH_NEXT;
  }

  static PLDHashOperator
  MatchPatternAndIndexes(const nsACString& aKey,
                         BaseType* aValue,
                         void* aUserArg)
  {
    MOZ_ASSERT(!aKey.IsEmpty(), "Empty key!");
    MOZ_ASSERT(aValue, "Null pointer!");
    MOZ_ASSERT(aUserArg, "Null pointer!");

    Closure* closure = static_cast<Closure*>(aUserArg);

    if (PatternMatchesOrigin(closure->mPattern, aKey)) {
      for (uint32_t index = 0; index < closure->mIndexes->Length(); index++) {
        aValue->AppendElementsTo(closure->mIndexes->ElementAt(index),
                                 closure->mSelf);
      }
    }

    return PL_DHASH_NEXT;
  }

  static PLDHashOperator
  MatchAll(const nsACString& aKey,
           BaseType* aValue,
           void* aUserArg)
  {
    MOZ_ASSERT(!aKey.IsEmpty(), "Empty key!");
    MOZ_ASSERT(aValue, "Null pointer!");
    MOZ_ASSERT(aUserArg, "Null pointer!");

    Closure* closure = static_cast<Closure*>(aUserArg);
    aValue->AppendElementsTo(closure->mSelf);

    return PL_DHASH_NEXT;
  }
};

END_QUOTA_NAMESPACE

#endif // mozilla_dom_quota_patternmatcher_h__