LevelDB & Crypto

Secondary Indexes

LevelDB & Crypto

Check out a free preview of the full LevelDB & Crypto course

The "Secondary Indexes" Lesson is part of the full, LevelDB & Crypto course featured in this preview video. Here's what you'd learn in this lesson:

James introduces how to create multiple keys for each post to create a secondary index.


Transcript from the "Secondary Indexes" Lesson

>> So you might notice that we have some other data in our keys that we might wanna, for example, list out the posts, but sort them by this timestamp. So if we also want to sort just based on username, which is what we can currently do, so if I want to sort, for example by a particular username, if we had multiple users, this would be a little bit better of an example.

But I want to get all of the posts that I've written, I can stick my username in there and then this ought to just list out everything that I've written. Or sorry, post, sorry, not user. So now this query lists out all of the posts that I've made.

But if I wanna list out the posts made by everyone based on the timestamp, then this key structure isn't gonna work. So what we can do instead now is builds a different way of inserting our documents, so every time we insert a new post, we can insert a second key.

That's gonna help us index into here. Right, so here's an example of what that might look like, so this is a little program that we can use to insert a document. So here I'm just using an ID that's based on some entropy. And when we insert the post, we do it like normal with the post in the username key, but we also have a new key right here.

So I think for the example that I wanna create we'll swap the order of the timestamp and the username so that we can list all of the posts across the entire database by timestamp. This is the kind of thing that you get for free with relational databases. So this is not the best example, because you could just use one of those, but if you have to build the code that works in the browser, well, you don't have any of that, so you have to build it yourself.

So, here's how to do that. All right, so why don't I make a new directory, I'll call it posts, and we can build a couple of Command-Line tools that are gonna insert new users and insert posts written by those users, and then we can build up some different kinds of queries based on this key structure.

All right, so the first thing we need to do is make a maybe user add method, we'll require level like normal Instantiate to post.db, we'll call it. And now we can call db.put, because with a user, maybe we don't have secondary indexes for users quite yet. So I'm just going to do user, and then I'm going to get the name from Command-Line argument.

And this time I'm gonna remember to turn on the value encoding of json. So that's what those object objects strings were. In case you ever see those, that's why. Okay, so we don't really need much other data. I guess I could put like a bio something in here, but I'm just gonna use an empty object for the value, because it's not too important.

So here's our basic code to add a user. All right, so very simple. Let's add a few users now, so substack, tuxkitty, that's my cat in. dogdoggerson. Any other good names? Anybody on the chat in person? Doggy McDoggerton? doggymcdoggerton, often confused for dogdoggerson, I'm sure. Anyways, that's pretty good.

Okay, so we have our user ad script. Why don't we go ahead and build a little script that's now gonna dump all of the users, just pretty much just like the other example, but I think it's good to repeat this kind of, whereas those groups into your brain, I hope so.

I'll call this one userlist.js, maybe userls like that, no, I like list better, that's good. Okay, so, I'm just gonna copy the useradd code, cuz it does the setup, which we want to be the same. But now, we're gonna do a db.create ReadStream now, and we're gonna use that to2 to consume that stream.

We could also have additional command line arguments to provide things like the greater than, less than keys, but I'm just gonna dump everything with this example, I think. So the greater than key is just going to be user, and the less than is gonna be user tilde, okay?

And then we need to consume that. So here's how we can consume that stream. And then make sure to call next. Okay, that's just pretty much the same thing that we did before. Here we go. So here's all of our users. I can format this a little more nicely so that maybe we don't, we can just pull out the user part of the key.

That would be a good thing to do. So, row.key.split on our delimiter exclamation mark 1, because it goes user, exclamation mark, and then the user name. If you're uncomfortable about using all of these loose delimiters in your code, there's a module called bytewise that's kind of, it's a bit slower, but it does do this kind of, it does delimiters a bit more intelligently for you.

But all of the same ideas about lexicographic ordering apply, and it adds additional kinds of type comparisons on top of that. I'm not gonna go into it much, because I don't use is too often, but it's good to know about. It's called bytewise on NPM, in case you want to look it up.

All right, so I think now our user list, right just prints out the usernames, sorted, which is nice.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now