Cogs and Levers A blog full of technical stuff

Conditionally turning on spell checking in vim

Turning on spell check in vim is easy, you just enter the following command at the prompt:

:set spell spelllang=en_au

Of course you substitute in the language code that suits you. So, when you’re tired of all of the highlighting, you can easily turn it off like so:

:set nospell

I use vim for both general text editing (say, like the Markdown document for instance) as well as for code editing. I really don’t want spell checking on for code editing, so I’ve added the following block to my .vimrc to turn it on for Markdown documents only:

autocmd FileType mkd set spell spelllang=en_au

Finally, if you’ve got a word that you’ve spelt incorrectly and you’re stuck on fixing it - put your cursor over the word and hit z=. You’ll get a list of suggestions to help you!

XMonad locks up by my window still works

I’ve just recently installed XMonad on my laptop and am just ironing out some issues that have popped up from time to time. Today, I’d noticed that XMonad had stopped responding to my mod key requests, however the focused application (in my case firefox) was still responding.

After doing some searching around the web, I’ve come across this article which has set me straight. It seems that a pipe that XMonad writes to is full - it just needs to be cleared.

First off, you’ll need to get yourelf to a terminal. If you’re unable to do that (like I was), I went to another virtual terminal all together by using alt+ctrl+f1.

You need to find XMonad’s currently running pid.

$ ps -ef | grep xmonad
michael  1406  1395  0 10:48 ?      00:00:09 /home/michael/.xmonad/xmonad-x86_64-linux

In my case, it’s 1406. You need to take a look at this pid’s file descriptors:

$ ls -l /proc/1406/fd
total 0
lr-x------ 1 michael michael 64 Jun 18 14:10 0 -> /dev/null
lrwx------ 1 michael michael 64 Jun 18 14:10 1 -> socket:[17946]
lrwx------ 1 michael michael 64 Jun 18 14:10 2 -> socket:[17946]
l-wx------ 1 michael michael 64 Jun 18 14:10 3 -> /var/log/slim.log
lrwx------ 1 michael michael 64 Jun 18 14:10 4 -> socket:[18899]
l-wx------ 1 michael michael 64 Jun 18 14:10 5 -> pipe:[18898]

So, in my case here #5 which is the pipe - needs to be cleared. You can do so really quickly just by catting it to screen.

$ cat /proc/1406/fd

XMonad should now be back in the land of the living.

Finally, this should never be a problem just as long as your xmonad.hs is configured with a logHook that will pipe the contents of this stream out.

xmproc <- spawnPipe "xmobar"

. . .
. . .

logHook = dynamicLogWithPP $ xmobarPP
         { ppOutput = hPutStrLn xmproc
         , ppTitle = xmobarColor "green" "" . shorten 50
         }

Using tailable cursors on the MongoDB oplog for realtime changes

MongoDB provides the ability to invoke to retrieve cursors of data that are tailable.

We can exploit this functionality by using on the oplog to provide a trigger-like effect on the Mongo database so that we can respond to changes in real-time.

Using pymongo you can setup a connection to your mongo server’s oplog like so:

tail_opts = { 'tailable': True, 'await_data': True }

# connect to the target mongo server
mongo_url = 'mongodb://localhost:27017'
db = MongoClient(mongo_url).local

# get the latest timestamp in the database
last_ts = db.oplog.rs.find().sort('$natural', -1)[0]['ts'];

while True:
  # prepare the tail query and kick it off
  query = { 'ts': { '$gt': last_ts } }
  cursor = db.oplog.rs.find(query, **tail_opts)
  cursor.add_option(_QUERY_OPTIONS['oplog_replay'])

  try:
     while cursor.alive:
        try:
           # grab a document if available
           doc = cursor.next()
           
           # do something interesting with "doc"

        except StopIteration:
           # thrown when the cursor is out of data, so wait
           # for a period for some more data
           time.sleep(10)
  finally:
     cursor.close()

This constant feedback loop will just keep pumping results down the pipe as they’re seen. You can already see that having an oplog setup on your database is a requirement of this solution. Without this, we have no way to measure the transactions that have executed.

The dictionary tail_opts is passed as the second argument to the find call. You can see that there are a couple of flags set here. The first one is tailable. tailable tells mongo that we want new results as they appear in scope of the cursor. await_data is another option that is set on the cursor to get the server to wait for data as it becomes available.

According to 10gen:

The sequence creates a cursor that will wait for few seconds after returning the full result set so that it can capture and return additional data added during the query

I have wrapped this functionality up into a server of its own (and client library) available from my GitHub repo. mutated-mongo takes the idea in this article and filters out only messages that particular clients have subscribed to. It’s still a work in progress.

How to setup an oplog on a single MongoDB instance

The MongoDB oplog allows you to keep track of changes that have happened on your database in real-time. This is a very useful tool that isn’t offered out of the box with a single server instance. You can follow these steps to enable to oplog on a standalone MongoDB instance.

Un-comment the following lines from your /etc/mongodb.conf file

replSet=rs0
oplogSize=1024

This will give your MongoDB server a replica set identity of rs0 and will allow your oplog to grow to 1024mb. You can tune these parameters to suit.

To complete the process, restart your MongoDB daemon and open a shell. You just need to issue rs.initiate() on the local database:

michael@mongo:~$ mongo
MongoDB shell version: 2.6.1
connecting to: test
> use local
switched to db local
> rs.initiate()
{
   "info2" : "no configuration explicitly specified -- making one",
   "me" : "mongo:27017",
   "info" : "Config now saved locally.  Should come online in about a minute.",
      "ok" : 1
   }
> show collections
me
oplog.rs
startup_log
system.indexes
system.replset

You now have the oplog available to you.

Assembly Syntax Intel & AT&T

This post is just a little cheat sheet for myself on Intel & AT&T syntax.

A useful table mapping some simple instructions between the two syntaxes linked through from the GCC-Inline-Assembly-HOWTO:

Intel Code AT&T Code
mov eax,1 movl $1,%eax
mov ebx,0ffh movl $0xff,%ebx
int 80h int $0x80
mov ebx, eax movl %eax, %ebx
mov eax,[ecx] movl (%ecx),%eax
mov eax,[ebx+3] movl 3(%ebx),%eax
mov eax,[ebx+20h] movl 0x20(%ebx),%eax
add eax,[ebx+ecx*2h] addl (%ebx,%ecx,0x2),%eax
lea eax,[ebx+ecx] leal (%ebx,%ecx),%eax
sub eax,[ebx+ecx*4h-20h] subl -0x20(%ebx,%ecx,0x4),%eax

Some important points to note:

  • Source and destinations are flipped in opcodes.
    • Intel is dest, src
    • AT&T is src, dest
  • AT&T decorates registers and immediates
    • Registers are prefixed with a “%”
    • Immediates are prefixed with a “$”. This applies to variables being passed in from C (when you’re inline).
  • Intel decorates memory operands to denote the operand’s size, AT&T uses different mnemonics to accomplish the same.
  • Intel syntax to dereference a memory location is “[ ]”. AT&T uses “( )”.