diff options
author | Lorry Tar Creator <lorry-tar-importer@lorry> | 2017-06-27 06:07:23 +0000 |
---|---|---|
committer | Lorry Tar Creator <lorry-tar-importer@lorry> | 2017-06-27 06:07:23 +0000 |
commit | 1bf1084f2b10c3b47fd1a588d85d21ed0eb41d0c (patch) | |
tree | 46dcd36c86e7fbc6e5df36deb463b33e9967a6f7 /Source/WebCore/Modules/indexeddb/IDBKey.h | |
parent | 32761a6cee1d0dee366b885b7b9c777e67885688 (diff) | |
download | WebKitGtk-tarball-master.tar.gz |
webkitgtk-2.16.5HEADwebkitgtk-2.16.5master
Diffstat (limited to 'Source/WebCore/Modules/indexeddb/IDBKey.h')
-rw-r--r-- | Source/WebCore/Modules/indexeddb/IDBKey.h | 172 |
1 files changed, 111 insertions, 61 deletions
diff --git a/Source/WebCore/Modules/indexeddb/IDBKey.h b/Source/WebCore/Modules/indexeddb/IDBKey.h index 90b62d599..bc5ed5032 100644 --- a/Source/WebCore/Modules/indexeddb/IDBKey.h +++ b/Source/WebCore/Modules/indexeddb/IDBKey.h @@ -23,123 +23,130 @@ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#ifndef IDBKey_h -#define IDBKey_h +#pragma once #if ENABLE(INDEXED_DATABASE) +#include "IndexedDB.h" +#include "ThreadSafeDataBuffer.h" #include <wtf/Forward.h> #include <wtf/RefCounted.h> +#include <wtf/Variant.h> #include <wtf/Vector.h> #include <wtf/text/WTFString.h> +using WebCore::IndexedDB::KeyType; + +namespace JSC { +class JSArrayBuffer; +class JSArrayBufferView; +} + namespace WebCore { class IDBKey : public RefCounted<IDBKey> { public: - typedef Vector<RefPtr<IDBKey>> KeyArray; - - static PassRefPtr<IDBKey> createInvalid() + static Ref<IDBKey> createInvalid() { - return adoptRef(new IDBKey()); + return adoptRef(*new IDBKey()); } - static PassRefPtr<IDBKey> createNumber(double number) + static Ref<IDBKey> createNumber(double number) { - return adoptRef(new IDBKey(NumberType, number)); + return adoptRef(*new IDBKey(KeyType::Number, number)); } - static PassRefPtr<IDBKey> createString(const String& string) + static Ref<IDBKey> createString(const String& string) { - return adoptRef(new IDBKey(string)); + return adoptRef(*new IDBKey(string)); } - static PassRefPtr<IDBKey> createDate(double date) + static Ref<IDBKey> createDate(double date) { - return adoptRef(new IDBKey(DateType, date)); + return adoptRef(*new IDBKey(KeyType::Date, date)); } - static PassRefPtr<IDBKey> createMultiEntryArray(const KeyArray& array) + static Ref<IDBKey> createMultiEntryArray(const Vector<RefPtr<IDBKey>>& array) { - KeyArray result; + Vector<RefPtr<IDBKey>> result; size_t sizeEstimate = 0; - for (size_t i = 0; i < array.size(); i++) { - if (!array[i]->isValid()) + for (auto& key : array) { + if (!key->isValid()) continue; bool skip = false; - for (size_t j = 0; j < result.size(); j++) { - if (array[i]->isEqual(result[j].get())) { + for (auto& resultKey : result) { + if (key->isEqual(*resultKey)) { skip = true; break; } } if (!skip) { - result.append(array[i]); - sizeEstimate += array[i]->m_sizeEstimate; + result.append(key); + sizeEstimate += key->m_sizeEstimate; } } - RefPtr<IDBKey> idbKey = adoptRef(new IDBKey(result, sizeEstimate)); + Ref<IDBKey> idbKey = adoptRef(*new IDBKey(result, sizeEstimate)); ASSERT(idbKey->isValid()); - return idbKey.release(); + return idbKey; } - static PassRefPtr<IDBKey> createArray(const KeyArray& array) + static Ref<IDBKey> createArray(const Vector<RefPtr<IDBKey>>& array) { size_t sizeEstimate = 0; - for (size_t i = 0; i < array.size(); ++i) - sizeEstimate += array[i]->m_sizeEstimate; + for (auto& key : array) + sizeEstimate += key->m_sizeEstimate; - return adoptRef(new IDBKey(array, sizeEstimate)); + return adoptRef(*new IDBKey(array, sizeEstimate)); } - ~IDBKey(); + static Ref<IDBKey> createBinary(const ThreadSafeDataBuffer&); + static Ref<IDBKey> createBinary(JSC::JSArrayBuffer&); + static Ref<IDBKey> createBinary(JSC::JSArrayBufferView&); - // In order of the least to the highest precedent in terms of sort order. - enum Type { - InvalidType = 0, - ArrayType, - StringType, - DateType, - NumberType, - MinType - }; + WEBCORE_EXPORT ~IDBKey(); - Type type() const { return m_type; } - bool isValid() const; + KeyType type() const { return m_type; } + WEBCORE_EXPORT bool isValid() const; - const KeyArray& array() const + const Vector<RefPtr<IDBKey>>& array() const { - ASSERT(m_type == ArrayType); - return m_array; + ASSERT(m_type == KeyType::Array); + return WTF::get<Vector<RefPtr<IDBKey>>>(m_value); } const String& string() const { - ASSERT(m_type == StringType); - return m_string; + ASSERT(m_type == KeyType::String); + return WTF::get<String>(m_value); } double date() const { - ASSERT(m_type == DateType); - return m_number; + ASSERT(m_type == KeyType::Date); + return WTF::get<double>(m_value); } double number() const { - ASSERT(m_type == NumberType); - return m_number; + ASSERT(m_type == KeyType::Number); + return WTF::get<double>(m_value); + } + + const ThreadSafeDataBuffer& binary() const + { + ASSERT(m_type == KeyType::Binary); + return WTF::get<ThreadSafeDataBuffer>(m_value); } - int compare(const IDBKey* other) const; - bool isLessThan(const IDBKey* other) const; - bool isEqual(const IDBKey* other) const; + int compare(const IDBKey& other) const; + bool isLessThan(const IDBKey& other) const; + bool isEqual(const IDBKey& other) const; size_t sizeEstimate() const { return m_sizeEstimate; } - static int compareTypes(Type a, Type b) + static int compareTypes(KeyType a, KeyType b) { return b - a; } @@ -147,16 +154,24 @@ public: using RefCounted<IDBKey>::ref; using RefCounted<IDBKey>::deref; +#if !LOG_DISABLED + String loggingString() const; +#endif + private: - IDBKey() : m_type(InvalidType), m_number(0), m_sizeEstimate(OverheadSize) { } - IDBKey(Type type, double number) : m_type(type), m_number(number), m_sizeEstimate(OverheadSize + sizeof(double)) { } - explicit IDBKey(const String& value) : m_type(StringType), m_string(value), m_number(0), m_sizeEstimate(OverheadSize + value.length() * sizeof(UChar)) { } - IDBKey(const KeyArray& keyArray, size_t arraySize) : m_type(ArrayType), 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); + explicit IDBKey(const ThreadSafeDataBuffer&); - const Type m_type; - const KeyArray m_array; - const String m_string; - const double m_number; + const KeyType m_type; + Variant<Vector<RefPtr<IDBKey>>, String, double, ThreadSafeDataBuffer> m_value; const size_t m_sizeEstimate; @@ -164,8 +179,43 @@ private: enum { OverheadSize = 16 }; }; +inline int compareBinaryKeyData(const Vector<uint8_t>& a, const Vector<uint8_t>& b) +{ + size_t length = std::min(a.size(), b.size()); + + for (size_t i = 0; i < length; ++i) { + if (a[i] > b[i]) + return 1; + if (a[i] < b[i]) + return -1; + } + + if (a.size() == b.size()) + return 0; + + if (a.size() > b.size()) + return 1; + + return -1; } -#endif // ENABLE(INDEXED_DATABASE) +inline int compareBinaryKeyData(const ThreadSafeDataBuffer& a, const ThreadSafeDataBuffer& b) +{ + auto* aData = a.data(); + auto* bData = b.data(); + + // Covers the cases where both pointers are null as well as both pointing to the same buffer. + if (aData == bData) + return 0; -#endif // IDBKey_h + if (aData && !bData) + return 1; + if (!aData && bData) + return -1; + + return compareBinaryKeyData(*aData, *bData); +} + +} // namespace WebCore + +#endif // ENABLE(INDEXED_DATABASE) |