Showing posts with label Code. Show all posts
Showing posts with label Code. Show all posts

Saturday, 9 May 2015

Twinkle Twinkle Little Star How Do I Wonder Which NoSql Database Is Suitable So Far?

NoSQL databases have no longer become an unknown database paradigm, since it has been almost a decade or two after its introduction in the market. Some of the most popular NoSQL databases include MongoDBCouchDBVoldemortCassandra etc. The use of these databases in the industry, mainly due to their elastic nature and simplified application development, is spreading like a viral disease these days. Everyone tries to use them in their applications, most probably because of their open source nature. But the question arises “Is every NoSQL databases fit for every kind of purpose?” Well, certainly not, partially because all of these databases have different features and schema definitions and partially because there is no single proper way to compare performance of these databases. “So how someone can compare what kind of NoSQL is suitable for their workload?” This is definitely a question to ponder upon.
Some NoSQL databases have their own benchmark tools such as the ones proposed byMarkLogic and Riak etc. One of such benchmarking tool is the open source YCSB (Yahoo Cloud Serving Benchmark) which is initially designed for testing the performance of Yahoo’s PNUTS (a parallel and geographically distributed database system). The main reason of developing YCSB as defined by Brian F. Cooper, the mastermind behind YCSB, is “The purpose of using the Yahoo! Cloud Serving Benchmark (YCSB) is to develop a framework and common set of workloads for evaluating the performance of different databases”. YCSB is mainly designed for evaluating the performance and scalability of NoSQL and “Cloud based” data stores. These are the two main tiers used for the evaluation of NoSQL databases though YCSB Benchmark. On one hand, the performance tier (tier 1) calculates the throughput and latency of NoSQL databases after increasing the server load, and on the other, the scalability tier (tier 2) measures the scalability of databases by increasing the number of servers and monitoring their performance. However, the configuration of YCSB is quite simple and consists of only two main parts:
  1. The workload generating client – used for generating load and making decisions about which operation to perform, what record to insert of delete etc.
  2. Workload packages – standard and custom defined packages for defining read/write mix operations, request distribution and record size etc.
Once you are done with these configurations and minor tweaking (in case this benchmark is not supporting your database), you can easily generate different comparison reports to compare performance and scalability of various NoSQL databases.  A few of the NoSQL database comparisons benchmarked by YCSB can be found in [1] and [2]. Furthermore, YCSB is also planning to release two more evaluation tiers of replication and availability in future which will definitely the increase the effectiveness of using this tool.
Here are some links which might interests you:

Shamir Secret Splitting Code In C++

This is the rough code for Shamir secret splitting and combining written in C++. Kindly change according to your own preferences :) P.S: you have to know the basics of Shamir Secret Sharing and Lagrange’s Interpolation before understanding this code. More details can be found here and a java implementation of Shamir Secret Splitting can be found here.
void split(int number, int available, int needed, int prime){
int coef[10];
int shar[10][2]={0,0};
coef[0] = number;
int n = prime - 1;
for(int c = 1; c < needed; c++) {
coef[c] = rand() % n + 1; // (prime-1)+1 is upper and lower bound respectively
}
int x, exp, y;
double accum;
for(x = 1, y = 0; x <= available; x++)
{
/*
* e.g coef = [1234, 166, 94] which is 1234x^0 + 166x^1 + 94x^2
*/
for(exp = 1, accum = coef[0]; exp < needed; exp++)
{
double inn= fmod((pow(double(x), exp)), prime); //pow func always take first argu as float or double
double ac = accum + fmod(coef[exp] * inn,prime);
accum = fmod(ac,prime);
//accum = (accum + (coef[exp] * (Math.pow(x, exp) % prime) % prime)) % prime;
} // Modular math
/*
* Store values as (1, 1494), (2, 1942), (3, 2578), (4, 3402), (5, 4414) (6, 5614) in a 2D Array
*/
shar[x – 1][y] = x;
shar[x – 1][y + 1] = accum;
std::cout << “( index = “<< shar[x – 1][y];
std::cout << “, value = “<< shar[x – 1][y + 1] << ” )” << std::endl;
}
}

/* This is the recursive method Using Extended Euclidean Algorithm*/
/* This function return the gcd of a and b followed by
the pair x and y of equation ax + by = gcd(a,b) */
pair<int, pair<int, int> > extendedEuclid(int a, int b) {
if(a == 0) return make_pair(b, make_pair(0, 1));
pair<int, pair<int, int> > p;
p = extendedEuclid(b % a, a);
return make_pair(p.first, make_pair(p.second.second – p.second.first*(b/a), p.second.first));
}
int modInverse(int a, int m) {
return (extendedEuclid(a,m).second.first + m) % m;
}
int combine(int shares[10][2], int primeNum, int threshold) {
long double accum = 0, startposition =0, nextposition =0;
for (int i=0 ; i<threshold; i++)
{
cout<< shares[i][0] << “-“<<shares[i][1] << endl;
}
for (int i = 0; i < threshold; i++) {
long double num = 1;
long double den = 1;
for (int j = 0; j < threshold; j++) {
if (i != j) {
startposition = shares[i][0];
nextposition = shares[j][0];
num = fmod((num * (-nextposition)), primeNum);
den = fmod((den * (startposition – nextposition)), primeNum);
}
else if (i==j) {continue;}
}
cout<< “den: ” << den ;
cout<<” num: ” << num ;
int mi = modInverse(den, primeNum);
cout<<” inv: ” << mi <<endl;
int value = shares[i][1];
long double tmp = value* num * (modInverse(den, primeNum));
long double acc = (accum + primeNum + tmp);
accum = fmod(acc,primeNum);
cout<< ” value: ” << value ;
cout<< ” tmp: ” << tmp ;
cout<< ” accum: ” << accum <<endl;
}
cout<< “The secret is: ” << accum;
return accum;
}