1/*
2 * Copyright (C) 2014 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 "IDBKeyRangeData.h"
28
29#if ENABLE(INDEXED_DATABASE)
30
31#include "IDBKey.h"
32
33namespace WebCore {
34
35IDBKeyRangeData::IDBKeyRangeData(IDBKey* key)
36 : isNull(!key)
37 , lowerKey(key)
38 , upperKey(key)
39 , lowerOpen(false)
40 , upperOpen(false)
41{
42}
43
44IDBKeyRangeData::IDBKeyRangeData(const IDBKeyData& keyData)
45 : isNull(keyData.isNull())
46 , lowerKey(keyData)
47 , upperKey(keyData)
48 , lowerOpen(false)
49 , upperOpen(false)
50{
51}
52
53IDBKeyRangeData IDBKeyRangeData::isolatedCopy() const
54{
55 IDBKeyRangeData result;
56
57 result.isNull = isNull;
58 result.lowerKey = lowerKey.isolatedCopy();
59 result.upperKey = upperKey.isolatedCopy();
60 result.lowerOpen = lowerOpen;
61 result.upperOpen = upperOpen;
62
63 return result;
64}
65
66RefPtr<IDBKeyRange> IDBKeyRangeData::maybeCreateIDBKeyRange() const
67{
68 if (isNull)
69 return nullptr;
70
71 return IDBKeyRange::create(lowerKey.maybeCreateIDBKey(), upperKey.maybeCreateIDBKey(), lowerOpen, upperOpen);
72}
73
74bool IDBKeyRangeData::isExactlyOneKey() const
75{
76 if (isNull || lowerOpen || upperOpen || !upperKey.isValid() || !lowerKey.isValid())
77 return false;
78
79 return !lowerKey.compare(upperKey);
80}
81
82bool IDBKeyRangeData::containsKey(const IDBKeyData& key) const
83{
84 if (lowerKey.isValid()) {
85 auto compare = lowerKey.compare(key);
86 if (compare > 0)
87 return false;
88 if (lowerOpen && !compare)
89 return false;
90 }
91 if (upperKey.isValid()) {
92 auto compare = upperKey.compare(key);
93 if (compare < 0)
94 return false;
95 if (upperOpen && !compare)
96 return false;
97 }
98
99 return true;
100}
101
102bool IDBKeyRangeData::isValid() const
103{
104 if (isNull)
105 return false;
106
107 if (!lowerKey.isValid() && !lowerKey.isNull())
108 return false;
109
110 if (!upperKey.isValid() && !upperKey.isNull())
111 return false;
112
113 return true;
114}
115
116#if !LOG_DISABLED
117String IDBKeyRangeData::loggingString() const
118{
119 auto result = makeString(lowerOpen ? "( " : "[ ", lowerKey.loggingString(), ", ", upperKey.loggingString(), upperOpen ? " )" : " ]");
120 if (result.length() > 400) {
121 result.truncate(397);
122 result.append("..."_s);
123 }
124
125 return result;
126}
127#endif
128
129} // namespace WebCore
130
131#endif // ENABLE(INDEXED_DATABASE)
132