Reviewing RavenBurgerCo: What Could be Improved?
Join the DZone community and get the full member experience.
Join For Freethere are two things that i would change in the ravenburgerco sample app.
the first would be session management, i dislike code like this:
i would much rather do that in a base controller and avoid manual session management. but that is most a design choice, and it ain’t really that important.
but what is important is the number of indexes that the application uses. we have:
- locationindex
- deliveryindex
- drivethruindex
and i am not really sure that we need all three. in fact, i am pretty sure that we don’t. what we can do is merge them all into a single index. i am pretty sure that the reason that there were three of them was because there there was a bug in ravendb that made it error if you gave it a null wkt (vs. just recognize this an a valid opt out). i fixed that bug, but even with that issue in place, we can get things working:
public class spatialindex : abstractindexcreationtask<restaurant> { public spatialindex() { map = restaurants => from restaurant in restaurants select new { _ = spatialgenerate(restaurant.latitude, restaurant.longitude), __ = restaurant.drivethruarea == null ? new object[0] : spatialgenerate("drivethru", restaurant.drivethruarea), ___ = restaurant.deliveryarea == null ? new object[0] : spatialgenerate("delivery", restaurant.deliveryarea) }; } }
and from then, it is just a matter of updating the queries, which now looks like the following:
getting the restaurants near my location (for eat in page):
return session.query<restaurant, spatialindex>() .customize(x => { x.withinradiusof(25, latitude, longitude); x.sortbydistance(); }) .take(250) .select( ... );
getting the restaurants that deliver to my location (delivery page):
return session.query<restaurant, spatialindex>() .customize(x => x.relatestoshape("delivery", point, spatialrelation.intersects)) // spatialrelation.contains is not supported // spatialrelation.intersects is ok because we are using a point as the query parameter .take(250) .select( ... ) ;
getting the restaurants inside a particular rectangle (map page):
return session.query<restaurant, spatialindex>() .customize(x => x.relatestoshape(constants.defaultspatialfieldname, rectangle, spatialrelation.within)) .take(512) .select( ... );
note that we use defaultspatialfieldname, instead of indexing the location twice.
and finally, getting the restaurants that are applicable for drive through for my route (drive thru page):
return session.query<restaurant, spatialindex>() .customize(x => x.relatestoshape("drivethru", linestring, spatialrelation.intersects)) .take(512) .select( ... );
and that is that.
really great project, and quite amazing, both client & server code. it is simple, it is elegant and it is effective. well done simon!
Published at DZone with permission of Oren Eini, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments