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"
8 #include "base/logging.h"
12 using WebKit::WebIDBKey;
13 using WebKit::WebIDBKeyType;
14 using WebKit::WebIDBKeyTypeArray;
15 using WebKit::WebIDBKeyTypeDate;
16 using WebKit::WebIDBKeyTypeInvalid;
17 using WebKit::WebIDBKeyTypeMin;
18 using WebKit::WebIDBKeyTypeNull;
19 using WebKit::WebIDBKeyTypeNumber;
20 using WebKit::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 static IndexedDBKey::KeyArray CopyKeyArray(const T& array) {
36 IndexedDBKey::KeyArray result;
37 result.reserve(array.size());
38 for (size_t i = 0; i < array.size(); ++i) {
39 result.push_back(IndexedDBKey(array[i]));
46 IndexedDBKey::IndexedDBKey()
47 : type_(WebIDBKeyTypeNull),
50 size_estimate_(kOverheadSize) {}
52 IndexedDBKey::IndexedDBKey(WebIDBKeyType type)
53 : type_(type), date_(0), number_(0), size_estimate_(kOverheadSize) {
54 DCHECK(type == WebIDBKeyTypeNull || type == WebIDBKeyTypeInvalid);
57 IndexedDBKey::IndexedDBKey(double number, WebIDBKeyType type)
61 size_estimate_(kOverheadSize + sizeof(number)) {
62 DCHECK(type == WebIDBKeyTypeNumber || type == WebIDBKeyTypeDate);
65 IndexedDBKey::IndexedDBKey(const KeyArray& keys)
66 : type_(WebIDBKeyTypeArray),
67 array_(CopyKeyArray(keys)),
70 size_estimate_(kOverheadSize + CalculateArraySize(keys)) {}
72 IndexedDBKey::IndexedDBKey(const string16& key)
73 : type_(WebIDBKeyTypeString),
75 size_estimate_(kOverheadSize +
76 (key.length() * sizeof(string16::value_type))) {}
78 IndexedDBKey::~IndexedDBKey() {}
80 int IndexedDBKey::Compare(const IndexedDBKey& other) const {
82 DCHECK(other.IsValid());
83 if (type_ != other.type_)
84 return type_ > other.type_ ? -1 : 1;
87 case WebIDBKeyTypeArray:
88 for (size_t i = 0; i < array_.size() && i < other.array_.size(); ++i) {
89 if (int result = array_[i].Compare(other.array_[i]))
92 if (array_.size() < other.array_.size())
94 if (array_.size() > other.array_.size())
97 case WebIDBKeyTypeString:
98 return -other.string_.compare(string_);
99 case WebIDBKeyTypeDate:
100 return (date_ < other.date_) ? -1 : (date_ > other.date_) ? 1 : 0;
101 case WebIDBKeyTypeNumber:
102 return (number_ < other.number_) ? -1 : (number_ > other.number_) ? 1 : 0;
103 case WebIDBKeyTypeInvalid:
104 case WebIDBKeyTypeNull:
105 case WebIDBKeyTypeMin:
114 bool IndexedDBKey::IsLessThan(const IndexedDBKey& other) const {
115 return Compare(other) < 0;
118 bool IndexedDBKey::IsEqual(const IndexedDBKey& other) const {
119 return !Compare(other);
122 bool IndexedDBKey::IsValid() const {
123 if (type_ == WebIDBKeyTypeInvalid || type_ == WebIDBKeyTypeNull)
126 if (type_ == WebIDBKeyTypeArray) {
127 for (size_t i = 0; i < array_.size(); i++) {
128 if (!array_[i].IsValid())
136 } // namespace content