dom/quota/StorageMatcher.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/quota/StorageMatcher.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,191 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim: set ts=2 et sw=2 tw=80: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.8 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#ifndef mozilla_dom_quota_patternmatcher_h__
    1.11 +#define mozilla_dom_quota_patternmatcher_h__
    1.12 +
    1.13 +#include "mozilla/dom/quota/QuotaCommon.h"
    1.14 +
    1.15 +#include "ArrayCluster.h"
    1.16 +#include "Utilities.h"
    1.17 +
    1.18 +BEGIN_QUOTA_NAMESPACE
    1.19 +
    1.20 +template <class ValueType, class BaseType = ArrayCluster<nsIOfflineStorage*> >
    1.21 +class StorageMatcher : public ValueType
    1.22 +{
    1.23 +  typedef StorageMatcher<ValueType, BaseType> SelfType;
    1.24 +
    1.25 +  struct Closure
    1.26 +  {
    1.27 +    Closure(SelfType& aSelf)
    1.28 +    : mSelf(aSelf), mPattern(EmptyCString()), mIndexes(nullptr)
    1.29 +    { }
    1.30 +
    1.31 +    Closure(SelfType& aSelf, const nsACString& aPattern)
    1.32 +    : mSelf(aSelf), mPattern(aPattern), mIndexes(nullptr)
    1.33 +    { }
    1.34 +
    1.35 +    Closure(SelfType& aSelf, const nsTArray<uint32_t>* aIndexes)
    1.36 +    : mSelf(aSelf), mPattern(EmptyCString()), mIndexes(aIndexes)
    1.37 +    { }
    1.38 +
    1.39 +    Closure(SelfType& aSelf, const nsACString& aPattern,
    1.40 +            const nsTArray<uint32_t>* aIndexes)
    1.41 +    : mSelf(aSelf), mPattern(aPattern), mIndexes(aIndexes)
    1.42 +    { }
    1.43 +
    1.44 +    SelfType& mSelf;
    1.45 +    const nsACString& mPattern;
    1.46 +    const nsTArray<uint32_t>* mIndexes;
    1.47 +  };
    1.48 +
    1.49 +public:
    1.50 +  template <class T, class U, class V>
    1.51 +  void
    1.52 +  Find(const nsBaseHashtable<T, U, V>& aHashtable,
    1.53 +       const nsACString& aPattern)
    1.54 +  {
    1.55 +    SelfType::Clear();
    1.56 +
    1.57 +    Closure closure(*this, aPattern);
    1.58 +    aHashtable.EnumerateRead(SelfType::MatchPattern, &closure);
    1.59 +  }
    1.60 +
    1.61 +  template <class T, class U, class V>
    1.62 +  void
    1.63 +  Find(const nsBaseHashtable<T, U, V>& aHashtable,
    1.64 +       const nsTArray<uint32_t>* aIndexes)
    1.65 +  {
    1.66 +    SelfType::Clear();
    1.67 +
    1.68 +    Closure closure(*this, aIndexes);
    1.69 +    aHashtable.EnumerateRead(SelfType::MatchIndexes, &closure);
    1.70 +  }
    1.71 +
    1.72 +  template <class T, class U, class V>
    1.73 +  void
    1.74 +  Find(const nsBaseHashtable<T, U, V>& aHashtable,
    1.75 +       uint32_t aIndex)
    1.76 +  {
    1.77 +    nsAutoTArray<uint32_t, 1> indexes;
    1.78 +    indexes.AppendElement(aIndex);
    1.79 +
    1.80 +    Find(aHashtable, &indexes);
    1.81 +  }
    1.82 +
    1.83 +  template <class T, class U, class V>
    1.84 +  void
    1.85 +  Find(const nsBaseHashtable<T, U, V>& aHashtable,
    1.86 +       const nsACString& aPattern,
    1.87 +       const nsTArray<uint32_t>* aIndexes)
    1.88 +  {
    1.89 +    SelfType::Clear();
    1.90 +
    1.91 +    Closure closure(*this, aPattern, aIndexes);
    1.92 +    aHashtable.EnumerateRead(SelfType::MatchPatternAndIndexes, &closure);
    1.93 +  }
    1.94 +
    1.95 +  template <class T, class U, class V>
    1.96 +  void
    1.97 +  Find(const nsBaseHashtable<T, U, V>& aHashtable,
    1.98 +       const nsACString& aPattern,
    1.99 +       uint32_t aIndex)
   1.100 +  {
   1.101 +    nsAutoTArray<uint32_t, 1> indexes;
   1.102 +    indexes.AppendElement(aIndex);
   1.103 +
   1.104 +    Find(aHashtable, aPattern, &indexes);
   1.105 +  }
   1.106 +
   1.107 +  template <class T, class U, class V>
   1.108 +  void
   1.109 +  Find(const nsBaseHashtable<T, U, V>& aHashtable)
   1.110 +  {
   1.111 +    SelfType::Clear();
   1.112 +
   1.113 +    Closure closure(*this);
   1.114 +    aHashtable.EnumerateRead(SelfType::MatchAll, &closure);
   1.115 +  }
   1.116 +
   1.117 +private:
   1.118 +  static PLDHashOperator
   1.119 +  MatchPattern(const nsACString& aKey,
   1.120 +               BaseType* aValue,
   1.121 +               void* aUserArg)
   1.122 +  {
   1.123 +    MOZ_ASSERT(!aKey.IsEmpty(), "Empty key!");
   1.124 +    MOZ_ASSERT(aValue, "Null pointer!");
   1.125 +    MOZ_ASSERT(aUserArg, "Null pointer!");
   1.126 +
   1.127 +    Closure* closure = static_cast<Closure*>(aUserArg);
   1.128 +
   1.129 +    if (PatternMatchesOrigin(closure->mPattern, aKey)) {
   1.130 +      aValue->AppendElementsTo(closure->mSelf);
   1.131 +    }
   1.132 +
   1.133 +    return PL_DHASH_NEXT;
   1.134 +  }
   1.135 +
   1.136 +  static PLDHashOperator
   1.137 +  MatchIndexes(const nsACString& aKey,
   1.138 +               BaseType* aValue,
   1.139 +               void* aUserArg)
   1.140 +  {
   1.141 +    MOZ_ASSERT(!aKey.IsEmpty(), "Empty key!");
   1.142 +    MOZ_ASSERT(aValue, "Null pointer!");
   1.143 +    MOZ_ASSERT(aUserArg, "Null pointer!");
   1.144 +
   1.145 +    Closure* closure = static_cast<Closure*>(aUserArg);
   1.146 +
   1.147 +    for (uint32_t index = 0; index < closure->mIndexes->Length(); index++) {
   1.148 +      aValue->AppendElementsTo(closure->mIndexes->ElementAt(index),
   1.149 +                               closure->mSelf);
   1.150 +    }
   1.151 +
   1.152 +    return PL_DHASH_NEXT;
   1.153 +  }
   1.154 +
   1.155 +  static PLDHashOperator
   1.156 +  MatchPatternAndIndexes(const nsACString& aKey,
   1.157 +                         BaseType* aValue,
   1.158 +                         void* aUserArg)
   1.159 +  {
   1.160 +    MOZ_ASSERT(!aKey.IsEmpty(), "Empty key!");
   1.161 +    MOZ_ASSERT(aValue, "Null pointer!");
   1.162 +    MOZ_ASSERT(aUserArg, "Null pointer!");
   1.163 +
   1.164 +    Closure* closure = static_cast<Closure*>(aUserArg);
   1.165 +
   1.166 +    if (PatternMatchesOrigin(closure->mPattern, aKey)) {
   1.167 +      for (uint32_t index = 0; index < closure->mIndexes->Length(); index++) {
   1.168 +        aValue->AppendElementsTo(closure->mIndexes->ElementAt(index),
   1.169 +                                 closure->mSelf);
   1.170 +      }
   1.171 +    }
   1.172 +
   1.173 +    return PL_DHASH_NEXT;
   1.174 +  }
   1.175 +
   1.176 +  static PLDHashOperator
   1.177 +  MatchAll(const nsACString& aKey,
   1.178 +           BaseType* aValue,
   1.179 +           void* aUserArg)
   1.180 +  {
   1.181 +    MOZ_ASSERT(!aKey.IsEmpty(), "Empty key!");
   1.182 +    MOZ_ASSERT(aValue, "Null pointer!");
   1.183 +    MOZ_ASSERT(aUserArg, "Null pointer!");
   1.184 +
   1.185 +    Closure* closure = static_cast<Closure*>(aUserArg);
   1.186 +    aValue->AppendElementsTo(closure->mSelf);
   1.187 +
   1.188 +    return PL_DHASH_NEXT;
   1.189 +  }
   1.190 +};
   1.191 +
   1.192 +END_QUOTA_NAMESPACE
   1.193 +
   1.194 +#endif // mozilla_dom_quota_patternmatcher_h__

mercurial