Changeset 70e5d7


Ignore:
Timestamp:
01/06/08 21:55:16 (4 years ago)
Author:
Tomash Brechko <tomash.brechko@…>
Branches:
master, ketama-compat
Children:
4c792d
Parents:
3a7104
git-author:
Tomash Brechko <tomash.brechko@…> (01/06/08 18:37:10)
git-committer:
Tomash Brechko <tomash.brechko@…> (01/06/08 21:55:16)
Message:

Add struct array.

Files:
2 added
5 edited

Legend:

Unmodified
Added
Removed
  • MANIFEST

    r381e4f r70e5d7  
    2424src/genparser.pl 
    2525src/reply.kw 
     26src/array.h 
     27src/array.c 
    2628src/client.h 
    2729src/client.c 
  • TODO

    r3a7104 r70e5d7  
    2121- UDP (?). 
    2222 
     23- Investigate why there are read() errors in no_eagain branch. 
     24 
     25- In find_bin() replace binary search with interpolation search. 
     26 
     27- Add test for noreply, do not list it in MANIFEST. 
     28 
    2329 
    2430For 0.07: 
     
    2935  instance. 
    3036 
    31 - Add test for noreply, do not list it in MANIFEST. 
    32  
    3337- Clean warnings for gcc 4.2.2. 
    3438 
    3539- Fix all ugliness introduced in 0.06 ;). 
    36  
    37 - Investigate why there are read() errors in no_eagain branch. 
    3840 
    3941- Replace av_push() with av_store(), test result. 
     
    4547- Rename bins to buckets. 
    4648 
    47 - In find_bin() replace binary search with interpolation search. 
    48  
    4949- Improve server_versions command. 
  • src/client.c

    r3a7104 r70e5d7  
    2323 
    2424#include "client.h" 
     25#include "array.h" 
    2526#include "connect.h" 
    2627#include "parse_keyword.h" 
     
    284285struct client 
    285286{ 
    286   struct server *servers; 
    287   int server_count; 
    288   int server_capacity; 
     287  struct array servers; 
    289288 
    290289  struct dispatch_state dispatch; 
     
    301300  int nowait; 
    302301 
    303   struct key_node *key_list; 
    304   int key_list_count; 
    305   int key_list_capacity; 
     302  struct array key_list; 
    306303 
    307304  generation_type generation; 
     
    357354get_key_index(struct command_state *state) 
    358355{ 
    359   return state->client->key_list[state->key_head].key; 
     356  struct key_node *node = 
     357    array_elem(state->client->key_list, struct key_node, state->key_head); 
     358  return node->key; 
    360359} 
    361360 
     
    365364next_key_index(struct command_state *state) 
    366365{ 
    367   state->key_head = state->client->key_list[state->key_head].next; 
     366  struct key_node *node = 
     367    array_elem(state->client->key_list, struct key_node, state->key_head); 
     368  state->key_head = node->next; 
    368369} 
    369370 
     
    376377    return NULL; 
    377378 
    378   c->servers = NULL; 
    379   c->server_count = c->server_capacity = 0; 
     379  array_init(&c->servers); 
     380  array_init(&c->key_list); 
    380381 
    381382  dispatch_init(&c->dispatch); 
     
    392393  c->nowait = 0; 
    393394 
    394   c->key_list = NULL; 
    395   c->key_list_count = c->key_list_capacity = 0; 
    396  
    397395  c->generation = 1;            /* Different from initial command state.  */ 
    398396 
     
    404402client_destroy(struct client *c) 
    405403{ 
     404  struct server *s; 
    406405  int i; 
    407406 
    408407  client_nowait_push(c); 
    409408 
    410   for (i = 0; i < c->server_count; ++i) 
    411     server_destroy(&c->servers[i]); 
     409  for (s = array_beg(c->servers, struct server); 
     410       s != array_end(c->servers, struct server); 
     411       ++s) 
     412    server_destroy(s); 
    412413 
    413414  dispatch_destroy(&c->dispatch); 
    414415 
    415   free(c->servers); 
     416  array_destroy(&c->servers); 
     417  array_destroy(&c->key_list); 
    416418  free(c->prefix); 
    417   free(c->key_list); 
    418419  free(c); 
    419420} 
     
    424425{ 
    425426  /* Should be called before we added any server.  */ 
    426   if (c->server_count > 0 || ketama_points < 0) 
     427  if (! array_empty(c->servers) || ketama_points < 0) 
    427428    return MEMCACHED_FAILURE; 
    428429 
     
    485486    return MEMCACHED_FAILURE; 
    486487 
    487   if (c->server_count == c->server_capacity) 
    488     { 
    489       int capacity = (c->server_capacity > 0 ? c->server_capacity + 1 : 1); 
    490       struct server *s = 
    491         (struct server *) realloc(c->servers, 
    492                                   capacity * sizeof(struct server)); 
    493       if (! s) 
    494         return MEMCACHED_FAILURE; 
    495  
    496       c->servers = s; 
    497       c->server_capacity = capacity; 
    498     } 
    499  
    500   res = server_init(&c->servers[c->server_count], c, 
     488  if (array_extend(c->servers, struct server, 1, ARRAY_EXTEND_EXACT) == -1) 
     489    return MEMCACHED_FAILURE; 
     490 
     491  res = server_init(array_end(c->servers, struct server), c, 
    501492                    host, host_len, port, port_len, noreply); 
    502493  if (res != MEMCACHED_SUCCESS) 
     
    504495 
    505496  res = dispatch_add_server(&c->dispatch, host, host_len, port, port_len, 
    506                             weight, c->server_count); 
     497                            weight, array_size(c->servers)); 
    507498  if (res == -1) 
    508499    return MEMCACHED_FAILURE; 
    509500 
    510   ++c->server_count; 
     501  array_push(c->servers); 
    511502 
    512503  return MEMCACHED_SUCCESS; 
     
    12601251static 
    12611252void 
    1262 client_mark_failed(struct client *c, int server_index) 
    1263 { 
    1264   struct server *s; 
    1265  
    1266   s = &c->servers[server_index]; 
    1267  
     1253client_mark_failed(struct client *c, struct server *s) 
     1254{ 
    12681255  if (s->cmd_state.fd != -1) 
    12691256    { 
     
    13181305  while (1) 
    13191306    { 
    1320       int max_fd, i, res; 
     1307      struct server *s; 
     1308      int max_fd, res; 
    13211309 
    13221310      max_fd = -1; 
    1323       for (i = 0; i < c->server_count; ++i) 
     1311      for (s = array_beg(c->servers, struct server); 
     1312           s != array_end(c->servers, struct server); 
     1313           ++s) 
    13241314        { 
    13251315          int may_write, may_read; 
    1326           struct command_state *state = &c->servers[i].cmd_state; 
     1316          struct command_state *state = &s->cmd_state; 
    13271317 
    13281318          if (! is_active(state)) 
     
    13901380                case MEMCACHED_CLOSED: 
    13911381                  deactivate(state); 
    1392                   client_mark_failed(c, i); 
     1382                  client_mark_failed(c, s); 
    13931383                  break; 
    13941384                } 
     
    14121402      FD_ZERO(&write_set); 
    14131403      FD_ZERO(&read_set); 
    1414       for (i = 0; i < c->server_count; ++i) 
     1404      for (s = array_beg(c->servers, struct server); 
     1405           s != array_end(c->servers, struct server); 
     1406           ++s) 
    14151407        { 
    1416           struct command_state *state = &c->servers[i].cmd_state; 
     1408          struct command_state *state = &s->cmd_state; 
    14171409 
    14181410          if (is_active(state)) 
     
    14471439      if (res <= 0) 
    14481440        { 
    1449           for (i = 0; i < c->server_count; ++i) 
     1441          for (s = array_beg(c->servers, struct server); 
     1442               s != array_end(c->servers, struct server); 
     1443               ++s) 
    14501444            { 
    1451               struct command_state *state = &c->servers[i].cmd_state; 
     1445              struct command_state *state = &s->cmd_state; 
    14521446 
    14531447              if (is_active(state)) 
     
    14601454                    state->u.value.object->free(state->u.value.opaque); 
    14611455 
    1462                   client_mark_failed(c, i); 
     1456                  client_mark_failed(c, s); 
    14631457                } 
    14641458            } 
     
    14841478static 
    14851479int 
    1486 get_server_fd(struct client *c, int index) 
    1487 { 
    1488   struct server *s; 
     1480get_server_fd(struct client *c, struct server *s) 
     1481{ 
    14891482  struct command_state *state; 
    1490  
    1491   s = &c->servers[index]; 
    14921483 
    14931484  /* 
     
    15181509 
    15191510  if (state->fd == -1) 
    1520     client_mark_failed(c, index); 
     1511    client_mark_failed(c, s); 
    15211512 
    15221513  return state->fd; 
     
    15251516 
    15261517static 
    1527 int 
    1528 client_get_server_index(struct client *c, const char *key, size_t key_len) 
    1529 { 
     1518struct server * 
     1519client_get_server(struct client *c, const char *key, size_t key_len) 
     1520{ 
     1521  struct server *s; 
    15301522  int index, fd; 
    15311523 
    15321524  index = dispatch_key(&c->dispatch, key, key_len); 
    15331525  if (index == -1) 
    1534     return -1; 
    1535  
    1536   fd = get_server_fd(c, index); 
     1526    return NULL; 
     1527 
     1528  s = array_elem(c->servers, struct server, index); 
     1529 
     1530  fd = get_server_fd(c, s); 
    15371531  if (fd == -1) 
    1538     return -1; 
    1539  
    1540   return index; 
     1532    return NULL; 
     1533 
     1534  return s; 
    15411535} 
    15421536 
     
    15811575 
    15821576  c = state->client; 
    1583   if (c->key_list_count == c->key_list_capacity) 
    1584     { 
    1585       int capacity = (c->key_list_capacity > 0 ? c->key_list_capacity * 2 : 1); 
    1586       struct key_node *list = 
    1587         (struct key_node *) realloc(c->key_list, 
    1588                                     sizeof(struct key_node) * capacity); 
    1589       if (! list) 
    1590         return MEMCACHED_FAILURE; 
    1591  
    1592       c->key_list = list; 
    1593       c->key_list_capacity = capacity; 
    1594     } 
     1577  if (array_extend(c->key_list, struct key_node, 1, ARRAY_EXTEND_TWICE) == -1) 
     1578    return MEMCACHED_FAILURE; 
    15951579 
    15961580  if (state->key_tail != -1) 
    1597     c->key_list[state->key_tail].next = c->key_list_count; 
     1581    array_elem(c->key_list, struct key_node, state->key_tail)->next = 
     1582      array_size(c->key_list); 
    15981583  else 
    1599     state->key_head = c->key_list_count; 
    1600  
    1601   state->key_tail = c->key_list_count; 
    1602  
    1603   node = &c->key_list[state->key_tail]; 
     1584    state->key_head = array_size(c->key_list); 
     1585 
     1586  state->key_tail = array_size(c->key_list); 
     1587 
     1588  node = array_elem(c->key_list, struct key_node, state->key_tail); 
    16041589  node->key = key_index; 
    16051590  node->next = -1; 
    16061591 
    1607   ++c->key_list_count; 
     1592  array_push(c->key_list); 
    16081593 
    16091594  return MEMCACHED_SUCCESS; 
     
    16151600client_reset_for_command(struct client *c) 
    16161601{ 
    1617   c->key_list_count = 0; 
     1602  array_clear(c->key_list); 
    16181603  ++c->generation; 
    16191604} 
     
    16351620     + sizeof(" " FLAGS_STUB " " EXPTIME_STUB " " VALUE_SIZE_STUB 
    16361621              " " NOREPLY "\r\n")); 
     1622  struct server *s; 
    16371623  struct command_state *state; 
    16381624  struct iovec *buf_iov; 
    16391625  char *buf; 
    1640   int server_index, res; 
     1626  int res; 
    16411627 
    16421628  client_reset_for_command(c); 
    16431629 
    1644   server_index = client_get_server_index(c, key, key_len); 
    1645   if (server_index == -1) 
     1630  s = client_get_server(c, key, key_len); 
     1631  if (! s) 
    16461632    return MEMCACHED_CLOSED; 
    16471633 
    1648   state = &c->servers[server_index].cmd_state; 
     1634  state = &s->cmd_state; 
    16491635  use_noreply = (noreply && state->noreply); 
    16501636  command_state_reset(state, (c->prefix_len ? 2 : 1), 1, 
     
    17121698     + sizeof(" " FLAGS_STUB " " EXPTIME_STUB " " VALUE_SIZE_STUB 
    17131699              " " CAS_STUB " " NOREPLY "\r\n")); 
     1700  struct server *s; 
    17141701  struct command_state *state; 
    17151702  struct iovec *buf_iov; 
    17161703  char *buf; 
    1717   int server_index, res; 
     1704  int res; 
    17181705 
    17191706  client_reset_for_command(c); 
    17201707 
    1721   server_index = client_get_server_index(c, key, key_len); 
    1722   if (server_index == -1) 
     1708  s = client_get_server(c, key, key_len); 
     1709  if (! s) 
    17231710    return MEMCACHED_CLOSED; 
    17241711 
    1725   state = &c->servers[server_index].cmd_state; 
     1712  state = &s->cmd_state; 
    17261713  use_noreply = (noreply && state->noreply); 
    17271714  command_state_reset(state, (c->prefix_len ? 2 : 1), 1, 
     
    17621749{ 
    17631750  size_t request_size = (sizeof(struct iovec) * (c->prefix_len ? 4 : 3)); 
     1751  struct server *s; 
    17641752  struct command_state *state; 
    1765   int server_index, res; 
     1753  int res; 
    17661754 
    17671755  client_reset_for_command(c); 
    17681756 
    1769   server_index = client_get_server_index(c, key, key_len); 
    1770   if (server_index == -1) 
     1757  s = client_get_server(c, key, key_len); 
     1758  if (! s) 
    17711759    return MEMCACHED_CLOSED; 
    17721760 
    1773   state = &c->servers[server_index].cmd_state; 
     1761  state = &s->cmd_state; 
    17741762  command_state_reset(state, (c->prefix_len ? 2 : 1), 1, parse_get_reply); 
    17751763  value_state_reset(&state->u.value, o, (cmd == CMD_GETS)); 
     
    18091797  size_t min_request_size = 
    18101798    (sizeof(struct iovec) * ((c->prefix_len ? 3 : 2) + 2)); 
     1799  struct server *s; 
    18111800  struct command_state *state; 
    18121801  int i; 
     
    18191808      size_t key_len; 
    18201809      size_t size; 
    1821       int server_index, res; 
     1810      int res; 
    18221811 
    18231812      key = get_key(o->arg, i, &key_len); 
    18241813 
    1825       server_index = client_get_server_index(c, key, key_len); 
    1826       if (server_index == -1) 
     1814      s = client_get_server(c, key, key_len); 
     1815      if (! s) 
    18271816        continue; 
    18281817 
    1829       state = &c->servers[server_index].cmd_state; 
     1818      state = &s->cmd_state; 
    18301819 
    18311820      if (is_active(state)) 
     
    18731862    } 
    18741863 
    1875   for (i = 0; i < c->server_count; ++i) 
    1876     { 
    1877       state = &c->servers[i].cmd_state; 
     1864  for (s = array_beg(c->servers, struct server); 
     1865       s != array_end(c->servers, struct server); 
     1866       ++s) 
     1867    { 
     1868      state = &s->cmd_state; 
    18781869 
    18791870      if (is_active(state)) 
     
    18981889    (sizeof(struct iovec) * (c->prefix_len ? 4 : 3) 
    18991890     + sizeof(" " ARITH_STUB " " NOREPLY "\r\n")); 
     1891  struct server *s; 
    19001892  struct command_state *state; 
    19011893  struct iovec *buf_iov; 
    19021894  char *buf; 
    1903   int server_index, res; 
     1895  int res; 
    19041896 
    19051897  client_reset_for_command(c); 
    19061898 
    1907   server_index = client_get_server_index(c, key, key_len); 
    1908   if (server_index == -1) 
     1899  s = client_get_server(c, key, key_len); 
     1900  if (! s) 
    19091901    return MEMCACHED_CLOSED; 
    19101902 
    1911   state = &c->servers[server_index].cmd_state; 
     1903  state = &s->cmd_state; 
    19121904  use_noreply = (noreply && state->noreply); 
    19131905  command_state_reset(state, (c->prefix_len ? 2 : 1), 1, 
     
    19581950    (sizeof(struct iovec) * (c->prefix_len ? 4 : 3) 
    19591951     + sizeof(" " DELAY_STUB " " NOREPLY "\r\n")); 
     1952  struct server *s; 
    19601953  struct command_state *state; 
    19611954  struct iovec *buf_iov; 
    19621955  char *buf; 
    1963   int server_index, res; 
     1956  int res; 
    19641957 
    19651958  client_reset_for_command(c); 
    19661959 
    1967   server_index = client_get_server_index(c, key, key_len); 
    1968   if (server_index == -1) 
     1960  s = client_get_server(c, key, key_len); 
     1961  if (! s) 
    19691962    return MEMCACHED_CLOSED; 
    19701963 
    1971   state = &c->servers[server_index].cmd_state; 
     1964  state = &s->cmd_state; 
    19721965  use_noreply = (noreply && state->noreply); 
    19731966  command_state_reset(state, (c->prefix_len ? 2 : 1), 1, 
     
    20051998    (sizeof(struct iovec) * 1 
    20061999     + sizeof("flush_all " DELAY_STUB " " NOREPLY "\r\n")); 
     2000  struct server *s; 
    20072001  double ddelay = delay, delay_step = 0.0; 
    20082002  int i; 
     
    20102004  client_reset_for_command(c); 
    20112005 
    2012   if (c->server_count > 1) 
    2013     delay_step = ddelay / (c->server_count - 1); 
     2006  if (array_size(c->servers) > 1) 
     2007    delay_step = ddelay / (array_size(c->servers) - 1); 
    20142008  ddelay += delay_step; 
    20152009 
    2016   for (i = 0; i < c->server_count; ++i) 
     2010  for (s = array_beg(c->servers, struct server); 
     2011       s != array_end(c->servers, struct server); 
     2012       ++s) 
    20172013    { 
    20182014      int use_noreply; 
     
    20242020      ddelay -= delay_step; 
    20252021 
    2026       fd = get_server_fd(c, i); 
     2022      fd = get_server_fd(c, s); 
    20272023      if (fd == -1) 
    20282024        continue; 
    20292025 
    2030       state = &c->servers[i].cmd_state; 
     2026      state = &s->cmd_state; 
    20312027      use_noreply = (noreply && state->noreply); 
    20322028      command_state_reset(state, 0, 0, 
     
    20562052client_nowait_push(struct client *c) 
    20572053{ 
     2054  struct server *s; 
    20582055  int i; 
    20592056 
     
    20632060  client_reset_for_command(c); 
    20642061 
    2065   for (i = 0; i < c->server_count; ++i) 
     2062  for (s = array_beg(c->servers, struct server); 
     2063       s != array_end(c->servers, struct server); 
     2064       ++s) 
    20662065    { 
    20672066      struct command_state *state; 
    20682067      int fd, res; 
    20692068 
    2070       state = &c->servers[i].cmd_state; 
     2069      state = &s->cmd_state; 
    20712070      if (state->nowait_count == 0) 
    20722071        continue; 
    20732072 
    2074       fd = get_server_fd(c, i); 
     2073      fd = get_server_fd(c, s); 
    20752074      if (fd == -1) 
    20762075        continue; 
     
    20922091{ 
    20932092  static const size_t request_size = (sizeof(struct iovec) * 1); 
     2093  struct server *s; 
    20942094  int i; 
    20952095 
    20962096  client_reset_for_command(c); 
    20972097 
    2098   for (i = 0; i < c->server_count; ++i) 
     2098  for (s = array_beg(c->servers, struct server); 
     2099       s != array_end(c->servers, struct server); 
     2100       ++s) 
    20992101    { 
    21002102      struct command_state *state; 
    21012103      int fd, res; 
    21022104 
    2103       fd = get_server_fd(c, i); 
     2105      fd = get_server_fd(c, s); 
    21042106      if (fd == -1) 
    21052107        continue; 
    21062108 
    2107       state = &c->servers[i].cmd_state; 
     2109      state = &s->cmd_state; 
    21082110      command_state_reset(state, 0, 0, parse_version_reply); 
    21092111      embedded_state_reset(&state->u.embedded, o); 
  • src/dispatch_key.c

    rcd76f9 r70e5d7  
    3939 
    4040 
    41 struct dispatch_continuum_point 
     41struct continuum_point 
    4242{ 
    4343  unsigned int point; 
     
    4646 
    4747 
    48 static inline 
    49 int 
    50 extend_bins(struct dispatch_state *state, int add) 
    51 { 
    52   int capacity = 
    53     (state->bins_capacity > 0 ? state->bins_capacity + add : add); 
    54   struct dispatch_continuum_point *b = 
    55     (struct dispatch_continuum_point *) 
    56       realloc(state->bins, 
    57               capacity * sizeof(struct dispatch_continuum_point)); 
    58  
    59   if (! b) 
    60     return -1; 
    61  
    62   state->bins = b; 
    63   state->bins_capacity = capacity; 
    64  
    65   return 0; 
    66 } 
    67  
    68  
    6948static 
    70 int 
     49struct continuum_point * 
    7150dispatch_find_bin(struct dispatch_state *state, unsigned int point) 
    7251{ 
    73   struct dispatch_continuum_point *left, *right; 
    74  
    75   left = state->bins; 
    76   right = state->bins + state->bins_count; 
     52  struct continuum_point *beg, *end, *left, *right; 
     53 
     54  beg = left = array_beg(state->bins, struct continuum_point); 
     55  end = right = array_end(state->bins, struct continuum_point); 
    7756 
    7857  while (left < right) 
    7958    { 
    80       struct dispatch_continuum_point *middle = left + (right - left) / 2; 
     59      struct continuum_point *middle = left + (right - left) / 2; 
    8160      if (middle->point < point) 
    8261        left = middle + 1; 
     
    8463        right = middle; 
    8564      else 
    86         return (middle - state->bins); 
     65        return middle; 
    8766    } 
    8867 
    8968  /* Wrap around.  */ 
    90   if (left == state->bins + state->bins_count) 
    91     left = state->bins; 
    92  
    93   return (left - state->bins); 
     69  if (left == end) 
     70    left = beg; 
     71 
     72  return left; 
    9473} 
    9574 
     
    10685  int i; 
    10786  double scale; 
    108  
    109   if (state->bins_count == state->bins_capacity) 
    110     { 
    111       int res = extend_bins(state, 1); 
    112       if (res == -1) 
    113         return -1; 
    114     } 
     87  struct continuum_point *p; 
     88 
     89  if (array_extend(state->bins, struct continuum_point, 
     90                   1, ARRAY_EXTEND_EXACT) == -1) 
     91    return -1; 
    11592 
    11693  state->total_weight += weight; 
    11794  scale = (1 - weight / state->total_weight); 
    118   for (i = 0; i < state->bins_count; ++i) 
    119     state->bins[i].point = ((double) state->bins[i].point * scale + 0.5); 
    120  
    121   state->bins[state->bins_count].point = DISPATCH_MAX_POINT; 
    122   state->bins[state->bins_count].index = index; 
    123  
    124   ++state->bins_count; 
     95  for (p = array_beg(state->bins, struct continuum_point); 
     96       p != array_end(state->bins, struct continuum_point); 
     97       ++p) 
     98    p->point = ((double) p->point * scale + 0.5); 
     99 
     100  p->point = DISPATCH_MAX_POINT; 
     101  p->index = index; 
     102  array_push(state->bins); 
    125103 
    126104  return 0; 
     
    143121    server index. 
    144122  */ 
    145   int bin; 
     123  struct continuum_point *p; 
    146124  unsigned int crc32 = compute_crc32(key, key_len); 
    147125  unsigned int hash = ((crc32 >> 16) & 0x00007fff); 
     
    155133  point += 1; 
    156134 
    157   bin = dispatch_find_bin(state, point); 
    158   return state->bins[bin].index; 
     135  p = dispatch_find_bin(state, point); 
     136  return p->index; 
    159137} 
    160138 
     
    173151  count = (state->ketama_points * weight + 0.5); 
    174152 
    175   if (state->bins_count + count > state->bins_capacity) 
    176     { 
    177       int add = state->bins_count + count - state->bins_capacity; 
    178       int res = extend_bins(state, add); 
    179       if (res == -1) 
    180         return -1; 
    181     } 
     153  if (array_extend(state->bins, struct continuum_point, 
     154                   count, ARRAY_EXTEND_EXACT) == -1) 
     155    return -1; 
    182156 
    183157  crc32 = compute_crc32(host, host_len); 
     
    189163      char buf[4]; 
    190164      unsigned int point; 
    191       int bin; 
     165      struct continuum_point *p; 
    192166 
    193167      /* 
     
    202176      point = compute_crc32_add(crc32, buf, 4); 
    203177 
    204       if (state->bins_count > 0) 
     178      if (! array_empty(state->bins)) 
    205179        { 
    206           bin = dispatch_find_bin(state, point); 
     180          p = dispatch_find_bin(state, point); 
    207181 
    208182          /* Check if we wrapped around but actually have new max point.  */ 
    209           if (bin == 0 && point > state->bins[0].point) 
     183          if (p == array_beg(state->bins, struct continuum_point) 
     184              && point > p->point) 
    210185            { 
    211               bin = state->bins_count; 
     186              p = array_end(state->bins, struct continuum_point); 
    212187            } 
    213188          else 
    214189            { 
    215               if (point == state->bins[bin].point) 
     190              if (point == p->point) 
    216191                { 
    217192                  /* 
     
    222197                    distribution. 
    223198                  */ 
    224                   ++bin; 
     199                  ++p; 
    225200                } 
    226201 
    227202              /* Move the tail one position forward.  */ 
    228               memmove(state->bins + bin + 1, state->bins + bin, 
    229                       (state->bins_count - bin) * sizeof(*state->bins)); 
     203              memmove(p + 1, p, 
     204                      ((array_end(state->bins, struct continuum_point) - p) 
     205                       * sizeof(*p))); 
    230206            } 
    231207        } 
    232208      else 
    233209        { 
    234           bin = 0; 
     210          p = array_beg(state->bins, struct continuum_point); 
    235211        } 
    236212 
    237       state->bins[bin].point = point; 
    238       state->bins[bin].index = index; 
    239  
    240       ++state->bins_count; 
     213      p->point = point; 
     214      p->index = index; 
     215      array_push(state->bins); 
    241216    } 
    242217 
     
    251226{ 
    252227  unsigned int point = compute_crc32(key, key_len); 
    253   int bin = dispatch_find_bin(state, point); 
    254   return state->bins[bin].index; 
     228  struct continuum_point *p = dispatch_find_bin(state, point); 
     229  return p->index; 
    255230} 
    256231 
     
    259234dispatch_init(struct dispatch_state *state) 
    260235{ 
    261   state->bins = NULL; 
    262   state->bins_count = state->bins_capacity = 0; 
     236  array_init(&state->bins); 
    263237  state->total_weight = 0.0; 
    264238  state->ketama_points = 0; 
     
    269243dispatch_destroy(struct dispatch_state *state) 
    270244{ 
    271   free(state->bins); 
     245  array_destroy(&state->bins); 
    272246} 
    273247 
     
    297271dispatch_key(struct dispatch_state *state, const char *key, size_t key_len) 
    298272{ 
    299   if (state->bins_count == 0) 
     273  if (array_empty(state->bins)) 
    300274    return -1; 
    301275 
    302   if (state->bins_count == 1) 
    303     { 
    304       return state->bins[0].index; 
     276  if (array_size(state->bins) == 1) 
     277    { 
     278      struct continuum_point *p = 
     279        array_beg(state->bins, struct continuum_point); 
     280      return p->index; 
    305281    } 
    306282  else 
  • src/dispatch_key.h

    rcd76f9 r70e5d7  
    2525#define DISPATCH_KEY_H 1 
    2626 
     27#include "array.h" 
    2728#include <stddef.h> 
    28  
    29  
    30 struct dispatch_continuum_point; 
    3129 
    3230 
    3331struct dispatch_state 
    3432{ 
    35   struct dispatch_continuum_point *bins; 
    36   int bins_count; 
    37   int bins_capacity; 
     33  struct array bins; 
    3834  double total_weight; 
    3935  int ketama_points; 
Note: See TracChangeset for help on using the changeset viewer.