Sunday, November 29, 2020

Java Maze Solver

 

Wednesday, November 18, 2020

AVL TREE LEFT RIGHT DOUBLE ROTATIONS, DEPTH FIRST SEARCHs, BREADTH FIRST SEARCH, RECURSIVE and ITERATIVE TRAVERSALS, HEIGHT, DEPTH, SIZE, LEAF, NODE FINDERS, ROOT to LEAF NODE PATHS




SINGLE ROTATION
DOUBLE ROTATION



Suppose the node to be rebalanced is X. 
There are 4 cases that we might have to fix (two are the mirror images of the other two):

  • An insertion in the left subtree of the left child of X,
  • An insertion in the right subtree of the left child of X,
  • An insertion in the left subtree of the right child of X, or
  • An insertion in the right subtree of the right child of X.

Balance is restored by tree rotations.

Case 1 and case 4 are symmetric and requires the same operation for balance. 
Cases 1,4 are handled by single rotation.

Case 2 and case 3 are symmetric and requires the same operation for balance.
Cases 2,3 are handled by double rotation

Sunday, February 9, 2020

REDIS STREAM - CLI

DOCKER
docker run -it --rm --net host --name redis1  redis
docker exec -it redis1 redis-cli

XREADGROUP
XGROUP CREATE mystream mygroup $ MKSTREAM
XADD mystream * message apple
XADD mystream * message orange
XADD mystream * message strawberry

XGROUP CREATE mystream grp1 0
XREADGROUP GROUP grp1 Bob COUNT 2 STREAMS mystream >

XGROUP CREATE mystream grp2 0
XREADGROUP GROUP grp2 Bobiy COUNT 2 STREAMS mystream >

The special ID >. This special ID is only valid in the context of consumer groups, and it means: messages never delivered to other consumers so far.




XREAD
XADD mystream * message newcoronavirus
XADD mystream * message nextvirus

XREAD BLOCK 0 STREAMS mystream $
 The special ID $. This special ID means that XREAD should use as last ID the maximum ID already stored in the stream mystream, so that we will receive only new messages, starting from the time we started listening. This is similar to the tail -f Unix command in some way.


Use ConsumerGroups for FANOUT
Use XREAD for P2P

Sunday, December 2, 2018

Java 8 CompletableFuture parallel tasks and Timeout example


Suppose that you have a list of item (id list referring to a table, url list to fetch page data from internet, customer keys for location query from a map service  etc...). And you will start some parallel tasks  for those ids. But you don't want to wait no longer than your threshold. Also you want to collect the data from the returned "CompletableFutures" which are not timed out.

How can you achieve this while using a CompletableFuture ?

PS: There is no .orTimeout ()  option in Java 8. It is included in Java10.  And other option is using .get(N, TimeUnit.SECONDS) but it will not gave you what you want.



Outputs : 

pool-1-thread-1 - - > will sleep for secs :0
Duration here = 233
pool-1-thread-2 - - > will sleep for secs :1000
pool-1-thread-3 - - > will sleep for secs :2000
pool-1-thread-4 - - > will sleep for secs :3000
pool-1-thread-5 - - > will sleep for secs :4000
pool-1-thread-6 - - > will sleep for secs :5000
Collected Results = [0, 1000, 2000, 3000, 4000, 5000, null, null, null, null]
Total Duration = 5235

How did i come here :) See below code pls
In this example your main thread will be waiting 2 seconds for each uncompleted future.
I guess N*2 seconds waiting is not the thing that you expect to see here
And yes.. There is 20 seconds duration job and it is cancelled You have a minimal gain at that point but ! But if you would have 20 tasks ...The picture would be very nagative again .


Outputs : 

ForkJoinPool.commonPool-worker-1 - - > will sleep : 0
ForkJoinPool.commonPool-worker-5 - - > will sleep : 4
ForkJoinPool.commonPool-worker-3 - - > will sleep : 2
ForkJoinPool.commonPool-worker-4 - - > will sleep : 3
ForkJoinPool.commonPool-worker-7 - - > will sleep : 6
ForkJoinPool.commonPool-worker-1 - - > will sleep : 5
ForkJoinPool.commonPool-worker-2 - - > will sleep : 1
ForkJoinPool.commonPool-worker-6 - - > will sleep : 7
Tasks are created here, duration = 79
Getting results
Getting results
Getting results
ForkJoinPool.commonPool-worker-2 - - > will sleep : 8
Getting results
ForkJoinPool.commonPool-worker-3 - - > will sleep : 9
Getting results
ForkJoinPool.commonPool-worker-4 - - > will sleep : 20
Getting results
Getting results
Getting results
Getting results
Getting results
Getting results
Total duration = 13080
CollectedResults = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, java.util.concurrent.TimeoutException]