Menu

#27 Excessive use of exceptions when searching for Storage and Stream children

v2.0
closed-fixed
Federico
None
5
2015-09-18
2015-01-27
No

I noticed that the GetStorage and GetStream methods make useof CFItemNotFound exceptions to indicate that a matching item could not be found. Generally this behavior should be avoided since exceptions incur significant performance overhead. In my case my software behaves differently if a particular stream/storage is already present or not (creating missing items as needed) This means my flow-control is based on handling exceptions which is not ideal. The only alternative available to me is to use the VisitEntries method and maintain my own external list of the children which is quite redundant.

I suggest adding TryGetStorage(String storageName) and TryGetStream(StorageName) to CFStorage. Below is my implementation:

public CFStorage TryGetStorage(String storageName)
{
    CheckDisposed();    
    CFMock tmp = new CFMock(storageName, StgType.StgStorage);
    CFItem outDe;
    if (Children.TryFind(tmp, out outDe) && outDe.DirEntry.StgType == StgType.StgStorage)
    {
        return outDe as CFStorage;
    }
    else
    {
        return null;
    }
}

public CFStream TryGetStream(String streamName)
{
    CheckDisposed();
    CFMock tmp = new CFMock(streamName, StgType.StgStream);
    CFItem outDe = null;
    if (Children.TryFind(tmp, out outDe) && outDe.DirEntry.StgType == StgType.StgStream)
    {
        return outDe as CFStream;
    }
    else
    {
        return null;
    }
}

An alternative is to provide a method that Gets a stream/storage if it exists, otherwise it is created. Perhaps an override to GetStorage/GetStream with a bool create parameter?

public CFStorage GetStorage(String storageName, bool create)
{    
    //...
}

Discussion

  • Federico

    Federico - 2015-09-18

    Thankk you for your suggestions. Integrated in release 2.0.
    Best Regards,
    Federico

     
  • Federico

    Federico - 2015-09-18
    • status: open --> closed-fixed
    • assigned_to: Federico
     

Log in to post a comment.

MongoDB Logo MongoDB