node.js and native code extensions by example

886 views
714 views

Published on

Over the last years node.js has evolved to be a great language to build web applications. The reason for this is not only that it is based on JavaScript which already is established around "the web" but also that it provides excellent facilities for extensions, not only via JavaScript but also integration of native C libraries. Couchbase makes a lot of use of this fact making the Couchbase node.js SDK (Couchnode) a wrapper around the C library providing a node.js like API, but leveraging the power of a native C library underneat. So how is this done? How does such a package look like? Let me show you how integration of C in node.js works and how to "read" a package like Couchnode.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
886
On SlideShare
0
From Embeds
0
Number of Embeds
13
Actions
Shares
0
Downloads
10
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • - Been doing node.js since some time and C longer
  • Ask audience
  • node-mysql is at about the same level as libmysql based drivers
    Felix Geissendörfer talked about that the difference is not that big
    Moving objects across the boundry is expensive
  • Well if you think so
  • It’s a matter of taste I guess
    Sorry no JavaScript in here
  • There is FFI but it has significant overhead
    Using FFI outside your own stuff is difficult (different machines and compilers and compatibilty)
  • - Ok everybody knows everything now right?
  • Founders were users and contributers to memcached
    They build membase -> make memcache persistent and useable as a Database
    Merge with Couchone to build Couchbase
  • Build in Memcache layer -> most ops happen in RAM
    CouchDB views for indexes
    Ok let me show you quickly how to use the views
  • Cool so we can create functions to index
  • Mostly like any database you know
    It can store JSON … HEYA!
  • Lets see it in action
    Cool nothing special except cas
  • - Lets finally see some C code
  • - bucket.js
    - new CBpp(…) where does it come from?
  • Ok so there is a binding still bucket.js
    If we now search where it comes from we will see it unltimatly comes form binding.cc
    It loads couchbase_impl
  • Lets take a look what happens in the binding.cc
    The init function is probably called when we load it
    A new function is set…
  • Ok so new does a bunch of stuff
    I would have loved to show you more of the internals of couchnode but I think this is not really helpful
    So lets rather look at how to read couchnode by exploreing how to build a native extension
  • Live code!
  • Method signiture changed
    A bunch of other stuff changed… so better move to nan
    Couchnode moved all the missing parts over as well
  • Hail to the Macro
    Gets rid of the differences between 0.10 and 0.11 yay
    Define methods with NAN_METHOD
    Use NanScope to hande the scope
  • - Lets look at some more parts of building an extension
  • - Dynamic linker being helpful
  • - Make sure to declare that, not really a node thing but easily missed, and the linker as always is hardly helpful
  • You need to do the extraction yourself
    You need to get the right types
    Look you can even have a callback
  • Call the callback is straight forward really
    You need to provide the context
    Now you are ready for async
    Just don’t touch v8 from outside the main thread!

  • This holds a reference to our couchbase struct so we can use it for our connection
    Extract the arguments (ParseKey… and following mainly just do this and check the type)
    Store the arguments in the way libcouchbase expect them
  • Save off the callback (ParseCookie)
    Call libcouchbase
    Return and let libcouchbase handle the get and call the callback
  • And why you shouldn’t modify it
    Or really look at it… it’s just an opague value seriously!
  • Now that you know so much detail
    Break couchbase 3.0!
  • Come to spanconf
    All about handling scale, for data and applications
    Speed, data analysis, and how the grey beards do it (Erlang!)
  • node.js and native code extensions by example

    1. 1. Node, native code and Couchnode
    2. 2. Twitter: @ischi Github: @sideshowcoder Developer Advocate, Couchbase PHILIPP FEHRE
    3. 3. Using C/C++ from node NODE.JS AND NATIVE CODE
    4. 4. Why would I want to do this?
    5. 5. Of course Speed!
    6. 6. Nope…
    7. 7. C is cool!
    8. 8. Probably the best reason USING A C LIBRARY FROM NODE.JS
    9. 9. Couchnode is one of those cases Based on libcouchbase + wrapper
    10. 10. Quick: What’s Couchbase?
    11. 11. A Document Database.
    12. 12. Focus on Performance Allow flexibility by running JS
    13. 13. Using it from node.js
    14. 14. What happens here?
    15. 15. Seriously I like to show code But this starts to be a little much
    16. 16. The hello world of extension
    17. 17. 0.10 vs 0.11 Everything changes, welcome to the world of nan
    18. 18. What could go wrong? Lets explore it by wrapping a C lib for murmur3 hashing
    19. 19. Wrapping C
    20. 20. Arguments and callbacks
    21. 21. Some more couchnode To finish of…
    22. 22. What happens on a GET
    23. 23. Why is cas an Array?
    24. 24. Couchbase Server 3.0 Beta Bug Bash Download, Find, Report, Win • DOWNLOAD Couchbase Server 3.0 Beta • Run some workload, play with it, FIND a bug • If you find a bug, REPORT it! - All bugs count – anything from correctness, to performance, to usability, to docs • WIN Prizes!! http://www.couchbase.com/bugbash Contest ends Aug 31st 2014
    25. 25. Come to spanconf.io
    26. 26. Thank you! Any questions?
    27. 27. Links • https://github.com/kkaefer/node-cpp-modules • https://github.com/TooTallNate/node-gyp • https://github.com/rvagg/nan • https://github.com/couchbase/couchnode • http://www.couchbase.com/communities/nodejs • https://github.com/sideshowcoder/nurmur/

    ×