Introduction to Git

Git SCM

Source Code Management with Git

Git is a Version Control System (VCS) and sometimes referred to as Source Code Management (SCM). A software that tracks the changes that have been made to files. Over time when different versions of a file exists, Git will:

  • Compare them and display the lines that are different.
  • Keep track of all the changes that have been made from one version to the other.
  • Merge different versions and changes together.

Git is however not just specific to source code, it can be used for managing versions of this files. For example you are working on a project proposal and you have named the files as proposal_v1.docx, proposal_v2.docx etc. Microsoft Word offers a form of version control through its feature Track Changes. Similarly Adobe Photoshop offer a feature called History. Wikis also offers the same thing.

Source Code Management before Git

Before Git, different other tools have been used to provide version control such as:

Source Code Control System (SCCS)
Released in 1972 by AT&T as a closed source product but was free with UNIX. It was used in Universities by students and later found its way in companies. Instead of saving three different versions of the same file it just saved a snapshot of the changes in each version.
Revision Control System (RCS)
Released in 1982 as an open source and cross-platform. Instead of keeping the original file as a whole and then keeping snapshots of all the changes made in every version, it did the opposite. Saved the complete file in the latest version and saved snapshot of changes for the older ones.
Concurrent Versions System (CVS)
Introduced in 1990, it added features such as able to add version control on multiple files. Additionally now multiple users were able to work concurrently.
Apache Subversion (SVN)
CVS was further improved in SVN introduced in 2000. It was faster and allowed saving of non-text files like images also. SVN was not just looking at changes made to certain file but was looking for all the changes happening in a directory. It was the most popular VCS before Git.
BitKeeper SCM
Released in 2000 as a proprietary solution but also had a Community Version which was free but had less features. Distributed Version Control was added to it. This free edition was used by the Linux Community for the Linux Kernel from 2002-2005. In April 2005, the Community Version was no more free and so Linux Foundation thought of coming up with their own solution.
Linus Torvalds

Linus Torvalds

Birth of Git

Git was created by Linus Torvalds in April 2005 as an open source product. Like BitKeeper it is also a Distributed Version Control System and is cross-platform. One other advantage is that it is many times faster than other SCMs. It has better safeguards built into it for data corruptions.

How Git Works

In centralized version control systems a copy of the code is placed centrally and users work on their end, when they commit the changes it will placed in the central repository. It is up to the user to keep track of what is happening in the central repository.

In Distributed Version Control Systems however, like Git, different users (or teams of users) maintain their own repositories, instead of working from a central repository. Git is very flexible and different workflows can be implemented with Git.

The changes are stored as “change sets” or “patches”. In other words, Git tracks changes not versions. These “change sets” or “patches” can be exchanged between repositories, merge with other “change sets” or “apply patches”. There are no single master repository instead there are many working copies each with their own combination of change sets.

Installing MongoDB

In the first post on MongoDB, we had a very brief introduction. MongoDB can be installed on a MAC, Windows and Linux system.

Installation

This tutorial is (for now) focused on Ubuntu. MAC and Windows users can directly download the package from MongoDB Downloads page and follow the instructions.

Installing MondoDB in Ubuntu is as simple as running the following command:

sudo apt-get install mongodb

As mentioned above that Ubuntu already has MongoDB package in its repositories but 10gen packages are more up to date. Following these steps will ensure 10gen’s MongoDB installation:

Public GPG Key of 10gen to ensure authenticity

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10

Add 10gen Repository
Either add the following line to the end of the /etc/apt/sources.list file or create a new file named /etc/apt/sources.list.d/10gen.list with these following contents:

deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen

Update with the new Repository

sudo apt-get update

Finally the actual installation

sudo apt-get install mongodb-10gen

Configure MongoDB

MongoDB on Ubuntu uses /var/lib/mongodb for storing data and /var/log/mongodb/mongodb.log for logging. This can be changed from the configuration file:

sudo vim /etc/mongodb.conf

Additionally the control script of MongoDB is located /etc/init.d/mongodb. The account used to run the MongoDB instance is mongodb user account. The directories mentioned above already have permissions with this account.

Starting & Stopping MongoDB

The service name is mongod for MongoDB>. To start it:

sudo service mongodb start

To stop it:

sudo service mongodb stop

Now that MongoDB is installed and running, we will start with actually using it in the next post.

Introduction to MongoDB

MongoDB Logo

MongoDB Logo

MongoDB belongs to a new brand of databases known as NoSQL Databases or Schema-less Document Oriented Databases. MongoDB was first introduced in 2009 and has been in very active development since then. Lets first see the difference between NoSQL and SQL databases.

How is NoSQL different from SQL Database

Traditional databases a.k.a RDBMS use relations or tables to structure data. Relations in the form of rows and columns. Where row or tuple stores a complete record of an item. A column represents the collection of all same attributes of the records. Then there are Primary Key and Foreign Key relations. Constraints can be added and you have a proper schema running with data types, data length, allow NULL or NOT NULL etc.

NoSQL databases on the other hand simply do not have any schema. Which means no constraint, no limitation on data types, no maximum length issues. The idea of structuring data is very simple, which is key-value storage. These key-value pairs can further be stored in a Document, which is basically a simple collection with different or similar fields. The details will be covered in later posts.

A major attraction is the ability to store data from objects in programming languages directly without a need to map the data members of objects to column names of the table. NoSQL makes storing data for objects in object oriented language very logical and natural unlike RDBMS where a mapping is required. In short Document databases are the right match for Object Oriented Programs with no translation required. Apart from object oriented tasks, another reason is that many times our data is not always can be structured in the form of rows and columns. Previously designers had to make an artificial structure just to store their data in RDBMS.

NoSQL is good for larger public websites where the user base is very large and the rate of data read and write is very high. Such websites need to grow with time that is they have to be very scalable. Replication is built right into them. A reason for this is that RDBMS databases primarily were developed in the era where data was to be stored on large single servers. This has changed a lot now.

Examples of NoSQL are MongoDB, CouchDB, HBase, Cassandra, Hypertable etc.

So Which One Wins?

This understanding is not correct that NoSQL is here to replace SQL. No that is not the case, in fact NoSQL databases’ application area is where SQL was not good at. The inspiration of NoSQL databases comes from Google’s BigTable and Amazon’s S3 which are used by these companies for their own storage purpose.

SQL databases still have their strengths and will remain.

MongoDB as an NoSQL Database

It uses JSON format to store all of the data, in fact it is BSON (binary of JSON). MongoDB is highly scalable and allows easy replication. Every entity can have whichever attributes it needs and skip whichever attributes it doesn’t needs. JavaScript is the interface language for MongoDB.

The next step is to install MongoDB on our system.

Programmically Adding Items to ListView Control in WPF

A ListView control is similar to a ListBox but can contain multiple columns. But to add data we need to bind it to an object which has the same data members as the columns of the ListView control.

This is how I have declared the ListView and its columns

<ListView Name="list">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Serial No." DisplayMemberBinding="{Binding Serial}" />
            <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" />
            <GridViewColumn Header="Age" DisplayMemberBinding="{Binding Age}" />
        </GridView>
    </ListView.View>
</ListView>

Notice the Binding values. Now I will create a class named Item (you can name it anything you like)

class Item
{
    int serial;
    string name;
    int age;

    public Item(int s, string n, int a)
    {
        serial = s;
        name = n;
        age = a;
    }

    public int Serial
    {
        set { serial = value; }
        get { return serial; }
    }

    public string Name
    {
        set { name = value; }
        get { return name; }
    }

    public int Age
    {
        set { age = value; }
        get { return age; }
    }
}

The property names of this class matches the binding values. Finally we can add new items:

list.Items.Add( new Item(1, "Ibrahim", 21) );
list.Items.Add( new Item(2, "Sumayyah", 15) );
list.Items.Add( new Item(3, "Yusuf", 20) );

This should work

PHP Coding Convention / Style

PHP being an open source product, lacks a formal and official coding convention. Different frameworks, CMS etc have come up with their own standards which in many cases are similar but at the same time do differ.

The convention mentioned below is mainly inspired from CodeIgniter documentation but does a very good job:

Class names
Class names should always start with a capital letter and multiple words should be separated by an underscore(_) letter. Example:

class Student_records { }

Method and Variable names
All method and variable names should be lowercase and separated by an underscore.

Private Methods and Variable names
Private members should additionally be prefixed with an underscore.

Constants
Constants should always be fully upper case and word should be separated by an underscore.

TRUE, FALSE and NULL
They should always be fully upper case.

Logical Operators
OR should be used instead of || and && should be preferred instead of AND. For negation there should be a space between ! and the word.

Comparison Operators
Always prefer the === and !== over == and != when comparing for boolean.

Whitespaces
For pure PHP files, there should be no whitespace before or after the php opening tags.

Strings
Always prefer single quotes over double quotes.

SQL Queries
SQL keywords should be capitalized like SELECT etc.

For a complete detailed description: http://codeigniter.com/user_guide/general/styleguide.html

How to use Delay in WPF

The problem at hand is that we want to simulate a Progress Bar as if there is some processing going on. If you simply apply a loop to change the value of the progress bar, the loop will end without the user even noticing any change. For the delay we need to use the DispatcherTimer that is available in the System.Windows.Threading namespace.

using System.Windows.Threading;

First have a look at the XAML code:

<Grid>
    <ProgressBar Minimum="0" Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" Maximum="100" Name="progressBar1" VerticalAlignment="Top" Width="479" />
    <Button Click="button1_Click" Content="Button" Height="23" HorizontalAlignment="Left" Margin="230,105,0,0" Name="button1" VerticalAlignment="Top" Width="75" />
</Grid>

We simply have added a ProgressBar named progressBar1, Minimum as 0 and Maximum as 100. Also we have a Button with a click event named button1_Click.

Now what we need is that when the user clicks on the button, the progress bar starts simulating. So that code will be placed in the button1_Click method.

private void button1_Click(object sender, RoutedEventArgs e)
{
    DispatcherTimer delay;
    delay = new DispatcherTimer(DispatcherPriority.Normal);
    delay.Interval = TimeSpan.FromMilliseconds(30);
    delay.Tick += new EventHandler(delay_Tick);
    delay.Start();
}

We have created an object of the DispatcherTimer named delay and set its Interval to 30 milliseconds. Next we have created handler for the Tick event (this event will be called every 30 milliseconds in our case). Finally we have started the delay timer.

In the Tick event handler we need the following code:

private void delay_Tick(object sender, EventArgs e)
{
    progressBar1.Value += 5;
    if (progressBar1.Value >= 100)
       ((System.Windows.Threading.DispatcherTimer)sender).Stop();
}

For every Tick event, the progressBar1 will progress with a value of 5. To stop this timer after the progressBar1 has reached its destination we need to stop the timer.

Hopefully this was easy. Post your questions if you have any.

Browser Back Button and Data Caching

By default, the web browser will always cache the data and when a user clicks on the browser Back button, the previously cached data will be loaded instead of generating a fresh request. While this is acceptable in most of the cases, some applications may not want this functionality. For example, a one-time form that has already been submitted etc.

All that is required is a set of instructions for the web browser. I have presented here three ways and any approach will be enough.

HTML

<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate, post-check=0, pre-check=0" />
<meta http-equiv="Pragma" content="no-cache" />

PHP

header("Cache-Control: no-cache, no-store, must-revalidate, post-check=0, pre-check=0");
header("Pragma: no-cache");

CodeIgniter

$this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
$this->output->set_header("Pragma: no-cache"); 

This will prevent your browser from caching the page.