[appengine-afterburner] r9 committed - Initial prototy of materiazed map view.

0 views
Skip to first unread message

appengine-...@googlecode.com

unread,
Nov 8, 2010, 6:18:52 AM11/8/10
to appengine-after...@googlegroups.com
Revision: 9
Author: mike.a...@gmail.com
Date: Mon Nov 8 03:15:55 2010
Log: Initial prototy of materiazed map view.


http://code.google.com/p/appengine-afterburner/source/detail?r=9

Added:
/trunk/python/src/afterburner/experimental/db/materialized_map.py
/trunk/python/src/afterburner/experimental/db/materialized_map_test.py

=======================================
--- /dev/null
+++ /trunk/python/src/afterburner/experimental/db/materialized_map.py Mon
Nov 8 03:15:55 2010
@@ -0,0 +1,70 @@
+#!/usr/bin/env python
+#
+# Copyright 2010 Google Inc.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+from afterburner.experimental.db import triggers
+from afterburner import util
+from google.appengine.ext import db
+
+MAPS = {}
+
+
+# TODO: use mapname as key name => save on property
+class SourceInfo(db.Model):
+ map_name = db.StringProperty()
+
+
+def trigger(key):
+ for f in MAPS.get(key.kind(), []):
+ def tx():
+ si_query = SourceInfo.all().ancestor(key).filter('map_name', f)
+ si_list = si_query.fetch(1)
+ si = None
+
+ if not si_list:
+ si = SourceInfo(parent=key, map_name=f)
+ db.put(si)
+ else:
+ # delete old
+ si = si_list[0]
+ q = db.Query()
+ q.ancestor(si)
+ db.delete(q)
+
+ entity = db.get(key)
+ if entity:
+ # create new
+ values = list(util.for_name(f)(entity))
+ for value in values:
+ value._parent = si
+ db.put(values)
+ else:
+ db.delete(si)
+
+ db.run_in_transaction(tx)
+
+
+# TODO: kind can be class
+def materialized_map(kind):
+ def decorator(f):
+ # TODO: duplicate registration
+ MAPS.setdefault(kind, []).append(
+ f.__module__ + "." + f.__name__)
+ triggers.TriggerManager.register_trigger(
+ kind, trigger.__module__ + "." + trigger.__name__)
+ return f
+
+ return decorator
+
=======================================
--- /dev/null
+++ /trunk/python/src/afterburner/experimental/db/materialized_map_test.py
Mon Nov 8 03:15:55 2010
@@ -0,0 +1,82 @@
+#!/usr/bin/env python
+#
+# Copyright 2010 Google Inc.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import unittest
+
+from afterburner.experimental.db import materialized_map as mmap
+from afterburner.experimental.db import triggers
+from afterburner.testing import test_base
+from google.appengine.ext import db
+
+
+class Foo(db.Model):
+ int_value = db.IntegerProperty()
+
+
+class Bar(db.Model):
+ int_value = db.IntegerProperty()
+
+
+...@mmap.materialized_map(Foo.kind())
+def inc_foo(foo):
+ yield Bar(int_value=foo.int_value + 1)
+
+
+class MaterialzedMapTest(test_base.TestBase):
+ """Base class for triggers tests."""
+
+ def setUp(self):
+ super(MaterialzedMapTest, self).setUp()
+
+ def testCreate(self):
+ foo = Foo(int_value=11)
+ foo.put()
+
+ mmap.trigger(foo.key())
+
+ bars = Bar.all().fetch(10)
+ self.assertEquals(1, len(bars))
+ self.assertEquals(12, bars[0].int_value)
+
+ def testUpdate(self):
+ foo = Foo(int_value=11)
+ foo.put()
+ mmap.trigger(foo.key())
+
+ foo.int_value = 12
+ foo.put()
+ mmap.trigger(foo.key())
+
+ bars = Bar.all().fetch(10)
+ self.assertEquals(1, len(bars))
+ self.assertEquals(13, bars[0].int_value)
+
+ def testDelete(self):
+ foo = Foo(int_value=11)
+ foo.put()
+ mmap.trigger(foo.key())
+
+ foo.delete()
+ mmap.trigger(foo.key())
+
+ bars = Bar.all().fetch(10)
+ self.assertEquals(0, len(bars))
+
+
+if __name__ == '__main__':
+ unittest.main()
+
+

Reply all
Reply to author
Forward
0 new messages