世上本没有路,走的人多了也就成了路。为什么不能在 Postgres上建一个MongoDB 呢?
Postgres 社区在 NoSQL采取一系列动作后并没有坐以待毙. Postgres一直在进步: 集成了 JSON和 PLV8. PLV8 引入了 V8 Javascript引擎 . 操作 JSON也更简单了 (需要验证).
开始前需要做的准备:
- Postgres 9.2+ (as of this blog entry, 9.2 is in beta) - http://www.postgresql.org/ftp/source/
- V8 - https://github.com/v8/v8
- PLV8 - http://code.google.com/p/plv8js/wiki/PLV8
MongoDB的最低级别是集合. 集合可以用表来表示:
- CREATE TABLE some_collection (
- some_collection_id SERIAL NOT NULL PRIMARY KEY,
- data JSON
- );
字符型的JSON 被保存在 Postgres 表里,简单易行 (现在看是这样).
下面实现自动创建集合. 保存在集合表里:
- CREATE TABLE collection (
- collection_id SERIAL NOT NULL PRIMARY KEY,
- name VARCHAR
- );
- -- make sure the name is unique
- CREATE UNIQUE INDEX idx_collection_constraint ON collection (name);
一旦表建好了,就可以通过存储过程自动创建集合. 方法就是先建表,然后插入建表序列.
- CREATE OR REPLACE FUNCTION create_collection(collection varchar) RETURNS
- boolean AS $$
- var plan1 = plv8.prepare('INSERT INTO collection (name) VALUES ($1)', [ 'varchar' ]);
- var plan2 = plv8.prepare('CREATE TABLE col_' + collection +
- ' (col_' + collection + '_id INT NOT NULL PRIMARY KEY, data JSON)');
- var plan3 = plv8.prepare('CREATE SEQUENCE seq_col_' + collection);
- var ret;
- try {
- plv8.subtransaction(function () {
- plan1.execute([ collection ]);
- plan2.execute([ ]);
- plan3.execute([ ]);
- ret = true;
- });
- } catch (err) {
- ret = false;
- }
- plan1.free();
- plan2.free();
- plan3.free();
- return ret;
- $$ LANGUAGE plv8 IMMUTABLE STRICT;
有了存储过程,就方便多了:
- SELECT create_collection('my_collection');
解决了集合存储的问题,下面看看MongoDB数据解析. MongoDB 通过点式注解方法操作完成这一动作:
- CREATE OR REPLACE FUNCTION find_in_obj(data json, key varchar) RETURNS
- VARCHAR AS $$
- var obj = JSON.parse(data);
- var parts = key.split('.');
- var part = parts.shift();
- while (part && (obj = obj[part]) !== undefined) {
- part = parts.shift();
- }
- // this will either be the value, or undefined
- return obj;
- $$ LANGUAGE plv8 STRICT;
上述功能返回VARCHAR,并不适用所有情形,但对于字符串的比较很有用:
- SELECT data
- FROM col_my_collection
- WHERE find_in_obj(data, 'some.element') = 'something cool'
除了字符串的比较, MongoDB还提供了数字类型的比较并提供关键字exists . 下面是find_in_obj() 方法的不同实现:
- CREATE OR REPLACE FUNCTION find_in_obj_int(data json, key varchar) RETURNS
- INT AS $$
- var obj = JSON.parse(data);
- var parts = key.split('.');
- var part = parts.shift();
- while (part && (obj = obj[part]) !== undefined) {
- part = parts.shift();
- }
- return Number(obj);
- $$ LANGUAGE plv8 STRICT;
- CREATE OR REPLACE FUNCTION find_in_obj_exists(data json, key varchar) RETURNS
- BOOLEAN AS $$
- var obj = JSON.parse(data);
- var parts = key.split('.');
- var part = parts.shift();
- while (part && (obj = obj[part]) !== undefined) {
- part = parts.shift();
- }
- return (obj === undefined ? 'f' : 't');
- $$ LANGUAGE plv8 STRICT;
接下来是数据查询. 通过现有的材料来实现 find() 方法.
#p#
在本部分中将覆盖保存数据以及从MongDB查询中构建WHERE从句,以便检索我们已经写入的数据。
保存数据到集合中很简单。首先,我们需要检查JSON对象并寻找一个_id值。这部分代码是原生的假设,如果_id已存在这意味着一个更新,否则就意味着一个插入。请注意,我们目前还没有创建objectID,只使用了一个序列待其发生:
- CREATE OR REPLACE FUNCTION save(collection varchar, data json) RETURNS
- BOOLEAN AS $$
- var obj = JSON.parse(data);
- var id = obj._id;
- // if there is no id, naively assume an insert
- if (id === undefined) {
- // get the next value from the sequence for the ID
- var seq = plv8.prepare("SELECT nextval('seq_col_" +
- collection + "') AS id");
- var rows = seq.execute([ ]);
- id = rows[0].id;
- obj._id = id;
- seq.free();
- var insert = plv8.prepare("INSERT INTO col_" + collection +
- " (col_" + collection + "_id, data) VALUES ($1, $2)",
- [ 'int', 'json']);
- insert.execute([ id, JSON.stringify(obj) ]);
- insert.free();
- } else {
- var update = plv8.prepare("UPDATE col_" + collection +
- " SET data = $1 WHERE col_" + collection + "_id = $2",
- [ 'json', 'int' ]);
- update.execute([ data, id ]);
- }
- return true;
- $$ LANGUAGE plv8 IMMUTABLE STRICT;
基于这个观点,我们可以构建一些插入的简单文档:
- {
- "name": "Jane Doe",
- "address": {
- "street": "123 Fake Street",
- "city": "Portland",
- "state": "OR"
- },
- "age": 33
- }
- {
- "name": "Sarah Smith",
- "address": {
- "street": "456 Real Ave",
- "city": "Seattle",
- "state": "WA"
- }
- }
- {
- "name": "James Jones",
- "address": {
- "street": "789 Infinity Way",
- "city": "Oakland",
- "state": "CA"
- },
- "age": 23
- }
让我们创建一个集合并插入一些数据:
- work=# SELECT create_collection('data');
- create_collection
- -------------------
- t
- (1 row)
- work=# SELECT save('data', '{ our object }');
- save
- ------
- t
- (1 row)
英文原文:Building a MongoDB Clone in Postgres: Part 1
译文链接:http://www.oschina.net/translate/building_a_mongodb_clone_in_postgres_part_1