Memoization

Memoization is the technique of caching repeated calculations thereby achieving a significant increase in the speed of computation.

I will take an example in illustrating this:

Here is the code to calculate fibonacci number at a given position which uses the concept of recursion:

private static BigInteger fibonacciNormal(long n) {
if (n <= 0) {
return BigInteger.ZERO;
}
if (n == 1 || n == 2) {
return BigInteger.ONE;
}
return fibonacciNormal(n-1).add(fibonacciNormal(n-2));
}

The problem with the above code is that it becomes too slow as you provide a bigger value of input n. The reason is that there are repeated calculations being done which can be avoided by caching them.
The following code used the concept of memoization by avoiding the repeated calculations by caching the results.

private static BigInteger fibonacci(long n) {
if (n <= 0) {
return BigInteger.ZERO;
}
if (n == 1 || n == 2) {
return BigInteger.ONE;
}
// See if we already computed fibonacci(n)
BigInteger result = fibonacciCache.get(n);
if (result == null) {
result = fibonacci(n-1).add(fibonacci(n-2));
// Update the cache
fibonacciCache.put(n, result);
}
return result;
}

Advertisements
Posted in Java, Uncategorized | Leave a comment

Event Broker Using Kafka

I have been working on Event broker application. We process messages in real time using Kafka brokers. Kafka is a distributed publish subscribe messaging service. Kafka helped us achieve scalability, fault tolerance and high throughput of more than 1 million messages per second. We selected Kafka Streams to process the messages that are stored in kafka. Kafka streams reads the message in the source topic, does processing and then writes the message into sink topic. We have a jdbc consumer which reads the data from the sink topic and then ingests it into the database.

Posted in Kafka, Uncategorized | Leave a comment

Proxy settings for maven

All of a sudden my maven commands started giving errors. I realized that maven was not able to connect to central repository.

I made the following changes to /conf/settings.xml and then it started working fine:

<proxy>
<id>optional</id>
<active>true</active>
<protocol>http</protocol>
<username>proxyuser</username>
<password>proxypass</password>
<host>web-proxy.corp.hpecorp.net</host>
<port>8080</port>
<!–<nonProxyHosts>web-proxy.corp.hpecorp.net</nonProxyHosts>–>
</proxy>

Posted in Maven | Leave a comment

Handling clob variables in plsql

If you are storing more than 4K characters in the database or more than 32K characters in pl/sql, then you are advised to use clob datatype instead of varchar2.

These functions will be very helpful in handling clob variables:

create or replace function CLOB_TO_BLOB (p_clob CLOB) return BLOB
as
l_blob blob;
l_dest_offset integer := 1;
l_source_offset integer := 1;
l_lang_context integer := DBMS_LOB.DEFAULT_LANG_CTX;
l_warning integer := DBMS_LOB.WARN_INCONVERTIBLE_CHAR;
BEGIN

DBMS_LOB.CREATETEMPORARY(l_blob, TRUE);
DBMS_LOB.CONVERTTOBLOB
(
dest_lob =>l_blob,
src_clob =>p_clob,
amount =>DBMS_LOB.LOBMAXSIZE,
dest_offset =>l_dest_offset,
src_offset =>l_source_offset,
blob_csid =>DBMS_LOB.DEFAULT_CSID,
lang_context=>l_lang_context,
warning =>l_warning
);
return l_blob;
END;

create or replace function blob_to_clob(p_blob blob) return CLOB as
l_clob clob;
l_dest_offsset integer := 1;
l_src_offsset integer := 1;
l_lang_context integer := dbms_lob.default_lang_ctx;
l_warning integer;

BEGIN

if p_blob is null then
return null;
end if;

dbms_lob.createTemporary(lob_loc => l_clob
,cache => false);

dbms_lob.converttoclob(dest_lob => l_clob
,src_blob => p_blob
,amount => dbms_lob.lobmaxsize
,dest_offset => l_dest_offsset
,src_offset => l_src_offsset
,blob_csid => dbms_lob.default_csid
,lang_context => l_lang_context
,warning => l_warning);

return l_clob;

END;

Posted in Database | Leave a comment

Rendering a flex widget in Extjs

In the head section include the following:

    extjs/ext-all.js in the script tag with type=”text/javascript” attribute

    swfobject/swfobject.js in the script tag with type=”text/javascript” attribute

    extjs/resources/css/ext-all.css in the link tag with rel=”stylesheet” type=”text/css” attributes

Ext.onReady(function () {
var win = Ext.widget(‘window’, {
title: “Inspections Widget!”,
layout: ‘fit’,
width: 700,
height: 500,
resizable: true,
items: {
xtype: ‘flash’,
url: ‘http://somedomain.com/someflashfile.swf&#8217;,
flashVars: {locale:’en’,dealerCode:’XTIMEMOTORS’,webKey:’xtimemotors’,make:’HYUNDAI’,vin:’5NPEU46FX6H146379′,day:’01/20/2015′,customerName:’DABROWSKI, AMY’,model:’SONATA’,year:’2006′}
}
});
win.show();
});

Note: You can download swfobject.js from

References:

Posted in Extjs, Flex | Leave a comment

Search for files containing a text on linux

I found this command to be very useful. Often we would like to search for a file containing a text.

$ find /path-here -type f -exec fgrep -l ‘text-to-find-here’ {} \;

Eg.

$ find /home/venkat -type f -exec fgrep -l ‘foo’ {} \;

Posted in Linux | Leave a comment

Handling non-english characters in .java files

You should save the .java file as “Encode in UTF-8 without BOM” using an editor like Notepad++.
Do not use Notepad. Notepad introduces some special characters because of which the jvm will throw compiling error because of the special characters.

This applies to .properties files also, which will have non-english characters.

Posted in Java | Leave a comment