Recuperar un rango de datos de Berkeley DB

How can I retrieve a range of data such as 10 < key < 20 from Berkeley DB? I couldn't find anything by searching.

preguntado el 09 de septiembre de 13 a las 23:09

¿Qué lenguaje de programación estás usando? -

I'm using python, but it seems I'd have to use C/C++ for other reasons anyway. So either works. -

--other reasons being bsddb3 python library doesn't seem to support integer keys/values for BTREE and HASH data structures. -

2 Respuestas

He echado un vistazo rápido a http://pybsddb.sourceforge.net/bsddb3.html , and the following idea looks promising: create a DBCursor object, call its .set method to find key 10, luego llama a su .next method until you reach 20.

I don't know the details of the C API, but I'd try the same idea: try to create a cursor, and call functions named like set y next en el cursor.

Respondido el 09 de Septiembre de 13 a las 23:09

Thx, that'd work. Just thought there would be a straight way like in Mongo. - xcorat

Maybe this code will help you. It extracts entries with key1<=key<=key2, but in can be modified for your condition. I use first DB_SET_RANGE flag to find key=>key1, and then DB_NEXT flag to get next values and check are they <=key2.

void get(DB *dbp, int key1, int key2){
  DBC *curs;
  DBT k,v;
  int fl;

  // Get a cursor
  dbp->cursor(dbp, NULL, &curs, 0);
  if (!curs) _dberr("can't get a cursor");

  // Set DBT for 1st key and value
  memset(&v, 0, sizeof(DBT));
  memset(&k, 0, sizeof(DBT));
  k.data = &key1;
  k.size = sizeof(key1);

  fl = DB_SET_RANGE; // first key will be >=key1
  while (curs->c_get(curs, &k, &v, fl)==0 &&
         key2 >= *(int *)k.data){
    fl = DB_NEXT;
    // use v.data
  }
}

contestado el 05 de mayo de 16 a las 17:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.