Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 73 lines (60 sloc) 1.649 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
-----------------------------------------------------------------------------
-- Tokyo Tyrant Set Operations (by Eivind Uggedal)
-- http://github.com/uggedal/tokyo-tyrant-sets/blob/f28b61e7c9e5aaa7282b9ef1c1bc81c48e806e6d/set.lua
-----------------------------------------------------------------------------
 
local SEP = '\n'
 
function _set_len(stream)
  local count = 0
  if stream then
    count = table.getn(_split(stream, SEP))
  end
  return count
end
 
function set_length(key, value)
  return _set_len(_get(key))
end

function set_get(key)
return _get(key)
end
 
function set_append(key, value)
  local stream = _get(key)
 
  if not stream then
    _put(key, value)
  else
    local set_len = _set_len(stream)
 
    if set_len == 1 then
      if stream == value then return nil end
    elseif set_len > 1 then
      for _, element in ipairs(_split(stream, SEP)) do
        if element == value then return nil end
      end
    end
    if not _putcat(key, SEP .. value) then
      return nil
    end
  end
  return value
end
 
function set_delete(key, value)
  local stream = _get(key)
 
  if stream then
    local set_len = _set_len(stream)
 
    if set_len == 1 and stream == value then
      if _out(key) then return value end
    elseif set_len > 1 then
      local found = -1
      local set_list = _split(stream, SEP)
 
      for i, element in ipairs(set_list) do
        if element == value then
          found = i
          break
        end
      end
 
      if found > -1 then
        table.remove(set_list, found)
        if _put(key, table.concat(set_list, SEP)) then return value end
      end
    end
  end
  return nil
end
Something went wrong with that request. Please try again.