Buenos dias. Antes de empezar, quería agradecer a toda la gente de la conferencia, Joseph, Mark, todos los marks de Cogrem y la gente de las camisetas verdes por haberme invitado aquí. Estoy encantado de estar en Barcelona y por más que nada haber organizado esta conferencia estupenda, que lo estamos pasando muy bien todo el mundo. Muchas gracias. So yeah, I was just saying thanks to everyone who works so hard on the conference. Thanks for putting this together. It's really a pleasure to be here. I speak some Spanish because actually my wife is from Spain, not from Catalonia, but she's from Oviedo, Esturias, which is a few hundred kilometers west of here on the northern coast of Spain. Anyway, 20,000 leagues under the sea. How many of you have actually read this book before? Few hands go up. I see a few hands. So yeah, for me, I hadn't read it either. It's one of those classic novels that we've all heard about. We've heard the name, Our Whole Lives, and Jules Verne is one of those famous authors that will never forget. But no one takes the time to actually read the book. So this summer, I was thinking about preparing this presentation, and I decided, I said to myself, you know what, I'm going to actually read the book. And I'm so happy I did. It was really fun. It was just an amazing read and blew my minds in a number of different ways. And so today, I'm going to tell you a little bit about the book as we go. But I'm actually here to talk about, there we go, active record. But first of all, before I get into active record, I want to talk about how active record works internally and how it actually creates SQL statements and executes them. But before I get into that stuff, why active record? Why talk about active record at a Ruby conference? It's not really an exciting new development with Ruby or Rails even. We all sort of know how to use this. The reason why, though, is for me, when I first came across Rails, it was probably in, I think, 2008, so whatever, six years ago now, this is what got me hooked. This is what got me excited. I still remember the day I first typed a line of code into the Rails console, something like this. And it gave me back, you know, converted into a select statement, gave me back an answer, and I thought to myself, wow, this is amazing. This is incredible. I can write this beautiful, elegant line of code instead of writing this crazy, horrible SQL statement. It converts it automatically for me, runs it, gives me the result back as a Ruby object. It's just amazing. And this is when I thought to myself, wow, there's something important going on here. There's something to this thing called Rails and Ruby. Before that, I don't know what I was doing. I was PHP, Java, something horrible. I blocked that part of my life out. And I started learning this. And this is why. It just seemed magical. Like, how does this actually work? But in fact, it's not magic. There's thousands of lines of code and years and years of computer science research behind this single line of code making that work.  I want to dive into ActiveRecord on the inside, look at how it works. I'm just curious. How does that line of code work? How does it get changed into a SQL statement? And then, why stop there? And then I decided, why not keep going and let's go into the database server itself? Let's go underneath ActiveRecord and look at how a select statement works and how it returns just the information we need from a relational database server. So here's the agenda. Here's what I want to talk about today. We're going to start at the beginning with ActiveRecord relation. This is the public API that we all use every day, probably, if you're a Rails developer. So I'll talk about how that works. Then we'll get inside of ActiveRecord and look at how it generates SQL statements. And then, like I said, we'll go underneath ActiveRecord. I'm going to use Postgres as an example. We'll go inside of Postgres and look at how it works. How does it execute a SQL statement when it gets one? Really interesting stuff. And if we have time at the end, we'll look at the Beatry algorithm, which is the real magic behind how database servers actually work. So fun stuff. Let's get into it. So the example I'm going to use is that first slide. UserWare name equals Captain Nemo. So I have a user model. It's an ActiveRecord base.  So I want to find all the records that have the name of Captain Nemo. So we're going to be looking for Captain Nemo all day long today. So how does this work?  It turns out the where method is defined in the ActiveRecord querying module. And that actually just redirects it or delegates it to another method called all. And what all does, you probably know, is returns a scope that gives you all the records in the table. And actually what it does, though, is it returns another new object to you, a new instance of a class called ActiveRecordRelation. And this is the class that actually implements the where method. So when I call user.Ware, it's actually entirely equivalent to saying, and actually this is what's happening, user.All.Ware name equals Captain Nemo. And then the where method is actually in that ActiveRecord Relation class. So when I call that, it gives me another copy of ActiveRecord Relation, another instance of this class. And what it does is you can see it saves some information about the query that I want to execute. So where name equals Captain Nemo. Then I call, let's say I want to call first. I just want the first object. And I want all the records that have the name Captain Nemo. I just want the first one. So same thing happens. I call first on that second object. And I get a third object back. Another instance of ActiveRecordRelation. And now you can see it has more metadata, more information about the query I want to execute. So we have, I don't know if you can all read that. We have where, name is Captain Nemo, order, ID ascending, limit, and offset. Where all those other values come from? Well, if we look inside of ActiveRecord, we can find the definition pretty quickly of first. So first is defined like this. If you pass in a limit, it does one thing. Normally, you don't pass in a limit. You just want the first record. It calls into something else called fine nth. So you can see that we pass in zero, meaning the first record. It's a zero-based index. And another thing is offset in case you wanted to start, somewhere farther along. Similar to first, there's also second, third, fourth, fifth methods. You get the idea. So you can take any query, put on dot second, dot third, and get that record back. And they all work the same way they call fine nth. They pass in a different number. And just in case you wanted the 42nd value, there's actually a method called 42. This is not a joke. This is actually in the ActiveRecord source code. So that's one thing you need to learn from today's presentation is to use that as often as possible. So back to the example. So my original example, user where first, is really equivalent to user all where fine nth, zero, zero. So again, zero is the first record, and offset zero. If we dive a little deeper, this actually just calls another internal method that we saw earlier, fine nth with limit. So in this case, the numbers have flipped. The zero is the offset, and the limit is one. Meaning I only want one record back. And it turns out that calls a couple other scopes. So as we call these different scopes, it just internally calls are the ones to build up the query that it needs. So internally, my example is actually the same as user all where named Captain Nemo, order arrow table primary key dot ascending. We'll get to arrow table in a minute, and limit one. So that's where all these values come from in my ActiveRecord relation object. So again, I haven't actually executed the query. What's happening here is this, and it's kind of neat. As you call these different methods, all where, order, limit, and lots of other methods, of course, that you guys probably all know about. ActiveRecord relation doesn't execute the query. It just caches the information that you're passing in. And each time you call one of the methods, it creates a new instance of that class and returns it back to you. This is one of the kind of beautiful things about ActiveRecord that I really like six years ago when I first came across it, and I still like it today. The idea that you can chain methods together, and you can use this technique in your own codes. We can learn about programming Ruby by studying Rails and other Ruby libraries. One of the reasons to look inside of things is to see how they work. So to do this in your own code, you just, each method, return a new instance of the same class, and then that class has all the methods that you can call again. So it's kind of cool and easy to do yourself. The other thing that's really neat about ActiveRecord relation is that it's lazy. So it's not actually executing the query. It's almost like we're using a functional programming language, something like Haskell or Lisp or something. It's smart enough to know you're not necessarily done, you're not ready to execute that query, which is expensive. We're gonna let you have the opportunity to make more of these scoping calls and specify more about the query. So it's not until you actually call 2A that you execute the query and load the data from the database. Internally, if you look at how 2A is implemented, it calls another thing, a method called load, and then returns the records. And finally, load eventually calls post-dress-sequel-adapter.select-all. That gives me all the data by running the actual query. Of course, if you're using a different database, it would be a different adapter. So this is a climactic scene from the novel, actually. So I won't tell you what happened, but this is when Captain Nemo and his crew get into this epic battle with a giant octopus. I think it's like a pulpo gigante, you would say, in Spanish, and you can see the tentacle coming right into the submarine. I won't tell you what happened in case any of you wanna actually read the book. So let's move on to take a closer look at Active Record Relation. So we still haven't figured really very much out. It's kind of cool how this class works, but how does it actually create the select statement? What goes on here? How do I get my information? And we can get a clue about that by looking at this in our Rails console or somewhere else. And you can see that those, the metadata about the query is actually formed by a series of other objects with class names like this, arrow nodes, equality, arrow nodes, attribute. What are these things? What is arrow? What are these nodes? Where do these come from? Dollar sign one, what's that? So if you go Google arrow and you look that up on the internet, you'll find there's a gem called arrow on GitHub. And it turns out that Rails pulls in an entirely different gem in order to do just what it needs to do, which is convert Ruby objects into SQL statements. So how does that work? So to figure out how Active Record works, what we really need to do is figure out how arrow works. But first of all, what does arrow mean? Where does that come from? And you can see the answer here at the top there. Arrow is a relational algebra. It doesn't, that's not very helpful.  And where does that come from? The other thing I wanna point out before we get to that is another interesting sentence. Arrow is a SQL AST manager for Ruby in the middle of the read me. I don't know if you can read that in the back. Again, interesting, but what does that really mean? What's a SQL AST manager? What does AST mean? So AST stands for abstract syntax tree. So we'll get to that in a minute too. But first let's start with relational algebra. That was invented or pioneered by this man here. This is Edgar Codd. He's a famous computer scientist from the 1960s and the 70s. He worked at IBM and Oracle and other places. While he was there, he wrote a seminal paper about relational algebra called a relational model of data for large shared data banks. This was written in 1970. So 45 years ago almost. And again, this invented the theory and the mathematics behind how relational databases actually work. And one of the things I wanna communicate to you guys today is take the time to go and read some of these academic papers. It's really cool stuff, really interesting. In this case, he actually used for the first time words like projection and join and cross product and other mathematical concepts that we sort of use every day every time we use a database. And so his ideas led to the invention of SQL, the language that we all know and love to execute queries with relational databases. So it turns out SQL existed before 1970, but it wasn't until Codd came along with these ideas that it took the shape and the form that we know today. You know, with joins and filters and you can select different columns. But still, how do we get to this? How does Aril create a line of code like this, a SQL statement from all of our Ruby objects? So that's where that other idea comes in, the abstract syntax tree. So let's go take a look at what that is. And this is an abstract syntax tree for my example, okay? This is a tree of Ruby objects that represents the SQL statement that we want to generate. So what Aril does, when you call Aril, when you use the Aril gem internally, builds up this AST, this abstract syntax tree, by inserting new Ruby objects into different branches. And you can kinda guess what these different things mean here and we'll go through this in a minute. So Aril actually is a lower level, more detailed way of specifying a query. So on the top is my active record line. So I have userware, name, Captain Nemo first. That's entirely equivalent. I could actually have typed in all the code below into my Rails app if I wanted to. You don't normally ever need to use Aril unless you have a really complex, sophisticated query that you need to run. For example, if you wanna use an OR clause, you need to use Aril. What you would just say users is a new Aril table. Then you say users dot project. Project is back to the paper from 1970. Dot where, users, name, equals, Captain Nemo, order on the user ID column, ascending, take one. Take almost as if it were a list per Haskell. So it's a lower level, more granular way of specifying a query. But again, how does this actually work? So when we call these different methods in Aril, and that's what ActiveRecord does, by the way, which simply calls these other methods for us, Aril creates these Ruby objects and puts them in this tree. And here's the other really cool thing about how this works. Here's how Aril gets to the SQL statement. It uses something called the visitor pattern. So the visitor pattern is one of, I think it's probably one of those gang of four patterns from Java, but it's actually a really cool idea, something else we can learn from studying the internals. So the way the visitor pattern works, what that idea really means is that there's something called a visitor, which is some kind of function, I'm showing it as a blue arrow here. It's some kind of function or class or piece of code of some kind that visits, that walks the tree or array or whatever structure you have, and goes to each one of those nodes and does something. Now, before I get to it, the other cool thing about Aril is it creates a different visitor depending on what type of database you're using. Right, so if you're using a MySQL server or a SQLite database, you get a MySQL or SQLite visitor. I was using Postgres, so I got a Postgres visitor. So another feature of Aril is that it will create a SQL that's customized for the database that you're actually using. So the way this works is the visitor starts at the top, select statement, and so it generates a string as it walks the tree. So it starts at the top, puts select up there on the top left, and then each time it visits a node, it appends or concatenates another piece of the query onto the same string. So let's see what happens. It starts at the top, it goes down to the first branch, select core, that turns out that doesn't really do anything, it's sort of a container for other things. So we continue on and we go over to attribute. So attribute is the projection to use the mathematical term or the list of attributes or columns from the table that I want to query. And so the visitor, when it gets there, writes users.star onto my string. Then we keep going, we jump over to, let's go to the join source. So join source lists all the joins or all the tables that I want to query from. And again, in my example, it's super simple. There's no joins, just one table. So I just have a single table node under there. So it writes from users, adds that onto the string. Then I go over here to the and node. So and the and node is the root of another subtree that represents the Boolean expression that I want to evaluate in my query. So again, I have a really simple example. So I don't have a complicated Boolean expression. I just have one, if you go down equality, I have one equals expression and it's comparing some attribute with a bind parameter. So it adds on where users.name equals dollar sign one. So dollar sign one is the bind parameter. And let's keep going. So we just go over to ascending, not a surprise that adds on order by users.id and the attribute there give us the id column and it's ascending. And finally limit on the right, the far right gives me limit one. So that's it. That's how Arrel generates a select statement from a tree of Ruby objects, which originally was generated by my active record expression. So very cool. So we're done, right? We've figured out how active record works. We figured out how we convert a simple line of code to a complex SQL statement. So back to Captain Nemo. This is actually one of the really cool parts of the book that I knew nothing about until I read it. This is when Captain Nemo discovers the South Pole. And so the funny thing about the novel is it's written in 1870. So at the time, I guess they didn't know that the South Pole was covered by Antarctica. So in the novel Captain Nemo and the Nautilus, journey to the South Pole underwater and under the ice, kind of a cool thing. And of course, it's like an anachronism. It's not actually true. The other thing going on here that's interesting I didn't know either is that there's this whole political subtext or subtone to the novel. And Captain Nemo doesn't have any citizenship. He's a citizen of the world. He's completely independent and lives under the oceans. And so when he gets to the South Pole, he claims it for himself. He puts his big black flag up there with an N on it. It's a kind of interesting political statement. But anyway, let's keep going. I was excited to figure out how we generate select statements from Ruby objects, but it wasn't enough. I wanted to know where does the data come from then? How does a database server actually work? How do I get the names that I want? So let's dive in and take a look. So I use Postgres mostly because I think it is the world's most advanced open source database. Some of you may use it, others use MySQL, maybe use a proprietary database like Oracle. I think they probably at this level all works more or less the same way. So let's go through how database servers work in 15 minutes or less. So it's gonna be a lightning tour. They basically have this four step process. And I'm gonna focus on the last thing, execute today. But I first wanna touch on parse, analyze, rewrite, plan, and then we'll get to execute. So let's start with parsing. How does the database server parse the SQL statement that we give it? Cause that's the first thing it has to do. We give it this complex expression with where clauses and joins and blah, blah, blah. It has to parse that. It has to figure out what it means. So it turns out, I never knew this, but it turns out that Postgres uses exactly the same parser generator engine that Ruby does. So this is a Bison. The name of the open source framework is Bison. It's a newer version of Yak, which is the original parser generator utility from the 1980s. So what Bison and Yak do is they take a series of grammar rules which are typically in an .y file and they convert them ahead of time into a set of C code or C functions that can parse a string according to those grammar rules. And so Ruby and Postgres do exactly the same thing. I'm not gonna get into how parsers work today. I don't really have time for that. If you're curious and interested in my book, Ruby Under a Microscope, actually the first chapter is all about parsers and how they work and actually it's about Bison. It works exactly the same way in Ruby as it does in Postgres. It takes tokens, it shifts them over into a stack and it uses reduces as a shift reduce algorithm that converts them into other grammar rules. So eventually we come up with, guess what? We've seen this before, an abstract syntax tree. So I was kind of shocked to discover that all that work that we just did, an active record just did for us, all those lines of Ruby code, all those methods, they work so hard to go from an abstract syntax tree to a select statement, to a string. What happens when we give that string to Postgres? Well the first thing that it does is it reverses all that work we just did. It converts it back into an abstract syntax tree. It's like why did we bother doing all that work? It's almost as if it would be better if we could somehow conceptually tell the database. I want this column and I'm from that table and I want this filter without having to generate this complicated string in this language called SQL. It makes me think that maybe we should be using a no SQL database. So anyway, food for thought. But this is actually very different than the AST that we saw earlier. Earlier we had Ruby objects, right? These are not Ruby objects. These are C memory structures inside the database server. But if you read them, if you look at them, they express the same ideas, it's the same string. So we have a select statement at the top. There's a target list, a from clause, a where clause. It's the same select statement represented in the same way just with different memory, different structures. Okay, so that's parsing. The next step was analyze and rewrite. So I won't really go into that today. I don't have time and I don't understand it either. So there's a lot of code, a lot of algorithms inside of databases to optimize queries to run faster. And that's really important when you have a select of a select of a select with joins and they can do a lot of work to bring the joins up and put them together and just make the SQL statement simpler. So there's a lot of sophisticated things going on here that I don't understand. But for my simple example, I followed this select statement through the C code in Postgres just to sort of figure out what was going on. It really didn't do much. It created different structures with different names but looks more or less the same. So these internal structures are just more, they're closer to what Postgres needs to actually execute this for us. And then the last step before we get to execute is called plan. This is really interesting. This is when the database server comes up with a plan. Like it's so organized, it doesn't just run the query. It first comes up with a plan, it follows the plan when it runs the query. So it's kind of cool. And this is yet another tree of structures. This one's pretty simple. Again, my query is very simple. And each of these nodes or boxes, these actually represent almost like a separate state machine in each case. Okay? And the way it works is you start at the top, Postgres starts at the top, and it goes down to the bottom. And each state machine calls the one below it in order to run the query. So let's go through this example. So at the top we have limit. Limit is a very simple state machine. What it does is it first calls the one below it and says, okay, give me one record back. And if it's limit one, then it's done. It returns that back to the caller. If it was limit five, it would say, give me one, give me two, give me three, give me four. So each time it calls down, by the way, inside the Postgres code, the lower inner loop is actually called the outer query. It's kind of a weird backwards thing. But I guess in databases, everything's backwards. So as you go down, you start with the inner query, you get to the outer query, the outermost query at the bottom. So limit calls sort. Sort is kind of simple to understand. What it's gonna do is the first time you call it, it actually needs to go to its outer query at the bottom and get all of the records back. So sort is actually unusual and different because it gets all the data because it needs to sort it. So it calls sequence scan at the bottom and says give me all the data and then sequence scan runs. And when sequence scan is finished, it returns all the records back to sort, which then sorts them, uses the quick sort algorithm. So not a surprise, the fastest sorting algorithm. And then returns results back up to limit, which will take the first one. But what sequence scan, how does that work? So at the bottom, sequence scan, you can see over here has a pointer to some expression. This is actually my Boolean expression from the query, the where name equals Captain Nemo. So sequence scan is pretty simple. What it does, it just goes through the table from top to bottom. These are just wacky made up names I got from the Faker gem. I don't know if any of you use Faker, it's just great gem. It makes random names. So it just, so the way the sequence scan works it starts at the top and goes down, but they're not random. These are the names that I inserted into the table in the order I inserted them. And it goes down and says, okay, is that Captain Nemo? No, is that Captain Nemo? No, and eventually we get to Captain Nemo. We found it, right, we're done. We found Captain Nemo, we can just return it, right, we're done, but we're not done. Because if you think about it, the query that we're running doesn't just say find Captain Nemo, it says select all from users where the name is Captain Nemo. So it gets all the records that have the name Captain Nemo. Then it sorts them based on the ID column, primary key, and then it gives me the first one. So in fact, the sequence scan needs to keep running. It needs to keep going through the entire table of names. There might be hundreds or thousands or millions of names. If that's the case, that sequence scan is gonna run all the way to the bottom of the table, get all the names back, even if there's only one Captain Nemo and still has to go through the whole table and give it back to me, and then I sort it and then I finally just take the first one and return that back. So it seems like I was doing something wrong here. There's something wrong with this picture. Like it doesn't seem like a very efficient database server. Why is it doing all this extra work for me? So I wanted to try to figure that out too. So back to Captain Nemo. So this is actually my favorite scene from the novel. This was from the beginning of the novel. So this is right after Professor Arana arrives on the Nautilus and Captain Nemo turns out to be a really nice guy. He takes him and his friends on a tour of the ship and they go all around the Nautilus, they look at the different stuff and the windows and all that stuff. And then they go into the engine room and Captain Nautilus, Captain Nemo, of course Jules Verne explains how the submarine actually works, how the engine works and what the technology is that it uses and it's really neat because this was again written in 1870. There were no submarines then or there were a few primitive submarines but they weren't, they didn't work the way the submarines today works. So this is Jules Verne imagining the future from a 19th century perspective. It's a really interesting read and it's actually known as the most scientifically accurate of Jules Verne's novels. For example, Journey to the Center of the Earth was complete fantasy but this is vaguely accurate so it's super fun to read through this. Anyway, to fix our problem, duh, it's very simple. All you need to do is create an index. Okay, so another rule they learned from today is just create an index if your database is running slow. Seems really simple, write active record migration, add index on the user's table on the name column. I'm done, everything's fixed, everything works great. But I wanted to find out why? Why is an index speeding this up? What is an index? How does an index work? What does Postgres do when I create an index? I had no idea, right? So I wanted to figure that out and this is what I managed to learn. So an index, when I say add index in Ruby it runs a SQL statement which is probably something like create index and when you run that what Postgres does is it creates another new copy of the entire table. So all the data at least of the column that I'm indexing it copies it into another memory space. So here's my original table over here, all the names in random order or whatever order I inserted them in. And on the left is the index and you'll notice that the index is sorted. So an index fundamentally is just a separate copy of the data that's been sorted. And each entry in the index has a pointer back to the original table so I can find my way to get the other columns of each record if I wanted them. So pretty simple actually. And so when I run the query now it's a lot faster. And actually when I ran the, when I looked at the plan and by the way you don't need to hack the C code in Postgres to look at the plan you can just run a query I should have written a slide for this, you just say explain select star.name, blah, blah, blah. So you just use the explain SQL statement and Postgres will print this out for you right on the console as a text string or as like an indented tree you can see the same tree structure. So if you have queries that are running really slowly and you're not sure why the first thing you should do is run an explain statement. And this looks exactly the same except for this one big difference that sequence scan is gone and it's been replaced by something else called index scan. I'm like, oh, so now I have an index I can do an index scan. Okay, what does that mean? So I got out GDB, I went into the C code of the index code in Postgres and tried to figure out what it was doing and here's what it was doing. So imagine now I have a separate copy of all the data in this index. So all the names from that table, from the user's table is now in this separate array really. And it's sorted. So that's really cool because it can help Postgres or any database server find the name I'm looking for very quickly. So I'm looking for Captain Nemo. So what does it do? It uses a binary search. You probably all heard of this algorithm. It's really simple, it works like this. Instead of just going from top to bottom like the sequence scan did the index scan starts in the middle and says, it picks out the name in the middle. I don't know, the one Kendall Ledner, M.D. Probably can't read that but it picks a name that begins with K and it says, is this greater than or less than Captain Nemo? Oh, it's greater than Captain Nemo, alphabetically. So we go up, we search, fire up and now we cut the remaining names in half again. So we go to the 25% point and we say, is this name greater than or less than Captain Nemo? And it turns out in this example it's still greater than Captain Nemo. So we take the remaining names and we go cut them in half again. So we go up to the top now we're at 12.5%. And we say, again, is this name equal to Captain Nemo? Greater than or less than. So you can see that it very, very quickly, exponentially narrows down to exactly the name that we're looking for with only a few comparisons. Even if we had millions of records it would very quickly, by dividing in half over and over again with find exactly the name I'm looking for. This is basically why indexes are fast and why you should index your data if you need to search it. Because it can do a binary search instead of a sequential search. But there's a couple of big problems with this. This is an oversimplification and there's a couple of theoretical issues with how I'm showing this. One has to do with threading and concurrency. So I'll talk about that in a few minutes. The other one has to do with the way that database servers manage memory. Okay, so if you're running Facebook or Twitter or some other SoundCloud or some other super successful company that has millions of users you can't just stick all those names in a single array in memory and search it with the binary search algorithm. Database servers don't work that way. What they do is instead they manage memory with a series of pages. This is the part of Postgres where my understanding is a little fuzzy but basically divides up the available memory into pages and then swaps them out the disk and swaps them back according to some algorithm. So in fact you can't fit millions of names into a single page. It can only fit about, it turned out when I measured it, about 200 names. So if you've got one, 10, 20, 30 names it just sticks them in a single array and it works fine. But if you have 200 names something has to give. It doesn't work anymore, the same way. So imagine that we had 200 names. We started with whatever, Amina and we went down to Zora, okay. Not a lot of names, everything was fine. But now let's say I insert that 200 first. I think it was actually 206 names would fit. So I insert that 206th name Captain Nemo. Let's say I'm inserting Captain Nemo now. Where does it go? Postgres doesn't have room for that name in the same page anymore. So it has to do something else. And this is where that B-Tree algorithm comes in. And B-Tree actually stands for balanced tree algorithms. This is another classic computer science theoretical algorithm. And what it means is you divide the page into two and you put the first 100 names into that first page and you put the second 100 names from the original 200 into the second page. So now each of the new pages has lots of free space and we can stick Captain Nemo up into that first page because he begins with a C. And then there's plenty more space for other new names to be inserted. So the idea is we can create this tree structure of pages that have all the data spread out in this sort of equally distributed fashion. That's why it's called balanced tree because we balance the amount of data in each of the pages. And the reason we wanna balance it this way is so that we can quickly search it. Now what I'm gonna do is I need to find Captain Nemo. So I can say, these are actually repeated names in the middle, but I can say, okay, where is Captain Nemo? I go to that parent page. Notice the parent page has a copy of the first name in each of the child pages. So we have a null for the top one. That's where the first few names are. We have Julius in the middle and so that second group should start with Julius. And the bottom we have Monte Nicholas. So we put him down at the bottom. So by recursively searching through here, we can find any one of the names that we need to. And so this balanced tree algorithm is really neat because you can add millions and millions of names and just creates this huge tree of pages that can go on and on and have different levels of hierarchy and it works very quickly when you're searching it. So that's how Postgres solves the problem of paging and memory management and how it finds space for all these names. So the one remaining problem I wanna talk about because it's really kind of a neat trick is how it deals with threading and multi-threading and concurrency. So there's a big problem with this which is that you can't simply lock the index if you're inserting a new name, right? So what happens if I try to search the index at the same time someone else, another thread and another client is inserting a new name and splitting it up? It's not gonna work right. So before 1970 or actually before 1980 when another algorithm came out, what database systems actually did was they locked the whole tree. They just said, you know what? If you're inserting into the index, no one else can use that index now. But that's not okay. It's not good enough. Database servers need to be smart enough to allow one client to insert into an index while another one or many other clients are searching it. You can't just stop your website whenever you insert new data. So what did they do? So back to computer science theory. Again, go read these papers. They're really fascinating. This one has a lot of interesting diagrams and pictures in it. So this is from 1981. This is by Lehman and Yao. And I found this actually because the C code inside of Postgres is super easy to follow and well explained. It has a lot of great comments. So that's another resource. If you understand C code, go read the C code for Postgres. So they explained that they use this algorithm and this is a really neat trick. So let me try to explain this in just a couple of minutes and get across how it works. So let's imagine that we're inserting and searching the same index. So one thread is trying to insert a new name at the same time as another thread is coming in to find it or to find some other name. So at the moment, the problems happen at the moment when we do that split. So when we split the page into two pages, everything goes wrong. If at the moment we do that, we stop, another thread runs and it tries to search because that second page doesn't have that parent pointer. It's sort of like this orphan page at that moment for a few microseconds or whatever. But again, if you're running a database server, you probably get tons of connections, tons of activity. So what happens if another one thread does this at the same instance, another thread starts and searches for a name using the tree, but actually the name it's looking for, maybe it's down in this other second half of the names that were just split. It's not gonna find them. So Layman Yao in their paper described this pretty cool trick. They came up with a variation on that balanced tree algorithm and they describe it in their paper. And the crux of it is it's more complicated than this, but the general idea is that when you split the page, you add this special pointer at the bottom of the first child to the next child over to the sibling or brother or sister page. So in fact, and this is the algorithm that Postgres uses and you need to sort of know this before you try to read the code and figure out what it's doing. Because so the indexes in Postgres actually look more like this. So the tree has the root or parent page at the top and then you go down the tree and then each of the child pages has a special pointer over to the next child. And why? It's because of this threading problem. So now Postgres can split any one of these pages. That pointer will still point to the next child over and even if you're searching the index at the same time that you do that, it'll find the name with no problem. So super cool stuff. I thought this was great, but let's just take a quick recap. What have we learned today? We've learned that ActiveRecord has a 42 method. That's pretty cool. We've learned that always create indexes on your database tables. Okay, you didn't need to come to Barcelona to learn that. What I really want to get across to you today is dive inside the tools that you're using, try to learn how they work and try to understand them. You don't need to know all the nitty gritty details. You don't need to know all the pointers and all the structures and everything that's going on. What you should know is something about the computer science, something about the algorithms that are at work, that you're using every day when you go about your work. And the reason why is it'll make you a better engineer and better developer and you'll be able to make smarter decisions. But more than that, it's just a lot of fun. I had a blast figuring this stuff out. If you want to learn more about internals and low level stuff, I wrote this book called Ruby Under a Microscope, which is a similar sort of conceptual treatment of how Ruby works itself. What happens when you run a Ruby program on the inside? And it was super fun to write this and I hope it's fun to read too. So that's all I got today, thanks a lot and I don't know if we have any time for questions. I probably won't know the answer anyway, but thank you.


Now, Roma Wines present the blue beard of Balak starring Merle O'Bron. Suspense is presented for your enjoyment by Roma Wines. That's R-O-M-A, Roma Wines. Those excellent California wines that can add so much pleasantness to the way you live. To your happiness and entertaining guests. To your enjoyment of everyday meals. Yes, right now a glass full would be very pleasant as Roma Wines bring you... Suspense! This is the man in black here for the Roma Wine Company of Fresno, California who tonight from Hollywood bring you a star, Miss Merle O'Bron and a suspense play inspired by an actual recent news item from occupied France. And so with the blue beard of Balak and with the performance of Merle O'Bron we again hope to keep you in suspense. Get back. Stay back there. Back of the wall. How many were there this time? One. They found most of him. And? It was a soldier. A German. I heard it was an officer. Stay back. Back there. Come on. But they're still digging? Yes. What officer was it? Did they say? They think it was Captain Müller. You know the doctor. They think. They don't know. They're looking for his head. One, two, three. And now five. How about this? Of course they found the body. Identify the body. Why don't they identify him? Identify him? The blue beard. Why they find out who he is. Listen. Listen. The victim of the blue beard of Balak is identified as Captain Franz Müller medical officer attached to the staff of the... The first time has been seen, has been provided. Is this private speaking of medium... Yes. I must keep the colonel's trail. Is he here? I have information. Information about what? About the blue beard. Is the colonel here? He's in the staff car. Over there. Please take me to him. I must see him. And who are you? Cecile Combre. Madame Combre. I live on the road to Flomé. Just outside the village. Well, all right. Come along, madam. Is that the colonel sitting in the back of the car? Yes. Now you wait here. I'll see. This is a woman from the village, a Madame Combre. She has information about the blue beard. Well, here again his hysterical wife, Mr. Oberst. What can we do, Kreuzer? I have a possibility. Where is she? Over there, Mr. Oberst. I told you so. All right. Come here, madam. Yes, Mr. Oberst. To the colonel you see you, madam. Oh, thank you. Madame Combre, Mr. Oberst. Madame, this is my aid, Lieutenant Kreuzer. It was kind of you to let me speak with you, monsieur. Kind? Naturally, madam. When we were told you have information about the blue beard. Yes, I have. We can't overlook anything. Now that he's killed one of my own officers. I know, I know. And I, I am next. So? The blue beard is going to kill me. He's going to kill me tonight unless you help me. Unless you come home with me and stop him. This is your information? Yes. Why, every woman in Balak believes she's next on the blue beard's list. But I know. Madam, the colonel's time is valuable. We thought you had real information. You don't expect us simply because you hysterically believe that... No, no, please. I know colonel Strelitz. How can you know? Because, monsieur. Because the blue beard is, is my husband. Why? Your husband is... Dr. Pierre Combre. Yes. He's the man you're looking for. I've known it for a long time. He is the blue beard. Tonight for suspense, Roma Wines are bringing you a star. Miss Merle Oberon, whom you've heard in the prologue to The Blue Beard of Balak by Sylvia Richards. Tonight's tale of suspense. In many foreign lands, wherever wine connoisseurs gather, they enthusiastically praise the distinguished character of Roma Wines. Such praise of Roma Wines in foreign lands can only mean that they are truly magnificent in quality. Roma Wines' excellence is due to a unique combination of California's perfect soil and climate. From whence come the choice Roma Wine grapes? Plus age-old winemaking skill and modern knowledge. These combine to make Roma constant in quality, uniformly fine, un-excelled in value. Tomorrow, discover for yourself the delightful Roma taste and goodness enjoyed by more Americans than any other wine. Simply serve as an appetizer before dinner, a cool glass of golden nut-like Roma California sherry. Then on the table, place a bottle of cool, hearty Roma Burgundy. You'll be pleasantly surprised at the extra delight it adds to your meal, how it will win new compliments from family or guests. Yet, the cost is only pennies a glassful. Yet, Roma Wines tomorrow, if your dealer is temporarily out of Roma, please try again soon. Ask for R-O-M-A, Roma Wines. Remember, more Americans enjoy Roma than any other wine. And now it is with pleasure that we bring back to our sound stage Miss Merle Oberon, as Madame Cecile Combray in the blue bed of Balak, a tale well calculated to keep you in suspense. 11 o'clock. He'll be here in an hour. And if you had not come here with me... You have nothing to fear, Madame. The house is well guarded. But won't the guards frighten him away? The entrances, the structures are to let him through. They will come in only when we have him trapped and we are armed. Yes, and he does not carry a gun. You know he uses a knife. Madame Combray, why didn't you come to us before if you knew your husband was a blue beard? First, I wasn't sure. Then when I was sure, well, he was still my husband. Then why now? Because now he will kill me, and I'm afraid. I knew when he went away with the body of Captain Miller yesterday. You saw the captain? Yes, I saw it. And when Pierre looked at me, I knew he'd kill me tonight. Yes, he may try. It's been weeks of fear in the night. The endless nights. Have you been married long, Madame? Long? No. Less than a year. You aren't French, are you? No, no, I'm English. I spent a summer in Flomé about four years ago, and I liked it, so I stayed. I taught English in the village school there. Dr. Combray? About a year ago, there was an epidemic in Flomé, and he, Pierre, came there to help. It seemed to me when I met him to be very kind, a very noble man. Of course, I didn't know him well, but when he asked me to marry him, I was very happy. The morning after we were married, he brought me here to Belac, to this house which had been his family's for many generations. It was a beautiful morning, early spring. We came, as you did tonight, to take the hill past the summer house to the front door. Well, here it is, Madame Combray. Come, I'll carry you over the threshold. Oh, careful, Pierre, don't drop me. No chance of that. Now, this is the hall. Shall I carry you on from room to room? Oh, Pierre, no. Put me down. Very well. Ah, come this way. Now, here is the parlor. Hello, Captain Müller. Good morning, Doctor. I've been waiting. I hope you don't mind. They said you'd be back this morning, the people down the road. Yes, I was held up by my wedding. Your wedding? Oh, I'm sorry. Cécile, this is Captain Müller, the medical officer in charge with Colonel Strelitz. Captain, my wife Cécile. How do you do here, Müller? Well, Doctor, I congratulate you. I see even an epidemic can be useful. That's one way of looking at it. So, what's up, Müller? Oh, uh... Oh, I wanted you to come in tomorrow to help with the vaccinations about 150 are going out and we need help. The last lot carried tie for it. I'd be glad to help with time. Well, they are leaving at noon. If you're there at eight o'clock, we'll have enough time. I'll be there. Very well, Doctor. Madame, tomorrow's there. Hi, Lidla. Hi, Lidla. Now we will go on without tour. Follow me. Pierre, who's to be vaccinated tomorrow? All labor draftees being sent to Germany. This is the kitchen. The stairs go up from here. What's upstairs? Well, of that first landing, there are several bedrooms, mine and others, and on the floor above, still more. Before the war, there were servants. This is a pantry? Yes. And this door? Where does it... Oh, it's locked. What is it? Oh, there's an old wine cellar. It's not used. A cellar? Do you have a key? There's nothing down there, Cecile. That would interest you. Oh, but I'd like to see. We might be able to grow mushrooms or... No, Cecile. It can't be used. Let's look. Where's the key? Cecile. Yes? That door must stay locked. But Pierre...  Yes, but why? No matter what happens, you must never try to go down there. You see, Monsieur, it was a small thing. Just a room I must not enter. He told me the room was used for his experiments, and I believed him. I was in love. But there were other things, and they added up to... fear. Just the taste of fear. A shadow so light, I... I didn't know it was there. It was first of all the gossip I heard in the village when I went to market. Oh, good morning, Madame Combray. You're late today. Pierre worked late last night. Is there any milk, Madame Boucher? I can let you have a little, a pint. Oh, well, that's all. Does the doctor work often at night? Quite often, in his laboratory. I could not bear a man who potted around after dark. But it's his profession. Maybe, but I would not sleep a wink, not with this blue beard around. No, I like a man who is steady, so I know what he's up to. Oh, how can you bear to live in that big, depressing house? Oh, but I love it. Well, you're young. I suppose it's romantic to you. It's no place for a woman to be alone. The first Madame Combray, you know. She died there. Yes, Pierre told me. But I know he did everything he could. Oh, yes. It was very sudden. Typhoid can be very sudden. Yes, it's certificate said Typhoid. That's why your coffin was sealed. Oh. Hey, young! Young, what is it? What is it? What's happened? Everyone's running. There's an announcement, Madame. They know who one of them is. One of whom? There were three last night. One of them was a man. Three? Why, the blue beard, he killed three in one night. Cut them to bits. Oh, how terrible. All sliced up and scattered around, Madame Combray. You should have seen. Oh, no. Where were they? In the middle north of the church. Why, that's near your house. The meadow? Yes, Madame Combray. If you had been awake, you would have heard him. You or the doctor. Yes. If I'd been awake, I would have heard him. And one night, I was awake. My husband and I had gone to bed early and I slept well. I'd worked in the garden most of the afternoon. But shortly after 11, something, some sound woke me. It may have been only an old screech. I lay in the dark and listened. I didn't hear it again. Then I heard another sound. And I saw there was a little moonlight that my husband was out of bed and that he was dressing. Pierre. Oh, did I wake you? Pierre, what is it? Nothing, Cecil. Go back to sleep. Is someone ill? Did someone come for you? I have to go out. Go back to sleep, Cecil. I won't be long. Something woke me. I heard a sound. I'm sorry. Was it Captain Muller? No. Who is it, Pierre? Who is it? No one you know. Where are you going, Pierre? Do you have to go far? Go back to sleep. I won't be long. But, but, Pierre... I'm sorry I woke you. You didn't. I'm sure I heard a... Cecil, it's best that you go back to sleep. I just wondered... It's best for you. Good night, Cecil. He went away, Colonel Strids. And I lay there in my bed, rigid, listening to his steps down the dark stairs into this kitchen. I heard the front door open and close. Then he went down the gravel path in the moonlight. I waited. It seemed long, yet it was only a little time. That clock there, I could hear through the floor, chime the quarter, then the half. When I heard him, it was not yet midnight. Then he came slowly, climbing the hill. I slipped out of bed and went down the stairs to the landing there, from where I could watch him come into the kitchen. And when he came into the kitchen, I could not speak. For he was not alone. Over his shoulders, he carried a body. A man, I think. And he was stooped under his horrible burden. He crossed the kitchen without looking up and did not hear what I was sure he must hear, the pounding of my heart. He took a key from his pocket, holding the body with one hand, unlocked that door to the wine cellar and went into its awful blackness. Then I was back in my room. I don't remember how I got there. Cold and shaking in my bed. When I heard, oh, Monsieur, it was pitiful. I heard rising from the dead silver house from where he had gone. You are still awake. No, Pierre, I... I told you could go back to sleep. I was, Pierre. I did. But you are awake now. Something... I heard something. Yes, you heard?  You came in suddenly. You must have... Yes, yes, I think I did. Well, we'll go to sleep now. Yes, we'll go to sleep. Pierre? Yes. Was someone ill? Yes. Who was it, Pierre? Did you have to go far? We'll go to sleep now, Cécile. What time is it? Time. It's just past midnight. Midnight was usually the hour. His hour. He always came back soon after. And the following day there were always the announcements on the loudspeakers in the village. Bodies. Pieces of bodies. It was the day when he and your men found out it.  And you remember the body was yet warm. So the whistles were blown calling all the village to the church square because the blue beard might still carry his knife or have blood on him. Or he might not get there and be known because he was missing. I was in the village and I ran to the square with the mayor. We stood in line with the others. Everyone was there. Everyone except Pierre. What terror I felt. Monsieur, when you began to call the names. Phénix-Armont? Pierre. Paul Arden? Pierre. Yvette-Partil-Mille? Madame Cambre, are you here? It's just the sun. It's so warm. Yet I feel chilly. You're very pale. Here. Don't see Pierre. Here. Here. He's probably with Captain Muller. You be here. Pierre Cambre. Pierre. Here. Here. I'm here. Pierre. Don't look at me like that. You'll attract notice. You're out of breath. Where will you, Pierre? It doesn't matter. I'm here. But your hands, your clothes, they're wet. I still did not know. And I needed to be sure. I could not live unless I was sure. So I stole the key. The key to the cellar. Yes, it was that easy. While he slept and the next day he went to the village with Captain Muller and left me alone with the answer in my hand. I opened the door and went down those steps, carrying a candle. You could go down there in a moment, Colonel Strill, to wait for him. And you will see there is a little room, bare and damp, by the candle's light. I saw there was something on the table. It was his case, filled with knives, surgical instruments, not strange for a doctor. There was blood on them, fresh blood. And there was more on the table, on the floor and much blood on a sheet which I found thrown into the corner. I found the strength to get out and to lock the door again. Put the key back that night. So again I did not know. I did not really know until yesterday. I was sewing in the front parlor and Pierre was walking up and down because he had an appointment with Captain Muller. The captain was late. What's keeping Muller? Perhaps he got orders. More soldiers came into Belac yesterday. Many more. Yes, I know, but that shouldn't keep him. There's something going on. They're getting ready for something. Yes, but why would Muller? Do sit down, Pierre. Your walking makes me nervous. I've spoiled the seam and have to rip it out. Oh, I'm sorry. Some of the soldiers were searching houses yesterday. I don't know what they were looking for. Pierre? Oh, no. At least not while I was here. I was in the village most of the afternoon. That's when I saw the soldiers. And there's nothing here to look for. Is there, Pierre? That's it, of course. Why didn't I think he's down there? Pierre, where are you going? I'm going down to my cellar, Cecile. I have work. Pierre, no. Not the captain. Yes, the captain and Cecile. I don't want to be disturbed. And then I knew he was going to kill Captain Muller. He was going to kill a German soldier. Worse, an officer. I knew then, Colonel Strelitz, that if he would go that far, he was no longer a man I could even attempt to reason with.  I had to stop him. Cecile, what are you doing? You can't come down here. You killed him. Cecile, now I'll have to. But I wasn't ready. It's not time. Captain Muller, they'll track you down. You can't get away. They'll be quiet. I warned you. I told you not to. Murder. You did it. You killed him and the others. Cecile, I told you. What will you do now? Do. What do you think I'll have to do? And now? Now you know too. And in a few minutes he'll come. And then it will all be over. It's almost midnight. That's a remarkable story, Madame. So he made no attempt to harm you last night. No, Colonel Strelitz. It wasn't time. Not time? He killed only when he feels the need. And he'd already killed one, so the need was gone. And it was daylight. He left then? Yes. I haven't seen him since. He looked at me. It was a terrible look. And he went. And I knew by the look that I would be the next. Tonight. And Captain Miller? He... he took the captain's body with him. Come, we must go down to the cellar now before midnight. Very well. Lieutenant, will you bring the lamp? Yes, I'll bring it. You see, it's unlocked. As he left it, close the door behind you, Colonel. Very well. Careful. The last step. Another door. It is small and damp. Is... is that the table? Where he... Yes. Set the lamp there, Lieutenant. You can see the stairs. They're quite dry by now. I see. And on the floor? Sit down, Monsieur. There, where you can watch the door. I... I will stand here at the back if you don't mind. You're right. It's safest for you. Listen. I heard the clock strike. And, yes, listen. It's a door. Have you your guns? Quiet. Yes, but we want to take him alive. You can. He's coming down. Cézine, are you there? Put your hands up, Dr. Hombrey. Our visitors? Well done, Cézine. Gasly visitors, doctor. A bit more unpleasant for you. Now, if you'll... Put your hands up, Colonel Strelitz. You too, Lieutenant. Yes, I too had a gun. But your husband... He's... The Bluebeard. I'll take your gun, Colonel, and yours. No, Colonel, I am no Bluebeard, as you knew very well. It was clever of you to plant the mutilated bodies of your victims to drive me into the open. To create this legend of a Bluebeard to make the people of Belac suspect all men who work at night, as I do. To make the village distrust me, their leader. But, Madame Combrey, you saw Captain Müller. Saw his body. The captain? Yes. Pierre killed Captain Müller. He was his one victim, because the captain was suspicious and pried a little too far into this room.  Open it, Pierre, and let the Colonel see. You see? The wall opens easily. And behind it is the nerve center for Belac, for our underground army. Army? Sneaks and cowards who set their women to lying. My life? What else? You said that he carried in the body, that there was a scream in the night, that there was a body. All true, Colonel. Yes, I carried home a man wounded by your soldiers, and I removed the bullet without anesthesia for we French have no such luxuries. So the blood of that patriot is mixed on the table with that of the late Captain Müller. Don't you know it's hopeless for you? Hopeless? Colonel, surely you know that our armies are in France, Americans, English, and our underground army, which surrounds you? But this house is surrounded by my men! It was, you mean, Colonel. Are you still there, Mr. Porter? Porter? Yeah, what do you say, Doc? Everything quiet? Quiet as a tomb, Doc. All things down there. Who is that? We have what you call a couple of rats, Mr. Porter. Then we are finished. Okay. That is Mr. Porter as you heard. He is an American. An American? He is commanding a large number of parachuters just an hour ago, dropped into our meadow, and who a few minutes ago very quietly killed the guards you mentioned. Killed? My God. Yes, and since you were here in the cellar, unfortunately you could not hear. But now, Pierre, you have your work to finish. Your work?  So with Cécile's help, I set this little trap. Oh, listen to me. You want to... Cécile, do you want to share the honor? No, no, no. Please, please. No, I don't! No, please, no! And so closes the blue beard of Belach, starring Merle Oberon. Tonight's study in suspense. Suspense is produced and directed by William Spear. Have you discovered how much good wine adds to the enjoyment of food? How Roma wine makes even the simplest, most inexpensive meals really exciting events. Well, all you need to do is place on the table with the meal. A cool bottle of hearty Roma California burgundy. Serve it in any kind of glass. You will find it delicious with any food. And if you are entertaining guests, you will find Roma wine just the gracious festive note that makes any dinner party or get together a happy, compliment-arousing occasion. And remember, Roma wines cost you only pennies a glassful. So any home can afford the pleasure they add to everyday living, too entertaining. Ask for R-O-M-A, Roma wines. Remember, more Americans enjoy Roma than any other wine. Merle Oberon is soon to be seen in the Columbia Technicolor production, a song to remember. Next Thursday, ladies and gentlemen, same time, you will hear Mr. Gene Kelly as star of Suspense. Placed at it by Roma wines. R-O-M-A. Made in California for enjoyment throughout the world. This is CBS, the Columbia Broadcasting System.


For example, here it is given on the left hand side that the resolution is inversely proportional to the momentum and which is inversely proportional to the root e at low energies and at high energies it is proportional to 1 by e. Now, if you so that means, if it is high energy you can get better resolution and therefore, you can you will be able to study much smaller dimensions. For example, these are some of the things which are given here that if you want to study this kind of dimensions. For example, if you want to study 1 millimeter kind of things then you need an energy kinetic energy of the particle which is of the other 10 to the power minus 4 electron volts that is good enough and if you want to study the you want to have resolutions of micron size then you have to go to about 0.1 electron volts. Similarly, if you want to have the resolution of 10 to the power minus 18 meters then the energy required is energy of the particle required is 10 to the power 11 electron volts and which is about roughly about 100 G e V and for particles of 10 to the power minus 20 meter that will be minus state 18 centimeter you will need this kind of energy and which are of the order of 10 T e V and therefore, if you want to find out what is the sub structure of quarks then you will need energy which are higher than this and these are shown here. Even to study the quarks you can see which are the dimensions of this type and the energy required will be in T e V several G e V energies and that is why when the quarks were discovered the energy of the energy of the electrons one of the order of 30 to 40 G e V kind of things. Now, where we have gone it is you can say these quarks actually they are what are the properties and there are six types of quarks which have been discovered and they are called up, down, charm, strange, top and bottom these are the six quarks up, down and they come in pair up, down, charm and strange so these two come in together and this charm and strange come together and top and bottom. Now, these are some of the parameters now you see here this is very interesting that up quark is 2 by 3 plus of electron here you can see that they are fractional charges and down is minus 1 3 1 by 3 so you can see here. So, if you put these things then proton consists of u u d because proton has charge 1 unit 1 and therefore u u d you can have 2 by 3 plus 2 by 3 minus is here and that is why you get 1 neutrons are neutral and therefore charges 0 net charge is 0 and therefore they are made up of u d d and that is equal to 2 by 3 that means there is one up quark that is 2 by 3 minus 1 by 3 that is d and minus 1 so that becomes a neutral particle so proton is having a positive charge is a positive charge and charges plus 1 while the neutron is neutral that the charge is 0 and that is why they are made up of these up up and quarks like this so these are some and this top quark is the heavy assist you can see the mass is is heavy assist 172 gv and this was discovered at Fermi lab accelerator Tevatron when the proton with anti proton been provided and the mass was measured to be about 172 gv this is a heaviest particle now this now you will be wondering can of course now the technology has developed and therefore you can have high energy particles but what was happening earlier earlier also a lot of good physics was done and even without these high energy accelerators so what were the sources of high energy particles so you can see that although theoretically in the right in the beginning of 19th 1905 the Einstein gave this equation and Dirac actually predicted antimatter and pi mesons were predicted in 1935 but the source of studies for particle physics or doing the experiment was that time it was cosmic rays and cosmic rays have the energies right from very small energy electron volt to up to TV they are all available particles and there are different kind of particles are available for example in 1932 itself the positron which is an anti particle of electron was discovered and later on pi mesons were discovered in 1937 so people were able to get all these information using the cosmic rays ions and in cosmic rays the particles of all energies are available now here I have talked about anti protons and pi mesons so one question obviously will come is that can you produce or can you get accelerated anti protons in the lab of course of course the answer is yes but you have to accelerate particles to appropriate energies and appropriate particle then only you can do for example if you collide two protons like in lab in the in the LSE then the then the you cannot simply make it's not possible to support two you two protons interact with each other then you cannot simply make one anti proton and one this is not allowed this is not allowed and the reason being that for example if you take the charge then charge is one here so it is one so it becomes two and charge of this is anti proton is minus one so it becomes zero so the charge is not conserved so that reaction is not allowed so that is why we have two protons collisions and we have p plus p and then this now if you want to have these four particles then the energy conservation says that you need higher energy that is why this anti protons p bar is created at a minimum energy of about 5.3 g and that is how this so this is not allowed you have to have this reaction and that will be possible only at higher energy so one needs highly energetic beam of particles to break the nucleus and instant the constituents that is a fundamental things that high energy new particles can be produced and that comes from the Einstein equation mc square and this has been done at for example large head on colliders which is in Geneva Switzerland where two protons of mass one g v is collide interact with each other and even the particles like Higgs bosons were produced which has a very large mass so you can see that accelerators can do what does and in different fields now let me just talk about little bit of physics of these accelerators so when the when the any charged particle is either traveling or through this electromagnetic force is electromagnetic fields then there will be force acting on it and both electric and magnetic field will be applying some force on that in the electric field force part will be in the same direction while the magnetic force this will be v cross v that means the force will perpendicular to both velocity as well as magnetic field and that is this is the principle this is the force which is used in cyclotons and synchro cyclotrons and so you can get this and you can get details of this for example the rate at which the work can be done on the ion is given by here and sorry this has to be only this portion as this is zero and only this force will be there first one which is e times so here again we have classified accelerators in different categories earlier based on type of ions and and and the whether the particle is heavier or not and things like that there is another category whether the acceleration is is due to electric field or it is because of magnetic field as well for example rate of change of magnetic field can be used for accelerating charged particles particularly electrons in the case of betaton accelerators so if it is a static magnetic field then it will not be accelerating it will only be deflecting the particles but if the v is changing with time then you will find that that can accelerate the particles also because there will be gradient accelerating gradient will be so basically then you can see that there are two categories one is that there is a static field which is given here and the second term will be zero here in this case and the acceleration by dc accelerates the and dc voltages will be done and there are different types of accelerators in this category which is as i said that for nuclear low energy nuclear physics experiment is the most useful one and they can be seen here that they are in the initially in the early late 20s the first one which came was a corporate walton type of multiplier which will which will convert into accelerators then later on the things for technology was improved and the vending graph electrostatic generators were developed and then further improvement led to building of tandem and paletron accelerators they all have different characteristics which i will be discussing and of course the dynamitrons and they are basically all electrostatic accelerators so in this case category i have said that if if the rate of change of magnetic field is used for accelerator acceleration then that accelerator is called betaton and if you are using changing electrostatic then they are called resonating because you have to resonate the particles with the actual frequency and the phase etc and they are called cyclotrons in this category these accelerators come into picture they are they are the one switcher so this is basic background for the accelerators and i have told you that why accelerators are important and why they are required what they can do and what we can gain from them and now let us see that how useful are these dc accelerators they are normally low energy accelerators but their their energy resolutions are much better than the other ones and therefore the for certain kind of studies they are much better than the in this category let me just talk about how the they they are functioning or how their work principle is involved and in this category there are three kind of accelerators which have been used and the first one is let's say this just i explained the how it is working suppose this in the beginning also i have explained that suppose there are two plates and i connect a battery here which is this portion is positive and this is negative that means this plate is negative and this is positive and then you inject a positive ion which is having a charge of q plus then they will be this these ions positive ions will be attracted by this second plate which is at ground potential these particles reach to the second plate the energy gain will be q times voltage so this is the basic principle of this so but the improvement of that is a cockroach walton so this act also like a like a capacitor so if you if you can have several capacitors and you have instead of this dc voltage you can have ac voltage and you use a another circuit which is called which has both capacitors and diode then the voltage can be much higher and you can see here that using this techniques people were able to go to higher voltages and one to about 1.5 million volt could be achieved with this now this cockroach walton type they were all open air open air type and therefore if you try to go to higher voltage there will be breakdowns and we will not able to go to higher voltages while this could be this problem could be overcome in the vendicraft type of accelerators where this whole accelerator is enclosed in a in a pressure vessel and that is a insulating gas is put inside that pressure vessel at higher pressures which could which improve the dielectric constant and therefore we were able to extend the higher voltage on the on the terminal or the point of high voltages which could which was different from the other ones here for example in the case of cockroach walton you are using condensers or capacitors directly while in the case of vendicraft and pelotons the capacitor is made by the electrodes by so they are made there itself so high voltage terminal acts as a capacitor in that case so that has to be charged so it is technology is slightly different now coming back to these accelerators what are the what are the components involved basically now we are talking about this I have explained earlier but the few components which are required of course is a source of charged particles then there has to be accelerating tube because inside which we have to accelerate the particle so and if we don't have a very high vacuum inside the accelerating tube then these particles will collide with the all the gas particles in the tube and they will lose the energy so not only the energy will be lost but also the also the resolution of the beam will be because then there will be a spectrum of energy rather than one energy single energy and therefore you have to have a very high vacuum in this one to avoid the energy loss etc now this this source has to be isolated from the other this source and this and that is what is done by the high voltage column section or the accelerating tube across which we maintain the electric field and that provides the force to accelerate them to the high energy so the beam which is coming at this point is accelerated beam and of course as I mentioned earlier that many times sometimes it is focused beam but many times it is a defocused beam and that has to be focused so here you have to put the beam help handling components like focusing devices and so on ultimately we want to take the beam to the target chamber where the experiments will be are done and therefore that is the ultimate now what is the criteria see ultimately and actually I was mentioning that in the cockroach Walton type you can go maximum up to 1.5 million volts what is that is there any study and Kilpratik studied these phenomena very nicely and he came with a criteria and that is that tells us that at what voltage the breakdown will take place what maximum voltage you can go which of course voltage means the gradient and that is more important than so the Kilpratik he gave the criteria after the empirical criteria based on lot of his studies and above that voltage he gave that at what voltage the breakdown will take place above that voltage the plasma will form or the discharge will form and voltage will not be able to go so in all these accelerators there is a limit to which the voltage can go above that there will be discharge and in these kind of accelerators for example normally normally the safe limit to for operation of these accelerators is about 1 million volt per meter of course in open air it is much smaller but by using very efficient insulating gas like SF6 which has a dielectric constant of about 2.5 times that of air at the same pressure you can go to higher present and still you are able to go to 1 million volt per meter so for example if you take 14 million volt peloton then the length of that accelerator is almost like 14 meters so the gradient is limited to safe gradient is limited to about 1 million volt or something like that now any accelerator which you develop has to have applications and they have to be used and one of the excellent one of the most fruitful application of even the small accelerators like occult valton was to generate the neutrons and there what you do is that you accelerate neutrons bombard on another neutron target it could be the target could be gas or it could be different kind of targets so if you do that then you have to see that to what voltage you have to achieve what we have voltage you have to achieve and the what is the energy required and that will be defined by the q value so if the q value is positive then the even at very low energies the neutrons will be produced for example if you take this dt reaction then the you are able to get two mvb beams two mvb neutron beams and that is because the q value of this is 2.7 mvb and this 2 mvb 2.7 mvb will be distributed on these particles as per the mass and the other other reaction which is very popular even today is dt reaction and in dt reaction when deutone beam of about let us say few hundred keb to up to one mvb or even higher yeah is bombarded on tritium target then helium is produced and a newton will come out of it and that newton will be 14 mvb because the q value 17.67 mvb and that will be distributed to this as per the kinematics between these two particles so the energy of this helium kinetic energy of helium 4 will be 1 by 5 and of the neutrons it will be 4 by 5 so that is why you get 14 mvb and this is one of the best ways to produce one of the unique ways to produce 14 mvb mono energetic neutrons which are even today so even these new these particles they are these accelerated accelerated particles from any of these dc accelerators even cockroach valton type of things are used for many many applications some of them are listed here and they are used in solid state physics ion beam modification of materials atomic physics ion beam analysis of materials like the other code that is that particle induced x-ray emission is called pixie particle induced gamma ray emission is called pig nuclear reaction analysis and elastic recoil detection astrophysics is very useful for this this astrophysics and nowadays they are finding very great uses in ecological research also thank you so this is the end of this lecture


Hey guys welcome back to my YouTube channel as the token person from Ireland in my social network here in Israel. I get asked all the time what's the best Irish pub do I recommend any Irish bars in Jerusalem. Let me answer the second question there straight out the bat. Don't recommend any Irish bars in Jerusalem. There are however a few good, bad Irish bars that recommend saving your money going there instead. However, if you happen to be in Tel Aviv then there is an Irish bar that I re-recommend. It's a place called Molly Blooms. Now they have a few different branches but the kind of flagship branch if you will is where I'm sitting outside today on this very very hot day in Tel Aviv. It's really hot, really sticky so there's nothing quite as good as a Magner's that's cooled you down aka Boomer's in Ireland. This is a popular haunt among the very very small Irish community here which is really mostly Irish Jews like myself. We also have Irish people working on construction projects and Molly's kind of the haunt if you will. Now this particular branch is located on the intersection of Hayan Kone and Mendele and this place on Patrick's Day is absolutely even with people. The reason I like this and don't rate the other Irish bars they've been to in Israel is firstly this is actually a real Irish owned establishment. One of the co-owners is a guy from Dublin and this is also when they'll put the Six Nations up on the screen when the rugby's in season. So this is a real Irish place. The other Irish bars they visited in different cities in Israel have just kind of been you know those kind of almost insulting attempts to be fake Irish with the and all that stuff. So this is the real thing it's really cozy inside. They have as I mentioned Boomer's on top, they have Guinness of course and a few other Israeli drinks as well. They have Irish trad sessions from local visiting Irish musicians and also just Israelis who are into Irish music. So let's go take a look inside. The bar man's kind of given me permission to film so let me take you around Molly Bloom's Irish pub on Hydercon in Tel Aviv. One of the signature features of Molly Bloom's is these mock Irish passports which they have on the way into the bar. These actually are menus. They've got their full drinks and food menu listed there. In terms of food this place does some Irish pub classics such as fish and chips. They've had deep fried mushrooms here not sure they're still on the menu as well of course as brown bread so if you're looking for a taste of home while you're over here in Israel in the hot hot weather then this is a place for some food as well. Subscribers to my YouTube channel may not be aware of this fact but they actually spent a very enjoyable summer in New York City many years ago on what's called the J1 visa interning at Irish Central which is a Neelodide Irish American news empire and while there I got to visit a lot of Irish communities living in the Tri-State area up in the Yonkers as well as in New York City and that combined with my extensive field research here in Israel by which I sitting in any Irish pub I could find on Google Maps. I come to the conclusion there are a few things that distinguish a truly good Irish pub outside of Ireland which is kind of not a great alternative. The first thing there's here's a few things I feel tips I'd give to people trying to set the quality of them so the first thing about Irish pubs is it's always bar service you never see in if you go to in Ireland at least I've almost never seen it it's been seven years since I live there full time but you don't really have waiters and waitresses coming around to the tables you go up to the bar you order your drink so I love that about Molly Blooms and any other bar that nature you just go straight up order your pint. The second thing is that people are drinking pints largely instead of slishes or even a quarter liters which you'll see a lot of in Israel. You always have your few kind of dowdy characters camped out by the bar for the whole day part of the furniture so you have those in Molly Blooms as well you have of course the actual wall fixtures and you have the kind of characters as well just kind of sitting there by the bar making banter with the bar man. What else can I point to in Ireland bars are first and foremost venues for mixing and mingling over a pint and that's another important factor but the normative value of alcohol in Israel they do what's called a half leader in Hebrew it's called a head seat and in Ireland the standard measure is called a pint so when you've got yourself a good pint glass that's another indication that you're in a place that actually understands Irish bar culture so the focus in Irish bars is very very much on the consumption of pints with your friends and the food is kind of secondary so when you see Irish pubs in Israel where they have that big kind of neon chamarrox signery and you can just see there's never been an Irish person involved and table service and they're mostly selling you know stuff they think is Irish food like pizza you know you're in a fraud of an establishment so Molly Blooms is a real deal it's had the real input of actual Irish people and that's very evident when you walk inside and you see the way the place is decorated and just the way it's laid out it's very much an Irish bar kind of feel those are just just kind of some of the things I point to when you're when you're sizing up the Irish bar experience Molly Blooms is a close to the real deal as I found here in Israel it's a place where you'll actually find people who come from come from Ireland and things that are important in Irish culture like for instance the Six Nations Rugby Tournament or the GAA matches they'll put upon the screen here whereas if you go to an Irish bar in Israel that's had no involvement from Irish people just run by Israelis they'll never have heard of the Six Nations never mind putting it on that locals interested in Irish culture is checking out Molly Blooms here in Tel Aviv


This video is produced with support from the Government of Canada's Social Development Partnerships Program, Disability Component. The opinions and interpretations in this video are those of the creator, and do not necessarily reflect those of the Government of Canada. Welcome to a Crash Course in Nells. This series is designed to give you the tools you need to produce accessible e-books, making them even more enjoyable for all readers. My name is Danny. I'm an accessibility tester with the National Network for Equitable Library Service. I was born blind, so I usually read books audibly or in braille. My name is Caden Farris. I don't have a sight impairment, so I can read print just fine. It's just reading print and understanding print are two very different things. Footnotes or endnotes are informative pieces of text that add context to something that's been included in the main narrative. So there might be a quote that fits in well with what the author is talking about, and then a little footnote that you can click on to find out who said the quote. A footnote is going to give more context to something that was important that appeared in the text, so it might indicate who is credited with a particular citation. It might indicate the venue or the year when that was said. It could indicate a URL of where that information was pulled from the internet, just something to back up or to give more information about a particular passage. So they say a whole bunch of stuff in the Titanic, and then in the footnotes it might be, did you know that there were half steel, half copper bolts used in this part of the ship? Footnotes should not break up the main narrative. So oftentimes we'll see footnotes placed at the end of pages or in between paragraphs, and that is not where they belong. Footnotes should appear at the very least at the end of chapters and ideally at the end of books, and that applies for endnotes as well. So it generally disrupts the reading experience and it can really hurt the understanding of the book. If I'm reading a book and all of a sudden there's a footnote, especially with a cognitive disability, it can really mess up the train of thought. What we want to see is end matter at the end of the main narrative, be it a chapter or the book where all these footnotes are listed. So they can be set up in a list, they can have numbers that correspond with the text, but they should not disrupt the main narrative. That's our number one goal here is whatever we do, we don't want to break up that main narrative because it could be set up in a way that's easy to skip visually, screen readers just going to barrel right through it. So we want to put those footnotes at the end where they're not going to disrupt the main reading experience. Footnotes should be indicated with a number that is set up so that screen readers know what it is and visual readers know what it is as well, and that number should correspond to an end note at the back of the book where they can find more information. When setting up your footnotes, it's very important to remember that each footnote number has to be a link. In the digital publishing world, we have such an amazing opportunity to direct readers where we want them to go. So directly after that citation or the passage that you want to add more context to, that footnote number needs to be an active link and it should be captioned with the number of the footnote. So that makes it very clear to everyone what's going on. So they read that passage, they click that note that takes them to the end of the book where they can read all about the extra information and then at the end of that footnote we need to have a return to text link and that just jumps them right back to where they were so they can keep reading. So it's a seamless experience. They read the quote, they click the footnote. Okay, that was said by Benjamin Franklin. Very cool. They click continue reading or return to reading, whatever you want to call it and then they keep reading the main narrative. Footnotes should be presented in the list. That really adds structure to your content. We can have heading separating chapters of footnotes, but we want those footnotes to be in a list so that it's really clear how many footnotes there are and exactly what material we're trying to present.