Google Appengine数据存储上的层次结构优化 [英] Hierarchy Optimization on Google Appengine Datastore

查看:86
本文介绍了Google Appengine数据存储上的层次结构优化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 类ToolCategories(db.Model)使用一个类似于下面的模型存储在数据存储中的分层数据: :
name = db.StringProperty()
parentKey = db.SelfReferenceProperty(collection_name =parent_category)
...
...

我想打印保存层次结构的所有类别名称,例如以这种形式表示:

   - 信息收集
---- OS指纹识别
---- DNS
------ dnstool
----端口扫描
------ windows
-------- nmap
---- DNS3
----无线嗅探器
------ Windows
-------- Kismet

为了实现上述目的,我使用了简单递归使用反向引用功能:

  class GetAllCategories(webapp.RequestHandler) :


def RecurseList(self,object,breaks):
output = breaks + obje ct.name +< / br>
for object.parent_category:
output = output + self.RecurseList(cat,breaks + - )

返回输出



def get(self):
output =
allCategories = ToolCategories.all()。filter('parentKey =',None)
for category in allCategories:
output = output + self.RecurseList(category, - )

self.response.out.write(输出)


由于我对App引擎编程非常陌生(自从我开始编写代码几乎没有3天),我不确定这是从数据存储访问的角度来完成所需的工作。

这是最好的方法吗?如果不是什么?

解决方案

您有一个非常合理的方法!我的主要警告是与GAE很少有关系,而且与Python有很多关系:不要 + + = 。相反,你要制作一个字符串片段列表(带有 append extend 或list comprehensions& c),当你'所有的人都已经完成了,你可以用''join(thelist)或类似的方法加入最终的字符串结果。尽管最近的Python版本努力优化 + 或<$ c本质上的 O(N squared)性能$ c> + = 循环,最后你总是更好的建立一个字符串列表,并且''。join ing他们在最后!


I have hierarchical data stored in the datastore using a model which looks like this:

class ToolCategories(db.Model):  
   name = db.StringProperty()  
   parentKey = db.SelfReferenceProperty(collection_name="parent_category")  
   ...  
   ...  

I want to print all the category names preserving the hierarchy, say in some form like this :

--Information Gathering  
----OS Fingerprinting  
----DNS  
------dnstool  
----Port Scanning   
------windows  
--------nmap  
----DNS3  
----wireless sniffers  
------Windows  
--------Kismet  

To do the above I have used simple recursion using the back referencing capability:

class GetAllCategories (webapp.RequestHandler) :


        def RecurseList(self, object, breaks) :
                output = breaks + object.name + "</br>"
                for cat in object.parent_category:
                        output = output + self.RecurseList(cat, breaks + "--")

                return output



        def get (self) :
                output = ""
                allCategories = ToolCategories.all().filter(' parentKey = ', None)
                for category in allCategories :
                        output = output + self.RecurseList(category, "--")

                self.response.out.write(output)

As I am very new to App engine programming (hardly 3 days since I started writing code), I am not sure if this the most optimized way from the Datastore access standpoint to do the desired job.

Is this the best way? if not what is?

解决方案

You have a very reasonable approach! My main caveat would be one having little to do with GAE and a lot with Python: don't build a string from pieces with + or +=. Rather, you make a list of string pieces (with append or extend or list comprehensions &c) and when you're all done you join it up for the final string result with ''.join(thelist) or the like. Even though recent Python versions strive hard to optimize the intrinsically O(N squared) performance of the + or += loops, in the end you're always better off building up lists of strings along the way and ''.joining them up at the very end!

这篇关于Google Appengine数据存储上的层次结构优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆