Example #1
0
bool Storage::commitTransaction()
{
    if (!d->db) {
        return false;
    }

    if (!d->inTransaction) {
        return true;
    }

    int rc = unqlite_commit(d->db);
    d->inTransaction = false;

    if (rc != UNQLITE_OK) {
        d->reportDbError("unqlite_commit");
    }

    return rc == UNQLITE_OK;
}
Example #2
0
int main(int argc,char *argv[])
{
	int db_extract = 0;          /* TRUE to extract records from the dabase */
	int db_store = 0;            /* TRUE to store files in the database */
	int db_iterate = 0;          /* TRUE to iterate over the inserted elements */
	unqlite *pDb;                /* Database handle */
	int c,i,rc;

	if( argc < 3 ){
		/* Missing database name */
		Help();
	}
	
	c = argv[2][0];
	if( c != '-' ){
		/* Missing command */
		Help();
	}
	c = argv[2][1];
	if( c == 'i' || c == 'I' ){
		/* Iterate over the inserted elements */
		db_iterate = 1;
	}else if( c == 'w' || c == 'W' ){
		/* Store some files */
		db_store = 1;
	}else{
		/* Extract some records */
		db_extract = 1;
	}
	

	/* Open our database */
	rc = unqlite_open(&pDb,argv[1],db_store ? UNQLITE_OPEN_CREATE : (UNQLITE_OPEN_READONLY|UNQLITE_OPEN_MMAP) /* Read-only DB */ );
	if( rc != UNQLITE_OK ){
		Fatal(0,"Out of memory");
	}
	
	if( db_store ){
		void *pMap;          /* Read-only memory view of the target file */
		unqlite_int64 nSize; /* file size */
		
		/* Start the insertion */
		for( i = 3 ; i < argc ; ++i ){
			const char *zFile = argv[i];
			printf("Inserting %s\t ... ",zFile);

			/* Obtain a read-only memory view of the whole file */
			rc = unqlite_util_load_mmaped_file(zFile,&pMap,&nSize);
			if( rc == UNQLITE_OK ){
				/* Store the whole file */
				rc = unqlite_kv_store(pDb,zFile,-1,pMap,nSize);
				/* Discard this view */
				unqlite_util_release_mmaped_file(pMap,nSize);
			}
			puts(rc == UNQLITE_OK ? "OK" : "Fail");
		}
		/* Mnually commit the transaction. 
		 * In fact, a call to unqlite_commit() is not necessary since UnQLite
		 * will automatically commit the transaction during a call to unqlite_close().
		 */
		rc = unqlite_commit(pDb);
		if( rc != UNQLITE_OK ){
			/* Rollback the transaction */
			rc = unqlite_rollback(pDb);
		}
		if( rc != UNQLITE_OK ){
			/* Something goes wrong, extract the database error log and exit */
			Fatal(pDb,0);
		}
	}else if( db_iterate ){
		/* Iterate over the inserted records */
		unqlite_kv_cursor *pCur;
		
		/* Allocate a new cursor instance */
		rc = unqlite_kv_cursor_init(pDb,&pCur);
		if( rc != UNQLITE_OK ){
			Fatal(0,"Out of memory");
		}

		/* Point to the first record */
		unqlite_kv_cursor_first_entry(pCur);
		
		/* Iterate over the entries */
		while( unqlite_kv_cursor_valid_entry(pCur) ){
			unqlite_int64 nDataLen; 
			
			/* Consume the key */
			unqlite_kv_cursor_key_callback(pCur,DataConsumerCallback,0);
			
			/* Extract the data size */
			unqlite_kv_cursor_data(pCur,0,&nDataLen);
			printf(":\t %ld Bytes\n",nDataLen);
			/* unqlite_kv_cursor_data_callback(pCur,DataConsumerCallback,0); */

			/* Point to the next entry */
			unqlite_kv_cursor_next_entry(pCur);
		}
		/* Finally, Release our cursor */
		unqlite_kv_cursor_release(pDb,pCur);
	}else{
		/* Extract one more records */
		for( i = 3 ; i < argc ; ++i ){
			const char *zFile = argv[i];
			rc = unqlite_kv_fetch_callback(pDb,zFile,-1,DataConsumerCallback,0);
			if( rc == UNQLITE_NOTFOUND ){
				printf("No such record: %s\n",zFile);
			}
		}
	}

	/* All done, close our database */
	unqlite_close(pDb);
	return 0;
}