1 package net.obsearch.index.bucket;
2
3 import java.io.IOException;
4 import java.nio.ByteBuffer;
5 import java.util.List;
6
7 import net.obsearch.OB;
8 import net.obsearch.OperationStatus;
9 import net.obsearch.Status;
10 import net.obsearch.exception.IllegalIdException;
11 import net.obsearch.exception.OBException;
12 import net.obsearch.filter.Filter;
13 import net.obsearch.index.utils.IntegerHolder;
14 import net.obsearch.query.AbstractOBQuery;
15 import net.obsearch.stats.Statistics;
16
17 import com.sleepycat.je.DatabaseException;
18
19 /*
20 OBSearch: a distributed similarity search engine This project is to
21 similarity search what 'bit-torrent' is to downloads.
22 Copyright (C) 2008 Arnoldo Jose Muller Molina
23
24 This program is free software: you can redistribute it and/or modify
25 it under the terms of the GNU General Public License as published by
26 the Free Software Foundation, either version 3 of the License, or
27 (at your option) any later version.
28
29 This program is distributed in the hope that it will be useful,
30 but WITHOUT ANY WARRANTY; without even the implied warranty of
31 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 GNU General Public License for more details.
33
34 You should have received a copy of the GNU General Public License
35 along with this program. If not, see <http://www.gnu.org/licenses/>.
36 */
37
38 /**
39 * A BucketContainer stores SMAP vectors of objects. It is possible to search,
40 * add and remove objects stored here.
41 *
42 * @param <O>
43 * OB object.
44 * @param <B>
45 * The bucket that will be employed.
46 * @author Arnoldo Jose Muller Molina
47 */
48 public interface BucketContainer<O extends OB, B extends BucketObject, Q> {
49
50 /**
51 * Deletes the given object from this {@link BucketContainer}.
52 *
53 * @param bucket
54 * This will should match this bucket's id. Used to pass
55 * additional information such as the SMAP vector
56 * @param object
57 * The object that will be deleted.
58 * @return {@link net.obsearch.Status#OK} and the deleted object's id if the
59 * object was found and successfully deleted.
60 * {@link net.obsearch.Status#NOT_EXISTS} if the object is not in
61 * the database.
62 */
63 OperationStatus delete(B bucket, O object) throws OBException,
64 IllegalIdException, IllegalAccessException, InstantiationException;
65
66 /**
67 * Inserts the given object with the given bucket details to this bucket.
68 * Warning: This method assumes that object does not exist in the DB. In
69 * bucket, an id will be provided by the caller.
70 *
71 * @param bucket
72 * This will should match this bucket's id. Used to pass
73 * additional information such as the SMAP vector.
74 * @return If {@link net.obsearch.Status#OK} or
75 * {@link net.obsearch.Status#EXISTS} then the result will hold the
76 * id of the inserted object and the operation is successful.
77 */
78 OperationStatus insert(B bucket, O object
79 ) throws OBException, IllegalIdException,
80 IllegalAccessException, InstantiationException;
81
82 /**
83 * Inserts the given object with the given bucket details to this bucket.
84 * No existence checks are performed, we believe the caller
85 * each object is unique or the caller does not care about
86 * uniqueness.
87 *
88 * @param bucket
89 * This will should match this bucket's id. Used to pass
90 * additional information such as the SMAP vector.
91 * @return {@link net.obsearch.Status#OK}
92 *
93 */
94 OperationStatus insertBulk(B bucket, O object
95 ) throws OBException, IllegalIdException,
96 IllegalAccessException, InstantiationException;
97
98 /**
99 * Return the object list!
100 * @return
101 */
102 List<B> getObjects();
103
104 /**
105 * Returns true if the object and its bucket definition exist in this
106 * container
107 *
108 * @param bucket
109 * The bucket associated to object
110 * @param object
111 * The object that will be inserted
112 * @return true if object exists in this container.
113 * @throws OBException
114 * @throws DatabaseException
115 * @throws IllegalIdException
116 * @throws IllegalAccessException
117 * @throws InstantiationException
118 * @return {@link net.obsearch.Status#EXISTS} and the object's id if the
119 * object exists in the database, otherwise
120 * {@link net.obsearch.Status#NOT_EXISTS} is returned.
121 */
122 OperationStatus exists(B bucket, O object) throws OBException,
123 IllegalIdException, IllegalAccessException, InstantiationException;
124
125
126 /**
127 * Match the given query and bucket but only for one record found in b.
128 *
129 * @param query
130 * The search parameters (range, priority queue with the closest
131 * elements)
132 * @param bucket
133 * The bucket of the given object.
134 * @param object
135 * The object that will be searched.
136 * @param filter Filter to be employed.
137 * @return # of distance computations executed.
138 */
139 void search(Q query, B bucket, ByteBuffer b, Filter<O> filter, Statistics stats) throws IllegalAccessException,
140 OBException, InstantiationException,
141 IllegalIdException;
142
143
144
145 /**
146 * Searches the given object with the given searchContainer parameters. The
147 * searchContainer will be updated as necessary.
148 *
149 * @param query
150 * The search parameters (range, priority queue with the closest
151 * elements)
152 * @param bucket
153 * The bucket of the given object.
154 * @param object
155 * The object that will be searched.
156 * @param filter Filter to be employed.
157 * @return # of distance computations executed.
158 */
159 void search(Q query, B bucket, Filter<O> filter, Statistics stats) throws IllegalAccessException,
160 OBException, InstantiationException,
161 IllegalIdException;
162
163
164 /**
165 * Same as {@link #search(AbstractOBQuery, BucketObject, Filter, Statistics)} but
166 * it forces the query AbstractOBQuery<O> into the correct type.
167 * @param q
168 * @param bucket
169 * @param stats
170 * @throws IllegalAccessException
171 * @throws DatabaseException
172 * @throws OBException
173 * @throws InstantiationException
174 * @throws IllegalIdException
175 */
176 void search(AbstractOBQuery<O> q, B bucket, Filter<O> filter, Statistics stats) throws IllegalAccessException,
177 OBException, InstantiationException,
178 IllegalIdException;
179
180 /**
181 * # of objects in this container.
182 *
183 * @return The # of objects in this container.
184 */
185 int size() throws OBException;
186
187 /**
188 * # of pivots for this container.
189 *
190 * @return
191 */
192 int getPivots();
193
194 /**
195 * Sets the # of pivots for this container.
196 */
197 void setPivots(int pivots);
198
199 /**
200 * Sets the key (bucket id) of a bucket container
201 * @param key
202 */
203 void setKey(byte[] key);
204
205
206 /**
207 * Serialize the bucket
208 */
209 byte[] serialize() throws OBException;
210
211 /**
212 * Return true if the bucket has been modified since it was instantiated.
213 * @return true if the bucket has been modified since it was instantiated.
214 */
215 boolean isModified();
216 }