wangzhengquan
2021-02-05 14c345b38d57fd814f217eb8465963a08ca79f7e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#include "usg_common.h"
#include "hashtable.h"
#include "mm.h"
#include "svsem.h"
#include "logger_factory.h"
#include <set>
#include <functional>
#include <limits.h>
 
 
 
typedef struct tailq_entry_t
{
  void *value;
  int key;
  /*
   * This holds the pointers to the next and previous joint in
   * the tail queue.
   */
  TAILQ_ENTRY(tailq_entry_t) joint;
} tailq_entry_t;
 
#define START_KEY 1000
 
typedef  TAILQ_HEAD(tailq_header_t, tailq_entry_t) tailq_header_t;
 
 
static size_t hashcode(int key);
 
void hashtable_init(hashtable_t *hashtable )
{
 
  memset(hashtable, 0, sizeof(hashtable_t));
  hashtable->mutex = svsem_get(IPC_PRIVATE, 1);
  hashtable->queueCount = 0;
  hashtable->currentKey = START_KEY;
}
 
void hashtable_destroy(hashtable_t *hashtable) {
  svsem_remove( hashtable->mutex);
}
 
 
static inline void *_hashtable_get(hashtable_t *hashtable, int key)
{
  size_t code = hashcode(key);
  tailq_entry_t *item;
  tailq_header_t *my_tailq_head = hashtable->array[code] ;
  if ( my_tailq_head == NULL)
  {
    return NULL;
  }
  else
  {
 
    TAILQ_FOREACH(item, my_tailq_head, joint)
    {
      if (key == item->key)
        return item->value;
    }
  }
  return NULL;
 
}
 
static inline void * _hashtable_put(hashtable_t *hashtable, int key, void *value)
{
  size_t code = hashcode(key);
  void *oldvalue;
  tailq_entry_t *item;
  tailq_header_t *my_tailq_head = hashtable->array[code] ;
  if ( my_tailq_head == NULL)
  {
    my_tailq_head  = (tailq_header_t*) mm_malloc(sizeof(tailq_header_t ));
    TAILQ_INIT(my_tailq_head);
    hashtable->array[code] = my_tailq_head;
    goto putnew;
  }
 
  TAILQ_FOREACH(item, my_tailq_head, joint)
  {
    if (key ==item->key)
    {
      oldvalue = item -> value;
      item->key= key;
      item -> value = value;
      return oldvalue;
    }
  }
putnew:
  item = (tailq_entry_t *) mm_malloc(sizeof(tailq_entry_t));
  item->key = key;
  item -> value = value;
  TAILQ_INSERT_TAIL(my_tailq_head, item, joint);
  return NULL;
}
 
void *hashtable_remove(hashtable_t *hashtable, int key)
{
  size_t code = hashcode(key);
  tailq_entry_t *item;
  void *oldvalue;
  int rv;
 
  if( (rv = svsem_wait(hashtable->mutex)) != 0) {
    LoggerFactory::getLogger()->error(errno, "hashtable_remove\n");
  }
  tailq_header_t *my_tailq_head = hashtable->array[code] ;
  if ( my_tailq_head == NULL)
  {
    if((rv = svsem_post(hashtable->mutex)) != 0) {
      LoggerFactory::getLogger()->error(errno, "hashtable_remove\n");
    }
    return NULL;
  } else {
    for (item = TAILQ_FIRST(my_tailq_head); item != NULL; item = TAILQ_NEXT(item, joint))
    {
      if (key == item->key)
      {
        oldvalue = item->value;
        /* Remove the item from the tail queue. */
        TAILQ_REMOVE(my_tailq_head, item, joint);
 
        /* mm_free the item as we don't need it anymore. */
        mm_free(item);
        hashtable->queueCount--;
        svsem_post(hashtable->mutex);
        return oldvalue;
      }
    }
 
    if((rv = svsem_post(hashtable->mutex)) != 0) {
      LoggerFactory::getLogger()->error(errno, "hashtable_remove\n");
    }
 
    return NULL;
  }
}
 
void *hashtable_get(hashtable_t *hashtable, int key) {
  void * res = _hashtable_get(hashtable, key);
  return res;
}
 
void hashtable_put(hashtable_t *hashtable, int key, void *value) {
  _hashtable_put(hashtable, key, value); 
  hashtable->queueCount++;
}
 
bool hashtable_check_put(hashtable_t *hashtable, int key, void *value, bool overwrite) {
 
  int rv;
  void * val;
  if(( rv = svsem_wait(hashtable->mutex)) != 0) {
    LoggerFactory::getLogger()->error(errno, "hashtable_put\n");
  }
  if(overwrite) {
    _hashtable_put(hashtable, key, value);
    goto suc;
  }
  val = _hashtable_get(hashtable, key);
  // val = 1是allockey的情况
  if(val != NULL && val != (void *)1) 
    goto fail;
 
  _hashtable_put(hashtable, key, value);
 
suc:
  if(( rv = svsem_post(hashtable->mutex)) != 0) {
    LoggerFactory::getLogger()->error(errno, "hashtable_put\n");
  }
  return true;
 
fail:
  if(( rv = svsem_post(hashtable->mutex)) != 0) {
    LoggerFactory::getLogger()->error(errno, "hashtable_put\n");
  }
  return false;
}
 
int hashtable_get_queue_count(hashtable_t *hashtable) {
  return hashtable->queueCount;
}
 
int hashtable_alloc_key(hashtable_t *hashtable) {
  int rv;
  int key = hashtable->currentKey;
 
  if( key == INT_MAX || key < START_KEY) {
    key = START_KEY;
  }
 
  rv = svsem_wait(hashtable->mutex);
  if(rv != 0) {
    LoggerFactory::getLogger()->error(errno, "hashtable_alloc_key\n");
  }
 
  while(_hashtable_get(hashtable, key) != NULL) {
    key++;
  }
  // 占用key
  _hashtable_put(hashtable, key, (void *)1);
 
  hashtable->currentKey = key;
  rv = svsem_post(hashtable->mutex);
  if(rv != 0) {
    LoggerFactory::getLogger()->error(errno, "hashtable_alloc_key\n");
  }
 
  return key;
}
 
static inline void _hashtable_foreach(hashtable_t *hashtable, std::function<void(int, void *)>  cb) {
  tailq_entry_t *item;
  for (int i = 0; i < MAPSIZE; i++) {
    tailq_header_t *my_tailq_head = hashtable->array[i] ;
 
    if (my_tailq_head == NULL )
      continue;
 
    TAILQ_FOREACH(item, my_tailq_head, joint)
    {
      cb(item->key,  item -> value);
    }
  }
}
 
 
void hashtable_foreach(hashtable_t *hashtable,  std::function<void(int, void *)>  cb) {
 return _hashtable_foreach(hashtable, cb);
 
}
 
 
std::set<int> * hashtable_keyset(hashtable_t *hashtable) {
  std::set<int> *keyset = new std::set<int>;
  tailq_entry_t *item;
  for (int i = 0; i < MAPSIZE; i++) {
    tailq_header_t *my_tailq_head = hashtable->array[i] ;
 
    if (my_tailq_head == NULL )
      continue;
 
    TAILQ_FOREACH(item, my_tailq_head, joint)
    {
      keyset->insert(item->key);
    }
  }
  return keyset;
}
 
 
 
int hashtable_lock(hashtable_t *hashtable) {
  return svsem_wait(hashtable->mutex);
}
 
int hashtable_unlock(hashtable_t *hashtable) {
  return svsem_post(hashtable->mutex);
}
 
 
void hashtable_removeall(hashtable_t *hashtable)
{
  tailq_entry_t *item;
  int rv;
  if( (rv = svsem_wait(hashtable->mutex)) != 0) {
    LoggerFactory::getLogger()->error(errno, "hashtable_removeall\n");
  }
  for (int i = 0; i < MAPSIZE; i++)
  {
    tailq_header_t *my_tailq_head = hashtable->array[i] ;
 
    if (my_tailq_head == NULL )
      continue;
 
    while ((item = TAILQ_FIRST(my_tailq_head)) )
    {
      TAILQ_REMOVE(my_tailq_head, item, joint);
      mm_free(item);
    }
    mm_free(my_tailq_head);
    hashtable->array[i] = NULL;
  }
 
  hashtable->queueCount = 0;
  if((rv = svsem_post(hashtable->mutex)) != 0) {
    LoggerFactory::getLogger()->error(errno, "hashtable_removeall\n");
  }
}
 
 
static size_t hashcode(int key)
{
 
  return key % MAPSIZE;
  /*printf("hashfun = %ld\n", code);*/
}
 
/**
 * for debug
 */
static void hashtable_printall(hashtable_t *hashtable)
{
  tailq_entry_t *item;
  for (int i = 0; i < MAPSIZE; i++)
  {
    tailq_header_t *my_tailq_head = hashtable->array[i] ;
 
    if (my_tailq_head == NULL )
      continue;
 
    printf("code=%d\n", i);
    TAILQ_FOREACH(item, my_tailq_head, joint)
    {
      printf("%d:%s\n", item->key, (char *)item->value);
    }
    printf("\n");
  }
}