views:

58

answers:

1

I'm trying to implement thinking-sphinx across multiple 'sites' hosted under a single rails application. I'm working with the developer of thinking-sphinx to sort through the finer details and am making good progress, but I need help with a maths problem:

Usually the formula for making a unique ID in a thinking-sphinx search index is to take the id, multiply it by the total number of models that are searchable, and add the number of the currently indexed model:

id * total_models + current_model

This works well, but now I also through an entity_id into the mix, so there are three vextors for making this ID unique. Could someone help me figure out the equation to gaurantee that the id's will never collide using these three variables:

id, total_models, total_entities

The entity ID is an integer.

I thought of:

id * (total_models + total_entities) + (current_model + current_entity)

but that results in collisions.

Any help would be greatly appreciated :)

A: 

Thought I'd clean up this tumbleweed. There was a comment that sort of gave a hint, but what I ended up doing was some bitshifting. It has its limits to the amount of entities you can have but it's 1024 (I think) in my example, which is plenty for us:

((primary_key * total_models + current_model_offset) << 10) + entity_id

Essentially we get our normal number, shift it to the left (or right) and add in the entity_id in the empty range we just opened up.

Brendon Muir