Friday, 27 July 2012

DOM, Page layouts

The HTML-DOM tree

HTML DOM:
         The HTML DOM interprets the HTML document as a tree structure. The DOM is like an API which interprets a HTML document in the following format. The DOM has a tree structure which has the document as the parent. 
             
                     The DOM helps in retrieving individual elements in the script language using the structure provided above. In the above mentioned tree the attribute, text, elements and comments are placed in the nodes of the tree. HTML DOM allows scripting languages to get the nodes and to process or apply changes over them.

For example (javascript) : we can use Document.getElementbyID('name'); will get an element from the HTML document which contains the give ID.
                 There also exists relationship between the nodes.
                 The above picture gives a clear picture of the relationships between different nodes in the tree. Each node is associated with some properties and methods. Properties are the values of nodes which can be extracted to use or changed. Methods are the methods which can be done over the nodes.

Document.getElementbyID('something').innerHTML; here getting the element is a method and the innerHTML is a property which is being retrieved.

Browser Specific Elements:
         
       Browser is a rendering machine. It gets the HTML page is interpreted it and displays the contents in the programmer desired format.  The rendering of the web page has the following process.






        The Render tree is built by combining the HTML file along with the CSS file which is included by the HTML page. Each browser has its own method of creating the web page by interpreting the DOM. Not all browsers will interpret the web page in the same manner. The difference may arise in the compatibility of the web browser.
       Most of the HTML4 commands will execute properly in all the standard web browsers. But the recently developed HTML5 is not widely supported and the same page will not be rendered properly in all browsers. Hence it is better to go for HTML4 for the time being till HTML5 is well established so that it could be used in all the web browsers.

Thursday, 26 July 2012

HTML as i know......


         HTML as you all know is a language that lets us design web pages. It contains tags which help in presenting such good visuals on the screen. There are a lot of browsers being used currently. Though all the browsers display the properties in the same way there are some browser specific attributes too.

          There are three parts in a web page. The HTML part, script part, css part. The HTML part contains all the tags which will appear on the screen. The script part helps in the web page being dynamic. This part will be similar to any other programming languages. The most popular one being javascript. We shall see about this in future.
         The css part helps in assigning the formatting to the tags in the HTML. There are class and id assigned to each element/tags in the HTML document. They both serve the same purpose, helping to assign the formatting to particular elements using css.
       The css and scripts can be either included in the same HTML document or written as separate files and include them into the HTML file.
       Id is is used in elements where the formatting will appear for only that element alone. Class is used when there are many elements which will contain the same formatting. For example : The text box whenever they appear on the screen will need to have the same formatting. So they will be having the same class name.
       The elements in HTML are either block elements or inline elements. <dev> is a block level element which acts as a container to contain other block level and inline elements. The formatting style for the block elements will be inherited in all the elements which it contains.
       <span> is an inline element which is used to span the formatting style in the text which it contains.
          To create the layout of the web page we can either go for <div> or <table>. Though it is now established that <div> is the proper way to do create the layout.
         Some characters are reserved in HTML. It is not possible to use the less than (<) or greater than (>) signs in your text, because the browser will mix them with tags. To actually display reserved characters, we must use character entities in the HTML source code. For example we can use @lt to make '<' to print on the screen.
        There are various events which are there to monitor the web page. Sometimes you may have to sign in in a new website. You will be giving a new password. If your password is small or not proper the website will print the problem once you leave the text box. This can be done by combining events and corresponding client side scripts.
        The webpage http://w3schools.com/html/html_quick.asp will contain quick view of syntax of all the available tags and entities available in the HTML.

Wednesday, 25 July 2012

Document based database

 Hi,
     Document base database. From the name you can get that it has something to do with a database and document. Initially i thought it as database with proper documentation. But it turned out to be database which uses documents to store the data. They can be opened and read like any other file.
      Many people who have been using RDBMS like mysql will find it difficult to switch on to this type. Because this is an entirely new concept where you need to unlearn things. We need to think apart from normalization and relations.So lets get started.

Why Document-based:
       Well each new concept has a reason behind it. And the reason for the document-based is to overcome the rigidity of the RDBMS. By rigid we mean the strict rules in terms of schema.DBD also prevent joining tables as the details which need to be retrieved by joining multiple tables can be provided  within the same document.
       There is no schema in DBD. Each document can contain its own set of attributes which can vary from one document to another(even though it is preferred to have similar attributes in the document ).

Comparing RDBMS and DBD using example:

       For example (in MongoDB):
       db.users.({name:'prem', likes['facebook','twitter'] });
       db.users.({name:'praveen',cars['ducati','ferrari']});
      
        The above statements adds two documents to the collection users with names "prem" and "praveen". These both documents have two different attributes i.e, likes and cars. Thus we can add each document different data types in runtime. Sounds good right....

       If we need the same thing to be done using MySQL the schema would be,
       create table users (name varchar(10),likes1 varchar,likes1 varchar,cars1  
       varchar, cars2 varchar);

      Then for prem there would be two NULL columns in cars1 and cars2. The same would be case for praveen in the likes column. If we need to increase the number of cars owned by praveen we need to alter the entire table in RDBMS. But in DBD only the particular document will be affected.

       Consider a query where you need to "find number of people who like facebook". The query can be done in DBD too. But searching each attribute of document to address the query is not a proper way. So you may need to rethink the structure of the documents.

Storage in DBD:
        Well another advantage of DBD is that the documents are in text files. So the processing of these documents are extremely fast compared to the relational database.Here the Documents are analogous to records and the collections are analogous to the tables.

Searching with JASON:
       Each documents are addressed by a key-value pair. For each document a unique id is generated. Thus search for a document is faster. DBD allows API's such as REST using JSON which is pretty much faster than RDBMS( JDBC , ODBC ). Each document is a object of JASON. In the previous example the two user objects 'prem' and 'praveen' are JSON. For good knowledge on JSON refer http://www.json.org/.
        The use of JASON makes the DBD to support javascript very much. They also form effective means of communication between server and client as the message transfered will be in the form of JASON rather than XML documents.
       Primarily the DBD came into use to manage huge amounts of data. The DBD allows writing very fast and hence they are used in write intensive application. Even in reads it is better to use rather than RDBMS.

Transactions:
        But the problem in this is that we cannot go for transactions in DBD. Even though there are facilities to support transactions these days. DBD are not preferred  for transactions. There is no possibility to go for locks in DBD.
        There can be situations when you need to split the collection into two with a common value to link the two collections(foreign key in RDBMS). Even though we can implement the above scenario using DBD it is not advisable.
       
          

Tuesday, 24 July 2012

Unix commands and working

The audio blog for the blog is at http://yourlisten.com/channel/content/16906239/Linux_commands

Before getting into the commands let us see the Linux directory structure.

/ :
 This is the root directory. Every other folders are under the root directory. This usually contains the sub folders so its best no to keep any direct files in it.

< /boot >
       This folder contains the information it needs when it is getting booted.

< /bin, /usr/bin >
        This folder contains important program like ls,grep etc.,

< /usr >
 It is the largest directory in the linux.
 Inside the usr directory we have these following folders.

doc : Documentation for the user apps, in many file formats.
share : Config files and graphics for many user apps.
src: Source code files for the system's software, including the Linux kernel.
include : Header files for the C compiler. The header files define structures and constants that are needed for building most standard programs. A subdirectory under  contains headers for the C++ compiler.

 </var>
          It has the data which will be constantly changing during the system execution. They contain directories like , log - to to contain the log information,
mail - to have the sent and received mails , spool - to contain the jobs which are queued up for process(eg. printing).

</dev>
        It has the various devices attached to the file system.

Now lets brush up a few linux commands which we normally get to use,

  • ls - to list all the files and directories in the current directory.
  • mv- moving file names.
  • rm- remove the file.
  • rmdir - remove directory.
  • rm -r - removes the directory with all the files ans sub directory inside it.
  • cp filename filename2 - creates the same file with the name as specified in the second parameter.
  • scp filename destination@ip_address - this will upload the given file into the destination address.
  • scp destination_name@ip_address:/path/filename - this will get the file from the destination to our machine.
  • ssh ip_address : This will let us to access the system of the given ip_address through the terminal.
  • chown - to change owner of a file
  • chmod - to change the mode of a given file
  • To start any any network application we can use /etc/init.d/application_name start.
  • ping url - used to check whether the given website is working.
  • ps -elf | grep application name -this will give the details of a particular process.
  • tail -n - prints the last n lines of the file
  • head -n - prints the first n lines of the file
  • top - it is simliar to ps but it shows the real time usage while ps is shows average values over a given snapshot.During top if you press shift + p it will give
  • kill - to kill a particular process given the process id. The process id can be found by using grep command along with the application name.
  • cat , less ,more - these are display commands which is used to display the given file in th terminal.
  • df - disk usage of the file system.
  • du - disk usage of each processes. 
       To know about more commands check this given link
       http://www.oreillynet.com/linux/cmd/



       

Monday, 23 July 2012

Digging deep....DB!!!

Hi guys in this blog we shall see about advanced features in Database.

JOINS:
          Joins are queries which are often executed when you need to fetch data from more than one tables. We can also execute queries without joins, but internally cross join will be getting executed. They are some joins which i might list,

This image give better explanation than any written statements. Thanks to Mr.Hariharan for providing the image.

UNION :
         Union clause is used to get the union of two or more queries. The distinct values will be getting listed. The result of a join and a union may look similar. But the list which is getting returned will be taller in UNION.
         select <columns> from  table1 union select <columns> from table2.

Import and Export :

             To export a database into a dump we can use the following command.
      mysqldump -u mysql_username -p name_of_database > databasefile.sql.
           
           This can be retrieved and used to create a new database.For doing it the following command should be executed.
        mysql -u mysql_username -p name_of_database < databasefile.sql
            These dumps are used as a backup along withe the export and import purposes.

Subqueries :
           The subqueries are queries within queries. It is used when you need to get the results of one query and apply another set of query operation over it.
          The syntax for subquery es,
          select <columns> from <tables> where column(s) {=|>|<|<=|>=|<>}{ANY | ALL | SOME} / { EXISTS | IN } {QUERY};

Normalization :
        Normalization is used in DBMS to overcome the redundancy, anomalies and dependency. This is done by splitting a larger table into smaller tables so that insertions, deletions and modifications can be made in a single table and the changes can be made to known to other tables by defining relationship between the tables. There are various normalized forms. They are,

2NF:
        This is done when there exists partial dependency. XY->Z be a relation. In addition if Y->Z is another relation that is existing. So its better if we could create a separate table having relation Y->Z.

3NF:
         This is done when there is a dependency of the give form. X->Y is one relation and Y->Z is another relation. This implies that X->Z. So we should split the table such that the relations X->Y and Y->Z appear in separate tables.

BCNF:
         This is done when the following scenario appears. When XY->K and another relation exists which states that K->Y. In that case we can remove the column Y appearing in the right side of the second relation. and create a separate table for the relation K->Y and another for X->K. The scenarios are (a)there are multiple candidate keys, (b) The keys are composed of multiple attributes, (c) There are common attributes between the keys.
           In this manner we can apply the normalization over the given tables.

SCENARIO :

We can see about the above mentioned NF's here.
Consider the table with columns,

person_name, company_name, designation, salary.

         here the person_name, company_name --> designation, salary. Logically we can also deduce that company_name, designation --> salary because all the persons in the company will have the same salary. So instead of repeating the group(company_name, designation --> salary) in each employee record we can separately have a table with these columns alone.
               Another problem which is avoided here is, if salary for a particular designation in the company is incremented we need not search for each employee with given designation in the given company and update their salary column. This problem is avoided with a single update in our new model.

              This is the use of normalization.

Concept of views, triggers and stored procedures.

 VIEWS:
          They are stored queries which can be accessed by the name. They are virtual tables and they do not exist in the physical form. Views are created to view certain portion if the tables along with aggregation applied over it. Whenever a view is accessed the query will be getting executed and it helps in abstracting the underlying complexity to the user.

  This can be done by the syntax,
           Create view as select <columns> from ... ; 

           Create view  total_customers as select comp_name as name,count(*)  as 
           no_of_customers from company, customer where 
           comp.cust_id=customer.cust_id group_by(comp_name) ;

           Whenever the above query is run the number of customers in each company will be listed. This can be done by simple running the query,
          select * from total_customers;


           Inside the query for the view is executed and the result is passed onto the query using the view.

           The view can be updatable when the view doesn't contain complex values such as 3.56, col1*col2, col1+3. Any updates in the view / table will be reflected back in the corresponding table / view.

TRIGGERS:
         Triggers are stored procedures which will be getting executed in case of an event. The triggers are used usually to provide integrity in a database for example, when a withdrawal from account causes a negative balance then such transactions should be prevented. This can be accomplished using Triggers. MySQL provides three types of triggers. They are Insert, Update and Delete.The syntax is,

CREATE TRIGGER <trigger_name> 
BEFORE/AFTER UPDATE/INSERT/DELETE ON <table_name> 
REFERENCING NEW ROW AS n, OLD ROW AS o FOR EACH ROW IF n.col_name <comparison_operator> o.col_name  
THEN  
END IF;

STORED PROCEDURES:
           Stored Procedures are piece of code to be encapsulated into a single procedure name and the  code will get invoked from another procedure or from a trigger. These are stored in server.
It has the following syntax,
 DELIMITER //
CREATE PROCEDURE Get_All_cars(IN input varchar(10),OUT output int)
BEGIN
SELECT count(*) INTO output FROM products where type=input;
END //
DELIMITER ; 
          The parameters can be of type IN, OUT or INOUT. In the IN type, the given input will be only for processing. In the OUT type a value will be getting stored in the variable and it will be used in the place from where it was called. INOUT is a combination of both the IN and OUT types.
         The procedure can contain loops and case statements.
        The procedure can be called using by the following syntax,
         CALL Get_All_cars('name',@output) 
          select @output ; // this will give the number of cars of given type.


There are both pros and cons. The pros are user abstraction and sharing logic. The cons are repeating logic in the server and increased in the server.