2 +--------------------------------------------------------------------+
3 | libmemcached-awesome - 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-2021 Michael Wallner https://awesome.co/ |
13 +--------------------------------------------------------------------+
16 #include "libmemcached/common.h"
17 #include "libmemcached/virtual_bucket.h"
24 struct memcached_virtual_bucket_t
{
28 struct bucket_t buckets
[];
31 memcached_return_t
memcached_virtual_bucket_create(memcached_st
*self
, const uint32_t *host_map
,
32 const uint32_t *forward_map
,
33 const uint32_t buckets
,
34 const uint32_t replicas
) {
35 if (self
== NULL
|| host_map
== NULL
|| buckets
== 0U) {
36 return MEMCACHED_INVALID_ARGUMENTS
;
39 memcached_virtual_bucket_free(self
);
41 struct memcached_virtual_bucket_t
*virtual_bucket
= (struct memcached_virtual_bucket_t
*) malloc(
42 sizeof(struct memcached_virtual_bucket_t
) + sizeof(struct bucket_t
) * buckets
);
44 if (virtual_bucket
== NULL
) {
45 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
48 virtual_bucket
->size
= buckets
;
49 virtual_bucket
->replicas
= replicas
;
50 self
->virtual_bucket
= virtual_bucket
;
53 for (; x
< buckets
; x
++) {
54 virtual_bucket
->buckets
[x
].master
= host_map
[x
];
56 virtual_bucket
->buckets
[x
].forward
= forward_map
[x
];
58 virtual_bucket
->buckets
[x
].forward
= 0;
62 return MEMCACHED_SUCCESS
;
65 void memcached_virtual_bucket_free(memcached_st
*self
) {
67 if (self
->virtual_bucket
) {
68 free(self
->virtual_bucket
);
69 self
->virtual_bucket
= NULL
;
74 uint32_t memcached_virtual_bucket_get(const memcached_st
*self
, uint32_t digest
) {
76 if (self
->virtual_bucket
) {
77 uint32_t result
= (uint32_t)(digest
& (self
->virtual_bucket
->size
- 1));
78 return self
->virtual_bucket
->buckets
[result
].master
;
81 return (uint32_t)(digest
& (self
->number_of_hosts
- 1));