Optimize all the queries!

While digging more into the code called “Moebooru” which was forked from “Danbooru”, I noticed this:

def self.included(m)
  m.extend(ClassMethods)
  m.after_create :increment_count
  m.after_destroy :decrement_count
end

def increment_count
  connection.execute("update table_data set row_count = row_count + 1 where name = 'users'")
end

def decrement_count
  connection.execute("update table_data set row_count = row_count - 1 where name = 'users'")
end

Counting takes ages, right. Except it is not. I’ve done this, yes, but on a table with 10+ millions of data (this one has ~400k in mainline danbooru), with multiple data inserted (this one got, uh, one every other week?) and queried every second (see below), and with the required count method not a simple select count(1) on some_table (which is what the example above used for).

The best part? It’s only used once, when user registers:

def set_role
  if User.fast_count == 0
    self.level = CONFIG["user_levels"]["Admin"]
  elsif CONFIG["enable_account_email_activation"]
    self.level = CONFIG["user_levels"]["Unactivated"]
  else
    self.level = CONFIG["starting_level"]
  end

  self.last_logged_in_at = Time.now
end

Moebooru Update

I haven’t been able to work on Moebooru for most of this month but I got to work on it this weekend. At ~30 changes, it went from -alpha to -beta.2 after receiving good amount of tests.

Summary of changes (more or less):

  • Most instances of RAILS_ENV and RAILS_ROOT have been replaced with newer Rails.env/Rails.root
  • Better Javascript handling in Development mode by moving cached Javascript from application.js to cached/all.js
  • Removed AssetCache modification library
  • Replaced HTML5Sanitizer’s html5sanitize with Rails’ built-in sanitize (because the former doesn’t work in 1.9)
  • Fixed Note Editor’s “History” button
  • Fixed Comment page’s pagination
  • Fixed Tag Relation search function
  • Updated “hack” for UTF-8 handling in Rails 2.3 with Ruby 1.9
  • Merged updates from moe branch (the one currently used in yande.re)
  • Reverted change which put class definition in <html> tag as it currently required by Javascript

zpool hourly status check

I’m setting up cron job for a storage server using ZFS. There’s zpool status -x but it returns “all pools are healthy” (or “no pools available”) on no error and prints errors to stdout (instead of stderr), rendering it annoying for cron job.

#!/bin/sh

set -e
set -u

zstatus="`zpool status -x 2>&1`"
case "${zstatus}" in
  "all pools are healthy"|"no pools available")
    return 0
  ;;
  *)
    printf "%s\n" "${zstatus}" >&2
    return 1
  ;;
esac

Put it in a file (e.g. /root/bin/zpool-status-cron), make it executable, and add it to crontab.

Unofficial Sentai Akibaranger

A metaseries on Super Sentai. In short, they opened tvtropes and jammed as many tropes as they can into the series.

Head over to Over-Time to grab the releases (currently 2 episodes). Don’t forget to read the notes, too! I wish more fansubbers do this for reference-heavy series.

I’m pretty sure “Nijiyome” there is abbreviation of “nijigen yome” (2D waifu).

Supipara demo movie

Demo movie for Supipara – minori’s latest game/series is out. No high quality video in sight though. And the animation isn’t by Shinkai – just like eden (and I suspect we’ll get no more minori/Shinkai combo) but still pretty enough so it’s okay.

As expected, there’s no male in sight. The game is all-ages though so we won’t get some actual yuri action either :(

[ Link to video ]

Accel World

I wonder what they are...

One of the hyped series for this season, spring 2012. Looks quite good. With Sunrise as production studio, I can safely expect stable animation over the series. No clear indication on how long this will be though considering the amount of its LN, I guess half-assed ending should be expected.

And yes, Black Snow Princess (aka Kuroyukihime) makes up 90+% of reason I’m watching this as of currently :>