Skip to content

Instantly share code, notes, and snippets.

@sharonovd
Created February 27, 2019 15:16
Show Gist options
  • Save sharonovd/575234521f1f5d6ca463b392235c1f7a to your computer and use it in GitHub Desktop.
Save sharonovd/575234521f1f5d6ca463b392235c1f7a to your computer and use it in GitHub Desktop.
Sample app for prefix-based selection
local fio = require('fio')
local log = require('log')
local json = require('json')
local utf = require('utf8')
box.cfg({})
--- specify data schema
box.once('init_addresses', function()
box.schema.create_space('address')
box.space.address:create_index('prefix', { type = 'tree', parts = { { 1, 'str', collation = 'unicode_ci' } }, unique = true })
end)
--- stored procedure for case-insensitive prefix-based search
select_by_prefix = function(prefix)
local result = {}
for _, addr in box.space.address.index.prefix:pairs(prefix, { iterator = 'GT' }) do
--log.debug(json.encode(addr))
if utf.casecmp(utf.sub(addr[1], 1, utf.len(prefix)), prefix) == 0 then
table.insert(result, addr)
else
break
end
end
return result
end
@o2gy84
Copy link

o2gy84 commented Mar 1, 2019

You should use iterator = 'GE' in case you want to search exact matches

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment