Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Some questions about BerkeleyDB

45 views
Skip to first unread message

cylin

unread,
Sep 16, 2004, 2:36:20 AM9/16/04
to
Dear all,

I'm a rookie to use BerkeleyDB ( version 4.2.52)
I have some qestions, please help me figuring out.
Thanks.

1. In db_create() function, there is a flag named "app_private".
Is there an example to use it?

2.First to create a Berkeley DB, we may set flags,page or cache size.
After close it, it'll save to file.
If I load this DB from this file, should I set flags,page or cache size
again?

3. If I create several database with a DB environment,
do I need to create a DB environment again in the next time so that
opening and operating other database?

4.In http://www.sleepycat.com/docs/api_c/dbt_class.html#DB_DBT_MALLOC
and http://www.sleepycat.com/docs/ref/am/second.html.
I still don't know whether set it or not, and how to do?
Can Any piece of c/c++ sample codes show it?

5. Why DB->remove() only remove the database name, but the data still exist?

6.I may use many secondary index databases for query when the application is
live.
So I need to create them. But close them, will save them to file.
When using an associated function to query,
key/data pairs maybe are varied in same secondary index database every
time.
Regarding this, I treat secondary index databases as temporal.
They should not save to file.
Is my opinion right?

Thanks for your help.

Regards,
cylin.

Patrick Schaaf

unread,
Sep 16, 2004, 3:17:02 AM9/16/04
to
"cylin" <cy...@avant.com.tw> writes:

>1. In db_create() function, there is a flag named "app_private".
> Is there an example to use it?

It is a pointer. It provides you with a place to put an arbitrary pointer
of your choice, and later retrieve it. Berkeley DB does not care about
the value of that pointer. You are free to use it any way you want, or
ignore it completely. You set and retrieve it like any other pointer
member of a C struct: db->app_private = frodo; printf("%p\n", db->app_private);

>2.First to create a Berkeley DB, we may set flags,page or cache size.
> After close it, it'll save to file.
> If I load this DB from this file, should I set flags,page or cache size
>again?

If I remember correctly, the documentation is pretty explicit in telling
you, for each flag and other setting, whether it is stored with the
environment, whether it needs to be specified again, whether it needs
to be specified again identically, or whether it can be changed at
any time as you see fit. There is no general answer, there is just
detailled documentation.

>3. If I create several database with a DB environment,
> do I need to create a DB environment again in the next time so that
>opening and operating other database?

The first time round, when working with environments, you need to create
the environment, by specifying the DB_CREATE flag to the environment's
->open() function call. Once created, you could omit DB_CREATE, but you
don't need to: an existing environment will be used even when you
specify DB_CREATE another time.

This question I don't understand. What is "it"?

>5. Why DB->remove() only remove the database name, but the data still exist?

Does it? What do you mean with "data still exists"? Did you terminate any
application using that database, before calling ->remove()?

>6.I may use many secondary index databases for query when the application is
>live.
> So I need to create them. But close them, will save them to file.
> When using an associated function to query,
> key/data pairs maybe are varied in same secondary index database every
>time.
> Regarding this, I treat secondary index databases as temporal.
> They should not save to file.
> Is my opinion right?

No, I don't think so. How would such a temporary secondary index be created
in the first place, initially, when your application starts up?

best regards
Patrick

cylin

unread,
Sep 16, 2004, 5:48:00 AM9/16/04
to
Hi Patrick,

Thans for you answer. But I am still poor to understand.

> >3. If I create several database with a DB environment,
> > do I need to create a DB environment again in the next time so that
> >opening and operating other database?
>
> The first time round, when working with environments, you need to create
> the environment, by specifying the DB_CREATE flag to the environment's
> ->open() function call. Once created, you could omit DB_CREATE, but you
> don't need to: an existing environment will be used even when you
> specify DB_CREATE another time.

I mean that reopen all databases from disk after all databases and DB
environment closing.
Do I need create a DB environment again?
If not, it means this DB environment had been saved to file?

> >4.In http://www.sleepycat.com/docs/api_c/dbt_class.html#DB_DBT_MALLOC
> > and http://www.sleepycat.com/docs/ref/am/second.html.
> > I still don't know whether set it or not, and how to do?
>
> This question I don't understand. What is "it"?
>

Sorry, I mean when we query by a key, and get return data( or key).
If we set DB_DBT_MALLOC or not ,what is the difference about
data.data/key.data?


> >6.I may use many secondary index databases for query when the application
is
> >live.
> > So I need to create them. But close them, will save them to file.
> > When using an associated function to query,
> > key/data pairs maybe are varied in same secondary index database every
> >time.
> > Regarding this, I treat secondary index databases as temporal.
> > They should not save to file.
> > Is my opinion right?
>
> No, I don't think so. How would such a temporary secondary index be
created
> in the first place, initially, when your application starts up?

So, I don't care about it? Just create,open, then query and close?

> >5. Why DB->remove() only remove the database name, but the data still
exist?
>
> Does it? What do you mean with "data still exists"? Did you terminate any
> application using that database, before calling ->remove()?
>

I have closed that database, then create, remove.
And I use "ultraedit" to view the database file, the data I stored still
exist.
Here is my code.
------------------------------------------------------------
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include "db.h"
using namespace std;
class CData {
public:
int m_iOID;
int m_iAge;
int m_iSalary;
char m_szName[16];
public:
CData(int ID,char* name, int age, int
salary):m_iOID(ID),m_iAge(age),m_iSalary(salary) {
strcpy(m_szName,name);
}
};

void insert(DB *dbp,CData* poData)
{
printf("%d,
name=%s,age=%d,salary=%d\n",poData->m_iOID,poData->m_szName,poData->m_iAge,p
oData->m_iSalary);
DBT key, data;

memset(&key, 0, sizeof(DBT));
memset(&data, 0, sizeof(DBT));

key.data=&(poData->m_iOID);
key.size=sizeof(int);

data.data=poData;
data.size=sizeof(CData);


if(dbp->put(dbp,NULL, &key, &data, 0) == 0) {
printf("\tInsert:");
}
else {
printf("\tFail to insert:");
}
printf(" OID=%d,
name=%s,age=%d,salary=%d\n",poData->m_iOID,poData->m_szName,poData->m_iAge,p
oData->m_iSalary);
}


int main()
{

DB *dbp;

if (db_create(&dbp,NULL,0)) {
return -1;
}

if (dbp->open(dbp,NULL,"a.db", "primary", DB_BTREE, DB_CREATE, 0666)) {
return -1;
}

// insert data.........................
int l_iOID=0;
CData* l_poData1=new CData(++l_iOID,"John", 20,100);
CData* l_poData2=new CData(++l_iOID,"Robber", 35,500);
CData* l_poData3=new CData(++l_iOID,"Joe", 21,100);
CData* l_poData4=new CData(++l_iOID,"Josh", 40,400);
CData* l_poData5=new CData(++l_iOID,"Reggie", 35,600);
CData* l_poData6=new CData(++l_iOID,"Joe", 35,800);

insert(dbp,l_poData1);
insert(dbp,l_poData2);
insert(dbp,l_poData3);
insert(dbp,l_poData4);
insert(dbp,l_poData5);
insert(dbp,l_poData6);
// end of insert data


DBT data, pkey, skey;
memset(&skey, 0, sizeof(DBT));
memset(&pkey, 0, sizeof(DBT));
memset(&data, 0, sizeof(DBT));


// get data using OID
int OID=3;
skey.data =&OID;
skey.size =sizeof(int);

if (dbp->get(dbp, NULL, &skey, &data, 0)== 0) {
CData* l_poData=(CData*)(data.data);
printf("\tGot: OID=%d,
name=%s,age=%d,salary=%d\n",l_poData->m_iOID,l_poData->m_szName,l_poData->m_
iAge,l_poData->m_iSalary);
}
else {
printf("fail to get data using OID =%s \n",*(int*)(skey.data));
return -1;
}
// end of get data using OID

if (dbp->close(dbp,0)==0) {
printf("Close primary database ok\n");
}


delete l_poData1;
delete l_poData2;
delete l_poData3;
delete l_poData4;
delete l_poData5;
delete l_poData6;


// reopen database and remove it
if (db_create(&dbp,NULL,0)) {
return -1;
}
else {
printf("create database ok\n");
}

cin.get(); // pause, and use ultraedit to check database name "primary"

int rv=dbp->remove(dbp,"a.db","primary",0);
if (rv!=0) {
printf("remove database:%s\n",db_strerror(rv));
return -1;
}
else {
printf("remove database ok\n");
}
printf("Finish.........\n");
return (0);
}
------------------------------------------------------------
>
> best regards
> Patrick


Patrick Schaaf

unread,
Sep 16, 2004, 10:31:09 AM9/16/04
to
Hi Cylin,

I'm only replying to one point; I'm not so sure about the others.

>> >4.In http://www.sleepycat.com/docs/api_c/dbt_class.html#DB_DBT_MALLOC


>I mean when we query by a key, and get return data( or key).
>If we set DB_DBT_MALLOC or not ,what is the difference about
>data.data/key.data?

DB_DBT_MALLOC is only relevant for the DBT in which Berkeley DB
gives you back data from the database.

Without DB_DBT_MALLOC, i.e. with DBT.flags set to 0, after the successful
query call, you will find in data.data a pointer into memory which is
under the responsibility of Berkeley DB. It may be (I don't know for sure)
a pointer into the memory mapped shared environment. You can copy from
there to some place safe, and I think you must NOT assume that the pointer
will be valid after another call to a retrieval function.

On the other hand, when you set (before the retrieval call) that DBT's
.flags field to DB_DBT_MALLOC, then the retrieval function of Berkeley DB
will automatically call malloc() to get _new_ memory for the retrieved
data, and you will find after the retrieval that data.data points
to that memory. As it is newly malloc()ed, you can access it for as
long as you want. IMPORTANT: it is also your responsibility to
use free() when you don't need it any more.

best regards
Patrick

cylin

unread,
Sep 16, 2004, 10:24:00 PM9/16/04
to
Hi Patrick,

I see cleary.
Thank you very much.

Regards,
cylin.
"Patrick Schaaf" <mailer...@bof.de> 撰寫於郵件新聞
:4149a3ad$0$138$9b62...@news.freenet.de...

0 new messages