1 package org.ajmm.obsearch.example;
2
3 import java.util.Arrays;
4
5 import org.ajmm.obsearch.OB;
6 import org.ajmm.obsearch.exception.OBException;
7 import org.ajmm.obsearch.ob.OBInt;
8 import org.ajmm.obsearch.ob.OBShort;
9
10 import com.sleepycat.bind.tuple.TupleInput;
11 import com.sleepycat.bind.tuple.TupleOutput;
12
13 /*
14 OBSearch: a distributed similarity search engine
15 This project is to similarity search what 'bit-torrent' is to downloads.
16 Copyright (C) 2007 Arnoldo Jose Muller Molina
17
18 This program is free software: you can redistribute it and/or modify
19 it under the terms of the GNU General Public License as published by
20 the Free Software Foundation, either version 3 of the License, or
21 (at your option) any later version.
22
23 This program is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 GNU General Public License for more details.
27
28 You should have received a copy of the GNU General Public License
29 along with this program. If not, see <http://www.gnu.org/licenses/>.
30 */
31 /**
32 * Example Object. Store many shorts into a vector and use 1-norm distance on
33 * them.
34 * @author Arnoldo Jose Muller Molina.
35 * @since 0.7
36 */
37
38 public class OBVectorExample implements OBShort {
39
40 /**
41 * Total number of elements to store.
42 */
43 private static final int VECTOR_SIZE = 20000;
44
45 /**
46 * 1) Actual data.
47 */
48 private short[] data;
49
50 /**
51 * 2) Default constructor is required by OBSearch.
52 */
53 public OBVectorExample() {
54 data = new short[VECTOR_SIZE];
55 }
56
57 /**
58 * Additional constructors can be created to make your life easier.
59 * (OBSearch does not use them)
60 */
61 public OBVectorExample(short[] data) {
62 assert data.length == VECTOR_SIZE;
63 this.data = data;
64 }
65
66 /**
67 * 3) 1-norm distance function. A casting error can happen here, but we
68 * don't check it for efficiency reasons.
69 * @param object
70 * The object to compare.
71 * @return The distance between this and object.
72 * @throws OBException
73 * if something goes wrong. But nothing should be wrong in this
74 * function.
75 */
76 public final short distance(final OBShort object) throws OBException {
77 OBVectorExample o = (OBVectorExample) object;
78 short res = 0;
79 int i = 0;
80 while (i < VECTOR_SIZE) {
81 res += Math.abs(data[i] - o.data[i]);
82 i++;
83 }
84 return res;
85 }
86
87 /**
88 * 4) Load method. Loads the data into this object. This is analogous to
89 * object deserialization.
90 * @param in
91 * Byte Stream with all the data that has to be loaded into this
92 * object.
93 */
94 public final void load(final TupleInput in) throws OBException {
95 int i = 0;
96 while (i < VECTOR_SIZE) {
97 // read a short from the stream and
98 // store it into our array.
99 data[i] = in.readShort();
100 i++;
101 }
102 }
103
104 /**
105 * 5) Store method. Write the contents of the object into out. Analogous to
106 * Java's object serialization.
107 * @param out
108 * Stream where we will store this object.
109 */
110 public final void store(TupleOutput out) {
111 int i = 0;
112 while (i < VECTOR_SIZE) {
113 // write each short into
114 // the stream
115 out.writeShort(data[i]);
116 i++;
117 }
118 }
119
120 /**
121 * 6) Equals method. Implementation of the equals method is required. A
122 * casting error can happen here, but we don't check it for efficiency
123 * reasons.
124 * @param object
125 * The object to compare.
126 * @return true if this and object are equal.
127 */
128 public final boolean equals(final Object object) {
129 OBVectorExample o = (OBVectorExample) object;
130 return Arrays.equals(data, o.data);
131 }
132
133 /**
134 * 7) Hash code method. This method is required too.
135 * @return The hash code of the data array.
136 */
137 public final int hashCode() {
138 // TODO: this value should be cached.
139 return Arrays.hashCode(data);
140 }
141
142 }