Thursday, May 21, 2015

Add a auto increment primary key to existing table in oracle

Suppose table1 is the table and its primary key is SLNO
First, create the sequence:
create sequence  table1_seq start with 1 increment by 1 nomaxvalue; 
Then create a trigger that increment upon insert:
create trigger table1_trigger
before insert on table1

for each row
   begin
     select table1_seq .nextval into :new.slno from dual;   end;

Wednesday, May 13, 2015

Some Linux System Commands

Kernal Parameters:

Kernel control parameters exist in  etc/sysctl.conf 

For changing any parameter or appending, after modifying the file for making it effected immediately:

/sbin/sysctl -p

------------------------------------
List of installed packages:  rpm -qa
------------------------------------
To get the RAM info: free -m
------------------------------------
To get the Hard drive info: df -h
------------------------------------------------
User Limits are defined in : /etc/security/limits.conf
------------------------------------------------
Making system Secured, Security Level config file/etc/selinux/config

Tuesday, May 12, 2015

UUID and its implementation

An UUID is a Universally Unique ID meaning that it will never appear anywhere else in the world. For the requirement  of  the IDs to be universally unique, UUIDs may be our only choice. We only need this if we're sharing our data publicly.

While we use UUIDs, we  store them as a number and not as a string. We don't mean inbinary format. We mean as a 128 bit number, rather than a 288 bit string. For instance, the word 'hello' in ASCII is 68 65 6C 6C 6F, which is the number 448,378,203,247. Storing the string '68656C6C6F' requires 10 bytes. The number 448,378,203,247 requires only 5. All in all, unless you really need the first U in UUID, you can't do much better than auto_increment If you have millions of records, then the saving in storage space will improve the performance . If our IDs do not need to be universally unique, then we can use auto_increment, which guarantees that IDs will be unique within a table (since the value will increment each time).

in Java to generate UUID we can follow the below snippet:

import java.util.UUID;

public class GenerateUUID {
  
  public static final void main(String... aArgs){
    //generate random UUIDs
    UUID idOne = UUID.randomUUID();
    UUID idTwo = UUID.randomUUID();
    log("UUID One: " + idOne);
    log("UUID Two: " + idTwo);
  }
  
  private static void log(Object aObject){
    System.out.println( String.valueOf(aObject) );
  }



Example run:
>java -cp . GenerateUUID
UUID One: 067e6162-3b6f-4ae2-a171-2470b63dff00 
UUID Two: 54947df8-0e9e-4471-a2f9-9af509fb5889


Since UUID is 128 bits and is written as hexadecimal, it's very easy to speed up and store the UUID.For that first, we need to use our programming language for removing the dashes

From 067e6162-3b6f-4ae2-a171-2470b63dff00 to 067e61623b6f4ae2a1712470b63dff00.
Now it's 32 chars (like an MD5 hash, which this also works with).
Since the binary format requires 1 bit to store what hexadecimal does in 4 bits, we will create a BINARY(16) field.

In mySql we can insert using:
INSERT INTO Table (FieldBin) VALUES (UNHEX("067e61623b6f4ae2a1712470b63dff00"))
and query using:
SELECT HEX(FieldBin) AS FieldBin FROM Table

Now in our programming language, we need to re-insert the dashes at the positions 9, 14, 19 and 24 to match your original UUID. If the positions are always different you could store that info in a second field.
Full example :

CREATE TABLE  `test_table` (
    `field_binary` BINARY( 16 ) NULL ,
    PRIMARY KEY (  `field_binary` )
) ENGINE = INNODB ;

INSERT INTO  `test_table` (
    `field_binary`
)
VALUES (
    UNHEX(  '067e61623b6f4ae2a1712470b63dff00' )
);

SELECT HEX(field_binary) AS field_binary FROM `test_table`

If you want to use this technique with any hex string, always do length / 2 for the field length. So for a sha512, the field would be BINARY (64) since a sha512 encoding is 128 characters long.

Wednesday, May 6, 2015

Python: String Formats

ESCAPEDESCRIPTIONEXAMPLE
%dDecimal integers (not floating point)."%d" % 45 == '45'
%iSame as %d."%i" % 45 == '45'
%oOctal number."%o" % 1000 == '1750'
%uUnsigned decimal."%u" % -1000 == '-1000'
%xHexadecimal lowercase."%x" % 1000 == '3e8'
%XHexadecimal uppercase."%X" % 1000 == '3E8'
%eExponential notation, lowercase 'e'."%e" % 1000 == '1.000000e+03'
%EExponential notation, uppercase 'E'."%E" % 1000 == '1.000000E+03'
%fFloating point real number."%f" % 10.34 == '10.340000'
%FSame as %f."%F" % 10.34 == '10.340000'
%gEither %f or %e, whichever is shorter."%g" % 10.34 == '10.34'
%GSame as %g but uppercase."%G" % 10.34 == '10.34'
%cCharacter format."%c" % 34 == '"'
%rRepr format (debugging format)."%r" % int == "'int'>"
%sString format."%s there" % 'hi' == 'hi there' %%A percent sign."%g%%" % 10.34 == '10.34%'



EScape



Python: Data Types

TYPEDESCRIPTIONEXAMPLE
TrueTrue boolean value.True or False == True
FalseFalse boolean value.False and True == False
NoneRepresents "nothing" or "no value".x = None
stringsStores textual information.x = "hello"
numbersStores integers.i = 100
floatsStores decimals.i = 10.389
listsStores a list of things.j = [1,2,3,4]
dictsStores a key=value mapping of things.e = {'x': 1, 'y': 2}

Python: Some Keywords

KEYWORDDESCRIPTIONEXAMPLE
andLogical and.True and False == False
asPart of the with-as statement.with X as Y: pass
assertAssert (ensure) that something is true.assert False, "Error!"
breakStop this loop right now.while True: break
classDefine a class.class Person(object)
continueDon't process more of the loop, do it again.while True: continue
defDefine a function.def X(): pass
delDelete from dictionary.del X[Y]
elifElse if condition.if: X; elif: Y; else: J
elseElse condition.if: X; elif: Y; else: J
exceptIf an exception happens, do this.except ValueError, e: print e
execRun a string as Python.exec 'print "hello"'
finallyExceptions or not, finally do this no matter what.finally: pass
forLoop over a collection of things.for X in Y: pass
fromImporting specific parts of a module.import X from Y
globalDeclare that you want a global variable.global X
ifIf condition.if: X; elif: Y; else: J
importImport a module into this one to use.import os
inPart of for-loops. Also a test of X in Y.for X in Y: pass also 1 in [1] == True
isLike == to test equality.1 is 1 == True
lambdaCreate a short anonymous function.s = lambda y: y ** y; s(3)
notLogical not.not True == False
orLogical or.True or False == True
passThis block is empty.def empty(): pass
printPrint this string.print 'this string'
raiseRaise an exception when things go wrong.raise ValueError("No")
returnExit the function with a return value.def X(): return Y
tryTry this block, and if exception, go to except.try: pass
whileWhile loop.while X: pass
withWith an expression as a variable do.with X as Y: pass
yieldPause here and return to caller.def X(): yield Y; X().next()

Sunday, May 3, 2015

Sublime-Text installation at Ubuntu

For Sublime-Text-2:
sudo add-apt-repository ppa:webupd8team/sublime-text-2
sudo apt-get update
sudo apt-get install sublime-text
For Sublime-Text-3:
sudo add-apt-repository ppa:webupd8team/sublime-text-3
sudo apt-get update
sudo apt-get install sublime-text-installer

Install Manually via Terminal:

Download from the Sublime Site:
32-bit:
wget http://c758482.r82.cf2.rackcdn.com/Sublime\ Text\ 2.0.2.tar.bz2
tar vxjf Sublime\ Text\ 2.0.2.tar.bz2
64-bit:
wget http://c758482.r82.cf2.rackcdn.com/Sublime\ Text\ 2.0.2\ x64.tar.bz2
tar vxjf Sublime\ Text\ 2.0.2\ x64.tar.bz2
For Both:
sudo mv Sublime\ Text\ 2 /opt/
sudo ln -s /opt/Sublime\ Text\ 2/sublime_text /usr/bin/sublime