Sunday, September 30, 2007

Jog – A piece of heaven

After a very long time all five of our engineering gang was meeting, and its been almost two and a half years since all of us went together for a trip. A much awaited break for all of us from our routine life. All of us had so much to catch up on each others lives. So finally at around 11 on a cold Friday night we started our journey of 400 kilometers from Bangalore to Jog. The ride was filled with pranks, pulling each others legs, remembering the good old days of our college life. By the time we thought of taking a nap we were more than half way through our journey. Our heated arguments, discussions and fun had probably enraged the car I believe it overheated! So we had to break for about an hour before we started moving. As the saying goes “Alls well that ends well”. This one hour break in our journey ensured that we reached Jog at a perfect time in the morning.


Six in the morning still quite a distance from Jog we were surrounded by the enchanting and picturesque beauty of the Western ghats. Frequent spells of fine spray of rain had made the curvy roads look even more beautiful, this made me jealous of the driver. To add to this we had lush greenery on both sides of the road. Me and my friend started clicking pictures of this timeless beauty.




Finally we reached Sagara, a small place 30 kilometers from Jog. This was the last place before most mobiles went silent due lack of network. After a quick break we started the final segment of our journey. The first place we stopped over was a bridge across the river Sharavati just a few minutes from the waterfall. Everyone jumped out of the car to enjoy the pleasant weather and view. Even at eight in the morning fog was taking its time to clear on this little hill station. Due to the dense fog we couldn't see things 100 metres away let alone the waterfalls.



After clicking few photos we headed to the main waterfall. As soon as the car came to a halt we literally ran to get a glimpse of the waterfall. Much to our dismay we could just hear the roaring of the waterfall and nothing much was visible due to heavy fog and thick cloud cover. But we had a pleasant surprise waiting for us, courtesy Mother Nature. Cloud cover just started to clear bit by bit to showcase the true beauty of Jog. All of us were jumping in joy to see “Raja”, first of the four waterfalls. Then “Rani” showed up quickly, followed by “Rocket”. But to see “Roarer” we had a wait a little more. Finally all four waterfalls were visible and we were totally bowled over by Mother Nature. We were jumping in joy, clapping hands. It took us awhile before we could even talk. I just cant express the euphoria in words. Quickly we decided to pack our breakfast head down towards the bottom of the falls and have it there.



A rocky decent of about 1500 steps. All the cementing of the steps were washed away due to recent heavy rains. As we were making our way downhill, the view and roar of the waterfalls was getting better. Half way through we were completely taken by the shear beauty of Jog. Its just then we started to get the feeling that “This is indeed a piece of Paradise of Earth”. No one can express the beauty of this place in words or in pictures, one has to see this place to enjoy its beauty. The rocky path gave us ample opportunity to enjoy Nature's beauty. The Shutter Bug wont allow you to take more than 10 or 15 steps before you feel like clicking another one!



3/4th the way down we were greeted by the fine spray from the waterfalls. With every step it was increasing. Still further down the fine spray was now like heavy drizzle. More closer to the falls we decided that the cameras and mobile are best inside the bag than outside, as no one had the patience to click pictures and the waterfall spray was really heavy. At the very bottom of our decent we were literally running towards the falls. Mother Nature had lot more surprises than we could ever imagine. All of a sudden the water flow increased and it was accompanied by gust of wind making the waterfall spray look like a heavy rain. We couldn't open our eyes even though we a good 100 metres from the falls. Since we were early to start the decent we had very few people near the falls, so we had all of Jog just for ourself!



We were so completely taken in by the beauty we were out of words and we were just screaming at the top of our voice to express our excitement and complete surrender to Mother Nature's beauty. Our trek downhill and the jumping and howling had drained our energy and we started feeling hungry. The gust of water was so heavy we had to find a rock to hide behind it and open our food packets. Everyone were so hungry and wanted to dance in this artificial rain, we ate in no time! And we headed straight to the waterfall. We could mange to go only to about 50 meters from the bottom of the fall as water was gushing with immense force and the water spray was too heavy. All of us just stood with an outstretched hand to enjoy the artificial rain. No one was ready to leave this place just yet. And Nature didn't leave us empty handed. Just as we were drenching ourselves Sun joined us in the party. Sun was hidden behind the hill had slowly started raising above the horizon. Sun cast its spell slowly by adding a tinge of golden yellow to each of the four water paths one by one . We again thanked Mother Nature and started our accent half heartedly. On our way uphill we took little breaks to rest and enjoy this timeless beauty.

Now that we had seen the bottom of the fall, now we were headed towards “Raja Spot” from where we can see the waterfalls from the top. It was already nearing noon when we reached up. We were completely exhausted by the climb so we decided to rest up a bit. After about an hours rest we started our 5 minutes drive to Raja Spot or now popularly called “Mungaru Male Spot”, as this place was used in shooting of a recent hit Kannada movie by the same name. Bottom of the falls gave us pure pleasure due to rain formed by the falls spray and the this gave us adrenaline rush. The view from the top is just breathtaking. You can see both the calm looking water and its power as it plummets to bottom. The depth of the falls is good enough to set your pulse raising, adding to that we have the roaring of the water. One can see the rocks carved out by the fury of gushing water. I couldn't stop myself from imagining how it would have been when the dams were opened due to the recent heavy rains. That would have been the sight of a lifetime, this was no less though! We have two places from were we can see the waterfalls. First one is a man-made structure from where we get to see the falls and its power. Next is from over the rocks just from where water path “Raja” falls. The view is just amazing. And soon one realizes that the later is the best choice to view as its much more closer to the falls.



From here you can experience the pure power of water, the gushing roar speaks for itself. We took turns to see the waterfall. By the time we reached Raja spot we had a lot of people, so we missed the peace and tranquility what we had at the bottom. After the high adrenaline pumping experience we took some time to enjoy the valley created by the river, its greenery on both sides of the valley. We were caught in two minds to leave this place or not. Ruthless Mind said we are running out of time and Heart said stay back, finally we decided to listen to the mind and now I regret that decision after returning from Jog!

Tuesday, January 09, 2007

MySQL Embedded Server

MySQL Embedded Server Application
MySQL is an Open Source Relational Database Management System. Its fast, reliable, secure RDBMS. To add to this it has a unique feature which allows the whole DBMS to be embedded into the application itself, known as the MySQL Embedded Server.

MySQL Embedded Server
Whole MySQL Database server is bundled into a library called MySQL Embedded Server. This means that a single Dynamic linking Library (a .dll on Windows, a .so on Unix) that acts as a server and the client all by itself. Using this library one can run a Full featured Database server within the client application.

Benifits of using Embedded Server
The developer doesnt have to worry about database
  • Installation
  • Availabilty
  • Connectivity
  • Portability
  • Speed
With embedded Server we dont need MySQL server to be installed on the target machine. Next the developer does not have to think about the availabilty and connectivity to the server. The embedded server resides within the application and uses Interprocess Communication (or pipes) instead of using the network (TCP/IP) in a conventional DBMS.It also the makes the application portable. Portable in the sense data can be moved from one machine to another easily. As the server is running within the client application the speed factor is also taken care of.

Working with the Embedded Server
The embedded server resides within the application, so it has to be started and shutdown by the application. Once the server is initialized (i,e started) we can use the regular MySQL API to perform database transactions as in any Database application. To use the MySQL embedded server we have to just link the application with libmysqld library and just add four(two in most cases, unless you are developing a multithreaded application) extra function calls to the existing MySQL client server application. The extra functions to be called being
mysql_server_init() - to start the server
mysql_thread_init() - to be called in each new thread that accesses MySQL
mysql_thread_end() - to be called before exiting
mysql_server_end() - to stop the server

Starting the Embedded Server
The Embedded server is initialzed using the mysql_server_init() function with the following function signature

int mysql_server_init(int argc, char **argv, char **groups)
Where argc and argv are same as the the main() function's argc and argv. The last argument is used to specify the active groups of the options file.

Options file
MySQL server startup can is customized with a list of command line options. If the number of arguments are large then the command line becomes very big. To avoid this problem we can put all the options in a file and instruct MySQL to read this "Options File" while starting up. The same holds good for the embedded server also. In a single options file we can have multiple groups of configuration. So we will have to "activate" a certain group using the third agrument in the mysl_server_init() function. By default MySQL looks at certain places to locate the options file. On Windows it looks at the following locations:

C:\my.cnf Global options
WINDIR\my.ini Global options
INSTALLDIR\my.ini Global options

WINDIR is where Windows is installed. It’s normally C:\WINDOWS or C:\SYSROOT.
INSTALLDIR is the folder where MySQL is installed.

On UNIX it looks at the following locations:

/etc/my.cnf Global options
DATADIR/my.cnf Server specific options
~/.my.cnf User specific options

We can override this default behaviour with the "--defaults-file" option in the command line argument(in the argv argument to the mysql_server_init function). The best and easy way of using this in an embedded server application is to ship a options file with the installation and use the --defaults-file argument to point to this options file. Once the embedded server has been started we can proceed further using the database with the regular MySQL C/C++ API.

Bare minimum Options File
The bare minimum options file has to contain two groups namely "[server]" and "[client]". Then this can be followed by other custom groups. Under server group we must have atleast the following two enteries:
datadir - The location where data has to stored
language - Path to the language files.
The client group has to have the language entry. In the options file if we are going to have just one set of options then we can skip server and client groups.

Sample Options File

[test_libmysqld_SERVER]
language = C:/Path/to/language/files
datadir = C:/path/to/data

[test_libmysqld_CLIENT]
language = C:/Path/to/language/files

Note that the Windows path needs to contain forward slashes (/) and not the backslash (\). To be on the safer side use absolute path. The above example is for Windows, some holds good for UNIX also, just change the path. Copy the language files from the MySQL installation to workspace.

Stopping the server
We have to stop the server when we are done with using the database. This is done using the mysql_server_end() function call. This function has no arguments and shutsdown the embedded server.

Example
Lets take a look at a simple example for using MySQL Embedded Server in C.

#include
#ifdef WIN32
#define WIN32_LEAN_AND_LEAN
#include
#include
#endif
#include

int main()
{
static char *server_options[] = {"mysql_test", "--defaults-file=my.cnf"};
int num_elements = sizeof(server_options) / sizeof(char *);
static char *server_groups[] = {"libmysqd_server", "libmysqd_client"};

MYSQL *mysql;
MYSQL_RES* result;
MYSQL_ROW row;

/*Initialize the server*/
if(!mysql_server_init(num_elements, server_options, server_groups))
{
printf("Error initializing the server\n");
return 1;
}

/*Initialize the MYSQL structure*/
mysql = mysql_init(NULL);

/*
* Set connection type to MYSQL_OPT_USE_EMBEDDED_CONNECTION
* When we do a real connect, then a embedded server connection is used
* instead of a conventional client server connection.
*/
mysql_options(mysql, MYSQL_OPT_USE_EMBEDDED_CONNECTION, NULL);

/*
* Establish a connection to the database. By default user authentication
* is disabled in a embedded server. If user authentication is required then use
* --with-embedded-privilege-control in the server_options.
*/
if(mysql_real_connect(mysql, NULL, NULL, NULL, "test", 0, NULL, 0) == NULL)
{
printf("Unable to connect to the database\n");
return 1;
}

/* Now that we are connect we can use the C API to proceed further */
mysql_query(mysql, "SELECT UserName FROM USERS");
result = mysql_store_result(mysql);
while((row = mysql_fetch_row(result) != NULL)
{
printf("UserName : %s\n", row[0]);
}

/*We are done with the database. Clean up!*/
mysql_free_result(result);

/*Close the connection*/
mysql_close(mysql);

/*Shutdown the server as well*/
mysql_server_end();
return 0;
}

The above code is taken from MySQL tutorial located at http://dev.mysql.com/tech-resources/articles/embedding-mysql-server.html and modified as required. Next step is to compile the program. Following is a makefile that works on UNIX.

##############################
# Makefile to build the MySQL Embedded #
# server example on UNIX #
##############################
CC=gcc
CFLAGS=`mysql_config --include`
LDFLAGS=`mysql_config --libmysqld-libs`

all: embeddedserver

embeddedserver: embeddedserver.o
$(CC) embeddedserver.o -o embeddedserver $(LDFLAGS)
embeddedserver.o: embeddedserver.c
$(CC) embeddedserver.c -o embeddedserver.o $(CFLAGS)

clean:
rm -f embeddedserver.o embeddedserver

##############################
# Makefile to build the MySQL Embedded #
# server example on Windows #
##############################
CFLAGS=/O2 /G5 /GA /I "C:\MySQL\include" /D "WIN32" /D "_WINDOWS" /D "_CONSOLE" /D "_DEBUG" /D "_MBCS" /FD /EHsc /MT /GS /Fo"Release/" /Fd"Release/vc70.pdb" /W3 /nologo /c /TP
LDFLAGS=/OUT:"Release/article.exe" /INCREMENTAL /NOLOGO /LIBPATH:"c:\MySQL\Embedded\DLL\release" /LIBPATH:"D:\MySQL\lib\opt" /NODEFAULTLIB:"libcmtd.lib" /SUBSYSTEM:CONSOLE /MACHINE:X86 libmysqld.lib mysys.lib kernel32.lib user32.lib

all: "Release/embeddedserver.exe"

Release/embeddedserver.exe : Release/embeddedserver.obj
link Release/embeddedserver.obj $(LDFLAGS)

Release/embeddedserver.obj : embeddedserver.c
cl embeddedserver.c -c $(CFLAGS)

clean:
del Release\embeddedserver.*