simpledb.index.hash
Class HashIndex

java.lang.Object
  extended by simpledb.index.hash.HashIndex
All Implemented Interfaces:
Index

public class HashIndex
extends Object
implements Index

A static hash implementation of the Index interface. A fixed number of buckets is allocated (currently, 100), and each bucket is implemented as a file of index records.

Author:
Edward Sciore

Field Summary
static int NUM_BUCKETS
           
 
Constructor Summary
HashIndex(String idxname, Schema sch, Transaction tx)
          Opens a hash index for the specified index.
 
Method Summary
 void beforeFirst(Constant searchkey)
          Positions the index before the first index record having the specified search key.
 void close()
          Closes the index by closing the current table scan.
 void delete(Constant val, RID rid)
          Deletes the specified record from the table scan for the bucket.
 RID getDataRid()
          Retrieves the dataRID from the current record in the table scan for the bucket.
 void insert(Constant val, RID rid)
          Inserts a new record into the table scan for the bucket.
 boolean next()
          Moves to the next record having the search key.
static int searchCost(int numblocks, int rpb)
          Returns the cost of searching an index file having the specified number of blocks.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NUM_BUCKETS

public static int NUM_BUCKETS
Constructor Detail

HashIndex

public HashIndex(String idxname,
                 Schema sch,
                 Transaction tx)
Opens a hash index for the specified index.

Parameters:
idxname - the name of the index
sch - the schema of the index records
tx - the calling transaction
Method Detail

beforeFirst

public void beforeFirst(Constant searchkey)
Positions the index before the first index record having the specified search key. The method hashes the search key to determine the bucket, and then opens a table scan on the file corresponding to the bucket. The table scan for the previous bucket (if any) is closed.

Specified by:
beforeFirst in interface Index
Parameters:
searchkey - the search key value.
See Also:
Index.beforeFirst(simpledb.query.Constant)

next

public boolean next()
Moves to the next record having the search key. The method loops through the table scan for the bucket, looking for a matching record, and returning false if there are no more such records.

Specified by:
next in interface Index
Returns:
false if no other index records have the search key.
See Also:
Index.next()

getDataRid

public RID getDataRid()
Retrieves the dataRID from the current record in the table scan for the bucket.

Specified by:
getDataRid in interface Index
Returns:
the dataRID stored in the current index record.
See Also:
Index.getDataRid()

insert

public void insert(Constant val,
                   RID rid)
Inserts a new record into the table scan for the bucket.

Specified by:
insert in interface Index
Parameters:
val - the dataval in the new index record.
rid - the dataRID in the new index record.
See Also:
Index.insert(simpledb.query.Constant, simpledb.record.RID)

delete

public void delete(Constant val,
                   RID rid)
Deletes the specified record from the table scan for the bucket. The method starts at the beginning of the scan, and loops through the records until the specified record is found.

Specified by:
delete in interface Index
Parameters:
val - the dataval of the deleted index record
rid - the dataRID of the deleted index record
See Also:
Index.delete(simpledb.query.Constant, simpledb.record.RID)

close

public void close()
Closes the index by closing the current table scan.

Specified by:
close in interface Index
See Also:
Index.close()

searchCost

public static int searchCost(int numblocks,
                             int rpb)
Returns the cost of searching an index file having the specified number of blocks. The method assumes that all buckets are about the same size, and so the cost is simply the size of the bucket.

Parameters:
numblocks - the number of blocks of index records
rpb - the number of records per block (not used here)
Returns:
the cost of traversing the index


Copyright © 2011. All Rights Reserved.