<commit_before>/* 
    This file is part of tgl-library

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    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

    Copyright <NAME> 2013-2015
*/
#include "tgl.h"
#include "updates.h"
#include "mtproto-common.h"
#include "tgl-binlog.h"
#include "auto.h"
#include "auto/auto-types.h"
#include "auto/auto-skip.h"
#include "auto/auto-fetch-ds.h"
#include "auto/auto-free-ds.h"
#include "tgl-structures.h"
#include "tree.h"

#include <assert.h>

static void fetch_dc_option_new (struct tgl_state *TLS, struct tl_ds_dc_option *DS_DO) {
  vlogprintf (E_DEBUG, "id = %d, name = %.*s ip = %.*s port = %d\n", DS_LVAL (DS_DO->id), DS_RSTR (DS_DO->hostname), DS_RSTR (DS_DO->ip_address), DS_LVAL (DS_DO->port));

  bl_do_dc_option (TLS, DS_LVAL (DS_DO->id), DS_STR (DS_DO->hostname), DS_STR (DS_DO->ip_address), DS_LVAL (DS_DO->port));
}

int tgl_check_pts_diff (struct tgl_state *TLS, int pts, int pts_count) {
  vlogprintf (E_ERROR, "pts = %d, pts_count = %d\n", pts, pts_count);
  assert (TLS->pts);
  if (pts < TLS->pts + pts_count) {
    vlogprintf (E_NOTICE, "Duplicate message with pts=%d\n", pts);
    return -1;
  }
  if (pts > TLS->pts + pts_count) {
    vlogprintf (E_NOTICE, "Hole in pts (pts = %d, count = %d, cur_pts = %d)\n", pts, pts_count, TLS->pts);
    tgl_do_get_difference (TLS, 0, 0, 0);
    return -1;
  }
  if (TLS->locks & TGL_LOCK_DIFF) {
    vlogprintf (E_DEBUG, "Update during get_difference. pts = %d\n", pts);
    return -1;
  }
  vlogprintf (E_DEBUG, "Ok update. pts = %d\n", pts);
  return 1;
}

int tgl_check_qts_diff (struct tgl_state *TLS, int qts, int qts_count) {
  vlogprintf (E_ERROR, "qts = %d, qts_count = %d\n", qts, qts_count);
  assert (TLS->qts);
  if (qts < TLS->qts + qts_count) {
    vlogprintf (E_NOTICE, "Duplicate message with qts=%d\n", qts);
    return -1;
  }
  if (qts > TLS->qts + qts_count) {
    vlogprintf (E_NOTICE, "Hole in qts (qts = %d, count = %d, cur_qts = %d)\n", qts, qts_count, TLS->qts);
    tgl_do_get_difference (TLS, 0, 0, 0);
    return -1;
  }
  if (TLS->locks & TGL_LOCK_DIFF) {
    vlogprintf (E_DEBUG, "Update during get_difference. qts = %d\n", qts);
    return -1;
  }
  vlogprintf (E_DEBUG, "Ok update. qts = %d\n", qts);
  return 1;
}
  
static int do_skip_seq (struct tgl_state *TLS, int seq) {
  if (!seq) {
    vlogprintf (E_DEBUG, "Ok update. seq = %d\n", seq);
    return 0;
  }
  if (TLS->seq) {
    if (seq <= TLS->seq) {
      vlogprintf (E_NOTICE, "Duplicate message with seq=%d\n", seq);
      return -1;
    }
    if (seq > TLS->seq + 1) {
      vlogprintf (E_NOTICE, "Hole in seq (seq = %d, cur_seq = %d)\n", seq, TLS->seq);
      //vlogprintf (E_NOTICE, "lock_diff = %s\n", (TLS->locks & TGL_LOCK_DIFF) ? "true" : "false");
      tgl_do_get_difference (TLS, 0, 0, 0);
      return -1;
    }
    if (TLS->locks & TGL_LOCK_DIFF) {
      vlogprintf (E_DEBUG, "Update during get_difference. seq = %d\n", seq);
      return -1;
    }
    vlogprintf (E_DEBUG, "Ok update. seq = %d\n", seq);
    return 0;
  } else {
    return -1;
  }
}

void tglu_work_update_new (struct tgl_state *TLS, int check_only, struct tl_ds_update *DS_U) {
  if (TLS->locks & TGL_LOCK_DIFF) {
    vlogprintf (E_DEBUG, "Update during get_difference. DROP\n");
    return;
  }

  if (DS_U->pts) {
    assert (DS_U->pts_count);

    if (!check_only && tgl_check_pts_diff (TLS, DS_LVAL (DS_U->pts), DS_LVAL (DS_U->pts_count)) <= 0) {
      return;
    }
  }
  
  if (DS_U->pts) {
    if (!check_only && tgl_check_qts_diff (TLS, DS_LVAL (DS_U->qts), 1) <= 0) {
      return;
    }
  }

  if (check_only > 0 && DS_U->magic != CODE_update_message_i_d) { return; }
  switch (DS_U->magic) {
  case CODE_update_new_message:
    {
      struct tgl_message *M = tglf_fetch_alloc_message_new (TLS, DS_U->message);<commit_msg>fixed qts<commit_after>/* 
    This file is part of tgl-library

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    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

    Copyright <NAME> 2013-2015
*/
#include "tgl.h"
#include "updates.h"
#include "mtproto-common.h"
#include "tgl-binlog.h"
#include "auto.h"
#include "auto/auto-types.h"
#include "auto/auto-skip.h"
#include "auto/auto-fetch-ds.h"
#include "auto/auto-free-ds.h"
#include "tgl-structures.h"
#include "tree.h"

#include <assert.h>

static void fetch_dc_option_new (struct tgl_state *TLS, struct tl_ds_dc_option *DS_DO) {
  vlogprintf (E_DEBUG, "id = %d, name = %.*s ip = %.*s port = %d\n", DS_LVAL (DS_DO->id), DS_RSTR (DS_DO->hostname), DS_RSTR (DS_DO->ip_address), DS_LVAL (DS_DO->port));

  bl_do_dc_option (TLS, DS_LVAL (DS_DO->id), DS_STR (DS_DO->hostname), DS_STR (DS_DO->ip_address), DS_LVAL (DS_DO->port));
}

int tgl_check_pts_diff (struct tgl_state *TLS, int pts, int pts_count) {
  vlogprintf (E_ERROR, "pts = %d, pts_count = %d\n", pts, pts_count);
  assert (TLS->pts);
  if (pts < TLS->pts + pts_count) {
    vlogprintf (E_NOTICE, "Duplicate message with pts=%d\n", pts);
    return -1;
  }
  if (pts > TLS->pts + pts_count) {
    vlogprintf (E_NOTICE, "Hole in pts (pts = %d, count = %d, cur_pts = %d)\n", pts, pts_count, TLS->pts);
    tgl_do_get_difference (TLS, 0, 0, 0);
    return -1;
  }
  if (TLS->locks & TGL_LOCK_DIFF) {
    vlogprintf (E_DEBUG, "Update during get_difference. pts = %d\n", pts);
    return -1;
  }
  vlogprintf (E_DEBUG, "Ok update. pts = %d\n", pts);
  return 1;
}

int tgl_check_qts_diff (struct tgl_state *TLS, int qts, int qts_count) {
  vlogprintf (E_ERROR, "qts = %d, qts_count = %d\n", qts, qts_count);
  if (qts < TLS->qts + qts_count) {
    vlogprintf (E_NOTICE, "Duplicate message with qts=%d\n", qts);
    return -1;
  }
  if (qts > TLS->qts + qts_count) {
    vlogprintf (E_NOTICE, "Hole in qts (qts = %d, count = %d, cur_qts = %d)\n", qts, qts_count, TLS->qts);
    tgl_do_get_difference (TLS, 0, 0, 0);
    return -1;
  }
  if (TLS->locks & TGL_LOCK_DIFF) {
    vlogprintf (E_DEBUG, "Update during get_difference. qts = %d\n", qts);
    return -1;
  }
  vlogprintf (E_DEBUG, "Ok update. qts = %d\n", qts);
  return 1;
}
  
static int do_skip_seq (struct tgl_state *TLS, int seq) {
  if (!seq) {
    vlogprintf (E_DEBUG, "Ok update. seq = %d\n", seq);
    return 0;
  }
  if (TLS->seq) {
    if (seq <= TLS->seq) {
      vlogprintf (E_NOTICE, "Duplicate message with seq=%d\n", seq);
      return -1;
    }
    if (seq > TLS->seq + 1) {
      vlogprintf (E_NOTICE, "Hole in seq (seq = %d, cur_seq = %d)\n", seq, TLS->seq);
      //vlogprintf (E_NOTICE, "lock_diff = %s\n", (TLS->locks & TGL_LOCK_DIFF) ? "true" : "false");
      tgl_do_get_difference (TLS, 0, 0, 0);
      return -1;
    }
    if (TLS->locks & TGL_LOCK_DIFF) {
      vlogprintf (E_DEBUG, "Update during get_difference. seq = %d\n", seq);
      return -1;
    }
    vlogprintf (E_DEBUG, "Ok update. seq = %d\n", seq);
    return 0;
  } else {
    return -1;
  }
}

void tglu_work_update_new (struct tgl_state *TLS, int check_only, struct tl_ds_update *DS_U) {
  if (TLS->locks & TGL_LOCK_DIFF) {
    vlogprintf (E_DEBUG, "Update during get_difference. DROP\n");
    return;
  }

  if (DS_U->pts) {
    assert (DS_U->pts_count);

    if (!check_only && tgl_check_pts_diff (TLS, DS_LVAL (DS_U->pts), DS_LVAL (DS_U->pts_count)) <= 0) {
      return;
    }
  }
  
  if (DS_U->qts) {
    if (!check_only && tgl_check_qts_diff (TLS, DS_LVAL (DS_U->qts), 1) <= 0) {
      return;
    }
  }

  if (check_only > 0 && DS_U->magic != CODE_update_message_i_d) { return; }
  switch (DS_U->magic) {
  case CODE_update_new_message:
    {
      struct tgl_message *M = tglf_fetch_alloc_message_new (TLS, DS_U->message);
{'fraction_non_alphanumeric': 0.1442707747666704, 'fraction_numerical': 0.011019903294726189, 'mean_word_length': 3.257539492580182, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 82, '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': '4494960', 'n_tokens_mistral': 3643, 'n_tokens_neox': 3514, 'n_words': 986}