Saturday, February 23, 2019

Why are prime numbers important in real life? What practical use are prime numbers?

Primes have properties of uniqueness such that multiplying primes together produce unique results, for example multiply 3*7=21 which is a unique method for creating 21.
Uniqueness is a confusing term in mathematics. To me, uniqueness describes a unique path or vector to reach a specific point.
You can exploit that concept using small primes for creating an efficient database. In fact the first program I ever wrote back in the early eighties had used such a method in a phone book program.
I was trying to create a phone book program that could filter records into multiple different groups or multiple different phone books such as coworkers, friends, clients, church group, poker players, etc. One record could exist in multiple groups, but each record should only be stored once such that if the number was to change for that person, it would only require changing the number once in a master database that contained every record.
How this worked was to assign a prime number to each phone book. Records would be assigned to each phone book by taking the factors of those primes.
For example, let’s say that Bob is a friend, a coworker, and a poker player. The phone book for friends had the prime number 2 assigned to it, coworkers was assigned the prime number 3, and poker players had the prime 7 assigned to it. Then Bobs record would have the number 2x3x7 = 42 linked to it’s record.
Then when I decide to have a poker game and look up poker players to invite to the game, it would search the factors linked to all records for numbers evenly divisible by 7 to display only the records of poker players.
I guess the same concept was later used for digital security only applied in a kind of inside out way where only 2 large primes were used and the idea was to create semiprime index that was so large that they became prohibitively unsolvable for finding their primes based on using math due to computers being so dumb at doing arithmetic for large numbers.
They call it a complexity issue, I would call it something else because its not really that complex. It’s just doing something simple lots and lots of times.

Friday, February 8, 2019

git stash - How to Save Your Changes Temporarily

git stash - How to Save Your Changes Temporarily

There are lots of situations where a clean working copy is recommended or even required: when merging branches, when pulling from a remote, or simply when checking out a different branch.
The "git stash" command can help you to (temporarily but safely) store your uncommitted local changes - and leave you with a clean working copy.

git stash: a Clipboard for Your Changes

if you have to switch context - e.g. because you need to work on an urgent bug - you need to get these changes out of the way. You shouldn't just commit them, of course, because it's unfinished work.
This is where "git stash" comes in handy:
our working copy is now clean: all uncommitted local changes have been saved on this kind of "clipboard" that Git's Stash represents. You're ready to start your new task (for example by pulling changes from remote or simply switching branches).

Continuing Where You Left Off

As already mentioned, Git's Stash is meant as a temporary storage. When you're ready to continue where you left off, you can restore the saved state easily:
The "pop" flag will reapply the last saved state and, at the same time, delete its representation on the Stash (in other words: it does the clean-up for you).

Thursday, November 22, 2018

MongoClient with SocketTimeOut

 public static MongoClient getMongoInstance() {
        if (_mongoClient == null) {
            try {
            MongoCredential credential = MongoCredential.createCredential(getMongoUser(), getAuthDatabase(), MONGO_PWD.toCharArray());
                ServerAddress serverAddress = new ServerAddress(getMongoDBServerName(), getMongoDBServerPort());
                MongoClientOptions.Builder options = new MongoClientOptions.Builder();
            _mongoClient = new MongoClient(serverAddress, credential, options.build());
            } catch (Exception e) {
                log.error("MongoDBClient::getMongoInstance Exception --> " + e.getLocalizedMessage());
                log.info("Exception : ", e);
            }
        }

        return _mongoClient;
    }

=====================================================
public static MongoClient getMongoInstance(int timeOut) {
     MongoClient myMongoClient = null;
     if(timeOut==-1) {
         return getMongoInstance();
        }else {
                try {
                 MongoCredential credential = MongoCredential.createCredential(getMongoUser(), getAuthDatabase(), MONGO_PWD.toCharArray());
                 MongoClientOptions.Builder options = new MongoClientOptions.Builder();
                 options.socketTimeout(timeOut).connectTimeout(timeOut).maxWaitTime(timeOut);
                
                    ServerAddress serverAddress = new ServerAddress(getMongoDBServerName(), getMongoDBServerPort());
                    myMongoClient = new MongoClient(serverAddress, Arrays.asList(credential),options.build());
                } catch (Exception e) {
                   log.error("MongoDBClient::getMongoInstance Exception --> " + e.getLocalizedMessage());
                      log.info("Exception : ", e);
                }
        }
        return myMongoClient;
    } 

Stoping MongoTemplate after completion of Operations

Step 1 : 

import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;

public class CustomMongoTemplate extends MongoTemplate {

public CustomMongoTemplate(MongoDbFactory mongoDbFactory) {
super(mongoDbFactory);
}

@Override
protected void finalize() throws Throwable {
this.getDb().getMongo().close();
}
}



Step 2:

  public static MongoClient getMongoInstance() {
        if (_mongoClient == null) {
            try {
            MongoCredential credential = MongoCredential.createCredential(getMongoUser(), getAuthDatabase(), MONGO_PWD.toCharArray());
                ServerAddress serverAddress = new ServerAddress(getMongoDBServerName(), getMongoDBServerPort());
                MongoClientOptions.Builder options = new MongoClientOptions.Builder();
            _mongoClient = new MongoClient(serverAddress, credential, options.build());
            } catch (Exception e) {
                log.error("MongoDBClient::getMongoInstance Exception --> " + e.getLocalizedMessage());
                log.info("Exception : ", e);
            }
        }

        return _mongoClient;
    }
======================================================
 public static MongoClient getMongoInstance(int timeOut) {
    MongoClient myMongoClient = null;
    if(timeOut==-1) {
        return getMongoInstance();
        }else {
                try {
                MongoCredential credential = MongoCredential.createCredential(getMongoUser(), getAuthDatabase(), MONGO_PWD.toCharArray());
                MongoClientOptions.Builder options = new MongoClientOptions.Builder();
                options.socketTimeout(timeOut).connectTimeout(timeOut).maxWaitTime(timeOut);
               
                    ServerAddress serverAddress = new ServerAddress(getMongoDBServerName(), getMongoDBServerPort());
                    myMongoClient = new MongoClient(serverAddress, Arrays.asList(credential),options.build());
                } catch (Exception e) {
                  log.error("MongoDBClient::getMongoInstance Exception --> " + e.getLocalizedMessage());
                      log.info("Exception : ", e);
                }
        }
        return myMongoClient;
    } 
======================================================
public MongoDbFactory mongoDbFactory(String dbName, int timeOut) {
return new SimpleMongoDbFactory(MongoDBClient.getMongoInstance(timeOut), "my_" + dbName);
}
========================================================
public MongoTemplate mongoTemplate(String dbName, int tomeOut) {
if (dbName == null || dbName.isEmpty())
return null;
return new CustomMongoTemplate(mongoDbFactory(dbName.replace(" ", "_"), tomeOut));
}
=====================================================
Step 3 : Usage -->

someClass.mongoTemplate("dbName", 23);




Thursday, November 8, 2018

Track changes in Word

Track changes in Word

Turning on Track Changes gives you and your coworkers a way to make changes that are easy to spot. The changes are like suggestions that you can review, and then remove them or make them permanent.
Turn Track Changes on and off by going to Review > Track Changes.
Track changes
  • When it's turned on, deletions are marked with a strikethrough, and additions are marked with an underline. Different authors' changes are indicated with different colors.
  • When it's turned off, Word stops marking changes, but the colored underlines and strikethrough are still in the document.

View suggested changes

To review the changes in your document, go to Review Tracking Display for Review.

Load JS File dynamically from JSP file.

function loadJSfile(language) {
var fileref = document.createElement('script')
fileref.setAttribute("type", "text/javascript");
var theURL = window.location.href;
var arr = theURL.split("/");
var result = arr[0] + "//" + arr[2] + "/" + arr[3];
var thePath ="/resources/json_messages/messageJson_"+language+".js";
var theMessageFileURL = result + thePath;
$.ajax({
url : theMessageFileURL,
type : 'HEAD',
async : false,
error : function() {
//language message file not exists
loadJSfile("en");
},
success : function() {
//language message file exists
fileref.setAttribute("src", theMessageFileURL)
if (typeof fileref != "undefined")
document.getElementsByTagName("head")[0]
.appendChild(fileref)
}
});
}

loadJSfile("${pageContext.response.locale}"); //dynamically load and add this .js file

Tuesday, October 30, 2018

Stop MongoDB in java

private static void stopMongoDB() throws IOException {
ProcessBuilder pb = new ProcessBuilder("taskkill", "/IM", "mongod.exe").inheritIO();
Process proc = pb.start();
try {
proc.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

calling the above method:

stopMongoDB();

Recent Post

Databricks Delta table merge Example

here's some sample code that demonstrates a merge operation on a Delta table using PySpark:   from pyspark.sql import SparkSession # cre...