开发者

Ensuring the right kind of operations on the right types

I want to write a program to load a key-value store with lots of random data of random types. Assuming the key-value store supports three types ( strings, lists and sets ) there are operations that are valid for a set (say, union) that are not valid for strings.

#define SUBSTR 0  // substring, only on strings 
#define SEARCH 1 // search, only on strings
#define PUSH 3 // only on lists
#define ADD 4 // only on sets
#define UNION 5 // only on sets

I am populating a table with random operations but want to ensure that those operations are executed on the key of the proper type. For example, ensuring that union operations are always executed on keys of type set.

int optab[100];
optab[0] = ADD
optab[1] = SUBSTR
.
.
.
optab[99] = UNION

For each operation to be performed, I could record the type of each key locally at the time the key is first created in my program or keep traversing the keyspace till I find the key of the right type. But I don't want to do either.

开发者_JS百科

With the first option the size of the structure used to maintain the details grows as new keys (and types) are added. And with the second option the time needed to find the right key for the type grows as keys (and types) are added.

Is there a clever way to match types and operations properly without depending on the size of keyspace or the number of types supported?

I am hoping that given an operation I could generate a random key name (like, key90876) with 90876 being the random part that would be of the right type.


If each operation is valid for only one type, you wouldn't require any extra space or time - you just load the keys into different lists based on their type and each operation would work on the corresponding list.

If the same operation may apply to more than one type, then there isn't any specifically "clever" way to do this. It's a matter of space-time tradeoff and you should choose whichever method suits you best. Randomly picking keys until it matches an operation will work, but there is no guarantee as to how many tries you need before you get the right type of key.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜