Monday, May 13, 2013

DATE with BASH Shell

Was struggling to get date validated in linux ... writing a script with all that date parsing checks (valid day, month etc.) naah there mus be some easy way, something that is built in. The OS itself manages date and time so it should have the logic built in. Why not leverage it.

Came across this date command in shell, which is designed to handle relative date calculations and return/print them to you in a user defined way.

Everyone is familiar with the $ date command

cmd:   $ date
result:  Tue May 14 07:47:45 IST 2013 


Now getting the previous day
cmd:   $ date --date="yesterday"
result:  Tue May 13 07:50:35 IST 2013 



Now getting the next day
cmd:   $ date --date="next day"
result:  Tue May 15 07:50:55 IST 2013 

Looks  great isn't it and not only this one can use different string formats to get the same output
Example

cmd:   $ date --date="1 days ago"   or
cmd:   $ date --date="1 day ago"  or
cmd:   $ date --date="-1 day"    or
cmd:   $ date --date="yesterday"    
result:  Tue May 13 07:50:35 IST 2013



Example of various string formats is
Past:
cmd:   $ date --date="yesterday"
cmd:   $ date --date="20 minute ago"
cmd:   $ date --date="2 hour ago"
cmd:   $ date --date="last Friday"
cmd:   $ date --date="10 days ago"
cmd:   $ date --date="10 week ago"
cmd:   $ date --date="10 month ago"

Future:
cmd:   $ date --date="tommorrow"
cmd:   $ date --date="20 minute"
cmd:   $ date --date="2 hour"
cmd:   $ date --date="this Friday"
cmd:   $ date --date="10 days "
cmd:   $ date --date="10 week"
cmd:   $ date --date="10 month "


Its amazing... very nicely coded and not only this you can specify your custom date format

cmd:   $ date --date="tommorrow" + "%d/%m/%Y"

Hope you find it interesting and helpful too..

Have a nice time playing with your dates. -S

Sunday, March 3, 2013

UPDATE .. FROM syntax difference in Sql Server and Oracle


There is syntactical difference in few operations in SQL in Oracel and Sql server

For example: UPDATE .. FROM

UPDATE students
     SET grade='A'
FROM students s 
INNER JOIN students_marks sm ON sm.student_id = s.student_id
WHERE sm.total_marks>80;

The above query syntax works in Sql Server but will fail in Oracle.

Below is the Oracle alternative.

UPDATE 
     (SELECT grade 
             FROM students s
             INNER JOIN students_marks sm ON sm.student_id = s.student_id
             WHERE sm.total_marks>80) t 
SET t.grade='A';


Below is the block diagram for the Oracle query



Wednesday, January 30, 2013

Find when a DB Object was created in ORACLE


I came across a requirement where I need to find when a table on the schema was modified. I am posting the solution here so as my other fellow programmers can get the idea.

SELECT * FROM  all_objects
WHERE  owner = 'object_owner'
AND object_name = 'object_name'

The column "CREATED" tells you when the object was created. The column "LAST_DDL_TIME" tells you when the last DDL was performed against the object.

Example:

SELECT created
FROM all_objects
WHERE object_name = 'my_table'
AND owner = 'sysdba'
AND object_type = 'TABLE'

It will tell you when a table was created.

The odds are good, but the goods are odd-S

Saturday, November 10, 2012

Awesome Java NIO

Easy File Reading

public static List getNamesFromFile(String filetoread)
{
 List lines = null;    
 try
 {
  lines = Files.readAllLines(Paths.get(filetoread), Charset.forName("UTF-8"));
 }
 catch(Exception e)
 {
  System.out.println("Error reading list file \n " + e);     
 }

 return lines;
} // end getNames




Easy File Copy

public static void copyFile(File src, File dest) throws IOException
{
 if (!src.exists())
 {
  // either return quietly OR throw an exception
  return;
 }

 if (!dest.exists())
 {
  dest.createNewFile();
 }

 try
 {
  FileChannel source = new FileInputStream(src).getChannel();
  FileChannel destination = new FileOutputStream(dest).getChannel();

  try
  {
   source.transferTo(0, source.size(), destination);
   // destination.transferFrom(source, 0, source.size());
  }
  finally
  {
   if (destination != null)
   {
    destination.close();
   }
  }
 }
 finally
 {
  if (source != null)
  {
   source.close();
  }
 }

} // function ends

 


Neo here is new NIO. -S

Monday, October 29, 2012

CSS ... Selectors to be learned

SelectorExampleExample descriptionSince CSS versionSupported in
.class.introSelects all elements with class="intro"1 IE6+ Firefox Chrome Safari Opera
#id#firstnameSelects the element with id="firstname"1 IE6+ Firefox Chrome Safari Opera
**Selects all elements2 IE6+ Firefox Chrome Safari Opera
elementpSelects all elements1 IE6+ Firefox Chrome Safari Opera
element,elementdiv,pSelects all
elements and all elements
1 IE6+ Firefox Chrome Safari Opera
element elementdiv pSelects all elements inside
elements
1 IE6+ Firefox Chrome Safari Opera
element>elementdiv>pSelects all elements where the parent is a
element
2 IE6+ Firefox Chrome Safari Opera
element+elementdiv+pSelects all elements that are placed immediately after
elements
2 IE6+ Firefox Chrome Safari Opera
[attribute][target]Selects all elements with a target attribute2 IE6+ Firefox Chrome Safari Opera
[attribute=value][target=_blank]Selects all elements with target="_blank"2 IE6+ Firefox Chrome Safari Opera
[attribute~=value][title~=flower]Selects all elements with a title attribute containing the word "flower"2 IE6+ Firefox Chrome Safari Opera
[attribute|=value][lang|=en]Selects all elements with a lang attribute value starting with "en"2 IE6+ Firefox Chrome Safari Opera
:linka:linkSelects all unvisited links1 IE6+ Firefox Chrome Safari Opera
:visiteda:visitedSelects all visited links1 IE6+ Firefox Chrome Safari Opera
:activea:activeSelects the active link1 IE6+ Firefox Chrome Safari Opera
:hovera:hoverSelects links on mouse over1 IE7+ Firefox Chrome Safari Opera
:focusinput:focusSelects the input element which has focus2 IE8+ Firefox Chrome Safari Opera
:first-letterp:first-letterSelects the first letter of every element1 IE8+ Firefox Chrome Safari Opera
:first-linep:first-lineSelects the first line of every element1 IE8+ Firefox Chrome Safari Opera
:first-childp:first-childSelects every element that is the first child of its parent2 IE8+ Firefox Chrome Safari Opera
:beforep:beforeInsert content before the content of every element2 IE7+ Firefox Chrome Safari Opera
:afterp:afterInsert content after every element2 IE7+ Firefox Chrome Safari Opera
:lang(language)p:lang(it)Selects every element with a lang attribute value starting with "it"2 IE8+ Firefox Chrome Safari Opera
element1~element2p~ulSelects every
    element that are preceded by a element
3 IE8+ Firefox Chrome Safari Opera
[attribute^=value]a[src^="https"]Selects every element whose src attribute value begins with "https"3 IE8+ Firefox Chrome Safari Opera
[attribute$=value]a[src$=".pdf"]Selects every element whose src attribute value ends with ".pdf"3 IE8+ Firefox Chrome Safari Opera
[attribute*=value]a[src*="coffee"]Selects every element whose src attribute value contains the substring "coffee"3 IE8+ Firefox Chrome Safari Opera
:first-of-typep:first-of-typeSelects every element that is the first element of its parent3 IE8+ Firefox Chrome Safari Opera
:last-of-typep:last-of-typeSelects every element that is the last element of its parent3 IE8+ Firefox Chrome Safari Opera
:only-of-typep:only-of-typeSelects every element that is the only element of its parent3 IE8+ Firefox Chrome Safari Opera
:only-childp:only-childSelects every element that is the only child of its parent3 IE8+ Firefox Chrome Safari Opera
:nth-child(n)p:nth-child(2)Selects every element that is the second child of its parent3 IE8+ Firefox Chrome Safari Opera
:nth-last-child(n)p:nth-last-child(2)Selects every element that is the second child of its parent, counting from the last child3 IE8+ Firefox Chrome Safari Opera
:nth-of-type(n)p:nth-of-type(2)Selects every element that is the second element of its parent3 IE8+ Firefox Chrome Safari Opera
:nth-last-of-type(n)p:nth-last-of-type(2)Selects every element that is the second element of its parent, counting from the last child3 IE8+ Firefox Chrome Safari Opera
:last-childp:last-childSelects every element that is the last child of its parent3 IE8+ Firefox Chrome Safari Opera
:root:rootSelects the document’s root element3 IE9+ Firefox Chrome Safari Opera
:emptyp:emptySelects every element that has no children (including text nodes)3 IE9+ Firefox3+ Chrome Safari Opera
:target#news:target Selects the current active #news element (clicked on a URL containing that anchor name)3 IE8+ Firefox Chrome Safari Opera
:enabledinput:enabledSelects every enabled input element3 IE9+ Firefox3+ Chrome Safari Opera
:disabledinput:disabledSelects every disabled input element3 IE9+ Firefox3+ Chrome Safari Opera
:checkedinput:checkedSelects every checked input element3 IE9+ Firefox3+ Chrome Safari Opera
:not(selector):not(p)Selects every element that is not a element3 IE9+ Firefox Chrome Safari Opera
::selection::selectionSelects the portion of an element that is selected by a user3 IE6+ Firefox Chrome Safari Opera

Selecting the selectors. -S

Friday, October 7, 2011

Cloud ... clearing the cloudy concept.

Was interacting on a message thread with a friend on a networking portal on Cloud Computing. Since it went deep want to share it over here...

The  question ...
"In virtualization single pc act as multiple And in cloud many act as single
Actually there are so many articles on web and I m confused which one is right, true?"


The Answer...

Virtualization is static ... Cloud is dynamic.
Eg.
I have a site. I need that site to support 10 million users and the best server I am getting from my provider can support 2 million ... so I have to buy 5 such servers from him. Now even if my site is being used by 1 million users I still end up paying up for 5 servers, where as in cloud you pay as you consume ... Now if my provider has a cloud setup of 20 servers (each supporting 2 million (same as above but wrapped by a cloud layer) ) the cloud will dynamically add on more server machine if the users exceed 2 million and remove it when the hit count gets low. So I'll end up paying for server only when I use it.

The next question ...

"I am clear 95% ...But the addition and removal of servers will happen automatically ?
mean am I need to inform my cloud provider first time about my peak value..
Or it will happen in this way that I took one instance and host my site And forgot about visitors The extra server will add automatically when hit count goes up and when hit count goes down the extra server is removed automatically ? As in the above example should I need to buy one instance or 5 instances ?"

The Answer...
Yes its automatic ... The service providers will boast of their cloud network ... its you who need to make a choice. Providers like Amazon EC2, Microsoft Windows Azure, Google Cloud SQL (just for database hosting) will advertise the strength of their cloud (how much data they can handle, what is the computation speed and other benchmarks), so you need to pick one that suit your need. They will not increase or decrease the strength of their cloud on your need (they will always look to add more to their cloud). Its you who has to decide that the services that they list will be sufficient for you?
You just nee to pick the right service provider which has the adequate network (of servers, balances etc ) to support your need, rest is automatically handled.
If you want something private (mean creating your own cloud) you need to have an OS that support it like CloudLinux(already released) or Windows Cloud(about to release in a month) and you can make it as fat as you want.
Hope this help for the rest 5%

 The next question ...
"Okay Bro 99% :) last question :)
lets take your first example I have a java application and my peak load value is 2 million also video chat of three people is there with fms,red5 or wowza. For that scenario which provider is best. Is amazon with ec2 have support like that or any other provider ?"

The Answer...
Here is the final 1% of text which I'll try to clear the remaining 99% of the doubts ...
Firstly what I get from your ''last question" is ...that you have understood cloud but you are not clear what Amazon EC2 is ...
I'll talk about that first ...
Amazon Cloud provides various types of instances
1. Standard
2. Micro
3. High Memory
4. High CPU
5. Cluster
Each of the above type have defined set (capacity, memory, computation etc). You need to pick the one that suits you (your need)
Amazon provides web services to access its features
1. EC2 (Elastic Cloud) - To manage instances
2. S3 (Storage Service) - To manage data
3. RDS (Relational Database Service) - To manage database
4. SQS (Simple Queue Service) - To manage Message Queues

You can choose from pre-configured AIMs Amazon Machine Images or create your own AIM. Its is this AIM that gets loaded when you create an new instance using your EC2 web-service.
Amazon's pre-configured AIMs have the cost of AIM depends the type of software it contains. If you want to have streaming you can choose and AIM which has wowza media pro (if you want to use FMS you need to bear the licensing cost (and that will be per instance) ).

Once everything is chosen (Instance type with AIM that is required to run your site), you are ready for launch.
Amazon charges you for Instance usage per hour.
So now lets go back to you problem : Suppose you chose High CPU with AIM having wowza and Amazon rate for the same are $0.50 per hour. Now you calculated that the instance you chose would comfortably support half a million users.
You launched your site with one instance up after after an hour the user count crossed half million mark , using EC2 service you start another instance, after two hours it started crossing the million again via your web-service you start another instance. After five hours the hit cam down to half a million again so using you web-service you'll turn off two instances.

Here is how you are charged.
Total hours 7
1st hour - $0.50
2nd hour - $0.50+$0.50
3rd hour - $0.50+$0.50+$0.50
4th hour - $0.50+$0.50+$0.50
5th hour - $0.50+$0.50+$0.50
6th hour - $0.50
7th hour - $0.50
You are charged in instance basis.You may activate 100 instances if you feel the need.

The above example is to make you understand. There are other charges involved too like data transfer charges, region based server charges etc.

Hope this helps... 


Clearing the Cloudy Cloud. -S 

Thursday, July 14, 2011

Immutation in JAVA Explained

Watched X-Men, mutants were far more stronger than a normal  human being. I recalled my biology class where we studied genetics and chromosomal mutations. Mutations were triggered to make species better. I left biology and am now into software. Here also we do the same thing "Mutate". We override objects of the existing classes in the SDK/API/Framework to make them better or fit to our need. But you cannot override each and every object of the SDK/API,  they are protected from getting or being mutated for a reason. Making decisions on creating immutable class needs thorough analysis. How do I create a class which cannot be mutated?

In JAVA String class is a good example, its immutable.

Below are the rules that must be followed in order to create an immutable object.

1. No "setter" methods — methods that modify class members or objects referred to by class variables.
2. Make all class variables final and private.
3. Do not allow subclasses to override methods. The simplest way to do this is to declare the class as final. A more sophisticated approach is to make the constructor private and construct instances in factory methods.
4. If the instance fields include references to mutable objects, don't allow those objects to be changed:
       * Do not provide methods that modify the mutable objects.
       * Do not share references to the mutable objects.
5. Never store references to external, mutable objects passed to the constructor; if necessary, create copies, and store references to the copies.
6. Similarly, create copies of your internal mutable objects when necessary to avoid returning the originals in your methods.


Immutable laws of the universe can teach more exalted lessons than the holy books.
-S