OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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 #ifndef CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_LEVELDB_CODING_H_ |
| 6 #define CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_LEVELDB_CODING_H_ |
| 7 |
| 8 #include <vector> |
| 9 |
| 10 #include "base/basictypes.h" |
| 11 #include "base/logging.h" |
| 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/string16.h" |
| 15 #include "content/common/indexed_db/indexed_db_key.h" |
| 16 #include "content/common/indexed_db/indexed_db_key_path.h" |
| 17 |
| 18 namespace content { |
| 19 |
| 20 class LevelDBSlice; |
| 21 |
| 22 CONTENT_EXPORT extern const unsigned char kMinimumIndexId; |
| 23 |
| 24 CONTENT_EXPORT std::vector<char> EncodeByte(unsigned char); |
| 25 CONTENT_EXPORT const char* DecodeByte(const char* p, |
| 26 const char* limit, |
| 27 unsigned char& found_char); |
| 28 CONTENT_EXPORT std::vector<char> MaxIDBKey(); |
| 29 CONTENT_EXPORT std::vector<char> MinIDBKey(); |
| 30 CONTENT_EXPORT std::vector<char> EncodeBool(bool value); |
| 31 CONTENT_EXPORT bool DecodeBool(const char* begin, const char* end); |
| 32 CONTENT_EXPORT std::vector<char> EncodeInt(int64); |
| 33 inline std::vector<char> EncodeIntSafely(int64 nParam, int64 max) { |
| 34 DCHECK_LE(nParam, max); |
| 35 return EncodeInt(nParam); |
| 36 } |
| 37 |
| 38 template <class T> int64 DecodeInt(T begin, T end) { |
| 39 // TODO(alecflett): Make this a DCHECK_LE(). |
| 40 DCHECK(begin <= end); |
| 41 int64 ret = 0; |
| 42 |
| 43 int shift = 0; |
| 44 while (begin < end) { |
| 45 unsigned char c = *begin++; |
| 46 ret |= static_cast<int64>(c) << shift; |
| 47 shift += 8; |
| 48 } |
| 49 |
| 50 return ret; |
| 51 } |
| 52 |
| 53 CONTENT_EXPORT std::vector<char> EncodeVarInt(int64); |
| 54 CONTENT_EXPORT const char* DecodeVarInt(const char* p, |
| 55 const char* limit, |
| 56 int64& found_int); |
| 57 CONTENT_EXPORT std::vector<char> EncodeString(const string16& string); |
| 58 CONTENT_EXPORT string16 DecodeString(const char* p, const char* end); |
| 59 CONTENT_EXPORT std::vector<char> EncodeStringWithLength(const string16& string); |
| 60 CONTENT_EXPORT const char* DecodeStringWithLength(const char* p, |
| 61 const char* limit, |
| 62 string16& found_string); |
| 63 CONTENT_EXPORT int CompareEncodedStringsWithLength(const char*& p, |
| 64 const char* limit_p, |
| 65 const char*& q, |
| 66 const char* limit_q, |
| 67 bool& ok); |
| 68 CONTENT_EXPORT std::vector<char> EncodeDouble(double value); |
| 69 CONTENT_EXPORT const char* DecodeDouble(const char* p, |
| 70 const char* limit, |
| 71 double* value); |
| 72 void EncodeIDBKey(const IndexedDBKey& key, std::vector<char>& into); |
| 73 CONTENT_EXPORT std::vector<char> EncodeIDBKey(const IndexedDBKey& key); |
| 74 CONTENT_EXPORT const char* DecodeIDBKey(const char* p, |
| 75 const char* limit, |
| 76 scoped_ptr<IndexedDBKey>* found_key); |
| 77 CONTENT_EXPORT const char* ExtractEncodedIDBKey(const char* start, |
| 78 const char* limit, |
| 79 std::vector<char>* result); |
| 80 CONTENT_EXPORT int CompareEncodedIDBKeys(const std::vector<char>& a, |
| 81 const std::vector<char>& b, |
| 82 bool& ok); |
| 83 CONTENT_EXPORT std::vector<char> EncodeIDBKeyPath( |
| 84 const IndexedDBKeyPath& key_path); |
| 85 CONTENT_EXPORT IndexedDBKeyPath DecodeIDBKeyPath(const char* start, |
| 86 const char* limit); |
| 87 |
| 88 CONTENT_EXPORT int Compare(const LevelDBSlice& a, |
| 89 const LevelDBSlice& b, |
| 90 bool index_keys = false); |
| 91 |
| 92 class KeyPrefix { |
| 93 public: |
| 94 KeyPrefix(); |
| 95 explicit KeyPrefix(int64 database_id); |
| 96 KeyPrefix(int64 database_id, int64 object_store_id); |
| 97 KeyPrefix(int64 database_id, int64 object_store_id, int64 index_id); |
| 98 static KeyPrefix CreateWithSpecialIndex(int64 database_id, |
| 99 int64 object_store_id, |
| 100 int64 index_id); |
| 101 |
| 102 static const char* Decode(const char* start, |
| 103 const char* limit, |
| 104 KeyPrefix* result); |
| 105 std::vector<char> Encode() const; |
| 106 static std::vector<char> EncodeEmpty(); |
| 107 int Compare(const KeyPrefix& other) const; |
| 108 |
| 109 enum Type { |
| 110 GLOBAL_METADATA, |
| 111 DATABASE_METADATA, |
| 112 OBJECT_STORE_DATA, |
| 113 EXISTS_ENTRY, |
| 114 INDEX_DATA, |
| 115 INVALID_TYPE |
| 116 }; |
| 117 |
| 118 static const size_t kMaxDatabaseIdSizeBits = 3; |
| 119 static const size_t kMaxObjectStoreIdSizeBits = 3; |
| 120 static const size_t kMaxIndexIdSizeBits = 2; |
| 121 |
| 122 static const size_t kMaxDatabaseIdSizeBytes = |
| 123 1ULL << kMaxDatabaseIdSizeBits; // 8 |
| 124 static const size_t kMaxObjectStoreIdSizeBytes = |
| 125 1ULL << kMaxObjectStoreIdSizeBits; // 8 |
| 126 static const size_t kMaxIndexIdSizeBytes = 1ULL << kMaxIndexIdSizeBits; // 4 |
| 127 |
| 128 static const size_t kMaxDatabaseIdBits = |
| 129 kMaxDatabaseIdSizeBytes * 8 - 1; // 63 |
| 130 static const size_t kMaxObjectStoreIdBits = |
| 131 kMaxObjectStoreIdSizeBytes * 8 - 1; // 63 |
| 132 static const size_t kMaxIndexIdBits = kMaxIndexIdSizeBytes * 8 - 1; // 31 |
| 133 |
| 134 static const int64 kMaxDatabaseId = |
| 135 (1ULL << kMaxDatabaseIdBits) - 1; // max signed int64 |
| 136 static const int64 kMaxObjectStoreId = |
| 137 (1ULL << kMaxObjectStoreIdBits) - 1; // max signed int64 |
| 138 static const int64 kMaxIndexId = |
| 139 (1ULL << kMaxIndexIdBits) - 1; // max signed int32 |
| 140 |
| 141 static bool IsValidDatabaseId(int64 database_id); |
| 142 static bool IsValidObjectStoreId(int64 index_id); |
| 143 static bool IsValidIndexId(int64 index_id); |
| 144 static bool ValidIds(int64 database_id, |
| 145 int64 object_store_id, |
| 146 int64 index_id) { |
| 147 return IsValidDatabaseId(database_id) && |
| 148 IsValidObjectStoreId(object_store_id) && IsValidIndexId(index_id); |
| 149 } |
| 150 static bool ValidIds(int64 database_id, int64 object_store_id) { |
| 151 return IsValidDatabaseId(database_id) && |
| 152 IsValidObjectStoreId(object_store_id); |
| 153 } |
| 154 |
| 155 Type type() const; |
| 156 |
| 157 int64 database_id_; |
| 158 int64 object_store_id_; |
| 159 int64 index_id_; |
| 160 |
| 161 static const int64 kInvalidId = -1; |
| 162 |
| 163 private: |
| 164 static std::vector<char> EncodeInternal(int64 database_id, |
| 165 int64 object_store_id, |
| 166 int64 index_id); |
| 167 // Special constructor for CreateWithSpecialIndex() |
| 168 KeyPrefix(enum Type, |
| 169 int64 database_id, |
| 170 int64 object_store_id, |
| 171 int64 index_id); |
| 172 }; |
| 173 |
| 174 class SchemaVersionKey { |
| 175 public: |
| 176 CONTENT_EXPORT static std::vector<char> Encode(); |
| 177 }; |
| 178 |
| 179 class MaxDatabaseIdKey { |
| 180 public: |
| 181 CONTENT_EXPORT static std::vector<char> Encode(); |
| 182 }; |
| 183 |
| 184 class DataVersionKey { |
| 185 public: |
| 186 static std::vector<char> Encode(); |
| 187 }; |
| 188 |
| 189 class DatabaseFreeListKey { |
| 190 public: |
| 191 DatabaseFreeListKey(); |
| 192 static const char* Decode(const char* start, |
| 193 const char* limit, |
| 194 DatabaseFreeListKey* result); |
| 195 CONTENT_EXPORT static std::vector<char> Encode(int64 database_id); |
| 196 static CONTENT_EXPORT std::vector<char> EncodeMaxKey(); |
| 197 int64 DatabaseId() const; |
| 198 int Compare(const DatabaseFreeListKey& other) const; |
| 199 |
| 200 private: |
| 201 int64 database_id_; |
| 202 }; |
| 203 |
| 204 class DatabaseNameKey { |
| 205 public: |
| 206 static const char* Decode(const char* start, |
| 207 const char* limit, |
| 208 DatabaseNameKey* result); |
| 209 CONTENT_EXPORT static std::vector<char> Encode(const string16& origin, |
| 210 const string16& database_name); |
| 211 static std::vector<char> EncodeMinKeyForOrigin(const string16& origin); |
| 212 static std::vector<char> EncodeStopKeyForOrigin(const string16& origin); |
| 213 string16 origin() const { return origin_; } |
| 214 string16 database_name() const { return database_name_; } |
| 215 int Compare(const DatabaseNameKey& other); |
| 216 |
| 217 private: |
| 218 string16 origin_; // TODO(jsbell): Store encoded strings, or just pointers. |
| 219 string16 database_name_; |
| 220 }; |
| 221 |
| 222 class DatabaseMetaDataKey { |
| 223 public: |
| 224 enum MetaDataType { |
| 225 ORIGIN_NAME = 0, |
| 226 DATABASE_NAME = 1, |
| 227 USER_VERSION = 2, |
| 228 MAX_OBJECT_STORE_ID = 3, |
| 229 USER_INT_VERSION = 4, |
| 230 MAX_SIMPLE_METADATA_TYPE = 5 |
| 231 }; |
| 232 |
| 233 CONTENT_EXPORT static std::vector<char> Encode(int64 database_id, |
| 234 MetaDataType type); |
| 235 }; |
| 236 |
| 237 class ObjectStoreMetaDataKey { |
| 238 public: |
| 239 enum MetaDataType { |
| 240 NAME = 0, |
| 241 KEY_PATH = 1, |
| 242 AUTO_INCREMENT = 2, |
| 243 EVICTABLE = 3, |
| 244 LAST_VERSION = 4, |
| 245 MAX_INDEX_ID = 5, |
| 246 HAS_KEY_PATH = 6, |
| 247 KEY_GENERATOR_CURRENT_NUMBER = 7 |
| 248 }; |
| 249 |
| 250 ObjectStoreMetaDataKey(); |
| 251 static const char* Decode(const char* start, |
| 252 const char* limit, |
| 253 ObjectStoreMetaDataKey* result); |
| 254 CONTENT_EXPORT static std::vector<char> Encode(int64 database_id, |
| 255 int64 object_store_id, |
| 256 unsigned char meta_data_type); |
| 257 CONTENT_EXPORT static std::vector<char> EncodeMaxKey(int64 database_id); |
| 258 CONTENT_EXPORT static std::vector<char> EncodeMaxKey(int64 database_id, |
| 259 int64 object_store_id); |
| 260 int64 ObjectStoreId() const; |
| 261 unsigned char MetaDataType() const; |
| 262 int Compare(const ObjectStoreMetaDataKey& other); |
| 263 |
| 264 private: |
| 265 int64 object_store_id_; |
| 266 unsigned char meta_data_type_; |
| 267 }; |
| 268 |
| 269 class IndexMetaDataKey { |
| 270 public: |
| 271 enum MetaDataType { |
| 272 NAME = 0, |
| 273 UNIQUE = 1, |
| 274 KEY_PATH = 2, |
| 275 MULTI_ENTRY = 3 |
| 276 }; |
| 277 |
| 278 IndexMetaDataKey(); |
| 279 static const char* Decode(const char* start, |
| 280 const char* limit, |
| 281 IndexMetaDataKey* result); |
| 282 CONTENT_EXPORT static std::vector<char> Encode(int64 database_id, |
| 283 int64 object_store_id, |
| 284 int64 index_id, |
| 285 unsigned char meta_data_type); |
| 286 CONTENT_EXPORT static std::vector<char> EncodeMaxKey(int64 database_id, |
| 287 int64 object_store_id); |
| 288 CONTENT_EXPORT static std::vector<char> EncodeMaxKey(int64 database_id, |
| 289 int64 object_store_id, |
| 290 int64 index_id); |
| 291 int Compare(const IndexMetaDataKey& other); |
| 292 int64 IndexId() const; |
| 293 unsigned char meta_data_type() const { return meta_data_type_; } |
| 294 |
| 295 private: |
| 296 int64 object_store_id_; |
| 297 int64 index_id_; |
| 298 unsigned char meta_data_type_; |
| 299 }; |
| 300 |
| 301 class ObjectStoreFreeListKey { |
| 302 public: |
| 303 ObjectStoreFreeListKey(); |
| 304 static const char* Decode(const char* start, |
| 305 const char* limit, |
| 306 ObjectStoreFreeListKey* result); |
| 307 CONTENT_EXPORT static std::vector<char> Encode(int64 database_id, |
| 308 int64 object_store_id); |
| 309 CONTENT_EXPORT static std::vector<char> EncodeMaxKey(int64 database_id); |
| 310 int64 ObjectStoreId() const; |
| 311 int Compare(const ObjectStoreFreeListKey& other); |
| 312 |
| 313 private: |
| 314 int64 object_store_id_; |
| 315 }; |
| 316 |
| 317 class IndexFreeListKey { |
| 318 public: |
| 319 IndexFreeListKey(); |
| 320 static const char* Decode(const char* start, |
| 321 const char* limit, |
| 322 IndexFreeListKey* result); |
| 323 CONTENT_EXPORT static std::vector<char> Encode(int64 database_id, |
| 324 int64 object_store_id, |
| 325 int64 index_id); |
| 326 CONTENT_EXPORT static std::vector<char> EncodeMaxKey(int64 database_id, |
| 327 int64 object_store_id); |
| 328 int Compare(const IndexFreeListKey& other); |
| 329 int64 ObjectStoreId() const; |
| 330 int64 IndexId() const; |
| 331 |
| 332 private: |
| 333 int64 object_store_id_; |
| 334 int64 index_id_; |
| 335 }; |
| 336 |
| 337 class ObjectStoreNamesKey { |
| 338 public: |
| 339 // TODO(jsbell): We never use this to look up object store ids, |
| 340 // because a mapping is kept in the IndexedDBDatabaseImpl. Can the |
| 341 // mapping become unreliable? Can we remove this? |
| 342 static const char* Decode(const char* start, |
| 343 const char* limit, |
| 344 ObjectStoreNamesKey* result); |
| 345 CONTENT_EXPORT static std::vector<char> Encode( |
| 346 int64 database_id, |
| 347 const string16& object_store_name); |
| 348 int Compare(const ObjectStoreNamesKey& other); |
| 349 string16 object_store_name() const { return object_store_name_; } |
| 350 |
| 351 private: |
| 352 string16 |
| 353 object_store_name_; // TODO(jsbell): Store the encoded string, or just |
| 354 // pointers to it. |
| 355 }; |
| 356 |
| 357 class IndexNamesKey { |
| 358 public: |
| 359 IndexNamesKey(); |
| 360 // TODO(jsbell): We never use this to look up index ids, because a mapping |
| 361 // is kept at a higher level. |
| 362 static const char* Decode(const char* start, |
| 363 const char* limit, |
| 364 IndexNamesKey* result); |
| 365 CONTENT_EXPORT static std::vector<char> Encode(int64 database_id, |
| 366 int64 object_store_id, |
| 367 const string16& index_name); |
| 368 int Compare(const IndexNamesKey& other); |
| 369 string16 index_name() const { return index_name_; } |
| 370 |
| 371 private: |
| 372 int64 object_store_id_; |
| 373 string16 index_name_; |
| 374 }; |
| 375 |
| 376 class ObjectStoreDataKey { |
| 377 public: |
| 378 static const char* Decode(const char* start, |
| 379 const char* end, |
| 380 ObjectStoreDataKey* result); |
| 381 CONTENT_EXPORT static std::vector<char> Encode( |
| 382 int64 database_id, |
| 383 int64 object_store_id, |
| 384 const std::vector<char> encoded_user_key); |
| 385 static std::vector<char> Encode(int64 database_id, |
| 386 int64 object_store_id, |
| 387 const IndexedDBKey& user_key); |
| 388 int Compare(const ObjectStoreDataKey& other, bool& ok); |
| 389 scoped_ptr<IndexedDBKey> user_key() const; |
| 390 static const int64 kSpecialIndexNumber; |
| 391 ObjectStoreDataKey(); |
| 392 ~ObjectStoreDataKey(); |
| 393 |
| 394 private: |
| 395 std::vector<char> encoded_user_key_; |
| 396 }; |
| 397 |
| 398 class ExistsEntryKey { |
| 399 public: |
| 400 ExistsEntryKey(); |
| 401 ~ExistsEntryKey(); |
| 402 |
| 403 static const char* Decode(const char* start, |
| 404 const char* end, |
| 405 ExistsEntryKey* result); |
| 406 CONTENT_EXPORT static std::vector<char> Encode( |
| 407 int64 database_id, |
| 408 int64 object_store_id, |
| 409 const std::vector<char>& encoded_key); |
| 410 static std::vector<char> Encode(int64 database_id, |
| 411 int64 object_store_id, |
| 412 const IndexedDBKey& user_key); |
| 413 int Compare(const ExistsEntryKey& other, bool& ok); |
| 414 scoped_ptr<IndexedDBKey> user_key() const; |
| 415 |
| 416 static const int64 kSpecialIndexNumber; |
| 417 |
| 418 private: |
| 419 std::vector<char> encoded_user_key_; |
| 420 DISALLOW_COPY_AND_ASSIGN(ExistsEntryKey); |
| 421 }; |
| 422 |
| 423 class IndexDataKey { |
| 424 public: |
| 425 IndexDataKey(); |
| 426 ~IndexDataKey(); |
| 427 static const char* Decode(const char* start, |
| 428 const char* limit, |
| 429 IndexDataKey* result); |
| 430 CONTENT_EXPORT static std::vector<char> Encode( |
| 431 int64 database_id, |
| 432 int64 object_store_id, |
| 433 int64 index_id, |
| 434 const std::vector<char>& encoded_user_key, |
| 435 const std::vector<char>& encoded_primary_key, |
| 436 int64 sequence_number = 0); |
| 437 static std::vector<char> Encode(int64 database_id, |
| 438 int64 object_store_id, |
| 439 int64 index_id, |
| 440 const IndexedDBKey& user_key); |
| 441 static std::vector<char> EncodeMinKey(int64 database_id, |
| 442 int64 object_store_id, |
| 443 int64 index_id); |
| 444 CONTENT_EXPORT static std::vector<char> EncodeMaxKey(int64 database_id, |
| 445 int64 object_store_id, |
| 446 int64 index_id); |
| 447 int Compare(const IndexDataKey& other, bool ignore_duplicates, bool& ok); |
| 448 int64 DatabaseId() const; |
| 449 int64 ObjectStoreId() const; |
| 450 int64 IndexId() const; |
| 451 scoped_ptr<IndexedDBKey> user_key() const; |
| 452 scoped_ptr<IndexedDBKey> primary_key() const; |
| 453 |
| 454 private: |
| 455 int64 database_id_; |
| 456 int64 object_store_id_; |
| 457 int64 index_id_; |
| 458 std::vector<char> encoded_user_key_; |
| 459 std::vector<char> encoded_primary_key_; |
| 460 int64 sequence_number_; |
| 461 |
| 462 DISALLOW_COPY_AND_ASSIGN(IndexDataKey); |
| 463 }; |
| 464 |
| 465 } // namespace content |
| 466 |
| 467 #endif // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_LEVELDB_CODING_H_ |
OLD | NEW |