You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@vxquery.apache.org by Vinayak Borkar <vb...@yahoo.com> on 2012/08/08 11:10:08 UTC

Use of ArrayBackedValueStorage

Preston,


I see some of your code using ArrayBackedValueStorage incorrectly.

For example, look at this snippet from ValueEqComparisonOperation

@Override
public boolean operateTimeTime(XSTimePointable timep1, XSTimePointable 
timep2, DynamicContext dCtx)
             throws SystemException, IOException {
     abvs.reset();
     DateTime.getTimezoneDateTime(timep1, dCtx, dOut);
     byte[] bytes1 = abvs.getByteArray();

     abvs.reset();
     DateTime.getTimezoneDateTime(timep2, dCtx, dOut);
     byte[] bytes2 = abvs.getByteArray();

     return XSDateTimePointable.getDayTime(bytes1, 0) == 
XSDateTimePointable.getDayTime(bytes2, 0);
}


ABVS reuses the byte array when the existing byte array is large enough 
to hold the new value. In effect bytes1 and bytes2 will point to the 
same byte[] in all likelihood and hence the comparison is wrong.

I saw some other places that reuse ABVSs and hold the byte array across 
resets on the same ABVS. Never do that.

The class should hold on to two different ABVS objects and use those in 
the two places instead of sharing the same ABVS for both the values.

Vinayak

Re: Use of ArrayBackedValueStorage

Posted by Vinayak Borkar <vb...@yahoo.com>.
Preston,

The ABVS class internally holds an array and tries as much as possible 
to reuse the same array over reset() calls. This way the data contained 
in the array can be modified for each value without requiring object 
construction -- the array is recycled for every data value. Once reset() 
is called, the array can be used for a new value and the old value is gone.

So a pattern like:

----------
abvs.reset();
// Put value1 in abvs
x = abvs.getByteArray();

abvs.reset(); // The value added into this abvs is gone
// Put value2 in abvs
y = abvs.getByteArray();
-----------


x no longer represents value1.

Makes sense?


Vinayak



On 8/9/12 2:21 PM, Eldon Carman wrote:
> Vinayak,
>
> Is this because when a new array is created its by reference and not a copy?
>
> Preston
>
> On Wed, Aug 8, 2012 at 2:10 AM, Vinayak Borkar <vb...@yahoo.com> wrote:
>> Preston,
>>
>>
>> I see some of your code using ArrayBackedValueStorage incorrectly.
>>
>> For example, look at this snippet from ValueEqComparisonOperation
>>
>> @Override
>> public boolean operateTimeTime(XSTimePointable timep1, XSTimePointable
>> timep2, DynamicContext dCtx)
>>              throws SystemException, IOException {
>>      abvs.reset();
>>      DateTime.getTimezoneDateTime(timep1, dCtx, dOut);
>>      byte[] bytes1 = abvs.getByteArray();
>>
>>      abvs.reset();
>>      DateTime.getTimezoneDateTime(timep2, dCtx, dOut);
>>      byte[] bytes2 = abvs.getByteArray();
>>
>>      return XSDateTimePointable.getDayTime(bytes1, 0) ==
>> XSDateTimePointable.getDayTime(bytes2, 0);
>> }
>>
>>
>> ABVS reuses the byte array when the existing byte array is large enough to
>> hold the new value. In effect bytes1 and bytes2 will point to the same
>> byte[] in all likelihood and hence the comparison is wrong.
>>
>> I saw some other places that reuse ABVSs and hold the byte array across
>> resets on the same ABVS. Never do that.
>>
>> The class should hold on to two different ABVS objects and use those in the
>> two places instead of sharing the same ABVS for both the values.
>>
>> Vinayak
>


Re: Use of ArrayBackedValueStorage

Posted by Eldon Carman <ec...@ucr.edu>.
Vinayak,

Is this because when a new array is created its by reference and not a copy?

Preston

On Wed, Aug 8, 2012 at 2:10 AM, Vinayak Borkar <vb...@yahoo.com> wrote:
> Preston,
>
>
> I see some of your code using ArrayBackedValueStorage incorrectly.
>
> For example, look at this snippet from ValueEqComparisonOperation
>
> @Override
> public boolean operateTimeTime(XSTimePointable timep1, XSTimePointable
> timep2, DynamicContext dCtx)
>             throws SystemException, IOException {
>     abvs.reset();
>     DateTime.getTimezoneDateTime(timep1, dCtx, dOut);
>     byte[] bytes1 = abvs.getByteArray();
>
>     abvs.reset();
>     DateTime.getTimezoneDateTime(timep2, dCtx, dOut);
>     byte[] bytes2 = abvs.getByteArray();
>
>     return XSDateTimePointable.getDayTime(bytes1, 0) ==
> XSDateTimePointable.getDayTime(bytes2, 0);
> }
>
>
> ABVS reuses the byte array when the existing byte array is large enough to
> hold the new value. In effect bytes1 and bytes2 will point to the same
> byte[] in all likelihood and hence the comparison is wrong.
>
> I saw some other places that reuse ABVSs and hold the byte array across
> resets on the same ABVS. Never do that.
>
> The class should hold on to two different ABVS objects and use those in the
> two places instead of sharing the same ABVS for both the values.
>
> Vinayak