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 from appengine_wrappers
import db
6 from datastore_models
import PersistentObjectStoreItem
7 from environment
import IsDevServer
8 from future
import Future
9 from object_store
import ObjectStore
11 class _AsyncGetFuture(object):
12 def __init__(self
, object_store
, keys
):
15 PersistentObjectStoreItem
.CreateKey(object_store
._namespace
, k
)))
19 return dict((key
, future
.get_result().GetValue())
20 for key
, future
in self
._futures
.iteritems()
21 if future
.get_result() is not None)
23 class PersistentObjectStore(ObjectStore
):
24 '''Stores data persistently using the AppEngine Datastore API.
26 def __init__(self
, namespace
):
27 self
._namespace
= namespace
29 def SetMulti(self
, mapping
):
31 for key
, value
in mapping
.items():
32 futures
.append(db
.put_async(
33 PersistentObjectStoreItem
.CreateItem(self
._namespace
, key
, value
)))
34 # If running the dev server, the futures don't complete until the server is
35 # *quitting*. This is annoying. Flush now.
37 [future
.wait() for future
in futures
]
39 def GetMulti(self
, keys
):
40 return Future(delegate
=_AsyncGetFuture(self
, keys
))
42 def DelMulti(self
, keys
):
45 futures
.append(db
.delete_async(
46 PersistentObjectStoreItem
.CreateKey(self
._namespace
, key
)))
47 # If running the dev server, the futures don't complete until the server is
48 # *quitting*. This is annoying. Flush now.
50 [future
.wait() for future
in futures
]