#include "../include/db.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* Open a database */
int open_db(DB **dbpp, const char *file_name, int is_secondary)
{
DB *dbp;
u_int32_t open_flags;
int ret;
/* Initialize the DB handle */
ret = db_create(&dbp, NULL, 0);
if(ret != 0)
return ret;
/* Point to the memory malloc'd by db_create() */
*dbpp = dbp;
/* Set the open flags */
open_flags = DB_CREATE;
/*
* sorted duplicates.
*/
if(is_secondary) {
ret = dbp->set_flags(dbp, DB_DUP);
if(ret != 0)
return ret;
}
/* Now open the database */
ret = dbp->open(dbp, /* Pointer to the database */
NULL, /* Txn pointer */
file_name, /* File name */
NULL, /* Logical db name */
DB_BTREE, /* Database type (using btree) */
open_flags, /* Open flags */
0); /* File mode. Using defaults */
if(ret != 0)
return ret;
return 0;
}
int main()
{
char keybuff[1024*100];
char valuebuff[1024*100];
DBT key, value;
int ret = -1;
char *buffer;
bzero(&key, sizeof(DBT));
bzero(&value, sizeof(DBT));
DB* dbp;
printf("open db\n");
open_db(&dbp,"new-ec-rule.db", 1);
DBC *dbc;
dbp->cursor(dbp, NULL, &dbc, 0);
key.data = keybuff;
key.ulen = 1024*100;
value.flags = DB_DBT_USERMEM;
printf("buffer addr :%d\n",(int)buffer);
printf("buffer size :%d\n",value.size);
}
dbp->err(dbp, ret, "DB->get");
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* Open a database */
int open_db(DB **dbpp, const char *file_name, int is_secondary)
{
DB *dbp;
u_int32_t open_flags;
int ret;
/* Initialize the DB handle */
ret = db_create(&dbp, NULL, 0);
if(ret != 0)
return ret;
/* Point to the memory malloc'd by db_create() */
*dbpp = dbp;
/* Set the open flags */
open_flags = DB_CREATE;
/*
* sorted duplicates.
*/
if(is_secondary) {
ret = dbp->set_flags(dbp, DB_DUP);
if(ret != 0)
return ret;
}
/* Now open the database */
ret = dbp->open(dbp, /* Pointer to the database */
NULL, /* Txn pointer */
file_name, /* File name */
NULL, /* Logical db name */
DB_BTREE, /* Database type (using btree) */
open_flags, /* Open flags */
0); /* File mode. Using defaults */
if(ret != 0)
return ret;
return 0;
}
int main()
{
char keybuff[1024*100];
char valuebuff[1024*100];
DBT key, value;
int ret = -1;
char *buffer;
bzero(&key, sizeof(DBT));
bzero(&value, sizeof(DBT));
DB* dbp;
printf("open db\n");
open_db(&dbp,"new-ec-rule.db", 1);
DBC *dbc;
dbp->cursor(dbp, NULL, &dbc, 0);
char* first=NULL;
//下面6行如果删除,则有内存泄露,因为db会自己malloc内存,但是这块内存不能直接free。
key.data = keybuff;
key.ulen = 1024*100;
value.flags = DB_DBT_USERMEM;
value.data = valuebuff;
value.ulen = 1024*100;
while( (ret = dbc->get(dbc, &key, &value, DB_NEXT)) == 0) {
buffer = (char *)value.data;printf("buffer addr :%d\n",(int)buffer);
printf("buffer size :%d\n",value.size);
}
dbp->err(dbp, ret, "DB->get");
return 0;
}