Skip to content

Instantly share code, notes, and snippets.

@i978sukhoi
i978sukhoi / ec2.sh
Created January 15, 2020 00:36
list up AWS EC2 instances using AWS CLI
#!/bin/bash
case "$1" in
"live")
AWS_PROFILE="--profile live ";;
*)
AWS_PROFILE="";;
esac
aws $AWS_PROFILE ec2 describe-instances\
@i978sukhoi
i978sukhoi / IPUtil.kt
Created January 3, 2020 00:32
CIDR to Integer Range, IPv4 to Integer, Integer to IPv4
object IPUtil {
fun cidr2range(cidr: String): IntRange {
val parts = cidr.split('/')
val address = ip2int(parts[0])
val mask = -0x1 shl (32 - (parts.getOrNull(1)?.toInt() ?: 0))
return IntRange(address and mask, address or mask.inv())
}
fun int2ip(ip: Int) =
String.format("%d.%d.%d.%d", ip shr 24 and 0xff, ip shr 16 and 0xff, ip shr 8 and 0xff, ip and 0xff)
@i978sukhoi
i978sukhoi / Bitnum.kt
Created December 23, 2019 06:25
use bit-mask to store Enum as multiple boolean flag.
import kotlin.reflect.KClass
/**
* Bit-mask [Enum].
*
* use bit-mask to store [Enum] as multiple boolean flag.
*
* usage:
* ```kotlin
* enum class MyEnum(override val pos: Int) : Bitnum {
@i978sukhoi
i978sukhoi / SimpleCache.kt
Created December 12, 2019 03:36
simple in-memory cache with expire functionality
import java.util.concurrent.ConcurrentHashMap
/**
* In-memory 초간단 캐시
* @param defaultTtl 기본 캐시 시간. 단위: 1초
* @param defaultIgnoreTtlOnFail 실패시 캐시가 만료됐더라도 값을 이용할지 여부. [get], [collect]에 한정.
*/
class SimpleCache<K, V>(
private val defaultTtl: Int = 600,
private val defaultIgnoreTtlOnFail: Boolean = false
@i978sukhoi
i978sukhoi / SimpleSqlBuilder.kt
Last active November 12, 2019 07:59
simple dynamic sql query builder
/**
* ```
* val ssb = SimpleSqlBuilder(logger)
* ssb.select("a.id, a.col1, b.col2, c.meta_val")
* .from("table_a as a")
* .join("table_b as b on b.id = a.id") // simple left join
* .join("table_c as c on c.parent = a.id and c.meta_key = ?", META_KEY) // condition on join clue
* .where("a.name = ?", "BOB") // simple clue
* .andIn("a.type in ?", listOf("A", "B", "C")) // translated to "a.id in ('A','B','C')"
* .andBetween("a.age", 20, 30) // a.age between 20 and 30