1868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Copyright (c) 2013 The Chromium Authors. All rights reserved.
2868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// found in the LICENSE file.
4868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
5868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#ifndef CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_LEVELDB_CODING_H_
6868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#define CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_LEVELDB_CODING_H_
7868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
87dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include <string>
9f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include <utility>
10f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include <vector>
11868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
12868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/basictypes.h"
13868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/logging.h"
14868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/memory/ref_counted.h"
15868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
16868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/string16.h"
17868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/string_piece.h"
18868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "content/common/indexed_db/indexed_db_key.h"
19868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "content/common/indexed_db/indexed_db_key_path.h"
20868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
21868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace content {
22868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
23868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)CONTENT_EXPORT extern const unsigned char kMinimumIndexId;
24868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
257dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochCONTENT_EXPORT std::string MaxIDBKey();
267dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochCONTENT_EXPORT std::string MinIDBKey();
27868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
28010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// DatabaseId, BlobKey
29010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)typedef std::pair<int64_t, int64_t> BlobJournalEntryType;
30010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)typedef std::vector<BlobJournalEntryType> BlobJournalType;
31010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
327dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochCONTENT_EXPORT void EncodeByte(unsigned char value, std::string* into);
337dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochCONTENT_EXPORT void EncodeBool(bool value, std::string* into);
347dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochCONTENT_EXPORT void EncodeInt(int64 value, std::string* into);
357dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochCONTENT_EXPORT void EncodeVarInt(int64 value, std::string* into);
36a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)CONTENT_EXPORT void EncodeString(const base::string16& value,
37a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                 std::string* into);
38a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)CONTENT_EXPORT void EncodeStringWithLength(const base::string16& value,
397dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                                           std::string* into);
40f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)CONTENT_EXPORT void EncodeBinary(const std::string& value, std::string* into);
417dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochCONTENT_EXPORT void EncodeDouble(double value, std::string* into);
427dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochCONTENT_EXPORT void EncodeIDBKey(const IndexedDBKey& value, std::string* into);
43868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)CONTENT_EXPORT void EncodeIDBKeyPath(const IndexedDBKeyPath& value,
447dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                                     std::string* into);
45010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)CONTENT_EXPORT void EncodeBlobJournal(const BlobJournalType& journal,
46010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                                      std::string* into);
47868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
48868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)CONTENT_EXPORT WARN_UNUSED_RESULT bool DecodeByte(base::StringPiece* slice,
49868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                                  unsigned char* value);
50868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)CONTENT_EXPORT WARN_UNUSED_RESULT bool DecodeBool(base::StringPiece* slice,
51868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                                  bool* value);
52868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)CONTENT_EXPORT WARN_UNUSED_RESULT bool DecodeInt(base::StringPiece* slice,
53868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                                 int64* value);
54868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)CONTENT_EXPORT WARN_UNUSED_RESULT bool DecodeVarInt(base::StringPiece* slice,
55868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                                    int64* value);
56868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)CONTENT_EXPORT WARN_UNUSED_RESULT bool DecodeString(base::StringPiece* slice,
57a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                                    base::string16* value);
58868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)CONTENT_EXPORT WARN_UNUSED_RESULT bool DecodeStringWithLength(
59868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    base::StringPiece* slice,
60a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    base::string16* value);
61f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)CONTENT_EXPORT WARN_UNUSED_RESULT bool DecodeBinary(base::StringPiece* slice,
62f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                                    std::string* value);
63868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)CONTENT_EXPORT WARN_UNUSED_RESULT bool DecodeDouble(base::StringPiece* slice,
64868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                                    double* value);
65868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)CONTENT_EXPORT WARN_UNUSED_RESULT bool DecodeIDBKey(
66868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    base::StringPiece* slice,
67868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    scoped_ptr<IndexedDBKey>* value);
68868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)CONTENT_EXPORT WARN_UNUSED_RESULT bool DecodeIDBKeyPath(
69868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    base::StringPiece* slice,
70868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    IndexedDBKeyPath* value);
71010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)CONTENT_EXPORT WARN_UNUSED_RESULT bool DecodeBlobJournal(
72010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    base::StringPiece* slice,
73010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    BlobJournalType* journal);
74868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
75868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)CONTENT_EXPORT int CompareEncodedStringsWithLength(base::StringPiece* slice1,
76868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                                   base::StringPiece* slice2,
77868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                                   bool* ok);
78868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
79868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)CONTENT_EXPORT WARN_UNUSED_RESULT bool ExtractEncodedIDBKey(
80868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    base::StringPiece* slice,
817dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    std::string* result);
82868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
83a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)CONTENT_EXPORT int CompareEncodedIDBKeys(base::StringPiece* slice1,
84a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                         base::StringPiece* slice2,
85868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                         bool* ok);
86868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
877dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochCONTENT_EXPORT int Compare(const base::StringPiece& a,
887dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                           const base::StringPiece& b,
89eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                           bool index_keys);
90868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
91868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)class KeyPrefix {
92868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) public:
93a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // These are serialized to disk; any new items must be appended, and none can
94a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // be deleted.
95868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  enum Type {
96868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    GLOBAL_METADATA,
97868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    DATABASE_METADATA,
98868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    OBJECT_STORE_DATA,
99868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    EXISTS_ENTRY,
100868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    INDEX_DATA,
101a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    INVALID_TYPE,
102a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    BLOB_ENTRY
103868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  };
104868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
105868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  static const size_t kMaxDatabaseIdSizeBits = 3;
106868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  static const size_t kMaxObjectStoreIdSizeBits = 3;
107868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  static const size_t kMaxIndexIdSizeBits = 2;
108868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
109868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  static const size_t kMaxDatabaseIdSizeBytes =
110868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      1ULL << kMaxDatabaseIdSizeBits;  // 8
111868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  static const size_t kMaxObjectStoreIdSizeBytes =
112868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      1ULL << kMaxObjectStoreIdSizeBits;                                   // 8
113868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  static const size_t kMaxIndexIdSizeBytes = 1ULL << kMaxIndexIdSizeBits;  // 4
114868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
115868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  static const size_t kMaxDatabaseIdBits =
116868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      kMaxDatabaseIdSizeBytes * 8 - 1;  // 63
117868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  static const size_t kMaxObjectStoreIdBits =
118868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      kMaxObjectStoreIdSizeBytes * 8 - 1;                              // 63
119868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  static const size_t kMaxIndexIdBits = kMaxIndexIdSizeBytes * 8 - 1;  // 31
120868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
121868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  static const int64 kMaxDatabaseId =
122868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      (1ULL << kMaxDatabaseIdBits) - 1;  // max signed int64
123868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  static const int64 kMaxObjectStoreId =
124868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      (1ULL << kMaxObjectStoreIdBits) - 1;  // max signed int64
125868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  static const int64 kMaxIndexId =
126868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      (1ULL << kMaxIndexIdBits) - 1;  // max signed int32
127868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1281320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  static const int64 kInvalidId = -1;
1291320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1301320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  KeyPrefix();
1311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  explicit KeyPrefix(int64 database_id);
1321320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  KeyPrefix(int64 database_id, int64 object_store_id);
1331320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  KeyPrefix(int64 database_id, int64 object_store_id, int64 index_id);
1341320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  static KeyPrefix CreateWithSpecialIndex(int64 database_id,
1351320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                          int64 object_store_id,
1361320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                          int64 index_id);
1371320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1381320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  static bool Decode(base::StringPiece* slice, KeyPrefix* result);
1391320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  std::string Encode() const;
1401320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  static std::string EncodeEmpty();
1411320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  int Compare(const KeyPrefix& other) const;
1421320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
14346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  CONTENT_EXPORT static bool IsValidDatabaseId(int64 database_id);
144868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  static bool IsValidObjectStoreId(int64 index_id);
145868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  static bool IsValidIndexId(int64 index_id);
146868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  static bool ValidIds(int64 database_id,
147868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                       int64 object_store_id,
148868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                       int64 index_id) {
149868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return IsValidDatabaseId(database_id) &&
150868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)           IsValidObjectStoreId(object_store_id) && IsValidIndexId(index_id);
151868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
152868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  static bool ValidIds(int64 database_id, int64 object_store_id) {
153868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return IsValidDatabaseId(database_id) &&
154868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)           IsValidObjectStoreId(object_store_id);
155868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
156868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
157868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  Type type() const;
158868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
159868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  int64 database_id_;
160868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  int64 object_store_id_;
161868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  int64 index_id_;
162868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
163868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) private:
164868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Special constructor for CreateWithSpecialIndex()
165868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  KeyPrefix(enum Type,
166868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            int64 database_id,
167868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            int64 object_store_id,
168868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            int64 index_id);
1691320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1701320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  static std::string EncodeInternal(int64 database_id,
1711320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                    int64 object_store_id,
1721320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                    int64 index_id);
173868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)};
174868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
175868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)class SchemaVersionKey {
176868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) public:
1777dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  CONTENT_EXPORT static std::string Encode();
178868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)};
179868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
180868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)class MaxDatabaseIdKey {
181868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) public:
1827dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  CONTENT_EXPORT static std::string Encode();
183868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)};
184868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
185868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)class DataVersionKey {
186868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) public:
1877dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  static std::string Encode();
188868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)};
189868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
190a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class BlobJournalKey {
191a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) public:
192a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  static std::string Encode();
193a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)};
194a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
195a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class LiveBlobJournalKey {
196a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) public:
197a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  static std::string Encode();
198a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)};
199a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
200868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)class DatabaseFreeListKey {
201868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) public:
202868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  DatabaseFreeListKey();
203ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  static bool Decode(base::StringPiece* slice, DatabaseFreeListKey* result);
2047dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  CONTENT_EXPORT static std::string Encode(int64 database_id);
2057dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  static CONTENT_EXPORT std::string EncodeMaxKey();
206868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  int64 DatabaseId() const;
207868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  int Compare(const DatabaseFreeListKey& other) const;
208868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
209868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) private:
210868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  int64 database_id_;
211868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)};
212868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
213868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)class DatabaseNameKey {
214868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) public:
215ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  static bool Decode(base::StringPiece* slice, DatabaseNameKey* result);
2167dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  CONTENT_EXPORT static std::string Encode(const std::string& origin_identifier,
217a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                           const base::string16& database_name);
2187dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  static std::string EncodeMinKeyForOrigin(
219eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      const std::string& origin_identifier);
2207dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  static std::string EncodeStopKeyForOrigin(
221eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      const std::string& origin_identifier);
222a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 origin() const { return origin_; }
223a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 database_name() const { return database_name_; }
224868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  int Compare(const DatabaseNameKey& other);
225868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
226868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) private:
227a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 origin_;  // TODO(jsbell): Store encoded strings, or just
228a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                           // pointers.
229a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 database_name_;
230868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)};
231868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
232868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)class DatabaseMetaDataKey {
233868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) public:
234868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  enum MetaDataType {
235868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    ORIGIN_NAME = 0,
236868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    DATABASE_NAME = 1,
237868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    USER_VERSION = 2,
238868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    MAX_OBJECT_STORE_ID = 3,
239868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    USER_INT_VERSION = 4,
240a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    BLOB_KEY_GENERATOR_CURRENT_NUMBER = 5,
241a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    MAX_SIMPLE_METADATA_TYPE = 6
242868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  };
243868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
244010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  CONTENT_EXPORT static const int64 kAllBlobsKey;
24523730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  static const int64 kBlobKeyGeneratorInitialNumber;
24623730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  // All keys <= 0 are invalid.  This one's just a convenient example.
24723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  static const int64 kInvalidBlobKey;
248a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
249a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  static bool IsValidBlobKey(int64 blobKey);
2507dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  CONTENT_EXPORT static std::string Encode(int64 database_id,
2517dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                                           MetaDataType type);
252868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)};
253868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
254868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)class ObjectStoreMetaDataKey {
255868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) public:
256868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  enum MetaDataType {
257868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    NAME = 0,
258868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    KEY_PATH = 1,
259868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    AUTO_INCREMENT = 2,
260868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    EVICTABLE = 3,
261868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    LAST_VERSION = 4,
262868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    MAX_INDEX_ID = 5,
263868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    HAS_KEY_PATH = 6,
264868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    KEY_GENERATOR_CURRENT_NUMBER = 7
265868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  };
266868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
267868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ObjectStoreMetaDataKey();
268ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  static bool Decode(base::StringPiece* slice, ObjectStoreMetaDataKey* result);
2697dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  CONTENT_EXPORT static std::string Encode(int64 database_id,
2707dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                                           int64 object_store_id,
2717dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                                           unsigned char meta_data_type);
2727dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  CONTENT_EXPORT static std::string EncodeMaxKey(int64 database_id);
2737dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  CONTENT_EXPORT static std::string EncodeMaxKey(int64 database_id,
2747dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                                                 int64 object_store_id);
275868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  int64 ObjectStoreId() const;
276868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  unsigned char MetaDataType() const;
277868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  int Compare(const ObjectStoreMetaDataKey& other);
278868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
279868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) private:
280868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  int64 object_store_id_;
281868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  unsigned char meta_data_type_;
282868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)};
283868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
284868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)class IndexMetaDataKey {
285868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) public:
286868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  enum MetaDataType {
287868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    NAME = 0,
288868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    UNIQUE = 1,
289868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    KEY_PATH = 2,
290868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    MULTI_ENTRY = 3
291868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  };
292868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
293868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  IndexMetaDataKey();
294ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  static bool Decode(base::StringPiece* slice, IndexMetaDataKey* result);
2957dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  CONTENT_EXPORT static std::string Encode(int64 database_id,
2967dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                                           int64 object_store_id,
2977dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                                           int64 index_id,
2987dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                                           unsigned char meta_data_type);
2997dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  CONTENT_EXPORT static std::string EncodeMaxKey(int64 database_id,
3007dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                                                 int64 object_store_id);
3017dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  CONTENT_EXPORT static std::string EncodeMaxKey(int64 database_id,
302868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                                 int64 object_store_id,
3037dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                                                 int64 index_id);
304868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  int Compare(const IndexMetaDataKey& other);
305868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  int64 IndexId() const;
306868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  unsigned char meta_data_type() const { return meta_data_type_; }
307868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
308868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) private:
309868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  int64 object_store_id_;
310868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  int64 index_id_;
311868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  unsigned char meta_data_type_;
312868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)};
313868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
314868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)class ObjectStoreFreeListKey {
315868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) public:
316868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ObjectStoreFreeListKey();
317ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  static bool Decode(base::StringPiece* slice, ObjectStoreFreeListKey* result);
3187dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  CONTENT_EXPORT static std::string Encode(int64 database_id,
3197dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                                           int64 object_store_id);
3207dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  CONTENT_EXPORT static std::string EncodeMaxKey(int64 database_id);
321868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  int64 ObjectStoreId() const;
322868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  int Compare(const ObjectStoreFreeListKey& other);
323868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
324868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) private:
325868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  int64 object_store_id_;
326868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)};
327868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
328868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)class IndexFreeListKey {
329868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) public:
330868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  IndexFreeListKey();
331ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  static bool Decode(base::StringPiece* slice, IndexFreeListKey* result);
3327dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  CONTENT_EXPORT static std::string Encode(int64 database_id,
3337dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                                           int64 object_store_id,
3347dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                                           int64 index_id);
3357dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  CONTENT_EXPORT static std::string EncodeMaxKey(int64 database_id,
3367dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                                                 int64 object_store_id);
337868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  int Compare(const IndexFreeListKey& other);
338868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  int64 ObjectStoreId() const;
339868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  int64 IndexId() const;
340868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
341868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) private:
342868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  int64 object_store_id_;
343868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  int64 index_id_;
344868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)};
345868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
346868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)class ObjectStoreNamesKey {
347868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) public:
348868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // TODO(jsbell): We never use this to look up object store ids,
349868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // because a mapping is kept in the IndexedDBDatabase. Can the
350868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // mapping become unreliable?  Can we remove this?
351ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  static bool Decode(base::StringPiece* slice, ObjectStoreNamesKey* result);
352a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  CONTENT_EXPORT static std::string Encode(
353a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      int64 database_id,
354a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      const base::string16& object_store_name);
355868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  int Compare(const ObjectStoreNamesKey& other);
356a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 object_store_name() const { return object_store_name_; }
357868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
358868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) private:
359eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // TODO(jsbell): Store the encoded string, or just pointers to it.
360a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 object_store_name_;
361868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)};
362868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
363868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)class IndexNamesKey {
364868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) public:
365868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  IndexNamesKey();
366868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // TODO(jsbell): We never use this to look up index ids, because a mapping
367868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // is kept at a higher level.
368ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  static bool Decode(base::StringPiece* slice, IndexNamesKey* result);
3697dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  CONTENT_EXPORT static std::string Encode(int64 database_id,
3707dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                                           int64 object_store_id,
371a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                           const base::string16& index_name);
372868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  int Compare(const IndexNamesKey& other);
373a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 index_name() const { return index_name_; }
374868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
375868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) private:
376868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  int64 object_store_id_;
377a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 index_name_;
378868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)};
379868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
380868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)class ObjectStoreDataKey {
381868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) public:
3821320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  static const int64 kSpecialIndexNumber;
3831320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
3841320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  ObjectStoreDataKey();
3851320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  ~ObjectStoreDataKey();
3861320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
387ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  static bool Decode(base::StringPiece* slice, ObjectStoreDataKey* result);
3887dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  CONTENT_EXPORT static std::string Encode(int64 database_id,
3897dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                                           int64 object_store_id,
3907dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                                           const std::string encoded_user_key);
3917dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  static std::string Encode(int64 database_id,
3927dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                            int64 object_store_id,
3937dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                            const IndexedDBKey& user_key);
394868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  scoped_ptr<IndexedDBKey> user_key() const;
395868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) private:
3967dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  std::string encoded_user_key_;
397868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)};
398868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
399868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)class ExistsEntryKey {
400868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) public:
401868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ExistsEntryKey();
402868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ~ExistsEntryKey();
403868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
404ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  static bool Decode(base::StringPiece* slice, ExistsEntryKey* result);
4057dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  CONTENT_EXPORT static std::string Encode(int64 database_id,
4067dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                                           int64 object_store_id,
4077dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                                           const std::string& encoded_key);
4087dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  static std::string Encode(int64 database_id,
4097dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                            int64 object_store_id,
4107dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                            const IndexedDBKey& user_key);
411868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  scoped_ptr<IndexedDBKey> user_key() const;
412868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
4131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci private:
414868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  static const int64 kSpecialIndexNumber;
415868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
4167dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  std::string encoded_user_key_;
417868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(ExistsEntryKey);
418868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)};
419868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
420a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class BlobEntryKey {
421a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) public:
422a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  BlobEntryKey() : database_id_(0), object_store_id_(0) {}
423a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  static bool Decode(base::StringPiece* slice, BlobEntryKey* result);
424a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  static bool FromObjectStoreDataKey(base::StringPiece* slice,
425a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                     BlobEntryKey* result);
426a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  static std::string ReencodeToObjectStoreDataKey(base::StringPiece* slice);
427a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  static std::string EncodeMinKeyForObjectStore(int64 database_id,
428a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                                int64 object_store_id);
429a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  static std::string EncodeStopKeyForObjectStore(int64 database_id,
430a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                                 int64 object_store_id);
431a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  static std::string Encode(int64 database_id,
432a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                            int64 object_store_id,
433a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                            const IndexedDBKey& user_key);
434a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  std::string Encode() const;
435a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  int64 database_id() const { return database_id_; }
436a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  int64 object_store_id() const { return object_store_id_; }
437a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
4381320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci private:
439a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  static const int64 kSpecialIndexNumber;
440a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
441a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  static std::string Encode(int64 database_id,
442a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                            int64 object_store_id,
443a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                            const std::string& encoded_user_key);
444a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  int64 database_id_;
445a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  int64 object_store_id_;
446a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // This is the user's ObjectStoreDataKey, not the BlobEntryKey itself.
447a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  std::string encoded_user_key_;
448a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)};
449a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
450868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)class IndexDataKey {
451868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) public:
452868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  IndexDataKey();
453868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ~IndexDataKey();
454ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  static bool Decode(base::StringPiece* slice, IndexDataKey* result);
4557dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  CONTENT_EXPORT static std::string Encode(
456868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      int64 database_id,
457868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      int64 object_store_id,
458868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      int64 index_id,
4597dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      const std::string& encoded_user_key,
4607dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      const std::string& encoded_primary_key,
461eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      int64 sequence_number);
4627dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  static std::string Encode(int64 database_id,
4637dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                            int64 object_store_id,
4647dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                            int64 index_id,
4657dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                            const IndexedDBKey& user_key);
466f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  static std::string Encode(int64 database_id,
467f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                            int64 object_store_id,
468f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                            int64 index_id,
469f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                            const IndexedDBKey& user_key,
470f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                            const IndexedDBKey& user_primary_key);
4717dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  static std::string EncodeMinKey(int64 database_id,
472868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                  int64 object_store_id,
4737dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                                  int64 index_id);
4747dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  CONTENT_EXPORT static std::string EncodeMaxKey(int64 database_id,
4757dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                                                 int64 object_store_id,
4767dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                                                 int64 index_id);
477868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  int64 DatabaseId() const;
478868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  int64 ObjectStoreId() const;
479868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  int64 IndexId() const;
480868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  scoped_ptr<IndexedDBKey> user_key() const;
481868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  scoped_ptr<IndexedDBKey> primary_key() const;
482868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
483868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) private:
484868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  int64 database_id_;
485868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  int64 object_store_id_;
486868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  int64 index_id_;
4877dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  std::string encoded_user_key_;
4887dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  std::string encoded_primary_key_;
489868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  int64 sequence_number_;
490868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
491868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(IndexDataKey);
492868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)};
493868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
494868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}  // namespace content
495868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
496868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#endif  // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_LEVELDB_CODING_H_
497