- add sources.
[platform/framework/web/crosswalk.git] / src / content / common / indexed_db / indexed_db_key.cc
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.
4
5 #include "content/common/indexed_db/indexed_db_key.h"
6
7 #include <string>
8 #include "base/logging.h"
9
10 namespace content {
11
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;
21
22 namespace {
23
24 // Very rough estimate of minimum key size overhead.
25 const size_t kOverheadSize = 16;
26
27 static size_t CalculateArraySize(const IndexedDBKey::KeyArray& keys) {
28   size_t size(0);
29   for (size_t i = 0; i < keys.size(); ++i)
30     size += keys[i].size_estimate();
31   return size;
32 }
33
34 template <typename T>
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]));
40   }
41   return result;
42 }
43
44 }  // namespace
45
46 IndexedDBKey::IndexedDBKey()
47     : type_(WebIDBKeyTypeNull),
48       date_(0),
49       number_(0),
50       size_estimate_(kOverheadSize) {}
51
52 IndexedDBKey::IndexedDBKey(WebIDBKeyType type)
53     : type_(type), date_(0), number_(0), size_estimate_(kOverheadSize) {
54   DCHECK(type == WebIDBKeyTypeNull || type == WebIDBKeyTypeInvalid);
55 }
56
57 IndexedDBKey::IndexedDBKey(double number, WebIDBKeyType type)
58     : type_(type),
59       date_(number),
60       number_(number),
61       size_estimate_(kOverheadSize + sizeof(number)) {
62   DCHECK(type == WebIDBKeyTypeNumber || type == WebIDBKeyTypeDate);
63 }
64
65 IndexedDBKey::IndexedDBKey(const KeyArray& keys)
66     : type_(WebIDBKeyTypeArray),
67       array_(CopyKeyArray(keys)),
68       date_(0),
69       number_(0),
70       size_estimate_(kOverheadSize + CalculateArraySize(keys)) {}
71
72 IndexedDBKey::IndexedDBKey(const string16& key)
73     : type_(WebIDBKeyTypeString),
74       string_(key),
75       size_estimate_(kOverheadSize +
76                      (key.length() * sizeof(string16::value_type))) {}
77
78 IndexedDBKey::~IndexedDBKey() {}
79
80 int IndexedDBKey::Compare(const IndexedDBKey& other) const {
81   DCHECK(IsValid());
82   DCHECK(other.IsValid());
83   if (type_ != other.type_)
84     return type_ > other.type_ ? -1 : 1;
85
86   switch (type_) {
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]))
90           return result;
91       }
92       if (array_.size() < other.array_.size())
93         return -1;
94       if (array_.size() > other.array_.size())
95         return 1;
96       return 0;
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:
106     default:
107       NOTREACHED();
108       return 0;
109   }
110   NOTREACHED();
111   return 0;
112 }
113
114 bool IndexedDBKey::IsLessThan(const IndexedDBKey& other) const {
115   return Compare(other) < 0;
116 }
117
118 bool IndexedDBKey::IsEqual(const IndexedDBKey& other) const {
119   return !Compare(other);
120 }
121
122 bool IndexedDBKey::IsValid() const {
123   if (type_ == WebIDBKeyTypeInvalid || type_ == WebIDBKeyTypeNull)
124     return false;
125
126   if (type_ == WebIDBKeyTypeArray) {
127     for (size_t i = 0; i < array_.size(); i++) {
128       if (!array_[i].IsValid())
129         return false;
130     }
131   }
132
133   return true;
134 }
135
136 }  // namespace content