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
  1. DZone
  2. Coding
  3. JavaScript
  4. RavenDB Awesome Feature of the Day: Formatted Indexes

RavenDB Awesome Feature of the Day: Formatted Indexes

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

Join the DZone community and get the full member experience.

Join For Free

there is a chance that you’ll look at me strangely for calling this the “feature of the day”. but that is actually quite a important little feature.

here is the deal, let us say that you have the following index

public class orders_search : abstractindexcreationtask<order, orders_search.reduceresult>
{
    public class reduceresult
    {
        public string query { get; set; }
        public datetime lastpaymentdate { get; set; }
    }

    public orders_search()
    {
        map = orders => from order in orders
                        let lastpayment = order.payments.lastordefault()
                        select new
                        {
                            query = new object[]
                            {
                                order.firstname, 
                                order.lastname, 
                                order.ordernumber, 
                                order.email, 
                                order.email.split('@'),
                                order.companyname,
                                order.payments.select(payment => payment.paymentidentifier),
                                order.licenseids
                            },
                            lastpaymentdate = lastpayment == null ? order.orderedat : lastpayment.at
                        };
    }
}

and you are quite happy with it. but that is the client side perspective. we don’t have any types on the server, so you can’t just execute this there. instead, we send a string representing the index to the server. that string is actually the output of the linq expression, which looks like this:

image

this is… somewhat hard to read, i think you’ll agree. so we had some minimal work done to improve this, and right now what you’ll get is (you’ll likely see it roll off the screen, that is expected):

docs.orders
    .select(order => new {order = order, lastpayment = order.payments.lastordefault()})
    .select(__h__transparentidentifier0 => new {query = new system.object []{__h__transparentidentifier0.order.firstname, __h__transparentidentifier0.order.lastname, __h__transparentidentifier0.order.ordernumber, __h__transparentidentifier0.order.email, __h__transparentidentifier0.order.email.split(new system.char []{'@'}), __h__transparentidentifier0.order.companyname, __h__transparentidentifier0.order.payments
    .select(payment => payment.paymentidentifier), __h__transparentidentifier0.order.licenseids}, lastpaymentdate = __h__transparentidentifier0.lastpayment == null ? __h__transparentidentifier0.order.orderedat : __h__transparentidentifier0.lastpayment.at})

this is still quite confusing, actually . but still better than the alternative.

as i said, it seems like a little thing, but those things are important. an index in its compiled form that is hard to understand for a user is a support issue for us. we needed to resolve this issue.

the problem is that source code beautifying is non trivial. i started playing with parsers a bit, but it was all way too complex. then i had an affiany. i didn’t actually care about the code, i just wanted it sorted. there aren’t many c# code beautifiers around, but there are a lot for javascript.

i started with the code from http://jsbeautifier.org/ , which rekna anker had already ported to c# . from there, it was an issue of making sure that for my purposes, the code generated the right output. i had to teach it c# idioms such as @foo, null coalescent and lambda expressions, but that sounds harder than it actually was. with that done, we go this output:

docs.orders.select(order => new {
    order = order,
    lastpayment = order.payments.lastordefault()
}).select(__h__transparentidentifier0 => new {
    query = new system.object[] {
        __h__transparentidentifier0.order.firstname,
        __h__transparentidentifier0.order.lastname,
        __h__transparentidentifier0.order.ordernumber,
        __h__transparentidentifier0.order.email,
        __h__transparentidentifier0.order.email.split(new system.char[] {
            '@'
        }),
        __h__transparentidentifier0.order.companyname,
        __h__transparentidentifier0.order.payments.select(payment => payment.paymentidentifier),
        __h__transparentidentifier0.order.licenseids
    },
    lastpaymentdate = __h__transparentidentifier0.lastpayment == null ? __h__transparentidentifier0.order.orderedat : __h__transparentidentifier0.lastpayment.at
})

and this is actually much better. still not good enough, mind. we can do better than that. it is a simple change:

docs.orders.select(order => new {
    order = order,
    lastpayment = order.payments.lastordefault()
}).select(this0 => new {
    query = new system.object[] {
        this0.order.firstname,
        this0.order.lastname,
        this0.order.ordernumber,
        this0.order.email,
        this0.order.email.split(new system.char[] {
            '@'
        }),
        this0.order.companyname,
        this0.order.payments.select(payment => payment.paymentidentifier),
        this0.order.licenseids
    },
    lastpaymentdate = this0.lastpayment == null ? this0.order.orderedat : this0.lastpayment.at
})

and now we got to something far more readable smile .

Awesome (window manager) Strings Data Types Form (document) Parser (programming language) JavaScript

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

  • API Design Patterns Review
  • Load Balancing Pattern
  • TDD: From Katas to Production Code
  • Top 10 Best Practices for Scaling Your Application: Expert Tips for Optimizing Performance and Ensuring Reliability

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: