Ayende @ Rahien

Hi!
My name is Oren Eini
Founder of Hibernating Rhinos LTD and RavenDB.
You can reach me by phone or email:

ayende@ayende.com

+972 52-548-6969

, @ Q c

Posts: 10 | Comments: 37

filter by tags archive

Implementing a document database: simple queries

time to read 3 min | 457 words

And now this passes:

public class PerformingQueries
{
    const string query = @"
var pagesByTitle = 
from doc in docs
where doc.type == ""page""
select new { Key = doc.title, Value = doc.content, Size = (int)doc.size };
";

    [Fact]
    public void Can_query_json()
    {
        var serializer = new JsonSerializer();
        var docs = (JArray)serializer.Deserialize(
                new JsonTextReader(
                    new StringReader(
                        @"[
{'type':'page', title: 'hello', content: 'foobar', size: 2},
{'type':'page', title: 'there', content: 'foobar 2', size: 3},
{'type':'revision', size: 4}
]")));
        var compiled = new LinqTransformer(query, "docs", typeof(JsonDynamicObject)).Compile();
        var compiledQuery = (AbstractViewGenerator<JsonDynamicObject>)Activator.CreateInstance(compiled);
        var actual = compiledQuery.Execute(docs.Select(x => new JsonDynamicObject(x)))
            .Cast<object>().ToArray();
        var expected = new[]
        {
            "{ Key = hello, Value = foobar, Size = 2 }",
            "{ Key = there, Value = foobar 2, Size = 3 }"
        };

        Assert.Equal(expected.Length, actual.Length);
        for (var i = 0; i < expected.Length; i++)
        {
            Assert.Equal(expected[i], actual[i].ToString());
        }
    }
}

You wouldn’t believe how much effort it took, and all in all, implementing this is about 500 lines of code or so.


Comments

yug
yug

So do we take it this is a sponsored effort or could you not help yourself ;-)

I take it the comment on effort was sarcasm? Regardless you are a legend.

James Newton-King

I'm hoping the effort was parsing and creating the query and not working with Json.NET :)

Ayende Rahien

James,

I have exactly 2 problems with Json.NET

1) I don't like the way I need JsonTextReader -> StringReader just to deserialize a string

2) R# has a bug that made me thing that I cannot access the Value property of JProperty.

2) is not your fault.

Other than that, it is lovely.

Rafal

Ayende, was the effor related to supporting Linq? Would it be the same if you implemented the same functionality with Boo / rhino dsl?

Ayende Rahien

Rafal,

Boo would give me different syntax, not as set based as this.

The effort was mainly in how to take the query and make it executable

James Newton-King

Ayende you could use JsonConvert.DeserializeObject without any type parameter to deserialize the string to a JArray. That would let you do it in one line and save having to create a JsonSerializer, JsonTextReader and StringReader.

Nathan

What are you using to parse / compile the LINQ? Are you doing it by hand, using a grammar, or just using CodeDom?

Ayende Rahien

Nathan,

A lot of magic and the C# compiler

Nathan OSullivan

One thing I'm not getting in this series is what is the persistence strategy for your views?

Chris Martin

...and it looks like ass.

Sorry but, you're fired! ;)

Comment preview

Comments have been closed on this topic.

FUTURE POSTS

  1. Production postmortem: The case of the memory eater and high load - 10 hours from now
  2. Production postmortem: The case of the lying configuration file - about one day from now
  3. Production postmortem: The industry at large - 2 days from now
  4. The insidious cost of allocations - 3 days from now
  5. Find the bug: The concurrent memory buster - 4 days from now

And 4 more posts are pending...

There are posts all the way to Sep 10, 2015

RECENT SERIES

  1. Find the bug (5):
    20 Apr 2011 - Why do I get a Null Reference Exception?
  2. Production postmortem (10):
    14 Aug 2015 - The case of the man in the middle
  3. What is new in RavenDB 3.5 (7):
    12 Aug 2015 - Monitoring support
  4. Career planning (6):
    24 Jul 2015 - The immortal choices aren't
View all series

Syndication

Main feed Feed Stats
Comments feed   Comments Feed Stats