2 +--------------------------------------------------------------------+
3 | libmemcached - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020 Michael Wallner <mike@php.net> |
13 +--------------------------------------------------------------------+
16 #include "libmemcached/common.h"
23 struct memcached_virtual_bucket_t
{
27 struct bucket_t buckets
[];
30 memcached_return_t
memcached_virtual_bucket_create(memcached_st
*self
, const uint32_t *host_map
,
31 const uint32_t *forward_map
,
32 const uint32_t buckets
,
33 const uint32_t replicas
) {
34 if (self
== NULL
|| host_map
== NULL
|| buckets
== 0U) {
35 return MEMCACHED_INVALID_ARGUMENTS
;
38 memcached_virtual_bucket_free(self
);
40 struct memcached_virtual_bucket_t
*virtual_bucket
= (struct memcached_virtual_bucket_t
*) malloc(
41 sizeof(struct memcached_virtual_bucket_t
) + sizeof(struct bucket_t
) * buckets
);
43 if (virtual_bucket
== NULL
) {
44 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
47 virtual_bucket
->size
= buckets
;
48 virtual_bucket
->replicas
= replicas
;
49 self
->virtual_bucket
= virtual_bucket
;
52 for (; x
< buckets
; x
++) {
53 virtual_bucket
->buckets
[x
].master
= host_map
[x
];
55 virtual_bucket
->buckets
[x
].forward
= forward_map
[x
];
57 virtual_bucket
->buckets
[x
].forward
= 0;
61 return MEMCACHED_SUCCESS
;
64 void memcached_virtual_bucket_free(memcached_st
*self
) {
66 if (self
->virtual_bucket
) {
67 free(self
->virtual_bucket
);
68 self
->virtual_bucket
= NULL
;
73 uint32_t memcached_virtual_bucket_get(const memcached_st
*self
, uint32_t digest
) {
75 if (self
->virtual_bucket
) {
76 uint32_t result
= (uint32_t)(digest
& (self
->virtual_bucket
->size
- 1));
77 return self
->virtual_bucket
->buckets
[result
].master
;
80 return (uint32_t)(digest
& (self
->number_of_hosts
- 1));