Skip to content
Snippets Groups Projects
Commit 75dad542 authored by Dmitry Simonenko's avatar Dmitry Simonenko
Browse files

sophia-index: proper iterator handling and destroy, added basic test.

parent f47b3bde
No related branches found
No related tags found
No related merge requests found
......@@ -68,7 +68,8 @@ key_validate(struct key_def *key_def, enum iterator_type type, const char *key,
* - ITERA_ALL iterator type, all index types
* - ITER_GE iterator in HASH index (legacy)
*/
if (key_def->type == TREE || type == ITER_ALL)
if (key_def->type == TREE || key_def->type == SOPHIA ||
type == ITER_ALL)
return;
/* Fall through. */
}
......
......@@ -156,6 +156,11 @@ class Index: public Object {
*/
Index(struct key_def *key_def);
/*
* Pre-allocated iterator to speed up the main case of
* box_process(). Should not be used elsewhere.
*/
struct iterator *m_position;
public:
virtual ~Index();
......@@ -194,12 +199,6 @@ class Index: public Object {
m_position = allocIterator();
return m_position;
}
private:
/*
* Pre-allocated iterator to speed up the main case of
* box_process(). Should not be used elsewhere.
*/
struct iterator *m_position;
};
/**
......
......@@ -65,39 +65,23 @@ sophia_gettuple(void *db, const char *key, size_t keysize)
auto scoped_guard = make_scoped_guard([=] { free(value); });
struct tuple *ret =
tuple_new(tuple_format_ber, value, value + valuesize);
tuple_ref(ret, 1);
return ret;
}
/* {{{ SophiaIndex */
SophiaIndex::SophiaIndex(struct key_def *key_def)
: Index(key_def)
{
db = NULL;
env = NULL;
}
SophiaIndex::~SophiaIndex()
{
if (db) {
int rc = sp_destroy(db);
if (rc == -1)
say_info("Sophia space %d close error: %s", key_def->space_id,
sp_error(env));
}
if (env) {
sp_destroy(env);
}
}
void
SophiaIndex::endBuild()
SophiaIndex::SophiaIndex(struct key_def *key_def_arg __attribute__((unused)))
: Index(key_def_arg)
{
env = sp_env();
if (env == NULL)
tnt_raise(ClientError, ER_MEMORY_ISSUE, sizeof(void*),
"SophiaIndex", "env");
auto env_freer =
make_scoped_guard([=] { sp_destroy(env); });
int rc = sp_ctl(env, SPCMP, sophia_index_compare, key_def);
if (rc == -1)
tnt_raise(ClientError, ER_SOPHIA, sp_error(env));
......@@ -115,6 +99,31 @@ SophiaIndex::endBuild()
tnt_raise(ClientError, ER_SOPHIA, sp_error(env));
say_info("Recover complete");
env_freer.is_active = false;
}
SophiaIndex::~SophiaIndex()
{
if (m_position != NULL) {
m_position->free(m_position);
m_position = NULL;
}
if (db) {
int rc = sp_destroy(db);
if (rc == -1)
say_info("Sophia space %d close error: %s", key_def->space_id,
sp_error(env));
}
if (env) {
sp_destroy(env);
}
}
void
SophiaIndex::endBuild()
{
}
size_t
......@@ -223,6 +232,7 @@ sophia_iterator_next(struct iterator *ptr)
const char *value = sp_value(it->cursor);
struct tuple *ret =
tuple_new(tuple_format_ber, value, value + valuesize);
tuple_ref(ret, 1);
return ret;
}
......
space = box.schema.create_space('tweedledum', { id = 123, engine = 'sophia' })
---
...
space:create_index('primary', { type = 'sophia', parts = {0, 'num'} })
---
...
for v=1, 10 do space:insert({v}) end
---
...
t = space.index[0]:select({}, {iterator = box.index.ALL})
---
...
t
---
- - [1]
- [2]
- [3]
- [4]
- [5]
- [6]
- [7]
- [8]
- [9]
- [10]
...
t = space.index[0]:select({}, {iterator = box.index.GE})
---
...
t
---
- - [1]
- [2]
- [3]
- [4]
- [5]
- [6]
- [7]
- [8]
- [9]
- [10]
...
t = space.index[0]:select(4, {iterator = box.index.GE})
---
...
t
---
- - [4]
- [5]
- [6]
- [7]
- [8]
- [9]
- [10]
...
t = space.index[0]:select({}, {iterator = box.index.LE})
---
...
t
---
- - [10]
- [9]
- [8]
- [7]
- [6]
- [5]
- [4]
- [3]
- [2]
- [1]
...
t = space.index[0]:select(7, {iterator = box.index.LE})
---
...
t
---
- - [7]
- [6]
- [5]
- [4]
- [3]
- [2]
- [1]
...
t = {}
---
...
for v=1, 10 do table.insert(t, space:get({v})) end
---
...
t
---
- - [1]
- [2]
- [3]
- [4]
- [5]
- [6]
- [7]
- [8]
- [9]
- [10]
...
space:drop()
---
...
os.execute("rm -rf space0123")
---
- 0
...
space = box.schema.create_space('tweedledum', { id = 123, engine = 'sophia' })
space:create_index('primary', { type = 'sophia', parts = {0, 'num'} })
for v=1, 10 do space:insert({v}) end
t = space.index[0]:select({}, {iterator = box.index.ALL})
t
t = space.index[0]:select({}, {iterator = box.index.GE})
t
t = space.index[0]:select(4, {iterator = box.index.GE})
t
t = space.index[0]:select({}, {iterator = box.index.LE})
t
t = space.index[0]:select(7, {iterator = box.index.LE})
t
t = {}
for v=1, 10 do table.insert(t, space:get({v})) end
t
space:drop()
os.execute("rm -rf space0123")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment