Exemple #1
0
static int
edb_set(char *path, char *key, char *value) {
  unqlite *pDb;               /* Database handle */
  int rc;

  /* Open our database */
  while (1) {
    rc = unqlite_open(&pDb, path, UNQLITE_OPEN_CREATE);
    if( rc != UNQLITE_OK ){
      syslog(LOG_WARNING, "db_set: unqlite_open failed with rc: %d\n", rc);
      rc = -1;
      break;
    }
    rc = unqlite_kv_store(pDb, key, -1, value, MAX_BUF);
    if( rc != UNQLITE_OK ){
      if( rc != UNQLITE_BUSY ){
#ifdef DEBUG
        syslog(LOG_WARNING, "db_set: unqlite_kv_store failed with rc: %d\n", rc);
#endif
        rc = -1;
        break;
      } else {
        unqlite_close(pDb);
        continue;
      }
   } else {
   /* Auto-commit the transaction and close our database */
    rc = 0;
    break;
   }
 }
  unqlite_close(pDb);
  return rc;
}
Exemple #2
0
bool Storage::write(const void *key, size_t keySize, const void *value, size_t valueSize)
{
    if (!d->db) {
        return false;
    }

    int rc = unqlite_kv_store(d->db, key, keySize, value, valueSize);

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

    return !rc;
}
Exemple #3
0
bool KVUnqlite::put(const std::string& key, const std::string& value)
{
  if (key.size() > PERSISTENT_STORE_MAX_KEY_SIZE || value.size() > PERSISTENT_STORE_MAX_VALUE_SIZE)
  {
    OSS_LOG_ERROR("KVUnqlite::put:  Maximum data/ size exceeded");
    return false;
  }
  unqlite* pDbHandle = static_cast<unqlite*>(_pDbHandle);
  if (!pDbHandle)
    return false;


  if (unqlite_kv_store(pDbHandle, key.c_str(), key.size(), value.c_str(), value.size()) != UNQLITE_OK)
  {
    log_error();
    return false;
  }
    
  return true;
}
Exemple #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;
}
Exemple #5
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;
}