News for hackers. Stuff that matters.
Re:NoSQL with MySQL in Ruby - Friendly by uggedal on Thursday December 24, 2009 @04:04PM (#1014391)
Tokyo Tyrant's Lua scripting of the db server has been great when developing http://wasitup.com - The following example would be hard to implement in an efficient manner with a traditional key/value store without scripting capabilities:

  -- Return keys with a given prefix for a particular node where there are a
  -- known total of nodes. Uses the reamainder of the keys' hash values to
  -- segment the keys.
  function slice(prefix, index_and_total)
    local args = _split(index_and_total, "\t")
    if #args ~= 2 then
      return nil
    end
  
    local index = tonumber(args[1])
    local total = tonumber(args[2])
  
    local res = {}
  
    local keys = _fwmkeys(prefix)
    for i = 1, #keys do
      local key = keys[i]
      local url = string.sub(
        key,
        string.find(key, ":", string.find(key, ":", 1, true) + 1, true) + 1
      )
  
      local modulo = tonumber(string.sub(_hash("md5", url), 1, 10), 16) % total
  
      if modulo == index then
        table.insert(res, key)
      end
    end
  
    if #res == 0 then
      return nil
    end
  
    return table.concat(res, "\t")
  end

There are no comments on this comment yet.