September 2011
3 posts
4 tags
Connecting to Cassandra with C# and Thrift
Here’s something I wanted to write down for a long time now, because every time I have to do this (when Cassandra changes its interface), I have already forgotten what and exactly how needs to be done. Let’s assume that we have Cassandra up and running, because most distributions should have a package ready to use, or at least a build script. And now we want to connect to it with C#....
3 tags
Web Server In 50 Lines
Web servers are useful, but not always you want or can install and configure one. Here’s a self contained, functional, multi-threaded and extensible web server in just few lines of C#.
1 using System;
2 using System.IO;
3 using System.Net;
4 using System.Text;
5
6
7 class Server {
8
9 static String root = "/home/you/web/root/";
10
11 static String prefix =...
2 tags
Transpose Rows To Columns
One very common problem when making reports from a RDBMS, is that you need to show, data that’s stored in rows, in columns. For example, from data like this:
1 CREATE TABLE #data_a (
2 cat varchar(20) NOT NULL,
3 mon date NOT NULL,
4 val int NOT NULL
5 );
6 INSERT INTO #data_a
7 VALUES
8 ('Foo', '2011-02-01', '752'),
9 ('Qux', '2011-03-01', '700'),
10 ('Qux',...
March 2010
1 post
4 tags
Error Logging
One of the very first things I do, for a new ASP.NET application, is to setup a decent error logging. I want to know when and why my application crashes and I don’t want to relay on users accurately reporting every error. It’s quite easy to do this using HttpApplication.Error event:
1 using System;
2 using System.Configuration;
3 using System.Data;
4 using...
February 2010
3 posts
1 tag
Undo Tree
A while ago, when I played around with E Text Editor, I was impressed by what they call a “Personal Revision Control”. It’s a nice concept, where instead of linear list of changes, which you can undo and redo, you can have branches.
For example, if you type “one”, then change it to “two”, then undo back to “one” and change it to...
1 tag
Objects
Trying to understand how objects work in JavaScript can be confusing, not because it’s complicated or difficult, but because the language is flexible enough to allow different approaches and also because many programmers simply aren’t familiar with prototype-based programming. I’ll try to demonstrate, what I think is the simplest and cleanest way to work with objects in...
3 tags
Latvian Keyboard Layout
Recently I got Nokia N900, but it doesn’t come with Latvian keyboard layout, not a big deal, since it’s running Linux and Xorg. This is a recollection of my journey into XKB configuration.
All of the fallowing code goes into /usr/share/X11/xkb/symbols/nokia_vndr/rx-51, after making changes, you run setxkbmap lv to activate the layout.
This was my first try:
1 partial...
January 2010
4 posts
3 tags
Computing MD5/SHA256/SHA512 Checksum
1 using System;
2 using System.Security.Cryptography;
3 using System.Text;
4
5 class Test {
6
7 public static int Main(String[] args) {
8 if (args.Length != 2) {
9 return 1;
10 } else {
11 HashAlgorithm algo;
12 switch (args[0].ToUpper()) {
13 case "MD5":
14 algo = new MD5CryptoServiceProvider();
15 break;
16 case...
2 tags
sp_lock Replacement
Here’s what I’ve been using instead of sp_lock:
1 SELECT
2 l.request_session_id AS sid,
3 r.status,
4 d.name AS [database],
5 o.name AS object,
6 l.request_mode,
7 l.request_status,
8 r.blocking_session_id AS blocking_sid,
9 r.wait_type,
10 r.wait_time,
11 r.total_elapsed_time,
12 r.percent_complete,
13 p.cpu,
14 p.physical_io,
15 p.memusage,
16 ...
4 tags
URL Mapping
Here’s full, working and as simple as possible example on how to get pretty URLs with .NET/Mono.
First, here’s all the code:
1 using System;
2 using System.Web;
3 using System.Web.Routing;
4
5
6 class RouteHandler : IRouteHandler {
7
8 Type PageType;
9
10 public RouteHandler(Type pageType) {
11 PageType = pageType;
12 }
13
14 public IHttpHandler...
3 tags
Type-Safe Collection of Diverse Types
So you have a couple of different objects (say, Foo and Bar), and would like to store them all in some sort of collection (Col), but in a type-safe way.
Here’s two solutions:
1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
4
5
6 // required only for Col2
7 public interface IItem {
8
9 void Print();
10
11 }
12
13
14 public class...
December 2009
4 posts
1 tag
Restore Cursor Position
Add the fallowing line to your .vimrc and when a file is opened, the cursor will be positioned to the same line it was left on, when this file previously was closed.
1 autocmd BufReadPost * if line("'\"") > 0|if line("'\"") <= line("$")|exe("norm '\"")|else|exe "norm $"|endif|endif
3 tags
String Aggregation
Say you have the following data and you want to concatenate all queries for each year.
1 CREATE TABLE years (year smallint);
2 INSERT INTO years VALUES
3 (2008),
4 (2007),
5 (2009);
6
7 CREATE TABLE top_queries (year smallint, place smallint, query varchar(50));
8 INSERT INTO top_queries VALUES
9 (2009, 4, 'twitter'),
10 (2008, 2, 'beijing 2008'),
11 (2008, 9, 'euro...
1 tag
Autocompletion
Vim has quite a few features for automatic text completion. The most basic is keyword completion - 1) open a file, 2) write first few characters of any word you see in the file, 3) hit CTRL+N to automatically complete the word. Simple, easy to use and very useful.
To make this feature more intuitive, consider mapping CTRL+Space to CTRL+N:
1 inoremap <C-Space> <C-n>
2 inoremap...
3 tags
Generating a Range of Values
For example, lets generate a set of months, from 2010-01-01 to 2011-01-01.
In SQL Server:
1 WITH t AS (
2 SELECT CONVERT(date, '2010-01-01') AS d
3 UNION ALL
4 SELECT DATEADD(MONTH, 1, d) FROM t WHERE d < '2010-12-01'
5 )
6 SELECT * FROM t ORDER BY d;
Same in PostgreSQL:
1 WITH RECURSIVE t AS (
2 SELECT '2010-01-01'::timestamp AS d
3 UNION ALL
4 SELECT d + '1 months' FROM t...