1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/common/indexed_db/indexed_db_key.h"
11 using blink::WebIDBKey
;
12 using blink::WebIDBKeyType
;
13 using blink::WebIDBKeyTypeArray
;
14 using blink::WebIDBKeyTypeBinary
;
15 using blink::WebIDBKeyTypeDate
;
16 using blink::WebIDBKeyTypeInvalid
;
17 using blink::WebIDBKeyTypeMin
;
18 using blink::WebIDBKeyTypeNull
;
19 using blink::WebIDBKeyTypeNumber
;
20 using blink::WebIDBKeyTypeString
;
24 // Very rough estimate of minimum key size overhead.
25 const size_t kOverheadSize
= 16;
27 static size_t CalculateArraySize(const IndexedDBKey::KeyArray
& keys
) {
29 for (size_t i
= 0; i
< keys
.size(); ++i
)
30 size
+= keys
[i
].size_estimate();
35 int Compare(const T
& a
, const T
& b
) {
36 // Using '<' for both comparisons here is as generic as possible (for e.g.
37 // objects which only define operator<() and not operator>() or operator==())
38 // and also allows e.g. floating point NaNs to compare equal.
41 return (b
< a
) ? 1 : 0;
45 static IndexedDBKey::KeyArray
CopyKeyArray(const T
& array
) {
46 IndexedDBKey::KeyArray result
;
47 result
.reserve(array
.size());
48 for (size_t i
= 0; i
< array
.size(); ++i
) {
49 result
.push_back(IndexedDBKey(array
[i
]));
56 IndexedDBKey::IndexedDBKey()
57 : type_(WebIDBKeyTypeNull
),
58 size_estimate_(kOverheadSize
) {}
60 IndexedDBKey::IndexedDBKey(WebIDBKeyType type
)
61 : type_(type
), size_estimate_(kOverheadSize
) {
62 DCHECK(type
== WebIDBKeyTypeNull
|| type
== WebIDBKeyTypeInvalid
);
65 IndexedDBKey::IndexedDBKey(double number
, WebIDBKeyType type
)
68 size_estimate_(kOverheadSize
+ sizeof(number
)) {
69 DCHECK(type
== WebIDBKeyTypeNumber
|| type
== WebIDBKeyTypeDate
);
72 IndexedDBKey::IndexedDBKey(const KeyArray
& array
)
73 : type_(WebIDBKeyTypeArray
),
74 array_(CopyKeyArray(array
)),
75 size_estimate_(kOverheadSize
+ CalculateArraySize(array
)) {}
77 IndexedDBKey::IndexedDBKey(const std::string
& binary
)
78 : type_(WebIDBKeyTypeBinary
),
80 size_estimate_(kOverheadSize
+
81 (binary
.length() * sizeof(std::string::value_type
))) {}
83 IndexedDBKey::IndexedDBKey(const base::string16
& string
)
84 : type_(WebIDBKeyTypeString
),
86 size_estimate_(kOverheadSize
+
87 (string
.length() * sizeof(base::string16::value_type
))) {}
89 IndexedDBKey::IndexedDBKey(const IndexedDBKey
& other
) = default;
90 IndexedDBKey::~IndexedDBKey() = default;
91 IndexedDBKey
& IndexedDBKey::operator=(const IndexedDBKey
& other
) = default;
93 bool IndexedDBKey::IsValid() const {
94 if (type_
== WebIDBKeyTypeInvalid
|| type_
== WebIDBKeyTypeNull
)
97 if (type_
== WebIDBKeyTypeArray
) {
98 for (size_t i
= 0; i
< array_
.size(); i
++) {
99 if (!array_
[i
].IsValid())
107 bool IndexedDBKey::IsLessThan(const IndexedDBKey
& other
) const {
108 return CompareTo(other
) < 0;
111 bool IndexedDBKey::Equals(const IndexedDBKey
& other
) const {
112 return !CompareTo(other
);
115 int IndexedDBKey::CompareTo(const IndexedDBKey
& other
) const {
117 DCHECK(other
.IsValid());
118 if (type_
!= other
.type_
)
119 return type_
> other
.type_
? -1 : 1;
122 case WebIDBKeyTypeArray
:
123 for (size_t i
= 0; i
< array_
.size() && i
< other
.array_
.size(); ++i
) {
124 int result
= array_
[i
].CompareTo(other
.array_
[i
]);
128 return Compare(array_
.size(), other
.array_
.size());
129 case WebIDBKeyTypeBinary
:
130 return binary_
.compare(other
.binary_
);
131 case WebIDBKeyTypeString
:
132 return string_
.compare(other
.string_
);
133 case WebIDBKeyTypeDate
:
134 case WebIDBKeyTypeNumber
:
135 return Compare(number_
, other
.number_
);
136 case WebIDBKeyTypeInvalid
:
137 case WebIDBKeyTypeNull
:
138 case WebIDBKeyTypeMin
:
145 } // namespace content