Title: [208310] trunk/Source/WebCore
Revision
208310
Author
beid...@apple.com
Date
2016-11-02 16:04:22 -0700 (Wed, 02 Nov 2016)

Log Message

Give IDBKey(Data) a WTF::Variant overhaul.
https://bugs.webkit.org/show_bug.cgi?id=164332

Reviewed by Alex Christensen and Andy Estes.

No new tests (Refactor, no behavior change).

* Modules/indexeddb/IDBKey.cpp:
(WebCore::IDBKey::IDBKey):
(WebCore::IDBKey::isValid):
(WebCore::IDBKey::compare):
* Modules/indexeddb/IDBKey.h:
(WebCore::IDBKey::array):
(WebCore::IDBKey::string):
(WebCore::IDBKey::date):
(WebCore::IDBKey::number):
(WebCore::IDBKey::IDBKey): Deleted.

* Modules/indexeddb/IDBKeyData.cpp:
(WebCore::IDBKeyData::IDBKeyData):
(WebCore::IDBKeyData::maybeCreateIDBKey):
(WebCore::IDBKeyData::isolatedCopy):
(WebCore::IDBKeyData::encode):
(WebCore::IDBKeyData::decode):
(WebCore::IDBKeyData::compare):
(WebCore::IDBKeyData::loggingString):
(WebCore::IDBKeyData::setArrayValue):
(WebCore::IDBKeyData::setStringValue):
(WebCore::IDBKeyData::setDateValue):
(WebCore::IDBKeyData::setNumberValue):
(WebCore::IDBKeyData::operator==):
* Modules/indexeddb/IDBKeyData.h:
(WebCore::IDBKeyData::hash):
(WebCore::IDBKeyData::string):
(WebCore::IDBKeyData::date):
(WebCore::IDBKeyData::number):
(WebCore::IDBKeyData::array):
(WebCore::IDBKeyData::encode):
(WebCore::IDBKeyData::decode):

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (208309 => 208310)


--- trunk/Source/WebCore/ChangeLog	2016-11-02 22:19:39 UTC (rev 208309)
+++ trunk/Source/WebCore/ChangeLog	2016-11-02 23:04:22 UTC (rev 208310)
@@ -1,3 +1,45 @@
+2016-11-02  Brady Eidson  <beid...@apple.com>
+
+        Give IDBKey(Data) a WTF::Variant overhaul.
+        https://bugs.webkit.org/show_bug.cgi?id=164332
+
+        Reviewed by Alex Christensen and Andy Estes.
+
+        No new tests (Refactor, no behavior change).
+
+        * Modules/indexeddb/IDBKey.cpp:
+        (WebCore::IDBKey::IDBKey):
+        (WebCore::IDBKey::isValid):
+        (WebCore::IDBKey::compare):
+        * Modules/indexeddb/IDBKey.h:
+        (WebCore::IDBKey::array):
+        (WebCore::IDBKey::string):
+        (WebCore::IDBKey::date):
+        (WebCore::IDBKey::number):
+        (WebCore::IDBKey::IDBKey): Deleted.
+
+        * Modules/indexeddb/IDBKeyData.cpp:
+        (WebCore::IDBKeyData::IDBKeyData):
+        (WebCore::IDBKeyData::maybeCreateIDBKey):
+        (WebCore::IDBKeyData::isolatedCopy):
+        (WebCore::IDBKeyData::encode):
+        (WebCore::IDBKeyData::decode):
+        (WebCore::IDBKeyData::compare):
+        (WebCore::IDBKeyData::loggingString):
+        (WebCore::IDBKeyData::setArrayValue):
+        (WebCore::IDBKeyData::setStringValue):
+        (WebCore::IDBKeyData::setDateValue):
+        (WebCore::IDBKeyData::setNumberValue):
+        (WebCore::IDBKeyData::operator==):
+        * Modules/indexeddb/IDBKeyData.h:
+        (WebCore::IDBKeyData::hash):
+        (WebCore::IDBKeyData::string):
+        (WebCore::IDBKeyData::date):
+        (WebCore::IDBKeyData::number):
+        (WebCore::IDBKeyData::array):
+        (WebCore::IDBKeyData::encode):
+        (WebCore::IDBKeyData::decode):
+
 2016-11-01  Sam Weinig  <s...@webkit.org>
 
         [WebIDL] Move interfaces and typed arrays over to JSDOMConvert

Modified: trunk/Source/WebCore/Modules/indexeddb/IDBKey.cpp (208309 => 208310)


--- trunk/Source/WebCore/Modules/indexeddb/IDBKey.cpp	2016-11-02 22:19:39 UTC (rev 208309)
+++ trunk/Source/WebCore/Modules/indexeddb/IDBKey.cpp	2016-11-02 23:04:22 UTC (rev 208310)
@@ -32,6 +32,29 @@
 
 namespace WebCore {
 
+using IDBKeyVector = Vector<RefPtr<IDBKey>>;
+
+IDBKey::IDBKey(KeyType type, double number)
+    : m_type(type)
+    , m_value(number)
+    , m_sizeEstimate(OverheadSize + sizeof(double))
+{
+}
+
+IDBKey::IDBKey(const String& value)
+    : m_type(KeyType::String)
+    , m_value(value)
+    , m_sizeEstimate(OverheadSize + value.length() * sizeof(UChar))
+{
+}
+
+IDBKey::IDBKey(const IDBKeyVector& keyArray, size_t arraySize)
+    : m_type(KeyType::Array)
+    , m_value(keyArray)
+    , m_sizeEstimate(OverheadSize + arraySize)
+{
+}
+
 IDBKey::~IDBKey()
 {
 }
@@ -42,7 +65,7 @@
         return false;
 
     if (m_type == KeyType::Array) {
-        for (auto& key : m_array) {
+        for (auto& key : WTF::get<IDBKeyVector>(m_value)) {
             if (!key->isValid())
                 return false;
         }
@@ -57,21 +80,27 @@
         return m_type > other.m_type ? -1 : 1;
 
     switch (m_type) {
-    case KeyType::Array:
-        for (size_t i = 0; i < m_array.size() && i < other.m_array.size(); ++i) {
-            if (int result = m_array[i]->compare(*other.m_array[i]))
+    case KeyType::Array: {
+        auto& array = WTF::get<IDBKeyVector>(m_value);
+        auto& otherArray = WTF::get<IDBKeyVector>(other.m_value);
+        for (size_t i = 0; i < array.size() && i < otherArray.size(); ++i) {
+            if (int result = array[i]->compare(*otherArray[i]))
                 return result;
         }
-        if (m_array.size() < other.m_array.size())
+        if (array.size() < otherArray.size())
             return -1;
-        if (m_array.size() > other.m_array.size())
+        if (array.size() > otherArray.size())
             return 1;
         return 0;
+    }
     case KeyType::String:
-        return -codePointCompare(other.m_string, m_string);
+        return -codePointCompare(WTF::get<String>(other.m_value), WTF::get<String>(m_value));
     case KeyType::Date:
-    case KeyType::Number:
-        return (m_number < other.m_number) ? -1 : ((m_number > other. m_number) ? 1 : 0);
+    case KeyType::Number: {
+        auto number = WTF::get<double>(m_value);
+        auto otherNumber = WTF::get<double>(other.m_value);
+        return (number < otherNumber) ? -1 : ((number > otherNumber) ? 1 : 0);
+    }
     case KeyType::Invalid:
     case KeyType::Min:
     case KeyType::Max:

Modified: trunk/Source/WebCore/Modules/indexeddb/IDBKey.h (208309 => 208310)


--- trunk/Source/WebCore/Modules/indexeddb/IDBKey.h	2016-11-02 22:19:39 UTC (rev 208309)
+++ trunk/Source/WebCore/Modules/indexeddb/IDBKey.h	2016-11-02 23:04:22 UTC (rev 208310)
@@ -31,6 +31,7 @@
 #include "IndexedDB.h"
 #include <wtf/Forward.h>
 #include <wtf/RefCounted.h>
+#include <wtf/Variant.h>
 #include <wtf/Vector.h>
 #include <wtf/text/WTFString.h>
 
@@ -103,25 +104,25 @@
     const Vector<RefPtr<IDBKey>>& array() const
     {
         ASSERT(m_type == KeyType::Array);
-        return m_array;
+        return WTF::get<Vector<RefPtr<IDBKey>>>(m_value);
     }
 
     const String& string() const
     {
         ASSERT(m_type == KeyType::String);
-        return m_string;
+        return WTF::get<String>(m_value);
     }
 
     double date() const
     {
         ASSERT(m_type == KeyType::Date);
-        return m_number;
+        return WTF::get<double>(m_value);
     }
 
     double number() const
     {
         ASSERT(m_type == KeyType::Number);
-        return m_number;
+        return WTF::get<double>(m_value);
     }
 
     int compare(const IDBKey& other) const;
@@ -143,15 +144,18 @@
 #endif
 
 private:
-    IDBKey() : m_type(KeyType::Invalid), m_number(0), m_sizeEstimate(OverheadSize) { }
-    IDBKey(KeyType type, double number) : m_type(type), m_number(number), m_sizeEstimate(OverheadSize + sizeof(double)) { }
-    explicit IDBKey(const String& value) : m_type(KeyType::String), m_string(value), m_number(0), m_sizeEstimate(OverheadSize + value.length() * sizeof(UChar)) { }
-    IDBKey(const Vector<RefPtr<IDBKey>>& keyArray, size_t arraySize) : m_type(KeyType::Array), m_array(keyArray), m_number(0), m_sizeEstimate(OverheadSize + arraySize) { }
+    IDBKey()
+        : m_type(KeyType::Invalid)
+        , m_sizeEstimate(OverheadSize)
+    {
+    }
 
+    IDBKey(KeyType, double number);
+    explicit IDBKey(const String& value);
+    IDBKey(const Vector<RefPtr<IDBKey>>& keyArray, size_t arraySize);
+
     const KeyType m_type;
-    const Vector<RefPtr<IDBKey>> m_array;
-    const String m_string;
-    const double m_number;
+    Variant<Vector<RefPtr<IDBKey>>, String, double> m_value;
 
     const size_t m_sizeEstimate;
 

Modified: trunk/Source/WebCore/Modules/indexeddb/IDBKeyData.cpp (208309 => 208310)


--- trunk/Source/WebCore/Modules/indexeddb/IDBKeyData.cpp	2016-11-02 22:19:39 UTC (rev 208309)
+++ trunk/Source/WebCore/Modules/indexeddb/IDBKeyData.cpp	2016-11-02 23:04:22 UTC (rev 208310)
@@ -46,18 +46,21 @@
     switch (m_type) {
     case KeyType::Invalid:
         break;
-    case KeyType::Array:
+    case KeyType::Array: {
+        m_value = Vector<IDBKeyData>();
+        auto& array = WTF::get<Vector<IDBKeyData>>(m_value);
         for (auto& key2 : key->array())
-            m_arrayValue.append(IDBKeyData(key2.get()));
+            array.append(IDBKeyData(key2.get()));
         break;
+    }
     case KeyType::String:
-        m_stringValue = key->string();
+        m_value = key->string();
         break;
     case KeyType::Date:
-        m_numberValue = key->date();
+        m_value = key->date();
         break;
     case KeyType::Number:
-        m_numberValue = key->number();
+        m_value = key->number();
         break;
     case KeyType::Max:
     case KeyType::Min:
@@ -73,21 +76,20 @@
     switch (m_type) {
     case KeyType::Invalid:
         return IDBKey::createInvalid();
-    case KeyType::Array:
-        {
-            Vector<RefPtr<IDBKey>> array;
-            for (auto& keyData : m_arrayValue) {
-                array.append(keyData.maybeCreateIDBKey());
-                ASSERT(array.last());
-            }
-            return IDBKey::createArray(array);
+    case KeyType::Array: {
+        Vector<RefPtr<IDBKey>> array;
+        for (auto& keyData : WTF::get<Vector<IDBKeyData>>(m_value)) {
+            array.append(keyData.maybeCreateIDBKey());
+            ASSERT(array.last());
         }
+        return IDBKey::createArray(array);
+    }
     case KeyType::String:
-        return IDBKey::createString(m_stringValue);
+        return IDBKey::createString(WTF::get<String>(m_value));
     case KeyType::Date:
-        return IDBKey::createDate(m_numberValue);
+        return IDBKey::createDate(WTF::get<double>(m_value));
     case KeyType::Number:
-        return IDBKey::createNumber(m_numberValue);
+        return IDBKey::createNumber(WTF::get<double>(m_value));
     case KeyType::Max:
     case KeyType::Min:
         ASSERT_NOT_REACHED();
@@ -116,16 +118,19 @@
     switch (source.m_type) {
     case KeyType::Invalid:
         return;
-    case KeyType::Array:
-        for (auto& key : source.m_arrayValue)
-            destination.m_arrayValue.append(key.isolatedCopy());
+    case KeyType::Array: {
+        destination.m_value = Vector<IDBKeyData>();
+        auto& destinationArray = WTF::get<Vector<IDBKeyData>>(destination.m_value);
+        for (auto& key : WTF::get<Vector<IDBKeyData>>(source.m_value))
+            destinationArray.append(key.isolatedCopy());
         return;
+    }
     case KeyType::String:
-        destination.m_stringValue = source.m_stringValue.isolatedCopy();
+        destination.m_value = WTF::get<String>(source.m_value).isolatedCopy();
         return;
     case KeyType::Date:
     case KeyType::Number:
-        destination.m_numberValue = source.m_numberValue;
+        destination.m_value = WTF::get<double>(source.m_value);
         return;
     case KeyType::Max:
     case KeyType::Min:
@@ -146,17 +151,19 @@
     switch (m_type) {
     case KeyType::Invalid:
         return;
-    case KeyType::Array:
-        encoder.encodeObjects("array", m_arrayValue.begin(), m_arrayValue.end(), [](KeyedEncoder& encoder, const IDBKeyData& key) {
+    case KeyType::Array: {
+        auto& array = WTF::get<Vector<IDBKeyData>>(m_value);
+        encoder.encodeObjects("array", array.begin(), array.end(), [](KeyedEncoder& encoder, const IDBKeyData& key) {
             key.encode(encoder);
         });
         return;
+    }
     case KeyType::String:
-        encoder.encodeString("string", m_stringValue);
+        encoder.encodeString("string", WTF::get<String>(m_value));
         return;
     case KeyType::Date:
     case KeyType::Number:
-        encoder.encodeDouble("number", m_numberValue);
+        encoder.encodeDouble("number", WTF::get<double>(m_value));
         return;
     case KeyType::Max:
     case KeyType::Min:
@@ -195,11 +202,15 @@
     if (result.m_type == KeyType::Min)
         return true;
 
-    if (result.m_type == KeyType::String)
-        return decoder.decodeString("string", result.m_stringValue);
+    if (result.m_type == KeyType::String) {
+        result.m_value = String();
+        return decoder.decodeString("string", WTF::get<String>(result.m_value));
+    }
 
-    if (result.m_type == KeyType::Number || result.m_type == KeyType::Date)
-        return decoder.decodeDouble("number", result.m_numberValue);
+    if (result.m_type == KeyType::Number || result.m_type == KeyType::Date) {
+        result.m_value = 0.0;
+        return decoder.decodeDouble("number", WTF::get<double>(result.m_value));
+    }
 
     ASSERT(result.m_type == KeyType::Array);
 
@@ -207,8 +218,8 @@
         return decode(decoder, result);
     };
     
-    result.m_arrayValue.clear();
-    return decoder.decodeObjects("array", result.m_arrayValue, arrayFunction);
+    result.m_value = Vector<IDBKeyData>();
+    return decoder.decodeObjects("array", WTF::get<Vector<IDBKeyData>>(result.m_value), arrayFunction);
 }
 
 int IDBKeyData::compare(const IDBKeyData& other) const
@@ -231,23 +242,30 @@
         // Invalid type should have been fully handled above
         ASSERT_NOT_REACHED();
         return 0;
-    case KeyType::Array:
-        for (size_t i = 0; i < m_arrayValue.size() && i < other.m_arrayValue.size(); ++i) {
-            if (int result = m_arrayValue[i].compare(other.m_arrayValue[i]))
+    case KeyType::Array: {
+        auto& array = WTF::get<Vector<IDBKeyData>>(m_value);
+        auto& otherArray = WTF::get<Vector<IDBKeyData>>(other.m_value);
+        for (size_t i = 0; i < array.size() && i < otherArray.size(); ++i) {
+            if (int result = array[i].compare(otherArray[i]))
                 return result;
         }
-        if (m_arrayValue.size() < other.m_arrayValue.size())
+        if (array.size() < otherArray.size())
             return -1;
-        if (m_arrayValue.size() > other.m_arrayValue.size())
+        if (array.size() > otherArray.size())
             return 1;
         return 0;
+    }
     case KeyType::String:
-        return codePointCompare(m_stringValue, other.m_stringValue);
+        return codePointCompare(WTF::get<String>(m_value), WTF::get<String>(other.m_value));
     case KeyType::Date:
-    case KeyType::Number:
-        if (m_numberValue == other.m_numberValue)
+    case KeyType::Number: {
+        auto number = WTF::get<double>(m_value);
+        auto otherNumber = WTF::get<double>(other.m_value);
+
+        if (number == otherNumber)
             return 0;
-        return m_numberValue > other.m_numberValue ? 1 : -1;
+        return number > otherNumber ? 1 : -1;
+    }
     case KeyType::Max:
     case KeyType::Min:
         return 0;
@@ -271,9 +289,10 @@
     case KeyType::Array: {
         StringBuilder builder;
         builder.appendLiteral("<array> - { ");
-        for (size_t i = 0; i < m_arrayValue.size(); ++i) {
-            builder.append(m_arrayValue[i].loggingString());
-            if (i < m_arrayValue.size() - 1)
+        auto& array = WTF::get<Vector<IDBKeyData>>(m_value);
+        for (size_t i = 0; i < array.size(); ++i) {
+            builder.append(array[i].loggingString());
+            if (i < array.size() - 1)
                 builder.appendLiteral(", ");
         }
         builder.appendLiteral(" }");
@@ -281,12 +300,12 @@
         break;
     }
     case KeyType::String:
-        result = "<string> - " + m_stringValue;
+        result = "<string> - " + WTF::get<String>(m_value);
         break;
     case KeyType::Date:
-        return String::format("<date> - %f", m_numberValue);
+        return String::format("<date> - %f", WTF::get<double>(m_value));
     case KeyType::Number:
-        return String::format("<number> - %f", m_numberValue);
+        return String::format("<number> - %f", WTF::get<double>(m_value));
     case KeyType::Max:
         return "<maximum>";
     case KeyType::Min:
@@ -307,7 +326,7 @@
 void IDBKeyData::setArrayValue(const Vector<IDBKeyData>& value)
 {
     *this = IDBKeyData();
-    m_arrayValue = value;
+    m_value = value;
     m_type = KeyType::Array;
     m_isNull = false;
 }
@@ -315,7 +334,7 @@
 void IDBKeyData::setStringValue(const String& value)
 {
     *this = IDBKeyData();
-    m_stringValue = value;
+    m_value = value;
     m_type = KeyType::String;
     m_isNull = false;
 }
@@ -323,7 +342,7 @@
 void IDBKeyData::setDateValue(double value)
 {
     *this = IDBKeyData();
-    m_numberValue = value;
+    m_value = value;
     m_type = KeyType::Date;
     m_isNull = false;
 }
@@ -331,7 +350,7 @@
 void IDBKeyData::setNumberValue(double value)
 {
     *this = IDBKeyData();
-    m_numberValue = value;
+    m_value = value;
     m_type = KeyType::Number;
     m_isNull = false;
 }
@@ -360,11 +379,11 @@
         return true;
     case KeyType::Number:
     case KeyType::Date:
-        return m_numberValue == other.m_numberValue;
+        return WTF::get<double>(m_value) == WTF::get<double>(other.m_value);
     case KeyType::String:
-        return m_stringValue == other.m_stringValue;
+        return WTF::get<String>(m_value) == WTF::get<String>(other.m_value);
     case KeyType::Array:
-        return m_arrayValue == other.m_arrayValue;
+        return WTF::get<Vector<IDBKeyData>>(m_value) == WTF::get<Vector<IDBKeyData>>(other.m_value);
     }
     RELEASE_ASSERT_NOT_REACHED();
 }

Modified: trunk/Source/WebCore/Modules/indexeddb/IDBKeyData.h (208309 => 208310)


--- trunk/Source/WebCore/Modules/indexeddb/IDBKeyData.h	2016-11-02 22:19:39 UTC (rev 208309)
+++ trunk/Source/WebCore/Modules/indexeddb/IDBKeyData.h	2016-11-02 23:04:22 UTC (rev 208310)
@@ -29,6 +29,7 @@
 #if ENABLE(INDEXED_DATABASE)
 
 #include "IDBKey.h"
+#include <wtf/Variant.h>
 #include <wtf/text/StringHash.h>
 
 namespace WebCore {
@@ -114,13 +115,13 @@
             break;
         case KeyType::Number:
         case KeyType::Date:
-            hashCodes.append(StringHasher::hashMemory<sizeof(double)>(&m_numberValue));
+            hashCodes.append(StringHasher::hashMemory<sizeof(double)>(&WTF::get<double>(m_value)));
             break;
         case KeyType::String:
-            hashCodes.append(StringHash::hash(m_stringValue));
+            hashCodes.append(StringHash::hash(WTF::get<String>(m_value)));
             break;
         case KeyType::Array:
-            for (auto& key : m_arrayValue)
+            for (auto& key : WTF::get<Vector<IDBKeyData>>(m_value))
                 hashCodes.append(key.hash());
             break;
         }
@@ -134,25 +135,25 @@
     String string() const
     {
         ASSERT(m_type == KeyType::String);
-        return m_stringValue;
+        return WTF::get<String>(m_value);
     }
 
     double date() const
     {
         ASSERT(m_type == KeyType::Date);
-        return m_numberValue;
+        return WTF::get<double>(m_value);
     }
 
     double number() const
     {
         ASSERT(m_type == KeyType::Number);
-        return m_numberValue;
+        return WTF::get<double>(m_value);
     }
 
     const Vector<IDBKeyData>& array() const
     {
         ASSERT(m_type == KeyType::Array);
-        return m_arrayValue;
+        return WTF::get<Vector<IDBKeyData>>(m_value);
     }
 
 private:
@@ -159,9 +160,8 @@
     static void isolatedCopy(const IDBKeyData& source, IDBKeyData& destination);
 
     KeyType m_type;
-    Vector<IDBKeyData> m_arrayValue;
-    String m_stringValue;
-    double m_numberValue { 0 };
+    Variant<Vector<IDBKeyData>, String, double> m_value;
+
     bool m_isNull { false };
     bool m_isDeletedValue { false };
 };
@@ -212,14 +212,14 @@
     case KeyType::Min:
         break;
     case KeyType::Array:
-        encoder << m_arrayValue;
+        encoder << WTF::get<Vector<IDBKeyData>>(m_value);
         break;
     case KeyType::String:
-        encoder << m_stringValue;
+        encoder << WTF::get<String>(m_value);
         break;
     case KeyType::Date:
     case KeyType::Number:
-        encoder << m_numberValue;
+        encoder << WTF::get<double>(m_value);
         break;
     }
 }
@@ -242,16 +242,19 @@
     case KeyType::Min:
         break;
     case KeyType::Array:
-        if (!decoder.decode(keyData.m_arrayValue))
+        keyData.m_value = Vector<IDBKeyData>();
+        if (!decoder.decode(WTF::get<Vector<IDBKeyData>>(keyData.m_value)))
             return false;
         break;
     case KeyType::String:
-        if (!decoder.decode(keyData.m_stringValue))
+        keyData.m_value = String();
+        if (!decoder.decode(WTF::get<String>(keyData.m_value)))
             return false;
         break;
     case KeyType::Date:
     case KeyType::Number:
-        if (!decoder.decode(keyData.m_numberValue))
+        keyData.m_value = 0.0;
+        if (!decoder.decode(WTF::get<double>(keyData.m_value)))
             return false;
         break;
     }
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to