Monday, October 24, 2011

Abt Some Linux Commands

Interesting Linux Terminal Commands

Notation => | - Cursor

CTRL + w
= Delete a word of the command typed
$ echo this is a line|
$ echo this is a |[After ^w]

CTRL + b = Move the cursor one character left[Backward]
$ cat filename|
$ cat filenam|e

CTRL + a = Move to the start of the command [Equivalent to Home]
$ cat filename|
$ |cat filename

CTRL + u = Clear the command from the cursor's point
$ cat file1 |file2
$ file2

CTRL + r
= Reversed search, Searchs for the pattern in the commands entered in the past and displays one after another continuously [Last entered command first]

CTRL + e
= Move to the end of the command
$ cat |file1 file2
$ cat file1 file2|

!! = Executes the last entered commmand

!$ = Returns the last argument of previously entered command
$ mkdir /home/user/temp
$ cd !$

!pattern = Executes the last command with this pattern

!pattern:p = Prints the last command with this pattern instead executing

_whatever = Never stores the command in history[_ = space character]
$  echo hai

Interesting things in vim

:earlier n[Sec 's'|Min 'm'|Hour 'h'|changes '']
:later n[Sec 's'|Min 'm'|Hour 'h'|changes '']

:earlier 10s

Switches from current to n[Sec|Min|Hour|changes] before[earlier] or after[later]

:w !sudo tee %
Write even in non-sudo user mode without exiting.

Indent the entire file

:set tabstop=n
Sets tab characters to n length

:set smartindent|autoindent|cindent
Sets the appropriate indentation

VU = Uppercase the current line

Vu = Lowecase the current line

VEU = Uppercase the word

g~~ = Invert case of the current line [~ - tilde]
In visual mode you can select the text and use tilde to invert case

CTRL + x = Decrements the number under the cursor by 1
CTRL + a = Increments the number under the cursor by 1

:split filename = Opens another file in the same window, split happens horizontally
:vsplit filename = Opens another file in the same window, split happens vertically
CTRL + w
[twice] = Move around the opened files

:hide = Hide current window
CTRL + g = Details of the file[Total no. of lines and % to go from current line]
H = Start of Screen 
M = Middle of Screen
L = Last of Screen
zt = Bring the line to the top [Kind of page up]
zz = Bring the line to the middle
zb = Bring the line to the bottom [Kind of page down]
G = End of file
gg = Start of file 
q: = Command History
q/ = Search History
$ = End of line
b = Beginning of non white space word in  backword direction
B = Beggining of non white space word in forward direction
* = Moves to next occurence [highlights all matches if enabled] of current word in forward direction
#Moves to next occurence of current word in backward direction
^ = Start of line
$ = End of line
<aB || >aB = Intend a Block
c% = Cut the current block
. = Repeat the previous action
CTRL+SHIFT+R+" = Paste the clip board content in insert mode
:set paste = Preserve Indentation
:set ignorecase = ignores case on file search 
All above mentioned commands are useful only if you find the use of them :) For example See the Ex. mentioned for !$ mentioned in the list.

Monday, October 10, 2011

Cassandra CQL PHP

After lots of googling I figured out how to access counter columns value via PHPCassa + CQL. Though it might not be efficient, this is all I was able to figure out after a long struggle :)

First Connecting to and Selection from Cassandra Instance via PHP using PHPCassa
$pool = new ConnectionPool("Stats",$servers);
$raw = $pool->get();
$rows = $raw->client->execute_cql_query("SELECT 'a' FROM Impressions WHERE KEY='xxx'", cassandra_Compression::NONE);
To Return Connection
To Update Counter value via PHPCassa
$pool = new ConnectionPool("Stats",$servers);
$raw = $pool->get();
$rows = $raw->client->execute_cql_query("UPDATE TestCounter SET 'counter'='counter'+1 WHERE KEY='xxx'", cassandra_Compression::NONE);
You can find a wrapper here

So, once you are done with fetch successfully. This is the result you may get
object(cassandra_CqlResult)#17 (3) {
  array(1) {
    object(cassandra_CqlRow)#18 (2) {
      string(4) "xxx"
      array(1) {
        object(cassandra_Column)#19 (4) {
          string(11) "a"
          string(8) " "                       // Actually there is the value between the quotes
The problem is CQL returns counter value as string rather int.

So, one general solution everyone would consider is hexdec($rows->rows[0]->columns[0]->value)

But the encoding of string returned is not a hex string. So, hexdec won't help.

So, I figured out a function to convert the string returned to a hex string
function strToHex($string)
for ($i=0; $i < strlen($string); $i++)
         $hex .= dechex(ord($string[$i]));  //ord returns the ascii equivalent of given char
     return $hex;
Now u have the hex string. Final step
$val = hexdec(strToHex($rows->rows[0]->columns[0]->value))
One more solution for the same
$data = unpack("H*",$rows->rows[0]->columns[0]->value);
$val = hexdec($data["1"]);

Reference: Hope it helps :)