You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

184 lines
4.9 KiB

  1. /*****************************************************************************
  2. Copyright (c) 2007, 2011, Oracle and/or its affiliates. All Rights Reserved.
  3. This program is free software; you can redistribute it and/or modify it under
  4. the terms of the GNU General Public License as published by the Free Software
  5. Foundation; version 2 of the License.
  6. This program is distributed in the hope that it will be useful, but WITHOUT
  7. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License along with
  10. this program; if not, write to the Free Software Foundation, Inc.,
  11. 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
  12. *****************************************************************************/
  13. /**************************************************//**
  14. @file ha/ha0storage.cc
  15. Hash storage.
  16. Provides a data structure that stores chunks of data in
  17. its own storage, avoiding duplicates.
  18. Created September 22, 2007 Vasil Dimov
  19. *******************************************************/
  20. #include "univ.i"
  21. #include "ha0storage.h"
  22. #include "hash0hash.h"
  23. #include "mem0mem.h"
  24. #include "ut0rnd.h"
  25. #ifdef UNIV_NONINL
  26. #include "ha0storage.ic"
  27. #endif
  28. /*******************************************************************//**
  29. Retrieves a data from a storage. If it is present, a pointer to the
  30. stored copy of data is returned, otherwise NULL is returned. */
  31. static
  32. const void*
  33. ha_storage_get(
  34. /*===========*/
  35. ha_storage_t* storage, /*!< in: hash storage */
  36. const void* data, /*!< in: data to check for */
  37. ulint data_len) /*!< in: data length */
  38. {
  39. ha_storage_node_t* node;
  40. ulint fold;
  41. /* avoid repetitive calls to ut_fold_binary() in the HASH_SEARCH
  42. macro */
  43. fold = ut_fold_binary(static_cast<const byte*>(data), data_len);
  44. #define IS_FOUND \
  45. node->data_len == data_len && memcmp(node->data, data, data_len) == 0
  46. HASH_SEARCH(
  47. next, /* node->"next" */
  48. storage->hash, /* the hash table */
  49. fold, /* key */
  50. ha_storage_node_t*, /* type of node->next */
  51. node, /* auxiliary variable */
  52. , /* assertion */
  53. IS_FOUND); /* search criteria */
  54. if (node == NULL) {
  55. return(NULL);
  56. }
  57. /* else */
  58. return(node->data);
  59. }
  60. /*******************************************************************//**
  61. Copies data into the storage and returns a pointer to the copy. If the
  62. same data chunk is already present, then pointer to it is returned.
  63. Data chunks are considered to be equal if len1 == len2 and
  64. memcmp(data1, data2, len1) == 0. If "data" is not present (and thus
  65. data_len bytes need to be allocated) and the size of storage is going to
  66. become more than "memlim" then "data" is not added and NULL is returned.
  67. To disable this behavior "memlim" can be set to 0, which stands for
  68. "no limit". */
  69. UNIV_INTERN
  70. const void*
  71. ha_storage_put_memlim(
  72. /*==================*/
  73. ha_storage_t* storage, /*!< in/out: hash storage */
  74. const void* data, /*!< in: data to store */
  75. ulint data_len, /*!< in: data length */
  76. ulint memlim) /*!< in: memory limit to obey */
  77. {
  78. void* raw;
  79. ha_storage_node_t* node;
  80. const void* data_copy;
  81. ulint fold;
  82. /* check if data chunk is already present */
  83. data_copy = ha_storage_get(storage, data, data_len);
  84. if (data_copy != NULL) {
  85. return(data_copy);
  86. }
  87. /* not present */
  88. /* check if we are allowed to allocate data_len bytes */
  89. if (memlim > 0
  90. && ha_storage_get_size(storage) + data_len > memlim) {
  91. return(NULL);
  92. }
  93. /* we put the auxiliary node struct and the data itself in one
  94. continuous block */
  95. raw = mem_heap_alloc(storage->heap,
  96. sizeof(ha_storage_node_t) + data_len);
  97. node = (ha_storage_node_t*) raw;
  98. data_copy = (byte*) raw + sizeof(*node);
  99. memcpy((byte*) raw + sizeof(*node), data, data_len);
  100. node->data_len = data_len;
  101. node->data = data_copy;
  102. /* avoid repetitive calls to ut_fold_binary() in the HASH_INSERT
  103. macro */
  104. fold = ut_fold_binary(static_cast<const byte*>(data), data_len);
  105. HASH_INSERT(
  106. ha_storage_node_t, /* type used in the hash chain */
  107. next, /* node->"next" */
  108. storage->hash, /* the hash table */
  109. fold, /* key */
  110. node); /* add this data to the hash */
  111. /* the output should not be changed because it will spoil the
  112. hash table */
  113. return(data_copy);
  114. }
  115. #ifdef UNIV_COMPILE_TEST_FUNCS
  116. void
  117. test_ha_storage()
  118. {
  119. ha_storage_t* storage;
  120. char buf[1024];
  121. int i;
  122. const void* stored[256];
  123. const void* p;
  124. storage = ha_storage_create(0, 0);
  125. for (i = 0; i < 256; i++) {
  126. memset(buf, i, sizeof(buf));
  127. stored[i] = ha_storage_put(storage, buf, sizeof(buf));
  128. }
  129. //ha_storage_empty(&storage);
  130. for (i = 255; i >= 0; i--) {
  131. memset(buf, i, sizeof(buf));
  132. p = ha_storage_put(storage, buf, sizeof(buf));
  133. if (p != stored[i]) {
  134. fprintf(stderr, "ha_storage_put() returned %p "
  135. "instead of %p, i=%d\n", p, stored[i], i);
  136. return;
  137. }
  138. }
  139. fprintf(stderr, "all ok\n");
  140. ha_storage_free(storage);
  141. }
  142. #endif /* UNIV_COMPILE_TEST_FUNCS */