DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Nuget Perf Problem, Part II–Importing To RavenDB

Oren Eini user avatar by
Oren Eini
·
Aug. 30, 12 · Interview
Like (0)
Save
Tweet
Share
2.40K Views

Join the DZone community and get the full member experience.

Join For Free

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"
    }.initialize())
{
    string url = "https://nuget.org/api/v2/packages";
    while (true)
    {
        console.writeline("get {0}", url);
        if (url == null)
            break;
        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)))
        .value<ravenjobject>("d");


    using (var session = store.opensession())
    {
        foreach (ravenjobject result in json.value<ravenjarray>("results"))
        {
            modifyresult(result);
            session.advanced.defer(new putcommanddata
                {
                    document = result,
                    metadata = new ravenjobject
                        {
                            {"raven-entity-name", "packages"}
                        },
                    key = "packages/" + result.value<string>("id") + "/" + result.value<string>("version")
                });
        }
        session.savechanges();
    }
    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:2.5.2.830|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));
    }
    else
    {
        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]});
                                    }));
    }
    result.remove("__metadata");
}

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

image

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.

NuGet

Published at DZone with permission of Oren Eini, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Spring Cloud: How To Deal With Microservice Configuration (Part 1)
  • Iptables Basic Commands for Novice
  • What Should You Know About Graph Database’s Scalability?
  • Java Development Trends 2023

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: