Monday, February 11, 2008

Google AJAX Search API

Hi,

While browsing through, I just stumblled upon what is known as Google AJAX Search API.

I was in seach of something where I could encorporate a google search right into my blog and which looks a touch different than the normal search page from google.

And I did well to land upon the Google AJAX Search API

It was quite simple and had enough tutorials and sample codes written for someone looking to encorporate a decent search into their site.

Take a look at the search box at the right bar on the blog. And try searching for something through it. It displays the results for each category as a seperate tab on the very same page.

Here is the way to encorporate the same in your blog.

1. Obtain a Google API Key. This is a kinda composite key for all the services of google that we would be using.

2. Integrate the API key code into your blog. OPen your blog and go to Edit HTML and in the head section of your blog, add the following script tag:
<script src='http://www.google.com/jsapi?key=developer_key'
type='text/javascript'/>

3. This will allow us to use all the services that google offers. Now we need to put up the code for displaying the search box. Here is the code for that.

<script type="'text/javascript'">
google.load("search", "1");

// Call this function when the page has been loaded

function initialize()
{

var searchControl = new google.search.SearchControl();

searchControl.addSearcher(new GwebSearch());

searchControl.addSearcher(new GvideoSearch());

searchControl.addSearcher(new GblogSearch());

searchControl.addSearcher(new GnewsSearch());

searchControl.addSearcher(new GimageSearch());

searchControl.addSearcher(new GbookSearch());

// create a drawOptions object

var drawOptions = new GdrawOptions();
// tell the searcher to draw itself in tabbed mode

drawOptions.setDrawMode(GSearchControl.DRAW_MODE_TABBED);
drawOptions.setSearchFormRoot(document.getElementById("searchPlaceHolder"));
searchControl.draw(document.getElementById("searchcontrol"), drawOptions);

}

google.setOnLoadCallback(initialize);
</script>




4. An explaination of this code is as below:
a. google.load("search", "1"); - This line of code prepares the search API from google and loads all the functions necessary to perfrom the search.

b. google.search.SearchControl();- This is the main control that displays the search results.

c. searchControl.addSearcher(new GwebSearch());- This is used to add various searchers to the control. Add as many searches as you like.

d. GdrawOptions();- A draawoptions object gives you the flexibility of customizing your search results display. The below two lines show the examples of its usage.

e. drawOptions.setDrawMode(GSearchControl.DRAW_MODE_TABBED); - This tells the search control how to display the search results. Here we have set it to display in the form of different tabs.

f. drawOptions.setSearchFormRoot(document.getElementById("searchPlaceHolder"));- This tells the search option the control to which the search box would be bind. I created a div with the ID searchPlaceHolder to hold my search box.

g. searchControl.draw(document.getElementById("searchcontrol"), drawOptions); - This is used to actually draw the search control with the appropriate draw options.

h.
Loading
- This provides us with an instance of a searchcontrol. Place it where you want the results to be displayed.


This way it would display a search box where you place the searchPlaceHolder control and would display the search results where you place the searchcontrol.


Hope this helps a lot of people like me ....

-- Ashutosh

Tuesday, January 8, 2008

Harbhajan Banned for 3 test matches.......

Yes this indeed is a technical blog but being a sentimental Indian, I could not stop myself from writing my own views of the story.

First of all, comments from a few commendable people:

  • "I am South African, and I understand the word racism. - Mike Proctor"
  • "They're entitled to do whatever they think is appropriate at the time but for me that would be a little bit extreme, I must admit. - Ricky Ponting"
  • "What to do? Calls for sackings are knee-jerk, the threat to abandon the tour nonsensical. Apart from anything else, the all-powerful television moguls here and in India would not countenance it. The tour will go on, and so will the captains. So law and order it must be. - Greg Baum (The AGE)"
  • "I saw the footage of what had happened involving Andrew Symonds when the
    Australians were in India. Most of the spectators were just having some light-hearted banter, and there was no malice in most cases. - Steve Waugh"
  • "Michael Clarke also had a dreadful match but he is a young man and has time to rethink his outlook. That his mind was in disarray could be told from his batting. In the first innings he offered no shot to a straight ball and in the second he remained at the crease after giving an easy catch to slip. On this evidence Clarke cannot be promoted to the vice-captaincy of his country. - Peter Roebuck"
  • There are times when you don't know. So, you ask the question. Every player has right to ask the umpire. I will say I don't appeal if I don't think they are out. If I am not sure, I will ask the umpire and I'll accept his decision. - Adam Gilchrist"

SO these are the view of certain people who are known to be experts of their own fields.
Well Mr. Mike Proctor just to let you know, We are the citizens of India and We really do not know what RACISM is. We never hear such words in our country because we never had views and culture as in SA our even Australia where people are discriminated on the basis of color.


And the statement from a reputed person from Australia "WHY SYMONDS ONLY !!" confirms the stand that they still have racism running through their blood. I mean had the same statement been made against Ponting or any white cricketer that would have not been a Racial comment but since it has been againsta Black, it is definitely the ONE..........

Regarding cancellation of the tour - As an Indian, yes I would most definitely like it to happen, but as an ardent cricket fan, I would most definitely like the tour to go on. Because the fight from here on would definitely be an interesting one becuase now it would most certainly not be a mere game of cricket but it would be a question of the national pride at stake.

As Greg Baum quoted - The television media that is presenting the live coverage all over the world is strong enough to make the tour continue. There would be a huge loss to the industry if it does not. After all we are talking of around 3 months of cricket going out of question.

And lastly, to comment on what Adam Gilchrist had to say on Rahul Dravid's dismissal, well Gilli your statement suggests that you were not confirm if that was out or not so you had put up a question to bucknor asking for his openion.

Well I would leave that to the readers to decide upon after having a look at the footage of the dismissal.


--Ashutosh

Tuesday, December 11, 2007

Double v/s Decimal

Just recently, one of my Colleagues approached me with a very strange problem. He had made a function for Rounding the numbers to the specified number of decimal places.

He was facing a problem where his function would Round off 544.435 to 544.43 but would Round off 544.445 to 544.45 which was the intended one.

After a fair bit of research I could conclude the following:

The error was because he was taking input and providing the output as DOUBLE.

Now MSDN says that DOUBLE is a FLOATING POINT VARIABLE Type. It takes up less space in the memory but is prone to some Rounding problems.

So while storing the above two DOUBLES, .NET stored them as follows:

544.435 ------------- 544.43499999999994543031789362430572509765625
544.445 ------------- 544.4450000000000500222085975110530853271484375

So doing a Multiplication with the precision factor (100 in this case) and taking a Math.Floor of that number converts

544.435 to 544.43
&
544.445 to 544.45

More explanation about the Binary Floating point could be found at

http://www.yoda.arachsys.com/csharp/floatingpoint.html

BTW, the solution to the above mentioned problem is using something that is Fixed Point Variable and not a Floating Point Variable like Double.

So using DECIMAL instead of DOUBLE in the application resolves the Rounding Issue.


-- Ashutosh

Thursday, November 22, 2007

Garbage Collection

Thanks to marble_eater for the article which is now in the form of a slideshow.......

If you wish to download the presentation, it can be downloaded from
here

Wednesday, October 17, 2007

Why is a Short of 4 bytes

Why is a Short of 4 bytes

This is where it all started. I faced an interesting problem while working on my Sessions application. Declaring a structure as :
Struct ShortInt
{
Short firstShort;
Int32 secondInt;
}

If someone is asked about the size of this structure. I bet most people would answer it as I did : 6 bytes.

But surprisingly, the answer is 8 bytes.

Simple reason for that being the memory is allocated in the chunks and if the declaration of structure is done wisely, it could save a lot of memory:

Taking example:

Struct exampleStruct
{
Byte b1;
Int32 i;
short s;
Byte b2;
}

Here if we look at this structure, the memory for this block is allocated as follows:
Byte b1 ----- A chunk of 4 bytes is allocated
Int32 i ------ This chunk has only 3 bytes left so a new chunk of 4 bytes alloted
short s ------ Previous chunk is full. So next 4 byte chunk is alloted.
Byte b2 ----- Previous chunk has 2 bytes left and can accomodate a byte. Hence the byte variable is accomodated in teh same chunk.

So total size becomes 12 bytes.

Now if we redefine our structure as follows:

Struct
exampleStruct
{
Byte b1;
Byte b2;

short s;
Int32 i;
}

Now the memory allocation would be as follows:

Byte b1 ----- A chunk of 4 bytes is alloted
Byte b2 ----- A byte in teh same chunk
short s ------ Previous chunk has 2 bytes left in it
Int32 i ----- Since the previous chunk is full, a new chunk is alloted

Hense the total size is 8 bytes only.

More could be found on the following links :

http://www.vsj.co.uk/articles/display.asp?id=501
http://msdn.microsoft.com/msdnmag/issues/05/01/MemoryOptimization/default.aspx



--Ashutosh

Wednesday, August 22, 2007

SecureString in c#

System.Security.SecureString



Per MSDN:

Represents text that should be kept confidential. The text is encrypted for privacy when being used, and deleted from computer memory when no longer needed. This class cannot be inherited.



Storing any sensitive data like passwords etc in the standard System.String can be a potential threat to the data for the following reasons:


>> It is stored on the Managed Heap and is not pinned in the memory, so the garbage collector can move it around at will leaving several copies in memory. The code will not know that this has happened, and even if it could figure out that the string was moved, there is no way to clear out the other copies. Instead we have to wait for the CLR to allocate another object where the sensitive data is so that the memory gets erased.

>> It's not encrypted, so anyone who can read process's memory will be able to see the value of the string easily. Also, if the process gets swapped out to disk, the unencrypted contents of the string will be written to the swap file.

>> It's not mutable, so whenever it is modified, there will be the old version and the new version both in memory

>> Since it's not mutable, there's no effective way to clear it out when you're done using it

Hence, .NET 2.0 introduced a new class under System.Security namespace called SecureString, that can be used in place of standard Strings to store sensitive values.

Using SecureString eliminates the above mentioned issues as:

>> The SecureString is not stored in the managed heap while standard strings are and therefore it will not be replicated to multiple locations in memory.

>> SecureStrings are stored in an encrypted form. They need to be decrypted when they are used. this period of decryption can be kept as small as possible. So even if the process is swapped out to disk while the string is encrypted, the plaintext will not end up in the swap file.

>> The keys used to encrypt the string are tied to the user, logon session, and process. This means that any minidumps taken of the process will contain secure strings which are not decryptable.

>> SecureStrings are securely zeroed out when they're disposed of. System.Strings are immutable and cannot be cleared when you've finished with the sensitive data

create a SecureString, you append one character at a time:

System.Security.SecureString secString = new System.Security.SecureString();
secString.AppendChar(p);
secString.AppendChar('a');
secString.AppendChar('s');
secString.AppendChar('s');
secString.AppendChar('w');
secString.AppendChar('d');

When the string contains the data you want, you can make it immutable and uncopyable by calling the MakeReadOnly method:

secString.MakeReadOnly();

To read the secure value, use the SecureStringToBSTR() method as follows:

IntPtr ptr = System.Runtime.InteropServices.Marshal.SecureStringToBSTR(secString);
string sDecrypString = System.Runtime.InteropServices.Marshal.PtrToStringUni(ptr);

The garbage collector will remove SecureStrings when they're no longer referenced, but you
can dispose of a SecureString by using the Dispose() method:

secString.Dispose();

Thursday, August 16, 2007

An ATOM feed ticker (scrolling one)

Just during my leisure time, while I had a small break from work, I gave a look to my blog that looked quite ugly and out came a thought to beautify it and in the process learn new things.

The best part of it was creating an ATOM feed reader for my blog. I finally succeeded in creating one using the idea from
Dynamic Drive.

Here I could create a ATOM Feed scroller which would show all the posts on the blog and also give a pause at each and every post with a link to the original post on my blog.

A sample could be seen on
THIS SITE where I have hosted it (This is a trial and hense would only be available to me till September 12 2007) as well as on the top of this blog.

By that time I would be looking to modify it so that it just required the client side code and no server side coding is involved.

Currently it uses an aspx page to display the posts as there is a bit of server side code involved in it. I would try to eliminate that ASAP.

Once done, I would make this a portable widget that could be used to display any ATOM feed providing its URL.

--Ashutosh

Friday, August 10, 2007

SelectSingleNode not selecting the node.

Recently I was working on creating an ATOM feed reader. Obtained the JS from DynamicDrive and coded the control to take up the URL and return back the posts from it.

It required XML reading and playing around with the nodes. Strange enough, looked easier to work, I had a hard time getting the node required to display the things out.
Below is the format that an ATOM xml uses:


<?xml version='1.0'
encoding='UTF-8'?>

<?xml-stylesheet
href="http://www.blogger.com/styles/atom.css"
type="text/css"?>

<feed xmlns='http://www.w3.org/2005/Atom'
xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/'>

<id>tag:blogger.com,1999:blog-36333526</id>
<updated>2007-08-08T16:18:29.608+05:30</updated>
<title type='text'>Ashutosh Vyas's
Blog</title>

...
...
...
...
<author>
<name>
Ashu
</name>
</author>
<generator version='7.00'
uri='http://www.blogger.com'>
Blogger</generator>
<openSearch:totalResults>22</openSearch:totalResults>
<openSearch:startIndex>1</openSearch:startIndex>
<openSearch:itemsPerPage>25</openSearch:itemsPerPage>
<entry>
<id>tag:blogger.com,1999:blog-36333526.post-3860689405428340431</id>
<published>2007-08-02T14:47:00.000+05:30</published>
<updated>2007-08-02T14:54:53.163+05:30</updated>
<title type='text'>Asynchronous
Page Concept in ASP.NET</title>

<content type='html'>
<link rel='replies'
type='application/atom+xml'>

<link rel='self'
type='application/atom+xml'

...
...
...
<author>
<name>
Ashu
</name>
</author>
</entry>


Now all I needed was to find out the root node and traverse to the Node "feed/title" to find out the title of the blog to display on the top of the scroller.

To my knowledge, it was as easy as
rssDoc.SelectSingleNode("feed/title").InnerText;
But that did not happen to be the case. It always returned me null.
I tried grabbing out the root node (feed) using
rssDoc.SelectSingleNode("feed/title").InnerText;
but this would again return me the same NULL.
Strange for me, doing a rssDoc.DocumentElement() would most certainly return me the required feed node.
After a bit of help from MSDN and other group, I discovered what I did not knew till now and I suspect many ppl do not because of lack of use.
You require a NAMESPACEMANAGER to get those nodes out.
So to dig out something from

<feed>
<title>
</feed>
</feed>

we need the following code.

XmlNode feedNode = rssDoc.DocumentElement;

XmlNamespaceManager nsMgr = new XmlNamespaceManager(rssDoc.NameTable);

nsMgr.AddNamespace("prefix", http://www.w3.org/2005/Atom);

String feedTitle = feedNode.SelectSingleNode("prefix:title",nsMgr).InnerText;


-- Ashutosh

Thursday, August 2, 2007

Asynchronous Page Concept in ASP.NET

Server Unavailable.

This is the error most of us have faced without a clue asto what leads to this error and server being unavailable.Heres the reason:

ASP.NET uses threads from a common language runtime (CLR) thread pool to process requests. As long as there are threads available in the thread pool, ASP.NET has no trouble dispatching incoming requests. But once the thread pool becomes saturated, i.e. all the threads inside it are busy processing requests and no free threads remain, new requests have to wait for threads to become free. If the logjam becomes severe enough and the queue fills to capacity, ASP.NET throws this error stating that Server is Unavailable.

SO whats the solution: Well the easiest way is to increase the maximum size of the thread pool, allowing more threads to be created. That's the course developers often take when repeated "Server unavailable" errors are reported. Another common course of action is adding more servers to the Web farm. But increasing the thread count-or the server count-doesn't solve the issue. It just provides temporary relief to the problem.

One solution to this implemented in ASP.NET 2.0 is the use of ASYNCHRONOUS PAGES.

When ASP.NET receives a request for a page, it grabs a thread from a thread pool and assigns that request to the thread. A normal, or synchronous, page holds onto the thread for the duration of the request, preventing the thread from being used to process other requests. If a synchronous request becomes I/O bound—for example, if it calls out to a remote Web service or queries a remote database and waits for the call to come back—then the thread assigned to the request is stuck doing nothing until the call returns. That impedes scalability because the thread pool has a finite number of threads available. If all request-processing threads are blocked waiting for I/O operations to complete, additional requests get queued up waiting for threads to be free. At best, throughput decreases because requests wait longer to be processed. At worst, the queue fills up and ASP.NET fails subsequent requests with 503 "Server Unavailable" errors.

Asynchronous pages offer a neat solution to the problems caused by I/O-bound requests. Page processing begins on a thread-pool thread, but that thread is returned to the thread pool once an asynchronous I/O operation begins in response to a signal from ASP.NET. When the operation completes, ASP.NET grabs another thread from the thread pool and finishes processing the request. Scalability increases because thread-pool threads are used more efficiently. Threads that would otherwise be stuck waiting for I/O to complete can now be used to service other requests. The direct beneficiaries are requests that don't perform lengthy I/O operations and can therefore get in and out of the pipeline quickly. Long waits to get into the pipeline have a disproportionately negative impact on the performance of such requests

The concept of Asynchronous Pages is available only in ASP.NET 2.0 but it could be implemented in ASP.NET 1.x in a way outlined in the below mentioned link.
http://msdn.microsoft.com/msdnmag/issues/03/06/Threading/

The trick here is to implement IHttpAsyncHandler in a page's codebehind class, prompting ASP.NET to process requests not by calling the page's IHttpHandler.ProcessRequest method, but by calling IHttpAsyncHandler.BeginProcessRequest instead.

ASP.NET 2.0 vastly simplifies the way you build asynchronous pages. You begin by including an Async="true" attribute in the page's @ Page directive, like so:

<%@ Page Async="true" ... %>

This property set to true, says the page to implement the IHttpAsyncHandler. Regarding this, you need to register the Begin method and End method of to the Page.AddOnPreRenderCompleteAsync.

// Register async methods
AddOnPreRenderCompleteAsync
(
new BeginEventHandler(BeginAsyncOperation),
new EndEventHandler(EndAsyncOperation)
);

By these actions, the starts its normal life cycle, until the end of the OnPreRender event invocation. At this point the ASP.NET calls the Begin method that we registered earlier and the operation begins (calling the database etc...), meanwhile, the thread that has been assigned to the request goeas back to the thread pool. At the end of the Begin method, an IAsyncResult is being sent automatically to the ASP.NET and let it determine in the operation had completed, a new thread is being called from the thread pool and there is call to the End method (that we registered earlier, remmember?).

Jeff Prosise explains it all in

http://msdn.microsoft.com/msdnmag/issues/05/10/WickedCode/



-- Ashutosh

Friday, July 6, 2007

FOR v/s FOREACH: Different Perspectives.

Over the years I have been coding just to get the work done. Now since last few months, I realized the importance of every single step taken to improve the performance and writing the code that is optimized.

But during this process of optimization, at times I felt if Approach 1 was more optimized or Approach 2.

One such condition was when I used For loops to iterate through the items of collection.

Now heres the theory:

FOR LOOP:

int[] indexArray = new int[5];
int total = 0;
for(int i = 0; i < indexArray.Length; i++)
{
total += indexArray[i];
}


FOREACH LOOP:

int[] indexArray = new int[5];
int total = 0;
foreach(int i in indexArray)
{
total += i;
}

The advantage of a foreach loop over a for loop is that it is not al all necessary to know the number of items within the collection when an iteration starts. This avoids iterating off the end of the collection using an index that is not available. A foreach loop also allows code to iterate over a collection without first loading the collection in entirety into memory.

So herein we can safely assume that using foreach is an optimized approach rather than using a for loop.

NOW LETS MOVE TO THE OTHER SIDE OF IT:

If we closely look at the IL Code for the above two constructs:

FOR LOOP:

Instruction
cmp dword ptr [eax+4],0
jle 0000000F
mov ecx,dword ptr [eax+edx*4+8]
inc edx
++icmp esi,dword ptr [eax+4]
jl FFFFFFF8


Here, the comparision is done at two stages:
1. For the first run it is done only once to check if the counter is good to continue into the loop.
2. Inside the loop where it is exactly comparing and recalling the code.
This is very well optimized in the loop.

FOREACH LOOP:

Instruction

cmp esi,dword ptr [ebx+4]
jl FFFFFFE3
cmp esi,dword ptr [ebx+4]
jb 00000009
mov eax,dword ptr [ebx+esi*4+8]
mov dword ptr [ebp-0Ch],eax
mov eax,dword ptr [ebp-0Ch]
add dword ptr [ebp-8],eax
inc esi
cmp esi,dword ptr [ebx+4]
jl FFFFFFE3


Clearly the two syntaxes are different. There are some unwanted comparisions and some moves that are exactly not required. Thats because foreach treats everything as a collection and hence uses the code for the same which reduces the performance if it is not a collection and is a simple array only.

So still I am at indecision whether For is an optimized version or ForEach is??.............. :(

--Ashutosh