Update in sync with enterprise version.
[csql.git] / src / storage / BucketList.cxx
blobb2e7ad6189a0ad5961e7c79c9c13101d835af9f6
1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 ***************************************************************************/
16 #include<Index.h>
17 #include<Allocator.h>
18 #include<Database.h>
19 #include<Debug.h>
20 DbRetVal BucketList::insert(Chunk *chunk, Database *db, void *key, void*tuple)
22 DbRetVal rv = OK;
23 HashIndexNode *newNode;// (HashIndexNode*) chunk->allocate(db, &rv);
24 int tries=0;
25 int totalTries = Conf::config.getMutexRetries();
26 while (tries < totalTries)
28 rv = OK;
29 newNode= (HashIndexNode*) chunk->allocate(db, &rv);
30 if (newNode !=NULL) break;
31 if (rv != ErrLockTimeOut)
33 printError(rv, "Unable to allocate hash index node");
34 return rv;
36 //printError (ErrWarning, "Hash Node Alloc: LockTimeOut Retry:%d", tries);
37 tries++;
39 if (newNode == NULL){
40 printError(rv, "Unable to allocate hash index node after %d retry", Conf::config.getMutexRetries());
41 return rv;
43 printDebug(DM_HashIndex,"Hash Index node allocated:%x", newNode);
44 newNode->ptrToKey_ = key;
45 newNode->ptrToTuple_ = tuple;
46 newNode->next_ = NULL;
48 //If this is the first node, set it as head
49 if (NULL == head)
51 printDebug(DM_HashIndex, "BucketList:insert head is null key:%x",key);
52 //head = newNode;
53 if ( 0 != Mutex::CASL((long*)&head, 0, (long)newNode)) {
54 printError(ErrLockTimeOut, "Unable to set bucket head..retry\n");
55 chunk->free(db, newNode);
56 return ErrLockTimeOut;
58 return OK;
61 HashIndexNode *it = head;
62 while (NULL != it->next_) it = it->next_;
63 //it->next_ = newNode;
64 if ( 0 != Mutex::CASL((long*)&it->next_, 0, (long)newNode)) {
65 printError(ErrLockTimeOut, "Unable to add to bucket..retry\n");
66 chunk->free(db, newNode);
67 return ErrLockTimeOut;
69 printDebug(DM_HashIndex, "BucketList:insert adding it to the end of list key:%x", key);
70 if (rv != OK) printError(ErrSysFatal, "rv is not OK %d\n", rv);
71 return rv;
73 //Returns 2 if the head itself is removed.
74 DbRetVal BucketList::remove(Chunk *chunk, Database *db, void *keyPtr)
76 if (NULL == head) return ErrNotFound;
77 HashIndexNode *ite = head, *prev = head;
78 while (ite != NULL)
80 if (ite->ptrToKey_ == keyPtr)
82 if ( ite == head) {
83 //head = ite->next_;
84 if ( 0 != Mutex::CASL((long*)&head, (long)ite,
85 (long)ite->next_)) {
86 printError(ErrLockTimeOut, "Unable to set bucket head..retry");
87 return ErrLockTimeOut;
89 chunk->free(db, ite);
90 return SplCase;
92 //prev->next_ = ite->next_;
93 if ( 0 != Mutex::CASL((long*)&prev->next_, (long)prev->next_, (long)ite->next_)) {
94 printError(ErrLockTimeOut, "Unable to remove hash bucket node..retry");
95 return ErrLockTimeOut;
97 chunk->free(db, ite);
98 return OK;
100 prev = ite;
101 ite = ite->next_;
103 printError(ErrNotFound, "Node not found in the bucket list");
104 printStackTrace();
105 return ErrNotFound;