Posts

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. Thoughts? 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]; } } //System.out.println(Arrays.toString(combo)); ...

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: development: adapter: sqlserver mode: odbc dsn: DRIVER=/opt/local/lib/libtdsodbc.so;TDS_Version=8.0;SERVER=10.0.6.20;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.

Ironically, The Open Web Foundation Requires Registration

So the irony is that the Open Web Foundation requires registration and justification for joining.

QOTD - Twitter Learning Scala

Several of us engineers at Twitter, Inc. are learning Scala as the language in which to develop new components for our system. From Graceless Failures: Hello World

Issues with Active Scaffold and Rails 2.1, Solved

If you are looking to upgrade to Rails 2.1 and you are using Active Scaffold , be aware it still has a few rough edges. Make sure you install the Rails 2.1 branch of Active Scaffold: git clone git://github.com/activescaffold/active_scaffold cd active_scaffold git branch -r (just lists the branches) git checkout origin/rails-2.1 (Check out the full thread for Rails 2.1 and Active Scaffold compatibility ) Second, I had to patch vendor/plugins/active_scaffold/lib/extensions/generic_view_path.rb . On line 53, make it look like this: if !@template.controller.is_a?(ActionMailer::Base) && @template.controller.class.uses_active_scaffold? I had to add !@template.controller.is_a?(ActionMailer::Base) && All my tests are now passing! I still love Active Scaffold, even though it's a bit behind the times.

Scalable Counters for Web Applications

So you need to provide a count or counter for your web application, but you want it to scale. The naive approach would be to simply select count(*) from table . That will fail under load because it requires scanning your entire collection. The first question you need to ask is, Do you need exact counts or will approximate counts be good enough? I bet in many situations, an approximate count will be perfectly reasonable. Think about the use case of tracking web hits. When you're talking about millions of hits, what is the difference between 1,000,000 and 1,000,001? Of course, only your business expert will know if approximate or exact answers are required. The decision, though, is crucial because it's the difference between an easy implementation and a hard (costly) implementation. Let's say, for the purposes of this article, that you'll need very close to accurate counts, plus you need to scale a lot . The first step is to pre-calculate the count, and cache the ...