示例#1
0
void fetchCursorData(unqlite_kv_cursor *cursor,
                     void **keyBuffer, int *keyBufferLength, void **dataBuffer, unqlite_int64 *dataBufferLength,
                     const std::function<bool(void *keyPtr, int keySize, void *valuePtr, int valueSize)> &resultHandler)
{
    int keyLength = 0;
    unqlite_int64 dataLength = 0;
    // now fetch the data sizes
    if (unqlite_kv_cursor_key(cursor, nullptr, &keyLength) == UNQLITE_OK &&
        unqlite_kv_cursor_data(cursor, nullptr, &dataLength) == UNQLITE_OK) {
        if (keyLength > *keyBufferLength) {
            *keyBuffer = realloc(*keyBuffer, keyLength);
            *keyBufferLength = keyLength;
        }

        if (dataLength > *dataBufferLength) {
            *dataBuffer = realloc(*dataBuffer, dataLength);
            *dataBufferLength = dataLength;
        }

        if (unqlite_kv_cursor_key(cursor, *keyBuffer, &keyLength) == UNQLITE_OK &&
            unqlite_kv_cursor_data(cursor, *dataBuffer, &dataLength) == UNQLITE_OK) {
            resultHandler(*keyBuffer, keyLength, *dataBuffer, dataLength);
        }
    }
}
示例#2
0
sxi64
UnqliteCursor::get_data_len()
{
	sxi64 nBytes = 0;
	unqlite_kv_cursor_data(this->_cursor, 0, &nBytes);

	return nBytes;
}
示例#3
0
ValueBuffer*
UnqliteCursor::get_data(
	bool as_binary,
	sxi64 value_len,
	pyunqlite::UserCallback* callback,
	pyunqlite::ValueBuffer* direct_buffer
)
{
	ValueBuffer* value = 0;

	// setup the buffer for retrieving data
	if (direct_buffer) {
		if (value_len < 0)
			value_len = direct_buffer->get_data_len();
		else if (direct_buffer->get_data_len() < value_len)
			throw UnqliteException(UNQLITE_INVALID);

		value = new ValueBuffer(*direct_buffer);
	}
	else if (!callback) {
		// determine the size of the stored data if it is unknown
		if (value_len < 0)
			value_len = get_data_len();

		// create a new buffer
		value = new ValueBuffer(as_binary, value_len);
		if (!value)
			throw UnqliteException(UNQLITE_NOMEM);
	}

	int rc;
	if (callback) {
		rc = unqlite_kv_cursor_data_callback(
			this->_cursor,
			callback->get_unqlite_callback_function(),
			callback->get_unqlite_callback_data()
		);
	}
	else {
		rc = unqlite_kv_cursor_data(this->_cursor, value->get_data(), &value_len);
	}

	if (callback && (rc == UNQLITE_ABORT))
		callback->process_exception();
	else if (rc != UNQLITE_OK)
		throw UnqliteException(rc, this->_db);

	return value;
}
示例#4
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;
}