1

I was trying to apply an iteration to my leveldb file, but unfortunately I couldn't get the result. The problem that I'm facing is a segmentation fault in using the pointer of the iterator. I used gdb And I got that the problem is in the line

leveldb::Iterator* it = db->NewIterator(leveldb::ReadOptions());

(I added before some values to this file /tem/userDb , and the add is working good .)

#include <assert.h>
#include <leveldb/db.h>
#include <iostream>
#include <sstream>
using namespace std;

void iteration(leveldb::DB* db)
{
    leveldb::Iterator* it = db->NewIterator(leveldb::ReadOptions());
    for (it->SeekToFirst(); it->Valid(); it->Next())
    {
        cout << "key :" << it->key().ToString() << " : "
             << "value:" << it - > value().ToString() << endl;
    }
    if (false == it->status().ok())
    {
        cerr << "An error was found during the scan" << endl;
        cerr << it->status().ToString() << endl;
    }
    delete it;
}

int main(int argc, char *argv[])
{
    leveldb::DB* db;
    leveldb::Options options;

    options.create_if_missing = true;

    // erase error if the database exists
    options.error_if_exists = true;
    leveldb::Status s = leveldb::DB::Open(options, "/tmp/userDb", &db);
    if (!s.ok()) 
        cerr << s.ToString() << endl;
    iteration(db);
    delete db;
}
Increasingly Idiotic
  • 5,700
  • 5
  • 35
  • 73
MadHer
  • 91
  • 3
  • 11

1 Answers1

0

Not familiar with the leveldb API, but you're using db regardless of whether s.ok() is true or false. I'd assume that if s.ok() is false, db is either NULL, or in a state such that iteration or other operations won't work.

You should change the code to:

if (!s.ok()) {
    cerr << s.ToString() << endl;
    return -1;
}
Horia Coman
  • 8,681
  • 2
  • 23
  • 25
  • actually you are right , after adding that return -1 , I didnt got the error of segmentation fault but got this "Invalid argument: /tmp/userDb: exists (error_if_exists is true) ", because its clear that the programm ended in the return -1 , what can be the cause in your opinion sir ?I dont understand why the pointer of the db is not working while its physically existing in my folders – MadHer May 19 '18 at 12:17
  • @MadHer -- Wouldn't the API you're using have a function to return more information as to the error? An API just returning `true` or `false` wouldn't sound robust -- there should be an additional function you would call to get more information once you get a failure return value. – PaulMcKenzie May 19 '18 at 12:28
  • @PaulMcKenzie actually there is not , else than s.ToString , what I know is that the problem is with the db because its existing , when you use for the first time there is no prblm ( creation time ), when you try to open it and to apply some updates its not working anymore – MadHer May 19 '18 at 13:03
  • @MadHer the message has all seems it's complaining that the database already exists. It does so because `options.error_if_exists` is set to `true`. Set it to `false` if it makes sense and won't overwrite it. – Horia Coman May 19 '18 at 13:20
  • @HoriaComan I set it to false , and actually guess its working because no more error about db existence , thanks for the help , I will update it more – MadHer May 19 '18 at 15:57