lucy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mar...@apache.org
Subject svn commit: r887255 - in /lucene/lucy/trunk: core/Lucy/Object/VArray.bp core/Lucy/Object/VArray.c core/Lucy/Test/Object/TestVArray.c perl/lib/Lucy.pm perl/lib/Lucy/Object/VArray.pm perl/t/binding/016-varray.t
Date Fri, 04 Dec 2009 16:26:32 GMT
Author: marvin
Date: Fri Dec  4 16:26:31 2009
New Revision: 887255

URL: http://svn.apache.org/viewvc?rev=887255&view=rev
Log:
Commit varray_serialization.diff from LUCY-81.

Modified:
    lucene/lucy/trunk/core/Lucy/Object/VArray.bp
    lucene/lucy/trunk/core/Lucy/Object/VArray.c
    lucene/lucy/trunk/core/Lucy/Test/Object/TestVArray.c
    lucene/lucy/trunk/perl/lib/Lucy.pm
    lucene/lucy/trunk/perl/lib/Lucy/Object/VArray.pm
    lucene/lucy/trunk/perl/t/binding/016-varray.t

Modified: lucene/lucy/trunk/core/Lucy/Object/VArray.bp
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/core/Lucy/Object/VArray.bp?rev=887255&r1=887254&r2=887255&view=diff
==============================================================================
--- lucene/lucy/trunk/core/Lucy/Object/VArray.bp (original)
+++ lucene/lucy/trunk/core/Lucy/Object/VArray.bp Fri Dec  4 16:26:31 2009
@@ -146,6 +146,12 @@
     Load(VArray *self, Obj *dump);
 
     public void
+    Serialize(VArray *self, OutStream *outstream);
+
+    public incremented VArray*
+    Deserialize(VArray *self, InStream *instream);
+
+    public void
     Destroy(VArray *self);
 }
 

Modified: lucene/lucy/trunk/core/Lucy/Object/VArray.c
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/core/Lucy/Object/VArray.c?rev=887255&r1=887254&r2=887255&view=diff
==============================================================================
--- lucene/lucy/trunk/core/Lucy/Object/VArray.c (original)
+++ lucene/lucy/trunk/core/Lucy/Object/VArray.c Fri Dec  4 16:26:31 2009
@@ -9,7 +9,10 @@
 #include "Lucy/Object/VArray.h"
 #include "Lucy/Object/Err.h"
 #include "Lucy/Util/Memory.h"
+#include "Lucy/Util/Freezer.h"
 #include "Lucy/Util/SortUtils.h"
+#include "Lucy/Store/InStream.h"
+#include "Lucy/Store/OutStream.h"
 
 #define MAYBE_GROW(_self, _new_size) \
     do { \
@@ -85,6 +88,46 @@
     return loaded;
 }
 
+void
+VA_serialize(VArray *self, OutStream *outstream)
+{
+    u32_t i;
+    u32_t last_valid_tick = 0;
+    OutStream_Write_C32(outstream, self->size);
+    for (i = 0; i < self->size; i++) {
+        Obj *elem = self->elems[i];
+        if (elem) {
+            OutStream_Write_C32(outstream, i - last_valid_tick);
+            FREEZE(elem, outstream);
+            last_valid_tick = i;
+        }
+    }
+    /* Terminate. */
+    OutStream_Write_C32(outstream, self->size - last_valid_tick);
+}
+
+VArray*
+VA_deserialize(VArray *self, InStream *instream)
+{
+    u32_t tick;
+    u32_t size = InStream_Read_C32(instream);
+    if (self) {
+        self->size = size;
+        self->cap = size + 1;
+        self->elems = (Obj**)CALLOCATE(self->cap, sizeof(Obj*));
+    }
+    else self = VA_new(size);
+    for (tick = InStream_Read_C32(instream); 
+         tick < size; 
+         tick += InStream_Read_C32(instream)
+    ) {
+        Obj *obj = THAW(instream);
+        self->elems[tick] = obj;
+    }
+    self->size = size;
+    return self;
+}
+
 VArray*
 VA_clone(VArray *self)
 {

Modified: lucene/lucy/trunk/core/Lucy/Test/Object/TestVArray.c
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/core/Lucy/Test/Object/TestVArray.c?rev=887255&r1=887254&r2=887255&view=diff
==============================================================================
--- lucene/lucy/trunk/core/Lucy/Test/Object/TestVArray.c (original)
+++ lucene/lucy/trunk/core/Lucy/Test/Object/TestVArray.c Fri Dec  4 16:26:31 2009
@@ -2,6 +2,7 @@
 #include "Lucy/Util/ToolSet.h"
 
 #include "Lucy/Test.h"
+#include "Lucy/Test/TestUtils.h"
 #include "Lucy/Test/Object/TestVArray.h"
 
 static CharBuf*
@@ -247,10 +248,24 @@
     DECREF(loaded);
 }
 
+static void
+test_serialization(TestBatch *batch)
+{
+    VArray *array  = VA_new(0);
+    VArray *dupe;
+    VA_Store(array, 1, (Obj*)CB_newf("foo"));
+    VA_Store(array, 3, (Obj*)CB_newf("bar"));
+    dupe = (VArray*)TestUtils_freeze_thaw((Obj*)array);
+    ASSERT_TRUE(batch, dupe && VA_Equals(array, (Obj*)dupe), 
+        "Round trip through FREEZE/THAW");
+    DECREF(dupe);
+    DECREF(array);
+}
+
 void
 TestVArray_run_tests()
 {
-    TestBatch *batch = Test_new_batch("TestVArray", 38, NULL);
+    TestBatch *batch = Test_new_batch("TestVArray", 39, NULL);
 
     PLAN(batch);
 
@@ -263,6 +278,7 @@
     test_Push_VArray(batch);
     test_Clone_and_Shallow_Copy(batch);
     test_Dump_and_Load(batch);
+    test_serialization(batch);
 
     batch->destroy(batch);
 }

Modified: lucene/lucy/trunk/perl/lib/Lucy.pm
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/perl/lib/Lucy.pm?rev=887255&r1=887254&r2=887255&view=diff
==============================================================================
--- lucene/lucy/trunk/perl/lib/Lucy.pm (original)
+++ lucene/lucy/trunk/perl/lib/Lucy.pm Fri Dec  4 16:26:31 2009
@@ -124,6 +124,7 @@
     package Lucy::Object::VArray;
     no warnings 'redefine';
     sub clone { CORE::shift->_clone }
+    sub deserialize { CORE::shift->_deserialize(@_) }
 }
 
 {

Modified: lucene/lucy/trunk/perl/lib/Lucy/Object/VArray.pm
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/perl/lib/Lucy/Object/VArray.pm?rev=887255&r1=887254&r2=887255&view=diff
==============================================================================
--- lucene/lucy/trunk/perl/lib/Lucy/Object/VArray.pm (original)
+++ lucene/lucy/trunk/perl/lib/Lucy/Object/VArray.pm Fri Dec  4 16:26:31 2009
@@ -17,6 +17,15 @@
 OUTPUT: RETVAL
 
 SV*
+_deserialize(either_sv, instream)
+    SV *either_sv;
+    lucy_InStream *instream;
+CODE:
+    CHY_UNUSED_VAR(either_sv);
+    RETVAL = LUCY_OBJ_TO_SV_NOINC(lucy_VA_deserialize(NULL, instream));
+OUTPUT: RETVAL
+
+SV*
 _clone(self)
     lucy_VArray *self;
 CODE:

Modified: lucene/lucy/trunk/perl/t/binding/016-varray.t
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/perl/t/binding/016-varray.t?rev=887255&r1=887254&r2=887255&view=diff
==============================================================================
--- lucene/lucy/trunk/perl/t/binding/016-varray.t (original)
+++ lucene/lucy/trunk/perl/t/binding/016-varray.t Fri Dec  4 16:26:31 2009
@@ -1,7 +1,8 @@
 use strict;
 use warnings;
 
-use Test::More tests => 1;
+use Test::More tests => 3;
+use Storable qw( nfreeze thaw );
 use Lucy::Test;
 
 my ( $varray, $evil_twin );
@@ -9,6 +10,20 @@
 $varray = Lucy::Object::VArray->new( capacity => 5 );
 $varray->push( Lucy::Object::CharBuf->new($_) ) for 1 .. 5;
 $varray->delete(3);
+my $frozen = nfreeze($varray);
+my $thawed = thaw($frozen);
+is_deeply( $thawed->to_perl, $varray->to_perl, "freeze/thaw" );
+
+my $ram_file = Lucy::Store::RAMFile->new;
+my $outstream = Lucy::Store::OutStream->open( file => $ram_file )
+    or die Lucy->error;
+$varray->serialize($outstream);
+$outstream->close;
+my $instream = Lucy::Store::InStream->open( file => $ram_file )
+    or die Lucy->error;
+my $deserialized = $varray->deserialize($instream);
+is_deeply( $varray->to_perl, $deserialized->to_perl,
+    "serialize/deserialize" );
 
 $evil_twin = $varray->_clone;
 is_deeply( $evil_twin->to_perl, $varray->to_perl, "clone" );



Mime
View raw message