Skip to content

Instantly share code, notes, and snippets.

@ufuk
ufuk / learn-which-process-has-allocated-the-tcp-port.sh
Last active March 23, 2018 11:30
Learn which process has allocated the TCP port (for example "8080")
lsof -i tcp:8080
# Example output:
# COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
# java 86935 ...
@ufuk
ufuk / postgres-create-drop-index-without-locking.sql
Last active March 10, 2018 20:43 — forked from okanmenevseoglu/postgres-create-drop-index-online.sql
Create/Drop a database index without locking the table's selects, inserts, etc. on PostgreSQL
CREATE INDEX CONCURRENTLY your_index_name on your_table_name (your_column_name);
DROP INDEX CONCURRENTLY your_index_name;
@ufuk
ufuk / postgresql-cryptography-functions.sql
Last active January 29, 2018 11:29
Enable cryptography functions (pgcrypto extension) in PostgreSQL.
CREATE EXTENSION pgcrypto;
-- Examples
SELECT ENCODE(DIGEST('password', 'md5'), 'hex');
SELECT ENCODE(DIGEST('password', 'sha1'), 'hex');
SELECT ENCODE(DIGEST('password', 'sha224'), 'hex');
SELECT ENCODE(DIGEST('password', 'sha256'), 'hex');
SELECT ENCODE(DIGEST('password', 'sha384'), 'hex');
SELECT ENCODE(DIGEST('password', 'sha512'), 'base64');
@ufuk
ufuk / ScaleAndTranslateIntoRange.java
Created July 25, 2017 10:51
Scales and translates a value (x) into a new range [a, b].
/**
* Scales and translates a value (x) into a new range [a, b].
*
* Formula:
* f(x) = a + (b - a)(x - min)/(max - min)
*
* See also: https://stackoverflow.com/a/5295202
*/
public BigDecimal scaleAndTranslateIntoRange(BigDecimal value, BigDecimal actualMin, BigDecimal actualMax, BigDecimal desiredMin, BigDecimal desiredMax) {
return desiredMax.subtract(desiredMin).multiply(value.subtract(actualMin))
@ufuk
ufuk / postgresql-export-import-schema.sh
Last active June 11, 2018 12:50
Exporting and then importing full schema for PostgreSQL.
# export
PGPASSWORD="<PASSWORD>" pg_dump -U <USERNAME> -h <HOST> -d <DB_NAME> -n <SCHEMA_NAME> > export.sql
# import
PGPASSWORD="<PASSWORD>" psql -U <USERNAME> -h <HOST> -d <DB_NAME> -n <SCHEMA_NAME> -f export.sql
@ufuk
ufuk / SomeSpringBeanThatNeedsAResource.java
Last active July 11, 2017 07:50
Getting and using a file from classpath with Spring's @ Value annotation.
package ...;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;
import java.io.InputStream;
@Service
public class SomeSpringBeanThatNeedsAResource {
@ufuk
ufuk / concatenate-grep-and-split-by-delimeter.sh
Created July 10, 2017 09:08
Concatenate files, grep lines, split by a delimeter using awk and print some columns as sorted and uniqe. May be useful for parsing logs.
cat file1 file2 ... fileN | grep "<SEARCHING TEXT>" | awk -F'<DELIMETER>' '{print $1 $2 ... $n;}' | sort | uniq
@ufuk
ufuk / find-largest-10-directories.sh
Last active July 9, 2017 19:22
Helps you to find out largest 10 directories in the current working directory in a UNIX system. (Command to see disk usage: df -h)
du -a . | sort -n -r | head -n 10
@ufuk
ufuk / siege-post-json.sh
Last active April 14, 2023 16:37
Siege command to benchmark POST method with JSON payload and headers example. ("-c" for concurrent request count) ("-t" for time) ("-H" for header)
siege -c50 -t5S -H 'Content-Type: application/json' -H 'access-token: ...' 'http://localhost:8080/... POST {"...": "...", ...}'
@ufuk
ufuk / AsyncConfiguration.java
Last active December 10, 2022 21:43
The easy way to disable @ Async annotation for test contexts. Same approach can be used to disable @ Scheduled annotation as well.
package ...configuration;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.scheduling.annotation.EnableAsync;
@Configuration
@EnableAsync
@Profile("!test")
public class AsyncConfiguration {