Wednesday, 23 January 2013

SVN Daily on Linux

[1] adding files to svn:ignore

prayag@Prayag:~/workspace_java/sbtc$ svn propedit svn:ignore .





[F3] to save
[Ctrl+X] to quit
Set new value for property 'svn:ignore' on '.'

[2] list all ignored patterns

prayag@Prayag:~/workspace_java/sbtc$ svn propget svn:ignore
target
c:
ctbs
catalina.base_IS_UNDEFINED

OR
prayag@Prayag:~/workspace_java/sbtc$ svn pg -R svn:ignore .
. - target
c:
ctbs
catalina.base_IS_UNDEFINED



[3] display the unversioned files
prayag@Prayag:~/workspace_java/sbtc$ svn status | grep -e ^?
OR
prayag@Prayag:~/workspace_java/sbtc$ svn status | grep "^?"


[4] recent log entries

prayag@Prayag:~/workspace_java/sbtc$ svn log -r {2012-12-26}:{2013-01-23}
Authentication realm: <http://10.13.212.254:80> Subversion Repositories
Password for 'pupd':

-----------------------------------------------------------------------
ATTENTION!  Your password for authentication realm:

   <http://10.13.212.24:80> Subversion Repositories

can only be stored to disk unencrypted!  You are advised to configure
your system so that Subversion can store passwords encrypted, if
possible.  See the documentation for details.

You can avoid future appearances of this warning by setting the value
of the 'store-plaintext-passwords' option to either 'yes' or 'no' in
'/home/prayag/.subversion/servers'.
-----------------------------------------------------------------------
Store password unencrypted (yes/no)? no
------------------------------------------------------------------------
r40557 | pupd | 2012-12-26 15:06:54 +0545 (Wed, 26 Dec 2012) | 1 line

UPDATE ETransaction to RemitDetails and UPDATE UI
------------------------------------------------------------------------

OR
prayag@Prayag:~/workspace_java/central-tb-system$ svn log --limit 3


[5] comparing diff between incoming changes

prayag@Prayag:~/workspace_java/sbtc$ svn diff -r BASE:HEAD

Index: src/main/resources/WEB-INF/resourcebundle/message.properties

===================================================================
--- src/main/resources/WEB-INF/resourcebundle/message.properties (working copy)
+++ src/main/resources/WEB-INF/resourcebundle/message.properties (revision 42213)
@@ -5,9 +5,8 @@
 serviceNotSupported=Service {0} is not supported.
 currencyNotSupported=Currency {0} is not supported.
 requestAcknowleged=Request Acknowledged.
+amount.isless=Amount less than minimum limit
+amount.isgreater=Amount more than minimum limit
 invalidServiceInfo=Invalid Service Info ID {0}.
 internalError=Internal Error
 invalidAmount=Amount is missing




Wednesday, 16 January 2013

RAM usage in Linux

1 . using command top
prayag@Prayag:~$ top | grep java
 PID USER      PR  NI  VIRT  RES  SHR S  %CPU %MEM    TIME+  COMMAND
 4216 prayag    20   0  429m 139m 8808 S   1.0  7.9   1:04.51 java                                                                                     
 4216 prayag    20   0  429m 139m 8808 S   0.3  7.9   1:04.52 java                                                                                     
 4216 prayag    20   0  429m 139m 8808 S   0.7  7.9   1:04.54 java                                                                                     
 4216 prayag    20   0  429m 139m 8808 S   0.3  7.9   1:04.55 java                                                                                     
 4216 prayag    20   0  429m 139m 8808 S   0.7  7.9   1:04.57 java

 6035 prayag    20   0  406m 107m 7812 S   1.0  6.1   1:02.63 java                                                                                    
 7249 prayag    20   0  857m 223m 7436 S   0.3 12.8   0:28.04 java                                                                                    
 7249 prayag    20   0  857m 223m 7436 S   0.3 12.8   0:28.05 java

#type h for help, q to quit

RES   = RESident Size (how much of your program is in actual, physical RAM.)
VIRT = VIRTual Memory Footprint (how much virtual memory, RAM + Swap, your program has allocated).
In my case, Java eats 429 MB - 857 MB.

2. free --help
prayag@Prayag:~$ free --mega
             total       used       free     shared    buffers     cached
Mem:          1754       1563        190          0          8        301
-/+ buffers/cache:       1253        500
Swap:         2859        867       1992


3. vmstat --help
prayag@Prayag:~$ vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa
 1  0 887604 192400   8996 310300   77  111   311   181  211 1096  8  2 82  8


References
Tomcat VIRT memory usage. 2012. http://serverfault.com/a/296721/113713

Why (on Linux) am I seeing so much RAM usage?. 2013.
http://www.chrisjohnston.org/ubuntu/why-on-linux-am-i-seeing-so-much-ram-usage


Monday, 14 January 2013

clojure REST API 101

Compojure by James Reeves(a British developer) is a small routing library for Ring that allows web applications to be composed of small, independent parts. (Compojure Github, 2012)

Ring is a Clojure web applications library inspired by Python's WSGI and Ruby's Rack.

                                                                    Fig. a Rack app

Why Compojure?
The language I work with from day to day is Ruby, so Compojure has a  lot in common with lightweight Ruby frameworks like Sinatra, and less  with existing Java frameworks. It's designed for impatient, lazy  people like myself, so it's quick to install and quick to start  developing in.

- @James Reeves (on Google Group)



After installing lein, the following are the steps to get started with Clojure on Compojure :

STEP 1 install lein-newnew 0.2.6 and compojure/lein-template 0.2.0 

prayag@Prayag:~$ lein plugin install lein-newnew 0.2.6
[INFO] Unable to find resource 'lein-newnew:lein-newnew:jar:0.2.6' in repository central (http://repo1.maven.org/maven2)
Downloading: stencil/stencil/0.2.0/stencil-0.2.0.pom from repository central at http://repo1.maven.org/maven2
Unable to locate resource in repository
[INFO] Unable to find resource 'stencil:stencil:pom:0.2.0' in repository central (http://repo1.maven.org/maven2)
Downloading: stencil/stencil/0.2.0/stencil-0.2.0.pom from repository clojars at http://clojars.org/repo/
Transferring 2K from clojars
Downloading: org/clojure/clojure/1.3.0/clojure-1.3.0.pom from repository clojars at http://clojars.org/repo/
Transferring 5K from clojars
Downloading: slingshot/slingshot/0.8.0/slingshot-0.8.0.pom from repository clojars at http://clojars.org/repo/
Transferring 1K from clojars
Downloading: org/clojure/clojure/1.2.1/clojure-1.2.1.pom from repository clojars at http://clojars.org/repo/
Transferring 1K from clojars
Downloading: bultitude/bultitude/0.1.5/bultitude-0.1.5.pom from repository central at http://repo1.maven.org/maven2
Unable to locate resource in repository
[INFO] Unable to find resource 'bultitude:bultitude:pom:0.1.5' in repository central (http://repo1.maven.org/maven2)
Downloading: bultitude/bultitude/0.1.5/bultitude-0.1.5.pom from repository clojars at http://clojars.org/repo/
Transferring 2K from clojars
Downloading: slingshot/slingshot/0.8.0/slingshot-0.8.0.jar from repository clojars at http://clojars.org/repo/
Downloading: bultitude/bultitude/0.1.5/bultitude-0.1.5.jar from repository central at http://repo1.maven.org/maven2
Downloading: stencil/stencil/0.2.0/stencil-0.2.0.jar from repository central at http://repo1.maven.org/maven2
Downloading: org/clojure/clojure/1.3.0/clojure-1.3.0.jar from repository clojars at http://clojars.org/repo/
Transferring 6K from clojars
Unable to locate resource in repository
[INFO] Unable to find resource 'bultitude:bultitude:jar:0.1.5' in repository central (http://repo1.maven.org/maven2)
Downloading: bultitude/bultitude/0.1.5/bultitude-0.1.5.jar from repository clojars at http://clojars.org/repo/
Unable to locate resource in repository
[INFO] Unable to find resource 'stencil:stencil:jar:0.2.0' in repository central (http://repo1.maven.org/maven2)
Downloading: stencil/stencil/0.2.0/stencil-0.2.0.jar from repository clojars at http://clojars.org/repo/
Transferring 3K from clojars
Transferring 16K from clojars
Transferring 3311K from clojars
Copying 3 files to /tmp/lein-6b1bc0ca-676e-4487-810f-c017583b8a43/lib
Including lein-newnew-0.2.6.jar
Including stencil-0.2.0.jar
Including bultitude-0.1.5.jar
Including slingshot-0.8.0.jar
Created lein-newnew-0.2.6.jar


prayag@Prayag:~$ lein plugin install compojure/lein-template 0.2.0
[INFO] Unable to find resource 'compojure:lein-template:jar:0.2.0' in repository central (http://repo1.maven.org/maven2)
Downloading: leinjacker/leinjacker/0.2.0/leinjacker-0.2.0.pom from repository central at http://repo1.maven.org/maven2
Unable to locate resource in repository
[INFO] Unable to find resource 'leinjacker:leinjacker:pom:0.2.0' in repository central (http://repo1.maven.org/maven2)
Downloading: leinjacker/leinjacker/0.2.0/leinjacker-0.2.0.pom from repository clojars at http://clojars.org/repo/
Transferring 3K from clojars
Downloading: trammel/trammel/0.7.0/trammel-0.7.0.pom from repository clojars at http://clojars.org/repo/
Transferring 2K from clojars
[INFO] snapshot thneed:thneed:1.0.0-SNAPSHOT: checking for updates from clojars
[INFO] snapshot thneed:thneed:1.0.0-SNAPSHOT: checking for updates from central
Downloading: thneed/thneed/1.0.0-SNAPSHOT/thneed-1.0.0-20120330.013054-1.pom from repository clojars at http://clojars.org/repo/
Transferring 1K from clojars
Downloading: leinjacker/leinjacker/0.2.0/leinjacker-0.2.0.jar from repository central at http://repo1.maven.org/maven2
Downloading: thneed/thneed/1.0.0-SNAPSHOT/thneed-1.0.0-20120330.013054-1.jar from repository clojars at http://clojars.org/repo/
Downloading: trammel/trammel/0.7.0/trammel-0.7.0.jar from repository clojars at http://clojars.org/repo/
Transferring 6K from clojars
Unable to locate resource in repository
[INFO] Unable to find resource 'leinjacker:leinjacker:jar:0.2.0' in repository central (http://repo1.maven.org/maven2)
Downloading: leinjacker/leinjacker/0.2.0/leinjacker-0.2.0.jar from repository clojars at http://clojars.org/repo/
Transferring 9K from clojars
Transferring 5K from clojars
Copying 3 files to /tmp/lein-77d9ecd7-c721-46ab-8760-5a5c9c2803df/lib
Including lein-template-0.2.0.jar
Including thneed-1.0.0-20120330.013054-1.jar
Including trammel-0.7.0.jar
Including leinjacker-0.2.0.jar
Created compojure-lein-template-0.2.0.jar


STEP 2 create a new project using the "compojure" template

prayag@Prayag:~$ sudo chmod 777 workspace_erujopmoc/
prayag@Prayag:~$ cd workspace_erujopmoc/
prayag@Prayag:~/workspace_erujopmoc$ lein new compojure hotel


STEP 3 Make sure project is created

prayag@Prayag:~/workspace_erujopmoc/hotel$ ls -la
total 44
drwxrwxr-x 7 prayag prayag 4096 Jan 14 18:09 .
drwxrwxrwx 3 root   root   4096 Jan 14 18:01 ..
drwxrwxr-x 2 prayag prayag 4096 Jan 14 18:09 classes
-rw-rw-r-- 1 prayag prayag  109 Jan 14 18:01 .gitignore
-rw-rw-r-- 1 prayag prayag   40 Jan 14 18:09 .lein-deps-sum
drwxrwxr-x 2 prayag prayag 4096 Jan 14 18:07 .lein-plugins
drwxrwxr-x 3 prayag prayag 4096 Jan 14 18:09 lib
-rw-rw-r-- 1 prayag prayag  309 Jan 14 18:01 project.clj
-rw-rw-r-- 1 prayag prayag  256 Jan 14 18:01 README.md
drwxrwxr-x 3 prayag prayag 4096 Jan 14 18:01 src
drwxrwxr-x 3 prayag prayag 4096 Jan 14 18:01 test


3.A) The dependencies are : 

(defproject hotel "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :dependencies [[org.clojure/clojure "1.4.0"]
                 [compojure "1.1.1"]]
  :plugins [[lein-ring "0.7.1"]]
  :ring {:handler hotel.handler/app}
  :dev-dependencies [[ring-mock "0.1.2"]])


3.B) The structure of main is : 

prayag@Prayag:~/workspace_erujopmoc/hotel$ ls -l src/hotel/
total 4
-rw-rw-r-- 1 prayag prayag 252 Jan 14 18:01 handler.clj

The src/hotel/handler.clj reads as follws :

(ns hotel.handler
  (:use compojure.core)
  (:require [compojure.handler :as handler]
            [compojure.route :as route]))

(defroutes app-routes
  (GET "/" [] "Hello World")
  (route/not-found "Not Found"))

(def app
  (handler/site app-routes))

Change (defroutes) to the following

(defroutes app-routes
  (GET "/" [] "Welcome To Rich Hickey Hotel")
  (route/not-found "Not Found"))


STEP 4 start a development server using Leiningen

prayag@Prayag:~/workspace_erujopmoc/hotel$ lein ring server
Downloading: lein-ring/lein-ring/0.7.1/lein-ring-0.7.1.pom from repository central at http://repo1.maven.org/maven2
Unable to locate resource in repository
[INFO] Unable to find resource 'lein-ring:lein-ring:pom:0.7.1' in repository central (http://repo1.maven.org/maven2)
Downloading: lein-ring/lein-ring/0.7.1/lein-ring-0.7.1.pom from repository clojars at http://clojars.org/repo/
Transferring 2K from clojars
Downloading: org/clojure/data.xml/0.0.3/data.xml-0.0.3.pom from repository clojars at http://clojars.org/repo/
Unable to locate resource in repository
[INFO] Unable to find resource 'org.clojure:data.xml:pom:0.0.3' in repository clojars (http://clojars.org/repo/)
Downloading: org/clojure/data.xml/0.0.3/data.xml-0.0.3.pom from repository central at http://repo1.maven.org/maven2
Transferring 1K from central
Downloading: org/clojure/pom.contrib/0.0.25/pom.contrib-0.0.25.pom from repository sonatype-oss-snapshots at https://oss.sonatype.org/content/repositories/snapshots
Unable to locate resource in repository
[INFO] Unable to find resource 'org.clojure:pom.contrib:pom:0.0.25' in repository sonatype-oss-snapshots (https://oss.sonatype.org/content/repositories/snapshots)
Downloading: org/clojure/pom.contrib/0.0.25/pom.contrib-0.0.25.pom from repository clojars at http://clojars.org/repo/
Unable to locate resource in repository
[INFO] Unable to find resource 'org.clojure:pom.contrib:pom:0.0.25' in repository clojars (http://clojars.org/repo/)
Downloading: org/clojure/pom.contrib/0.0.25/pom.contrib-0.0.25.pom from repository central at http://repo1.maven.org/maven2
Transferring 5K from central
Downloading: org/clojure/data.xml/0.0.3/data.xml-0.0.3.jar from repository clojars at http://clojars.org/repo/
Downloading: lein-ring/lein-ring/0.7.1/lein-ring-0.7.1.jar from repository central at http://repo1.maven.org/maven2
Unable to locate resource in repository
[INFO] Unable to find resource 'lein-ring:lein-ring:jar:0.7.1' in repository central (http://repo1.maven.org/maven2)
Downloading: lein-ring/lein-ring/0.7.1/lein-ring-0.7.1.jar from repository clojars at http://clojars.org/repo/
[...]
Copying 1 file to /home/prayag/workspace_erujopmoc/hotel/lib/dev
2013-01-14 18:09:24.142:INFO:oejs.Server:jetty-7.6.1.v20120215
Started server on port 3000
2013-01-14 18:09:24.218:INFO:oejs.AbstractConnector:Started SelectChannelConnector@0.0.0.0:3000


STEP 5 Hit http://localhost:3000/ and scream 

 Welcome To Rich Hickey Hotel



More On Compojure
[1] What's the “big idea” behind compojure routes?, http://stackoverflow.com/a/3490479/432903
https://docs.google.com/document/edit?id=1kKxwuPW0bkzxmbOvyMyJxfZNp71aMz4FifcVZclpTQY

[2] Is Clojure just "a Lisp syntax on the JVM"? Or is it more than that?, http://qr.ae/Ifo36

References
[1] Getting Started, https://github.com/weavejester/compojure/wiki/Getting-Started

[2] Installing Compojure from a clean Ubuntu, http://sharetheconversation.blogspot.com/2010/05/installing-compojure.html

[3] Using Leiningen to build Clojure code, http://alexott.net/en/clojure/ClojureLein.html#sec2

[4] Compojure Demystified with an example,
http://techbehindtech.com/2010/08/24/compojure-demystified-with-an-example-part-4/

Wednesday, 9 January 2013

clojure and lein REPL 101

I'm interested in clojure a bit because of its functional approach borrowed from LISP. Its a constant learning obviously. Im writing about basic 101 stuffs here.

 clojure has a build tool leiningen is a that helps bootstrapping.

It installs its dependencies upon the first run on unix, so the first run will take longer.

STEP 1 : download lein and add to path
$ wget https://raw.github.com/technomancy/leiningen/stable/bin/lein > /usr/bin/lein
$ sudo chmod +x /usr/bin/lein

My PATH reads as given below :
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/java/jdk1.6.0_23/bin


STEP 2 : start REPLa simple Interactive prog env
REPL helps while learning a new programming lang, for example the groovy, scala also have implementation of REPL.

using lein repl

$ lein repl
REPL started; server listening on localhost port 63090
user=> (println *clojure-version*)
{:major 1, :minor 2, :incremental 1, :qualifier }
nil
user=> (+ 1 3)
4

user=> (println "Thread: " (.getName (Thread/currentThread)))
Thread:  nREPL-worker-0
nil

user=> (clojure-version)
"1.2.1"
user=> (inc 1)
2
user=>

I can connect to above REPL from another machine as below
$ lein repl :connect 50924
Connecting to nREPL at 127.0.0.1:50924
REPL-y 0.3.7, nREPL 0.2.12
Clojure 1.8.0

inc returns a number one greater than num.

STEP 3 clojure collections

clojure.lang.PersistentVector

user=> (class ["order-1", "order-2"])
clojure.lang.PersistentVector

user=> (count ["order-1", "order-2", "order-3"])
3

user=> (reverse ["prayagupd", "ocean", "seattle"])
("seattle" "ocean" "prayagupd")

;; access

user=> (get ["cancer" "diabetes" "anxiety"] 1)
"diabetes"

user=> (assoc ["cancer" "diabetes" "anxiety"] 2 "social anxiety")
["cancer" "diabetes" "social anxiety"]

;; map
user=> (map #(+ % 3) [1, 2, 3]);
(4 5 6)

user=> (mapv #(str "Mental condition - " % "!" ) ["Social anxiety" "dysthymia" "PTSD"])
["Mental condition - Social anxiety!" "Mental condition - dysthymia!" "Mental con

user=> (partition 2 1 [1 2 3 4])
((1 2) (2 3) (3 4))

user=> (interleave [1 2 3] ["Social anxiety" "dysthymia" "PTSD"])
(1 "Social anxiety" 2 "dysthymia" 3 "PTSD")

Seq/ lazy Seq


user=> (map #(str "ordered sku is " %) ["sku-1" "sku-2" "sku-3"]) 
("ordered sku is sku-1" "ordered sku is sku-2" "ordered sku is sku-3")

user=> (class (map #(str "ordered sku is " %) ["sku-1" "sku-2" "sku-3"]))
clojure.lang.LazySeq


user=> (doseq [order ["order-1", "order-2", "order-3"]] 
  #_=> (println (str order " is about to be supplied.")))
order-1 is about to be supplied.
order-2 is about to be supplied.
order-3 is about to be supplied.
nil

hash-set

user=> (hash-set "social anxiety" "PTSD" "dysthymia")
#{"PTSD" "social anxiety" "dysthymia"}

user=> (conj (hash-set "social anxiety" "PTSD" "dysthymia") "depression")
#{"depression" "PTSD" "social anxiety" "dysthymia"}

user=> (disj (hash-set "social anxiety" "PTSD" "dysthymia") "PTSD")
#{"social anxiety" "dysthymia"}

user=> (contains? (hash-set "social anxiety" "PTSD" "dysthymia") "PTSD")
true

hash-map

user=> (def purchaseorder (hash-map :order-number 1234,
  #_=>     :scheduled-date 20171028,
  #_=>     :fulfillment-centre "Iowa Fulfillment Centre ",
  #_=>     :supplier "Native USA",
  #_=>     :order-skus (list "sku1" "sku2" "sku3")))
#'user/purchaseorder

user=> (:order-number purchaseorder)
1234

user=> (some #(= % "sku1") (:order-skus purchaseorder))
true

user=> (type purchaseorder)
clojure.lang.PersistentHashMap

user=> (class purchaseorder)
clojure.lang.PersistentHashMap

fp = Var + fn

user=> (defn receive [item] (println (str item " is received")))
#'user/receive

user=> (defn scan [item] (println (str item " is scanned")))
#'user/scan

user=> (defn update-inventory [item] (println (str "inventory for " item " is updated")))
#'user/update-inventory

user=> (def receiving (comp update-inventory scan receive))
#'user/receiving

user=> (receiving "item1")
item1 is received
 is scanned
inventory for  is updated
nil

References
[1] http://leiningen.org/#install

[2] How to install Clojure on Ubuntu 10.04 from Github repo with no clojure.jar, http://stackoverflow.com/a/5984183/432903

[3] http://clojure.org/getting_started

[4] Using Leiningen to build Clojure code, http://alexott.net/en/clojure/ClojureLein.html#sec2

[5] Striving to Make Things Simple and Fast - Phil Bagwell, http://www.youtube.com/watch?v=K2NYwP90bNs

[6] http://www.se-radio.net/2010/03/episode-158-rich-hickey-on-clojure/