code reorg
[csql.git] / src / storage / BucketList.cxx
blob173b697c5e1f6c4c520242a23672a4f5e1b7d8bb
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 = NULL;
25 rv = OK;
26 newNode= (IndexNode*) chunk->tryAllocate(db, &rv);
27 if (newNode == NULL){
28 printError(rv, "Unable to allocate hash index node after %d retry", Conf::config.getMutexRetries());
29 return rv;
31 printDebug(DM_HashIndex,"Hash Index node allocated:%x", newNode);
32 newNode->ptrToKey_ = key;
33 newNode->ptrToTuple_ = tuple;
34 newNode->next_ = NULL;
36 //If this is the first node, set it as head
37 if (NULL == head)
39 printDebug(DM_HashIndex, "BucketList:insert head is null key:%x",key);
40 //head = newNode;
41 if ( 0 != Mutex::CASL((long*)&head, 0, (long)newNode)) {
42 printError(ErrLockTimeOut, "Unable to set bucket head..retry\n");
43 chunk->free(db, newNode);
44 return ErrLockTimeOut;
46 return OK;
49 IndexNode *it = head;
50 while (NULL != it->next_) it = it->next_;
51 //it->next_ = newNode;
52 if ( 0 != Mutex::CASL((long*)&it->next_, 0, (long)newNode)) {
53 printError(ErrLockTimeOut, "Unable to add to bucket..retry\n");
54 chunk->free(db, newNode);
55 return ErrLockTimeOut;
57 printDebug(DM_HashIndex, "BucketList:insert adding it to the end of list key:%x", key);
58 if (rv != OK) printError(ErrSysFatal, "rv is not OK %d\n", rv);
59 return rv;
62 void BucketList::print()
64 if (NULL == head) return ;
65 IndexNode *ite = head, *prev = head;
66 while (ite != NULL)
68 printf( "%d ", *((int*)ite->ptrToKey_));
69 ite = ite->next_;
71 return;
74 //Returns 2 if the head itself is removed.
75 DbRetVal BucketList::remove(Chunk *chunk, Database *db, void *keyPtr)
77 if (NULL == head) return ErrNotFound;
78 IndexNode *ite = head, *prev = head;
79 while (ite != NULL)
81 if (ite->ptrToKey_ == keyPtr)
83 if ( ite == head) {
84 //head = ite->next_;
85 if ( 0 != Mutex::CASL((long*)&head, (long)ite,
86 (long)ite->next_)) {
87 printError(ErrLockTimeOut, "Unable to set bucket head..retry");
88 return ErrLockTimeOut;
90 chunk->free(db, ite);
91 return SplCase;
93 //prev->next_ = ite->next_;
94 if ( 0 != Mutex::CASL((long*)&prev->next_, (long)prev->next_, (long)ite->next_)) {
95 printError(ErrLockTimeOut, "Unable to remove hash bucket node..retry");
96 return ErrLockTimeOut;
98 chunk->free(db, ite);
99 return OK;
101 prev = ite;
102 ite = ite->next_;
104 printError(ErrNotFound, "Node not found in the bucket list");
105 printStackTrace();
106 return ErrNotFound;