1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/common/indexed_db/indexed_db_key.h"
#include <string>
#include "base/logging.h"
namespace content {
using blink::WebIDBKey;
using blink::WebIDBKeyType;
using blink::WebIDBKeyTypeArray;
using blink::WebIDBKeyTypeBinary;
using blink::WebIDBKeyTypeDate;
using blink::WebIDBKeyTypeInvalid;
using blink::WebIDBKeyTypeMin;
using blink::WebIDBKeyTypeNull;
using blink::WebIDBKeyTypeNumber;
using blink::WebIDBKeyTypeString;
namespace {
// Very rough estimate of minimum key size overhead.
const size_t kOverheadSize = 16;
static size_t CalculateArraySize(const IndexedDBKey::KeyArray& keys) {
size_t size(0);
for (size_t i = 0; i < keys.size(); ++i)
size += keys[i].size_estimate();
return size;
}
template <typename T>
static IndexedDBKey::KeyArray CopyKeyArray(const T& array) {
IndexedDBKey::KeyArray result;
result.reserve(array.size());
for (size_t i = 0; i < array.size(); ++i) {
result.push_back(IndexedDBKey(array[i]));
}
return result;
}
} // namespace
IndexedDBKey::IndexedDBKey()
: type_(WebIDBKeyTypeNull),
date_(0),
number_(0),
size_estimate_(kOverheadSize) {}
IndexedDBKey::IndexedDBKey(WebIDBKeyType type)
: type_(type), date_(0), number_(0), size_estimate_(kOverheadSize) {
DCHECK(type == WebIDBKeyTypeNull || type == WebIDBKeyTypeInvalid);
}
IndexedDBKey::IndexedDBKey(double number, WebIDBKeyType type)
: type_(type),
date_(number),
number_(number),
size_estimate_(kOverheadSize + sizeof(number)) {
DCHECK(type == WebIDBKeyTypeNumber || type == WebIDBKeyTypeDate);
}
IndexedDBKey::IndexedDBKey(const KeyArray& keys)
: type_(WebIDBKeyTypeArray),
array_(CopyKeyArray(keys)),
date_(0),
number_(0),
size_estimate_(kOverheadSize + CalculateArraySize(keys)) {}
IndexedDBKey::IndexedDBKey(const std::string& key)
: type_(WebIDBKeyTypeBinary),
binary_(key),
size_estimate_(kOverheadSize +
(key.length() * sizeof(std::string::value_type))) {}
IndexedDBKey::IndexedDBKey(const base::string16& key)
: type_(WebIDBKeyTypeString),
string_(key),
size_estimate_(kOverheadSize +
(key.length() * sizeof(base::string16::value_type))) {}
IndexedDBKey::~IndexedDBKey() {}
int IndexedDBKey::Compare(const IndexedDBKey& other) const {
DCHECK(IsValid());
DCHECK(other.IsValid());
if (type_ != other.type_)
return type_ > other.type_ ? -1 : 1;
switch (type_) {
case WebIDBKeyTypeArray:
for (size_t i = 0; i < array_.size() && i < other.array_.size(); ++i) {
if (int result = array_[i].Compare(other.array_[i]))
return result;
}
if (array_.size() < other.array_.size())
return -1;
if (array_.size() > other.array_.size())
return 1;
return 0;
case WebIDBKeyTypeBinary:
return binary_.compare(other.binary_);
case WebIDBKeyTypeString:
return string_.compare(other.string_);
case WebIDBKeyTypeDate:
return (date_ < other.date_) ? -1 : (date_ > other.date_) ? 1 : 0;
case WebIDBKeyTypeNumber:
return (number_ < other.number_) ? -1 : (number_ > other.number_) ? 1 : 0;
case WebIDBKeyTypeInvalid:
case WebIDBKeyTypeNull:
case WebIDBKeyTypeMin:
default:
NOTREACHED();
return 0;
}
NOTREACHED();
return 0;
}
bool IndexedDBKey::IsLessThan(const IndexedDBKey& other) const {
return Compare(other) < 0;
}
bool IndexedDBKey::IsEqual(const IndexedDBKey& other) const {
return !Compare(other);
}
bool IndexedDBKey::IsValid() const {
if (type_ == WebIDBKeyTypeInvalid || type_ == WebIDBKeyTypeNull)
return false;
if (type_ == WebIDBKeyTypeArray) {
for (size_t i = 0; i < array_.size(); i++) {
if (!array_[i].IsValid())
return false;
}
}
return true;
}
} // namespace content
|