Skip to content

Instantly share code, notes, and snippets.

@bigdragon1977
bigdragon1977 / show_usage_table.sql
Created November 10, 2015 10:57
Postgresql-Disk Usage
SELECT nspname || '.' || relname AS "relation",
pg_size_pretty(pg_relation_size(C.oid)) AS "size"
FROM pg_class C
LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
WHERE nspname NOT IN ('pg_catalog', 'information_schema')
ORDER BY pg_relation_size(C.oid) DESC
LIMIT 20;
@bigdragon1977
bigdragon1977 / DBCP.java
Created September 9, 2015 19:08
HikariCP + Sql2o + log4jdbc-log4j2 + Wicket
@Singleton
public class DBCP implements IDBCP {
private final Sql2o sql2o;
public DBCP() {
String configPath =
getClass().getClassLoader().getResource("").getPath() + "hikari.properties";
sql2o =
new Sql2o(new HikariDataSource(new HikariConfig(filePath)), new PostgresQuirks());
@bigdragon1977
bigdragon1977 / ResultMap.scala
Created September 6, 2015 19:11
PositionedResult
// http://stackoverflow.com/questions/20262036/slick-query-multiple-tables-databases-with-getting-column-names/20278761#20278761
import scala.slick.jdbc.{GetResult,PositionedResult}
object ResultMap extends GetResult[Map[String,Any]] {
def apply(pr: PositionedResult) = {
val rs = pr.rs // <- jdbc result set
val md = rs.getMetaData();
val res = (1 to pr.numColumns).map{ i=> md.getColumnName(i) -> rs.getObject(i) }.toMap
pr.nextRow // <- use Slick's advance method to avoid endless loop
res
}
/*
gcc -Wall -pedantic -std=c99 -O2 -o timegm timegm.c
Test manual epoch calculation with tm_yday vs TZ+mktime.
On Linux, gcc 4.4.7, the manual method is ~5x faster.
Usage:
./timegm
<ctrl-C> to quit
@bigdragon1977
bigdragon1977 / arrows.js
Created January 7, 2015 19:11
Arrows in Leaflet
var arrowPolyline = L.Polyline.extend({
addArrows: function(){
var points = this.getLatLngs()
for (var p = 0; p +1 < points.length; p++){
var diffLat = points[p+1]["lat"] - points[p]["lat"]
var diffLng = points[p+1]["lng"] - points[p]["lng"]
var center = [points[p]["lat"] + diffLat/2,points[p]["lng"] + diffLng/2]
var angle = 360 - (Math.atan2(diffLat, diffLng)*57.295779513082)
var arrowM = new L.marker(center,{
icon: new L.divIcon({
@bigdragon1977
bigdragon1977 / bitwise.java
Created November 30, 2014 20:23
Using simple bitwise operations
//Using simple bitwise operations:
data[0] = (byte) (width & 0xFF);
data[1] = (byte) ((width >> 8) & 0xFF);
//How it works:
/*
& 0xFF masks all but the lowest eight bits.
>> 8 discards the lowest 8 bits by moving all bits 8 places to the right.
The cast to byte is necessary because these bitwise operations work on an int and return an int, which is a bigger data type than byte. The case is safe, since all non-zero bits will fit in the byte. For more information, see Conversions and Promotions.
@bigdragon1977
bigdragon1977 / Crc16.java
Created November 30, 2014 20:18
crc16 implementation java
public class Crc16 {
public static void main(String... a) {
byte[] bytes = new byte[] { (byte) 0x08, (byte) 0x68, (byte) 0x14, (byte) 0x93, (byte) 0x01, (byte) 0x00,
(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x06, (byte) 0x00, (byte) 0x00, (byte) 0x01,
(byte) 0x00, (byte) 0x13, (byte) 0x50, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x22, (byte) 0x09,
(byte) 0x11 };
byte[] byteStr = new byte[4];
Integer crcRes = new Crc16().calculate_crc(bytes);
System.out.println(Integer.toHexString(crcRes));
@bigdragon1977
bigdragon1977 / ctags.vim
Last active August 29, 2015 14:09 — forked from agateau/ctags.vim
" ctags.vim: Display function name in the title bar and/or status line.
" Author: Alexey Marinichev <lyosha-vim@lyosha.no-ip.org>
" Maintainer: Gary Johnson <garyjohn@spk.agilent.com>
" Contributor: Keith Reynolds
" Last Change: 2003-11-26 00:23:22
" Version: 2.1
" URL(1.0): http://vim.sourceforge.net/scripts/script.php?script_id=12
" URL(>=2.0): http://vim.sourceforge.net/scripts/script.php?script_id=610
" DETAILED DESCRIPTION:
@bigdragon1977
bigdragon1977 / getByteBuf.java
Created November 13, 2014 09:03
Netty java getting data from ByteBuf
ByteBuf buf = ...
byte[] bytes = new byte[buf.readableBytes()];
buf.readBytes(bytes);
//If you don't want the readerIndex to change:
ByteBuf buf = ...
byte[] bytes = new byte[buf.readableBytes()];
int readerIndex = buf.readerIndex();
buf.getBytes(readerIndex, bytes);