Sunday, March 16, 2014

Sharding globals with GTM

With GTM and "^variable" (globals variables), you can shard easily to handle the massive number of records. You can see that as partitioned tables for ODBC. But on steroids :)

For example we want to shard usernames and userid. Let's say that our userid is a non_neg_integer() of 10 digits. And that we want to shard on the 7 first digits...

Extracting 7 characters from a string is done using the '$e[xtract]' function:

GTM> w $extract("12345678910",0,7)
1234567

Now that you can extract characters, you can use the "_" operators to concatenate the results with whatever you want. For this experimentation we will use the "^users" global:

GTM> w "^users"_$extract("12345678910",0,7)
^users1234567


Here's the full code:
shard(userid)
    q $$%shard("users",userid)

%shard(root,id)
    n gbl
    n prefix s prefix=$e(id,0,7)
    q "^"_root_prefix


Everything from the shell:

GTM> zed "users"
[ Copy/Paste the code and exists your editor ]
GTM> zl "users"
GTM> w $$shard^users(1234567890)
^users1234567


With this new function you can easily access any userid without thinking about the shard method. You just need to use the 'indirection' notation from GTM i.e.'@variable'.

userinfo(userid)
    n gbl s gbl=$$shard(userid)
    w "Login for userid: ",userid,!
    w @gbl@("login")
    q

No comments:

Sticky