Showing posts from October, 2008

A Count(*) View for CouchDB

I've been working with CouchDB a lot lately. The first thing I tried to do, after I loaded 1,000,000 records, is to do the equivalent of count(*). Below is the view I used to implement a "count how many records I have in this database" or "count all":

{ "_id": "_design/counts",
"language": "javascript",
"views": {
"all": {
"map": "function(doc) { emit(null, 1); }",
"reduce": "function(keys, values, combine) { return sum(values); }"

Calculating Combinations Using Java and Lots of Bits

I was feeling nostalgic and went back to see how I calculated combinations in Erlang and combinations in Ruby. I wanted to see if there was a fun way to do it without resorting to recursion.

I started with my crazy hack to use bit strings to calculate combinations. I didn't want to resort to creating bit strings, as I wanted to minimize the amount of work I needed to do. Therefore, I thought about simply using bitwise operators.

Below is my Java algorithm for creating calculations using iteration and bitwise operators to create all combinations from an array.


public static void generate(String[] list) {
int max = (int) Math.pow(list.length, 2)-1;
System.out.println(max + " combinations");
for (long i = 0; i < max; i++) {
String[] combo = new String[Long.bitCount(i)];
int comboPos = 0;
for (int j = 0; j < list.length; j++) {
if ((i & (1L< 0) {
combo[comboPos++] = list[j];

Update: handle …

Easy Way to Configure Rails ActiveRecord and SQL Server on Mac OS X

Thanks to How to Connect To Microsoft SQL Server From Rails On OSX, I found out a very easy way to configure the database connection.

Assuming you have FreeTDS installed (easily through macports) and iODBC (comes out of the box with Mac OS X) then this is all you need in your database.yml file:

adapter: sqlserver
mode: odbc
dsn: DRIVER=/opt/local/lib/;TDS_Version=8.0;SERVER=;DATABASE=awesome_development;Port=1433;uid=sa;pwd=password;

No configuration of a DSN required, and no mucking about with freetds.conf!

Now, if I could just use database.yml to get rid of SQL Server all together, I'd be a happy developer.