<commit_before>#include <arpc.h>
#include <crypt.h>

#include <location.h>
#include <misc_utils.h>
#include "libadb.h"
#include <adb_prot.h>

static chord_node
make_chord_node (const adb_vnodeid &n)
{
  chord_node_wire x;
  bzero (&x, sizeof (chord_node_wire));
  // XXX Will it be a problem to have zero coordinates and
  //     zero accordion data?
  x.machine_order_ipv4_addr  = n.machine_order_ipv4_addr;
  x.machine_order_port_vnnum = n.machine_order_port_vnnum;
  return make_chord_node (x);
}

adb::adb (str sock_name, str name, bool hasaux) :
  name_space (name),
  hasaux (hasaux),
  next_batch (NULL)
{
  int fd = unixsocket_connect (sock_name);
  if (fd < 0) {
    fatal ("adb_connect: Error connecting to %s: %s\n",
	   sock_name.cstr (), strerror (errno));
  }
  make_async (fd);
  c = aclnt::alloc (axprt_unix::alloc (fd, 1024*1025),
		    adb_program_1);

  adb_initspacearg arg;
  arg.name = name_space;
  arg.hasaux = hasaux;

  adb_status *res = New adb_status ();

  c->call (ADBPROC_INITSPACE, &arg, res,
	   wrap (this, &adb::initspace_cb, res));
}

void
adb::initspace_cb (adb_status *astat, clnt_stat stat)
{
  if (stat)
    fatal << "adb_initspace_cb: RPC error for " << name_space << ": " << stat << "\n";
  else if (*astat)
    fatal << "adb_initspace_cb: adbd error for " << name_space << ": " << *astat << "\n";
  delete astat;
}

void
adb::store (chordID key, str data, u_int32_t auxdata, cb_adbstat cb)
{
  assert (hasaux);
  adb_storearg arg;
  arg.key = key;
  arg.name = name_space;
  arg.data = data;
  arg.auxdata = auxdata;

  adb_status *res = New adb_status ();

  c->call (ADBPROC_STORE, &arg, res,
	   wrap (this, &adb::generic_cb, res, cb));
  return;
}

void
adb::store (chordID key, str data, cb_adbstat cb)
{
  assert (!hasaux);
  adb_storearg arg;
  arg.key = key;
  arg.name = name_space;
  arg.data = data;

  adb_status *res = New adb_status ();

  c->call (ADBPROC_STORE, &arg, res,
	   wrap (this, &adb::generic_cb, res, cb));
  return;
}

void
adb::generic_cb (adb_status *res, cb_adbstat cb, clnt_stat err)
{
  if (cb) 
    if (err || !res)
      cb (ADB_ERR);
    else 
      cb (*res);
  delete res;
}

void
adb::fetch (chordID key, cb_fetch cb)
{
  adb_fetcharg arg;
  arg.key = key;
  arg.name = name_space;

  adb_fetchres *res = New adb_fetchres (ADB_OK);
  c->call (ADBPROC_FETCH, &arg, res,
	   wrap (this, &adb::fetch_cb, res, key, cb));
}

void
adb::fetch_cb (adb_fetchres *res, chordID key, cb_fetch cb, clnt_stat err)
{
  if (err || (res && res->status)) {
    str nodata = "";
    cb ((err ? ADB_ERR : res->status) , key, nodata);
  } else {
    assert (key == res->resok->key);
    str data (res->resok->data.base (), res->resok->data.size ());
    cb (ADB_OK, res->resok->key, data);
  }
  delete res;
  return;
}

void
adb::getkeys (u_int32_t id, cb_getkeys cb, bool ordered, u_int32_t batchsize, bool getaux)
{
  adb_getkeysarg arg;
  arg.name = name_space;
  arg.ordered = ordered;
  arg.batchsize = batchsize;
  arg.getaux = getaux;

  if (id == 0) {
    arg.continuation = 0;
  } else {
    chordID *x = getkeystab[id];
    if (x) {
      arg.continuation = *x;
      getkeystab.remove (id);
    }
    else {
      delaycb (0, wrap (this, &adb::getkeys_cb, getaux, (adb_getkeysres *) NULL, cb, RPC_FAILED));
    }
  }

  adb_getkeysres *res = New adb_getkeysres (ADB_OK);
  c->call (ADBPROC_GETKEYS, &arg, res,
	   wrap (this, &adb::getkeys_cb, getaux, res, cb));
}

void
adb::getkeys_cb (bool getaux, adb_getkeysres *res, cb_getkeys cb, clnt_stat err)
{
  u_int32_t id (0);
  vec<adb_keyaux_t> keys;
  if (err || (res && res->status == ADB_ERR)) {
    cb (ADB_ERR, id, keys);
  } else {
    for (unsigned int i = 0; i < res->resok->keyaux.size (); i++) {
      adb_keyaux_t ka;
      ka.key = res->resok->keyaux[i].key;
      ka.auxdata = res->resok->keyaux[i].auxdata;
      keys.push_back (ka);
    }
    id = random_getword ();
    getkeystab.insert (id, res->resok->continuation);

    adb_status ret = (res->resok->complete) ? ADB_COMPLETE : ADB_OK;<commit_msg>libadb: avoid invoking the default copy-constructor for getkeys.<commit_after>#include <arpc.h>
#include <crypt.h>

#include <location.h>
#include <misc_utils.h>
#include "libadb.h"
#include <adb_prot.h>

static chord_node
make_chord_node (const adb_vnodeid &n)
{
  chord_node_wire x;
  bzero (&x, sizeof (chord_node_wire));
  // XXX Will it be a problem to have zero coordinates and
  //     zero accordion data?
  x.machine_order_ipv4_addr  = n.machine_order_ipv4_addr;
  x.machine_order_port_vnnum = n.machine_order_port_vnnum;
  return make_chord_node (x);
}

adb::adb (str sock_name, str name, bool hasaux) :
  name_space (name),
  hasaux (hasaux),
  next_batch (NULL)
{
  int fd = unixsocket_connect (sock_name);
  if (fd < 0) {
    fatal ("adb_connect: Error connecting to %s: %s\n",
	   sock_name.cstr (), strerror (errno));
  }
  make_async (fd);
  c = aclnt::alloc (axprt_unix::alloc (fd, 1024*1025),
		    adb_program_1);

  adb_initspacearg arg;
  arg.name = name_space;
  arg.hasaux = hasaux;

  adb_status *res = New adb_status ();

  c->call (ADBPROC_INITSPACE, &arg, res,
	   wrap (this, &adb::initspace_cb, res));
}

void
adb::initspace_cb (adb_status *astat, clnt_stat stat)
{
  if (stat)
    fatal << "adb_initspace_cb: RPC error for " << name_space << ": " << stat << "\n";
  else if (*astat)
    fatal << "adb_initspace_cb: adbd error for " << name_space << ": " << *astat << "\n";
  delete astat;
}

void
adb::store (chordID key, str data, u_int32_t auxdata, cb_adbstat cb)
{
  assert (hasaux);
  adb_storearg arg;
  arg.key = key;
  arg.name = name_space;
  arg.data = data;
  arg.auxdata = auxdata;

  adb_status *res = New adb_status ();

  c->call (ADBPROC_STORE, &arg, res,
	   wrap (this, &adb::generic_cb, res, cb));
  return;
}

void
adb::store (chordID key, str data, cb_adbstat cb)
{
  assert (!hasaux);
  adb_storearg arg;
  arg.key = key;
  arg.name = name_space;
  arg.data = data;

  adb_status *res = New adb_status ();

  c->call (ADBPROC_STORE, &arg, res,
	   wrap (this, &adb::generic_cb, res, cb));
  return;
}

void
adb::generic_cb (adb_status *res, cb_adbstat cb, clnt_stat err)
{
  if (cb) 
    if (err || !res)
      cb (ADB_ERR);
    else 
      cb (*res);
  delete res;
}

void
adb::fetch (chordID key, cb_fetch cb)
{
  adb_fetcharg arg;
  arg.key = key;
  arg.name = name_space;

  adb_fetchres *res = New adb_fetchres (ADB_OK);
  c->call (ADBPROC_FETCH, &arg, res,
	   wrap (this, &adb::fetch_cb, res, key, cb));
}

void
adb::fetch_cb (adb_fetchres *res, chordID key, cb_fetch cb, clnt_stat err)
{
  if (err || (res && res->status)) {
    str nodata = "";
    cb ((err ? ADB_ERR : res->status) , key, nodata);
  } else {
    assert (key == res->resok->key);
    str data (res->resok->data.base (), res->resok->data.size ());
    cb (ADB_OK, res->resok->key, data);
  }
  delete res;
  return;
}

void
adb::getkeys (u_int32_t id, cb_getkeys cb, bool ordered, u_int32_t batchsize, bool getaux)
{
  adb_getkeysarg arg;
  arg.name = name_space;
  arg.ordered = ordered;
  arg.batchsize = batchsize;
  arg.getaux = getaux;

  if (id == 0) {
    arg.continuation = 0;
  } else {
    chordID *x = getkeystab[id];
    if (x) {
      arg.continuation = *x;
      getkeystab.remove (id);
    }
    else {
      delaycb (0, wrap (this, &adb::getkeys_cb, getaux, (adb_getkeysres *) NULL, cb, RPC_FAILED));
    }
  }

  adb_getkeysres *res = New adb_getkeysres (ADB_OK);
  c->call (ADBPROC_GETKEYS, &arg, res,
	   wrap (this, &adb::getkeys_cb, getaux, res, cb));
}

void
adb::getkeys_cb (bool getaux, adb_getkeysres *res, cb_getkeys cb, clnt_stat err)
{
  u_int32_t id (0);
  vec<adb_keyaux_t> keys;
  if (err || (res && res->status == ADB_ERR)) {
    cb (ADB_ERR, id, keys);
  } else {
    for (unsigned int i = 0; i < res->resok->keyaux.size (); i++) {
      keys.push_back ();
      keys.back().key = res->resok->keyaux[i].key;
      keys.back().auxdata = res->resok->keyaux[i].auxdata;
    }
    id = random_getword ();
    getkeystab.insert (id, res->resok->continuation);

    adb_status ret = (res->resok->complete) ? ADB_COMPLETE : ADB_OK;
{'fraction_non_alphanumeric': 0.154149224328983, 'fraction_numerical': 0.006156119182467373, 'mean_word_length': 3.433951965065502, 'pattern_counts': {'":': 4, '<': 59, '<?xml version=': 0, '>': 67, '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': '6133715', 'n_tokens_mistral': 3442, 'n_tokens_neox': 3365, 'n_words': 795}