<commit_before>/* -*- c-basic-offset: 2 -*- */
/*
  Copyright(C) 2015 Brazil

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License version 2.1 as published by the Free Software Foundation.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include "ts_expr_builder.h"

#include <string.h>

#include "../grn_ctx.h"
#include "../grn_db.h"

#include "ts_log.h"
#include "ts_util.h"

/*-------------------------------------------------------------
 * grn_ts_expr_bridge.
 */

/* grn_ts_expr_bridge_init() initializes a bridge. */
static void
grn_ts_expr_bridge_init(grn_ctx *ctx, grn_ts_expr_bridge *bridge)
{
  memset(bridge, 0, sizeof(*bridge));
  bridge->src_table = NULL;
  bridge->dest_table = NULL;
}

/* grn_ts_expr_bridge_fin() finalizes a bridge. */
static void
grn_ts_expr_bridge_fin(grn_ctx *ctx, grn_ts_expr_bridge *bridge)
{
  if (bridge->dest_table) {
    grn_obj_unlink(ctx, bridge->dest_table);
  }
  /* Note: bridge->src_table does not increment a reference count. */
}

/*-------------------------------------------------------------
 * grn_ts_expr_builder.
 */

/* grn_ts_expr_builder_init() initializes an expression builder. */
static void
grn_ts_expr_builder_init(grn_ctx *ctx, grn_ts_expr_builder *builder)
{
  memset(builder, 0, sizeof(*builder));
  builder->table = NULL;
  builder->curr_table = NULL;
  builder->nodes = NULL;
  builder->bridges = NULL;
}

/* grn_ts_expr_builder_fin() finalizes an expression builder. */
static void
grn_ts_expr_builder_fin(grn_ctx *ctx, grn_ts_expr_builder *builder)
{
  size_t i;
  if (builder->bridges) {
    for (i = 0; i < builder->n_bridges; i++) {
      grn_ts_expr_bridge_fin(ctx, &builder->bridges[i]);
    }
    GRN_FREE(builder->bridges);
  }
  if (builder->nodes) {
    for (i = 0; i < builder->n_nodes; i++) {
      if (builder->nodes[i]) {
        grn_ts_expr_node_close(ctx, builder->nodes[i]);
      }
    }
    GRN_FREE(builder->nodes);
  }
  /* Note: builder->curr_table does not increment a reference count. */
  if (builder->table) {
    grn_obj_unlink(ctx, builder->table);
  }
}

grn_rc
grn_ts_expr_builder_open(grn_ctx *ctx, grn_obj *table,
                         grn_ts_expr_builder **builder)
{
  grn_rc rc;
  grn_ts_expr_builder *new_builder;
  if (!ctx) {
    return GRN_INVALID_ARGUMENT;
  }
  if (!table || !grn_ts_obj_is_table(ctx, table) || !builder) {
    GRN_TS_ERR_RETURN(GRN_INVALID_ARGUMENT, "invalid argument");
  }
  new_builder = GRN_MALLOCN(grn_ts_expr_builder, 1);
  if (!new_builder) {
    GRN_TS_ERR_RETURN(GRN_NO_MEMORY_AVAILABLE,
                      "GRN_MALLOCN failed: %" GRN_FMT_SIZE,
                      sizeof(grn_ts_expr_builder));
  }
  rc = grn_ts_obj_increment_ref_count(ctx, table);
  if (rc != GRN_SUCCESS) {
    GRN_FREE(new_builder);
    return rc;
  }
  grn_ts_expr_builder_init(ctx, new_builder);
  new_builder->table = table;
  new_builder->curr_table = table;
  *builder = new_builder;
  return GRN_SUCCESS;
}

grn_rc
grn_ts_expr_builder_close(grn_ctx *ctx, grn_ts_expr_builder *builder)
{
  if (!ctx) {
    return GRN_INVALID_ARGUMENT;
  }
  if (!builder) {
    GRN_TS_ERR_RETURN(GRN_INVALID_ARGUMENT, "invalid argument");
  }
  grn_ts_expr_builder_fin(ctx, builder);
  return GRN_SUCCESS;
}

/* grn_ts_expr_deref() dereferences a node. */
static grn_rc
grn_ts_expr_builder_deref(grn_ctx *ctx, grn_ts_expr_builder *builder,
                          grn_ts_expr_node **node_ptr)
{
  grn_ts_expr_node *node = *node_ptr;
  while (node->data_kind == GRN_TS_REF) {
    grn_rc rc;
    grn_ts_expr_node *key_node, *bridge_node;
    grn_id table_id = node->data_type;
    grn_obj *table = grn_ctx_at(ctx, table_id);
    if (!table) {
      return GRN_OBJECT_CORRUPT;
    }
    if (!grn_ts_obj_is_table(ctx, table)) {
      grn_obj_unlink(ctx, table);
      return GRN_OBJECT_CORRUPT;
    }
    rc = grn_ts_expr_key_node_open(ctx, table, &key_node);
    grn_obj_unlink(ctx, table);
    if (rc != GRN_SUCCESS) {
      return rc;
    }
    rc = grn_ts_expr_bridge_node_open(ctx, node, key_node, &bridge_node);<commit_msg>grn_ts: fix a memory leak<commit_after>/* -*- c-basic-offset: 2 -*- */
/*
  Copyright(C) 2015 Brazil

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License version 2.1 as published by the Free Software Foundation.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include "ts_expr_builder.h"

#include <string.h>

#include "../grn_ctx.h"
#include "../grn_db.h"

#include "ts_log.h"
#include "ts_util.h"

/*-------------------------------------------------------------
 * grn_ts_expr_bridge.
 */

/* grn_ts_expr_bridge_init() initializes a bridge. */
static void
grn_ts_expr_bridge_init(grn_ctx *ctx, grn_ts_expr_bridge *bridge)
{
  memset(bridge, 0, sizeof(*bridge));
  bridge->src_table = NULL;
  bridge->dest_table = NULL;
}

/* grn_ts_expr_bridge_fin() finalizes a bridge. */
static void
grn_ts_expr_bridge_fin(grn_ctx *ctx, grn_ts_expr_bridge *bridge)
{
  if (bridge->dest_table) {
    grn_obj_unlink(ctx, bridge->dest_table);
  }
  /* Note: bridge->src_table does not increment a reference count. */
}

/*-------------------------------------------------------------
 * grn_ts_expr_builder.
 */

/* grn_ts_expr_builder_init() initializes an expression builder. */
static void
grn_ts_expr_builder_init(grn_ctx *ctx, grn_ts_expr_builder *builder)
{
  memset(builder, 0, sizeof(*builder));
  builder->table = NULL;
  builder->curr_table = NULL;
  builder->nodes = NULL;
  builder->bridges = NULL;
}

/* grn_ts_expr_builder_fin() finalizes an expression builder. */
static void
grn_ts_expr_builder_fin(grn_ctx *ctx, grn_ts_expr_builder *builder)
{
  size_t i;
  if (builder->bridges) {
    for (i = 0; i < builder->n_bridges; i++) {
      grn_ts_expr_bridge_fin(ctx, &builder->bridges[i]);
    }
    GRN_FREE(builder->bridges);
  }
  if (builder->nodes) {
    for (i = 0; i < builder->n_nodes; i++) {
      if (builder->nodes[i]) {
        grn_ts_expr_node_close(ctx, builder->nodes[i]);
      }
    }
    GRN_FREE(builder->nodes);
  }
  /* Note: builder->curr_table does not increment a reference count. */
  if (builder->table) {
    grn_obj_unlink(ctx, builder->table);
  }
}

grn_rc
grn_ts_expr_builder_open(grn_ctx *ctx, grn_obj *table,
                         grn_ts_expr_builder **builder)
{
  grn_rc rc;
  grn_ts_expr_builder *new_builder;
  if (!ctx) {
    return GRN_INVALID_ARGUMENT;
  }
  if (!table || !grn_ts_obj_is_table(ctx, table) || !builder) {
    GRN_TS_ERR_RETURN(GRN_INVALID_ARGUMENT, "invalid argument");
  }
  new_builder = GRN_MALLOCN(grn_ts_expr_builder, 1);
  if (!new_builder) {
    GRN_TS_ERR_RETURN(GRN_NO_MEMORY_AVAILABLE,
                      "GRN_MALLOCN failed: %" GRN_FMT_SIZE,
                      sizeof(grn_ts_expr_builder));
  }
  rc = grn_ts_obj_increment_ref_count(ctx, table);
  if (rc != GRN_SUCCESS) {
    GRN_FREE(new_builder);
    return rc;
  }
  grn_ts_expr_builder_init(ctx, new_builder);
  new_builder->table = table;
  new_builder->curr_table = table;
  *builder = new_builder;
  return GRN_SUCCESS;
}

grn_rc
grn_ts_expr_builder_close(grn_ctx *ctx, grn_ts_expr_builder *builder)
{
  if (!ctx) {
    return GRN_INVALID_ARGUMENT;
  }
  if (!builder) {
    GRN_TS_ERR_RETURN(GRN_INVALID_ARGUMENT, "invalid argument");
  }
  grn_ts_expr_builder_fin(ctx, builder);
  GRN_FREE(builder);
  return GRN_SUCCESS;
}

/* grn_ts_expr_deref() dereferences a node. */
static grn_rc
grn_ts_expr_builder_deref(grn_ctx *ctx, grn_ts_expr_builder *builder,
                          grn_ts_expr_node **node_ptr)
{
  grn_ts_expr_node *node = *node_ptr;
  while (node->data_kind == GRN_TS_REF) {
    grn_rc rc;
    grn_ts_expr_node *key_node, *bridge_node;
    grn_id table_id = node->data_type;
    grn_obj *table = grn_ctx_at(ctx, table_id);
    if (!table) {
      return GRN_OBJECT_CORRUPT;
    }
    if (!grn_ts_obj_is_table(ctx, table)) {
      grn_obj_unlink(ctx, table);
      return GRN_OBJECT_CORRUPT;
    }
    rc = grn_ts_expr_key_node_open(ctx, table, &key_node);
    grn_obj_unlink(ctx, table);
    if (rc != GRN_SUCCESS) {
      return rc;
    }
    rc = grn_ts_expr_bridge_node_open(ctx, node, key_node, &bridge_node);
{'fraction_non_alphanumeric': 0.13606935147591354, 'fraction_numerical': 0.005047734006364534, 'mean_word_length': 3.319431279620853, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7357420', 'n_tokens_mistral': 3378, 'n_tokens_neox': 3319, 'n_words': 728}