[ZODB-Dev] Problems when reading from ZODB

Christian Reis kiko at async.com.br
Sat May 17 19:47:08 EDT 2003


Oba. Would have been a bit easier if you told us *what* you were doing
right at the top :-)

On Sat, May 17, 2003 at 01:37:37PM -0500, Luis Machado wrote:
> The following program writes 100 customers into ZODB

Okaaay, so:

> from Customer import Customer
> from ZODB import FileStorage, DB
> 
> storage =
> FileStorage.FileStorage('/home/lmachado/dev/zodb/filestorage01.fs')
> db = DB(storage)
> conn = db.open()
> dbroot = conn.root()
> if dbroot.has_key('customerDB'):
>   del dbroot['customerDB']
> 
> if not dbroot.has_key('customerDB'):
>   from BTrees import OOBTree 
>   customerDB = OOBTree.OOBTree()
>   dbroot['customerDB'] = customerDB
>   get_transaction().commit()   
> 
> c = Customer()

Why are you creating the customer only once? :-) Move the instantiation
into the loop and you'll be doing better.

> for i in range (1,100):
>  c.company = 'Any Company' + '-' + str(i) 
>  c.name = 'Jim Brown'  + '-' + str(i) 
>  c.address1 = '15230 Lexington Blv' + '-' + str(i) 
>  c.address2 = ''
>  c.city = 'Sugar Land'  + '-' + str(i) 
>  c.state = 'TX'  + '-' + str(i) 
>  c.zip = '77478'  + '-' + str(i) 
>  c.country = 'USA'  + '-' + str(i) 
>  c.phone =  str(i)  
>  c.web = 'www.anycompany.com'  + '-' + str(i) 
>  c.email = 'jim.brown at anycompany.com'  + '-' + str(i) 
>  c.fax = '15345232'  + '-' + str(i)
>  c_p_changed = 1
>  customerDB.insert(c.company,c)
>  customerDB._p_changed = 1  
>  get_transaction().commit()

Another thing that I'm a bit confused about is your insert here. If you
want to store in an OOBTree, remember it is a mapping, and in a mapping
we have keys mapped to values (that's what o.insert(a,b) does in
disguise: o[a] = b).

> customerDB = dbroot['customerDB']
> for k in customerDB.keys():
>  c = customerDB.__getitem__(k)
>  print k + ' - ' + c.name

Two things:

    a) I'm not sure how you want to store your Customer objects. If you
    want to store them keyed on company name, it would make sense to
    have the names be checked for uniqueness, or else each
    customerDB[name] = c with a repeated name will crush the previous
    one.

    n) You can use c = customerDB[k], it's compatible with a Python
    dict -- __getitem__ is overkill :-)

look at IndexedCatalog. Sooner or later you will outgrow your
"customerDB" database and you'll want a smarter data structure --
Catalogs do exactly that for you, and free of charge ;)

    http://www.async.com.br/projects/IndexedCatalog/

Take care,
--
Christian Reis, Senior Engineer, Async Open Source, Brazil.
http://async.com.br/~kiko/ | [+55 16] 261 2331 | NMFL



More information about the ZODB-Dev mailing list