[CSHARP-1112] Simplify IByteBuffer interface Created: 20/Nov/14  Updated: 06/Apr/15  Resolved: 09/Mar/15

Status: Closed
Project: C# Driver
Component/s: BSON
Affects Version/s: None
Fix Version/s: 2.0

Type: Improvement Priority: Major - P3
Reporter: Robert Stam Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

The IByteBuffer interface represents a logical byte buffer that might be backed by various means (contiguous, segmented, using buffers from a pool or not, etc...).

But the IByteBuffer interface probably has too many methods. Many of the methods could be moved out to extension methods on the interface (so they only need to be implemented once).

A new streamlined interface that deals only with the buffer abstraction could look like this:

public interface IByteBuffer : IDisposable
{
    // properties
    int Capacity { get; }
    int Length { get; set; }
 
    // methods
    void EnsureCapacity(int minimumCapacity);
    ArraySegment<byte> AccessBackingBytes(int offset, int limit);
}

This reduces the responsibility of this interface to a single responsibility, namely to represent a (possibly) segmented variable length byte array.



 Comments   
Comment by Robert Stam [ 06/Apr/15 ]

In the end the IByteBuffer interface ended up having a few more members that the super streamlined version suggested in the description.

public interface IByteBuffer : IDisposable
{
    int Capacity { get; }
    bool IsReadOnly { get; }
    int Length { get; set; }
 
    ArraySegment<byte> AccessBackingBytes(int position);
    void Clear(int position, int count);
    void EnsureCapacity(int minimumCapacity);
    IByteBuffer GetSlice(int position, int length);
    void MakeReadOnly();
    byte GetByte(int position);
    void GetBytes(int position, byte[] destination, int offset, int count);
    void SetByte(int position, byte value);
    void SetBytes(int position, byte[] source, int offset, int count);
}

The reason was efficiency. While several of these additional methods could have been implemented in terms of AccessBackingBytes, they can in fact be more efficiently implemented inside the classes that implement the IByteBuffer interface.

Comment by Githook User [ 09/Mar/15 ]

Author:

{u'username': u'rstam', u'name': u'rstam', u'email': u'robert@robertstam.org'}

Message: CSHARP-1112 and CSHARP-1117: Simplify IByteBuffer and enhance IBsonStream.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/068438c60f091c9412e85870e80813b5a755c8f5

Generated at Wed Feb 07 21:38:41 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.