void  write_into_the_database(DB_t DB[], DB_t updated_DB[], int8_t flags[], DB_t b[],  DB_t final_CW, bool party, size_t db_nitems, size_t rotate_by = 0)
{
 	for(size_t j = 0; j < db_nitems; ++j)
 	{ 
	  const size_t ind = (j + rotate_by) % db_nitems;
	 	DB_t tmp = (flags[ind] * final_CW);
	 	DB[j] = DB[j] + b[ind]  + tmp;     
		 updated_DB[j] = DB[j];
 	}
}
 
void reconstruct_database(tcp::socket& sb, DB_t DB[], const size_t db_nitems)
{
		for(size_t j = 0; j < db_nitems; ++j)
		{
			DB_t DB_j;
			boost::asio::write(sb, boost::asio::buffer(&DB[j], sizeof(DB[j])));
			boost::asio::read(sb, boost::asio::buffer(&DB_j, sizeof(DB_j)));
			DB_j = DB_j + DB[j];
			if(DB_j != 0) std::cout << j << " -> " << DB_j << std::endl;
		}
}


auto DuoramUpdate(bool party, size_t db_nitems,   size_t rotate_by_,
				  DB_t DB[], DB_t updated_DB[], int8_t writing_b[], DB_t b[],  DB_t final_CW, DB_t message,
				  int8_t writing_c[], int8_t writing_d[], DB_t c[], DB_t d[])
{

	read_flags_for_updating(party, db_nitems);
	write_into_the_database(DB, updated_DB, writing_b, b, final_CW, party, db_nitems, - rotate_by_);
			
	#ifdef ThreeParty
		if(!party) duoram_refresh(writing_b, writing_c, writing_d, b, c, d,  final_CW, party, db_nitems, -rotate_by_); 			
		if( party) duoram_refresh(writing_b, writing_d, writing_c, b, d, c,  final_CW, party, db_nitems, -rotate_by_); 
	#endif
}