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 google
.appengine
.ext
.db
as db
7 from datastore_models
import PersistentObjectStoreItem
8 from environment
import IsDevServer
9 from future
import All
, Future
10 from object_store
import ObjectStore
13 class PersistentObjectStoreAppengine(ObjectStore
):
14 '''Stores or retrieves persistent data using the AppEngine Datastore API.
16 def __init__(self
, namespace
):
17 self
._namespace
= namespace
19 def SetMulti(self
, mapping
):
20 entities
= [PersistentObjectStoreItem
.CreateItem(
21 self
._namespace
, key
, value
)
22 for key
, value
in mapping
.iteritems()]
23 # Some entites may be None if they were too large to insert. Skip those.
24 rpcs
= [db
.put_async(entity
for entity
in entities
if entity
)]
25 # If running the dev server, the futures don't complete until the server is
26 # *quitting*. This is annoying. Flush now.
28 [rpc
.wait() for rpc
in rpcs
]
29 return All(Future(callback
=lambda: rpc
.get_result()) for rpc
in rpcs
)
31 def GetMulti(self
, keys
):
32 db_futures
= dict((k
, db
.get_async(
33 PersistentObjectStoreItem
.CreateKey(self
._namespace
, k
)))
36 return dict((key
, future
.get_result().GetValue())
37 for key
, future
in db_futures
.iteritems()
38 if future
.get_result() is not None)
39 return Future(callback
=resolve
)
41 def DelMulti(self
, keys
):
44 futures
.append(db
.delete_async(
45 PersistentObjectStoreItem
.CreateKey(self
._namespace
, key
)))
46 # If running the dev server, the futures don't complete until the server is
47 # *quitting*. This is annoying. Flush now.
49 [future
.wait() for future
in futures
]