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. ``AS IS'' AND ANY |
14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #pragma once |
27 | |
28 | #if ENABLE(INDEXED_DATABASE) |
29 | |
30 | #include "IDBCursorInfo.h" |
31 | #include "IDBKeyData.h" |
32 | #include "IndexValueEntry.h" |
33 | #include <wtf/HashMap.h> |
34 | |
35 | namespace WebCore { |
36 | |
37 | class IDBError; |
38 | |
39 | struct IDBKeyRangeData; |
40 | |
41 | namespace IDBServer { |
42 | |
43 | class MemoryIndex; |
44 | |
45 | typedef HashMap<IDBKeyData, std::unique_ptr<IndexValueEntry>, IDBKeyDataHash, IDBKeyDataHashTraits> IndexKeyValueMap; |
46 | |
47 | class IndexValueStore { |
48 | WTF_MAKE_FAST_ALLOCATED; |
49 | public: |
50 | IndexValueStore(bool unique); |
51 | |
52 | const IDBKeyData* lowestValueForKey(const IDBKeyData&) const; |
53 | Vector<IDBKeyData> allValuesForKey(const IDBKeyData&, uint32_t limit) const; |
54 | uint64_t countForKey(const IDBKeyData&) const; |
55 | IDBKeyData lowestKeyWithRecordInRange(const IDBKeyRangeData&) const; |
56 | bool contains(const IDBKeyData&) const; |
57 | |
58 | IDBError addRecord(const IDBKeyData& indexKey, const IDBKeyData& valueKey); |
59 | void removeRecord(const IDBKeyData& indexKey, const IDBKeyData& valueKey); |
60 | |
61 | void removeEntriesWithValueKey(MemoryIndex&, const IDBKeyData& valueKey); |
62 | |
63 | class Iterator { |
64 | friend class IndexValueStore; |
65 | public: |
66 | Iterator() |
67 | { |
68 | } |
69 | |
70 | Iterator(IndexValueStore&, IDBKeyDataSet::iterator, IndexValueEntry::Iterator); |
71 | Iterator(IndexValueStore&, CursorDuplicity, IDBKeyDataSet::reverse_iterator, IndexValueEntry::Iterator); |
72 | |
73 | void invalidate(); |
74 | bool isValid(); |
75 | |
76 | const IDBKeyData& key(); |
77 | const IDBKeyData& primaryKey(); |
78 | const ThreadSafeDataBuffer& value(); |
79 | |
80 | Iterator& operator++(); |
81 | Iterator& nextIndexEntry(); |
82 | |
83 | private: |
84 | IndexValueStore* m_store { nullptr }; |
85 | bool m_forward { true }; |
86 | CursorDuplicity m_duplicity { CursorDuplicity::Duplicates }; |
87 | IDBKeyDataSet::iterator m_forwardIterator; |
88 | IDBKeyDataSet::reverse_iterator m_reverseIterator; |
89 | |
90 | IndexValueEntry::Iterator m_primaryKeyIterator; |
91 | }; |
92 | |
93 | // Returns an iterator pointing to the first primaryKey record in the requested key, or the next key if it doesn't exist. |
94 | Iterator find(const IDBKeyData&, bool open = false); |
95 | Iterator reverseFind(const IDBKeyData&, CursorDuplicity, bool open = false); |
96 | |
97 | // Returns an iterator pointing to the key/primaryKey record, or the next one after it if it doesn't exist. |
98 | Iterator find(const IDBKeyData&, const IDBKeyData& primaryKey); |
99 | Iterator reverseFind(const IDBKeyData&, const IDBKeyData& primaryKey, CursorDuplicity); |
100 | |
101 | #if !LOG_DISABLED |
102 | String loggingString() const; |
103 | #endif |
104 | |
105 | private: |
106 | IDBKeyDataSet::iterator lowestIteratorInRange(const IDBKeyRangeData&) const; |
107 | IDBKeyDataSet::reverse_iterator highestReverseIteratorInRange(const IDBKeyRangeData&) const; |
108 | |
109 | IndexKeyValueMap m_records; |
110 | IDBKeyDataSet m_orderedKeys; |
111 | |
112 | bool m_unique; |
113 | }; |
114 | |
115 | } // namespace IDBServer |
116 | } // namespace WebCore |
117 | |
118 | #endif // ENABLE(INDEXED_DATABASE) |
119 | |