View Javadoc

1   package net.obsearch.storage.tc;
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.OBException;
25  import net.obsearch.exception.OBStorageException;
26  import net.obsearch.storage.OBStorageConfig;
27  import net.obsearch.storage.OBStoreLong;
28  import net.obsearch.storage.TupleLong;
29  
30  import com.sleepycat.bind.tuple.LongBinding;
31  
32  import com.sleepycat.je.DatabaseEntry;
33  
34  import com.sleepycat.bind.tuple.TupleOutput;
35  import com.sleepycat.bind.tuple.TupleInput;
36  
37  import com.sleepycat.je.Database;
38  import com.sleepycat.je.DatabaseException;
39  import com.sleepycat.je.OperationStatus;
40  import java.nio.ByteBuffer;
41  
42  import tokyocabinet.DBM;
43  import net.obsearch.storage.OBStoreFactory;
44  
45  /**
46   * BDBOBStoreLong is a wrapper for Berkeley indexes that assumes that keys are
47   * longs and values are byte[].
48   * 
49   * @author Arnoldo Jose Muller Molina
50   */
51  
52  public final class TCOBStorageLong extends AbstractTCOBStorage<TupleLong>
53  		implements OBStoreLong {
54  
55  	/**
56  	 * Builds a new Storage system by receiving a Berkeley DB database that uses
57  	 * longs as a primary indexing method.
58  	 * 
59  	 * @param db
60  	 *            The database to be stored.
61  	 * @param seq
62  	 *            Sequences database.
63  	 * @param name
64  	 *            Name of the database.
65  	 * @throws OBException 
66  	 * @throws OBStorageException 
67  	 * @throws DatabaseException
68  	 *             if something goes wrong with the database.
69  	 */
70  	public TCOBStorageLong(String name, DBM db, OBStoreFactory fact,
71  			OBStorageConfig storageConf) throws OBStorageException, OBException {
72  		super(name, db, fact, storageConf);
73  	}
74  
75  	public net.obsearch.OperationStatus delete(long key)
76  			throws OBStorageException {
77  		return super.delete(getBytes(key));
78  	}
79  
80  	/**
81  	 * Converts the given value to an array of bytes.
82  	 * 
83  	 * @param value
84  	 *            the value to be converted.
85  	 * @return An array of bytes with the given value encoded.
86  	 */
87  	private byte[] getBytes(long value) {
88  		return fact.serializeLong(value);
89  	}
90  
91  	/**
92  	 * Converts the value of the given entry into its primitive type.
93  	 * 
94  	 * @param entry
95  	 *            The place where we will put the entry.
96  	 */
97  	public long bytesToValue(byte[] entry) {
98  		return fact.deSerializeLong(entry);
99  	}
100 
101 	public byte[] getValue(long key) throws IllegalArgumentException,
102 			OBStorageException {
103 		return super.getValue(getBytes(key));
104 	}
105 
106 	public net.obsearch.OperationStatus put(long key, byte[] value)
107 			throws IllegalArgumentException, OBStorageException {
108 		return super.put(getBytes(key), value);
109 	}
110 
111 	public CloseIterator<TupleLong> processRange(long low, long high) {
112 		throw new IllegalArgumentException();		
113 	}
114 
115 	public CloseIterator<TupleLong> processRangeNoDup(long low, long high)
116 			throws OBStorageException {
117 		throw new IllegalArgumentException();
118 		// return new LongIterator(low, high, false,false);
119 	}
120 
121 	public CloseIterator<TupleLong> processRangeReverse(long low, long high)
122 			throws OBStorageException {
123 		throw new IllegalArgumentException();
124 		// return new LongIterator(low, high,true,true);
125 	}
126 
127 	public CloseIterator<TupleLong> processRangeReverseNoDup(long low, long high)
128 			throws OBStorageException {
129 		throw new IllegalArgumentException();
130 		// return new LongIterator();
131 	}
132 
133 	public CloseIterator<TupleLong> processAll() throws OBStorageException {
134 		return new LongIterator();
135 	}
136 
137 	/**
138 	 * Iterator used to process range results.
139 	 */
140 	/*
141 	 * TODO: I am leaving the closing of the cursor to the last iteration or the
142 	 * finalize method (whichever happens first). We should test if this is ok,
143 	 * or if there is an issue with this because Berkeley's iterator explicitly
144 	 * have a "close" method.
145 	 */
146 	final class LongIterator extends CursorIterator<TupleLong> {
147 
148 		private LongIterator() throws OBStorageException {
149 			super();
150 		}
151 
152 		protected TupleLong createTuple(byte[] key, byte[] value) {
153 			return new TupleLong(bytesToValue(key), value);
154 		}
155 	}
156 
157 	@Override
158 	public byte[] prepareBytes(byte[] in) {
159 		return in;
160 	}
161 }