Check out a free preview of the full LevelDB & Crypto course:
The "Subleveldown" 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 Subleveldown, which a library for LevelDB that creates nested sub-databases with unique namespaces;

Get Unlimited Access Now

Transcript from the "Subleveldown" Lesson

>> Now that we've seen some examples of how you can kinda build something that's less than trivial in LevelDB, I wanna talk about some of the libraries that are available in the ecosystem of NPM. And sort of what some good design principles are for using those libraries and also building your own libraries.

[00:00:20] Because this, I think, one of the biggest strengths of LevelDB is its quite vast ecosystem of packages that you can find on NPM, hundreds of packages to do all kinds of stuff. The first one is called subleveldown. There's actually another package called sublevel that you might also see and they do pretty much the same thing.

[00:00:41] I like this one a little better. But you can use whichever one you like. So what subleveldown lets you do is it lets you organize your key space into sublevels. So just like we were doing with our posts with these kinds of key prefixes, so subleveldown, it can take care of that for you.

[00:01:04] But it builds a new dummy database instance, where it's going to automatically prepend a certain key prefix onto your namespace. This is really useful if you wanna take a single database on disk and farm out different sort of namespaces of that database into other packages that do things.

[00:01:27] So that they don't have to worry about stepping on each other's toes, writing to the same keys, that kinda stuff. So just as an example for what that looks like, we can write a quick little demo using subleveldown. First thing you can do is require subleveldown after you npm install it, so you do subleveldown.

[00:01:52] And so you set up your LevelDB like normal and create an instance called sub.db, maybe without a value encoding this time. And then you can create separate databases that have a prefix. So how that works is that you can do sublevel. You give I think the DB as the first argument, and then you give the prefix name as the second argument.

[00:02:19] So we'll be pretty unoriginal, call them a and b. And now, just to show you that, in fact, these are separate namespaces, we'll try to write to the same key. So adb.put, count, and we'll just kind of do the same little count demo, but maybe we'll have two counts that go at the same time.

[00:02:48] So maybe one of our counters will increment by ten and the other increment by one just so we can see that they're separate, okay? So our adb.get, and then we can do adb.put('count', (value or 0)+1. And just like before, I meant to save that to a variable. So you put that value in, and then we can log it out.

[00:03:25] So ('a:count', n), do the same thing for b. So all I need to do is replace adb with b, and then we should have two counters that operate in their own little namespaces. So now if I run this program, we see 1, 11. First of all, they're strings, which is pretty funny.

[00:03:53] Because we're not using the value encoding, we have to actually convert those two numbers if we wanna use them. The second thing is that I meant to increment them differently. So here we're gonna add one to one of them and the other one we'll add ten. So that ought to fix it.

[00:04:10] Yeah, here we go, so 112, 121, 113, 131, works like we would expect. So behind the scenes, subleveldown is just doing the same kind of trick with a delimiter and a prefix, and it's formatting these namespaces for us. But what's cool is that if you're dealing with a sublevel, you don't have to worry as much about formatting the greater than and less than queries.

[00:04:39] Because you're only gonna get data that exists inside of that namespace.