<commit_before>/*
  Copyright 2014 DataStax

  Licensed 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.
*/

#include "dht_metadata.hpp"
#include "md5.hpp"
#include "murmur3.hpp"

#include <algorithm>
#include <string>

namespace cass {

static const COWHostVec EMPTY_REPLICA_COW_PTR(new HostVec());

static void parse_int64(const char* p, size_t n, int64_t* out) {
  int c;
  const unsigned char* s = (unsigned char*)p;
  for (; n != 0 && isspace(c = *s); ++s, --n) {}

  if (n == 0) {
    *out = 0;
    return;
  }

  int64_t sign = 1;
  if (c == '-') {
    sign = -1;
    ++s; --n;
  }

  int64_t value = 0;
  for (; n != 0  && isdigit(c = *s); ++s, --n) {
    value *= 10;
    value += c - '0';
  }
  *out = sign*value;
}

static void parse_int128(const char* p, size_t n, uint64_t output[]) {
  // no sign handling because C* uses [0, 2^127]
  int c;
  const unsigned char* s = (unsigned char*)p;
  for (; n != 0 && isspace(c = *s); ++s, --n) {}

  if (n == 0) {
    memset(output, 0, sizeof(uint64_t) * 2);
    return;
  }

  uint64_t hi = 0;
  uint64_t lo = 0;
  uint64_t hi_tmp;
  uint64_t lo_tmp;
  uint64_t lo_tmp2;
  for (; n != 0  && isdigit(c = *s); ++s, --n) {
    hi_tmp = hi;
    lo_tmp = lo;

    //value *= 10;
    lo = lo_tmp << 1;
    hi = (lo_tmp >> 63) + (hi_tmp << 1);
    lo_tmp2 = lo;
    lo += lo_tmp << 3;
    hi += (lo_tmp >> 61) + (hi_tmp << 3) + (lo < lo_tmp2 ? 1 : 0);

    //value += c - '0';
    lo_tmp = lo;
    lo += c - '0';
    hi += (lo < lo_tmp) ? 1 : 0;
  }
  encode_int64((char*)output, hi);
  encode_int64((char*)(output+1), lo);
}


void TokenMap::update_host(SharedRefPtr<Host>& host, const TokenStringList& token_strings) {
  // There's a chance to avoid purging if tokens are the same as existing; deemed
  // not worth the complexity because:
  // 1.) Updates should only happen on "new" host, or "moved"
  // 2.) Moving should only occur on non-vnode clusters, in which case the
  //     token map is relatively small and easy to purge/repopulate
  purge_address(host->address());

  for (TokenStringList::const_iterator i = token_strings.begin();
       i != token_strings.end(); ++i) {
    token_map_[token_from_string_ref(*i)] = host;
  }
  map_replicas();
}

void TokenMap::remove_host(SharedRefPtr<Host>& host) {
  if (purge_address(host->address())) {
    map_replicas();
  }
}

void TokenMap::update_keyspace(const std::string& ks_name, const KeyspaceMetadata& ks_meta) {
  ScopedPtr<ReplicaPlacementStrategy> rps_now(ReplicaPlacementStrategy::from_keyspace_meta(ks_meta));
  KeyspaceStrategyMap::iterator i = keyspace_strategy_map_.find(ks_name);
  if (i == keyspace_strategy_map_.end() ||
      !i->second->equals(*rps_now)) {
    map_keyspace_replicas(ks_name, *rps_now);
    if (i == keyspace_strategy_map_.end()) {
      keyspace_strategy_map_[ks_name].reset(rps_now.release());
    } else {
      i->second.reset(rps_now.release());
    }
  }
}

void TokenMap::drop_keyspace(const std::string& ks_name) {
  keyspace_replica_map_.erase(ks_name);
  keyspace_strategy_map_.erase(ks_name);
}
<commit_msg>Add address to map when token map updated<commit_after>/*
  Copyright 2014 DataStax

  Licensed 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.
*/

#include "dht_metadata.hpp"
#include "md5.hpp"
#include "murmur3.hpp"

#include <algorithm>
#include <string>

namespace cass {

static const COWHostVec EMPTY_REPLICA_COW_PTR(new HostVec());

static void parse_int64(const char* p, size_t n, int64_t* out) {
  int c;
  const unsigned char* s = (unsigned char*)p;
  for (; n != 0 && isspace(c = *s); ++s, --n) {}

  if (n == 0) {
    *out = 0;
    return;
  }

  int64_t sign = 1;
  if (c == '-') {
    sign = -1;
    ++s; --n;
  }

  int64_t value = 0;
  for (; n != 0  && isdigit(c = *s); ++s, --n) {
    value *= 10;
    value += c - '0';
  }
  *out = sign*value;
}

static void parse_int128(const char* p, size_t n, uint64_t output[]) {
  // no sign handling because C* uses [0, 2^127]
  int c;
  const unsigned char* s = (unsigned char*)p;
  for (; n != 0 && isspace(c = *s); ++s, --n) {}

  if (n == 0) {
    memset(output, 0, sizeof(uint64_t) * 2);
    return;
  }

  uint64_t hi = 0;
  uint64_t lo = 0;
  uint64_t hi_tmp;
  uint64_t lo_tmp;
  uint64_t lo_tmp2;
  for (; n != 0  && isdigit(c = *s); ++s, --n) {
    hi_tmp = hi;
    lo_tmp = lo;

    //value *= 10;
    lo = lo_tmp << 1;
    hi = (lo_tmp >> 63) + (hi_tmp << 1);
    lo_tmp2 = lo;
    lo += lo_tmp << 3;
    hi += (lo_tmp >> 61) + (hi_tmp << 3) + (lo < lo_tmp2 ? 1 : 0);

    //value += c - '0';
    lo_tmp = lo;
    lo += c - '0';
    hi += (lo < lo_tmp) ? 1 : 0;
  }
  encode_int64((char*)output, hi);
  encode_int64((char*)(output+1), lo);
}


void TokenMap::update_host(SharedRefPtr<Host>& host, const TokenStringList& token_strings) {
  // There's a chance to avoid purging if tokens are the same as existing; deemed
  // not worth the complexity because:
  // 1.) Updates should only happen on "new" host, or "moved"
  // 2.) Moving should only occur on non-vnode clusters, in which case the
  //     token map is relatively small and easy to purge/repopulate
  purge_address(host->address());

  for (TokenStringList::const_iterator i = token_strings.begin();
       i != token_strings.end(); ++i) {
    token_map_[token_from_string_ref(*i)] = host;
  }
  mapped_addresses_.insert(host->address());
  map_replicas();
}

void TokenMap::remove_host(SharedRefPtr<Host>& host) {
  if (purge_address(host->address())) {
    map_replicas();
  }
}

void TokenMap::update_keyspace(const std::string& ks_name, const KeyspaceMetadata& ks_meta) {
  ScopedPtr<ReplicaPlacementStrategy> rps_now(ReplicaPlacementStrategy::from_keyspace_meta(ks_meta));
  KeyspaceStrategyMap::iterator i = keyspace_strategy_map_.find(ks_name);
  if (i == keyspace_strategy_map_.end() ||
      !i->second->equals(*rps_now)) {
    map_keyspace_replicas(ks_name, *rps_now);
    if (i == keyspace_strategy_map_.end()) {
      keyspace_strategy_map_[ks_name].reset(rps_now.release());
    } else {
      i->second.reset(rps_now.release());
    }
  }
}

void TokenMap::drop_keyspace(const std::string& ks_name) {
  keyspace_replica_map_.erase(ks_name);
  keyspace_strategy_map_.erase(ks_name);
}

{'fraction_non_alphanumeric': 0.14571388616976647, 'fraction_numerical': 0.023213536568312123, 'mean_word_length': 3.4012307692307693, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7446598', 'n_tokens_mistral': 2759, 'n_tokens_neox': 2613, 'n_words': 732}