English 中文(简体)
• 同步地寻找分级配对。 回收? 固定式
原标题:Finding a substring match asynchronously. Recursion? Flattening?

我真的想像不要再dis,但我可以 con笑。 再次重申 在传统数据库和语言中,我的任务很简单。 我的问题更是如何在同步的数据库中实现控制流动和逻辑,而不是我解决问题的办法是否最佳。

在此,我试图做些什么:我重开了由言辞组成的钥匙,只说<条码>、<条码>和<条码>。 现在,鉴于一项投入,我想知道最长的补贴是什么,与重新处置的关键相称。 我只需要从特定扼杀的零点开始检查次点,因此,复杂性很低。

例:cardinal 的关键代码<>card/code>载于其中,car,但card更长。 <代码>Cape不匹配任何钥匙。

我的方法是:从整个扼杀和检查开始,如果它与钥匙相匹配。 如果是的话,那是关键。 否则,又重复了同样的进程,其次重于最后的性质。

How can I accomplish this task? Different approaches are welcome.

我对<代码>sync的图书馆知之甚少,它像<代码>水深/代码”一样,对Im做的最好。 然而,我似乎需要从扼杀.length,扼杀所有职能。 期限-1等到最后一种性质。 我所期望的是,一个休息的休息场所是好的替代品。

Below I test with an input I assume is always 3 characters or more (because it s already ugly and more nesting seems pointless for testing). It works, carde resulting in card, and care -> car. Nonsense gives no match.

var http = require("http");
var redis = require("redis");

http.createServer(function(request, response) {
    client = redis.createClient();
    word = "carde";
    client.keys(word, function(err, reply) {
        if(err) { response.end(err); client.end(); }
        else {
          if(reply.length > 0) {
            response.end(word);
            client.end();
          }
          else {
            client.keys(word.slice(0,-1), function(err, reply) {
                if(err) { response.end(err); client.end(); }
                else {
                  if(reply.length > 0) {
                    response.end(word.slice(0, -1));
                    client.end();
                  }
                  else {
                    client.keys(word.slice(0, -2), function(err,reply) {
                        if(err) { response.end(err); client.end(); }
                        else {
                          if(reply.length > 0) {
                            response.end(word.slice(0, -2));
                            client.end();
                          }
                          else {
                            response.end("no match");
                          }
                        }
                      });
                  }
                }
              });
          }
        }
    });
  }).listen(8000);

我也试图再次入侵,这可能是最佳方法。 (Thanks to Ti个月y Strimple for recing a mis).

http.createServer(function(request, response) {
    client = redis.createClient();
    recursiveKeys("cardinalsin", client, response);
  }).listen(8000);

function recursiveKeys(word, client, response) {
  if (word.length == 0) {
    response.write("0");
    response.end();
    client.end();
  }
  else {
    client.keys(word, function(err, reply) {
        if(err) {
          response.write("0");
          response.end();
          client.end();
        }
        else {
          if(reply.length > 0) {
            response.write(word);
            response.end();
            client.end();
          }
          else {
            return recursiveKeys(word.slice(0,-1), client, response);
          }
        }
      });
  }
}
最佳回答

I agree that a recursive solution is probably the best. I approached this problem before studying your code (so as to not affect the experiment) and arrived at a very similar solution. Search for a word with http://localhost:3000/?word=cardinal

var http  = require( http );
var redis = require( redis );
var url   = require( url );

var findWord = function(needle, client, callback) {
  if (needle.length <= 0) { return callback("Word not found") }

  client.keys(needle, function(err, reply) {
    if (err) { callback("Word not found"); }
    else {
      if (reply.length > 0) {
        callback(needle);
      } else {
        findWord(needle.slice(0, -1), client, callback);
      }
    }
  });
};

var server = http.createServer(function(request, response) {
  var query  = url.parse(request.url, true).query;
  var word   = query.word || "";
  var client = redis.createClient();

  findWord(word, client, function(found) {
    client.end();
    response.writeHead(200, {"Content-Type": "text/plain"});
    response.end(found);
  });
});
server.listen(3000);
问题回答

暂无回答




相关问题
How do you mix SQL DB vs. Key-Value store (i.e. Redis)

I m reviewing my code and realize I spend a tremendous amount of time taking rows from a database, formatting as XML, AJAX GET to browser, and then converting back into a hashed javascript object ...

Predis sharding (consistent hashing)

Predis claim to have Client-side sharding (support for consistent hashing of keys). http://github.com/nrk/predis I can do sharding using connect to an array of profiles (nodes) but it isn t ...

key value stores for extendable objects

http://www.infoq.com/presentations/newport-evolving-key-value-programming-model is a video about KV stores, and the whole premise is that redis promotes a column-based style for storing the attributes ...

nginx/redis and handling tracking params in url

I am using nginx and redis in my website. For several items on my site, I want to add tracking params to their urls so that when a user clicks on an item, I can collect statistics of user usage apart ...

热门标签