Friday, July 14, 2017

NoSQL Data Modelling

NoSQL Data Modelling has two choices viz. Embedding (non-normalized) vs Referencing (normalized).

Aspects to choose:
  • 1:1 relationship => Prefer Embedding
  • 1-to-Many (small & bounded) => Prefer Embedding
  • 1-to-Many (unbounded) => Prefer Referencing
  • Volatility (frequently changing sub-documents) => Prefer Referencing
  • Immense read-speeds needed => Prefer Embedding

Sunday, April 3, 2016

Summarized Hashing Fundamentals

Need of Hashing: Searching is typically the most frequent data operation. Linear Search on an unsorted array has O(n) while Binary Search on a sorted array has O(log n) time complexities. Hash Search further optimizes it to have a O(1) asymptotic time complexity.

Hash Function: A function that ensures uniform distribution of hash values & least collisions.

Collision: Collision happens when multiple keys hash to the same bucket.

Collision Avoidance Techniques:
  • Direct Chaining/Separate Chaining: Collided elements are added to Linked list.
  • Open Addressing: Involves Linear or Quadratic Probing.
    • Linear Probing: [( H(x) + f(i) ) mod ArrLen] preferred in simple scenarios.
    • Quadratic Probing: [( H(x) + f(i^2) ) mod ArrLen] preferred in simple scenarios.
  • Closed Addressing: Involves Double Hashing.
    • Double Hashing: [( H1(x) + (i * H2(x) ) ) mod ArrLen] where H2(x) = [PrimeNum – x mod PrimeNum] is for complex scenarios.
Collision Avoidance Tips:
  • Use separate bucket for null key (special case).
  • Avoid clustering of values one besides other.
  • Choose twice array size than the values to be inserted.
  • Choose Prime Number (smaller than table size) as the initial size of data-structure.
  • For double-hashing, ensure that second Hash function (H2(x)) shouldn't evaluate to zero & should probe all locations.
Collision Avoidance Example: Following example is the actual HashMap implementation code.

 public V put(K key, V value) {  
   if (key == null)  
     return putForNullKey(value); // Used separate bucket for null-key.  
   int hash = hash(key.hashCode()); // Called Hash-function to get hash-value.  
   int i = indexFor(hash, table.length);  
   for (Entry e = table[i]; e != null; e = e.next) {  
     Object k;  
     if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {  
       V oldValue = e.value;  
       e.value = value;  
       e.recordAccess(this);  
       return oldValue;  
     }  
   }  
   modCount++;  
   addEntry(hash, key, value, i); // Stored element using above hash-value.  
   return null;  
 }  

Saturday, March 26, 2016

Tips for approaching Algorithmic problems

  1. Exemplify (Investigation): Understand problem by creating minimum two examples of the algorithm. Use this step to create test-inputs.
  2. Algo Pattern: Identify the similar algorithms (or patterns of algorithms) that you've encountered previously. Use this step to identify best applicable data-structure. 
  3. Data Structure Brainstorm: This is hit and trial method. Try fitting relevant data structure till you find the best match. There are far better techniques for choosing it as well (watch this space and I shall update it shortly).
  4. Simplify and Generalize (Divide and Conquer): Break-down the big problem into smaller fragments. Use this step to modularize. 
  5. Base case and Build: Identify base case (eg. just one element case) for algorithm behavior and start from there. 
  6. Algo Features: Keep a check on sequence, decision, repetition and Asymptotic Complexity (Big O notation)

JVM Architecture




Sunday, May 31, 2015

Log4j configuration for JUnit log-statements to be displayed in Eclipse Console.

Problem Statement: In Eclipse Console while running the JUnit you are getting following error:

log4j:WARN Please initialize the log4j system properly

Diagnosis: Log4J needs to be configured for this logging to work properly. Most likely the log4j.properties (or log4j.xml) file isn't in the root of your test classpath.

Solution:
1. Open "Run Configurations" dialog (click the "Run" menu and go to "Run Configurations")
2. Go to the "Classpath" tab
3. Select the "User Entries" and click the "Advanced" button on the right side.
4. Now select the "Add folder" radio button.
5. Select the "test/resources" folder


Thursday, May 28, 2015

Batch Script: Deploy to local Tomcat.

 :: deploy.bat  
 ::  
 :: Runs maven build commands on project folder,  
 :: copies generated binary to application server,  
 :: rename the binary to simpler name and  
 :: re-starts the application server.  
 ::  
 :: @author Ameya Aloni  
 ::  
 @ECHO OFF  
 @REM Declare the variables  
 SET project.home=<C:\path\to\project\directory>  
 SET app.server.home=<C:\path\to\application\server\home>  
 SET app.server.port=8282  
 SET app.context=<applicationcontext>  
 SET working.directory=%CD%  
 @REM Echoes information:  
 ECHO %project.home%  
 ECHO %app.server.home%  
 ECHO %app.server.port%  
 ECHO %app.context%  
 ECHO %working.directory%  
 ECHO.  
 CLS  
 CD %project.home%  
 ECHO Generating the war file...  
 CALL mvn clean package -Dmaven.test.skip=true  
 ECHO.  
 @REM Read war file name  
 FOR %%F IN (%project.home%\target\*.war) DO (  
   SET project.war.file.name=%%F  
   ECHO Found war file: %project.war.file.name%  
   GOTO rename  
 )  
 IF NOT EXIST %project.home%\target\*.war (  
   ECHO *** Aborting deployment as build has failed. ***  
   GOTO eof  
 )  
 :rename  
 SET renamed.project.war.file.name=%project.home%\target\%app.context%.war  
 ECHO Renaming war file: %project.war.file.name% to %renamed.project.war.file.name%  
 MOVE %project.war.file.name% %renamed.project.war.file.name%  
 ECHO.  
 @REM Cleanup existing app deployment.  
 IF NOT EXIST %app.server.home%\webapps\%app.context%.war (  
   GOTO deleteFolder  
 )  
 ECHO Removing war file: %app.server.home%\webapps\%app.context%.war  
 DEL %app.server.home%\webapps\%app.context%.war  
 :deleteFolder  
 IF NOT EXIST %app.server.home%\webapps\%app.context% (  
   GOTO copyWarFile  
 )  
 ECHO Removing war folder: %app.server.home%\webapps\%app.context%  
 RD /S /Q %app.server.home%\webapps\%app.context%  
 ECHO.  
 :copyWarFile  
 ECHO Copying war file: %renamed.project.war.file.name% to %app.server.home%\webapps  
 COPY %renamed.project.war.file.name% %app.server.home%\webapps  
 ECHO.  
 CD %app.server.home%\bin  
 ECHO Restarting application server...  
 NETSTAT -na | find "LISTENING" | find /C /I ":%app.server.port%" > NUL  
 IF ERRORLEVEL 1 (  
   ECHO Application server is stopped. Starting new instance...  
   ECHO.  
 ) ELSE (  
   ECHO Application server is running. Stopping existing instance...  
   CALL %app.server.home%\bin\shutdown.bat  
   ECHO.  
 )  
 CALL %app.server.home%\bin\startup.bat  
 :eof  
 @REM Back to working directory is:  
 CD %working.directory%  

Tuesday, August 5, 2014

Java Control Panel Issue

Java Control Panel for Java 6 64-bit or below looks like:

My machine had Java 6 64-bit as default running on Windows 7 64-bit. So when the Java Control Panel is opened from Control Panel -> Programs -> Java; it looks like above.
Java Control Panel for Java 7 32-bit looks like:

But I also had Java 7 which browser plugins were using and I wanted to Security Exception to the Site Lists and I was expecting above interface for Security Tab. I later figured out that it was because of multiple Java versions. Eventually I found that my 64-bit Java was installed in "Program Files" while 32-bit Java was installed in "Program Files (x86)". My browser being 32-bit was using 32-bit Java. So, I opened above Control Panel by running "Program Files (x86) -> java -> jre7 -> bin -> javacpl.exe" file.