Nuget Perf Problem, Part II–Importing To RavenDB

time to read 7 min | 1397 words

The first part of actually showing how RavenDB can handle the NuGet scenario was to actually get the data to RavenDB. Luckily, NuGet makes the data accessible using OData, so I quickly hacked up the following program:

using (var store = new DocumentStore
        Url = "http://localhost:8080",
        DefaultDatabase = "Nuget"
    string url = "";
    while (true)
        Console.WriteLine("GET {0}", url);
        if (url == null)
        var webRequest = (HttpWebRequest)WebRequest.Create(url);
        webRequest.Accept = "application/json";
        using (var resp = webRequest.GetResponse())
        using (var strema = resp.GetResponseStream())
            url = WritePackagesToRaven(strema, store);

This is imply going to NuGet and asking for the packages in json format. It is very easy for us to work with json data with RavenDB, so that is what we are doing.

The next stage is to actually read the response and write the packages to RavenDB, this is handled here:

private static string WritePackagesToRaven(Stream strema, IDocumentStore store)
    var json = RavenJToken.ReadFrom(new JsonTextReader(new StreamReader(strema)))

    using (var session = store.OpenSession())
        foreach (RavenJObject result in json.Value<RavenJArray>("results"))
            session.Advanced.Defer(new PutCommandData
                    Document = result,
                    Metadata = new RavenJObject
                            {"Raven-Entity-Name", "Packages"}
                    Key = "packages/" + result.Value<string>("Id") + "/" + result.Value<string>("Version")
    return json.Value<string>("__next");

I am not really sure why we have this “d” as the beginning of the json results, but that is what NuGet returns. We iterate over the query results, and write all of them to RavenDB.

You might note that we use the Defer() option, which means that we can rely on the session to handle batching for us and only go to the server once, when we call SaveChanges(). We also set the document metadata to be pretty basic, merely indicating that this should go on the Packages collection. Finally, we set the id to be composed of the package id and the version, resulting in a unique and human readable key for the imported package.

Note that we return the next page location, and continue on working on that in the next page loop.

There is one thing that we need to do, the NuGet data is still highly relational, and quite ugly at times. For example, let us take Tags and Dependencies. Here is how they show up in the raw results:

  • Dependencies: AboditUnits:1.0.4|Autofac.Mef:|ImpromptuInterface:5.6.2|log4net:1.2.11
  • Tags: Ian_Mercer Natural_Language Abodit NLP

That isn’t a really nice way to work with the data, so before we save the results to RavenDB, we modify it slightly.

private static void ModifyResult(RavenJObject result)
    var tags = result.Value<string>("Tags");
    if (tags != null)
        result["Tags"] =
            new RavenJArray(tags.Split(new[] {' ', ',', ';'}, StringSplitOptions.RemoveEmptyEntries));
        result["Tags"] = new RavenJArray();
    var deps = result.Value<string>("Dependencies");
    if (deps != null)
        result["Dependencies"] =
            new RavenJArray(deps.Split(new[] {'|'}, StringSplitOptions.RemoveEmptyEntries)
                                .Select(s =>
                                        var strings = s.Split(':');
                                        return RavenJObject.FromObject(new {Package = strings[0], Version = strings[1]});

Finally, let us take a peek at RavenDB and see how the results look like there:


Now that is much more like it.

On my next post, I am going to show how to do some queries againt this data, which currently have about 66,483 results.