Searching with multiple values in YDN-DB

185 views Asked by At

Is it possible to find multiple value (like sql IN keyword) in YDN-DB?

    //simple scenario
    "SELECT * FROM categories WHERE code IN ('AB','CD','EF','GH','IJ','KL');" 

    var code = "'AB','CD','EF','GH','IJ','KL'";
    var key_range = ydn.db.KeyRange.only(code);     
    cacheDb.values('categories','code',key_range).then(function(record){
        console.log(record);            
    },function(e){
        console.log(e);
    });
1

There are 1 answers

0
Kyaw Tun On BEST ANSWER

You can use multi query approach, as described in recently updated tutorial.

Basically, you will create iterator for each element of IN clause and join (union) the result, as follow:

var keys = [];
var multiQuery = function(sec_keys, pri_keys) {
  var advance = [];
  var cmp = ydn.db.cmp(pri_keys[0], pri_keys[1]);
  if (cmp == 1) { // pri_keys[0] > pri_keys[1]
    if (keys[keys.length - 1] != pri_keys[1]) {
      keys.push(pri_keys[1]);
    }
    advance[1] = true; // advance iter_1 on step
  } else if (cmp == -1) { // pri_keys[0] < pri_keys[1]
    if (keys[keys.length - 1] != pri_keys[0]) {
      keys.push(pri_keys[0]);
    }
    advance[1] = true; // advance iter_1 on step
  } else { // pri_keys[0] == pri_keys[1]
    if (keys[keys.length - 1] != pri_keys[0]) {
      keys.push(pri_keys[0]);
    }
    advance[0] = true; // advance iter_0 on step
    advance[1] = true; // advance iter_1 on step
  }

  if (keys.length >= 10) {
    return [];
  } else {
    return advance;
  }
};

Then use scan method:

db.scan(multiQuery, [iterators...]).done(function() {
  db.values('article', keys).done(function(values) {
    console.log(keys, values);
  })
});