Skip to content

Instantly share code, notes, and snippets.

View rponte's full-sized avatar
🏠
Working from home

Rafael Ponte rponte

🏠
Working from home
View GitHub Profile
@rponte
rponte / TLVEncoder.java
Last active June 18, 2020 14:06
Just a simple implementation of a TLV encoder
package br.com.rponte.gist.util.tlv;
import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.commons.lang3.StringUtils.join;
import static org.apache.commons.lang3.StringUtils.leftPad;
import java.math.BigDecimal;
import java.util.LinkedHashMap;
import java.util.Map;
@rponte
rponte / postgresql-list-connections-status.sql
Created May 2, 2020 15:10
PostgreSQL: listing all connections status
SELECT count(*), state FROM pg_stat_activity GROUP BY 2;
@rponte
rponte / TransferenciaEntreContasDeMesmoCpf.java
Created April 28, 2020 20:45
DDD: Exemplo de Domain Service
@ApplicationScoped
public class TransferenciaEntreContasDeMesmoCpf {
@Inject
public ContasDoCliente contas;
@Transactional
public void transfere(Integer origemId, Integer destinoId, BigDecimal valor) {
Conta origem = contas.buscaPorId(origemId);
@rponte
rponte / taskqueues.sql
Created April 26, 2020 00:40 — forked from purcell/taskqueues.sql
Easy task queues using PostgreSQL
-- Let's say you have a table full of work:
CREATE TABLE tasks (
id UUID PRIMARY KEY NOT NULL DEFAULT gen_random_uuid(),
status TEXT NOT NULL DEFAULT 'pending',
payload JSON NOT NULL, -- or just have meaningful columns!
created_at TIMESTAMP NOT NULL DEFAULT NOW()
);
@rponte
rponte / postgresql-dates-and-timezones.sql
Last active February 11, 2022 12:53
PostgreSQL: playing with datetime and timezone
SET timezone TO 'America/Fortaleza';
SET timezone TO 'America/Sao_Paulo';
select now() as "now()"
, now()::timestamp as "now()::timestamp"
, now()::timestamptz as "now()::timestamptz"
, now() at time zone 'utc-0' as "now() - utc-0"
, now() at time zone 'America/Fortaleza' as "now() - fortaleza"
, now() at time zone 'America/Sao_Paulo' as "now() - sao paulo"
, localtimestamp
@rponte
rponte / postgresql-cache-hit-ratio.sql
Last active May 8, 2020 22:23
PostgreSQL: showing the cache hit ratio and the top unused indexes
select sum(heap_blks_read) as heap_read
,sum(heap_blks_hit) as heap_hit
,sum(heap_blks_hit) / (sum(heap_blks_hit) + sum(heap_blks_read)) as ratio
from pg_statio_user_tables
;
@rponte
rponte / CTE-with-DML-inside.sql
Last active August 7, 2024 19:57
PostgreSQL and CTE with DMLs: I didn't know it was possible (DELETE and Soft-DELETE)
with deleted_rows as
(
delete
from tweet.users
where not exists
(
select 1
from tweet.message
where userid = users.userid
)
@rponte
rponte / .bashrc_example.sh
Last active May 26, 2020 21:01
Tailing log files with colored output
# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
# User specific aliases and functions
tail-color() {
##
@rponte
rponte / HowToDecryptAPIN.java
Last active December 30, 2020 20:22
How to decrypt a PIN
public class HowToDecryptAPIN {
/**
* Using fake data
*/
public static void main(String[] args) {
// PAN and PIN informed by the user (ie.: via POS or ATM)
String pan = "4000340000000500"; // card number
String encryptedPin = "FA8EA4D3FCB5466E"; // pin=2020
@rponte
rponte / PinblockTool.java
Created March 13, 2020 16:29 — forked from Gilmor/PinblockTool.java
Pinblock ISO 9564 format 0
package cz.monetplus.mnsp.tools.misc;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.lang.StringUtils;
/**
* Tools for encoding a decoding pinblock
*
* @author Tomas Jacko <tomas.jacko [at] monetplus.cz>