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