Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / common / extensions / docs / server2 / datastore_models.py
blobeaa8cab43a0be724b89cd8c32822e5c888852543
1 # Copyright 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.
5 import cPickle
6 import google.appengine.ext.db as db
7 import logging
8 import traceback
11 _MAX_ENTITY_SIZE = 1024*1024
14 # A collection of the data store models used throughout the server.
15 # These values are global within datastore.
17 class PersistentObjectStoreItem(db.Model):
18 pickled_value = db.BlobProperty()
20 @classmethod
21 def CreateKey(cls, namespace, key):
22 path = '%s/%s' % (namespace, key)
23 try:
24 return db.Key.from_path(cls.__name__, path)
25 except Exception:
26 # Probably AppEngine's BadValueError for the name being too long, but
27 # it's not documented which errors can actually be thrown here, so catch
28 # 'em all.
29 raise ValueError(
30 'Exception thrown when trying to create db.Key from path %s: %s' % (
31 path, traceback.format_exc()))
33 @classmethod
34 def CreateItem(cls, namespace, key, value):
35 pickled_value = cPickle.dumps(value)
36 if len(pickled_value) > _MAX_ENTITY_SIZE:
37 logging.warn('Refusing to create entity greater than 1 MB in size: %s/%s'
38 % (namespace, key))
39 return None
40 return PersistentObjectStoreItem(key=cls.CreateKey(namespace, key),
41 pickled_value=pickled_value)
43 def GetValue(self):
44 return cPickle.loads(self.pickled_value)