All posts in Development

Excluding ID field doesn’t work with FindAndModify

While playing with incremental IDs with Mongo DB the other day, I stumbled across a bug in mongodb.

Consider the following command:

db.sequence.findAndModify({
     query: {"_id": "customer"},
     update : {$inc : {"seq":1}},
     fields:{"_id":0},
     upsert:true,
     new:true})

Notice the fields:{"_id":0}, part (highlighted)

According to the documentation, this should prevent the id from being returned, however when I try and execute this command, I get this error:

Uncaught exception: findAndModifyFailed failed: "exception: assertion c:\\builds\laves\\mongo\\windows_64bit_v1.6\\mongo\\db\\../bson/bsonbjbuilder.h:115"

image

I opened a ticket with their issue tracking system (Jira)

After a day or so, I had a response that this was caused by a limitation to how FindAndModify was implemented.

Luckily, they’re planning on re-implementing this feature (see this ticket) which should remove this restriction.

In the meantime, the method of auto-incrementing ID’s described in my previous post will still work, but it will return the _id as well.

In a future release of mongodb, we’ll be able to strip this out, and make it even speedier!

MongoDB – Incremental IDs

I’ve been reading a lot recently on MongoDB and the use of incrementing an ID

This article offers an in depth look: http://shiflett.org/blog/2010/jul/auto-increment-with-mongodb

Taking this a little further, and from reading the findAndModify documentation I put together the following:

  db.sequence.findAndModify({
    query: {"_id": "customer"},
     update : {$inc : {"seq":1}},
     upsert:true,
     new:true})

Here is what this command does:

  • Finds (or creates) the “sequence” collection
  • Gets the document with id “customer”
  • Increments the value of “seq” by 1
  • If this document doesn’t exist, it creates it (upsert:true)
  • Returns the new value of “seq”

Of course, in production, I’d shorten “seq” to just “s” or something like that, for even speedier responses!

The advantages of this are you don’t need to initialise your “sequence” collection first, or check if your document exists first. If it doesn’t exist, it will create it!

Important Note

At the time of writing, unfortunately, the whole document (ie- id and seq) is returned with this operation

According to the documentation, we should be able to specify “fields” to remove the id from the returned doc, however this doesn’t seem to work.

I have opened a ticket on MognoDB Jira for this.

See this post for more information.

Joe Blogs Wrapper V2 – Help required

I’m on the verge of being able to release a second, much improved version of the Joe Blogs WordPress & MetaWeblog API Wrapper.

Some very brief details below, and a cry for help (or several cries!)

New Features / Improvements

Friendlier API
Instead of having to deal with the XML-RPC interfaces directly, I’ve added a “friendly” layer of classes on top.
The WordPressWrapper class has helper methods, with friendly properties, for doing things like uploading files etc…

  • More examples
    The sample project (console application) included in the project demonstrates all methods and features of the library.
  • Better documentation
    XML RPC Comments on all the public properties and methods

Help required

I need help with some of the following, both for this version, and subsequent versions

  • PHP WordPress Re-install script
    Script (configured as CRON job) to “refresh” test install of WordPress (every couple of hours or something)
  • Unit Tests
    Need better code coverage., and more integration tests
    I’m not TDD expert, so unfortunately, a lot of this will be retro-fitting unit tests into the current code.
  • WordPress Plugin development
    I plan to release an “extended” version of XML RPC – with some enhanced methods in, for use with JoeBlogs – for example “GetRecentPosts”
  • ASP.Net Demo Project
    Eventually, and ASP.net MVC “blog” project, running from WordPress – for no other reason than “it can be done”
  • Moving project to GitHub
    See below
  • General code improvement
    Refactoring, performance enhancements, etc…
  • Logo Designed
    ‘cos every cool open source project has to have a cool logo, right?
  • Website
    I’ve dipped my hand into my pocket, and bought joeblogswrapper.org (under £7 – GoDaddy)
    Will have a site dedicated to it on there soon. Obvious CMS choice would be WordPress, but if anyone can think of a better option for Wiki style pages, I’m all ears
    (Media wiki..? What are the benefits?)

Mailing List

I’ve created a Google Group – http://groups.google.com/group/joeblogs/
Please, sign up on there for announcements (I’ll announce when the next versions of the wrapper are available)

GitHub

I really like the idea of moving this project over to GitHub.

Problem is, I have absolutely no experience using Git.

I’ve tried following the instructions, and got as far as some files imported into my repository.. but I can’t get it to work, or commit my changes.

If anyone would like to offer their hand-holding expertise on getting this moved over (and training me on committing files etc…) then I’ll gladly move it to GitHub, which long term, I believe could be beneficial to the overall development of the project.

Get in touch

If you’d like to help with ANY of the above (or anything I’ve missed for that matter) please get in touch, or use the Google Group

Getting W3 Cache Working on shared hosting

I wanted to install w3 cache on shared hosting

After uploading the plugin to /wp-content/plugins/ and trying to activate it, I got an error asking me to set CHMOD 777 on /wp-content/w3tc

I tried running the command given, however it didn’t work.
I also tried manually creating the directory, setting 777 on it, and re-activating the plugin.
This, however, didn’t work, as the plugin deleted my newly created directory when it was “activating” and threw the same error.

I finally resolved this particular error by ensuring the site was not running in safe mode.
For this, I had to email the support guys with my particular hosting company (ehosting.com)

Trying to activate it again, I now got this error:

w3CacheInstallError1

/var/www/vhosts/<domain>/httpdocs/wp-config.php could not be written, please edit config and add:
define(‘WP_CACHE’, true); before require_once(ABSPATH . ‘wp-settings.php’);

So, I opened up wp-config, and made the change (around line 60)

define(‘WP_CACHE’, true);

Re-activated the plugin, and it worked!

C# – String.Concat vs String.Join

I had a quick Google search for a comparison between string.concat and string.join, but I couldn’t find anything.

Say you want to construct the sentence “The quick brown fox jumps over the lazy dog”

This is comprised of 9 words, 8 spaces.

Using string.concat:

public void CreateSentanceUsingStringConcat()
{
    string space = " ";
    string the = "the";
    string quick = "quick";
    string brown = "brown";
    string fox = "fox";
    string jumps = "jumps";
    string over = "over";
    string lazy = "lazy";
    string dog = "dog";

    var result = string.Concat(the, space, quick, space, brown, space, fox, space, jumps, space, over, space, the, space, lazy, space, dog);
}

Using string.join

public void CreateSentanceUsingStringJoin()
{
    var result = string.Join(space, the, quick, brown, fox, jumps, over, the, lazy, dog);
}

With string.concat, we must explicitly add the separator (in our case, space) between each string.

String.join however, allows us to specify the separator as the first parameter.

Performance

I wrote a small benchmark test on the two (see bottom of this post for code)

We can see string.concat consistently performs better

 string.join - 516ms / string.concat - 329ms

Benchmark Code:

class Program
{
    const int m = 1000000;

    const string space = " ";
    const string the = "the";
    const string quick = "quick";
    const string brown = "brown";
    const string fox = "fox";
    const string jumps = "jumps";
    const string over = "over";
    const string lazy = "lazy";
    const string dog = "dog";

    const string expected = "the quick brown fox jumps over the lazy dog";

    static void Main()
    {
        Console.WriteLine("Pass #1");
        Go();
        Console.WriteLine();
        Console.WriteLine("Pass #2");
        Go();
        Console.WriteLine();
        Console.WriteLine("Pass #3");
        Go();

        Console.Read();
    }

    private static void Go()
    {
        Stopwatch s1 = Stopwatch.StartNew();

        for (int i = 0; i < m; i++)
            string.Concat(the, space, quick, space, brown, space, fox, space, jumps, space, over, space, the, space, lazy, space, dog);

        s1.Stop();

        Stopwatch s2 = Stopwatch.StartNew();
        for (int i = 0; i < m; i++)
            string.Join(space, the, quick, brown, fox, jumps, over, the, lazy, dog);

        s2.Stop();

        Console.WriteLine("string.join - {0}", s1.ElapsedMilliseconds);
        Console.WriteLine("string.concat- {0}", s2.ElapsedMilliseconds);
    }
}