Hello,
On Monday 18 January 2010 04:32:41 Maciej Sobczak wrote:
> Shridhar Daithankar wrote:
> > What is the expected way of handling vector ORM mapped objects?
>
> Bulk operations with ORM mapped types are not supported.
> If you need to read many rows of ORM data, you should use prepared
> statements.
Unfortunately prepared statements with ORM is a real performance killer. PFA
a test program. The results with db server on localhost are as follows.
shrid...@bheem$ bjam && time bin/gcc-4.4.2/release/bulk-orm orm
...found 56 targets...
...updating 2 targets...
gcc.compile.c++ bin/gcc-4.4.2/release/bulk-orm.o
gcc.link bin/gcc-4.4.2/release/bulk-orm
...updated 2 targets...
real 2m20.055s
user 2m17.741s
sys 0m0.337s
shrid...@bheem $ bjam && time bin/gcc-4.4.2/release/bulk-orm insert
...found 56 targets...
real 0m0.674s
user 0m0.060s
sys 0m0.087s
shrid...@bheem $ bjam && time bin/gcc-4.4.2/release/bulk-orm select
...found 56 targets...
real 0m0.043s
user 0m0.010s
sys 0m0.000s
with ORM it is possible to generate mapping by means of a program and maintain
them easily. They are really easy to use too, since all the possible fields in
a query are already present. With individual into/use elements, things are lot
more hap-hazard and repetative. :(
Is it really impossible to support bulk operations for ORM types? I tried
going thr. the code but couldn't give it enough time.
--
Shridhar
#include <soci.h>
#include <soci-postgresql.h>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#undef NDEBUG
#include <cassert>
using namespace soci;
const int maxSize = 20000;
struct person
{
std::string name,address;
};
namespace soci {
template<> struct type_conversion<person>
{
typedef values base_type;
static void from_base(values const & v, indicator , person & p)
{
p.name = v.get<std::string>("name");
p.address = v.get<std::string>("address");
}
static void to_base(const person & p, values & v, indicator & ind)
{
v.set("name",p.name);
v.set("address",p.address);
ind = soci::i_ok;
}
};
}
void setvalue(const person & p,soci::values & v)
{
v.set("name",p.name);
v.set("address",p.address);
}
void prepare(std::vector<person> & v, std::size_t n=5)
{
v.clear();
for(std::size_t i=0;i<n;i++)
{
person p;
std::stringstream s;
s << "shridhar" << i;
p.name = s.str();
s.clear();s.str("");
s << "pune" << i ;
p.address = s.str();
// std::cout << p.name << " " << p.address << std::endl;
v.push_back(p);
}
}
void prepare(std::vector<std::string> & names, std::vector<std::string> & addresses,std::size_t n=5)
{
names.clear();addresses.clear();
for(std::size_t i=0;i<n;i++)
{
std::stringstream s;
s << "shridhar" << i;
names.push_back(s.str());
s.clear();s.str("");
s << "pune" << i ;
addresses.push_back(s.str());
}
}
void orm(soci::session & sql)
{
std::vector<person> v;
transaction tr(sql);
sql.once << "delete from person";
prepare(v,maxSize);
statement st(sql);
st.alloc();
st.prepare("insert into person(name,address) values(:name,:address)");
// values val,val1;
for(std::size_t i=0;i<v.size();i++)
{
values val;
setvalue(v[i],val);
st.bind(val);
st.execute(true);
}
tr.commit();
std::cout << std::endl;
}
void insert(soci::session & sql)
{
transaction tr(sql);
sql.once << "delete from person";
std::vector<std::string> names,addresses;
prepare(names,addresses,maxSize);
statement st = (sql.prepare << "insert into person(name,address) values(:name,:address)",use(names),use(addresses));
st.execute(true);
tr.commit();
}
void select(soci::session & sql)
{
std::vector<std::string> names,addresses;
names.resize(maxSize>50?maxSize/20:20);addresses.resize(maxSize>50?maxSize/20:20);
statement st = (sql.prepare << "select name,address from person",into(names),into(addresses));
st.execute();
//when fetch returns false, the vector is sized down to zero
while(st.fetch());
}
int main(int argc,char **argv)
{
if(argc<2)
{
std::cout << "usage:" << argv[0] << " insert|orm|select" << std::endl;
return -1;
}
std::string test = argv[1];
try
{
session sql(postgresql,"dbname=test");
if(test=="insert")
insert(sql);
else
{
if(test=="orm")
orm(sql);
else
{
if(test=="select")
select(sql);
else
std::cout << "Unrecognized test:" << test << std::endl;
}
}
}
catch(std::exception & e)
{
std::cout << e.what() << std::endl;
}
return 0;
}
------------------------------------------------------------------------------
Throughout its 18-year history, RSA Conference consistently attracts the
world's best and brightest in the field, creating opportunities for Conference
attendees to learn about information security's most important issues through
interactions with peers, luminaries and emerging and established companies.
http://p.sf.net/sfu/rsaconf-dev2dev
_______________________________________________
Soci-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/soci-users