编写领域特定语言,用于从表中选择行 [英] Writing a Domain Specific Language for selecting rows from a table

查看:139
本文介绍了编写领域特定语言,用于从表中选择行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写,我希望由许多不同的人可以运行一个服务器,而不是所有的人我会直接接触。服务器将相互在集群通信。的服务器的功能部件包括从一个潜在的非常大的表中选择行的一小部分。哪些行被选中的准确选择,将需要一些调整,这是重要的,它可能运行在群集(如我)在没有得到每一个服务器管理员部署服务器的新版本更新选择标准的人

简单地写在Python的功能是不是一个真正的选择,因为没有人会想安装一个服务器下载并在运行时执行任意Python code。

我需要的是在实现一个领域特定语言来实现这一目标最简单的方法建议。语言需要能够简单的EX pression评估,以及查询表的索引和迭代通过返回的行。易于书写和阅读的语言是次要的,以减轻实现它。我还preFER不要有写一个完整的查询优化,使的东西,明确指定了索引的查询将是理想的。

这将对编译针对该接口将在能力,什么App Engine数据存储的出口相似:您可以查询顺序范围上的任何索引表(例如,小于,大于,范围和平等的查询),那么任何布尔EX pression筛选返回的行。您也可以连接多个独立的结果集在一起。

我意识到这个问题听起来很像我所要求的SQL。但是,我不想,要求数据存储备份这些数据是一个关系型数据库,而且我也不想试图重新实现的SQL自己的开销。我还涉及仅一个表具有已知架构。最后,没有联接是必需的。事情要简单得多将远远preferable。

编辑:扩展的描述,澄清一些误解。

解决方案

建立一个DSL是除$ P $被Python PTED。

步骤1.构建运行时类和对象。这些类将所有的光标循环和SQL语句,并都在他们的方法局促的算法处理。你会大量使用命令和的战略设计模式来构建这些类。大多数东西都是一个命令,选项和选择是插入式的策略。看设计为Apache Ant的任务 API - 这是一个很好的例子。

第2步:验证的对象,本系统的实际工作。确保设计简单,完整。你的测试将建设指挥部和战略的对象,然后执行顶层Command对象。该命令对象将做的工作。

在这一点上你基本上完成。您的运行时间是从上述域创建的对象只是一个配置。 [这并不像听起来那么容易。它需要一些护理定义一组可以被实例化,然后在彼此交谈做你的应用程序的工作类。]

请注意,你将有需要不外乎声明。有什么不好的程序?一个你开始写程序性元素的DSL,您发现您需要越来越多的功能,直到你写的Python具有不同的语法。不好。

此外,程序语言间preters仅仅是很难写。国家执行和参考范围仅仅是很难管理。

您可以使用本机Python - 停止担心失控沙箱。事实上,这就是你将如何进行单元测试的一切,用一个简短的Python脚本来创建你的对象。 Python将是DSL。

[别急,你说,如果我只是使用Python作为DSL的人可以执行任意的事情。要看什么是对的PYTHONPATH和sys.path中。看看各种方法来控制什么可用的网站模块。]

一个声明DSL是最简单的。这是完全实乃再presentation。 Python的一个块,仅仅设置一些变量的值是很好的。这就是Django使用。

您可以使用 ConfigParser 为重presenting语言物体的运行时配置。

您可以使用 JSON 或的 YAML 为重presenting对象的运行时配置的语言。现成的解析器是完全可用。

您可以使用XML,太。这是很难设计和解析,但它工作正常。人们喜欢它。这就是Ant和Maven(以及许多其他工具)使用声明的语法来描述程序。我不建议这样做,因为它是在颈部罗嗦疼痛。我建议只使用Python的。

或者,你可以去离深端和发明自己的语法和编写自己的解析器。

I'm writing a server that I expect to be run by many different people, not all of whom I will have direct contact with. The servers will communicate with each other in a cluster. Part of the server's functionality involves selecting a small subset of rows from a potentially very large table. The exact choice of what rows are selected will need some tuning, and it's important that it's possible for the person running the cluster (eg, myself) to update the selection criteria without getting each and every server administrator to deploy a new version of the server.

Simply writing the function in Python isn't really an option, since nobody is going to want to install a server that downloads and executes arbitrary Python code at runtime.

What I need are suggestions on the simplest way to implement a Domain Specific Language to achieve this goal. The language needs to be capable of simple expression evaluation, as well as querying table indexes and iterating through the returned rows. Ease of writing and reading the language is secondary to ease of implementing it. I'd also prefer not to have to write an entire query optimiser, so something that explicitly specifies what indexes to query would be ideal.

The interface that this will have to compile against will be similar in capabilities to what the App Engine datastore exports: You can query for sequential ranges on any index on the table (eg, less-than, greater-than, range and equality queries), then filter the returned row by any boolean expression. You can also concatenate multiple independent result sets together.

I realise this question sounds a lot like I'm asking for SQL. However, I don't want to require that the datastore backing this data be a relational database, and I don't want the overhead of trying to reimplement SQL myself. I'm also dealing with only a single table with a known schema. Finally, no joins will be required. Something much simpler would be far preferable.

Edit: Expanded description to clear up some misconceptions.

解决方案

Building a DSL to be interpreted by Python.

Step 1. Build the run-time classes and objects. These classes will have all the cursor loops and SQL statements and all of that algorithmic processing tucked away in their methods. You'll make heavy use of the Command and Strategy design patterns to build these classes. Most things are a command, options and choices are plug-in strategies. Look at the design for Apache Ant's Task API -- it's a good example.

Step 2. Validate that this system of objects actually works. Be sure that the design is simple and complete. You're tests will construct the Command and Strategy objects, and then execute the top-level Command object. The Command objects will do the work.

At this point you're largely done. Your run-time is just a configuration of objects created from the above domain. [This isn't as easy as it sounds. It requires some care to define a set of classes that can be instantiated and then "talk among themselves" to do the work of your application.]

Note that what you'll have will require nothing more than declarations. What's wrong with procedural? One you start to write a DSL with procedural elements, you find that you need more and more features until you've written Python with different syntax. Not good.

Further, procedural language interpreters are simply hard to write. State of execution, and scope of references are simply hard to manage.

You can use native Python -- and stop worrying about "getting out of the sandbox". Indeed, that's how you'll unit test everything, using a short Python script to create your objects. Python will be the DSL.

["But wait", you say, "If I simply use Python as the DSL people can execute arbitrary things." Depends on what's on the PYTHONPATH, and sys.path. Look at the site module for ways to control what's available.]

A declarative DSL is simplest. It's entirely an exercise in representation. A block of Python that merely sets the values of some variables is nice. That's what Django uses.

You can use the ConfigParser as a language for representing your run-time configuration of objects.

You can use JSON or YAML as a language for representing your run-time configuration of objects. Ready-made parsers are totally available.

You can use XML, too. It's harder to design and parse, but it works fine. People love it. That's how Ant and Maven (and lots of other tools) use declarative syntax to describe procedures. I don't recommend it, because it's a wordy pain in the neck. I recommend simply using Python.

Or, you can go off the deep-end and invent your own syntax and write your own parser.

这篇关于编写领域特定语言,用于从表中选择行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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