code reorg
[csql.git] / src / storage / BucketList.cxx
blobb4af334a5eba1140e7665223f2234fc87f468291
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<os.h>
17 #include<Index.h>
18 #include<Allocator.h>
19 #include<Database.h>
20 #include<Debug.h>
21 DbRetVal BucketList::insert(Chunk *chunk, Database *db, void *key, void*tuple)
23 DbRetVal rv = OK;
24 IndexNode *newNode;// (IndexNode*) chunk->allocate(db, &rv);
25 int tries=0;
26 int totalTries = Conf::config.getMutexRetries();
27 while (tries < totalTries)
29 rv = OK;
30 newNode= (IndexNode*) chunk->allocate(db, &rv);
31 if (newNode !=NULL) break;
32 if (rv != ErrLockTimeOut)
34 printError(rv, "Unable to allocate hash index node");
35 return rv;
37 //printError (ErrWarning, "Hash Node Alloc: LockTimeOut Retry:%d", tries);
38 tries++;
40 if (newNode == NULL){
41 printError(rv, "Unable to allocate hash index node after %d retry", Conf::config.getMutexRetries());
42 return rv;
44 printDebug(DM_HashIndex,"Hash Index node allocated:%x", newNode);
45 newNode->ptrToKey_ = key;
46 newNode->ptrToTuple_ = tuple;
47 newNode->next_ = NULL;
49 //If this is the first node, set it as head
50 if (NULL == head)
52 printDebug(DM_HashIndex, "BucketList:insert head is null key:%x",key);
53 //head = newNode;
54 if ( 0 != Mutex::CASL((long*)&head, 0, (long)newNode)) {
55 printError(ErrLockTimeOut, "Unable to set bucket head..retry\n");
56 chunk->free(db, newNode);
57 return ErrLockTimeOut;
59 return OK;
62 IndexNode *it = head;
63 while (NULL != it->next_) it = it->next_;
64 //it->next_ = newNode;
65 if ( 0 != Mutex::CASL((long*)&it->next_, 0, (long)newNode)) {
66 printError(ErrLockTimeOut, "Unable to add to bucket..retry\n");
67 chunk->free(db, newNode);
68 return ErrLockTimeOut;
70 printDebug(DM_HashIndex, "BucketList:insert adding it to the end of list key:%x", key);
71 if (rv != OK) printError(ErrSysFatal, "rv is not OK %d\n", rv);
72 return rv;
74 void BucketList::print()
76 if (NULL == head) return ;
77 IndexNode *ite = head, *prev = head;
78 while (ite != NULL)
80 printf( "%d ", *((int*)ite->ptrToKey_));
81 ite = ite->next_;
83 return;
85 //Returns 2 if the head itself is removed.
86 DbRetVal BucketList::remove(Chunk *chunk, Database *db, void *keyPtr)
88 if (NULL == head) return ErrNotFound;
89 IndexNode *ite = head, *prev = head;
90 while (ite != NULL)
92 if (ite->ptrToKey_ == keyPtr)
94 if ( ite == head) {
95 //head = ite->next_;
96 if ( 0 != Mutex::CASL((long*)&head, (long)ite,
97 (long)ite->next_)) {
98 printError(ErrLockTimeOut, "Unable to set bucket head..retry");
99 return ErrLockTimeOut;
101 chunk->free(db, ite);
102 return SplCase;
104 //prev->next_ = ite->next_;
105 if ( 0 != Mutex::CASL((long*)&prev->next_, (long)prev->next_, (long)ite->next_)) {
106 printError(ErrLockTimeOut, "Unable to remove hash bucket node..retry");
107 return ErrLockTimeOut;
109 chunk->free(db, ite);
110 return OK;
112 prev = ite;
113 ite = ite->next_;
115 printError(ErrNotFound, "Node not found in the bucket list");
116 printStackTrace();
117 return ErrNotFound;