Question

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

Était-ce utile?

La solution

I've taken a quick look at http://pybsddb.sourceforge.net/bsddb3.html , and the following idea looks promising: create a DBCursor object, call its .set method to find key 10, then call its .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 and next on the cursor.

Autres conseils

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
  }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top