Sunday, November 25, 2012

What does a website know about you

Every time you visit any webpage you transmit the following information to them

The following information is included in the HTTP request:

  • Your browser version
  • Your Operating system version
  • Which website you came from
  • All the cookies generated by that website

The following information gets transmitted in the TCP/IP packet that gets sent out to the webserver

  • Your WAN IP address
  • The logical port on your machine from which the request was made

Saturday, November 24, 2012

HTTP status codes tutorial

If you delete your internet explorer cache, open the f12 tools  (by pressing F12 key) and then try to visit, this is what you see in the f12 tool

So this is what happens, since you cleared the cache, the browser gets all the resources from server. The 200 (inside the red box) that you see in the result column, means the fetch was successful. This HTTP status code is a part of the response header.

Now if you refresh the page, this is what you see,

Notice that the HTTP status codes for all the image and javascript resources has a status code of 304. What this means is that this resource has not changed since it was last requested. When the browser gets this message, it loads this resource from the browser cache.

A complete list of HTTP status code can be found here

Status codes are classified into the following categories.

What it means
Some success message
Some form of redirection
Client made a bad request
There was some error at the webserver

These are the most common status codes you would come across

Status code
The request has succeeded
Resource not modified since the last fetch.
Not found

Friday, November 23, 2012

Difference between uri and url

URI is meant to identify a resource. A URI can be a URL or a URN. To explain further I will use an example. 

Suppose you want to buy the book "Network Analysis"  by Van Valkenburg off of Amazon. You can identify that book by its



URN:  ISBN-10: 0136110959

URNs are still in the experimental stage. There is no infrastructure yet to convert the names into addresses. Most of the URIs you come across on the internet are URLs.

Sunday, November 11, 2012

RavenDB tutorial (A beginners introduction to RavenDB)

What is NoSQL?

RavenDB is a NoSQL database. Lets take a brief look at NoSQL before proceeding to RavenDB. NoSQL stands for not only SQL. Since the late 80s RDBMS (Oracle SQL etc) have been the technology of choice to save large amounts of data. They offered many advantages such as ability to perform advanced queries, ability to set referential integrity to maintain consistency of data and so on. For a majority of the enterprise needs, RDBMS lends itself very well. However there is one fatal flaw in RDBMS. They do not scale very well. You could employ sharding (Database sharding is a technique of horizontal partitioning data across multiple physical servers to provide application scale-out.) to scale SQL server, but then you lose many of the advantages such as referential integrity, ability to join etc, that RDMS have to offer. Plus there are other technical challenges that is beyond the scope of this article. To overcome the hurdle of scaling RDBMS, NoSQL was invented. Google (with their Big Table) and Amazon (with their Dynamo) are widely believed to have started the trend towards NoSQL.

When to use NoSQL?

If your application demands the following, NoSQL should be considered as an alternative to SQL server or Oracle
  • You application needs to scale across multiple servers.
  • Your data is semi structured.  (eg discussion threads, product catalogs, this blog article !)

However if your application has the following needs, RDBMS would be strongly preferred.

  • You need to run advanced queries against your data.
  • Referential integrity between data is very important.

By far scalability is the most important reason to choose NoSQL.

It is important to note that within a single application you could use both a relational database and NoSQL based on the requirements of the module. This new trend of using multiple persistence technologies within an application is called Polyglot Persistence.

Kinds of NoSQL databases.

NoSQL can be broadly classified into the following three kinds
  • Key-Value store
  • Document database (Lotus notes was the first document database to be sold commercially)
  • Column family database

In this article we will be focusing on one kind of a document database called the RavenDb. Although MongoDB is the most popular document DB out there, we still choose Raven Db because its the best document database for .net developers. Mainly because it supports LINQ. (However there are drivers out there which facilitate some use of Linq in MongoDB. I think in time it might develop into a full fledged LINQ support.)

Advantages of RavenDB
  • It is not limited to querying by key.
  • Supports partial update.
  • Supports Linq
  • Facilitates coarse grained locks

Installing RavenDB Management Studio

  • .net 4.0 framework
  • Silverlight
  • IIS 7.5

Follow these steps to manipulate RavenDB database using a browser.

1>Download RavenDB

2>Unzip the file and copy "Web" folder to C:\inetpub\wwwroot

3>Modify the web.config to update
<add key="Raven/AnonymousAccess" value="Get"/> 
<add key="Raven/AnonymousAccess" value="All"/>
 (Note: This is not recommended for production deployment. Check here for more)

4>In IIS manager, add an application that points to this folder.Set the Alias of the application as RavenStudio.  Make sure the application pool is set to use .net framework 4.0.

Now you can view and edit the database in ie (or any browser) using this url

Play around in this management studio to get a good feel for RavenDB.  Note that the files that you save in RavenDB are NOT visible in the file folder as separate files. They are all created inside a single file called Data. If you delete this file, all the files in the database is lost.

Setting up Visual studio to manipulate RavenDB

Follow these steps to be able use C# code to manipulate RavenDB. Please note that you should have successfully set up RavenDB Studio as shown above before proceeding ahead.

1>Create an MVC 3 (or 4) application using Visual Studio

2>Add references to the following files that you got in the download above. It is located under RavenDB-Build-960\Client.

  • Raven.Abstractions.dll
  • Raven.Client.Lightweight.dll
  • Raven.Client.MvcIntegration
  • Newtonsoft.Json.dll

3>Add this to your web.config

   <add name="Server" connectionString="Url=http://localhost/Raven"/>

4>Qualify the following namespaces

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Raven.Client;
using Raven.Client.Document;

Accessing RavenDB through code

Suppose a class Person as shown below exists

public class Person
    public string Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }

Before you can start using RavenDb, you need to create a store. Note that creating a store is very expensive and only one store must exist per application.

var store = new DocumentStore
   ConnectionStringName = "Server"

To save a new object in RavenDB

var person = new Person { Id="1", Name = "Bill", Age = 50 };

using (IDocumentSession session = store.OpenSession())


To query RavenDB

using (IDocumentSession session = store.OpenSession())
    var results = from p in session.Query<Person>()
    where p.Name == "Bill"
    select p;

To update a Person record in RavenDB where the ID is 1

using (IDocumentSession session = store.OpenSession())
  var existingPerson = session.Load<Person>("1");

  existingPerson.Age = 47;

To delete an existing object in RavenDB

using (IDocumentSession session = store.OpenSession())

    var existingPerson = session.Load<Person>("1");


Tuesday, November 6, 2012

SQL Server cursor example

DECLARE @first_name INT
DECLARE @age        INT

DECLARE test_cursor CURSOR
        SELECT          first_name,age
        FROM            myTable
        WHERE           last_name='Biden'
OPEN test_cursor

FROM  test_cursor
INTO  @first_name,@age

        Select  @first_name,@age
            FETCH NEXT
            FROM  test_cursor
            INTO  @first_name,@age

CLOSE test_cursor
DEALLOCATE test_cursor