BDB read, write, delete, update things, examples

xiaoxiao2021-03-30  229

} Bool Modify (DB * DBP, INT TXN, CHAR * UID, Char * Folder, Char * Name, Char * Database {DBC * DBCP; DBT Key, Data; DB_TXN * TXNP; Int Ret; Memset (& Key, 0, Sizeof (key); Memset (& Data); key.data = uid; key.size = strlen (uid); txnp = null; dbp-> dbenv-> txn_begin (dbp-> dbenv, NULL, & TXNP, 0); / * acquire a cursor for the database. * / If ((RET = DBP-> Cursor (DBP, TXNP, & DBCP, 0))! = 0) {dbp-> Err (DBP, RET , "DB-> Cursor");} Bool Blnexist = false; while ((Ret = DBCP-> C_GET (DBCP, & Key, & Data, DB_NEXT) == 0) {INT C1, C2, C3; Char * P = (char *) data.data; char * p1 = p; char TMPFolder [255] = {0}; char TMPFILENAME [255] = {0}; Memcpy (& C1, P, SIZEOF (int)); p = Sizeof (int); MEMSET (TMPFolder, 0, 255); Memcpy (TmpFolder, P, C1); P = C1; Memcpy (& C2, P, SIZEOF (int)); p = sizeof (int); MEMSET (TMPFileName , 0, 255); Memcpy (TmpFileName, P, C2); IF (StrCMP (Folder, TmpFolder) == 0 && strcmp (Name, TMPFileName) == 0) {char * p2 = & gbuf [0]; memcpy (p2 , P1, C1 C2 SIZEOF (INT) * 2); P2 = C1 C2 SIZEOF (INT) * 2; in T len = strlen ((char *) DataBuf); Memcpy (p2, & len, sizeof (len)); p2 = sizeof (len); Memcpy (P2, DataBuf, len); data.data = GBUF; data.size = 2 * 1024 * 1024; DBCP-> C_PUT (DBCP, & key, & data, db_current); blnexist = true; break;}} DBCP-> c_close (dbcp); txnp-> commit (txnp, 0); if (! BLNEXIST && RET == DB_NOTFOUND) {Return False;}}}} BOOL APPEND (DB * DBP, INT TXN, CHAR * UID, Char * Folder, Char * Name, Char * Database {DBC * DBCP; DBT Key, Data; DB_TXN * TXNP; int R; MEMSET (&)

Key, 0, Sizeof (Key); Memset (& Data); key.data = uid; key.size = strlen (UID); txnp = null; dbp-> dbenv-> TXN_BEGIN (DBP -> DBENV, NULL, & TXNP, 0); / * Acquire a cursor for the database. * / if ((RET = DBP-> Cursor (DBP, TXNP, & DBCP, 0))! = 0) {dBp-> ERR (DBP, RET, "DB-> Cursor");} bool blnexist = false; while ((RET = DBCP-> C_GET (DBCP, & Key, & Data, DB_Next)) == 0) {INT C1, C2, C3; Char * p = (char *) Data.Data; char * p1 = p; char TMPFolder [255] = {0}; char TMPFileName [255] = {0}; Memcpy (& C1, P, SIZEOF (int)); P = SizeOf (int); MEMSET (TmpFolder, 0, 255); Memcpy (TmpFolder, P, C1); P = C1; Memcpy (& C2, P, SIZEOF (int)); p = Sizeof (int) MEMSET (TMPFileName, 0, 255); Memcpy (TmpFileName, P, C2); P = C2; Memcpy (& C3, P, SIZEOF (int)); p = sizeof (int); if (Strcmp (Folder, TmpFolder) == 0 && strCMP (Name, TMPFileName) == 0) {char * p2 = & gbuf [0]; Memcpy (P2, P1, C1 C2 SIZEOF (int) * 2); p2 = C1 C2 sizeof (int) * 2; int Len = C3 Strlen ((char *) Database; Memcpy (p2, & len, sizeof (len)); P2 = Sizeof (LEN) Memcpy (P2, P, C3); P2 = C3; Memcpy (P2, DATABUF, LEN); DATA.DATA = GBUF; DATA.SIZE = 2 * 1024 * 1024; DBCP-> C_PUT (DBCP, & Key, & Data , Db_current); blnexist = true; break;}} dbcp-> c_close (dbcp); txnp-> commit (txnp, 0); if (! BLNEXIST && RET == DB_NOTFOUND) {Return false;} else {Return True; }}} Bool Del (DB * DBP, INT TXN, CHAR * UID, Char * Folder, Char * Name) {DBC * DBCP; DBT Key, Data; DB_TXN * TXNP; Int Ret; Memset (& Key, 0, SIZEOF ); MEMSET (&)

Data, 0, sizeof (data); key.data = uid; key.size = strlen; txnp = null; dbp-> dbenv-> TXN_BEGIN (DBP-> DBENV, NULL, & TXNP, 0); / * Acquire a cursor for the database. * / If ((RET = DBP-> CURSOR (DBP, TXNP, & DBCP, 0))! = 0) {dBp-> ERR (DBP, RET, "DB-> Cursor") } Bool Blnexist = false; while ((Ret = DBCP-> C_GET (DBCP, & Key, & Data, DB_Next) == 0) {INT C; char * p = (char *) Data.Data; char TMPFolder [255 ] = {0}; Char TmpFileName [255] = {0}; Memcpy (& C, P, SizeOf (int)); p = sizeof (int); MEMSET (TmpFolder, 0, 255); Memcpy (Tmpfolder, P , c); p = C; Memcpy (& C, p, sizeof (int)); p = sizeof (int); MEMSET (TMPFileName, 0, 20); Memcpy (TMPFileName, P, C); if (strcmp) (Folder, TmpFolder) == 0 && strcmp (name, tmpfilename) == 0) {dbcp-> c_del (dbcp, 0); blnexist = true; Break;}} DBCP-> C_Close (dbcp); Txnp-> Commit (txnp, 0); if (! BLNEXIST && RET == DB_NOTFOST) {Return False;} else {return}}} BOOL MKFILE (DB * DBP, INT TXN, CHAR * UID, Char * Folder, Char * Name) {DBC * DBCP; DBT Key, Data; DB_TXN * TXNP; Int Re T; MEMSET (& key, 0, sizeof (key)); MEMSET (& Data); key.data = uid; key.size = strlen (uid); txnp = null; dbp-> dbenv- > TXN_BEGIN (DBP-> DBENV, NULL, & TXNP, 0); / * acquire a cursor for the database. * / if ((RET = DBP-> Cursor (DBP, TXNP, & DBCP, 0))! = 0) { DBP-> Err (DBP, RET, "DB-> Cursor");} Bool Blnexist = false; while ((Ret = DBCP-> C_GET (DBCP, & Key, & Data, DB_NEXT) == 0) {INT C; Char * p = (char *) Data.Data; char TMPFolder [255] = {0}; char TMPFILENAME [255] =

{0}; Memcpy (& C, P, SIZEOF (int)); p = sizeof (int); MEMSET (TmpFolder, 0, 255); Memcpy (TmpFolder, P, C); P = C; Memcpy (& C) , p, sizeof (int)); MEMSET (TMPFileName, 0, 20); Memcpy (TmpFileName, P, C); IF (Strcmp (Folder, TmpFolder) == 0 && strcmp (Name , TMPFILENAME) == 0) {P = C; Memcpy (& C, P, SIZEOF (int)); p = sizeof (int); MEMSET (GBUF, 0, 2 * 1024 * 1024); Memcpy (GBUF, p, c); file * pfile = fopen (TMPFileName, "W"); if (pfile) {FWRITE (GBUF, SIZEOF (CHAR), C, PFILE; fclose (pfile);} BLNEXIST = True; Break; } DBCP-> C_Close (DBCP); TXNP-> Commit (TXNP, 0); if (! BLNEXIST && RET == DB_NOTFOTFOUND) {Return False;}}} BOOL JUDGE (DB * DBP, INT TXN , Char * UID, Char * Folder, Char * Name) {DBC * DBCP; DBT Key, Data; DB_TXN * TXNP; Int Ret; Memset (& Key, 0, Sizeof (KEY)); MEMSET (& Data, 0, Sizeof) Data); key.data = uid; key.size = strlen (uid); txnp = null; dbp-> dbenv-> TXN_BEGIN (DBP-> DBENV, NULL, & TXNP, 0); / * Acquire a CURSOR for THE Database. * / if ((Ret = DBP-> Cursor (DBP, TXN p, & dbcp, 0))! = 0) {dbp-> Err (DBP, RET, "DB-> Cursor");} Bool Blnexist = false; while ((Ret = DBCP-> C_Get (DBCP, & Key, & Data , Db_next) == 0) {INT C; char * p = (char *) data.data; char TMPFolder [255] = {0}; char TMPFileName [255] = {0}; Memcpy (& C, P, SIZEOF (int)); p = sizeof (int); MEMSET (TmpFolder, 0, 255); Memcpy (TmpFolder, P, C); P = C; Memcpy (& C, P, SizeOf (int)); P = sizeof (int); MemSet (TMPFileName, 0, 20); Memcpy (TMPFileName, P, C); IF (StrCMP (Folder, TmpFolder) == 0 &&

Strcmp (name, tmpfilename) == 0) {blnexist = true; break;}} dbcp-> c_close (dbcp); txnp-> commit (txnp, 0); if (! BLNEXIST && RET == DB_NOTFOTFOUND) {Return False } else {return true;}} / * * Get - loop getting batches of records. * * / int GET (DB * DBP, INT TXN, INT DATALEN, INT NUM, INT DUPS, ITITITER, INT * CountP) {DBC * DBCP; DBT Key, Data; DB_TXN * TXNP; U_INT32_T LEN, KLEN; INT Count, Flags, I, J, Ret; Void * Pointer, * DP, * KP; Memset (& Key, 0, Sizeof (Key) ); MEMSET (& DATA, 0, SIZEOF (DATA); key.data = "datianxia"; key.size = strlen; txnp = null; dbp-> dbenv-> txn_begin (dbp-> dbenv, NULL, & TXNP, 0); / * acquire a cursor for the database. * / If ((RET = DBP-> Cursor (DBP, TXNP, & DBCP, 0))! = 0) {dbp-> Err (DBP, RET , "Db-> cursor");} while ((RET = DBCP-> C_GET (DBCP, & Key, & Data, DB_Next) == 0) {INT C; char * p = (char *) data.data; char TMP [20] = {0}; Memcpy (& C, P, SIZEOF (int)); p = sizeof (int); MEMSET (TMP, 0, 20); Memcpy (TMP, P, C); Printf (" % S / N ", TMP); P = C; Memcpy (& C, P, SIZEOF (Int ))); p = sizeof (int); MEMSET (TMP, 0, 20); Memcpy (TMP, P, C); Printf ("% S / N", TMP);} if (RET == DB_NOTFOUND) { Printf ("DFDS / N");} DBCP-> C_Close (DBCP); if (TXN) TXNP-> Commit (TXNP, 0); * countp = count; return (0);} / * * Fill - Fill A db * Since we open / created the db with transactions (potentially), * we need to populate it with transactions. We'll bundle the puts * 10 to a transaction. * / # definePUTS_PER_TXN1 int fill (DB_ENV * dbenv, DB * dbp , int TXN, INT DATALEN, INT NUM, INT DUPS) {DBT KEY, DATA; DB_TXN * TXNP;

INT Count, I, Ret; / * Insert RECORDS INTO The Database, WHERE THE IS The User * Input and the data is the user input in reverse order. * / txnp = NULL; RET = 0; count = 0; DATA .flags = db_dbt_usermem; Memset (& key, 0, sizeof (dbt)); MEMSET (& DATA, 0, SIZEOF (DBT)); struct _finddata_t c_file; char szbuf [255] = {0}; char SZDIR [255] = { 0}; struffpy (szbuf, desfolder); strcpy (szdir, desfolder); strcat (szdir, "////////*.*"); long hfile = _findfirst (szbuf, & c_file); IF (HFILE == -1L) Return False; while (_findnext (hfindnext (hfile, & c_file)! = -1L) {if (strcmp (c_file.name, "..")! = 0) {if (c_file.attrib&& _a_subdir ) {// do nothing} else {char * pkey = "datianxia"; key.data = pkey; key.size = strlen (pkey); char szfile [255] = {0}; sprintf (szfile, "% s% S ", szdir, c_file.name); int foldir; int name = sizeof (c_file.name); char * pfolder; pfolder = & gbuf [0]; int LEN = strlen (szdir); Memcpy (GBUF , & len, SizeOf (LEN); PFOLDER = SizeOf (LEN); Memcpy (Pfolder, Szdir, Len); PFolder = LEN; LEN = Strlen (c_file.name); Memcpy (Pfolder, & Len, Sizeof (LEN)); PFolder = SizeOf (LEN); Memcpy (pfolder, c_file.name, len); PFolder = Len; File * pfile = fopen; if (! pfile) return false; fseek (pfile, 0, seek_ek; int filesize = ftell (pfile); ReWind (pfile); Memcpy (Pfolder, & FileSize, Sizeof LEN); PFOLDER = SizeOf (Freesize); Fread (Pfolder, Sizeof (Char), FileSize, Pfile; Data.Data = GBUF; DATA.SIZE = 2 * 1024 * 1024; IF (i % PUTS_PER_TXN == 0) {if (TxNP! = NULL) {RET =

Txnp-> Commit (TXNP, 0); TXNP = NULL; if (RET! = 0) goto err;} f ((Ret = DBENV-> TXN_BEGIN (DBENV, NULL, & TXNP, 0))! = 0) goto err } i ; if (Judge (DBP, TXN, Datianxia, ​​SZDIR, C_FILE.NAME)) Continue; Do {Switch (Ret = DBP-> PUT (DBP, TXNP, & Key, & Data, 0) {Case 0 : count ; Break; default: dbp-> Err (DBP, RET, "DB-> PUT"); goto err;}} while (RET! = 0);}}} // end of find file _findclose (hfile) ; if (txnp! = null) RET = TXNP-> Commit (TXNP, 0); Printf ("% d / n", count); return (RET); err: if (txnp! = null) (Void) TXNP -> Abort (TXNP); Return (RET);} int main (int Argc, char * argv []) {db * dbp; db_env * dbenv; db_txn * TxNP; int Cache, Ch, Count, Datalen, DUPS, ENV , IT RET, RFLAG, TXN; TXNP = NULL; DATALEN = 20; iter = NUM ​​= 10000; ENV = 1; DUPS = INIT = RFLAG = TXN = 0; Pagesize = 65536; cache = 1000 * Pagesize; Env_dir_create (); DBENV = NULL; IF ((DBENV = DB_INIT ("Bench_001", "Bench_001", Cache, TXN) == NULL) RETURN (-1); if (init) exit (0) ; / * CREATE AND INITIALIZE DA Tabase Object, Open the Database. * / IF ((Ret = DB_CREATE (& DBP, DBENV, 0)))! = 0) {FPRINTF (stderr, "% s: db_create:% s / n", progName, db_strerror (RET) ); exit_failure;} dBp-> set_errfile (dbp, stderr); dbp-> set_errpfx (dbp, progName); if ((RET = DBP-> set_pagesize (dbp, pagesize))! = 0) {dBp- > Err (DBP, RET, "SET_PAGESIZE"); goto err1;} if ((RET = DBP-> set_flags (dbp, db_dup))! = 0) {dBp-> ERR (DBP, RET, "SET_FLAGS"); Goto Err1;} IF ((Ret = DBENV-> TXN_BEGIN (DBENV, NULL, &

转载请注明原文地址:https://www.9cbs.com/read-130726.html

New Post(0)