adding test scripts
[csql.git] / src / storage / Util.cxx
blob984ec95d0013f54123b62eb92fd8764b2460ade8
1 #include<Util.h>
2 #include<Config.h>
4 unsigned int Util::hashBinary(char *strVal, int length)
6 unsigned int hval, g;
7 hval = 0;
8 char *str =strVal;
9 int iter = 0;
10 while (iter != length)
12 hval <<= 4;
13 hval += (unsigned int) *str++;
14 g = hval & ((unsigned int) 0xf << (32 - 4));
15 if (g != 0)
17 hval ^= g >> (32 - 8);
18 hval ^= g;
20 iter++;
22 return hval;
25 unsigned int Util::hashString(char *strVal)
27 unsigned int hval, g;
28 hval = 0;
29 char *str =strVal;
30 while (*str != '\0')
32 hval <<= 4;
33 hval += (unsigned int) *str++;
34 g = hval & ((unsigned int) 0xf << (32 - 4));
35 if (g != 0)
37 hval ^= g >> (32 - 8);
38 hval ^= g;
41 return hval;
44 DbRetVal GlobalUniqueID::create()
46 int key = Conf::config.getShmIDKey();
47 int id = os::shm_create(key, MAX_UNIQUE_ID *sizeof(int), 0666);
48 if (-1 == id) {
49 if (errno != EEXIST)
50 printError(ErrOS, "Unable to create shared memory");
51 return ErrOS;
53 ptr = os::shm_attach(id, NULL, 0);
54 if ((void*)-1 == ptr) {
55 printError(ErrOS, "Unable to attach shared memory");
56 return ErrOS;
58 memset(ptr, 0, MAX_UNIQUE_ID *sizeof(int));
61 DbRetVal GlobalUniqueID::open()
63 if (ptr != NULL) return OK;
64 int key = Conf::config.getShmIDKey();
65 int id = os::shm_open(key, MAX_UNIQUE_ID *sizeof(int), 0666);
66 if (-1 == id) {
67 printError(ErrOS, "Unable to create shared memory");
69 ptr = os::shm_attach(id, NULL, 0);
70 if ((void*)-1 == ptr) {
71 printError(ErrOS, "Unable to create shared memory");
73 return OK;
76 DbRetVal GlobalUniqueID::destroy()
78 int key = Conf::config.getShmIDKey();
79 int id = os::shm_open(key, MAX_UNIQUE_ID *sizeof(int), 0666);
80 if (-1 == id) {
81 // printError(ErrOS, "Unable to open shared memory");
82 return ErrOS;
84 os::shmctl(id, IPC_RMID);
87 int GlobalUniqueID::getID(UniqueIDType type)
89 InUse *id = (int*)(((char*)ptr) + sizeof(int) * type);
90 InUse oldVal = *id;
91 InUse newVal = oldVal + 1;
92 int ret = Mutex::CASGen(id, oldVal, newVal);
93 if (ret) return -1;
94 return *id;