Running MRuby in a Database - ArangoDB - RuPy 2012

  • 890 views
Uploaded on

Slides for Frank Celler's talk on Mruby as alternative to server-side JavaScript in ArangoDB, a nosql database, in Brno in November 2012. …

Slides for Frank Celler's talk on Mruby as alternative to server-side JavaScript in ArangoDB, a nosql database, in Brno in November 2012.

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
890
On Slideshare
0
From Embeds
0
Number of Embeds
2

Actions

Shares
Downloads
8
Comments
0
Likes
3

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Running MRuby in a database Totally awesome, useful or just another pointless approach? Frank Celler, triAGENS, Cologne RuPy 2012, Brno 2012-11-16 www.arangodb.org (c) f.celler@triagens.deMittwoch, 14. November 12
  • 2. Agenda NoSQL database ArangoDB Use-Cases for actions mruby V8 or mruby? www.arangodb.org (c) f.celler@triagens.deMittwoch, 14. November 12
  • 3. NoSQL Databases Key/Value Store Document (Object) DB Graph Database www.arangodb.org (c) f.celler@triagens.deMittwoch, 14. November 12
  • 4. NoSQL Databases 2012-11-11: 150 DB on nosql-databases.org Key/Value Store Document (Object) DB Graph Database www.arangodb.org (c) f.celler@triagens.deMittwoch, 14. November 12
  • 5. NoSQL Databases 2012-11-11: 150 DB on nosql-databases.org Key/Value Store Document (Object) DB Graph Database Multi-Model Databases Polyglot Persistence www.arangodb.org (c) f.celler@triagens.deMittwoch, 14. November 12
  • 6. Multi-Model Open-Source Database Various Indexes: Skip-Lists, Hashes, Geo, Cap Language API/Drivers for big Ps, Ruby, JS Embedded JavaScript & Ruby engine Written in C/C++ Query Language for Joins and Sub-Structures www.arangodb.org (c) f.celler@triagens.deMittwoch, 14. November 12
  • 7. Query Language: FOR u in Users FOR u IN users LET rs = ( LET friends = ( FOR r in Recommendations FOR p in PATHS(user, relations, ``outbound´´, 1) FILTER r.rec_by == u._id && r.type == "Book" FILTER p._from == u._id RETURN r.book_title ) ) FILTER RETURN length(rs) >= 3 { user: u, closeFriends: friends } RETURN { "name" : u.name, "titles" : rs } (compare with UNQL / JSONiq) www.arangodb.org (c) f.celler@triagens.deMittwoch, 14. November 12
  • 8. Use-Cases for script languages GET /user/fceller function (req, res) { var user = db.users.byExample({ username: req.urlParameters.username }); ... deal with unknown user... enrich result user.age = ... compute user age from birthday ...; delete user.hashedPassword; actions.resultOk(req, res, actions.HTTP_OK, user); hide info } www.arangodb.org (c) f.celler@triagens.deMittwoch, 14. November 12
  • 9. Use-Cases for script languages GET /user/fceller/eccentricity function (req, res) { var user = db.users.byExample({ username: req.urlParameters.username }); ... deal with unknown user... needs every node var eccentricity = ... compute eccentricity ...; actions.resultOk(req, res, actions.HTTP_OK, eccentricity); } www.arangodb.org (c) f.celler@triagens.deMittwoch, 14. November 12
  • 10. Use-Cases for script languages Transactions Triggers Predefined Objects for AJAX www.arangodb.org (c) f.celler@triagens.deMittwoch, 14. November 12
  • 11. MRuby Matzs embeddable minimal implementation of Ruby language www.arangodb.org (c) f.celler@triagens.deMittwoch, 14. November 12
  • 12. MRuby RiteVM as core Minimal standard libraries quotes from Matz Embeddable C API No perfect languages, even Ruby Like to provide choices www.arangodb.org (c) f.celler@triagens.deMittwoch, 14. November 12
  • 13. MRuby vs V8 JavaScript is a “complicated” language for embedding C++ class hierarchies V8 itself is complex (isolates, contexts, handle scopes), but fast mruby is plain and simple C code documentation is sketchy for both www.arangodb.org (c) f.celler@triagens.deMittwoch, 14. November 12
  • 14. MRuby vs V8 def fact n; (n > 1) ? n*fact(n-1) : 1; end mruby: 20 time-units V8: 0.6 time-units php: 20 time-units Ruby 1.9 (int): 9 time-units www.arangodb.org (c) f.celler@triagens.deMittwoch, 14. November 12
  • 15. Why use MRuby? “Like to provide choices” i.e. if you don‘t like prototype inheritance “still young” i.e. instead of using RiteVM, compile to machine code www.arangodb.org (c) f.celler@triagens.deMittwoch, 14. November 12
  • 16. MRuby and LLVM def fact n; (n > 1) ? n*fact(n-1) : 1; end 000 OP_ENTER    1:0:0:0:0:0:0 001 OP_MOVE     R4      R1 002 OP_LOADI    R5      1 003 OP_LOADNIL  R6 004 OP_GT       R4      >     1 005 OP_JMPNOT   R4      017 006 OP_MOVE     R4      R1 007 OP_LOADSELF R5 008 OP_MOVE     R6      R1 009 OP_LOADI    R7      1 010 OP_LOADNIL  R8 011 OP_SUB      R6      -     1 012 OP_LOADNIL  R7 013 OP_SEND     R5      fact 1 014 OP_LOADNIL  R6 015 OP_MUL      R4      *     1 016 OP_JMP              018 017 OP_LOADI    R4      1 018 OP_RETURN   R4 www.arangodb.org (c) f.celler@triagens.deMittwoch, 14. November 12
  • 17. MRuby and LLVM CASE(OP_SUB) { /* A B C R(A) := R(A)-R(A+1) (Syms[B]=:-,C=1)*/ int a = GETARG_A(i); /* need to check if op is overridden */ switch (TYPES2(mrb_type(regs[a]),mrb_type(regs[a+1]))) { case TYPES2(MRB_TT_FIXNUM,MRB_TT_FIXNUM): { mrb_int x, y, z; x = mrb_fixnum(regs[a]); y = mrb_fixnum(regs[a+1]); z = x - y; if (((x < 0) ^ (y < 0)) == 0 && (x < 0) != (z < 0)) { /* integer overflow */ SET_FLT_VALUE(regs[a], (mrb_float)x - (mrb_float)y); break; } SET_INT_VALUE(regs[a], z); } break; Simply replacing OP codes by machine code already gives a factor of 2 www.arangodb.org (c) f.celler@triagens.deMittwoch, 14. November 12
  • 18. Why use MRuby? If you have use-cases for scripts in ArangoDB (or any other database), mruby will eventually be an alternative Otherwise, use a language driver (aka Ashikawa for Ruby) and CRuby or JRuby www.arangodb.org (c) f.celler@triagens.deMittwoch, 14. November 12
  • 19. Thank You! Stay in Touch: Fork me on github Google Group: ArangoDB Twitter: @fceller & @arangodb www.arangodb.org www.arangodb.org (c) f.celler@triagens.deMittwoch, 14. November 12