Multi-Threading in JavaScript (HTML 5)

Published on July 7, 2010 by Amir Sedighi

Introduction
Multi-threading is a basic technique in programming languages. Many applications and software services work using threads. Actually in OS level whatever runs are threads. Every programming language has its syntax and commands to use threading.
An interactive reach user interface should use multithreading. However JavaScript didn’t support multi-threading before HTML5. Now developers are able to use multi-threading features that support by new version of JavaScript within HTML5.

Web Worker
HTML 5 solution for implementing multi-threading is Web Worker. Web Workers let you run multiple processes in the background concurently. All workers run at the same time with together depends on the client computer speed. Using this threads you can make a network request, calculation or persisting data using local storage or fetchnig data from a database server.

Check if your browser supports Web Workers or not. Below function is undefined if it doesn’t.

function supports_web_workers() {
  return !!window.Worker;
}

A Sample
Here you can find some samples of HTML 5. But I like to review another example together. Check this out.
I just want to highlight below parts. You can find the complete code here.
Here we have two separated thread. Ticker and Searcher:


   // TICKER
   var symbol = 'GOOG'; // default symbol to watch
   var ticker = new Worker('ticker.js');

   // SEARCHER
   var searcher = new Worker('searcher.js');
   function search(query) {
     searcher.postMessage(query);
   }

These thread work together at the same time. For JavaScript developers implementing of this kind of processes was just as a so far dream even in few years ago.

Posted in JavaScript

Looking at HTML5 LocalStorage

Published on July 5, 2010 by Amir Sedighi

Introduction
Ajax brings some kind of flexibility for web developers. Using Ajax developers are able to develop lazy routines much easier. Ajax libraries and API let the front-end events to fire their own server-side handlers on demand. This is a great facility because whatever you make a page lighter then you have a faster loading. In the opposite side your application will be lazy in sending responses to the user actions. Here is a trade-of and HTML5 has a proper solution for decreasing response time.

Local Storage
LocalStorage is part of HTML5 standard specification that becomes very useful for caching server-side information into the client machine. HTML5 storage stays on your computer and works even when you reopen the browser and go to the site that you stored its information before.
Actually it is a memory space provides by the browser that websites can use to persistent data that doesn’t need to be sent to the server at the moment. It also can be used for pre-buffering.
LocalStorages remembers all fields that you fetched from the server side database. LocalStorage also increase the amount of downloads of a partial network. Lack of some kind of wide caching system within a business application makes interactive applications slow.
There are plenty of requirements that LocalStorage will get them the proper answer such as buffering and off-line facility.
LocalStorage is a key/value storage. Moreover web browser and API vendors will have their specific implementations that provides such as api for accessing stored data by a semi SQL.

To finding out if your browse support HTML 5 run below just function:

function isMyBrowserSupportLocalStorage() {
  return  !(window['localStorage'] && ('localStorage' in window) == null);
}

Below javascript lines shows that how localStorage helps the developer for maintaining variables:

globalStorage[''].globalVariable= 'This means globalVariable to be used by anyone';
globalStorage['org'].foo1 = 'Accessible for *.org websites';
globalStorage['blog.datispars.com'].companyVar= 'This is a value that can be accessed by the company website';

Same-Origin Restriction
A LocalStorage is not pretty secure as server-side remote databases. Every application or user that who has access to the physical storage probably will access to your LoclalStorage. Moreover any web site can read and modify its own values within a database. Websites can not access other websites databases. This has been called a same-origin restriction.

Conclusion
JavaScript developers used to cache the application informations into memory using structure that they built by coding into the memory such as array, lists and formatted strings. Some 3rd party tools and API have been came to make client side storing easier and more effective such as SQLLite. Now in a big improvement HTML5 is the first version HTML standard that supports local storages within contract. After this developers will be able to provide faster and more reach applications.

Posted in JavaScript

Fetching lines partially using command line

Published on July 5, 2010 by Saeid Zebardast

To see some part of a text file, use the following methods:

1- To get top 20 lines of a file header, use:

$ head -n20 FILE

2- To get 30 line of bottom of a file, use:

$ tail -n30 FILE

3- If you want to print some lines that they are not in the first part or in the last part of a file, you can use:

$ more +num10 FILE | head -n20

The previous command print from line 10 to 30 (20 lines) of FILE.

Tags: ,
Posted in Linux

Software Market In Iran – Part 2

Published on July 3, 2010 by Amir Sedighi

Previous Part.

Sanctions
Economic sanctions has prevented large scale companies and products to be present in Iran’s software market. But smaller companies pass it like mice that pass a wall by its cavities. In other hand Software is not such an industry that works using huge and complex manufactures. Software development just needs human and knowledge and not more. Moreover most of scientific fields have their own opensource products that consists a lot of implemented knowledge within procedures and behaviors. Indeed, economic sanctions just specialized software industry of Iranian engineers rather restrict them at all. Internet basically designed to makes available knowledge and break censorship. It seems Software is not a kind of restrict-able knowledges. Iranian software development teams favor Microsoft .Net and Php for general purposes applications.

Operating System
MS-Windows has captured Iranian operating system market for many years. Microsoft Windows keeps its share market but something starts to change. These are some years that Linux flavors are getting popular between Iranian software developers. So here are many local products on the shelf that uses Linux flavors as platform. Even Iranian universities developed an Iranian version of Linux. New edition of rich client Linux such as Ubuntu are getting their community between Iranian software professionals. Moreover, Linux already has captured most of networking servers market share. All ISP’s prefer to use Linux or Free BSD server rather than Windows. Windows has home using market yet, but I think it is changing. Unauthorized Apple resellers sell latest Apple’s products more than ever. iPhone and MacBook Pro are the most popular electronic products between Iranian youth.
In Minicomputer and Mainframe industries Sun and IBM products are more popular for Iranians. IBM is trust-able and Iranian companies accept to pay twice or more times of price of a IBM Z series mainframe for high transactional businesses. There are some difficulties for spare parts and technical references. But noting is impossible here.

Software Development
We reviewed in past part that Copyright doesn’t support for software products in Iran. So Iranian students almost never pay for any software during getting familiar with software platforms. Cracked software are available always. You don’t have to pay for them. Even you like to pay, There are no authorized reseller. Even if you find a reseller he isn’t able to sell you because of economic sanctions. So you have to download and use softwares without any responsibility. In this case Microsoft, JBoss, IBM, Oracle products are more favorite. For example Oracle databases are using widely by large scale organizations. In many cases they prefer to buy from an authorized branch. The only Iranian branch of Oracle is not support by Oracle any more. So the Iranian Oracle administrator, will buy the enterprise edition by paying something less than 5$ and deploy it after some challenges. MS-SQLServer is the most favorite database in this market I think. Also MySQL gets famous by using in web-based projects. I also worked with DB2 in some cases. So In database business all databases are available. PHP, C#.Net and Java are the most popular technologies. .Net has thousands programmers. PHP has its market share and is getting larger day by day. J2SE and J2EE almost are available. Spring, JBoss and other frameworks are using widely. Indeed in enterprise area Java technology is the major one that follows COBOL legacy projects. In fact, J2EE using a vast of opensource technologies is the most favorite platform for Iranian companies who are working in enterprise level of the business such as Banking. Some companies has been accomplished their enterprise projects using Delphi or .Net, However now Java in enterprise level has no competitor. ERP solutions is a main competition advantage for large scale companies. But actually economic sanction has limited this market for western ERP companies. So Indian or Singaporean companies try this area more than European. Also some Iranian companies have tried to localize open source ERP solutions. Lack of technical reference and legal supports has effected ERP and specific industries more than general areas.

Integration
Following limitations and sanctions, software market has no a complete harmony between solutions and platforms. So integration is a rich market in Iran. I as a senior developer or consultant have worked in many integration projects. In fact integration is an inseparable part of large scale projects. In Iran a large scale software project implementation is just like somehow to completing a puzzle that you don’t have all pieces. The map is out of date. Moreover you have to use some pieces of other different puzzles to cover some areas of your puzzle with them. Some how there are missing pieces that you have to develop them. Finely you have to integrate them all. This is why I told that Iranian software market is something different.

Iranian Developer Teams, Working style
Following what I told, here you can find talented and solid developers who they know how to find the way. They almost have learned how to develop using minimum resources. They know how to integrate tools and products. They know how to dive into a unfamiliar technology. In this way they have a lot of, difficulties that even aren’t easy to imagine for other countries. Sun JDK’s, Source Forge, Oracle, Google Codes and many other libraries are closed to them. In other hand governmental restriction and filtering prevented Iranian students to search freely. At this time that I am writing this, WordPress and some other giant blogs are bocked to Iranians by some local rules and restrictions. Low speed internet connection is another problem. But, with all restrictions and problems the Iranian Software market keeps growing.

Posted in Software Engineering

Using temporary table to increase MySQL query performance

Published on July 3, 2010 by Saeid Zebardast

Sometimes you need to get reports or something like, that runs a complex query which includes `JOIN`,  `GROUP BY`, `HAVING` and etc. The time of running is depends on amount of records and complexity. To run queries faster we can use temporary tables.

How Temporary table helps us

How to use Temporary table in MySQL?
Creating temporary table is the same as creating regular table. I created the `user_usage_report` temporary table using below command:

CREATE TEMPORARY TABLE user_usage_report
SELECT u.id, o_u.size AS out_size, i_u.size AS in_size
FROM user u
INNER JOIN out_usage o_u ON o_u.user_id=u.id
INNER JOIN in_usage i_n ON i_n.user_id=u.id
WHERE
   o_u.start_date BETWEEN '2009-01-01' AND NOW()
  AND
   i_u.start_date BETWEEN '2009-01-01' AND NOW()
GROUP BY u.id

Next I run below select query through the same connection:

SELECT id, in_size, out_size FROM user_usage_report ORDER BY in_size, out_size

After using the temporary table, I realized that it acquires results several times faster than pure select. Just try it!

Tags: ,
Posted in MySQL

Software Market In Iran – Part 1

Published on July 1, 2010 by Amir Sedighi

Introduction
Iranian software companies favor some technologies and platforms, however they almost didn’t enter to other zones that are popular in other parts of the world. Here you will find a quick overview about favorite technologies that are in use by Iranian software companies and individual experts.

Iranian Software Market, Separated Just Like an Iceland
Following special political situation Iran’s software market has a special characteristic that you cant compare it with other countries in some ways. Following economic sanction and reducing international trades in high-tech area, Iranian software industry has passed its specific growing path among the years. Some technologies are getting more popular and some others remain unknown even for the best companies.

Also regarding to the global motivations of immigration, most of young talented Iranian engineers leave their motherland toward overseas. Some of them come back to Iran after a while to start new businesses. Some others try to outsource software projects to Iranian teams and individuals.

Copyright
Most of Iranian students doesn’t have any respect for Copyright rules likewise the government. So software duplicators have a large black legal market! Some how software brokers catch more than the software producer. I believe this is a side effect of restrictions. Anyway lack of copyright rule let them download anything and use without a cost and without any authorized support.

Lack of legal Services
Lack of legal branches and authorized software resellers, provides a huge market for companies and individual persons who can support prevented technologies that are under economic sanction policies. For example you may find an individual engineer who used to work with IBM mainframes in past years and now he supports some large mainframes alone.
In this area there are many hardware companies that they are importing hardware and high-tech technologies much expensice than their international costs without providing any support to their customers. They sell complex machines just like a chewing gum. “No return, no money back, no support, just take it and go.” In this complex and difficult market some customers prefer to have a low quality Huawei router that is supported by chines engineers rather than an unsupported brilliant Cisco device.

Every Threat is an Opportunity
Software market is not an exception too. In software business lack of legal branches makes a large market. However software industry grows locally to capture market of giants companies such as People Soft, SAP and IBM. Lack of authorized branches gives a great opportunity to some Iranian companies to gaining a lot of money by producing localized business applications. Some local companies now reached such as popularity that knowledge of using their applications is a pretty valuable skill. They runs their rules in general business application development. Iranian people are not familiar with English language as many other countries so they prefer to use localized business applications. As I mentioned Iran software market uses some special platforms and technologies instead of some other. In next part I introduce such technologies that use by Iranian companies widely.

Next Part.

Posted in Software Engineering

Continues Integration (CI)

Published on June 28, 2010 by Amir Sedighi

Introduction
This article have a quick overview of Continuous Integration, concepts and its current usage.
“Continuous Integration is a software development practice where members of a team integrate their work frequently, usually each person integrates at least daily – leading to multiple integrations per day. Each integration is verified by an automated build (including test) to detect integration errors as quickly as possible. Many teams find that this approach leads to significantly reduced integration problems and allows a team to develop cohesive software more rapidly. “ – Martin Fowler

Major Parts
When we implement a modification such as change, bug-fix or improvement thorough a software code, almost causes new bugs and failures that it could be better to find and fix them as soon as possible. The proper solution that has recommended by most of professionals is using of a continuous integration system within the development process.

Repository
The first and main tools of team development is the repository. All artifacts required should be placed in the repository. Repository keeps and maintain code versions and help us to track the development steps one by one. Repository also enables the team members to share their results together.

Build System
One of continuous integration disciplines is that, a single command should have the capability of building whole of the system. This means much easier and faster product generation.
There are many tools on the shelf that make us enable to build the application such as Ant and Maven.

Testing
Obviously we prefer to find bugs as soon as possible. Using automated testing the build system will be enable to run tests, find bugs and notify us automatically. In a team development everybody may commit partial cods within a day. Early building and testing is a key technique for finding bugs and incompatibilities. Build systems are able to run tests and find bugs immediately when the programmers commit the code or at least in a short time. The theory is that every commit should be qualified.

Advantages
Encourage using of source controls and unit testing that are the best practices of software development.
Early warning of code failures and bugs
Early running of unit tests.
Fast and easy building system.
Easy support for multiple branches of code.
It should be easy to find out who made the relevant changes to the new bug.

Posted in Software Engineering

Finding in Linux – Part 2

Published on June 22, 2010 by Amir Sedighi

Find is the must powerful tool for seeking files or directories in Linux. Here I just used find in some samples. People almost learn better using samples. A famous man who I don’t remember his name told that “the example is not a major tool for learning people. It is the only one.” So have a look on our samples:

amir@linuxus:~/projects$ find / -name project.txt

Looks for project.txt file starting at root directory. The -name switch defines that you are looking for a file name.

amir@linuxus:~/projects$ find . -name "*pars*"
./snmp/src/com/datispars
./snmp/out/production/snmp/com/datispars
./grails/com.datispars.blog
./grails/com.datispars.blog/scripts/com/datispars
./grails/com.datispars.blog/target/classes/com/datispars
./grails/com.datispars.blog/grails-app/controllers/com/datispars
./grails/com.datispars.blog/grails-app/domain/com/datispars
./grails/com.datispars.blog/web-app/WEB-INF/classes/com/datispars
./grails/com.datispars.blog/test/unit/com/datispars
./grails/tequila/target/classes/com/datispars
./grails/tequila/src/java/com/datispars

Looks for a ???pars??? file that placed in the current directory.

find /home/amir/projects/ -ipath "*projects*bluej*"
/home/amir/projects/bluej
/home/amir/projects/bluej/BoxImpl.class
/home/amir/projects/bluej/Box.java
/home/amir/projects/bluej/Box.class
/home/amir/projects/bluej/package.bluej
/home/amir/projects/bluej/BoxImpl.java
/home/amir/projects/bluej/README.TXT
/home/amir/projects/bluej/Box.ctxt
/home/amir/projects/bluej/BoxImpl.ctxt

Looks for a path name that consists ???projects????bluej??? phrases starting at “/home/amir/projects/” directory.

amir@linuxus:~/projects/eztracking/src$ find . -type d -name "*"
.
./db
./face
./util
./report
./bus
./web

Looks for whatever directories starting from current place.

amir@linuxus:~/projects$ find . -size -26c -size +23c -ls
3088834    4 -rwxrwxrwx   1 root     root           25 May 28 19:40 ./.metadata/.plugins/org.eclipse.jdt.core/988434607.index

This is fantastic. This simply lists the files that are greater than 23 characters and less than 26 characters and placed in the current directory.

amir@linuxus:~/projects$ find . -empty -exec ls '{}' \;
./eztracking/logs/slringapp.log
./eztracking/war/secure/images/tree/blank.html
./eztracking/out/artifacts/eztracking_Web/secure/images/tree/blank.html

This command needs a bit attention because it looks for empty files and list them all starting at current directory all by a single command!

find . -name "*.doc" -exec mv '{}' '{}x' \; 

If you gonna rename all *.doc files to *.docx this is the right one!

find . -mtime -2 -type f -exec ls -l '{}' \;

Provides a list of all files that modified within past two days. How Linux is flexible!

find /home/amir/projects -user amir

Looks for all files that belongs to the amir starting at “/home/amir/projects” directory.

find /home/amir/projects -user amir

Looks for all files that belongs to the amir starting at “/home/amir/projects” directory.

find . -iregex ".*oss.*"

Finds all files containing oss in their path name ignore case starting at current directory.

Posted in Linux

Sending tasks to background thread in cocoa with performSelectorInBackground

Published on June 22, 2010 by Kamran Vatanabadi

Download, open and run the following example, written in Xcode 3.2.2 and tested in Mac OS 10.6.4: Download simple multi thread example

Ok, before describing how it works, let us see the application it selfs. It has 3 counters, the first from the left has red counter, if you push the “Start in main thread”, the red counter starts to count from 0 to 10. While it counts the UI will be locked, so you can’t push other areas of the window even “minimize” or the “Quit” button, because the loop is running in main run loop.

But if you push one or both of the green or blue counter button you’ll see both of the counters start to count and the UI works well too. You can quit the application while they are counting, even you can start the red counter too. These two red and blue counters has been sent to background threads. Let us see how this can be done.

PerformSelectorInBackground is one of the methods of NSObject which allows to run a selector (method) in background thread. This is how we have used it in our program:

- (IBAction) startLoop2:(id) sender {
	if (!counter2IsActive)
           [self
             performSelectorInBackground:@selector(loop2)
             withObject:nil];

}

Too easy! We don’t want to pass any object to the selector so we’ve passed nil for “withObject”. The counter2IsActive flag is just for controlling the counter triggering, it starts to count if it hasn’t been started already. Now let us see what consideration we have to have in loop2 method.

- (void) loop2 {
	counter2IsActive = YES;
	NSAutoreleasePool *pool = [NSAutoreleasePool new];
	int i = 0;
	while (i < 10) {
		i++;
		[loop2Counter setIntValue:i];
		sleep(1);
	}
	[pool drain];
	counter2IsActive = NO;
}

At the start we initialize the counter2IsActive to YES, to make sure pushing the button doesn't start it again, this is why we make it NO at the end. But the most important part is assigning a pool to the new thread. If you don't you'll see some leaking error in Xcode console, something like this:

“2010-06-22 07:02:54.123 SimpleMultiThread[488:4813] *** __NSAutoreleaseNoPool(): Object 0x1004a7670 of class NSCFString autoreleased with no pool in place - just leaking”

All you have to do is writing the following code:

NSAutoreleasePool *pool = [NSAutoreleasePool new];

The cocoa will use implicitly the pool for new thread. Just don't forget to drain the pool at the end of the method.

Also note that we have used simple C function of sleep(1) to have delay in background threads but we can't use this in main thread because it totally blocks the run loop, we have instead used some other method to make 1 second delay in run loop as follows:

- (void) mainRunLoopDealy {
    NSDate *date = [NSDate dateWithTimeIntervalSinceNow:1];
    [[NSRunLoop currentRunLoop] runUntilDate:date];
}

Download simple multi thread example

Posted in Cocoa/Objective-C

Finding in Linux – Part 1

Published on June 21, 2010 by Amir Sedighi

Introduction
Having an acknowledge of Linux file system let to work better. Here is an introduction to Linux Filesystem Hierarchy Standard (FHS) and some samples of finding files commands.

Linux Filesystem Hierarchy Standard (FHS)
Linux or other UNIX®-like systems use a specific standard for categorizing and management Filesystem Hierarchy. This standard has definced as a common layout of directories. FHS lets both users and software to guess the location of installed files, libraries and directories.

FSH Categories

FSH defined two separated categories:
Shareable/unshareable
Shareable files can be used by another system; however, unshareable files are just useable by some specific applications.

Static/variable
Static files modify just in an administration activity such as installing or updating packages; but, every other else files that modify during using is a varaible files such as logs, spools, database, streams …
Have a look in below table:

           Shareable      Unshareable
Static	 /usr              /etc
         /opt	            /boot
Variable   /var/mail         /var/run
                            /var/lock

Where is my file?
Linux works based on files so it has thousands and directory. Whatever you know how to use files then you use Linux better. Some few commands and cominations help me to work within this vast sea of files.

Where the PATH does locate?
Below samples show how to find out where the PATH locate. This is important to know path is user dependent. Below sample shows it clearly that every user has different PATH.

amir@linuxus:~$ echo $PATH
/home/amir/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/amir/java/xmlbeans-2.4.0/bin:/home/amir/grails-1.2.2/bin:/home/amir/java/maven/bin
amir@linuxus:~$ sudo su
[sudo] password for amir:
root@linuxus:/home/amir# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

Assume I need to add /home/amir/run location to the PATH. Then I use below command:

export PATH=/home/amir/run:$PATH

After a while you will notify that you have separated files and locations from a certain application on your system and you need to know which one placed in the PATH. To search through the PATH you can use which command:

amir@linuxus:~$ which java
/usr/bin/java

Sometimes you need to find out any other possibly location that a file may placed:

amir@linuxus:~$ which ruby
/usr/local/bin/ruby
amir@linuxus:~$ which -a ruby
/usr/local/bin/ruby
/usr/bin/ruby

Now mix it up with ls command:

amir@linuxus:~$ ls -l $(which -a ruby)
lrwxrwxrwx 1 root root  7 2010-04-04 15:02 /usr/bin/ruby -> ruby1.8
lrwxrwxrwx 1 root root 16 2010-04-04 15:08 /usr/local/bin/ruby -> /usr/bin/ruby1.8

If you need more information than just the location of a file, then you can use the whereis command:

amir@linuxus:~$ whereis java
java: /usr/bin/java /etc/java /usr/share/java /usr/share/man/man1/java.1.gz

Also you can use locat command:

amir@linuxus:~$ locate Setup.xcu
/home/amir/.openoffice.org/3/user/registry/data/org/openoffice/Setup.xcu
/home/amir/.openoffice.org2/user/registry/data/org/openoffice/Setup.xcu
/usr/lib/openoffice/basis3.0/share/registry/data/org/openoffice/Setup.xcu
/usr/lib/openoffice/basis3.0/share/registry/res/en-GB/org/openoffice/Setup.xcu
/usr/lib/openoffice/basis3.0/share/registry/res/en-US/org/openoffice/Setup.xcu
/usr/lib/openoffice/basis3.0/share/registry/res/en-ZA/org/openoffice/Setup.xcu

Locate runs fast because it uses a database of stored path information rather than searching the filesystem every time.
To run daily updates, the root user sould edit /etc/updatedb.conf and set DAILY_UPDATE=yes. To create the database immediately, run the updatedb command as root user. Get the statistics using -S:

amir@linuxus:~$ locate -S
Database /var/lib/mlocate/mlocate.db:
	62,263 directories
	497,078 files
	38,229,762 bytes in file names
	15,193,900 bytes used to store database

Posted in Linux

« Older Entries
Newer Entries »

Categories

Archives