Skip to content

Instantly share code, notes, and snippets.

@legkvla
legkvla / hierarchy.sql
Last active August 29, 2015 14:26
Hierarchy query example
SELECT goals.id, goals.parent_id, depth, goals.name FROM (WITH RECURSIVE crumbs AS (
SELECT goals.*, -1 AS depth FROM goals WHERE id in (1)
UNION ALL
SELECT alias1.*, crumbs.depth + 1 FROM crumbs JOIN goals alias1 on alias1.parent_id = crumbs.id
WHERE crumbs.depth + 1 < 100000
) SELECT * FROM crumbs) as goals WHERE goals.depth + 1 < 100002 ORDER BY depth, position;
@legkvla
legkvla / Gemfile
Created July 22, 2015 07:26
Checking pop3 e-mail using mail gem
gem 'mail'
@legkvla
legkvla / path_counter.rb
Last active August 29, 2015 14:23
This script can be used to determine quamtity of rest api method calls using nginx access logs
$words_counter = {}
def process_token(token)
current_value = $words_counter[token]
if current_value
current_value.inc
else
$words_counter[token] = TokenCount.new(token)
end
end
mike@rbci:~$ psql -U postgres
psql (9.0.3)
Type "help" for help.
postgres=# update pg_database set datallowconn = TRUE where datname = 'template0';
UPDATE 1
postgres=# \c template0
You are now connected to database "template0".
template0=# update pg_database set datistemplate = FALSE where datname = 'template1';
UPDATE 1
@legkvla
legkvla / SimpleLock.java
Last active August 29, 2015 14:19
Jgroups lock example (warning: in normal code you must unlock in finally block)
package ru.examples;
import org.jgroups.JChannel;
import org.jgroups.blocks.locking.LockService;
import java.util.concurrent.locks.Lock;
public class SimpleLock {
/** You should provide one parameter - node name */
@legkvla
legkvla / wc.py
Created March 29, 2015 20:32
WC replacement on python (I was needed it for my busy-box)
#!/usr/bin/env python
import sys
if __name__ == "__main__":
# Initialize a names dictionary as empty to start with.
# Each key in this dictionary will be a name and the value
# will be the number of times that name appears.
names = {}
# sys.stdin is a file object. All the same functions that
# can be applied to a file object can be applied to sys.stdin.
@legkvla
legkvla / time_on_rows
Last active August 29, 2015 14:06
My Mondrian DB query examples
SELECT
non empty {[Product].[All Products].Children} ON COLUMNS,
non empty {[Time].[Quarter].members, [Time].[Year].members} ON ROWS
FROM [Sales]
@legkvla
legkvla / profiler.rb
Created August 28, 2014 08:28
Simple Ruby Profiler for JRuby
require 'java'
class Profiler
def initialize(name)
@name = name
@metrics = {}
@start_times = {}
end
@legkvla
legkvla / convert_dicts
Created July 25, 2014 07:58
Russian full text search setup in pgsql
#path can be different. For Red Hat it is /opt/PostgreSQL/9.3/share/postgresql/tsearch_data
iconv -f koi8-r -t utf-8 < ru_RU.aff > /usr/share/postgresql/tsearch_data/russian.affix
iconv -f koi8-r -t utf-8 < ru_RU.dic > /usr/share/postgresql/tsearch_data/russian.dict
@legkvla
legkvla / birhdays_query.sql
Last active August 29, 2015 14:00
Birthday ordering on postgres
select * from
(
select
CASE
WHEN days < (current_date - date_trunc('year', current_date)) THEN days + interval '366 days'
ELSE days
END as position, birthday
from
(select birthday, birthday - date_trunc('year', birthday) as days from birthdays) bd_in_days
) bd