Skip to content

Absrd Home

hernansaab edited this page Aug 29, 2012 · 28 revisions

Welcome to the absrd wiki! This tool takes advantage of concurrency implemented with gcd allowing concurrent sql queries performed as one. It also offers a lean object oriented wrapper to mysql. This code is not tested in mobile devices. It is meant to run on macbook pro or other osx10 desktops.

Sample Show Off Code

Here we are creating 5 different query blocks. 1 of them query db called default and 4 of them query db called web1

Partial code as follows:

#import "ABSRD/MODELS/AbsrdConf.h"
#import "ABSRD/MODELS/QueryBlockBuster.h"
#import "ABSRD/MODELS/DbManager.h"
#import "ABSRD/MODELS/DbConnector.h"

......
......
...
// first step is to setup the conf for both databases we are going to use
    NSDictionary *conf = @{ @"absrd":
    @{@"databases":@{
            @"default": @{
                @"type": @"mysql" ,
                @"port": @3306,
                @"host":@"localhost",
                @"user": @"root",
                @"password":@"myroot",
                @"database": @"fb"
            },
            @"web1": @{
                @"type": @"mysql",
                @"port": @3306,
                @"host":@"web1",
                @"user": @"root",
                @"password":@"myroot",
                @"database": @"Proficiency"
            }
        }
    }};
// NSDictionary containing db configuration data for 2 different databases are set into the setConfiguration 
//method.
    [AbsrdConf setConfiguration:conf];

AbsrdQueryBlock block1 = ^(){
        DbConnector *default_conn = [DbManager getConnector:@"default"];;
        // you can also use queries like this anywhere in your code. The data is returned in an array 
        // of dictionaries where the elements of the array are the rows. Each row is a dict with keys as
        // columns in NSString  with values in NSString.
        NSMutableArray *res = [default_conn query:@"select * from courses limit 1"];
        return res;
    };

    AbsrdQueryBlock block2 = ^(){
        DbConnector *web1_conn    = [DbManager getConnector:@"web1"];;
        NSMutableArray *res = [web1_conn query:@"select * from t_contents limit 1"];
        return res;
    };

    AbsrdQueryBlock block3 = ^(){
        DbConnector *web1_conn    = [DbManager getConnector:@"web1"];;
        NSMutableArray *res = [web1_conn query:@"select * from t_resources limit 1"];
        return res;

   };
    
    AbsrdQueryBlock block4 = ^(){
        DbConnector *web1_conn    = [DbManager getConnector:@"web1"];;
        NSMutableArray *res = [web1_conn query:@"select * from t_activities limit 1"];
        return res;
    };
    
   AbsrdQueryBlock block5 = ^(){
        DbConnector *web1_conn    = [DbManager getConnector:@"web1"];;
        NSMutableArray *res = [web1_conn query:@"select * from t_content_thumbnails limit 1"];
        return res;
    };

// Now we add these query blocks to the query blaster

    QueryBlockBuster *bb = [[QueryBlockBuster alloc] init];
    [bb addQueryBlock:block1];
    [bb addQueryBlock:block2];
    [bb addQueryBlock:block3];
    [bb addQueryBlock:block4];
    [bb addQueryBlock:block5];


// And now we execute all queries in 5 concurrent queries saving you a fraction of a time compared to
// querying them sequentially

//AND HERE IS THE AWESOME COMMAND!!!
    NSMutableArray *result = [bb multiQueryParallel];

// The initial queries will be slower because connections are created. 
//But connections are recycled and are used to make sure that every concurrent queue has its own 
//connection for a given db. This means that future queries performed by multiQueryParallel will be a lot 
//faster once connections are reused.

--------------

function "addQueryBlock:" allows you to add a block query. This means that you can also perform 
a rest request or any other service in parallel with other queries. If addQueryBlock is too powerful 
and verbose for you, try function "addQuerySqlString:(NSString*) query forDb: (NSString*) db".
For example adding block1 is equivalent to the following:

[bb addQuerySqlString:@"select * from courses limit 1" :forDb:@"default"];




Installation

PRE XCODE STEPS

1. Make sure you have the mysql client installed in your mac. Go to the following link for details:
http://dev.mysql.com/doc/refman/5.0/en/macosx-installation.html

After installation if you didn't try anything fancy you should have a directory exactly like the following:
/usr/local/mysql 
This means you should have the following paths that you will use in the project settings
/usr/local/mysql/lib
/usr/local/mysql/include

Now here is another fancy step. After installation inside  /usr/local/mysql/lib there should be a file 
called libmysqlclient.18.dylib  or libmysqlclient.19.dylib or 20 or so. Version doesn't matter.

2. /usr/local/mysql is a virtual link. Find the actual link and run a command like the following 
(please don't ask. If you don't do it, XCODE will not be able to perform sql queries):

sudo install_name_tool -id /usr/local/mysql-5.5.14/lib/libmysqlclient.18.dylib  /usr/local/mysql-5.5.14/lib/libmysqlclient.18.dylib   
   
The goal in here is to find the actual full path of file libmysqlclient.18.dylib 
(or whatever version you get) and to make it usable.


XCODE Steps

1. Add the following path to "header search paths under "build settings"
/usr/local/mysql/include

2. Add the following path to "library search paths under "build settings"
/usr/local/mysql/bin

3. Drag and drop file libmysqlclient.18.dylib (or whatever number you get) to your XCODE project. 
You can chose to copy it or reference it in your project. I chose to reference it.

4. Now drag and drop the ABSRD path into your XCODE project inside the github project for absrd. 
Make sure you  chose the "copy items into destination" and "create folder references" and 
the target must checked.

5. Add file libabsrd_static_library.a  to your XCODE project, from static_lib directory in GIT absrd project.

At this point you should have 3 items added to you project, files libmysqlclient.18.dylib and libabsrd_static_library.a and a directory called ABSRD.

6. Now you can start using absrd. Use the examples provided to help you out.

Licensing

I am basically using the new BSD license

Copyright (c) 2012, hernan saab
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the <organization> nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL hernan saab BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


Clone this wiki locally