BDB重复数据操作

/*
 ============================================================================
 Name        : test_bdb.c
 Author      : 
 Version     :
 Copyright   : Your copyright notice
 Description : Hello World in C, Ansi-style
 ============================================================================
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <db.h>

DB *DBOpenRead(char *psFileName)
{
	DB *dbp = NULL;
	int nRes = db_create(&dbp, NULL, 0);
	if(nRes != 0)
		return NULL;

	nRes = dbp->open(dbp, NULL, psFileName, NULL, DB_HASH, DB_RDONLY, 0);
	if(nRes != 0)
		return NULL;

	return dbp;
}

DB *DBOpenWrite(char *psFileName)
{
	DB *dbp = NULL;
	int nRes = db_create(&dbp, NULL, 0);
	if(nRes != 0)
		return NULL;

	nRes = dbp->set_flags(dbp, DB_DUP | DB_DUPSORT);
	if(nRes != 0)
		return NULL;

	nRes = dbp->open(dbp, NULL, psFileName, NULL, DB_HASH, DB_CREATE, 0);
	if(nRes != 0)
		return NULL;

	return dbp;
}

void DBGetValue(char *psFile, int nKey)
{
	DB *dbp = DBOpenRead(psFile);

	if(NULL == dbp)
		return;

	DBT key, value;

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

	key.size = sizeof(int);
	key.data = &nKey;

	int nRes = dbp->get(dbp, NULL, &key, &value, 0);

	if(0 == nRes)
	{
		printf("get:size=%d,value=%d\r\n", value.size, *((int *)value.data));
	}

	dbp->close(dbp, 0);
}

void DBGetValueList(char *psFile, int nKey)
{
	DB *dbp = DBOpenRead(psFile);

	if(NULL == dbp)
		return;

	DBT key, value;

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

	key.size = sizeof(int);
	key.data = &nKey;

	DBC *cursor;
	int nRes = dbp->cursor(dbp, NULL, &cursor, 0);

	if(0 == nRes)
	{
		int ret = cursor->c_get(cursor, &key, &value, DB_SET);
		while (ret != DB_NOTFOUND)
		{
			printf("cursor get:size=%d,value=%d\r\n", value.size, *((int *)value.data));
			ret = cursor->c_get(cursor, &key, &value, DB_NEXT_DUP);
		}
		cursor->c_close(cursor);
	}
	dbp->close(dbp, 0);
}


void DBSet(char *psFile, int nKey, int nValue)
{
	DB *dbp = DBOpenWrite(psFile);
	if(NULL == dbp)
		return;

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

	key.size = sizeof(int);
	key.data = &nKey;

	value.size = sizeof(int);
	value.data = &nValue;

	int nRes = dbp->put(dbp, NULL, &key, &value, DB_NOOVERWRITE);

	printf("set:res=%d\r\n", nRes);

	dbp->close(dbp, 0);
}

void DBSetList(char *psFile, int nKey, int nValueFrom, int nValueTo)
{
	if(nValueTo < nValueFrom)
		return;

	DB *dbp = DBOpenWrite(psFile);
	if(NULL == dbp)
		return;

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

	key.size = sizeof(int);
	key.data = &nKey;

	DBC *cursor;
	int nRes = dbp->cursor(dbp, NULL, &cursor, 0);

	if(0 == nRes)
	{
		int i = 0;
		for( i = nValueFrom; i < nValueTo; i++)
		{
			DBT value;
			memset(&value, 0, sizeof(DBT));
			value.size = sizeof(int);
			value.data = &i;

			nRes = cursor->c_put(cursor, &key, &value, DB_KEYLAST);
			printf("cursor put:res=%d\r\n", nRes);
		}
	}
	dbp->close(dbp, 0);
}

void DBDump(char *psFile)
{
	DB *dbp = DBOpenRead(psFile);
	if( NULL == dbp)
		return;

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

	DBC *cursor;
	int nRes = dbp->cursor(dbp, NULL, &cursor, 0);

	if(0 == nRes)
	{
		while(cursor->c_get(cursor, &key, &value, DB_NEXT) == 0)
		{
			printf("dumper:key size=%d,value size=%d,key=%d,value=%d\r\n", key.size, value.size, *((int *)key.data), *((int *)value.data));
			memset(&key, 0, sizeof(DBT));
			memset(&value, 0, sizeof(DBT));
		}
		cursor->c_close(cursor);
	}
	dbp->close(dbp, 0);
}

int main(void)
{
	char *psFile = "test.db";

	int nKey = 1;
	int nValue = 100;
	int nValueFrom = 101;
	int nValueTo = 105;

	DBGetValue(psFile, nKey);

	DBSet(psFile, nKey, nValue);

	DBGetValue(psFile, nKey);

	DBSetList(psFile, nKey, nValueFrom, nValueTo);

	DBGetValueList(psFile, nKey);

	DBDump(psFile);

	return EXIT_SUCCESS;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值