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