Beispiel #1
0
ValueBuffer*
UnqliteCursor::get_key(int key_len, pyunqlite::UserCallback* callback)
{
	ValueBuffer* value = 0;
	int rc;
	if (callback)
	{
		rc = unqlite_kv_cursor_key_callback(
			this->_cursor,
			callback->get_unqlite_callback_function(),
			callback->get_unqlite_callback_data()
		);
	}
	else
	{
		int nBytes = 0;
		unqlite_kv_cursor_key(this->_cursor, 0, &nBytes);

		// create the buffer
		value = new ValueBuffer(false, nBytes);
		if (!value)
			throw UnqliteException(UNQLITE_NOMEM);

		rc = unqlite_kv_cursor_key(this->_cursor, value->get_data(), &nBytes);
	}

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

	return value;
}
Beispiel #2
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);
        }
    }
}
Beispiel #3
0
int main(int argc,char *argv[])
{
	unqlite *pDb;               /* Database handle */
	unqlite_kv_cursor *pCur;    /* Cursor handle */
	char zKey[14];              /* Random generated key */
	char zData[32];             /* Dummy data */
	int i,rc;

	puts(zBanner);

	/* Open our database */
	rc = unqlite_open(&pDb,argc > 1 ? argv[1] /* On-disk DB */ : ":mem:" /* In-mem DB */,UNQLITE_OPEN_CREATE);
	if( rc != UNQLITE_OK ){
		Fatal(0,"Out of memory");
	}
	
	printf("Starting insertions of %d random records...\n",MAX_RECORDS);
	
	/* Start the random insertions */
	for( i = 0 ; i < MAX_RECORDS; ++i ){
		
		/* Genearte the random key first */
		unqlite_util_random_string(pDb,zKey,sizeof(zKey));

		/* Perform the insertion */
		rc = unqlite_kv_store(pDb,zKey,sizeof(zKey),zData,sizeof(zData));
		if( rc != UNQLITE_OK ){
			/* Something goes wrong */
			break;
		}
	}
	if( rc != UNQLITE_OK ){
		/* Something goes wrong, extract the database error log and exit */
		Fatal(pDb,0);
	}
	puts("Done...Starting the iteration process");

	/* 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);
	/* To point to the last record instead of the first, simply call [unqlite_kv_cursor_last_entry()] as follows */
	
	/* unqlite_kv_cursor_last_entry(pCur); */
		
	/* Iterate over the entries */
	while( unqlite_kv_cursor_valid_entry(pCur) ){
		int nKeyLen;
		/* unqlite_int64 nDataLen; */
		
		/* Consume the key */
		unqlite_kv_cursor_key(pCur,0,&nKeyLen); /* Extract key length */
		printf("\nKey ==> %u\n\t",nKeyLen);
		unqlite_kv_cursor_key_callback(pCur,DataConsumerCallback,0);
			
		/* Consume the data */
		/*
		unqlite_kv_cursor_data(pCur,0,&nDataLen);
		printf("\nData ==> %lld\n\t",nDataLen);
		unqlite_kv_cursor_data_callback(pCur,DataConsumerCallback,0);
		*/


		/* Point to the next entry */
		unqlite_kv_cursor_next_entry(pCur);

		/*unqlite_kv_cursor_prev_entry(pCur); //If [unqlite_kv_cursor_last_entry(pCur)] instead of [unqlite_kv_cursor_first_entry(pCur)] */
	}
	/* Finally, Release our cursor */
	unqlite_kv_cursor_release(pDb,pCur);
	
	/* Auto-commit the transaction and close our database */
	unqlite_close(pDb);
	return 0;
}