Bummer! This is just a preview. You need to be signed in with a Pro account to view the entire video.
Dev Track: Ruby Meets Clojure Flavored Database - Yoko Harada47:34 with Yoko Harada
Datomic is a new database categorized as NewSQL and was created by Rich Hickey. Everybody knows this big name and thinks of Clojure. It is true that Datomic fits well to Clojure programming, however, it is not only for Clojure people and Rubyists can use it too with the Diametric gem (https://github.com/relevance/diametric). Using Diametric, we can dive into Datomic from Ruby, from Rails. This talk will introduce Diametric gem and how to use it as well as why its API design is good for us. Also, it will cover how Ruby helped to integrate Datomic API in Diametric gem.
[Ruby Meets Clojure Flavored Database - Yoko Harada] [Blend Conference 2013] 0:00 [Yoko Harada] Okay. The time is 11:47, so let's get started. 0:04 I will talk about this topic, Ruby Meets Clojure Flavored Database. 0:11 Today I will talk about database as Datomic and one more thing is Diametric gem. 0:18 It is a Ruby gem. 0:28 So it's about Ruby and Datomic. 0:30 My name is Yoko Harada, and my Twitter ID, GitHub ID, etc, is almost all yokolet. 0:40 So find me at yokolet. 0:49 I am a JRuby core contributor and also I'm a Nokogiri core contributor. 0:52 Nokogiri is an XML processing library for Ruby. 1:01 And I am a developer at Relevance. 1:06 I work for Relevance for about a year. 1:12 I have been in Durham, North Carolina, for about a year. So I'm almost a local. [laughing] 1:16 First time to Charlotte but probably I can say almost local. 1:26 Today's topics will be 3. 1:34 First I will introduce what is Diametric gem. 1:37 This is a brief introduction. 1:41 Then I will talk about what is Datomic, 1:43 but it is just for introduction and people who don't know Datomic at all. 1:47 And then Diametric gem. These 2 words are very close. 1:54 I always confuse Diametric and Datomic. 2:00 I will talk about Diametric gem last. 2:05 Also I will show a couple of demonstrations. 2:08 First thing is what is Diametric gem? 2:15 It is a Ruby gem, and it is open source. 2:20 This gem is hosted at Relevance, my company, but it is open source. 2:26 It is an ActiveModel style or ActiveRecord style wrapper for Datomic, 2:32 a more friendly wrapper for Datomic. 2:39 This gem supports MRI and JRuby, 2:42 but currently JRuby is the best platform for this gem. 2:48 How Diametric gem supports Datomic. 2:59 This is a diagram. [coughing] Excuse me. 3:04 Datomic has REST service and Peer service. 3:09 REST service is just poking around REST API. 3:13 So Datomic has REST service 3:18 and connected from a Ruby gem using the normal HTTP and poking, sending a REST request. 3:20 Other thing is Peer. 3:30 REST and Peer, the biggest difference is REST works just on memory. 3:33 So you can't save data, just test data. 3:42 But Peer has a transactor and it saves data. 3:46 So using as a database, you need Peer and now this Peer service. 3:52 So from connecting Diametric to Datomic, now it is implemented by Java extension. 4:02 Probably people never heard of the word Java extension, 4:13 but this is like a C extension for MRI, CRuby. 4:19 There's a way of Java extension for JRuby. 4:25 I will show you this later also. 4:31 Who? These are developers of Diametric so far. 4:36 Clinton, on the left, started this project. 4:42 And in the middle, Ryan jumped in and he worked for Rails integration and some other stuff. 4:50 And almost the same time as Ryan, I started this gem 4:59 and I mostly work for JRuby-specific part, so make it Java extension or something. 5:03 So today's talk is almost all about JRuby implementation. 5:12 It is a challenging thing. 5:24 People talk about Datomic. It almost implies Clojure. 5:28 There's a couple Datomic samples. 5:36 Datomic has a GitHub repo and some public, and those are almost all Clojure. 5:43 So where is Ruby? It is database but no Ruby so far. 5:50 As far as looking at Datomic mailing list, almost all people look using Clojure 5:59 and sparsely Java because Diametric has a Java API. 6:10 This is for Peer API, sparsely Java and rarely Scala. 6:17 There's Scala implementation, Scala wrapper over Datomic Scala. 6:23 Sometimes this Scala pops up. 6:29 And no Ruby. Yeah, very sad. 6:33 Datomic is really Clojure flavored, 6:39 but Datomic is a really awesome database. 6:46 Why not use it from Ruby? This is our motivation. 6:51 Okay. This is the introduction of Diametric. 6:58 Then I will talk about Datomic next. 7:01 All right. 7:07 Datomic is created by Rich Hickey. 7:09 Do you know who is Rich Hickey? 7:13 >>[inaudible audience member comment] 7:17 Yeah. He is the creator of Clojure. 7:18 So Datomic is Clojure flavored. That makes sense. 7:21 And Datomic is like Clojure. Data is immutable. 7:30 Data is immutable. 7:38 What?! 7:40 "What!?" was my first impression when I first learned Datomic. 7:42 It is database. Why can it be immutable? 7:50 This is the idea behind Datomic, 7:58 the idea behind especially data is immutable, 8:02 because resources are not limited anymore. 8:07 Think about that. 8:13 RDBMS was invented in 1970. 8:15 In those days, hard disk was probably this big. 8:21 And even though this big, it has 30 megabytes, or it depends, 8:29 but not different from 30 megabytes or so and very expensive. 8:36 Nobody had this disk at home. 8:44 Then time goes by. This lovely Macintosh was released in 1987. 8:48 It had 40 megabytes. Much better. 8:57 And time goes by, 1990, 2000, 2010. Lovely Macintosh turns into stylish iMac. 9:01 iMac, this model, is 2011 and it has 256 gigabytes. 9:12 And now iMac has even terabyte. 9:24 Okay? Thirty megabyte to terabyte. 9:30 So technically, it limited. 9:35 But ideally— Yeah, so the idea behind resources are not limited anymore. 9:40 Why not think about stick to replacing data to save resources? 9:49 So this idea is in Datomic, so Datomic has it. That's sort of a new idea. 9:56 Resources are not limited. 10:11 We don't need to replace, so we don't delete all the data. 10:13 Data is totally immutable. 10:18 And data is called a fact. 10:21 In the Datomic way it is called datum. 10:24 Fact never changes. 10:28 Facts are always added. 10:31 So this is a comparison of RDBMS and Datomic. 10:34 In RDBMS new data comes in. 10:39 This will replace some column of a record table and all the data will go. 10:43 Compared to that, Datomic never deletes all the facts. It will add. 10:51 So this small circle is each fact. 10:57 This morning Ben of my company, Relevance, talked right here, 11:01 and in his talk there is Leonardo da Vinci's talk, and he said in—I don't remember the precise year— 11:09 he was a student at some year and then he was a doctor at some year, 11:21 artist in some year, scientist later. 11:29 If there is a form he needs to fill out and there's an occupation what he still is, 11:34 he should have written student first at some point. 11:43 Then years later he should have replaced that with doctor or artist, scientist. 11:48 In RDBMS all that data is gone. 11:58 But in the Datomic, the fact that he was a student, he was a doctor, he was an artist, scientist, 12:02 everything is in Datomic's database, never deletes, always adds. 12:10 That's Datomic. 12:19 So everything is in Datomic. 12:23 This is easy. Datomic has an idea of time. 12:26 Maybe you have looked at Time Machine of OS X like that one. 12:31 We can roll back at some point, pull out some data. 12:40 Like that, Datomic can roll back database state of an hour ago, 12:46 database state of months ago or so, and pull out that state. 12:54 This is Datomic, a very cool part. 13:01 From here is a little bit negative for Ruby side. 13:10 Datomic is like Clojure, has an idea of simplicity. This is okay. 13:16 Datomic's schema, data, and queries are all simple array and hash. Okay? 13:23 Schema, data, query, everything, simple array and hash. 13:32 All right. This is a schema of Datomic. 13:40 Even Clojure people are surprised with this schema. 13:45 But this is a schema, and recently people write this schema 13:53 for people who use Datomic from Clojure, and this is absolutely very far from Ruby style. 13:59 This is data, also array and hash. 14:13 Yeah. Very far from Ruby style. 14:19 More surprisingly, this is a query. 14:27 Datomic uses Datalog. 14:32 This is a Datalog syntax, and query consists of array in array. 14:36 This is very surprising. 14:46 People don't understand what is a query at first. Very far from Ruby. 14:48 Okay. This is a brief introduction of Datomic. 15:00 Then I will go to Diametric gem in depth. 15:04 This is Blend Conf. So blend it. 15:13 These 4 technologies are used in Diametric: 15:20 Datomic, Ruby, Clojure, Java. 15:26 So put everything in a blending machine and push the button. Then Diametric we created. 15:31 This is the idea. 15:40 Datomic schema turned into this model definition. 15:45 Internally, Diametric converts this model to Datomic schema, 15:52 but people will write this class definition. 16:01 This is not complete yet but almost. This is ActiveRecord style. 16:06 Then transaction data turned into model instance. 16:19 This is totally Ruby friendly. 16:27 All right. 16:31 Then query. Very hard to understand query turned into just a model method, poking around model method. 16:33 This is probably really understandable to Ruby people. 16:47 So again, Diametric compared this query to Datomic query internally. 16:53 And Diametric has not all ActiveRecord methods so far, 17:02 but Diametric has most of people, ordinary use method people, often use it. 17:09 All, where, find something. 17:21 All right. Demo time. 17:27 I will show you some demonstrations. 17:31 First thing is—oh, very small. 17:38 Can you read that? Okay? Okay. 17:49 This is our spec(?) demonstration. 17:53 Just a fragment is there. 17:59 I will show you demonstration code first. 18:03 This is a demonstration. First, class definition. It's small. 18:10 This is a person class. Person has name and nerd rate. 18:24 Sorry. I created this for Rails Conf this year, but feature is working so I use that here. 18:30 Datomic_uri. I will specify datomic_uri. I'm using memory database here. 18:41 If I connect, then after that, create_schema. 18:51 This create_schema is some sort of migration in ActiveRecord. 19:03 But we don't need to separate the migration from the program, 19:09 so we can run migration in the code. 19:16 Then I will create new person, something. Okay. Back to this here. Okay. 19:19 Now we've got that connection. 19:30 Yes. This is a Ruby object, but people might want Rails object— 19:39 actually a Datomic object. 19:51 People might want Datomic object in such a case. Diametric has toJava method. 19:55
You need to sign up for Treehouse in order to download course files.Sign up