Ejemplo n.º 1
0
bool DbstlMultipleRecnoDataIterator::next(db_recno_t &recno, Dbt &data)
{
	if (*p_ == (u_int32_t)0) {
		recno = 0;
		data.set_data(0);
		data.set_size(0);
		p_ = 0;
	} else {
		recno = *p_--;
		data.set_data(data_ + *p_--);
		data.set_size(*p_--);
	}
	return (p_ != 0);
}
Ejemplo n.º 2
0
bool DbstlMultipleDataIterator::next(Dbt &data)
{
	if (*p_ == (u_int32_t)-1) {
		data.set_data(0);
		data.set_size(0);
		p_ = 0;
	} else {
		data.set_data(data_ + *p_--);
		data.set_size(*p_--);
		if (data.get_size() == 0 && data.get_data() == data_)
			data.set_data(0);
	}
	return (p_ != 0);
}
Ejemplo n.º 3
0
Bdb::ResponseCode Bdb::
remove(const std::string& key)
{
    if (!inited_) {
        return Error;
    }
    Dbt dbkey;
    dbkey.set_data(const_cast<char*>(key.c_str()));
    dbkey.set_size(key.size());

    int rc = 0;
    for (uint32_t idx = 0; idx < numRetries_; idx++) {
        rc = db_->del(NULL, &dbkey, 0);
        if (rc == 0) {
            return Success;
        } else if (rc == DB_NOTFOUND) {
            return KeyNotFound;
        } else if (rc != DB_LOCK_DEADLOCK) {
            fprintf(stderr, "Db::del() returned: %s", db_strerror(rc));
            return Error;
        }
    }
    fprintf(stderr, "update failed %d times", numRetries_);
    return Error;
}
Ejemplo n.º 4
0
void berkeleydb_store<Object>::get(const timestamp& time,
                                   const long lp_id,
                                   obj_ptr& ret) {
  boost::lock_guard<boost::mutex> guard(get_mutex_);
  /* generate key */
  std::vector<char> key;
  key_lpid_to_char(time, lp_id, key);
  Dbt db_key(&key[0], key.size());

  /* get data */
  char* event_binary;
  Dbt data;
  data.set_data(event_binary);
  data.set_flags(DB_DBT_MALLOC);

  db->get(NULL, &db_key, &data, 0);

  /* deserialize the data */
  std::string binary = std::string((char*) data.get_data(), data.get_size());
  std::stringstream from_ss;
  from_ss << binary;
  boost::archive::binary_iarchive iar(from_ss);
  Object obj;
  iar >> obj;

  ret = boost::make_shared<Object>(obj);
};
Ejemplo n.º 5
0
bool DbMultipleKeyDataIterator::next(Dbt &key, Dbt &data)
{
	if (*p_ == (u_int32_t)-1) {
		key.set_data(0);
		key.set_size(0);
		data.set_data(0);
		data.set_size(0);
		p_ = 0;
	} else {
		key.set_data(data_ + *p_--);
		key.set_size(*p_--);
		data.set_data(data_ + *p_--);
		data.set_size(*p_--);
	}
	return (data.get_data() != 0);
}
Ejemplo n.º 6
0
void LoginDB::change(string user, string pass)
{
    openDb();

    Dbc *cursorp;
    Dbt key(const_cast<char*> (user.data()), user.size());
    Dbt data;

    // Get a cursor
    db->cursor(NULL, &cursorp, 0);

    // Position the cursor
    int ret = cursorp->get(&key, &data, DB_SET);
    if (ret == 0)
        {
            data.set_data(const_cast<char*>(pass.data()));
            data.set_size(pass.size());
            cursorp->put(&key, &data, DB_CURRENT);
        }
    if (cursorp != NULL)
        cursorp->close();

    closeDb();

}
Ejemplo n.º 7
0
RecordData Database::get(const RecordID& id) const
{
	Dbt key(id.data(), id.size());
	Dbt data;
	thread_local static std::vector<char> buffer(256);
		
	while (true)
	{
		try
		{
			data.set_flags(DB_DBT_USERMEM);
			data.set_data(buffer.data());
			data.set_ulen(buffer.size());
			const int err = dbMain_.get(/*txnid*/nullptr, &key, &data, /*flags*/0);
			assert (err == 0);
			break;
		}
		catch(DbException const& ex)
		{
			if (ex.get_errno() !=  DB_BUFFER_SMALL)
			{
				throw;
			}

			buffer.resize(data.get_size() * 1.5);
		}
	}
	
	return RecordData(data);
}
Ejemplo n.º 8
0
db_iterator Database::dirs_begin() const
{
    Dbc* pCursor = nullptr;
	dbDirs_.cursor(NULL, &pCursor, 0);
	assert(pCursor);
	
    RecordID id;
    Dbt key;
    Dbt data;
    
    key.set_flags(DB_DBT_USERMEM);
    key.set_data(id.data());
    key.set_ulen(id.size());
    
    const int err = pCursor->get(&key, &data, DB_FIRST);
    
    if (err)
    {
        pCursor->close();
        
        if (err != DB_NOTFOUND)
        {
            throw DbException("Failed to obtain first directory record", err);
        }
        
        return dirs_end();
    }
    
    return db_iterator(pCursor, make_Record(std::move(id), RecordData(data)));
}
Ejemplo n.º 9
0
HeaderGroup* BulkHeaderGroup::getGroup(NewsGroup* ng, QString& articleIndex)
{
    HeaderGroup *hg = 0;

    int ret;
    Dbt groupkey;
    Dbt groupdata;
    memset(&groupkey, 0, sizeof(groupkey));
    memset(&groupdata, 0, sizeof(groupdata));
    groupdata.set_flags(DB_DBT_MALLOC);

    QByteArray ba = articleIndex.toLocal8Bit();
    const char *k= ba.constData();
    groupkey.set_data((void*)k);
    groupkey.set_size(articleIndex.length());

    Db* groupsDb = ng->getGroupingDb();
    ret=groupsDb->get(NULL, &groupkey, &groupdata, 0);
    if (ret != 0) //key not found
    {
        qDebug() << "Failed to find group with key " << articleIndex;
    }
    else
    {
        qDebug() << "Found group with key " << articleIndex;

        hg=new HeaderGroup(articleIndex.length(), (char*)k, (char*)groupdata.get_data());
        void* ptr = groupdata.get_data();
        Q_FREE(ptr);
    }

    return hg;
}
Ejemplo n.º 10
0
AntiCacheBlock AntiCacheDB::readBlockBerkeleyDB(std::string tableName, int16_t blockId) {
    
    Dbt key;
    key.set_data(&blockId);
    key.set_size(sizeof(int16_t));

    Dbt value;
    value.set_flags(DB_DBT_MALLOC);
    
    VOLT_DEBUG("Reading evicted block with id %d", blockId);
    
    int ret_value = m_db->get(NULL, &key, &value, 0);
    if (ret_value != 0) 
    {
        VOLT_ERROR("Invalid anti-cache blockId '%d' for table '%s'", blockId, tableName.c_str());
        throw UnknownBlockAccessException(tableName, blockId);
    }
    else 
    {
//        m_db->del(NULL, &key, 0);  // if we have this the benchmark won't end
        assert(value.get_data() != NULL);
    }
    
    AntiCacheBlock block(blockId, static_cast<char*>(value.get_data()), value.get_size());
    return (block);
}
Ejemplo n.º 11
0
void JabberBotSession::Status(const std::string *from, MessageStanza::MessageTypes type, const std::string *id) {
  std::string account_id, resource;
  split_identifier(from, account_id, resource);
  char keyValue[account_id.size()+1];
  account_id.copy(keyValue, std::string::npos);
  keyValue[account_id.size()] = '\0';
  time_t last = 0;

  Dbt key(keyValue, account_id.size()+1);
  Dbt data;
  data.set_data(&last);
  data.set_ulen(sizeof(time_t));
  int result = m_subscriptionDb.get(NULL, &key, &data, 0);

  MessageStanza message;
  message.Type(type);
  message.To(from);
  message.Thread(id);
  if (0 != result) {
    message.Body("You are not subscribed");
  }
  else {
    message.Body("You are subscribed");
  }
  m_session->SendMessage(message, false);
}
Ejemplo n.º 12
0
/**
 * Cursor must be closed before the transaction is aborted/commited.
 * http://download.oracle.com/docs/cd/E17076_02/html/programmer_reference/transapp_cursor.html
 */
Bdb::ResponseCode Bdb::
update(const std::string& key, const std::string& value)
{
    if (!inited_) {
        fprintf(stderr, "insert called on uninitialized database");
        return Error;
    }
    DbTxn* txn = NULL;
    Dbc* cursor = NULL;

    Dbt dbkey, dbdata;
    dbkey.set_data(const_cast<char*>(key.c_str()));
    dbkey.set_size(key.size());
    dbdata.set_data(const_cast<char*>(value.c_str()));
    dbdata.set_size(value.size());

    Dbt currentData;
    currentData.set_data(NULL);
    currentData.set_ulen(0);
    currentData.set_dlen(0);
    currentData.set_doff(0);
    currentData.set_flags(DB_DBT_USERMEM | DB_DBT_PARTIAL);

    int rc = 0;
    for (uint32_t idx = 0; idx < numRetries_; idx++) {
        env_->txn_begin(NULL, &txn, 0);
        (*db_).cursor(txn, &cursor, DB_READ_COMMITTED);

        // move the cursor to the record.
        rc = cursor->get(&dbkey, &currentData, DB_SET | DB_RMW);
        if (rc != 0) {
            cursor->close();
            txn->abort();
            if (rc == DB_NOTFOUND) {
                return KeyNotFound;
            } else if (rc != DB_LOCK_DEADLOCK) {
                fprintf(stderr, "Db::get() returned: %s", db_strerror(rc));
                return Error;
            }
            continue;
        }

        // update the record.
        rc = cursor->put(NULL, &dbdata, DB_CURRENT);
        cursor->close();
        if (rc == 0) {
            txn->commit(DB_TXN_SYNC);
            return Success;
        } else {
            txn->abort();
            if (rc != DB_LOCK_DEADLOCK) {
                fprintf(stderr, "Db::put() returned: %s", db_strerror(rc));
                return Error;
            }
        }
    }
    fprintf(stderr, "update failed %d times", numRetries_);
    return Error;
}
Ejemplo n.º 13
0
void AntiCacheDB::writeBlock(const std::string tableName,
                             int16_t blockId,
                             const int tupleCount,
                             const char* data,
                             const long size) {
    Dbt key; 
    key.set_data(&blockId);
    key.set_size(sizeof(int16_t));
    
    Dbt value;
    value.set_data(const_cast<char*>(data));
    value.set_size(static_cast<int32_t>(size)); 
    
    VOLT_DEBUG("Writing out a block #%d to anti-cache database [tuples=%d / size=%ld]",
               blockId, tupleCount, size);
    // TODO: Error checking
    m_db->put(NULL, &key, &value, 0);
}
Ejemplo n.º 14
0
 static void make_record(const std::string& uid, const std::string& id, const std::string& owner, const std::list<std::string>& meta, Dbt& key, Dbt& data) {
   key.set_data(NULL); key.set_size(0);
   data.set_data(NULL); data.set_size(0);
   uint32_t l = 4 + uid.length();
   for(std::list<std::string>::const_iterator m = meta.begin(); m != meta.end(); ++m) {
     l += 4 + m->length();
   };
   make_key(id,owner,key);
   void* d = (void*)::malloc(l);
   if(!d) {
     ::free(key.get_data());
     key.set_data(NULL); key.set_size(0);
     return;
   };
   data.set_data(d); data.set_size(l);
   d = store_string(uid,d);
   for(std::list<std::string>::const_iterator m = meta.begin(); m != meta.end(); ++m) {
     d = store_string(*m,d);
   };
 }
Ejemplo n.º 15
0
float CKeyValuePair::GetFloat(const std::string &key) {
  structKey k( Float, key.size(), key.c_str() );
  Dbt v;
  float value;
  v.set_flags( DB_DBT_USERMEM );
  v.set_ulen( sizeof( float ) );
  v.set_size( sizeof( float ) );
  v.set_data( &value );  
  Get( k, &v );
  return value;
}
Ejemplo n.º 16
0
double CKeyValuePair::GetDouble(const std::string &key) {
  structKey k( Double, key.size(), key.c_str() );
  Dbt v;
  double value;
  v.set_flags( DB_DBT_USERMEM );
  v.set_ulen( sizeof( double ) );
  v.set_size( sizeof( double ) );
  v.set_data( &value );  
  Get( k, &v );
  return value;
}
Ejemplo n.º 17
0
void operator<<(Dbt& dbt, const T& obj)
{
  QByteArray ba;
  QDataStream ds(&ba, QIODevice::WriteOnly);

  // Serialize the object
  ds << obj;

  dbt.set_ulen(ba.size());
  dbt.set_data(ba.data());
}
Ejemplo n.º 18
0
short CKeyValuePair::GetShort(const std::string &key) {
  structKey k( Int16, key.size(), key.c_str() );
  Dbt v;
  short value;
  v.set_flags( DB_DBT_USERMEM );
  v.set_ulen( sizeof( short ) );
  v.set_size( sizeof( short ) );
  v.set_data( &value );  
  Get( k, &v );
  return value;
}
Ejemplo n.º 19
0
unsigned long CKeyValuePair::GetUnsignedLong(const std::string &key) {
  structKey k( UInt32, key.size(), key.c_str() );
  Dbt v;
  unsigned long value;
  v.set_flags( DB_DBT_USERMEM );
  v.set_ulen( sizeof( unsigned long ) );
  v.set_size( sizeof( unsigned long ) );
  v.set_data( &value );  
  Get( k, &v );
  return value;
}
Ejemplo n.º 20
0
void berkeleydb_store<Object>::get_range(const timestamp& from,
                                         const timestamp& to,
                                         const long lp_id,
                                         std::vector<obj_ptr>& ret_obj) {
  boost::lock_guard<boost::mutex> guard(get_mutex_);
  if (from > to) return;

  /* generate key */
  std::vector<char> from_char;
  key_lpid_to_char(from, lp_id, from_char);
  Dbt db_key = Dbt(&from_char[0], from_char.size());

  /* prepare data */
  char* binary;
  Dbt data;
  data.set_data(binary);
  data.set_flags(DB_DBT_MALLOC);

  /* seek to from */
  Dbc* cursorp;
  db->cursor(NULL, &cursorp, 0);
  int ret = cursorp->get(&db_key, &data, DB_SET_RANGE);
  if (ret) {
    if (cursorp != NULL) cursorp->close();
    return;
  }
  std::string get_data_binary
      = std::string((char*) data.get_data(), data.get_size());
  std::stringstream from_ss;
  from_ss << get_data_binary;
  boost::archive::binary_iarchive iar(from_ss);
  Object obj;
  iar >> obj;
  ret_obj.push_back(boost::make_shared<Object>(obj));

  /* get until end */
  while ((ret = cursorp->get(&db_key, &data, DB_NEXT)) == 0) {
    timestamp tmstmp;
    long id;
    char_to_key_lpid((char*) db_key.get_data(), tmstmp, id);
    if (tmstmp > to || id != lp_id) break;

    get_data_binary = std::string((char*) data.get_data(), data.get_size());
    std::stringstream ss;
    ss << get_data_binary;
    boost::archive::binary_iarchive iar(ss);
    Object deserialized_obj;
    iar >> deserialized_obj;
    ret_obj.push_back(boost::make_shared<Object>(deserialized_obj));
  }

  if (cursorp != NULL) cursorp->close();
};
Ejemplo n.º 21
0
void DatabaseManager::addRecord(const StringPimpl &key,
								const StringPimpl &value)
{
	//Set up the key
	Dbt dbKey;
	dbKey.set_data((void *)key.c_str());
	dbKey.set_size((key.size() + 1) * sizeof(char));

	//Set up the data to insert
	Dbt dbData;
	dbData.set_data((void *)value.c_str());
	dbData.set_size((value.size() + 1) * sizeof(char));
	
	try 
	{
		m_pimpl->checkTSSInit();
		int ret = m_pimpl->m_database->put(&(**m_pimpl->m_transaction), &dbKey, &dbData, 0);
	}
	catch (DbException &e) 
	{
            std::cerr << "Error in addRecord: "
                       << e.what() << e.get_errno() << std::endl;
    }
}
Ejemplo n.º 22
0
 string read(const char* skey){
   Dbt key((void*)skey, ::strlen(skey));
   //fetch
   char* buf = _allocHeap(); //keep as heap mem
   Dbt data;
   data.set_data(buf);
   data.set_ulen(MAX_LEN);
   data.set_flags(DB_DBT_USERMEM);
   if( _db->get(nullptr, &key, &data, 0) == DB_NOTFOUND){
     return string("F");
   };
   string res(buf);
   delete[] buf;
   return res;
 };
Ejemplo n.º 23
0
void test_dbt(void) {
    u_int32_t size  = 3;
    u_int32_t flags = 5;
    u_int32_t ulen  = 7;
    void*     data  = &size;
    Dbt dbt;

    dbt.set_size(size);
    dbt.set_flags(flags);
    dbt.set_data(data);
    dbt.set_ulen(ulen);
    assert(dbt.get_size()  == size);
    assert(dbt.get_flags() == flags);
    assert(dbt.get_data()  == data);
    assert(dbt.get_ulen()  == ulen);
}
Ejemplo n.º 24
0
 /* Adds a single word pertinent to a single edit */
 void addWord(const std::string & word, bool is_bad) {
     Dbt key(const_cast<void *>(reinterpret_cast<const void *>(word.c_str())), word.size());
     Dbt data;
     BayesDBData bdata;
     if(db.get(NULL, &key, &data, 0) != DB_NOTFOUND) {
         int s = data.get_size();
         if(s != sizeof(bdata)) throw std::runtime_error("Bayesian DB returned wrong record size");
         void * vdata = data.get_data();
         memcpy((void *)&bdata, vdata, s);
     }
     if(is_bad) {
         bdata.bad_edits++;
     } else {
         bdata.good_edits++;
     }
     data.set_data((void *)&bdata);
     data.set_size(sizeof(bdata));
     db.put(NULL, &key, &data, 0);
 }
// Shows a vendor record. Each vendor record is an instance of
// a vendor structure. See loadVendorDB() in
// example_database_load for how this structure was originally
// put into the database.
int
show_vendor(MyDb &vendorDB, const char *vendor)
{
	Dbt data;
	VENDOR my_vendor;

	try {
		// Set the search key to the vendor's name
		// vendor is explicitly cast to char * to stop a compiler
		// complaint.
		Dbt key((char *)vendor, (u_int32_t)strlen(vendor) + 1);

		// Make sure we use the memory we set aside for the VENDOR
		// structure rather than the memory that DB allocates.
		// Some systems may require structures to be aligned in memory
		// in a specific way, and DB may not get it right.

		data.set_data(&my_vendor);
		data.set_ulen(sizeof(VENDOR));
		data.set_flags(DB_DBT_USERMEM);

		// Get the record
		vendorDB.getDb().get(NULL, &key, &data, 0);
		std::cout << "        " << my_vendor.street << "\n"
		<< "        " << my_vendor.city << ", "
		<< my_vendor.state << "\n"
		<< "        " << my_vendor.zipcode << "\n"
		<< "        " << my_vendor.phone_number << "\n"
		<< "        Contact: " << my_vendor.sales_rep << "\n"
		<< "                 " << my_vendor.sales_rep_phone
		<< std::endl;

	}
	catch (DbException &e) {
		vendorDB.getDb().err(e.get_errno(), "Error in show_vendor");
		throw e;
	}
	catch (std::exception &e) {
		throw e;
	}
	return (0);
}
Ejemplo n.º 26
0
int UserDatabase::del_record(unsigned long uid)
{
    if(m_pmazeuserDB == NULL)
	   	return -1;
	   
	Dbt key;
	init_DBT(&key,&key);
	int ret;

	key.set_data(&uid);
	key.set_size(sizeof(uid));
	
	ret = m_pmazeuserDB->del(NULL, &key, 0);
	if (ret != 0)
	{
		debug("del record error!"<<uid);
	}
	return ret;

}
Ejemplo n.º 27
0
bool QueueScheduler::dbDelete(QString _scheduleName)
{
	Dbt key;

	QByteArray ba;
    const char *c_str = 0;

	ba = _scheduleName.toLocal8Bit();
	c_str = ba.data();

	key.set_data((void*)c_str);
	key.set_size(_scheduleName.size());

	if ((schedDb->del(NULL, &key, 0)) != 0)
		return false;
	else
	{
		schedDb->sync(0);
		return true;
	}
}
Ejemplo n.º 28
0
void berkeleydb_store<Object>::get_prev(const timestamp& time,
                                        const long lp_id,
                                        obj_ptr& ret_obj,
                                        timestamp& time_of_ret) {
  boost::lock_guard<boost::mutex> guard(get_mutex_);
  /* generate key */
  std::vector<char> key;
  key_lpid_to_char(time, lp_id, key);
  Dbt db_key = Dbt(&key[0], key.size());

  /* prepare data */
  char* binary;
  Dbt data;
  data.set_data(binary);
  data.set_flags(DB_DBT_MALLOC);

  /* seek to key */
  Dbc* cursorp;
  db->cursor(NULL, &cursorp, 0);
  int ret = cursorp->get(&db_key, &data, DB_SET_RANGE);

  /* seek back */
  ret = cursorp->get(&db_key, &data, DB_PREV);

  /* get value */
  std::string obj_binary
      = std::string((char*) data.get_data(), data.get_size());
  std::stringstream ss;
  ss << obj_binary;
  boost::archive::binary_iarchive iar(ss);
  Object obj;
  iar >> obj;
  ret_obj = boost::make_shared<Object>(obj);

  /* get key */
  long lp_id_;
  char_to_key_lpid((char*) db_key.get_data(), time_of_ret, lp_id_);
  if (cursorp != NULL) cursorp->close();
};
Ejemplo n.º 29
0
int UserDatabase::del_record(string email)
{
    if(m_psec_mazeuserDB == NULL)
	   	return -1;
	   
	int ret;
	Dbt key;
	
	init_DBT(&key,&key);
	
	key.set_data(const_cast<char*>(email.c_str()) );
	key.set_size(email.length() + 1);
	
	ret = m_psec_mazeuserDB->del(NULL, &key, 0); 
	
	if (ret != 0)
	{
		debug("Delete record Error!"<<email);
	}
	return ret;

}
Ejemplo n.º 30
0
void MultiPartHeader::deleteAllParts(Db* pDB)
{
	int ret = 0;
    Dbc *cursorp = 0;

	Dbt key;
	Dbt data;

    //qDebug() << "Deleting all parts for key " << multiPartKey;

	key.set_data(&multiPartKey);
	key.set_size(sizeof(quint64));

	// Get a cursor
	pDB->cursor(NULL, &cursorp, DB_WRITECURSOR);

	while ((ret = cursorp->get(&key, &data, DB_SET)) == 0)
	    cursorp->del(0);

	pDB->sync(0);

	if (cursorp != NULL)
	    cursorp->close();
}