Coder Social home page Coder Social logo

Comments (10)

hoytech avatar hoytech commented on August 22, 2024 1

It looks like you are assuming your values will be NUL-terminated:

printf("We got '%s'\nValue '%s'\n", k.data(), v.data());

But your puts above aren't writing the NUL byte.

Don't use the C string routines since you won't be able to store NUL bytes in your keys/values. Instead do something like:

std::string myKey(k.data(), k.size());
std::string myValue(v.data(), v.size());
std::cout << "We got " << myKey << " and " << myValue << std::endl;

(untested)

Or, better yet, upgrade to C++17 and use my fork and get the string_view hotness :)

from lmdbxx.

DrissiReda avatar DrissiReda commented on August 22, 2024

I know about string_view and about your fork, but I have constraint that prevent me from going above c++11. Since you're here, could you tell me how can I force my database to sorted by input order (instead of lexicographically). Also on first creation of database, it always crashes with "bus_error", but if I open the environment, then close it then open it again and execute my code, it works.

EDIT: How can I make sure my puts do add the null byte, in order to avoid this problem?

from lmdbxx.

hoytech avatar hoytech commented on August 22, 2024
  1. Make it so your keys increase every time you insert, or use a secondary index

  2. I don't know, I'd need code to reproduce it. Make sure you are creating a big enough MAPSIZE, and the same mapsize each time. Make sure you commit the transaction that creates the tables.

  3. I suggest not storing the NUL byte. It's a waste of space since LMDB tracks size anyway. Furthermore, it indicates you are unable to store NUL bytes as part of your keys or values (as I described above)

from lmdbxx.

DrissiReda avatar DrissiReda commented on August 22, 2024

How can I use a secondary index?

from lmdbxx.

hoytech avatar hoytech commented on August 22, 2024

Every time you insert into your main table, you also insert into another table. In this secondary table, the key is an increasing integer, and the value is the key into your main table.

Then when you want to iterate over your items in insertion order, iterate through the secondary index. For each value, use it as a key to look up the item from the main table.

from lmdbxx.

DrissiReda avatar DrissiReda commented on August 22, 2024

I'm sorry for all these questions but I can't find where else to ask them, is it possible to get the position of an entry without iterating through my database with a cursor and then incrementing a counter?

from lmdbxx.

hoytech avatar hoytech commented on August 22, 2024

It's OK. What do you mean get the position? You can position the cursor directly to a know key with cursor ops like MDB_SET, or go to somewhere nearby with MDB_SET_RANGE.

If you mean get an item by its postion index (ie, get the 10th element in the DB) then afaik this is not possible. If you're always appending to the DB (and never removing or inserting in the middle) then you could maintain the position index in a secondary index.

from lmdbxx.

DrissiReda avatar DrissiReda commented on August 22, 2024

I meant, doing a dbi.get() to search for the position index of a certain key, (e.g inputting "user_email" and receiving 10). Is this possible without holding duplicate databases with secondary indexes?

from lmdbxx.

hoytech avatar hoytech commented on August 22, 2024

AFAIK, no.

from lmdbxx.

DrissiReda avatar DrissiReda commented on August 22, 2024

I'll see which one would be more cost effective, iterating through the database and counting, or keeping a duplicate of each concerned database with indices.

from lmdbxx.

Related Issues (19)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.