Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ogarrett
Created April 25, 2013 14:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ogarrett/5459933 to your computer and use it in GitHub Desktop.
Save ogarrett/5459933 to your computer and use it in GitHub Desktop.
libTable: TrafficScript libraries to manage on-disk tables of data

These Riverbed TrafficScript libraries implements a means to efficiently search tables of data stored as files in the Resource directory (conf/extra) of Stingray Traffic Manager

For more details and usage instructions, check out https://splash.riverbed.com/docs/DOC-1618

Tags: #Stingray #TrafficScript #splash.riverbed.com

# libTable.rts
#
# Efficient lookups of key/value data in large resource files (>100 lines)
# Use getFirst() and getNext() to iterate through the table
sub lookup( $filename, $key ) {
update( $filename );
$pid = sys.getPid();
return data.get( "resourcetable".$pid.$filename."::".$key );
}
sub getFirst( $filename ) {
update( $filename );
$pid = sys.getPid();
return data.get( "resourcetable".$pid.$filename.":first" );
}
sub getNext( $filename, $key ) {
update( $filename );
$pid = sys.getPid();
return data.get( "resourcetable".$pid.$filename.":next:".$key );
}
# Internal functions
sub update( $filename ) {
$pid = sys.getPid();
$md5 = resource.getMD5( $filename );
if( $md5 == data.get( "resourcetable".$pid.$filename.":md5" ) ) return;
data.reset( "resourcetable".$pid.$filename.":" );
data.set( "resourcetable".$pid.$filename.":md5", $md5 );
$contents = resource.get( $filename );
$pkey = "";
foreach( $l in string.split( $contents, "\n" ) ) {
if( ! string.regexmatch( $l, "(.*?)\\s+(.*)" ) ) continue;
$key = string.trim( $1 );
$value = string.trim( $2 );
data.set( "resourcetable".$pid.$filename."::".$key, $value );
if( !$pkey ) {
data.set( "resourcetable".$pid.$filename.":first", $key );
} else {
data.set( "resourcetable".$pid.$filename.":next:".$pkey, $key );
}
$pkey = $key;
}
}
# libTableSmall.rts: Efficient lookups of key/value data in a small resource file (<100 lines)
sub lookup( $filename, $key ) {
$contents = resource.get( $filename );
if( string.regexmatch( $contents, '\n'.$key.'\s+([^\n]+)' ) ) return $1;
if( string.regexmatch( $contents, '^'.$key.'\s+([^\n]+)' ) ) return $1;
return "";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment