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