Sunday, January 17, 2016

One quick way to save battery on android

Install a firewall - mobiwol app 

https://play.google.com/store/apps/details?id=com.netspark.firewall&hl=en

This firewall doesn't need your phone to be rooted.

It has a simple interface. Configure the apps that you want it to access the internet via your data pack or Wifi. Like in the below screenshot.

   Mobiwol: NoRoot Firewall- screenshot

For example you can set an app to access internet only via wifi and not your data pack hence saving you your expensive data pack by a few mbs per app.

On selecting only the apps which you want to access the internet, you are disabling

  • Frequent update checks
  • Frequent version checks etc.


Which inturn reduces the battery usages.

( Explanation: Even if you have an app that you never used, it would still make calls to its servers to check for updates. The frequency of this depends on the app. Once you install this app on checking upon the logs you can find out those apps and block em temporarily )

There are a lot of other basic rules like, 
1. Use wifi less , manage it well, there are apps that can schedule it to run every once in 15 mins or whatever time period you set.
2. turn off GPS when not in use.
3. Uninstall apps that you dont use
4. Charge your phone once it is below 10% not above it. (purchase a portable charger if it would help your cause.)
5. Set Datasaver ON on your Chrome Browser / any other browser that provides the feature
Chrome - Settings - Data Saver - On

I have been using my moto x for some time now. I use mobiwol, you can try other apps in the store. For me this app has reduced my battery drain and saved considerable amount of data usage.


Note:This firewall uses a VPN for more info checkout Android forums.
http://forum.xda-developers.com/showthread.php?t=2790642


Thursday, January 7, 2016

Console Application to Create and Read from Couchbase views

Creating a View

Log into Web Console

Go to Views - Development Views - Add View



Give a Design doc name and View Name can be anything.

In my case I will keep it as trial and trialView respectively.

Now Under Development Views you will find the view you have created

Click edit.







Here you can ignore the top row.
The View Code will contain the logic of your view. In sql terms , the select Query,

function (doc, meta) {
  if(doc.Name)
    emit(meta.id,{"Emp_name":doc.Name, "Emp_age": doc.Age, "Emp_salary": doc.Salary} );
}

This code checks if the documents have a property called name, if so it will return the name, age and salary. This query will be run on default DB.

The Reduce box to the right helps us with aggregate functions such as count , sum etc.

Once you have entered the MAP code hit SAVE

Go to Views page now.

 In development Views select PUBLISH against the VIEW you have CREATED.



Now to integrate it with the C# code

  private static void GetFromView()
        {
            using (var bucket = Cluster.OpenBucket())
            {
                var query = bucket.CreateQuery("trial", "trialView", false);  // trial is the Design Doc name and trialView is the View name

                var result = bucket.Query<Employee>(query);             // will be using the Employee POCO class defined below
                foreach (var row in result.Rows)
                {
                   // Console.WriteLine(row.Key+" "+row.Value);
                    Console.WriteLine(row.Key + " EmployeeName: " + row.Value.Name + " EmployeeAge:"+ row.Value.Age +" EmployeeSalary:"+ row.Value.Salary);
                }
            }


public class Employee
{
    [JsonProperty("Emp_name")]
    public string Name { get; set; }

    [JsonProperty("Emp_age")]
    public string Age { get; set; }

    [JsonProperty("Emp_salary")]
    public string Salary { get; set; }

}




Creating a Console Application for Couchbase CRUD operations

Here is a sample application for performing few CRUD operations.

Requirements: VS 2012

Steps:

Create a console Application and

1. GO to Tools > Library Package Manager > Package Console

2. Install-Package CouchbaseNetClient





Upsert - Insert new records or Update existing records


  private static void UpsertData()
        {
            using (var bucket = Cluster.OpenBucket())   // Opens "default" DB if DB name is not specified
            {
                var document = new Document<dynamic>
                {
                    Id = "Hello",
                    Content = new
                    {
                        name = "Couchbase"
                    }
                };                                                                  // Create Document/Row to be inserted

                var upsert = bucket.Upsert(document);
                if (upsert.Success)
                {
                    var get = bucket.GetDocument<dynamic>(document.Id);
                    document = get.Document;
                    var msg = string.Format("{0} {1}!", document.Id, document.Content.name);
                    Console.WriteLine(msg);
                }
  
            }
      

 private static void DeleteData()
        {
            using (var bucket = Cluster.OpenBucket())
            {
                var document = new Document<dynamic>
                {
                    Id = "Hello"

                };

                var get = bucket.GetDocument<dynamic>(document.Id);
                document = get.Document;
                var msg = string.Format("{0} {1}! Deleted", document.Id, document.Content.name);
                var remove = bucket.Remove(document); // Delete Document Using ID
                if (remove.Success)
                {
                  
                    Console.WriteLine(msg);
                }

            }
        }







Wednesday, January 6, 2016

Query Workbench Installation - N1QL - SQL type queries


Query Workbench is when you want a SSMS / mysql client kinda interface.


  • Enables you to write queries like SQL queries. 
  • These queries are called N1QL queries pronounced as Nickel Queries

Download : 

  • Extract
  • Run the "Launch ..." bat file.
  • Command Window will popup saying Launching UI server, to use, point browser at 
  • http://localhost:8094Hit enter to stop server:
  • From Browser go to the address mentioned

Once you are done:

you can run SQL like queries ,


NOTE: if you are not able to see anything in Queryable Buckets on the left side then 

Type 
CREATE INDEX `beer-sample-type-index` ON `beer-sample`(type) USING GSI;

or

CREATE INDEX id_ix on `beer-sample`(meta().id);

These are diff types of indexes being set on beer-sample DB.

Also the DB name should be enclosed in BACK TICK and not single inverted commas



Sample N1QL queries: 




Installing and configuring Couchbase 3.0

For single Cluster
Step 1
Download couchbase from http://www.couchbase.com/nosql-databases/downloads

Step 2
After you run the MSI file your browser will open up with this page. Click on Setup to continue




Step 3
Here we need to Provide the path to Disk Storage - Change it if you want to else keep the same.

Ram Quota I have set it for 2 GB




In the same Page Below Select
Start New Cluster




Step 5

Then in the next page you will be asked to install sample Data buckets / Sample Tables



Select any one or both


Step 6

Create Default Bucket: this is your Default Database. Kinda like the Switch Statement DEFAULT functionality like when we dont define any DB name , the query will be executed in the default DB




Step 7
Just select I agree on terms and conditions



Step 8:
Choose Username and Password






Step 9:
Run the Couchbase console from Program Files of your system.



So here once you go to "Data Buckets" you will find the sample DB you installed along with the default DB.

In Server Nodes : You will have one Server i.e Local Host .
Cluster Overview  : will contain realtime self explanatory graphs

Views: They are similar to the views in SQL / relational DBs.
Index : consits of index ids of all the documents.
XDCR : Disaster recovery








SQL to Couchbase - Quick Reference/ Crash Course

Why Couchbase ? Why migrate from Relational ? For the SQL mind

short notes on Couchbase

Image result for couchbase sql funny



Terminologies

RDBMS           Couchbase/Nosql
Table                Collection/Bucket

Row                  Document

Column             Field

Relationships    Linking and Embedding Documents


Scalability: 
Since Data is consistently increasing , couchbase handles it well when compared to relationalDB


High Performance:
Data Retrieval is faster and data is rendered faster from queries preventing timeouts which is freq in SQL in case of large tables. 


24*365 Availability:
Whenever there is a schema change in SQL or something related to constraints etc we usually need downtime (not at all times) - In case of Couchbase there is no need for Downtime, schema changes can be dynamic. Thus making it a good choice for Agile apps.

Flexible DataModels:
Complex data types can be stored in JSON format - dataStructures such as lists, arrays , hash can be stored easily in JSON format. Think about how much time is saved by preventing the need to fill the LIST/Array from SQL DB in the code.


Architecture:

Consists Mainly of:




Data Manager: Consists Ramcache and the Storage Engine

Cluster Manager: uses Erlang (the same used in whatsapp Servers to manage load better)



Data Types Supported:

string, int, boolean, datetime, number, binary. (stored in base 64 format)

Complex Data types such as Arrays, Hashes, lists can be stored in JSON format

Schema changes can vary document to document i.e in SQL language it can vary from one row to another and can be done dynamically without dropping the existing table and data.


Storage Operations:

Data Stored in the form of Key Value - like hash

Data is stored in Ram Cache . ie. for Read or Write operation in the DB
the Ram Cache acts as the interface. Hence reducing the response time tremendously.
As disk Read / Write (i.e to the DB) takes more time.  For set/ write operation the data is always written to the Ram Cache and put into a Disk write Queue & Replication Queue.
In case of CacheMiss disk read occurs thru Ram Cache some latency will be there.