Codility

Binary Gap

import java.util.regex.Pattern;  
import java.util.regex.Matcher; 
import java.util.Collections;
import java.util.ArrayList;
import java.util.NoSuchElementException;

class Solution {
    public int solution(int N) {
        String bin = Integer.toBinaryString(N);
        Pattern p = Pattern.compile("(1)(0+)(?=1)");
        Matcher m = p.matcher(bin);
        ArrayList<Integer> lst= new ArrayList<Integer>();
        while(m.find()){
            lst.add(m.group(2).length());
        }
        try{
            return Collections.max(lst);
        }catch(NoSuchElementException e){
            return 0;
        }
    }
}

dB2 LUW v9.7

How to copy an entire schema between AIX and Windows:

A few months ago I had the need to setup a standalone environment in my laptop in order to have independency while testing and develop a new system architecture.

The problem was the dB2 schema that I needed was on a AIX machine. So I had to copy it all and move to my Windows machine.

Here’s the step-by-step of how to do it:

  1. On your AIX machine, export data of schema MY_SCHEMA

  2. db2move DBNAME export -sn MY_SCHEMA

    The output of this produced is:

    • EXPORT.out-Contains a summary of the completed action (ASCII).

    • db2move.lst -Contains a list of original table names, their corresponding PC/IXF file names, and message file names (ASCII).

    • tabn.ixf -Contains exported data from a user table, identified by n (binary).

    • tabn.msg -Contains messages about the requested action against a user table, identified by n (ASCII).

    • tabna.nnn -Contains exported large object (LOB) data for a user table, identified by n. The file name extension is a number ranging from 001 to 999, and a is an alphabetic character.These LOB files, which are created only if the table being exported contains LOB data, are placed in the LOB path directories system.msg

  3. Extracts all DDL statements for database objects (-e) for a specific schema (-z). Also generates tablespaces, databsse partition groups, and bufferpools (-l)

  4. db2look -d MY_SCHEMA -o db2look_sch.sql -e -z MY_SCHEMA -l

  5. Compress it all and download into your Windows machine

  6. tar cf - * | gzip > /big/schdb2.tar.gz
    rec aixmachinename /big/schdb2.tar.gz

    This rec funtion is intended to downdload the file from the remote aix machine through scp tool

  7. On the Windows machine, open a bash console, e.g. cygwin and do this:

  8. sed -i 's_/pfdata/db2ipf2/DBNAME/db2ipf2/_C:\\DB2\\_' db2look_sch.sql
    sed -i 's_/pfdata/db2ipf2/MY_SCHEMA/_C:\\DB2\\NODE0000\\SAMPLE\\_' db2look_sch.sql
    sed -i 's_/pfdata/db2ipf2/datatmp\_tbs/_C:\\DB2\\NODE0000\\_' db2look_sch.sql
    sed -i -r 's/(USING *\(FILE .* )([0-9]*)\)/\11 M\)/' db2look_sch.sql

  9. Open a DB2 CLP console

  10. sed -i 's_MY_SCHEMA_SAMPLE_' db2look_sch.sql
    sed -i 's/AIXDBOWNER /WINDBOWNER/' db2look_sch.sql

  11. Run DDL definition:
  12. db2 -tvf db2look_sch.sql > db2look_sch.log

  13. Load data

  14. db2move SAMPLE load

  15. Finally you will get SQLCODE=-668 SQLSTATE=57016 with “reason code”=1. To fix it please check snippet with SET_INTEGRITY

  16. You might also have Not Authorized SQLCODES 551N. Fix it running GRANT with db2admin

  17. GRANT ALL ON SECURITY_MANAGER.T_SECURITY_MAINTENANCE TO DB2USER

HMAC SHA-256 @CLI

A few days ago we were implementing HMAC SHA-256 algorithm in java accordingly to RFC 2104. At a certain point the question was: “Is that implementation correct?” How could we be sure of it? The following openssl command cleared out the doubts.

echo -n [SAMPLE_INPUT_TEXT] | openssl dgst -sha256 -mac hmac -macopt hexkey:[HEXA_KEY]

Putting some value on it we got:

echo -n "TEXT_INPUT_FOR_HMAC" | openssl dgst -sha256 -mac hmac -macopt hexkey:CBA54DBA951FB57F905AC8696BDE3A77D8A525DB3CFC61787F9C13A78C78CBB58D12F7C6
(stdin)= 0aa133f891f038b31f20843280a61ca1f6e057a16fc178fd907202f5ea11eb0c

That way we were confident to promote the software into production

tmux

I was introduced in tmux with this arcticle.

All tmux commands start with Ctrl+b.
Some few startup notes:

Ctrl+b;" Horizontal split
Ctrl+b;% Vertical split
Ctrl+b;c Start new session
Ctrl+b;a Broadcast command to all windows
Ctrl+b;z Maximize window session
Strl+b;: setw synchronize-panes on Broadcast commands

ex editor

In vi you have edit mode, insert mode and visual mode.
For insert mode press i, edit mode esc, visual v.
In edit mode, move left press h, down h, up k, rigth l.

In edit mode, you can send commands to ex editor.
The colon : tells vi to invoke ex:

Temporarily display the line number of a set of files, :1,10#
Print total number of files, :=
Print current line number, :.=

View line numbers: :set nu
Not view line numbers::set nonu
Search with case insensitive: :set ic
Search with case sensitivte: :set noic

Delete from current line to end of file, :.,$d
Delete from line 20 to current line, :20,.d
Delete all lines in file :%d

Search a pattern and print the following 10 lines, :/pattern/;+10 p

Read the contents of a file2 into the current file, :r file2. Content of file2 is inserted at current cursor position.

Open a file in a new tab, while editing one already, do : tabe [file_name]
Move to previous tab : tabp
Move to next tab : tabn
View all opened files : args

View the file in hexa mode do :%!xxd

Save and exit :x.

vi editor

Using pure vi commands:

Copy to a named buffers, e.g. r two lines of text "r2yy
Insert the contents of buffer r into current cursos position.

Search a text forward /[text_to_search] and backward ?[text_to_search].
Repeat the search forward press n, and backward N

Search and replace, i.e. substitue general command:
:[start],[finish]s/[find]/[replace]/g

:%s/the/The/g &#09 Replace the with The. % means the complete file.
:%s/the/The/gc &#09 Replace the with The but ask before substituting.

Repeats the last action press .(dot) key in edit mode

Execute a command from within vi do :![your command].
For instance, change file permission to allow you to write without close it :!chmod +w %. The % will be replaced by the file name.

SQL1639N

Did you got this error SQL1639N before ?

SQL1639N The database server was unable to perform authentication becausesecurity-related database manager files on the server do not have the requiredoperating system permissions. SQLSTATE=08001

You might get it while doing db2 connect to SAMPLE user db2user.
If so just proceed like this:

# cd /home/db2inst1/sqllib/security
# chown root:root db2chpw
# chown root:root db2ckpw
# chmod u+s db2chpw
# chmod u+s db2ckpw

Although stated in documentation, on version DB2 v11.1.3.3″ there’s no need to run db2iupdt

Terminator: 10 shortcuts

Ten useful shortcuts while using terminator:

1. CTRL + SHIFT + E : Open another instance adjacent to the one you have, but withou openning a new window. Usefull when you wanna send several commands and hava a one galnce view to all.

2. CTRL + SHIFT + T : Open another instance in a new tab of the same window.

3. CTRL + SHIFT + W : Closes an instance.

4. ALT + A : Broadcast to all tabs of terminator

5. ALT + G : Broadcast to the group of the selected tab

6. ALT + O : Turn off broadcast

7. SUPER + G : Group all windows

8. SHIFT + SUPER + G : Ungroup all windows

9. CTRL + SHIFT + X : Toggle between one tab terminal and all tabs.

10. ALT + F2 : Run a command

Though one thing terminator lacks is how to open an new terminator window while establishing a new ssh connection to a remote host. You can indeed, achieve that with profiles but if you have dozens of remote servers, it can become hard to manage.
In this case consider using tmux.

Base 64 Encoding

Base 64 enconding is a quite common way of transmitting binary data this days. It’s based on 26 characters shown in this table:

base64

For each character encoding we need indeed 6 bits. One more character is used for padding, the =”

Let’s see then how would you encode the message “Man”, which does not need padding:

Selection_012

That is Man is encoded as TWFu.
The pseudo-code for padding is:

if (( text.length % 3) != 0) then {
Right padding with zeros encoded with ‘=’.
Number of added bytes is equal number of ‘=’ .
}

For instance, Face in base 64 is RmFjZQ==:

Face

You can see the extra zeros bits highlighted with yellow.
Applying the pseudo-code to determine the number of extra bytes needed:

8 * 4 = 32 bits length of text.
( 32 + ( 8 * 0 )) % 3 = 10,67 //NOK
( 32 + ( 8 * 1 )) % 3 = 13,3(3) //NOK
( 32 + ( 8 * 2 )) % 3 = 0 // OK!

And we’ve indeed used 2 = characters.