Created
February 27, 2019 15:16
-
-
Save sharonovd/575234521f1f5d6ca463b392235c1f7a to your computer and use it in GitHub Desktop.
Sample app for prefix-based selection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You should use
iterator = 'GE'
in case you want to search exact matches