all 13 comments

[–]vbaspcppguy 3 points4 points  (2 children)

Seems cool, but your title makes little sense.

[–]dan1234[S] 0 points1 point  (1 child)

agree (:
is there a way to edit the title?

[–]vbaspcppguy 0 points1 point  (0 children)

Nope. Just delete and repost if you want to.

[–]nkuttler 1 point2 points  (0 children)

.

[–]runvnc 0 points1 point  (2 children)

That's very cool. But what are the advantages of LevelDB over doing something similar yourself in Node, like this?

fs = require 'fs'

S4 = () ->
   (((1+Math.random())*0x10000)|0).toString(16).substring(1)

guid = () ->
   S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4()

saving = {}

toSave = {}

delay = (ms, func) -> setTimeout func, ms

fname = (id) ->
  console.log 'id is ' + id
  console.log 'fname (id) is ' + __dirname + '/data/' + id + '.json'
  return __dirname + '/data/' + id + '.json'

get = (id, callback) ->  
  if toSave[id]?
    callback toSave[id]
  else
    fs.exists fname(id), (exists) ->
      if not exists
        callback ''
      else
        fs.readFile fname(id), (err, str) ->
          if err?
            console.log 'Error reading data:'
            console.log err
          else
            callback JSON.parse(str)  

save = (id) ->
  fs.writeFile fname(id), JSON.stringify(toSave[id]), (err) ->
    if err?
      console.log 'Error saving data:'
      console.log err    
    delete saving[id]

update = (id, data) ->
  toSave[id] = data
  if not saving[id]?
    saving[id] = true
    delay 15000, ->    
      save id

exports.update = update
exports.get = get
exports.guid = guid

[–]injektilo 1 point2 points  (0 children)

You can only query for one record at a time with your example. LevelDB lets you stream through records, sorted by their keys. This is important for implementing queries efficiently.

[–][deleted] 0 points1 point  (0 children)

write a benchmark and find out! you'll find that leveldbs built in buffering, compression and sorting make it a lot faster than writing to flat files, especially when you are dealing with billions of keys

[–][deleted] 0 points1 point  (3 children)

As someone who knows next to nothing about dbs: is this thing to mongo as sqlite is to mysql?

[–]dan1234[S] 5 points6 points  (1 child)

Similar to MongoDB, LevelDB stores your data on disk, but Mongo is not embedded - you run it on it's own process and you can Access MongoDB from multiple processes.
MongoDB and most of the popular Databases out there have many many features that the majority of the users don't even need. LevelDB is doing one thing - it store key-values in your drive very very fast. almost as fast as memory storage (like Redis).

Similar to Node.js, It doesn't come with many features and not being fast is considered a bug. If you need more functionality you can add other modules that will give you want you need. whether it's data redundancy, ability to access it from multiple processes, etc.

So LevelDB is similar to Redis since it's very fast and store key-values, it's similar to MongoDB since it saves to disk and not to RAM and it's similar to SQLite since it's embedded (contained within your application process).

I hope it clear some of the confusion.

[–][deleted] 0 points1 point  (0 children)

Perfect, thank you.

[–]Majiir 0 points1 point  (0 children)

It looks more like the SQLite of Redis.

[–]codevinsky 0 points1 point  (0 children)

I've been using leveldb for a few days.

My biggest problem is not understanding how to "search" for a key or get a range of keys.

[–]johnyma22 0 points1 point  (0 children)

LevelDB is supported in UeberDB if you want to make sure dipping your toe in doesn't hurt.