OGR Close Datasource?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

OGR Close Datasource?

Charles F. I. Savage
Wondering if it would make sense to introduce the concept of "closing" a
datasource to the OGR API as a distinct thing from "destroying" a data
source object?

I bring this up because I run into a problem with a test script in Ruby:

1.  Create temporary csv data source
2.  Do some stuff to it
3.  At the end of the test, delete the temporary data source

Pseudo code:

driver = Ogr.get_driver_by_name('CSV')
ds_name = "tempds"
ds = driver.open(ds_name)
...
ds = nil
GC.start     <- Try to gc the ds object here
driver.delete_data_source(ds_name)  <- Delete the data source

Before deleting the temporary data source, the ds object must be
destroyed (because it has the temporary data source open).  This means
that the test is reliant on the garbage collector deleting the ds
object.  Depending on the dynamic language, this may or may not work.

Thus it would be handy to have a "close" method on a datasource.  The
one gottcha I see is that once you've closed a datasource you can't use
it anymore.  Also, you would need to make sure the destructor of the
object doesn't try to close the datasource a second time.

Charlie

_______________________________________________
Gdal-dev mailing list
[hidden email]
http://lists.maptools.org/mailman/listinfo/gdal-dev
Reply | Threaded
Open this post in threaded view
|

Re: OGR Close Datasource?

Ari Jolma
Charlie Savage wrote:

> Wondering if it would make sense to introduce the concept of "closing" a
> datasource to the OGR API as a distinct thing from "destroying" a data
> source object?
>
> I bring this up because I run into a problem with a test script in Ruby:


What is the problem you run into? When I run this test code with Perl
bindings, the delete data source -method deletes the datasource (the
directory on hard disk) in both cases, whether datasource object still
exists or is deleted.

However, the datasource object seems at least somewhat operational after
the DeleteDataSource, it reports its name for example without an error.

Ari

>
> 1.  Create temporary csv data source
> 2.  Do some stuff to it
> 3.  At the end of the test, delete the temporary data source
>
> Pseudo code:
>
> driver = Ogr.get_driver_by_name('CSV')
> ds_name = "tempds"
> ds = driver.open(ds_name)
> ...
> ds = nil
> GC.start     <- Try to gc the ds object here
> driver.delete_data_source(ds_name)  <- Delete the data source
>
> Before deleting the temporary data source, the ds object must be
> destroyed (because it has the temporary data source open).  This means
> that the test is reliant on the garbage collector deleting the ds
> object.  Depending on the dynamic language, this may or may not work.
>
> Thus it would be handy to have a "close" method on a datasource.  The
> one gottcha I see is that once you've closed a datasource you can't use
> it anymore.  Also, you would need to make sure the destructor of the
> object doesn't try to close the datasource a second time.
>
> Charlie
>
> _______________________________________________
> Gdal-dev mailing list
> [hidden email]
> http://lists.maptools.org/mailman/listinfo/gdal-dev



--
Prof. Ari Jolma
Kartografia ja Geoinformatiikka / Cartography and Geoinformatics
Teknillinen Korkeakoulu / Helsinki University of Technology
POBox 1200, 02015 TKK, Finland
Email: ari.jolma at tkk.fi URL: http://www.tkk.fi/~jolma

_______________________________________________
Gdal-dev mailing list
[hidden email]
http://lists.maptools.org/mailman/listinfo/gdal-dev
Reply | Threaded
Open this post in threaded view
|

Re: OGR Close Datasource?

Frank Warmerdam
In reply to this post by Charles F. I. Savage
On 9/21/05, Charlie Savage <[hidden email]> wrote:
> Wondering if it would make sense to introduce the concept of "closing" a
> datasource to the OGR API as a distinct thing from "destroying" a data
> source object?
>
> I bring this up because I run into a problem with a test script in Ruby:
>
> 1.  Create temporary csv data source
> 2.  Do some stuff to it
> 3.  At the end of the test, delete the temporary data source

Charlie,

I am guessing that you want an explicit concept of Close() on the
C++ OGRDataSource object, is that right?  That is, close the datasource
regardless of how many references exist on it.  The problem I have with
this is that closing in all the drivers is currently accomplished in the
datasource destructors and so it would be a bit job to change all the
drivers to a separate Close() method.  Especially since then ideally
the datasource object would gracefully error out in case of accesses
to a closed data source.

So, I am against changing the core to include a Close() method.

However, I don't mind adding a Close() method in ogr.i that just
decrements the datasource reference count, and destroys it if
it has dropped to zero.  The downside of this approach is that any
other variables referring to the datasource that haven't been garbage
collected will still have a reference and hold open the datasource.

Sorry...

PS. In answer to Ari's point, on unix the Destroy method may have
unlinked files for a datasource but if they are still open they aren't
really deleted.  Behavior is different on win32 where unlinking an
in use file will fail.  Generally speaking you should not have a datasource
open when destroying it as results are undefined.

Best regards,
--
---------------------------------------+--------------------------------------
I set the clouds in motion - turn up   | Frank Warmerdam, [hidden email]
light and sound - activate the windows | http://pobox.com/~warmerdam
and watch the world go round - Rush    | Geospatial Programmer for Rent

_______________________________________________
Gdal-dev mailing list
[hidden email]
http://lists.maptools.org/mailman/listinfo/gdal-dev
Reply | Threaded
Open this post in threaded view
|

Re: OGR Close Datasource?

Charles F. I. Savage
In reply to this post by Ari Jolma
>
> What is the problem you run into? When I run this test code with Perl
> bindings, the delete data source -method deletes the datasource (the
> directory on hard disk) in both cases, whether datasource object still
> exists or is deleted.

What OS are you running?  On Windows XP it doesn't work because the csv
file is still in use by the open data source, thus you can't delete it.

Charlie


>>
>> 1.  Create temporary csv data source
>> 2.  Do some stuff to it
>> 3.  At the end of the test, delete the temporary data source
>>
>> Pseudo code:
>>
>> driver = Ogr.get_driver_by_name('CSV')
>> ds_name = "tempds"
>> ds = driver.open(ds_name)
>> ...
>> ds = nil
>> GC.start     <- Try to gc the ds object here
>> driver.delete_data_source(ds_name)  <- Delete the data source
>>
>> Before deleting the temporary data source, the ds object must be
>> destroyed (because it has the temporary data source open).  This means
>> that the test is reliant on the garbage collector deleting the ds
>> object.  Depending on the dynamic language, this may or may not work.
>>
>> Thus it would be handy to have a "close" method on a datasource.  The
>> one gottcha I see is that once you've closed a datasource you can't use
>> it anymore.  Also, you would need to make sure the destructor of the
>> object doesn't try to close the datasource a second time.
>>
>> Charlie
>>
>> _______________________________________________
>> Gdal-dev mailing list
>> [hidden email]
>> http://lists.maptools.org/mailman/listinfo/gdal-dev
>
>
>


_______________________________________________
Gdal-dev mailing list
[hidden email]
http://lists.maptools.org/mailman/listinfo/gdal-dev
Reply | Threaded
Open this post in threaded view
|

Re: OGR Close Datasource?

Charles F. I. Savage
In reply to this post by Frank Warmerdam
No problem, thought I'd ask though.  I don't think its worth doing in
the SWIG bindings because you end up with two reference counts - one in
SWIG and one for shared datasources in OGR.  Not sure how much this
problem would actually occur in real life.

Charlie

Frank Warmerdam wrote:

> On 9/21/05, Charlie Savage <[hidden email]> wrote:
>> Wondering if it would make sense to introduce the concept of "closing" a
>> datasource to the OGR API as a distinct thing from "destroying" a data
>> source object?
>>
>> I bring this up because I run into a problem with a test script in Ruby:
>>
>> 1.  Create temporary csv data source
>> 2.  Do some stuff to it
>> 3.  At the end of the test, delete the temporary data source
>
> Charlie,
>
> I am guessing that you want an explicit concept of Close() on the
> C++ OGRDataSource object, is that right?  That is, close the datasource
> regardless of how many references exist on it.  The problem I have with
> this is that closing in all the drivers is currently accomplished in the
> datasource destructors and so it would be a bit job to change all the
> drivers to a separate Close() method.  Especially since then ideally
> the datasource object would gracefully error out in case of accesses
> to a closed data source.
>
> So, I am against changing the core to include a Close() method.
>
> However, I don't mind adding a Close() method in ogr.i that just
> decrements the datasource reference count, and destroys it if
> it has dropped to zero.  The downside of this approach is that any
> other variables referring to the datasource that haven't been garbage
> collected will still have a reference and hold open the datasource.
>
> Sorry...
>
> PS. In answer to Ari's point, on unix the Destroy method may have
> unlinked files for a datasource but if they are still open they aren't
> really deleted.  Behavior is different on win32 where unlinking an
> in use file will fail.  Generally speaking you should not have a datasource
> open when destroying it as results are undefined.
>
> Best regards,
> --
> ---------------------------------------+--------------------------------------
> I set the clouds in motion - turn up   | Frank Warmerdam, [hidden email]
> light and sound - activate the windows | http://pobox.com/~warmerdam
> and watch the world go round - Rush    | Geospatial Programmer for Rent


_______________________________________________
Gdal-dev mailing list
[hidden email]
http://lists.maptools.org/mailman/listinfo/gdal-dev