Tuesday, 9 May 2017

Cloud Computing

Working in an investment bank. I always wonder why we dont use clouds. Latest in the market.
Therefore, curiosity lead to me read about Amazon Web services.

Amazon web services really have cool stuff in there.
It provides servers to run your applications, save your code, firewalls, caching, databases
Everything that is require for running your business (hardware and Software) can be on cloud.
Cool isn't it? No it is not.

Everything on cloud, for a business like investment bank.
One can't risk it due to following reasons -
1. Vulnerable for attack
2. Downtime
3. security and privacy
4. Limited control & flexibility
A bank  can't afford first three points at any cost. As any of this can make loss of millions or even billions.

Since so much things provided in AWS, for any system in bank which doesn't directly effect business. we can definitely analyse cloud computing.

Or we can think of making such a service (cloud computing) within the bank which is reliable, secure and as vulnerable as other bank systems making its easy to manage the servers and services then making the banking system cost effective

Quick Sort

public class QuickSort {
    private final int[] arr;

    QuickSort(int[] arr) { this.arr = arr; }

    public void sort() {
        sort(0, arr.length - 1);
        System.out.println(count);
    }

    static int count = 0;

    private void sort(int p, int q) {
        count++; int pivot = partition(p, q);
        if (pivot - 1 > p) sort(p, pivot - 1);
        if (pivot + 1 < q) sort(pivot + 1, q);
    }

    private int partition(int p, int q) {
        int x = arr[p];
        int i = p + 1;
        int j = q;
        while (i <= j) {
            if (arr[i] < x) i++;
            else if (arr[j] > x) j--;
            else if (arr[i] > x && arr[j] < x) swap(i++, j--);
        }
        swap(p, i - 1);
        return i - 1;
    }

    private void swap(int i, int j) {
        int tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp;
    }
}

Caching

Here we are caching "int" "pageNumber" in cache. in queueNode.
Objects can be saved instead of "int" and Caches can be maintained and objects can be removed from Cache based on policy. that we need to define in our saved "Object".

Here I have given Java Implementation of LRUCache. Soon I will be back with another policy cache.

public class LRUCache {
    Queue queue; Hash hash;

    public LRUCache(int numberOfFrames, int capacity) {
        queue = new Queue(numberOfFrames);
        hash = new Hash(capacity);
    }

    public static void main(String[] args) {
        // Let cache can hold 4 pages & 10 different pages        // can be requested (numbered from 0 to 9)        LRUCache cache = new LRUCache(4, 10);
        cache.referencePage(1);        cache.referencePage(2);
        cache.referencePage(3);        cache.referencePage(1);
        cache.referencePage(4);        cache.referencePage(5);
        cache.queue.print();
    }

    class QNode {
        QNode prev, next;
        int pageNumber;
        QNode(int pageNumber) { this.pageNumber = pageNumber; }
    }

    class Queue {
        int count, numberOfFrames;
        QNode front, rear;
        Queue(int numberOfFrames) { this.numberOfFrames = numberOfFrames; }

        void print() {
            QNode temp = front;
            while (temp != null) {
                // Let us print cache frames after the above referenced pages                System.out.println(temp.pageNumber);  temp = temp.next;
            } } }

    class Hash {
        int capacity;
        QNode array[];

        Hash(int capacity) { this.capacity = capacity;
            array = new QNode[capacity];
        } }

    boolean areAllFramesFull(Queue queue) {
        return queue.count == queue.numberOfFrames;
    }

    boolean isQueueEmpty(Queue queue) { return queue.rear == null; }

    void deQueue(Queue queue) {
        if (isQueueEmpty(queue)) return;
        // if this is the only node in list, then change front        if (queue.front == queue.rear) queue.front = null;
        queue.rear = queue.rear.prev;
        if (queue.rear != null) queue.rear.next = null;
        queue.count--;
    }

    void enQueue(Queue queue, Hash hash, int pageNumber) {
        if (areAllFramesFull(queue)) {
            hash.array[queue.rear.pageNumber] = null;
            deQueue(queue);
        }
        QNode temp = new QNode(pageNumber);
        temp.next = queue.front;
        if (isQueueEmpty(queue)) queue.rear = queue.front = temp;
        else { queue.front.prev = temp;
            queue.front = temp;
        }
        hash.array[pageNumber] = temp;   queue.count++;
    } 
    
    void referencePage(int pageNumber) {
        QNode reqPage = hash.array[pageNumber];
        // the page is no3t in cache, bring it        if (reqPage == null) enQueue(queue, hash, pageNumber);
            // page is there and not at front, change pointer        else if (reqPage != queue.front) {
            // Unlink rquested page from its current location in queue.            reqPage.prev.next = reqPage.next;
            if (reqPage.next != null) reqPage.next.prev = reqPage.prev;
            // If the requested page is rear, then change rear as this node will be moved to front            if (reqPage == queue.rear) {
                queue.rear = reqPage.prev;
                queue.rear.next = null; }
            // Put the requested page before current front            reqPage.next = queue.front;  reqPage.prev = null;
            // Change prev of current front            reqPage.next.prev = reqPage; 
            // Change front to the requested page            queue.front = reqPage;
        } } }