Skip to content

Instantly share code, notes, and snippets.

View ufuk's full-sized avatar
🤔

Ufuk Uzun ufuk

🤔
View GitHub Profile
@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-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 / 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 / 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 / TurkishNumberUtils.java
Last active November 13, 2020 07:55
Util for converting whole numbers to Turkish words
public final class TurkishNumberUtils {
private static final String SPACE = " ";
private static final String EMPTY = "";
private static final String[] PERIOD_NAMES = {EMPTY, "bin", "milyon", "milyar", "trilyon", "katrilyon", "kentilyon"};
private static final String[] UNITS_TEXTS = {EMPTY, "bir", "iki", "üç", "dört", "beş", "altı", "yedi", "sekiz", "dokuz"};
@ufuk
ufuk / posgresql-lightspeed-alter-table.sql
Last active November 5, 2018 07:59
Increase performance of alter tables or any other expensive operations by increasing WORK_MEM parameter for current session in PostgreSQL
BEGIN;
LOCK TABLE ... IN SHARE MODE;
SET LOCAL WORK_MEM = '256MB';
ALTER TABLE ...;
COMMIT;
@ufuk
ufuk / postgresql-export-tables-to-s3-as-csv-file.sh
Last active July 10, 2023 04:04
BASH script to export some tables from the PostgreSQL database to S3 as CSV files.
#!/bin/bash
export AWS_ACCESS_KEY_ID=...
export AWS_SECRET_ACCESS_KEY=...
export AWS_DEFAULT_REGION=...
PG_HOST=...
PG_USER=...
PG_PASS='...'
PG_DB=...
@ufuk
ufuk / bash-echo-time-by-timezone.sh
Created January 31, 2019 13:55
Echo time by timezone in BASH
echo "$(TZ='Europe/Moscow' date +%Y-%m-%d\ %H\:%M)"
# output => 2019-01-31 16:52
@ufuk
ufuk / import-csv-file-from-s3-into-aws-redshift.sql
Created January 31, 2019 14:01
Import CSV file from S3 into AWS Redshift
-- Before importing, you need to create table
CREATE TABLE example_table
(
...
);
-- Importing...
COPY example_table
FROM 's3://<BUCKET_NAME>/.../example_table.csv'
CREDENTIALS 'aws_access_key_id=...;aws_secret_access_key=...'
@ufuk
ufuk / BaseMockito2JUnit4Test.java
Last active October 24, 2021 17:52
Performs "verify no more interactions" check automatically for all mock objects (works with Mockito version 2 or higher & JUnit version 4 and 5). For detailed description: https://ufukuzun.wordpress.com/2019/04/09/ne-olup-bittiginden-habersiz-testlere-derman-mockscollector/ (Turkish)
import org.apache.commons.lang3.ArrayUtils;
import org.junit.After;
import org.junit.runner.RunWith;
import org.mockito.InOrder;
import org.mockito.junit.MockitoJUnitRunner;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.verifyNoMoreInteractions;
@RunWith(MockitoJUnitRunner.class)