summaryrefslogtreecommitdiff
path: root/chromium/content/browser/indexed_db/indexed_db_leveldb_coding.h
blob: ecd8b9b2d9967e1ed052bd588c43673fbd89e0b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
// 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 <string>

#include "base/basictypes.h"
#include "base/logging.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/strings/string16.h"
#include "base/strings/string_piece.h"
#include "content/common/indexed_db/indexed_db_key.h"
#include "content/common/indexed_db/indexed_db_key_path.h"

namespace content {

CONTENT_EXPORT extern const unsigned char kMinimumIndexId;

CONTENT_EXPORT std::string MaxIDBKey();
CONTENT_EXPORT std::string MinIDBKey();

CONTENT_EXPORT void EncodeByte(unsigned char value, std::string* into);
CONTENT_EXPORT void EncodeBool(bool value, std::string* into);
CONTENT_EXPORT void EncodeInt(int64 value, std::string* into);
CONTENT_EXPORT void EncodeVarInt(int64 value, std::string* into);
CONTENT_EXPORT void EncodeString(const string16& value, std::string* into);
CONTENT_EXPORT void EncodeStringWithLength(const string16& value,
                                           std::string* into);
CONTENT_EXPORT void EncodeDouble(double value, std::string* into);
CONTENT_EXPORT void EncodeIDBKey(const IndexedDBKey& value, std::string* into);
CONTENT_EXPORT void EncodeIDBKeyPath(const IndexedDBKeyPath& value,
                                     std::string* into);

CONTENT_EXPORT WARN_UNUSED_RESULT bool DecodeByte(base::StringPiece* slice,
                                                  unsigned char* value);
CONTENT_EXPORT WARN_UNUSED_RESULT bool DecodeBool(base::StringPiece* slice,
                                                  bool* value);
CONTENT_EXPORT WARN_UNUSED_RESULT bool DecodeInt(base::StringPiece* slice,
                                                 int64* value);
CONTENT_EXPORT WARN_UNUSED_RESULT bool DecodeVarInt(base::StringPiece* slice,
                                                    int64* value);
CONTENT_EXPORT WARN_UNUSED_RESULT bool DecodeString(base::StringPiece* slice,
                                                    string16* value);
CONTENT_EXPORT WARN_UNUSED_RESULT bool DecodeStringWithLength(
    base::StringPiece* slice,
    string16* value);
CONTENT_EXPORT WARN_UNUSED_RESULT bool DecodeDouble(base::StringPiece* slice,
                                                    double* value);
CONTENT_EXPORT WARN_UNUSED_RESULT bool DecodeIDBKey(
    base::StringPiece* slice,
    scoped_ptr<IndexedDBKey>* value);
CONTENT_EXPORT WARN_UNUSED_RESULT bool DecodeIDBKeyPath(
    base::StringPiece* slice,
    IndexedDBKeyPath* value);

CONTENT_EXPORT int CompareEncodedStringsWithLength(base::StringPiece* slice1,
                                                   base::StringPiece* slice2,
                                                   bool* ok);

CONTENT_EXPORT WARN_UNUSED_RESULT bool ExtractEncodedIDBKey(
    base::StringPiece* slice,
    std::string* result);

CONTENT_EXPORT int CompareEncodedIDBKeys(const std::string& a,
                                         const std::string& b,
                                         bool* ok);

CONTENT_EXPORT int Compare(const base::StringPiece& a,
                           const base::StringPiece& b,
                           bool index_keys);

class KeyPrefix {
 public:
  KeyPrefix();
  explicit KeyPrefix(int64 database_id);
  KeyPrefix(int64 database_id, int64 object_store_id);
  KeyPrefix(int64 database_id, int64 object_store_id, int64 index_id);
  static KeyPrefix CreateWithSpecialIndex(int64 database_id,
                                          int64 object_store_id,
                                          int64 index_id);

  static bool Decode(base::StringPiece* slice, KeyPrefix* result);
  std::string Encode() const;
  static std::string EncodeEmpty();
  int Compare(const KeyPrefix& other) const;

  enum Type {
    GLOBAL_METADATA,
    DATABASE_METADATA,
    OBJECT_STORE_DATA,
    EXISTS_ENTRY,
    INDEX_DATA,
    INVALID_TYPE
  };

  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 kMaxDatabaseId =
      (1ULL << kMaxDatabaseIdBits) - 1;  // max signed int64
  static const int64 kMaxObjectStoreId =
      (1ULL << kMaxObjectStoreIdBits) - 1;  // max signed int64
  static const int64 kMaxIndexId =
      (1ULL << kMaxIndexIdBits) - 1;  // max signed int32

  static bool IsValidDatabaseId(int64 database_id);
  static bool IsValidObjectStoreId(int64 index_id);
  static bool IsValidIndexId(int64 index_id);
  static bool ValidIds(int64 database_id,
                       int64 object_store_id,
                       int64 index_id) {
    return IsValidDatabaseId(database_id) &&
           IsValidObjectStoreId(object_store_id) && IsValidIndexId(index_id);
  }
  static bool ValidIds(int64 database_id, int64 object_store_id) {
    return IsValidDatabaseId(database_id) &&
           IsValidObjectStoreId(object_store_id);
  }

  Type type() const;

  int64 database_id_;
  int64 object_store_id_;
  int64 index_id_;

  static const int64 kInvalidId = -1;

 private:
  static std::string EncodeInternal(int64 database_id,
                                    int64 object_store_id,
                                    int64 index_id);
  // Special constructor for CreateWithSpecialIndex()
  KeyPrefix(enum Type,
            int64 database_id,
            int64 object_store_id,
            int64 index_id);
};

class SchemaVersionKey {
 public:
  CONTENT_EXPORT static std::string Encode();
};

class MaxDatabaseIdKey {
 public:
  CONTENT_EXPORT static std::string Encode();
};

class DataVersionKey {
 public:
  static std::string Encode();
};

class DatabaseFreeListKey {
 public:
  DatabaseFreeListKey();
  static bool Decode(base::StringPiece* slice, DatabaseFreeListKey* result);
  CONTENT_EXPORT static std::string Encode(int64 database_id);
  static CONTENT_EXPORT std::string EncodeMaxKey();
  int64 DatabaseId() const;
  int Compare(const DatabaseFreeListKey& other) const;

 private:
  int64 database_id_;
};

class DatabaseNameKey {
 public:
  static bool Decode(base::StringPiece* slice, DatabaseNameKey* result);
  CONTENT_EXPORT static std::string Encode(const std::string& origin_identifier,
                                           const string16& database_name);
  static std::string EncodeMinKeyForOrigin(
      const std::string& origin_identifier);
  static std::string EncodeStopKeyForOrigin(
      const std::string& origin_identifier);
  string16 origin() const { return origin_; }
  string16 database_name() const { return database_name_; }
  int Compare(const DatabaseNameKey& other);

 private:
  string16 origin_;  // TODO(jsbell): Store encoded strings, or just pointers.
  string16 database_name_;
};

class DatabaseMetaDataKey {
 public:
  enum MetaDataType {
    ORIGIN_NAME = 0,
    DATABASE_NAME = 1,
    USER_VERSION = 2,
    MAX_OBJECT_STORE_ID = 3,
    USER_INT_VERSION = 4,
    MAX_SIMPLE_METADATA_TYPE = 5
  };

  CONTENT_EXPORT static std::string Encode(int64 database_id,
                                           MetaDataType type);
};

class ObjectStoreMetaDataKey {
 public:
  enum MetaDataType {
    NAME = 0,
    KEY_PATH = 1,
    AUTO_INCREMENT = 2,
    EVICTABLE = 3,
    LAST_VERSION = 4,
    MAX_INDEX_ID = 5,
    HAS_KEY_PATH = 6,
    KEY_GENERATOR_CURRENT_NUMBER = 7
  };

  ObjectStoreMetaDataKey();
  static bool Decode(base::StringPiece* slice, ObjectStoreMetaDataKey* result);
  CONTENT_EXPORT static std::string Encode(int64 database_id,
                                           int64 object_store_id,
                                           unsigned char meta_data_type);
  CONTENT_EXPORT static std::string EncodeMaxKey(int64 database_id);
  CONTENT_EXPORT static std::string EncodeMaxKey(int64 database_id,
                                                 int64 object_store_id);
  int64 ObjectStoreId() const;
  unsigned char MetaDataType() const;
  int Compare(const ObjectStoreMetaDataKey& other);

 private:
  int64 object_store_id_;
  unsigned char meta_data_type_;
};

class IndexMetaDataKey {
 public:
  enum MetaDataType {
    NAME = 0,
    UNIQUE = 1,
    KEY_PATH = 2,
    MULTI_ENTRY = 3
  };

  IndexMetaDataKey();
  static bool Decode(base::StringPiece* slice, IndexMetaDataKey* result);
  CONTENT_EXPORT static std::string Encode(int64 database_id,
                                           int64 object_store_id,
                                           int64 index_id,
                                           unsigned char meta_data_type);
  CONTENT_EXPORT static std::string EncodeMaxKey(int64 database_id,
                                                 int64 object_store_id);
  CONTENT_EXPORT static std::string EncodeMaxKey(int64 database_id,
                                                 int64 object_store_id,
                                                 int64 index_id);
  int Compare(const IndexMetaDataKey& other);
  int64 IndexId() const;
  unsigned char meta_data_type() const { return meta_data_type_; }

 private:
  int64 object_store_id_;
  int64 index_id_;
  unsigned char meta_data_type_;
};

class ObjectStoreFreeListKey {
 public:
  ObjectStoreFreeListKey();
  static bool Decode(base::StringPiece* slice, ObjectStoreFreeListKey* result);
  CONTENT_EXPORT static std::string Encode(int64 database_id,
                                           int64 object_store_id);
  CONTENT_EXPORT static std::string EncodeMaxKey(int64 database_id);
  int64 ObjectStoreId() const;
  int Compare(const ObjectStoreFreeListKey& other);

 private:
  int64 object_store_id_;
};

class IndexFreeListKey {
 public:
  IndexFreeListKey();
  static bool Decode(base::StringPiece* slice, IndexFreeListKey* result);
  CONTENT_EXPORT static std::string Encode(int64 database_id,
                                           int64 object_store_id,
                                           int64 index_id);
  CONTENT_EXPORT static std::string EncodeMaxKey(int64 database_id,
                                                 int64 object_store_id);
  int Compare(const IndexFreeListKey& other);
  int64 ObjectStoreId() const;
  int64 IndexId() const;

 private:
  int64 object_store_id_;
  int64 index_id_;
};

class ObjectStoreNamesKey {
 public:
  // TODO(jsbell): We never use this to look up object store ids,
  // because a mapping is kept in the IndexedDBDatabase. Can the
  // mapping become unreliable?  Can we remove this?
  static bool Decode(base::StringPiece* slice, ObjectStoreNamesKey* result);
  CONTENT_EXPORT static std::string Encode(int64 database_id,
                                           const string16& object_store_name);
  int Compare(const ObjectStoreNamesKey& other);
  string16 object_store_name() const { return object_store_name_; }

 private:
  // TODO(jsbell): Store the encoded string, or just pointers to it.
  string16 object_store_name_;
};

class IndexNamesKey {
 public:
  IndexNamesKey();
  // TODO(jsbell): We never use this to look up index ids, because a mapping
  // is kept at a higher level.
  static bool Decode(base::StringPiece* slice, IndexNamesKey* result);
  CONTENT_EXPORT static std::string Encode(int64 database_id,
                                           int64 object_store_id,
                                           const string16& index_name);
  int Compare(const IndexNamesKey& other);
  string16 index_name() const { return index_name_; }

 private:
  int64 object_store_id_;
  string16 index_name_;
};

class ObjectStoreDataKey {
 public:
  static bool Decode(base::StringPiece* slice, ObjectStoreDataKey* result);
  CONTENT_EXPORT static std::string Encode(int64 database_id,
                                           int64 object_store_id,
                                           const std::string encoded_user_key);
  static std::string Encode(int64 database_id,
                            int64 object_store_id,
                            const IndexedDBKey& user_key);
  int Compare(const ObjectStoreDataKey& other, bool* ok);
  scoped_ptr<IndexedDBKey> user_key() const;
  static const int64 kSpecialIndexNumber;
  ObjectStoreDataKey();
  ~ObjectStoreDataKey();

 private:
  std::string encoded_user_key_;
};

class ExistsEntryKey {
 public:
  ExistsEntryKey();
  ~ExistsEntryKey();

  static bool Decode(base::StringPiece* slice, ExistsEntryKey* result);
  CONTENT_EXPORT static std::string Encode(int64 database_id,
                                           int64 object_store_id,
                                           const std::string& encoded_key);
  static std::string Encode(int64 database_id,
                            int64 object_store_id,
                            const IndexedDBKey& user_key);
  int Compare(const ExistsEntryKey& other, bool* ok);
  scoped_ptr<IndexedDBKey> user_key() const;

  static const int64 kSpecialIndexNumber;

 private:
  std::string encoded_user_key_;
  DISALLOW_COPY_AND_ASSIGN(ExistsEntryKey);
};

class IndexDataKey {
 public:
  IndexDataKey();
  ~IndexDataKey();
  static bool Decode(base::StringPiece* slice, IndexDataKey* result);
  CONTENT_EXPORT static std::string Encode(
      int64 database_id,
      int64 object_store_id,
      int64 index_id,
      const std::string& encoded_user_key,
      const std::string& encoded_primary_key,
      int64 sequence_number);
  static std::string Encode(int64 database_id,
                            int64 object_store_id,
                            int64 index_id,
                            const IndexedDBKey& user_key);
  static std::string EncodeMinKey(int64 database_id,
                                  int64 object_store_id,
                                  int64 index_id);
  CONTENT_EXPORT static std::string EncodeMaxKey(int64 database_id,
                                                 int64 object_store_id,
                                                 int64 index_id);
  int Compare(const IndexDataKey& other, bool ignore_duplicates, bool* ok);
  int64 DatabaseId() const;
  int64 ObjectStoreId() const;
  int64 IndexId() const;
  scoped_ptr<IndexedDBKey> user_key() const;
  scoped_ptr<IndexedDBKey> primary_key() const;

 private:
  int64 database_id_;
  int64 object_store_id_;
  int64 index_id_;
  std::string encoded_user_key_;
  std::string encoded_primary_key_;
  int64 sequence_number_;

  DISALLOW_COPY_AND_ASSIGN(IndexDataKey);
};

}  // namespace content

#endif  // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_LEVELDB_CODING_H_