Vedis Users Forum

what's the defference between vedis_lib_shutdown() and vedis_close(pStore)

append delete sewain.chen@innosmar

Dear Sir,

what's the defference between vedis_lib_shutdown() and vedis_close(pStore)

Reply RSS

Replies

append delete #1. sewain.chen@innosmar

Sir, I have take a test on read record from database. Step is as follow:

step1: makesure database file abc.vds not exist.
step2: crate abc.vds, and write 2 records to database.
step3: read records number from database in a for loop. and also take a test: open any directory.

My Question:
in step3 read database records number in a for loop, when the index run to 1018, I get 0 records. Now, I try to open a directory with no problem.

But when the index goto 1019, I also get 0 record from database.
And now opendir failed with errno 24(Too many open files).

Very much hope to get help。 Thanks a lot!

===================== This is my C Code ==================

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>

#include "vedis.h"

#define BUFFER_SIZE 256
char buffer[BUFFER_SIZE] = { 0 };

// write 2 record to a database. key is "TimeStamps"
int write_database(const char *vdsFile)
{
int ret = 0;
int i, rc, timestamp;
vedis *pStore;
vedis_value *pResult;
int max_index;

printf("write_database. vdsFile = %s\n", vdsFile);

if (!vdsFile) {
printf("Error: vdsFile is null.");
return 0;
}

do {
// 打开数据库
rc = vedis_open(&pStore, vdsFile);
if( rc != VEDIS_OK ) {
printf("Error: can not open database file. \n");
break;
}

// write a record 1000 to key:TimeStamps
memset(buffer, 0, BUFFER_SIZE);
sprintf(buffer, "LPUSH %s %d", "TimeStamps", 1000);
rc = vedis_exec(pStore, buffer, -1);
if( rc != VEDIS_OK ) {
printf("Error: LPUSH command! \n");
break;
}

// write a record 1001 to key:TimeStamps
memset(buffer, 0, BUFFER_SIZE);
sprintf(buffer, "LPUSH %s %d", "TimeStamps", 1001);
rc = vedis_exec(pStore, buffer, -1);
if( rc != VEDIS_OK ) {
printf("Error: LPUSH command! \n");
break;
}

ret = 1;
} while (0);

// 关闭数据库
if (pStore) {
printf("close %s \n", vdsFile);

rc = vedis_close(pStore);
printf("vedis_close. rc = %d \n", rc);
rc = vedis_lib_shutdown();
printf("vedis_lib_shutdown. rc = %d \n", rc);
}

return ret;
}

// read record number from database
int read_database(const char *vdsFile)
{
printf("\t read_datebase. vdsFile = %s \n", vdsFile);

int ret = 0;
int i, rc, timestamp;
vedis *pStore;
vedis_value *pResult;
int max_index;

if (!vdsFile) {
printf("\t Error: vdsFile is null.");
return 0;
}

do {
// 打开数据库
rc = vedis_open(&pStore, vdsFile);
if( rc != VEDIS_OK ) {
printf("\t Error: can not open database file. \n");
break;
}

// 读取时间戳 List 长度
printf("\t read record number: ");
memset(buffer, 0, BUFFER_SIZE);
sprintf(buffer, "LLEN %s", "TimeStamps");
vedis_exec(pStore, buffer, -1);
if( rc != VEDIS_OK ) break;
rc = vedis_exec_result(pStore, &pResult);
if( rc != VEDIS_OK ) break;
int tsTotal = vedis_value_to_int64(pResult);
printf("\t tsTotal = %d \n", tsTotal);

ret = 1;
} while (0);

// 关闭数据库
if (pStore) {
printf("\t close %s \n", vdsFile);

rc = vedis_close(pStore);
printf("\t vedis_close. rc = %d \n", rc);
rc = vedis_lib_shutdown();
printf("\t vedis_lib_shutdown. rc = %d \n", rc);
}

return ret;
}

/*
step1: makesure database file abc.vds not exist.
step2: crate abc.vds, and write 2 records to database.
step3: read records number from database in a for loop. and also take a test: open any directory.

Question:
in step3 read database records number in for loop, when the index run to 1018, I get 0 records.
Now, I try to open a directory with no problem.

But when the index goto 1019, I also get 0 record from database.
And now opendir failed with errno 24(Too many open files)
*/
int main(int argc, char *argv[])
{
int rc;
int index = 0;
int total = 1022; // read times
const char *vdsFile = "abc.vds";

printf("remove vds file[%s] if it exists \n", vdsFile);
rc = remove(vdsFile);

printf("write record to vds file... \n");
rc = write_database(vdsFile);
if (0 == rc) {
printf("write_database failed!");
exit(-2);
}

printf("\n\nbegin read vds file record number... \n");
for (index = 0; index < total; ++index)
{
printf("\n\n===> index = %d \n", index);

read_database(vdsFile);

printf("take a test: open a directory[./test] \n");
DIR* dp = opendir("./test");
if (dp) {
printf("\topendir OK! \n");
closedir(dp);
} else {
printf("\topendir Failed!!!! errno = %d\n", errno);
}
}
}

append delete #2. sewain.chen@innosmar

On my ubuntu 14.04, I run this command:
$ ulimit -a
......
open files (-n) 1024

then, I change this limit to 100:
$ ulimit -n 100

And then run the C code, when the index run to 94, I can not get records number from database. and can not open any directory.

==============>
So, maybe I have not close the database file thoroughly。
But I have call the 2 functions: vedis_close() and vedis_lib_shutdown().

why?? How to close vedis database file completely?

append delete #3. chm

You seem running in an infinite loop with the **do{ }while(0);** statement.

Of course you'll run out of file descriptor pretty soon. you have to close the database before the while(0); with vedis_close();

append delete #4. sewain.chen@innosmar

======================
// read record number from database
int read_database(const char *vdsFile)
{
printf("\t read_datebase. vdsFile = %s \n", vdsFile);

int ret = 0;
int i, rc, timestamp;
vedis *pStore;
vedis_value *pResult;
int max_index;

if (!vdsFile) {
printf("\t Error: vdsFile is null.");
return 0;
}

do {
// open database
rc = vedis_open(&pStore, vdsFile);
if( rc != VEDIS_OK ) {
printf("\t Error: can not open database file. \n");
break; // jump out of while(0)
}

printf("\t read record number... \n");
memset(buffer, 0, BUFFER_SIZE);
sprintf(buffer, "LLEN %s", "TimeStamps");
vedis_exec(pStore, buffer, -1);
if( rc != VEDIS_OK ) break; // jump out of while(0)
rc = vedis_exec_result(pStore, &pResult);
if( rc != VEDIS_OK ) break; // jump out of while(0)
int tsTotal = vedis_value_to_int64(pResult);
printf("\t tsTotal = %d \n", tsTotal);

ret = 1; // jump out of while(0)
} while (0);

// close database
if (pStore) {
printf("\t close %s \n", vdsFile);

rc = vedis_close(pStore);
printf("\t vedis_close. rc = %d \n", rc);
rc = vedis_lib_shutdown();
printf("\t vedis_lib_shutdown. rc = %d \n", rc);
}

return ret;
}

========================

the do...while(0) statement is just run once(used for "break" to stop), it's not infinite loop.
And I'm sure the vedis_close() is called. the print log is:

===> index = 109
read_datebase. vdsFile = abc.vds
read record number...
tsTotal = 0
close abc.vds
vedis_close. rc = 0
vedis_lib_shutdown. rc = 0
take a test: open a directory[./test]
opendir Failed!!!! errno = 24

append delete #5. chm

can you send the full C code to devel@symisc.net for further analysis.

append delete #6. sewain.chen@innosmar

Thanks very much.
I have sent a mail to devel@symisc.net with attachment just now.

Reply

(Leave this as-is, it’s a trap!)

There is no need to “register”, just enter the same name + password of your choice every time.

Pro tip: Use markup to add links, quotes and more.