Skip to content

Commit e00fd42

Browse files
lockerkyukhin
authored andcommitted
index: add iterator_space helper
The new helper function returns the space the iterator was created for. It looks up the space by id and caches it in iterator struct. We need it for space upgrade: we will use it in box_iterator_next() to update tuples returned by iterator_next(). NO_DOC=internal NO_TEST=internal NO_CHANGELOG=internal
1 parent defe421 commit e00fd42

File tree

3 files changed

+55
-8
lines changed

3 files changed

+55
-8
lines changed

src/box/index.cc

+31-7
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ box_index_iterator(uint32_t space_id, uint32_t index_id, int type,
398398
txn_rollback_stmt(txn);
399399
return NULL;
400400
}
401+
it->space = space;
401402
txn_commit_ro_stmt(txn, &svp);
402403
rmean_collect(rmean_box, IPROTO_SELECT, 1);
403404
return it;
@@ -461,6 +462,27 @@ iterator_create(struct iterator *it, struct index *index)
461462
it->space_id = index->def->space_id;
462463
it->index_id = index->def->iid;
463464
it->index = index;
465+
it->space = NULL;
466+
}
467+
468+
/**
469+
* Helper function that checks that the iterated index wasn't dropped
470+
* and updates it->space and it->space_cache_version on success.
471+
* Returns 0 on success, -1 on failure.
472+
*/
473+
static int
474+
iterator_check_space(struct iterator *it)
475+
{
476+
struct space *space = space_by_id(it->space_id);
477+
if (space == NULL)
478+
return -1;
479+
struct index *index = space_index(space, it->index_id);
480+
if (index != it->index ||
481+
index->space_cache_version > it->space_cache_version)
482+
return -1;
483+
it->space_cache_version = space_cache_version;
484+
it->space = space;
485+
return 0;
464486
}
465487

466488
static bool
@@ -470,18 +492,20 @@ iterator_is_valid(struct iterator *it)
470492
if (it->space_id == 0)
471493
return true;
472494
if (unlikely(it->space_cache_version != space_cache_version)) {
473-
struct space *space = space_by_id(it->space_id);
474-
if (space == NULL)
495+
if (iterator_check_space(it) != 0)
475496
return false;
476-
struct index *index = space_index(space, it->index_id);
477-
if (index != it->index ||
478-
index->space_cache_version > it->space_cache_version)
479-
return false;
480-
it->space_cache_version = space_cache_version;
481497
}
482498
return true;
483499
}
484500

501+
struct space *
502+
iterator_space_slow(struct iterator *it)
503+
{
504+
if (iterator_check_space(it) != 0)
505+
return NULL;
506+
return it->space;
507+
}
508+
485509
int
486510
iterator_next(struct iterator *it, struct tuple **ret)
487511
{

src/box/index.h

+23
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,11 @@ struct iterator {
300300
* state has not changed since the last lookup.
301301
*/
302302
struct index *index;
303+
/**
304+
* Pointer to the space this iterator is for.
305+
* Don't access directly, use iterator_space().
306+
*/
307+
struct space *space;
303308
};
304309

305310
/**
@@ -312,6 +317,24 @@ struct iterator {
312317
void
313318
iterator_create(struct iterator *it, struct index *index);
314319

320+
/** iterator_space() slow path. */
321+
struct space *
322+
iterator_space_slow(struct iterator *it);
323+
324+
/**
325+
* Returns the space this iterator is for or NULL if the iterator is invalid
326+
* (e.g. the index was dropped).
327+
*/
328+
static inline struct space *
329+
iterator_space(struct iterator *it)
330+
{
331+
extern uint32_t space_cache_version;
332+
if (likely(it->space != NULL &&
333+
it->space_cache_version == space_cache_version))
334+
return it->space;
335+
return iterator_space_slow(it);
336+
}
337+
315338
/**
316339
* Iterate to the next tuple.
317340
*

src/box/memtx_engine.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ enum memtx_reserve_extents_num {
118118
* allocated for each iterator (except rtree index iterator that
119119
* is significantly bigger so has own pool).
120120
*/
121-
#define MEMTX_ITERATOR_SIZE (184)
121+
#define MEMTX_ITERATOR_SIZE (192)
122122

123123
struct memtx_engine {
124124
struct engine base;

0 commit comments

Comments
 (0)