[英]Ruby MongoDB Driver Client Side Query Matcher
我正在使用WebSockets在Ruby中构建一个系统,该系统将通知JS客户端有关适用于JS客户端正在查看的模型和集合的集合的更改。 我希望JS客户端定期向WebSocket发送注册消息,告知其当前正在查看的模型以及集合(或查询指定的集合子集)。
因此,为了使此工作正常进行,托管WebSocket服务器的API将需要测试查询是否与已更新/创建的文档匹配。 我不想在不向Mongo发送查询的情况下执行此操作,因此我在C驱动程序中找到了可在(mongo)客户端运行的解决方案: http : //api.mongodb.org/c/current/mongoc_matcher_new.html http://api.mongodb.org/c/current/matcher.html
不幸的是,我没有看到通过Ruby驱动程序调用此方法的方法。 有什么线索可以在Ruby中使用mongoc_matcher_new函数吗? 还是有人有更好的建议来改进此解决方案的体系结构,以便仅向JS客户端发送适用的更新?
我认为如果不查询Mongo,您将无法做到这一点。 执行此操作的标准方法是拖尾oplog,但是oplog仅会为您提供数据库/集合和_id。 因此,我认为您仅靠oplog就无法支持任意查询,而是需要获取文档才能确定匹配项。
我建议您看看流星如何做到这一点。 这篇博客文章概述了他们的方法。 在OplogObserveDriver上还有一个维基页面,其中包含更多详细信息 。
我最终使用FFI运行所需的代码:
MongoC.test_query_match(document_string, json_query_string)
C代码:
#include <bcon.h>
#include <mongoc.h>
#include <stdio.h>
int test_query_match(char *document, char *query) {
mongoc_matcher_t *matcher;
bson_t *d;
bson_t *q;
bson_error_t doc_parse_error;
bson_error_t query_parse_error;
bson_error_t matcher_error;
d = bson_new_from_json(document, strlen(document), &doc_parse_error);
q = bson_new_from_json(query, strlen(query), &query_parse_error);
matcher = mongoc_matcher_new(q, &matcher_error);
if ( !matcher ) {
bson_destroy(q);
bson_destroy(d);
return 0;
}
int match = mongoc_matcher_match(matcher, d);
bson_destroy(q);
bson_destroy(d);
mongoc_matcher_destroy(matcher);
return match;
}
Ruby FFI代码:
require 'ffi'
module MongoC
extend FFI::Library
ffi_lib 'c'
ffi_lib File.dirname(__FILE__) + '/mongoc/mongoc.so'
attach_function :test_query_match, [:string, :string], :int
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.