lucy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nwelln...@apache.org
Subject [1/6] lucy git commit: Rewrite TextTermStepper
Date Sun, 15 Nov 2015 11:43:18 GMT
Repository: lucy
Updated Branches:
  refs/heads/master b2c004b77 -> 19921d847


Rewrite TextTermStepper

Switch type of ivars->value from CharBuf to String.

Before:
ivars->value  => CharBuf
ivars->string => String

After:
ivars->value   => String
ivars->charbuf => CharBuf

Only accept Strings in Set_Value and Write_Key_Frame (which erroneously
assumed CharBufs before) and optimize accordingly.

Only accept CharBufs in Write_Delta for now.


Project: http://git-wip-us.apache.org/repos/asf/lucy/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy/commit/d6fdddb0
Tree: http://git-wip-us.apache.org/repos/asf/lucy/tree/d6fdddb0
Diff: http://git-wip-us.apache.org/repos/asf/lucy/diff/d6fdddb0

Branch: refs/heads/master
Commit: d6fdddb0d4fa626c82c2a50bad04165c5f4b4d66
Parents: b2c004b
Author: Nick Wellnhofer <wellnhofer@aevum.de>
Authored: Sun Sep 27 00:22:12 2015 +0200
Committer: Nick Wellnhofer <wellnhofer@aevum.de>
Committed: Tue Nov 10 13:19:38 2015 +0100

----------------------------------------------------------------------
 core/Lucy/Plan/TextType.c   | 108 +++++++++++++++++++--------------------
 core/Lucy/Plan/TextType.cfh |   2 +-
 2 files changed, 53 insertions(+), 57 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy/blob/d6fdddb0/core/Lucy/Plan/TextType.c
----------------------------------------------------------------------
diff --git a/core/Lucy/Plan/TextType.c b/core/Lucy/Plan/TextType.c
index a27020a..a9f66b2 100644
--- a/core/Lucy/Plan/TextType.c
+++ b/core/Lucy/Plan/TextType.c
@@ -49,83 +49,78 @@ TextTermStepper*
 TextTermStepper_init(TextTermStepper *self) {
     TermStepper_init((TermStepper*)self);
     TextTermStepperIVARS *const ivars = TextTermStepper_IVARS(self);
-    ivars->value  = (Obj*)CB_new(0);
-    ivars->string = NULL;
+    ivars->charbuf = CB_new(0);
     return self;
 }
 
 void
 TextTermStepper_Destroy_IMP(TextTermStepper *self) {
     TextTermStepperIVARS *const ivars = TextTermStepper_IVARS(self);
-    DECREF(ivars->string);
+    DECREF(ivars->charbuf);
     SUPER_DESTROY(self, TEXTTERMSTEPPER);
 }
 
+static void
+S_set_value(TextTermStepper *self, Obj *value) {
+    TextTermStepperIVARS *const ivars = TextTermStepper_IVARS(self);
+    if (ivars->value != value) {
+        DECREF(ivars->value);
+        ivars->value = INCREF(value);
+    }
+}
+
 void
 TextTermStepper_Set_Value_IMP(TextTermStepper *self, Obj *value) {
-    TextTermStepperIVARS *const ivars = TextTermStepper_IVARS(self);
-    CERTIFY(value, STRING);
-    CB_Mimic((CharBuf*)ivars->value, value);
-    // Invalidate string.
-    DECREF(ivars->string);
-    ivars->string = NULL;
+    S_set_value(self, CERTIFY(value, STRING));
 }
 
 Obj*
 TextTermStepper_Get_Value_IMP(TextTermStepper *self) {
     TextTermStepperIVARS *const ivars = TextTermStepper_IVARS(self);
-    if (ivars->string == NULL) {
-        ivars->string = CB_To_String((CharBuf*)ivars->value);
+    if (ivars->value == NULL) {
+        ivars->value = (Obj*)CB_To_String(ivars->charbuf);
     }
-    return (Obj*)ivars->string;
+    return ivars->value;
 }
 
 void
 TextTermStepper_Reset_IMP(TextTermStepper *self) {
     TextTermStepperIVARS *const ivars = TextTermStepper_IVARS(self);
-    CB_Set_Size((CharBuf*)ivars->value, 0);
-    // Invalidate string.
-    DECREF(ivars->string);
-    ivars->string = NULL;
+    DECREF(ivars->value);
+    ivars->value = NULL;
+    CB_Set_Size(ivars->charbuf, 0);
 }
 
 void
 TextTermStepper_Write_Key_Frame_IMP(TextTermStepper *self,
                                     OutStream *outstream, Obj *value) {
-    TextTermStepperIVARS *const ivars = TextTermStepper_IVARS(self);
-    CharBuf *charbuf = (CharBuf*)ivars->value;
-    CB_Mimic(charbuf, value);
-    const char *buf  = CB_Get_Ptr8(charbuf);
-    size_t      size = CB_Get_Size(charbuf);
+    String     *string = (String*)CERTIFY(value, STRING);
+    const char *buf    = Str_Get_Ptr8(string);
+    size_t      size   = Str_Get_Size(string);
     OutStream_Write_C32(outstream, size);
     OutStream_Write_Bytes(outstream, buf, size);
-    // Invalidate string.
-    DECREF(ivars->string);
-    ivars->string = NULL;
+
+    S_set_value(self, value);
 }
 
 void
 TextTermStepper_Write_Delta_IMP(TextTermStepper *self, OutStream *outstream,
                                 Obj *value) {
     TextTermStepperIVARS *const ivars = TextTermStepper_IVARS(self);
-    CharBuf    *charbuf   = (CharBuf*)ivars->value;
-    const char *last_text = CB_Get_Ptr8(charbuf);
-    size_t      last_size = CB_Get_Size(charbuf);
-    const char *new_text  = NULL;
-    size_t      new_size  = 0;
-
-    if (Obj_is_a(value, STRING)) {
-        String *new_string = (String*)value;
-        new_text = Str_Get_Ptr8(new_string);
-        new_size = Str_Get_Size(new_string);
-    }
-    else if (Obj_is_a(value, CHARBUF)) {
-        CharBuf *new_charbuf = (CharBuf*)value;
-        new_text = CB_Get_Ptr8(new_charbuf);
-        new_size = CB_Get_Size(new_charbuf);
+    CharBuf    *charbuf  = (CharBuf*)CERTIFY(value, CHARBUF);
+    const char *new_text = CB_Get_Ptr8(charbuf);
+    size_t      new_size = CB_Get_Size(charbuf);
+
+    const char *last_text;
+    size_t      last_size;
+    if (ivars->value) {
+        String *last_string = (String*)ivars->value;
+        last_text = Str_Get_Ptr8(last_string);
+        last_size = Str_Get_Size(last_string);
     }
     else {
-        THROW(ERR, "'value' must be a String or CharBuf");
+        last_text = CB_Get_Ptr8(ivars->charbuf);
+        last_size = CB_Get_Size(ivars->charbuf);
     }
 
     // Count how many bytes the strings share at the top.
@@ -139,11 +134,11 @@ TextTermStepper_Write_Delta_IMP(TextTermStepper *self, OutStream *outstream,
     OutStream_Write_String(outstream, diff_start_str, diff_len);
 
     // Update value.
-    CB_Mimic_Utf8(charbuf, new_text, new_size);
+    CB_Mimic_Utf8(ivars->charbuf, new_text, new_size);
 
-    // Invalidate string.
-    DECREF(ivars->string);
-    ivars->string = NULL;
+    // Invalidate string value.
+    DECREF(ivars->value);
+    ivars->value = NULL;
 }
 
 void
@@ -153,12 +148,11 @@ TextTermStepper_Read_Key_Frame_IMP(TextTermStepper *self,
     const uint32_t text_len = InStream_Read_C32(instream);
 
     // Allocate space.
-    CharBuf *charbuf = (CharBuf*)ivars->value;
-    char    *ptr     = CB_Grow(charbuf, text_len);
+    char *ptr = CB_Grow(ivars->charbuf, text_len);
 
     // Set the value text.
     InStream_Read_Bytes(instream, ptr, text_len);
-    CB_Set_Size(charbuf, text_len);
+    CB_Set_Size(ivars->charbuf, text_len);
     if (!StrHelp_utf8_valid(ptr, text_len)) {
         THROW(ERR, "Invalid UTF-8 sequence in '%o' at byte %i64",
               InStream_Get_Filename(instream),
@@ -168,9 +162,9 @@ TextTermStepper_Read_Key_Frame_IMP(TextTermStepper *self,
     // Null-terminate.
     ptr[text_len] = '\0';
 
-    // Invalidate string.
-    DECREF(ivars->string);
-    ivars->string = NULL;
+    // Invalidate string value.
+    DECREF(ivars->value);
+    ivars->value = NULL;
 }
 
 void
@@ -181,12 +175,14 @@ TextTermStepper_Read_Delta_IMP(TextTermStepper *self, InStream *instream)
{
     const uint32_t total_text_len   = text_overlap + finish_chars_len;
 
     // Allocate space.
-    CharBuf *charbuf = (CharBuf*)ivars->value;
-    char    *ptr     = CB_Grow(charbuf, total_text_len);
+    if (ivars->value) {
+        CB_Mimic(ivars->charbuf, ivars->value);
+    }
+    char *ptr = CB_Grow(ivars->charbuf, total_text_len);
 
     // Set the value text.
     InStream_Read_Bytes(instream, ptr + text_overlap, finish_chars_len);
-    CB_Set_Size(charbuf, total_text_len);
+    CB_Set_Size(ivars->charbuf, total_text_len);
     if (!StrHelp_utf8_valid(ptr, total_text_len)) {
         THROW(ERR, "Invalid UTF-8 sequence in '%o' at byte %i64",
               InStream_Get_Filename(instream),
@@ -196,9 +192,9 @@ TextTermStepper_Read_Delta_IMP(TextTermStepper *self, InStream *instream)
{
     // Null-terminate.
     ptr[total_text_len] = '\0';
 
-    // Invalidate string.
-    DECREF(ivars->string);
-    ivars->string = NULL;
+    // Invalidate string value.
+    DECREF(ivars->value);
+    ivars->value = NULL;
 }
 
 

http://git-wip-us.apache.org/repos/asf/lucy/blob/d6fdddb0/core/Lucy/Plan/TextType.cfh
----------------------------------------------------------------------
diff --git a/core/Lucy/Plan/TextType.cfh b/core/Lucy/Plan/TextType.cfh
index b2bf014..934529c 100644
--- a/core/Lucy/Plan/TextType.cfh
+++ b/core/Lucy/Plan/TextType.cfh
@@ -27,7 +27,7 @@ class Lucy::Plan::TextType inherits Lucy::Plan::FieldType {
 class Lucy::Index::TermStepper::TextTermStepper
     inherits Lucy::Index::TermStepper {
 
-    String *string;
+    CharBuf *charbuf;
 
     inert incremented TextTermStepper*
     new();


Mime
View raw message