22 October 2014

About Batabase Transactions

DB Transactions has 4 attributes, which is expressed as ACID, where:

A -> Atomicity, means transaction run as an atomic single unit of work, either all is successes and committed or all rollbacked in case of failure.

C -> Consistency, means the transactions should leave the DB data in consistent state, regardless of its success or fail. so, this attribute is ensured using the first attribute.

I -> Isolation, means the user transaction should run in isolation from other users transaction, and no one should affected by others during the single transaction. also this ensures DB consistency.

D -> Durable, means the transaction should be written permanent to the DB after the transaction committed, even if the system crashes afterwards.

When two ore more transactions are operate concurrently on the same data, the following errors might happen:
  1. The first transaction write some data to the DB but still the transaction not committed, a second transaction come and read the modified data, then the first transaction rolled-back.
    This called "Dirty reads", and can prevented by applying the "Isolation" attribute, so every transaction should be isolated from other transactions in terms on data modifications.

  2. The first transaction read some data, then a second transaction come and modify that data and then commit, so the data written to the database. meanwhile the first transaction is still running and when come to read the same data again, it find it changed.
    This called "Non repeatable reads" which means multiple reads by some transaction to the same data is differ. and this can prevented by having some row-level locks on the database, so once a transaction start reading/modifying some data no other transaction cannot use until this transaction ends.

  3. The first transaction read some rows using a certain where condition, then a second transaction come and insert a new row that reside in the where condition area and then committed. When the first transaction come and re-query the first select with that certain where condition, it finds the rows number changed.
    This called "Phantom reads" and this cab be prevented by applying table-level lock whenever a transaction come and start reading/modifying some data in a table, the transaction acquire full-table lock.
This leads to talk about Isolation levels, which are (from less control to more control, and more control means low performance):
  • Read Uncommitted
    Read dirty data, data before being committed by other transactions, leads to all there "Dirty reads", "Non repeatable reads" and "Phantom reads"
  • Read Committed
    Read only committed data, lead to only "Non repeatable reads" and "phantom reads".
  • Repeatable Read (lock data)
    Transaction acquire lock on the data being read (either cell-level or row-level locks), so nobody can change the data until transaction commit or rollback. but still "Phantom reads" can happen.
  • Serializable (lock table)
    Table-level lock. Nobody can touch the whole table until the transaction committed or rolled-back. no isolation violations can happen.

Read more here:

Source (with my modifications) Spring in action 3rd ed. ch 06

21 October 2014

Indirect login to Target System

The following is a sequence diagram for direct/indirect login to a Target system using user's credentials os Main System.

18 October 2014

shortly.pw is an open source URL shorten, unshorten and tracker

I'd like to talk about shortly.pw which is an website to allow you not just to shorten your URLs, also to unshorten the URL for any shorten services and a URL click tracker.

Feature (see screenshots below):

  • Easily copy the result link.
  • Track the shorten link in terms of time of clicks and details about each click.
  • Unshorten URLs from other services such as goo.gl, tiny url, etc..
  • API available to use the service from your app (mobile, desktop, website, etc...)
  • you get all the other features with no need to login.

Easily copy the result URL:

 Shortly keeps detailed information about links you shorten. (no login required) 
You can unshorten URLs from any other service (bit.ly, tiny url, goo.gl, etc..)

Also you can use the HTTP API from your application, see: http://api.shortly.pw/apidoc

New features are being added..
Shortly plan: https://trello.com/b/or9dg3Zr/shortly-pw-plan
Shortly source code at Github: https://github.com/MuhammadHewedy/short-url

27 September 2014

Ifelse shortcut

if (testCondition() && executeMe() || executeMeOtherwise());

Origin come from bash, to execute if else on a single line:
[ $# == 0 ]  && echo 'invalid input' || processInput($*)
If no parameters passed, the echo 'Invalid input' else, start processing the input.

02 September 2014

Objective-c delegates

It is a long time I since I stopped working on iPhone and objc.

But some collage is working and we was discussing about objc delegates and what is it and how it is related to categories.

Following is an example illustrate how things going from Categories to Delegates.

// NSObject+TurnLights.h
// a Category on NSObject to allow any subclass to turn on/off lights
@interface NSObject (TurnLights)

-(void) turnLightsOn;
-(void) turnLightsOff;


// this is the lights class that delegate its work to some object (of type id) to do the on/off instead of itself
@implementation Lights

@synthesize id delegate;

-(void) doLightings{
      [delegate turnLightsOn];
      [delegate turnLightsOff];

// is my controller class that decided to be a delegate for the Lights class

#import "NSObject+TurnLights.h"

@implementation MyController

-(void) btnClicked:(id) sender{
      Lights* lights = ...
      [lights setDelegate: self];

-(void) turnLightsOn{

-(void) turnLightsOff{

30 May 2014

continually monitor CPU usage for a command

cmd=[m]oviecrawler;while :; do echo -ne "CPU: `ps aux | grep $cmd | awk '{print $3}'`\\r" ;  sleep 1; done