View Javadoc

1   package net.obsearch.storage.bdb;
2   
3   /*
4   		OBSearch: a distributed similarity search engine This project is to
5    similarity search what 'bit-torrent' is to downloads. 
6       Copyright (C) 2008 Arnoldo Jose Muller Molina
7   
8     	This program is free software: you can redistribute it and/or modify
9       it under the terms of the GNU General Public License as published by
10      the Free Software Foundation, either version 3 of the License, or
11      (at your option) any later version.
12  
13      This program is distributed in the hope that it will be useful,
14      but WITHOUT ANY WARRANTY; without even the implied warranty of
15      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16      GNU General Public License for more details.
17  
18      You should have received a copy of the GNU General Public License
19      along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21  import java.util.Iterator;
22  import java.util.NoSuchElementException;
23  import net.obsearch.storage.CloseIterator;
24  import net.obsearch.exception.OBStorageException;
25  import net.obsearch.storage.OBStoreShort;
26  import net.obsearch.storage.TupleShort;
27  
28  import com.sleepycat.bind.tuple.ShortBinding;
29  
30  import com.sleepycat.je.DatabaseEntry;
31  
32  import com.sleepycat.bind.tuple.TupleOutput;
33  import com.sleepycat.bind.tuple.TupleInput;
34  
35  import com.sleepycat.je.Database;
36  import com.sleepycat.je.DatabaseException;
37  import com.sleepycat.je.OperationStatus;
38  import java.nio.ByteBuffer;
39  import net.obsearch.storage.OBStoreFactory;
40  
41  /** 
42  	*  BDBOBStoreShort is a wrapper for Berkeley indexes that assumes
43  	*  that keys are shorts and values are byte[].
44  	*  
45    *  @author      Arnoldo Jose Muller Molina    
46    */
47  
48  public final class BDBOBStoreJeShort
49          extends AbstractBDBOBStoreJe<TupleShort> implements OBStoreShort {
50  
51      /**
52       * Builds a new Storage system by receiving a Berkeley DB database that uses
53       * shorts as a primary indexing method.
54       * @param db
55       *                The database to be stored.
56  		 * @param seq     Sequences database.
57       * @param name
58       *                Name of the database.
59       * @throws DatabaseException
60       *                 if something goes wrong with the database.
61       */
62  						public BDBOBStoreJeShort(String name, Database db, Database seq, OBStoreFactory fact, boolean duplicates) throws DatabaseException {
63  								super(name, db, seq, fact, duplicates);
64      }
65  
66      public net.obsearch.OperationStatus delete(short key) throws OBStorageException {
67          return super.delete(getBytes(key));
68      }
69  
70      /**
71       * Converts the given value to an array of bytes.
72       * @param value
73       *                the value to be converted.
74       * @return An array of bytes with the given value encoded.
75       */
76      private byte[] getBytes(short value) {
77          return BDBFactoryJe.shortToBytes(value);
78      }
79  
80  
81  		/**
82       * Converts the value of the given entry into its primitive type.
83       * @param entry
84       *                The place where we will put the entry.
85       */
86      public short bytesToValue(byte[] entry) {
87          //TupleInput in = new TupleInput(entry);
88  				//return in.readShort();
89  				DatabaseEntry e = new DatabaseEntry(entry);
90          return ShortBinding.entryToShort(e);        
91      }
92  
93      public byte[] getValue(short key) throws IllegalArgumentException,
94              OBStorageException {
95          return super.getValue(getBytes(key));
96      }
97  
98      public net.obsearch.OperationStatus put(short key, byte[] value) throws IllegalArgumentException,
99              OBStorageException {
100         return super.put(getBytes(key), value);
101     }
102 
103     public CloseIterator < TupleShort > processRange(short low, short high)
104             throws OBStorageException {
105         return new ShortIterator(low, high);
106     }
107 
108 		public CloseIterator < TupleShort > processRangeNoDup(short low, short high)
109             throws OBStorageException {
110         return new ShortIterator(low, high, false,false);
111     }
112 
113 		public CloseIterator < TupleShort > processRangeReverse(short low, short high)
114             throws OBStorageException {
115         return new ShortIterator(low, high,true,true);
116     }
117 
118 		public CloseIterator < TupleShort > processRangeReverseNoDup(short low, short high)
119             throws OBStorageException {
120         return new ShortIterator(low, high,true,false);
121     }
122 
123 		public CloseIterator < TupleShort > processAll()
124             throws OBStorageException {
125         return new ShortIterator();
126     }
127 
128     /**
129      * Iterator used to process range results.
130      */
131     /*
132      * TODO: I am leaving the closing of the cursor to the last iteration or the
133      * finalize method (whichever happens first). We should test if
134      * this is ok, or if there is an issue with this because
135      * Berkeley's iterator explicitly have a "close" method.
136      */
137     final class ShortIterator extends CursorIterator < TupleShort > {
138         
139        
140         private ShortIterator(short min, short max) throws OBStorageException {
141 						super(getBytes(min), getBytes(max));
142         }
143 
144 				/**
145 				 * Creates a new ShortIterator given a min range, max range and
146 				 * a flag saying if this iterator will go "forward" or "backwards"
147 				 */
148 				private ShortIterator(short min, short max, boolean reverseMode, boolean dups) throws OBStorageException {
149 						super(getBytes(min), getBytes(max), false, reverseMode, dups);
150         }
151 
152 				private ShortIterator() throws OBStorageException {
153 						super(null, null,true, false,true);
154         }
155 
156 				protected TupleShort createTuple(byte[] key, byte[] value) {
157             return new TupleShort(bytesToValue(key),value);
158         }
159     }
160 }
161 
162 
163