today i learned

Tuesday, 19 February 2021
in postgres timestamp with time zone does not actually save the time zone information

Monday, 18 February 2021
sorting a 2D array in Java based on a specific column

int[][] arr;
Arrays.sort(arr, Comparator.comparingInt(o -> o[1]));
// Decending
Arrays.sort(arr, Comparator.comparingInt(o -> -o[1]));
// Alternatively
Arrays.sort(arr, ((o1, o2) -> o1[1] - o2[1]));
// Alternatively
Arrays.sort(arr, ((o1, o2) -> Integer.compare(o1[1], o2[1])));

Thursday, 11 February 2021
adding swap memory to an elasticbeanstalk instance via prebuild custom platform hook

# .platform/hooks/prebuild/01_add_swap_memory.sh

#!/bin/bash

set -o xtrace
set -e

if grep -E 'SwapTotal:\s+0+\s+kB' /proc/meminfo; then
    echo "no swap space identified, creating some."
    dd if=/dev/zero of=/var/swapfile bs=1M count=512
    /sbin/mkswap /var/swapfile
    chmod 000 /var/swapfile
    /sbin/swapon /var/swapfile
else
    echo "doing nothing."
fi

Wednesday, 10 February 2021
Java Shell(JShell) is a REPL that was introduced out of the box with JDK 9

$ jshell
jshell> System.out.println("Welcome to JShell")
Welcome to JShell

jshell> /exit
|  Goodbye


Tuesday, 9 February 2021
to run a custom platform_hook on AWS elasticbeanstalk as leader_only:

if [[ "$EB_IS_COMMAND_LEADER" == "true" ]]; then
  //script
fi

Monday, 8 February 2021
to make uuid as the default primary key in Rails
add a migration: $ ./bin/rails g migration PkDefaultUUID

class PkDefaultUUID < ActiveRecord::Migration[6.1]
  def change
    enable_extension 'pgcrypto'
  end
end
run the migration: $ ./bin/rails db:migrate

class CreateWorld < ActiveRecord::Migration
  def change
    create_table :worlds, id: :uuid  do |t|
      t.string :population
    end
  end
end

Sunday, 7 February 2021
for iterating through a range Java 8 introduced IntStream

IntStream.range(0, n).forEach(i -> {
    System.out.println(i);
});
// If order matters
IntStream.range(0, n).forEachOrdered(i -> {
    System.out.println(i);
});

Saturday, 6 February 2021
to avoid NoSuchElementException, Java stream returns an OptionaInt

int[] arr;

int max = Arrays.stream(arr).max().getAsInt();
// Arrays.stream(arr).max() returns an OptionalInt hence the use of getAsInt()

Friday, 5 February 2021
ways to check if a character is present in a String

// check for charater in a string
1. str.indexOf(ch);
2. str.contains(String.valueOf(ch));
3. str.contains("" + ch);

Thursday, 4 February 2021
limitations of WebRTC

1. peer-to-peer WebRTC is more suited to limited number of participants
2. heavy computing required to decode and re-encode streams
3. lack of support for certain features on safari and edge

Wednesday, 3 February 2021
tailwindcss is a utility-first CSS framework with the ability to purge all unused css for production

Thursday, 14 January 2021
in Ruby a single object and an array can be treated the same with [*var]

sample_object = 1
sample_array = [1, 2, 3]

[*sample_object].each { |element| p element }
[*sample_array].each  { |element| p element }

Wednesday, 13 January 2021
low default(1024) of nginx worker_connections can impact the number of client connections. 10k is a good starting point for production servers

Tuesday, 12 January 2021
creating an array with random integers

# ruby
Array.new(10) { rand 50 }   # ruby

# python
from random import randint
[randint(0, 50) for p in range(0, 10)]

// Java
Random random = new Random();
random.ints(10,0,50).toArray()

Monday, 11 January 2021
AWS: Custom platform hooks are discontinued in Amazon Linux 2 servers. Platform hooks written previously in the .ebextensions folder need to be migrated to .platform/hooks

Saturday, 9 January 2021
pitfalls of TOTP as single factor authorization:
  • it has a shared secret, which if compromised would give access to all logins
  • fast expiration can require user to enter multiple codes
  • database compromise will require resetting the secret

Friday, 8 January 2021
Instance Initialization Block(IIB) are executed whenever the class is initialized and before constructors

class ExampleClass {
    // Instance Initialization Block 
    {  
        System.out.println("I perform additional operations while assigning value"); 
    } 
}
 
Thursday, 7 January 2021
Rails 6 added create_or_find_by as an improvement over find_or_create_by to avoid stale reads between SELECT and INSERT