Wednesday, March 21, 2018

How to recover accidentally lost code from detached head in Git

Recover accidentally lost code from detached head in Git:-

While working on the detached head on the master, I made the changes and committed the code to a detached head. Accidentally I switched to another remote branch. When I switched back to master branch, all code changes were not shown here somehow. 

To Recover the code solution is, In the .git folder of repository, there is 'logs' folders. It contains  a file head that keeps the last of all the changes with respective hashes and dates to repository. To recover the last code changes made on a detached head, Copy the last hashcode corresponding to your change and perform the following steps in your project folder
  1. git checkout <copied hash of required commit from the list>
  2. git checkout -b <new branch name>
  3. git checkout <master>
  4. git merge <branch name>
All the changes were recovered. It saved me a lot of rework. 

Tuesday, March 20, 2018

Create New User for a Postgres DB

To create a new user for a Postgres DB and allow it to access a database,  Commands are:-

Switch to Postgres User:-  sudo su postgres

To Create a User with Password:-
CREATE ROLE 'shiprauser' LOGIN PASSWORD 'shiprauserpassword';

To Create a Database with Owner:-
CREATE DATABASE shipradb OWNER shiprauser ENCODING 'UTF8';

To Grant all privileges on Database:-
 grant all privileges on database shipradb to shiprauser; 

Wednesday, March 7, 2018

Riak with Java tutorial - Basic Riak Setup and Cluster Connection Part 1

Basic Riak Setup and Cluster Connection

Start with riak setup on local or remote machine. Click here to get started with Installation Of Riak.

Maven dependency to include Riak client in your project, add it to pom.xml of your project.

       <dependency>
            <groupId>com.basho.riak</groupId>
            <artifactId>riak-client</artifactId>
            <version>2.0.0</version>
        </dependency>

Setting up a Cluster: First step in using java is to setup riak cluster, riak client and then check connection with Riak DB.
RiakConfig class

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

import com.basho.riak.client.core.RiakCluster;
import com.basho.riak.client.core.RiakNode;

public class RiakConfig {
    private static RiakCluster cluster = RiakConfig.getCluster();
    /**
     * This value is dervied from property file.
     * its value can be 127.0.0.1:10017, 127.0.0.1:10018 or remote riak db address
     */
    private static final String RIAK_SERVERS = "riak.servers";
    /**
     * Setup cluster.
     *
     * @return RiakCluster RiakCluster
     * @throws UnknownHostException
     *             UnknownHostException
     */
    public RiakCluster setUpCluster() throws UnknownHostException {
        if (cluster == null) {
             PropertyConfig cfg = PropertyConfig.getInstance();
             String riakServersDetails = cfg.get(RIAK_SERVERS);
             String[] riakServerArray = riakServersDetails.split(",");
             synchronized (RiakConfig.class) {
                List<RiakNode> riakNodeList = new ArrayList<RiakNode>();
                for (final String riakServer : riakServerArray) {
                    RiakNode node = new RiakNode.Builder()
                        .withRemoteAddress(riakServer.split(":")[0])
                        .withRemotePort(Integer.parseInt(riakServer.split(":")[1]))
                        .build();
                    riakNodeList.add(node);
                }

                // This cluster object takes our one node as an argument
                cluster = new RiakCluster.Builder(riakNodeList).build();

                // The cluster must be started to work, otherwise you will see
                // errors
                cluster.start();

            }
        }
        return cluster;
    }
    public static RiakCluster getCluster() {
        return cluster;
    }
    public static void setCluster(final RiakCluster cluster) {
        RiakConfig.cluster = cluster;
    }
}

RiakClientFactory Class




import java.net.UnknownHostException;

import com.basho.riak.client.api.RiakClient;
import com.basho.riak.client.core.RiakCluster;
/**
 * @author Shipra Garg
 */
public class RiakClientFactory {
     private RiakCluster riakCluster;
     /**
      * @param riakConfig is an object of RiakConfig type.
      */
     public RiakClientFactory(final RiakConfig riakConfig) {
          try {
               riakCluster = riakConfig.setUpCluster();
          } catch (UnknownHostException e) {
               throw new RuntimeException();
          }
     }

     public RiakClient getRiakClient() {
          return new RiakClient(riakCluster);
     }

     public RiakCluster getRiakCluster() {
          return riakCluster;
     }
}

You can get the RiakCluster Or RiakClient Object diectly from RiakClientFactory class. It will instantiate the RiakCluster on  object creation of RiakClientFactory.

RiakClient Creation

RiakClient riakClient = new RiakClient(riakCluster);

Method to check Riak Connection
@Override
public boolean checkRiakConnection()
throws Exception {
RiakClient riakClient = riakClientFactory.getRiakClient();
if (riakClient != null) {
return true;
}
return false;


Stay tuned for more Riak tutorials.





 

htop Command to monitor processes on Linux

htop Command to monitor processes on Linux

htop is used to monitor system processes on Linux. It will give list of all process running on the system, with current cpu usage and memory. It's very good utility to keep tab on processes, which takes more CPU and memory.

To Install htop on Linux, command is:-

sudo apt-get install htop

After htop is installed, Just type htop in command prompt to launch it, you will see below text based graph in terminal.

 



Press Ctrl + c to exit from terminal.