Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit43c0beb

Browse files
authored
Merge pull requestmicrosoft#19 from Microsoft/inMemoryDB-MSRA
Memory Database Bug Fix: Merge to inMemoryDB branch
2 parents683b15a +0ec0a3a commit43c0beb

File tree

7 files changed

+169
-93
lines changed

7 files changed

+169
-93
lines changed

‎apache2/ag_mdb/ag_mdb.cpp‎

Lines changed: 59 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010

1111
/**
12-
** Create and initialize aAGMDB_lock, if the lock with given key exists, just link to the lock.
12+
** Create and initialize aagmdb_lock, if the lock with given key exists, just link to the lock.
1313
** The new_lock should have been created before calling this function.
1414
** @param new_lock: The pointer to save the information of the lock.
1515
** @param lock_name: The name of lock.
@@ -18,7 +18,7 @@
1818
** AGMDB_LOCK_SUCCESS_OPEN if successfully link to an existed lock,
1919
** AGMDB_FAIL if failed.
2020
*/
21-
intLock_create(structAGMDB_Lock *new_lock,constchar* lock_name,int lock_name_length) {
21+
intLock_create(structagmdb_lock *new_lock,constchar* lock_name,int lock_name_length) {
2222
#ifndef _WIN32
2323
union semun sem_union;
2424
int lock_id =AGMDB_hash(lock_name, lock_name_length, DEFAULT_AGMDB_ID_RANGE);
@@ -89,13 +89,13 @@ int Lock_create(struct AGMDB_Lock *new_lock, const char* lock_name, int lock_nam
8989

9090
/**
9191
** Decrease a lock's value by a given number.
92-
** @param db_lock: TheAGMDB_lock sturcture
92+
** @param db_lock: Theagmdb_lock sturcture
9393
** @param index: The index of atom lock (read or write) .
9494
** @param val: The value you want to decrease from the lock.
9595
** return: AGMDB_SUCCESS if success;
9696
** or AGMDB_FAIL if failed
9797
*/
98-
intLock_P(conststructAGMDB_Lock *db_lock,int index,int val) {
98+
intLock_P(conststructagmdb_lock *db_lock,int index,int val) {
9999
#ifndef _WIN32
100100
structsembuf sem_op;
101101
if (val <0)
@@ -126,13 +126,13 @@ int Lock_P(const struct AGMDB_Lock *db_lock, int index, int val) {
126126

127127
/**
128128
** Increase a lock's value by a given number.
129-
** @param db_lock: TheAGMDB_lock sturcture
129+
** @param db_lock: Theagmdb_lock sturcture
130130
** @param index: The index of atom lock (read or write) .
131131
** @param val: The value you want to add to the lock.
132132
** return: AGMDB_SUCCESS if success;
133133
** or AGMDB_FAIL if failed
134134
*/
135-
intLock_V(conststructAGMDB_Lock *db_lock,int index,int val) {
135+
intLock_V(conststructagmdb_lock *db_lock,int index,int val) {
136136
#ifndef _WIN32
137137
structsembuf sem_op;
138138
if (val <0)
@@ -614,10 +614,10 @@ unsigned int AGMDB_hash(const char* key, int key_len, unsigned int output_val_ra
614614
** return: AGMDB_SUCCESS if successfully created or AGMDB_FAIL if failed.
615615
*/
616616
intAGMDB_getSharedLock(structagmdb_handler *dbm) {
617-
structAGMDB_Lock *db_lock;
617+
structagmdb_lock *db_lock;
618618
if(dbm ==NULL)
619619
return AGMDB_FAIL;
620-
db_lock = (structAGMDB_Lock *)(dbm->db_lock);
620+
db_lock = (structagmdb_lock *)&(dbm->db_lock);
621621

622622
if(Lock_P(db_lock, SEM_ID_WRITE,1) == AGMDB_FAIL)
623623
return AGMDB_FAIL;
@@ -641,10 +641,10 @@ int AGMDB_getSharedLock(struct agmdb_handler *dbm) {
641641
** return: AGMDB_SUCCESS if successfully created or AGMDB_FAIL if failed.
642642
*/
643643
intAGMDB_getExclusiveLock(structagmdb_handler *dbm) {
644-
structAGMDB_Lock *db_lock;
644+
structagmdb_lock *db_lock;
645645
if(dbm ==NULL)
646646
return AGMDB_FAIL;
647-
db_lock = (structAGMDB_Lock *)(dbm->db_lock);
647+
db_lock = (structagmdb_lock *)&(dbm->db_lock);
648648

649649
if(Lock_P(db_lock, SEM_ID_WRITE,1) == AGMDB_FAIL)
650650
return AGMDB_FAIL;
@@ -663,10 +663,10 @@ int AGMDB_getExclusiveLock(struct agmdb_handler *dbm) {
663663
** return: AGMDB_SUCCESS if successfully created or AGMDB_FAIL if failed.
664664
*/
665665
intAGMDB_freeSharedLock(structagmdb_handler *dbm){
666-
structAGMDB_Lock *db_lock;
666+
structagmdb_lock *db_lock;
667667
if(dbm ==NULL)
668668
return AGMDB_FAIL;
669-
db_lock = (structAGMDB_Lock *)(dbm->db_lock);
669+
db_lock = (structagmdb_lock *)&(dbm->db_lock);
670670

671671
if(Lock_V(db_lock, SEM_ID_READ,1) == AGMDB_FAIL)
672672
return AGMDB_FAIL;
@@ -680,10 +680,10 @@ int AGMDB_freeSharedLock(struct agmdb_handler *dbm){
680680
** return: AGMDB_SUCCESS if successfully created or AGMDB_FAIL if failed.
681681
*/
682682
intAGMDB_freeExclusiveLock(structagmdb_handler *dbm){
683-
structAGMDB_Lock *db_lock;
683+
structagmdb_lock *db_lock;
684684
if(dbm ==NULL)
685685
return AGMDB_FAIL;
686-
db_lock = (structAGMDB_Lock *)(dbm->db_lock);
686+
db_lock = (structagmdb_lock *)&(dbm->db_lock);
687687

688688
if(Lock_V(db_lock, SEM_ID_READ, SEM_READ_INITVAL) == AGMDB_FAIL)
689689
return AGMDB_FAIL;
@@ -810,7 +810,6 @@ inline int AGMDB_deleteEntry(CPTR_VOID shm_base, PTR_OFFSET entry_id){
810810
*/
811811
intAGMDB_openDB(structagmdb_handler* dbm,constchar* db_name,int db_name_length,unsignedint entry_num) {
812812
int rc;
813-
structAGMDB_Lock *db_lock;
814813
PTR_VOID shm_base;
815814

816815
if(dbm ==NULL)
@@ -830,12 +829,10 @@ int AGMDB_openDB(struct agmdb_handler* dbm, const char* db_name, int db_name_len
830829
if((rc != AGMDB_SHM_SUCCESS_CREATE) && (rc != AGMDB_SHM_SUCCESS_OPEN))
831830
return rc;
832831

833-
db_lock = (structAGMDB_Lock *)malloc(sizeof(structAGMDB_Lock));
834-
rc =Lock_create(db_lock, db_name, db_name_length);
832+
rc =Lock_create(&(dbm->db_lock), db_name, db_name_length);
835833
if((rc != AGMDB_LOCK_SUCCESS_CREATE) && (rc != AGMDB_LOCK_SUCCESS_OPEN))
836834
return rc;
837835

838-
dbm->db_lock = (void*) db_lock;
839836
dbm->shm_base = shm_base;
840837

841838
return AGMDB_SUCCESS;
@@ -1079,10 +1076,11 @@ int AGMDB_removeStale(struct agmdb_handler *dbm) {
10791076
** Get the number of keys in a database.
10801077
** You have to get SHARED or EXCLUSIVE LOCK of the database before calling this function.
10811078
** @param dbm: the database you want to read.
1082-
** return: number of keys in the database
1079+
** @param keynum: the value to store the number of keys in the database
1080+
** return: AGMDB_SUCCESS if no error
10831081
** or AGMDB_FAIL if failed.
10841082
*/
1085-
unsignedintAGMDB_getKeyNum(structagmdb_handler *dbm) {
1083+
intAGMDB_getKeyNum(structagmdb_handler *dbm,int* keynum) {
10861084
CPTR_VOID shm_base;
10871085
unsignedint timelist_cnt =0;
10881086
PTR_VOID entry_addr;
@@ -1098,10 +1096,10 @@ unsigned int AGMDB_getKeyNum(struct agmdb_handler *dbm) {
10981096
timelist_cnt ++;
10991097
entry_id = *ENTRY_TIME_NEXT(entry_addr);
11001098
}
1101-
1099+
*keynum = timelist_cnt;
11021100
// Check the length of expirartion time linklist and the busy entry counter;
11031101
if(timelist_cnt == *SHM_BUSY_ENTRY_CNT(shm_base))
1104-
returntimelist_cnt;
1102+
returnAGMDB_SUCCESS;
11051103
else
11061104
return AGMDB_FAIL;
11071105
}
@@ -1122,6 +1120,45 @@ int AGMDB_getHashValue(const char* key, int key_len, int output_val_range) {
11221120
returnAGMDB_hash(key, key_len, output_val_range);
11231121
}
11241122

1123+
/**
1124+
** Get the const pointers of all keys and values in a database.
1125+
** You have to get SHARED or EXCLUSIVE LOCK of the database before calling this function.
1126+
** You should call AGMDB_getKeyNum() before to get the number of keys, then assign proper space for keys and values. Although, you can just assign a large enough space for these two arrays.
1127+
** @param dbm: the database you want to read.
1128+
** @param array_size: the maximum size of key array and value array
1129+
** @param keys: the array to store pointers of keys.
1130+
** @param values: the array to store pointers of values.
1131+
** @param vals_len: the array to store length of values.
1132+
** return: AGMDB_SUCCESS if no error
1133+
** or AGMDB_FAIL if failed.
1134+
*/
1135+
intAGMDB_getAllKeysValues(structagmdb_handler *dbm,int array_size,constchar* keys[],constchar * values[],unsignedint vals_len[]) {
1136+
CPTR_VOID shm_base;
1137+
PTR_VOID entry_addr;
1138+
PTR_OFFSET entry_id;
1139+
PTR_OFFSET* time_list_head;
1140+
int keys_counter =0;
1141+
1142+
if(dbm ==NULL)
1143+
return AGMDB_FAIL;
1144+
shm_base = (CPTR_VOID)(dbm->shm_base);
1145+
time_list_head =SHM_EXPIRE_TIME_LIST_HEAD(shm_base);
1146+
1147+
entry_id = *time_list_head;
1148+
while(entry_id != AGMDB_INVALID_INDEX){
1149+
if (keys_counter >= array_size)
1150+
return AGMDB_FAIL;
1151+
entry_addr =ENTRY_ADDR(shm_base, entry_id);
1152+
keys[keys_counter] =ENTRY_KEY(entry_addr);
1153+
values[keys_counter] =ENTRY_DATA(entry_addr);
1154+
vals_len[keys_counter] = *ENTRY_LENG(entry_addr);
1155+
entry_id = *ENTRY_TIME_NEXT(entry_addr);
1156+
keys_counter++;
1157+
}
1158+
1159+
return AGMDB_SUCCESS;
1160+
}
1161+
11251162

11261163

11271164

‎apache2/ag_mdb/ag_mdb_external.h‎

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@
55
extern"C" {
66
#endif
77

8+
/**
9+
**========================================================
10+
** AG Memory Database Limitation Definition
11+
**========================================================
12+
*/
13+
#defineAGMDB_MAX_ENTRY_SIZE 1024
14+
15+
16+
817
/**
918
**========================================================
1019
** AG Memory Database Status Definition
@@ -22,13 +31,30 @@ extern "C" {
2231
*/
2332

2433
/**
25-
** Handler of AG-MDB, which contains a block of shared momory and a set of semaphores.
34+
** The structure stores the Read/Write locks information.
35+
** In Linux, a set of semaphores is used to implement the Read/Write locks.
36+
** In Windows, The Windows Mutex is used to implement the Read/Write locks.
37+
** @param sem_id: The identifier of semaphore set in Linux.
38+
** @param read_lock_handle: The handler of the read locks in Windows.
39+
** @param write_lock_handle: The handler of the write lock in Windows.
40+
*/
41+
structagmdb_lock{
42+
#ifndef_WIN32
43+
intsem_id;
44+
#else
45+
HANDLEread_lock_handle;
46+
HANDLEwrite_lock_handle;
47+
#endif
48+
};
49+
50+
/**
51+
** Handler of AG-MDB, which contains a block of shared momory and the lock structure.
2652
** @param shm_base: The base address of the shared memory.
27-
** @paramsem_id: Theidentifier of semaphore set.
53+
** @paramdb_lock: Thestructure that stores the lock information.
2854
*/
2955
structagmdb_handler{
3056
constvoid*shm_base;
31-
void*db_lock;
57+
structagmdb_lockdb_lock;
3258
};
3359

3460
/**
@@ -169,10 +195,11 @@ int AGMDB_removeStale(struct agmdb_handler *dbm);
169195
** Get the number of keys in a database.
170196
** You have to get SHARED or EXCLUSIVE LOCK of the database before calling this function.
171197
** @param dbm: the database you want to read.
172-
** return: number of keys in the database
198+
** @param keynum: the value to store the number of keys in the database
199+
** return: AGMDB_SUCCESS if no error
173200
** or AGMDB_FAIL if failed.
174201
*/
175-
unsignedintAGMDB_getKeyNum(structagmdb_handler*dbm);
202+
intAGMDB_getKeyNum(structagmdb_handler*dbm,int*keynum);
176203

177204
/**
178205
** Use AGMDB's hash function to hash a string into an integer.
@@ -184,6 +211,20 @@ unsigned int AGMDB_getKeyNum(struct agmdb_handler *dbm);
184211
*/
185212
intAGMDB_getHashValue(constchar*key,intkey_len,inthash_nums);
186213

214+
/**
215+
** Get the const pointers of all keys and values in a database.
216+
** You have to get SHARED or EXCLUSIVE LOCK of the database before calling this function.
217+
** You should call AGMDB_getKeyNum() before to get the number of keys, then assign proper space for keys and values. Although, you can just assign a large enough space for these two arrays.
218+
** @param dbm: the database you want to read.
219+
** @param array_size: the maximum size of key array and value array
220+
** @param keys: the array to store pointers of keys.
221+
** @param values: the array to store pointers of values.
222+
** @param vals_len: the array to store length of values.
223+
** return: AGMDB_SUCCESS if no error
224+
** or AGMDB_FAIL if failed.
225+
*/
226+
intAGMDB_getAllKeysValues(structagmdb_handler*dbm,intarray_size,constchar*keys[],constchar*values[],unsignedintvals_len[]);
227+
187228
#ifdef__cplusplus
188229
}
189230
#endif

‎apache2/ag_mdb/ag_mdb_internal.h‎

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@
1212
#include<sys/ipc.h>
1313
#endif
1414
#include<cstring>
15-
#include"murmur3.h"
1615
#include<stdlib.h>
1716
#include<time.h>
1817
#include<stdio.h>
18+
#include"murmur3.h"
19+
#include"ag_mdb_external.h"
1920

2021
#ifdef__cplusplus
2122
extern"C" {
@@ -136,7 +137,7 @@ typedef unsigned long long PTR_OFFSET;
136137
#defineSHM_ENTRIES(base) ((PTR_VOID ) (base + SHM_ENTRIES_OFFSET))
137138

138139
/* SHM Default Setting */
139-
#defineDEFAULT_ENTRY_SIZE1024 // default size of each entry (1024B)
140+
#defineDEFAULT_ENTRY_SIZEAGMDB_MAX_ENTRY_SIZE // default size of each entry (1024B)
140141
#defineMAX_KEY_SIZE 128 // maximum key length (128B)
141142

142143
#defineDEFAULT_SHM_SIZE 0x40000000 // the default size of database (256MB)
@@ -324,14 +325,6 @@ int Entry_setKeyValue(PTR_OFFSET entry_id, const char* key, int key_len, const c
324325

325326
#defineAGMDB_LOCK_SUCCESS_CREATE 0
326327
#defineAGMDB_LOCK_SUCCESS_OPEN 1
327-
structAGMDB_Lock{
328-
#ifndef_WIN32
329-
intsem_id;
330-
#else
331-
HANDLEread_lock_handle;
332-
HANDLEwrite_lock_handle;
333-
#endif
334-
};
335328

336329
/**
337330
** Required by Linux.
@@ -346,36 +339,36 @@ union semun{
346339
};
347340

348341
/**
349-
** Create and initialize aAGMDB_lock, if the lock with given key exists, just link to the lock.
342+
** Create and initialize aagmdb_lock, if the lock with given key exists, just link to the lock.
350343
** The new_lock should have been created before calling this function.
351344
** @param new_lock: The pointer to save the information of the lock.
352345
** @param lock_key: The identifier of the lock.
353-
** @param lock_num: The number of atom lock in theAGMDB_lock sturcture. Default is 2 (read lock and write lock).
346+
** @param lock_num: The number of atom lock in theagmdb_lock sturcture. Default is 2 (read lock and write lock).
354347
** return: AGMDB_LOCK_SUCCESS_CREATE if successfully created a new lock,
355348
** AGMDB_LOCK_SUCCESS_OPEN if successfully link to an existed lock,
356349
** AGMDB_FAIL if failed.
357350
*/
358-
intLock_create(structAGMDB_Lock*new_lock,intlock_id,intlock_num);
351+
intLock_create(structagmdb_lock*new_lock,intlock_id,intlock_num);
359352

360353
/**
361354
** Decrease a lock's value by a given number.
362-
** @param db_lock: TheAGMDB_lock sturcture
355+
** @param db_lock: Theagmdb_lock sturcture
363356
** @param index: The index of atom lock (read or write) .
364357
** @param val: The value you want to decrease from the lock.
365358
** return: AGMDB_SUCCESS if success;
366359
** or AGMDB_FAIL if failed
367360
*/
368-
intLock_P(conststructAGMDB_Lock*db_lock,intindex,intval);
361+
intLock_P(conststructagmdb_lock*db_lock,intindex,intval);
369362

370363
/**
371364
** Increase a lock's value by a given number.
372-
** @param db_lock: TheAGMDB_lock sturcture
365+
** @param db_lock: Theagmdb_lock sturcture
373366
** @param index: The index of atom lock (read or write) .
374367
** @param val: The value you want to add to the lock.
375368
** return: AGMDB_SUCCESS if success;
376369
** or AGMDB_FAIL if failed
377370
*/
378-
intLock_V(conststructAGMDB_Lock*db_lock,intindex,intval);
371+
intLock_V(conststructagmdb_lock*db_lock,intindex,intval);
379372

380373
/**
381374
**========================================================

‎apache2/ag_mdb/murmur3.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
//-----------------------------------------------------------------------------
1313
// Platform-specific functions and macros
1414

15-
#ifdef__GNUC__
15+
#ifdef_MSC_VER
16+
#defineFORCE_INLINE __forceinline
17+
#elif defined(__GNUC__)
1618
#defineFORCE_INLINE __attribute__((always_inline)) inline
1719
#else
1820
#defineFORCE_INLINE inline

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp