Chromium Code Reviews| Index: content/browser/indexed_db/indexed_db_leveldb_coding.h |
| diff --git a/content/browser/indexed_db/indexed_db_leveldb_coding.h b/content/browser/indexed_db/indexed_db_leveldb_coding.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ded13e0515b09fdc153414f23f4f4fb52b06aa6c |
| --- /dev/null |
| +++ b/content/browser/indexed_db/indexed_db_leveldb_coding.h |
| @@ -0,0 +1,477 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_LEVELDB_CODING_H_ |
| +#define CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_LEVELDB_CODING_H_ |
| + |
| +#include <vector> |
| + |
| +#include "base/logging.h" |
| +#include "base/memory/ref_counted.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/string16.h" |
| +#include "content/common/indexed_db/indexed_db_key.h" |
| +#include "content/common/indexed_db/indexed_db_key_path.h" |
| + |
| +namespace content { |
| + |
| +class LevelDBSlice; |
| + |
| +namespace IndexedDBLevelDBCoding { |
| + |
| +const unsigned char MinimumIndexId = 30; |
|
jamesr
2013/05/21 23:56:06
kBlahBlah
should probably just declare the variab
jsbell
2013/05/22 17:54:44
Done.
|
| + |
| +// As most of the IndexedDBKeys and encoded values are short, we initialize some |
| +// Vectors with a default inline buffer size |
| +// to reduce the memory re-allocations when the Vectors are appended. |
| +static const size_t DefaultInlineBufferSize = 32; |
|
jamesr
2013/05/21 23:56:06
ditto
jsbell
2013/05/22 17:54:44
Done.
|
| + |
| +CONTENT_EXPORT std::vector<char> EncodeByte(unsigned char); |
| +CONTENT_EXPORT const char* DecodeByte(const char* p, |
| + const char* limit, |
| + unsigned char& found_char); |
| +CONTENT_EXPORT std::vector<char> MaxIDBKey(); |
| +CONTENT_EXPORT std::vector<char> MinIDBKey(); |
| +CONTENT_EXPORT std::vector<char> EncodeBool(bool); |
| +CONTENT_EXPORT bool DecodeBool(const char* begin, const char* end); |
| +CONTENT_EXPORT std::vector<char> EncodeInt(int64_t); |
| +inline std::vector<char> EncodeIntSafely(int64_t nParam, int64_t max) { |
| + DCHECK(nParam <= max); |
| + return EncodeInt(nParam); |
| +} |
| + |
| +template <class T> int64_t DecodeInt(T begin, T end) { |
| + DCHECK(begin <= end); |
| + int64_t ret = 0; |
| + |
| + int shift = 0; |
| + while (begin < end) { |
| + unsigned char c = *begin++; |
| + ret |= static_cast<int64_t>(c) << shift; |
| + shift += 8; |
| + } |
| + |
| + return ret; |
| +} |
| + |
| +CONTENT_EXPORT std::vector<char> EncodeVarInt(int64_t); |
| + |
| +CONTENT_EXPORT const char* DecodeVarInt(const char* p, |
| + const char* limit, |
| + int64_t& found_int); |
| +CONTENT_EXPORT std::vector<char>::iterator DecodeVarInt( |
| + std::vector<char>::iterator start, |
| + std::vector<char>::iterator limit, |
| + int64_t& found_int); |
| + |
| +CONTENT_EXPORT std::vector<char> EncodeString(const string16&); |
| +CONTENT_EXPORT string16 DecodeString(const char* p, const char* end); |
| +CONTENT_EXPORT std::vector<char> EncodeStringWithLength(const string16&); |
| +CONTENT_EXPORT const char* DecodeStringWithLength(const char* p, |
| + const char* limit, |
| + string16& found_string); |
| +CONTENT_EXPORT int CompareEncodedStringsWithLength(const char*& p, |
| + const char* limit_p, |
| + const char*& q, |
| + const char* limit_q, |
| + bool& ok); |
| +CONTENT_EXPORT std::vector<char> EncodeDouble(double); |
| +CONTENT_EXPORT const char* DecodeDouble(const char* p, |
| + const char* limit, |
| + double*); |
| +void EncodeIDBKey(const IndexedDBKey&, std::vector<char>& into); |
| +CONTENT_EXPORT std::vector<char> EncodeIDBKey(const IndexedDBKey&); |
| +CONTENT_EXPORT const char* DecodeIDBKey(const char* p, |
| + const char* limit, |
| + scoped_ptr<IndexedDBKey>* found_key); |
| +CONTENT_EXPORT const char* ExtractEncodedIDBKey(const char* start, |
| + const char* limit, |
| + std::vector<char>* result); |
| +CONTENT_EXPORT int CompareEncodedIDBKeys(const std::vector<char>&, |
| + const std::vector<char>&, |
| + bool& ok); |
| +CONTENT_EXPORT std::vector<char> EncodeIDBKeyPath(const IndexedDBKeyPath&); |
| +CONTENT_EXPORT IndexedDBKeyPath DecodeIDBKeyPath(const char*, const char*); |
| + |
| +CONTENT_EXPORT int Compare(const LevelDBSlice&, |
| + const LevelDBSlice&, |
| + bool index_keys = false); |
| + |
| +class KeyPrefix { |
| + public: |
| + KeyPrefix(); |
| + explicit KeyPrefix(int64_t database_id); |
| + KeyPrefix(int64_t database_id, int64_t object_store_id); |
| + KeyPrefix(int64_t database_id, int64_t object_store_id, int64_t index_id); |
| + static KeyPrefix CreateWithSpecialIndex(int64_t database_id, |
| + int64_t object_store_id, |
| + int64_t index_id); |
| + |
| + static const char* Decode(const char* start, |
| + const char* limit, |
| + KeyPrefix* result); |
| + std::vector<char> Encode() const; |
| + static std::vector<char> EncodeEmpty(); |
| + int Compare(const KeyPrefix& other) const; |
| + |
| + enum Type { |
| + GlobalMetaData, |
|
jamesr
2013/05/21 23:56:06
SHOUT
jsbell
2013/05/22 17:54:44
Done.
|
| + DatabaseMetaData, |
| + ObjectStoreData, |
| + ExistsEntry, |
| + IndexData, |
| + InvalidType |
| + }; |
| + |
| + static const size_t kMaxDatabaseIdSizeBits = 3; |
| + static const size_t kMaxObjectStoreIdSizeBits = 3; |
| + static const size_t kMaxIndexIdSizeBits = 2; |
| + |
| + static const size_t kMaxDatabaseIdSizeBytes = |
| + 1ULL << kMaxDatabaseIdSizeBits; // 8 |
| + static const size_t kMaxObjectStoreIdSizeBytes = |
| + 1ULL << kMaxObjectStoreIdSizeBits; // 8 |
| + static const size_t kMaxIndexIdSizeBytes = 1ULL << kMaxIndexIdSizeBits; // 4 |
| + |
| + static const size_t kMaxDatabaseIdBits = |
| + kMaxDatabaseIdSizeBytes * 8 - 1; // 63 |
| + static const size_t kMaxObjectStoreIdBits = |
| + kMaxObjectStoreIdSizeBytes * 8 - 1; // 63 |
| + static const size_t kMaxIndexIdBits = kMaxIndexIdSizeBytes * 8 - 1; // 31 |
| + |
| + static const int64_t kMaxDatabaseId = |
| + (1ULL << kMaxDatabaseIdBits) - 1; // max signed int64_t |
| + static const int64_t kMaxObjectStoreId = |
| + (1ULL << kMaxObjectStoreIdBits) - 1; // max signed int64_t |
| + static const int64_t kMaxIndexId = |
| + (1ULL << kMaxIndexIdBits) - 1; // max signed int32_t |
| + |
| + static bool IsValidDatabaseId(int64_t database_id); |
| + static bool IsValidObjectStoreId(int64_t index_id); |
| + static bool IsValidIndexId(int64_t index_id); |
| + static bool ValidIds(int64_t database_id, |
| + int64_t object_store_id, |
| + int64_t index_id) { |
| + return IsValidDatabaseId(database_id) && |
| + IsValidObjectStoreId(object_store_id) && IsValidIndexId(index_id); |
| + } |
| + static bool ValidIds(int64_t database_id, int64_t object_store_id) { |
| + return IsValidDatabaseId(database_id) && |
| + IsValidObjectStoreId(object_store_id); |
| + } |
| + |
| + Type type() const; |
| + |
| + int64_t database_id_; |
| + int64_t object_store_id_; |
| + int64_t index_id_; |
| + |
| + static const int64_t InvalidId = -1; |
| + |
| + private: |
| + static std::vector<char> EncodeInternal(int64_t database_id, |
| + int64_t object_store_id, |
| + int64_t index_id); |
| + // Special constructor for CreateWithSpecialIndex() |
| + KeyPrefix(enum Type, |
| + int64_t database_id, |
| + int64_t object_store_id, |
| + int64_t index_id); |
| +}; |
| + |
| +class SchemaVersionKey { |
| + public: |
| + CONTENT_EXPORT static std::vector<char> Encode(); |
| +}; |
| + |
| +class MaxDatabaseIdKey { |
| + public: |
| + CONTENT_EXPORT static std::vector<char> Encode(); |
| +}; |
| + |
| +class DataVersionKey { |
| + public: |
| + static std::vector<char> Encode(); |
| +}; |
| + |
| +class DatabaseFreeListKey { |
| + public: |
| + DatabaseFreeListKey(); |
| + static const char* Decode(const char* start, |
| + const char* limit, |
| + DatabaseFreeListKey* result); |
| + CONTENT_EXPORT static std::vector<char> Encode(int64_t database_id); |
| + static CONTENT_EXPORT std::vector<char> EncodeMaxKey(); |
| + int64_t DatabaseId() const; |
| + int Compare(const DatabaseFreeListKey& other) const; |
| + |
| + private: |
| + int64_t database_id_; |
| +}; |
| + |
| +class DatabaseNameKey { |
| + public: |
| + static const char* Decode(const char* start, |
| + const char* limit, |
| + DatabaseNameKey* result); |
| + CONTENT_EXPORT static std::vector<char> Encode(const string16& origin, |
| + const string16& database_name); |
| + static std::vector<char> EncodeMinKeyForOrigin(const string16& origin); |
| + static std::vector<char> EncodeStopKeyForOrigin(const string16& origin); |
| + string16 origin() const { return origin_; } |
| + string16 database_name() const { return database_name_; } |
| + int Compare(const DatabaseNameKey& other); |
| + |
| + private: |
| + string16 origin_; // TODO: Store encoded strings, or just pointers. |
| + string16 database_name_; |
| +}; |
| + |
| +class DatabaseMetaDataKey { |
| + public: |
| + enum MetaDataType { |
| + OriginName = 0, |
| + DatabaseName = 1, |
| + UserVersion = 2, |
| + MaxObjectStoreId = 3, |
| + UserIntVersion = 4, |
| + MaxSimpleMetaDataType = 5 |
| + }; |
| + |
| + CONTENT_EXPORT static std::vector<char> Encode(int64_t database_id, |
| + MetaDataType); |
| +}; |
| + |
| +class ObjectStoreMetaDataKey { |
| + public: |
| + enum MetaDataType { |
| + Name = 0, |
| + KeyPath = 1, |
| + AutoIncrement = 2, |
| + Evictable = 3, |
| + LastVersion = 4, |
| + MaxIndexId = 5, |
| + HasKeyPath = 6, |
| + KeyGeneratorCurrentNumber = 7 |
| + }; |
| + |
| + ObjectStoreMetaDataKey(); |
| + static const char* Decode(const char* start, |
| + const char* limit, |
| + ObjectStoreMetaDataKey* result); |
| + CONTENT_EXPORT static std::vector<char> Encode(int64_t database_id, |
| + int64_t object_store_id, |
| + unsigned char meta_data_type); |
| + CONTENT_EXPORT static std::vector<char> EncodeMaxKey(int64_t database_id); |
| + CONTENT_EXPORT static std::vector<char> EncodeMaxKey(int64_t database_id, |
| + int64_t object_store_id); |
| + int64_t ObjectStoreId() const; |
| + unsigned char MetaDataType() const; |
| + int Compare(const ObjectStoreMetaDataKey& other); |
| + |
| + private: |
| + int64_t object_store_id_; |
| + unsigned char meta_data_type_; |
| +}; |
| + |
| +class IndexMetaDataKey { |
| + public: |
| + enum MetaDataType { |
| + Name = 0, |
| + Unique = 1, |
| + KeyPath = 2, |
| + MultiEntry = 3 |
| + }; |
| + |
| + IndexMetaDataKey(); |
| + static const char* Decode(const char* start, |
| + const char* limit, |
| + IndexMetaDataKey* result); |
| + CONTENT_EXPORT static std::vector<char> Encode(int64_t database_id, |
| + int64_t object_store_id, |
| + int64_t index_id, |
| + unsigned char meta_data_type); |
| + CONTENT_EXPORT static std::vector<char> EncodeMaxKey(int64_t database_id, |
| + int64_t object_store_id); |
| + CONTENT_EXPORT static std::vector<char> EncodeMaxKey(int64_t database_id, |
| + int64_t object_store_id, |
| + int64_t index_id); |
| + int Compare(const IndexMetaDataKey& other); |
| + int64_t IndexId() const; |
| + unsigned char meta_data_type() const { return meta_data_type_; } |
| + |
| + private: |
| + int64_t object_store_id_; |
| + int64_t index_id_; |
| + unsigned char meta_data_type_; |
| +}; |
| + |
| +class ObjectStoreFreeListKey { |
| + public: |
| + ObjectStoreFreeListKey(); |
| + static const char* Decode(const char* start, |
| + const char* limit, |
| + ObjectStoreFreeListKey* result); |
| + CONTENT_EXPORT static std::vector<char> Encode(int64_t database_id, |
| + int64_t object_store_id); |
| + CONTENT_EXPORT static std::vector<char> EncodeMaxKey(int64_t database_id); |
| + int64_t ObjectStoreId() const; |
| + int Compare(const ObjectStoreFreeListKey& other); |
| + |
| + private: |
| + int64_t object_store_id_; |
| +}; |
| + |
| +class IndexFreeListKey { |
| + public: |
| + IndexFreeListKey(); |
| + static const char* Decode(const char* start, |
| + const char* limit, |
| + IndexFreeListKey* result); |
| + CONTENT_EXPORT static std::vector<char> Encode(int64_t database_id, |
| + int64_t object_store_id, |
| + int64_t index_id); |
| + CONTENT_EXPORT static std::vector<char> EncodeMaxKey(int64_t database_id, |
| + int64_t object_store_id); |
| + int Compare(const IndexFreeListKey& other); |
| + int64_t ObjectStoreId() const; |
| + int64_t IndexId() const; |
| + |
| + private: |
| + int64_t object_store_id_; |
| + int64_t index_id_; |
| +}; |
| + |
| +class ObjectStoreNamesKey { |
| + public: |
| + // TODO: We never use this to look up object store ids, because a mapping |
| + // is kept in the IndexedDBDatabaseImpl. Can the mapping become unreliable? |
| + // Can we remove this? |
| + static const char* Decode(const char* start, |
| + const char* limit, |
| + ObjectStoreNamesKey* result); |
| + CONTENT_EXPORT static std::vector<char> Encode( |
| + int64_t database_id, |
| + const string16& object_store_name); |
| + int Compare(const ObjectStoreNamesKey& other); |
| + string16 object_store_name() const { return object_store_name_; } |
| + |
| + private: |
| + string16 object_store_name_; // TODO: Store the encoded string, or just |
| + // pointers to it. |
| +}; |
| + |
| +class IndexNamesKey { |
| + public: |
| + IndexNamesKey(); |
| + // TODO: We never use this to look up index ids, because a mapping |
| + // is kept at a higher level. |
| + static const char* Decode(const char* start, |
| + const char* limit, |
| + IndexNamesKey* result); |
| + CONTENT_EXPORT static std::vector<char> Encode(int64_t database_id, |
| + int64_t object_store_id, |
| + const string16& index_name); |
| + int Compare(const IndexNamesKey& other); |
| + string16 index_name() const { return index_name_; } |
| + |
| + private: |
| + int64_t object_store_id_; |
| + string16 index_name_; |
| +}; |
| + |
| +class ObjectStoreDataKey { |
| + public: |
| + static const char* Decode(const char* start, |
| + const char* end, |
| + ObjectStoreDataKey* result); |
| + CONTENT_EXPORT static std::vector<char> Encode( |
| + int64_t database_id, |
| + int64_t object_store_id, |
| + const std::vector<char> encoded_user_key); |
| + static std::vector<char> Encode(int64_t database_id, |
| + int64_t object_store_id, |
| + const IndexedDBKey& user_key); |
| + int Compare(const ObjectStoreDataKey& other, bool& ok); |
| + scoped_ptr<IndexedDBKey> user_key() const; |
| + static const int64_t SpecialIndexNumber; |
| + ObjectStoreDataKey(); |
| + ~ObjectStoreDataKey(); |
| + |
| + private: |
| + std::vector<char> encoded_user_key_; |
| +}; |
| + |
| +class ExistsEntryKey { |
| + public: |
| + ExistsEntryKey(); |
| + ~ExistsEntryKey(); |
| + |
| + static const char* Decode(const char* start, |
| + const char* end, |
| + ExistsEntryKey* result); |
| + CONTENT_EXPORT static std::vector<char> Encode( |
| + int64_t database_id, |
| + int64_t object_store_id, |
| + const std::vector<char>& encoded_key); |
| + static std::vector<char> Encode(int64_t database_id, |
| + int64_t object_store_id, |
| + const IndexedDBKey& user_key); |
| + int Compare(const ExistsEntryKey& other, bool& ok); |
| + scoped_ptr<IndexedDBKey> user_key() const; |
| + |
| + static const int64_t SpecialIndexNumber; |
| + |
| + private: |
| + std::vector<char> encoded_user_key_; |
| + DISALLOW_COPY_AND_ASSIGN(ExistsEntryKey); |
| +}; |
| + |
| +class IndexDataKey { |
| + public: |
| + IndexDataKey(); |
| + ~IndexDataKey(); |
| + static const char* Decode(const char* start, |
| + const char* limit, |
| + IndexDataKey* result); |
| + CONTENT_EXPORT static std::vector<char> Encode( |
| + int64_t database_id, |
| + int64_t object_store_id, |
| + int64_t index_id, |
| + const std::vector<char>& encoded_user_key, |
| + const std::vector<char>& encoded_primary_key, |
| + int64_t sequence_number = 0); |
| + static std::vector<char> Encode(int64_t database_id, |
| + int64_t object_store_id, |
| + int64_t index_id, |
| + const IndexedDBKey& user_key); |
| + static std::vector<char> EncodeMinKey(int64_t database_id, |
| + int64_t object_store_id, |
| + int64_t index_id); |
| + CONTENT_EXPORT static std::vector<char> EncodeMaxKey(int64_t database_id, |
| + int64_t object_store_id, |
| + int64_t index_id); |
| + int Compare(const IndexDataKey& other, bool ignore_duplicates, bool& ok); |
| + int64_t DatabaseId() const; |
| + int64_t ObjectStoreId() const; |
| + int64_t IndexId() const; |
| + scoped_ptr<IndexedDBKey> user_key() const; |
| + scoped_ptr<IndexedDBKey> primary_key() const; |
| + |
| + private: |
| + int64_t database_id_; |
| + int64_t object_store_id_; |
| + int64_t index_id_; |
| + std::vector<char> encoded_user_key_; |
| + std::vector<char> encoded_primary_key_; |
| + int64_t sequence_number_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(IndexDataKey); |
| +}; |
| + |
| +} // namespace IndexedDBLevelDBCoding |
| + |
| +} // namespace content |
| + |
| +#endif // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_LEVELDB_CODING_H_ |