Technology

I am going through the Type Aloud code trying to improve queries, implement some fragment caching, and generally clean anything up that I can before flipping the proverbial switch on this bad boy. I came across something a few weeks ago that, combined with the awesomeness that is Arel, makes my life a hell of a lot easier writing queries.

These named scopes have been around in Rails for awhile now, but as always I am coming late to the party. Because Type Aloud contains “draft mode” where stories and chapters can both be in an unpublished state I need to be able to call only the chapters and here is a quick, simple way to do that on the model.

scope :published, lambda {
   joins(:story).where(:disabled => false, :stories => {:disabled => false}).includes(:story, :user)
}

The named scope will filter out any chapters that are not published or any chapters in stories that are not published. This is very important when displaying, let’s say, a random chapter for reading that may not have yet been published in an existing story. The last statement at the end there will eager load the story (and user) because there really is never an instance where I don’t want the story (user) records associated with this. Here are the queries that are executed.
And here and the queries that are executed from this little snippet.

SELECT `chapters`.* FROM `chapters` INNER JOIN `stories` ON `stories`.`id` = `chapters`.`story_id` WHERE (`chapters`.`disabled` = 0) AND (`stories`.`disabled` = 0)
SELECT `stories`.* FROM `stories` WHERE (`stories`.`id` = 1)
SELECT `users`.* FROM `users` WHERE (`users`.`id` = 2)

And of course, this is what the signature looks like from the controller.

@chapter = Chapter.published.limit(1).first

For this particular operation it resulted in a 0.5ms improvement in time spent during the Active Record phase. That may not sound like a lot, but it actually is a 19 percent time optimization. Good stuff!

Mar
01
Posted by JB at 11:06 pm

Well, tomorrow is officially the day that Apple is going to update the iPad. Many of these tech blogging sites are wondering if we’re going to possibly see a Thunderbolt (Light Peak) port on the iPad, or will we be seeing a SD card reader, perhaps dual cameras or how about that brand spanking new Retina display that the iPhone has been sporting for over a year? All of this shit would be absolutely great, but as someone that doesn’t own an iPad (and doesn’t plan on buying one in the near future, sans possibly developing applications) I am looking forward to one thing, and one thing only: the possibility that tomorrow we may finally see an iTunes cloud streaming service.

It is a widely known fact that Apple has been purchasing some real estate in North Carolina to house a massive data center to what they’ve told investors is for their expanding cloud services division. For the past couple of years Apple has been trying to get people to pay $100 per year for MobileMe when you can get the same services, with nearly the same Mac integration, for absolutely free from Google. Apple told its investors that this new data center is going to be strictly for MobileMe and iTunes; pray tell what exactly does iTunes need a billion dollar data center for on the east coast? MobileMe sure as hell doesn’t need it; the service is crap, and most people only pay for it because they forget to take it off their credit card after the first three months they’ve owned a new Mac.

Another point of interest is that Apple purchased Lala, a music streaming startup, and has done absolutely nothing with the service to date. Steve Jobs sure as hell does not buy companies unless he plans on using their technologies, because otherwise they would just roll their own. You can bet your dollars that tomorrow we’re going to see three things: a brand new iPad, without a Retina display, with two cameras, a bumped up processor and memory; a new take on the MobileMe offering, most likely free and including some kind of premium services ala Dropbox storage; and a cloud streaming service powered buy the Lala purchase.

If we’re lucky iTunes catalog streaming from the cloud. That would absolutely make my year. And I am about to launch a a website myself!

I have been running into a problem recently with my development over at Type Aloud; some of my column data types differ from MySQL (my development database) and PostgreSQL (my production database). I store two copies of the story in the database: the first is the “human readable” version that uses the Markdown syntax and the second is the HTML version of that syntax so it is easier to fragment cache and display when someone is viewing the chapter. This problem is particularly irritating because when it first happened in MySQL the text was merely truncated, but in PostgreSQL it correctly does not allow the insertion into the table if the text is too long.

For the past couple of days I have been doing some light searching trying to figure out a way to have a RDBMS specific database migration, and I once I did some pecking in the ActiveRecord::Migration class I was able to figure this out pretty easily. I figured that I would share it below since it took me a little while to find it. Now normally I do not believe in migrations being platform specific, but in this case because I wanted to actually have the application work on two types of platforms (in this case, two RDBMS).

class AddLongtextToPostgresql < ActiveRecord::Migration
  def self.up
    case ActiveRecord::Base.connection.adapter_name
    when 'PostgreSQL'
      execute "CREATE DOMAIN longtext as text"
      execute "ALTER TABLE chapters ALTER COLUMN html TYPE longtext"
      execute "ALTER TABLE chapters ALTER COLUMN body TYPE longtext"
    else
      puts "This migration is not supported on this platform."
    end
  end
 
  def self.down
  end
end

When I started doing some serious programming about ten years ago this new buzzword started to get passed around. When I was browsing the isles at Barnes and Noble every other new-age programming book would pop up describing how a service oriented architecture would be the future of software development. How it would bridge the gaps between platform dependent code and allow us to use cheap hardware when necessary to serve enterprise clients (and other services). It promised a more modular approach to programming, encapsulating functionality in a bundled executable called a “service” which had a schema that it responded to, but it via XML, SOAP or some other protocol.

Whenever I dream up a project I tend to use it as a driver for learning a new software library or language. When reading first about the service oriented architecture paradigm the Java programming stack was usually the king of the hill when it came to discussions. Even today, after Oracle purchased Sun and basically started to put nails in the proverbial Java coffin, it still reminds a leader in enterprise service oriented design. I am a C/C++ programming, self taught and currently by trade, but I would be a fool to ignore the advantages that this platform offered. So as any good project manager I read up a lot on Java (at the time) and learned as much as I could about SOA practices.

A few years ago I started attending college for the computer science discipline and Java was the primary language that was being taught everywhere. Although, until some of my later entrepreneurial projects, I heard not a word about SOA practices in any of my programming or software engineering courses. A few years ago the topic of the “Cloud” became all the rage; people were making money off writing books, companies such as Microsoft (and even Sun) were working on Cloud projects to help developers and Amazon started selling specialized hosting geared towards metered bandwidth, CPU and memory usage. I began asking myself: what is different about software in the Cloud?

Well, the truth is, software that is written to be able to take advantage of all the regularly toted features of the Cloud such as being able to elastically adjust your application’s presence on demand, is just the service oriented architecture approach of designing software taken to the limit. The actual truth of the matter is that any piece of software that is designed to be fully “cloud compliant” is most likely some kind of service, be it a web application using the HTTP protocol or a Glassfish Java application stack using XML or SOAP as communication protocol. When designed properly a service oriented platform stack should be able to scale horizontally much more efficiently than it does vertically.

What the is the difference between scaling out vertically and horizontally? When an application scales out vertically this means adding more memory, CPU power, hard-drive space, etc, to an existing system that is servicing clients. Scaling out vertically is merely adding additional computing nodes. This means everything to a distributed architecture; as soon as the node touches the grid it should be servicing clients. But as I said earlier in this essay the idea of SOA practices have existed long before the marketing engine of Salesforce and Amazon have trademarked the word cloud.

The principle of the cloud is the ability to scale out vertically on demand. If your application is about to get hit by a swarm of geeks from Slashdot any normal application running on cheap Linux metal in a data center would likely cripple under the additional CPU, bandwidth and memory requirements put in place by this surge of new clients. An application that is able to be scaled out can elastically grow and shrink on demand, servicing the needs of its client base at any single point in time.

But what does that mean for business? You only pay a metered rate for bandwidth and CPU cycles on the amount that you’re using at any given point in time. Your traffic at 2AM in the morning on a Thursday likely is not anywhere near the traffic that you have at peak Internet browsing hours. Why should you be paying those massive hosting fees when you’re really only using your hardware to the limit about three to four hours a day?

This is the principle behind “the cloud,” and if you couple this with a logical service oriented application design (see: distributed) your business will much more agile and cost effective in both the peak and downtime hours.

One of the features that was omitted from the Type Aloud beta launch earlier this year was the ability to work on unpublished stories and chapters. My testing period in early December showed that the workflow of most of the users was to develop their story in some offline editor such as Word or Pages first, and then pasting in the finished product into the text area making slight modifications when needed for markup.

A few friends brought this to my attention and this afternoon I started cooking up some changes to make it possible to “publish” and “depublish” stories and chapters from the website. Any story that is created first begins in an unpublished state (draft mode) which needs to be explicitly toggled by the owner before it goes live on the site. At any point in time the user has the option to take their story offline making it unpublished again. It should then immediately become unavailable across the site. I am working on making sure that comments and subscriptions correctly disappear when a story becomes unpublished as well.

One good thing is that I do not plan on deleting subscriptions and comments on stories (chapters) when someone decides to depublish a story. This is assuming that the story will eventually come back because it was not deleted. When a story is deleted, the chapters, comments and subscriptions will follow.

Jan
17

I have been waiting a very long time to be able to say these words. For the past couple of months I have had a bottle of champagne sitting around, waiting, for me to finish the site enough to launch it to the public as a beta. I am glad, honored and relieved that the day is finally here that I can crack open that bottle and drink it (actually, finishing up the last bit of it now). My close group of friends have been pounding away at the site already reporting bugs that I promise I will get on top of as soon as I can.

Type Aloud has been a dream of mine for at least four years now. I envisioned a writing and reading community where people will not only be able to share their stories (and poems) but also get collaborative feedback for both editing, and hopefully, print publishing. Some of the most intriguing writing I have read came from people that were not published professionally. I look forward to being able to read (and write!) much more than I have in the past with this service. My primary goal is not to be profitable: Type Aloud is a service that I want to use myself. I am a lucky man if I am able to break even.

If you have a couple of minutes, or even better some writings that you want published, please take the time and head on over to the open beta page and sign up for a free account. Remember that all work posted to Type Aloud you retain ownership to. In the coming days I will have a proper copyright agreement up on there which will basically amount to you giving us the permission to publish it on the website.

There is much, much, more to be done, but I feel as if the first hurdle has already been past.

Jan
06
Posted by JB at 11:56 pm

Last year I wrote a little blog post about my New Year’s resolutions. After the beginning of the year getting off to a rocky start I was going to forgo the whole official post thing, but I feel that tonight may be the night that I needed to write a little bit. I only achieved some of the items off of last year’s list. Instead of recapping my failures for the year I think I am just going to dive right into the good stuff.

  1. I will travel outside of the United States.

    Last year I made a note to take a proper vacation. This is something that I do not believe that I achieved, but there are reasons for that. So my first resolution is that I will travel outside of the United States this year. I do not have an official plan on when (or where) but some of the ideas that are floating around include either Mexico or, better, Europe.

  2. I will finish what I start.

    Last year saw many beginnings for me. I started a new career with a great bunch of individuals, I got nearly done with my writing community and there are many other things that I could go into. This year I want to finish some of the short-term projects that I have started. My plan for the first month of the year is to get an initial beta up of my my writing community, Type Aloud, which has been in the making for several years now.

  3. I will find someone that understands and accepts me.

    I enjoy writing software. I talk about technology, watch movies, read and write, and cook food. Any person that comes into my life needs to completely understand and accept me, for me. No substitutes.

  4. I will take the GRE and apply to graduate school.

    I have not decided yet what my major will be, but while doing that I need to prepare by taking the proper tests. Once I decide where and what I want to study I need to (obviously) apply and get accepted. The short list includes NYU and Columbia University in New York City.

  5. I will continue to eat healthier and live a more active life.

    Last year I did not achieve my weight loss goal, but I did get half-way there. I stopped eating junk food, started eating more fruit and vegetables, and began cooking meals at home more often than I had before. I also started packing my own lunch a few days a week to save myself some money.

  6. Write more words, play more video games and enjoy life.

    My passions include many things. Last year I said that I wanted to begin writing a book. For me, the first step in that is getting Type Aloud out of the door. I have several ideas for books (and short stories) that I want to start penning as soon as I have a website to write them on! Some of my other modes of relaxation include playing video games. I can probably count the games that I played with friends on one hand. This has to change.

  7. Drink more beer and open source more software.

    For some reason last year I drank less beer (alcohol in general) and wrote less software, especially open source. These two things I want to change, and I feel that they’ll both contribute to an overall, better, me. All of you should hold me to it.

  8. I will find someone who will stay here, and not move away.

    I don’t need to explain this. Anyone special in my life needs to be here, in my life.

I think that’s the list. Right now I am going to head to bed and hope that there isn’t too much snow here in the morning so I can get to work and end the week on a high note. Peace and happy New Year everyone.