The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
/* Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

parcel Lucy;

__C__

#define lucy_FType_TEXT    1
#define lucy_FType_BLOB    2
#define lucy_FType_INT32   3
#define lucy_FType_INT64   4
#define lucy_FType_FLOAT32 5
#define lucy_FType_FLOAT64 6
#define lucy_FType_PRIMITIVE_ID_MASK 0x7

#ifdef LUCY_USE_SHORT_NAMES
  #define FType_TEXT              lucy_FType_TEXT
  #define FType_BLOB              lucy_FType_BLOB
  #define FType_INT32             lucy_FType_INT32
  #define FType_INT64             lucy_FType_INT64
  #define FType_FLOAT32           lucy_FType_FLOAT32
  #define FType_FLOAT64           lucy_FType_FLOAT64
  #define FType_PRIMITIVE_ID_MASK lucy_FType_PRIMITIVE_ID_MASK
#endif

__END_C__

/** Define a field's behavior.
 *
 * FieldType is an abstract class defining a set of traits and behaviors which
 * may be associated with one or more field names.
 *
 * Properties which are common to all field types include `boost`,
 * `indexed`, `stored`, `sortable`,
 * `binary`, and `similarity`.
 *
 * The `boost` property is a floating point scoring multiplier
 * which defaults to 1.0.  Values greater than 1.0 cause the field to
 * contribute more to a document's score, lower values, less.
 *
 * The `indexed` property indicates whether the field should be
 * indexed (so that it can be searched).
 *
 * The `stored` property indicates whether to store the raw field
 * value, so that it can be retrieved when a document turns up in a search.
 *
 * The `sortable` property indicates whether search results should
 * be sortable based on the contents of the field.
 *
 * The `binary` property indicates whether the field contains
 * binary or text data.  Unlike most other properties, `binary` is
 * not settable.
 *
 * The `similarity` property is a
 * [](cfish:Similarity) object which defines matching
 * and scoring behavior for the field.  It is required if the field is
 * `indexed`.
 */
public abstract class Lucy::Plan::FieldType nickname FType
    inherits Clownfish::Obj {

    float         boost;
    bool          indexed;
    bool          stored;
    bool          sortable;

    inert FieldType*
    init(FieldType *self);

    inert FieldType*
    init2(FieldType *self, float boost = 1.0, bool indexed = false,
          bool stored = false, bool sortable = false);

    /** Setter for `boost`.
     */
    public void
    Set_Boost(FieldType *self, float boost);

    /** Accessor for `boost`.
     */
    public float
    Get_Boost(FieldType *self);

    /** Setter for `indexed`.
     */
    public void
    Set_Indexed(FieldType *self, bool indexed);

    /** Accessor for `indexed`.
     */
    public bool
    Indexed(FieldType *self);

    /** Setter for `stored`.
     */
    public void
    Set_Stored(FieldType *self, bool stored);

    /** Accessor for `stored`.
     */
    public bool
    Stored(FieldType *self);

    /** Setter for `sortable`.
     */
    public void
    Set_Sortable(FieldType *self, bool sortable);

    /** Accessor for `sortable`.
     */
    public bool
    Sortable(FieldType *self);

    /** Indicate whether the field contains binary data.
     */
    public bool
    Binary(FieldType *self);

    /** Compare two values for the field.  The default implementation
     * dispatches to the [](cfish:.Compare_To) method of argument `a`.
     *
     * @return a negative number if a is "less than" b, 0 if they are "equal",
     * and a positive number if a is "greater than" b.
     */
    int32_t
    Compare_Values(FieldType *self, Obj *a, Obj *b);

    /** NULL-safe comparison wrapper which sorts NULLs towards the back.
     */
    inert inline int32_t
    null_back_compare_values(FieldType *self, Obj *a, Obj *b);

    /** Produce a Stepper suitable for use by a Lexicon.
     */
    abstract incremented TermStepper*
    Make_Term_Stepper(FieldType *self);

    /** Internal id used for switch() ops.  Unique for each primitive type.
     */
    abstract int8_t
    Primitive_ID(FieldType *self);

    /** Produce a special mimimal dump which does not include Similarity or
     * Analyzer dumps.  For exclusive internal use by Schema.
     */
    abstract incremented Hash*
    Dump_For_Schema(FieldType *self);

    abstract incremented Obj*
    Dump(FieldType *self);

    abstract incremented Obj*
    Load(FieldType *self, Obj *dump);

    /** Compares all common properties.
     */
    public bool
    Equals(FieldType *self, Obj *other);
}

__C__

static CFISH_INLINE int32_t
lucy_FType_null_back_compare_values(lucy_FieldType *self,
                                    cfish_Obj *a, cfish_Obj *b) {
    if (a == NULL) {
        if (b == NULL) { return 0; }
        else { return 1; }
    }
    else if (b == NULL) {
        return -1;
    }
    else {
        return LUCY_FType_Compare_Values(self, a, b);
    }
}

__END_C__