| 1 | /* |
| 2 | * Copyright (C) 2015 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 23 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | #include "IDBObjectStoreInfo.h" |
| 28 | #include <wtf/text/StringBuilder.h> |
| 29 | |
| 30 | #if ENABLE(INDEXED_DATABASE) |
| 31 | |
| 32 | namespace WebCore { |
| 33 | |
| 34 | IDBObjectStoreInfo::IDBObjectStoreInfo() |
| 35 | { |
| 36 | } |
| 37 | |
| 38 | IDBObjectStoreInfo::IDBObjectStoreInfo(uint64_t identifier, const String& name, Optional<IDBKeyPath>&& keyPath, bool autoIncrement) |
| 39 | : m_identifier(identifier) |
| 40 | , m_name(name) |
| 41 | , m_keyPath(WTFMove(keyPath)) |
| 42 | , m_autoIncrement(autoIncrement) |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | IDBIndexInfo IDBObjectStoreInfo::createNewIndex(const String& name, IDBKeyPath&& keyPath, bool unique, bool multiEntry) |
| 47 | { |
| 48 | IDBIndexInfo info(++m_maxIndexID, m_identifier, name, WTFMove(keyPath), unique, multiEntry); |
| 49 | m_indexMap.set(info.identifier(), info); |
| 50 | return info; |
| 51 | } |
| 52 | |
| 53 | void IDBObjectStoreInfo::addExistingIndex(const IDBIndexInfo& info) |
| 54 | { |
| 55 | ASSERT(!m_indexMap.contains(info.identifier())); |
| 56 | |
| 57 | if (info.identifier() > m_maxIndexID) |
| 58 | m_maxIndexID = info.identifier(); |
| 59 | |
| 60 | m_indexMap.set(info.identifier(), info); |
| 61 | } |
| 62 | |
| 63 | bool IDBObjectStoreInfo::hasIndex(const String& name) const |
| 64 | { |
| 65 | for (auto& index : m_indexMap.values()) { |
| 66 | if (index.name() == name) |
| 67 | return true; |
| 68 | } |
| 69 | |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | bool IDBObjectStoreInfo::hasIndex(uint64_t indexIdentifier) const |
| 74 | { |
| 75 | return m_indexMap.contains(indexIdentifier); |
| 76 | } |
| 77 | |
| 78 | IDBIndexInfo* IDBObjectStoreInfo::infoForExistingIndex(const String& name) |
| 79 | { |
| 80 | for (auto& index : m_indexMap.values()) { |
| 81 | if (index.name() == name) |
| 82 | return &index; |
| 83 | } |
| 84 | |
| 85 | return nullptr; |
| 86 | } |
| 87 | |
| 88 | IDBIndexInfo* IDBObjectStoreInfo::infoForExistingIndex(uint64_t identifier) |
| 89 | { |
| 90 | auto iterator = m_indexMap.find(identifier); |
| 91 | if (iterator == m_indexMap.end()) |
| 92 | return nullptr; |
| 93 | |
| 94 | return &iterator->value; |
| 95 | } |
| 96 | |
| 97 | IDBObjectStoreInfo IDBObjectStoreInfo::isolatedCopy() const |
| 98 | { |
| 99 | IDBObjectStoreInfo result = { m_identifier, m_name.isolatedCopy(), WebCore::isolatedCopy(m_keyPath), m_autoIncrement }; |
| 100 | |
| 101 | for (auto& iterator : m_indexMap) { |
| 102 | result.m_indexMap.set(iterator.key, iterator.value.isolatedCopy()); |
| 103 | if (iterator.key > result.m_maxIndexID) |
| 104 | result.m_maxIndexID = iterator.key; |
| 105 | } |
| 106 | |
| 107 | ASSERT(result.m_maxIndexID == m_maxIndexID); |
| 108 | |
| 109 | return result; |
| 110 | } |
| 111 | |
| 112 | Vector<String> IDBObjectStoreInfo::indexNames() const |
| 113 | { |
| 114 | Vector<String> names; |
| 115 | names.reserveCapacity(m_indexMap.size()); |
| 116 | for (auto& index : m_indexMap.values()) |
| 117 | names.uncheckedAppend(index.name()); |
| 118 | |
| 119 | return names; |
| 120 | } |
| 121 | |
| 122 | void IDBObjectStoreInfo::deleteIndex(const String& indexName) |
| 123 | { |
| 124 | auto* info = infoForExistingIndex(indexName); |
| 125 | if (!info) |
| 126 | return; |
| 127 | |
| 128 | m_indexMap.remove(info->identifier()); |
| 129 | } |
| 130 | |
| 131 | void IDBObjectStoreInfo::deleteIndex(uint64_t indexIdentifier) |
| 132 | { |
| 133 | m_indexMap.remove(indexIdentifier); |
| 134 | } |
| 135 | |
| 136 | #if !LOG_DISABLED |
| 137 | String IDBObjectStoreInfo::loggingString(int indent) const |
| 138 | { |
| 139 | StringBuilder builder; |
| 140 | for (int i = 0; i < indent; ++i) |
| 141 | builder.append(' '); |
| 142 | |
| 143 | builder.appendLiteral("Object store: " ); |
| 144 | builder.append(m_name); |
| 145 | builder.appendNumber(m_identifier); |
| 146 | for (auto index : m_indexMap.values()) { |
| 147 | builder.append(index.loggingString(indent + 1)); |
| 148 | builder.append('\n'); |
| 149 | } |
| 150 | |
| 151 | return builder.toString(); |
| 152 | } |
| 153 | |
| 154 | String IDBObjectStoreInfo::condensedLoggingString() const |
| 155 | { |
| 156 | return makeString("<OS: " , m_name, " (" , m_identifier, ")>" ); |
| 157 | } |
| 158 | |
| 159 | #endif |
| 160 | |
| 161 | } // namespace WebCore |
| 162 | |
| 163 | #endif // ENABLE(INDEXED_DATABASE) |
| 164 | |