Skip to content

Commit 4caad0c

Browse files
authored
Fixes #916 (#918)
* Fixes #916 * changelog updated
1 parent b09e663 commit 4caad0c

File tree

15 files changed

+125
-43
lines changed

15 files changed

+125
-43
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
## Release 4.2.2
2+
3+
### Issue Fixes
4+
5+
- Fix for #916
6+
- Fix for #911
7+
- Version upgrade for several dependencies
8+
19
## Release 4.2.1 - Feb 19, 2024
210

311
### Issue Fixes

nitrite-jackson-mapper/src/test/java/org/dizitart/no2/integration/NitriteTest.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.dizitart.no2.collection.Document;
2525
import org.dizitart.no2.collection.NitriteCollection;
2626
import org.dizitart.no2.common.mapper.JacksonMapperModule;
27+
import org.dizitart.no2.exceptions.IndexingException;
2728
import org.dizitart.no2.exceptions.NitriteIOException;
2829
import org.dizitart.no2.exceptions.ValidationException;
2930
import org.dizitart.no2.index.IndexOptions;
@@ -241,6 +242,24 @@ public void testGetCollectionInvalidName() {
241242
db.getCollection(META_MAP_NAME);
242243
}
243244

245+
@Test(expected = IndexingException.class)
246+
public void testUniqueAndTextIndex() {
247+
try (final Nitrite nitrite = Nitrite.builder()
248+
.loadModule(new JacksonMapperModule())
249+
.openOrCreate()) {
250+
nitrite.getRepository(EntityUniqueFullText.class);
251+
}
252+
}
253+
254+
@Test(expected = IndexingException.class)
255+
public void testNoUniqueAndTextIndex() {
256+
try (final Nitrite nitrite = Nitrite.builder()
257+
.loadModule(new JacksonMapperModule())
258+
.openOrCreate()) {
259+
nitrite.getRepository(EntityNoUniqueFullText.class);
260+
}
261+
}
262+
244263

245264
@Data
246265
@NoArgsConstructor
@@ -263,4 +282,26 @@ public enum Status {
263282

264283
public static class EmptyClass {
265284
}
285+
286+
@Indices({
287+
@Index(fields = "value", type = IndexType.FULL_TEXT),
288+
@Index(fields = "value")
289+
})
290+
@Data
291+
@NoArgsConstructor
292+
@AllArgsConstructor
293+
public static class EntityUniqueFullText {
294+
private String value;
295+
}
296+
297+
@Indices({
298+
@Index(fields = "value", type = IndexType.FULL_TEXT),
299+
@Index(fields = "value", type = IndexType.NON_UNIQUE)
300+
})
301+
@Data
302+
@NoArgsConstructor
303+
@AllArgsConstructor
304+
public static class EntityNoUniqueFullText {
305+
private String value;
306+
}
266307
}

nitrite-mvstore-adapter/src/main/java/org/dizitart/no2/mvstore/compat/v1/UpgradeUtil.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@
3838
import java.util.concurrent.ConcurrentSkipListSet;
3939
import java.util.concurrent.CopyOnWriteArrayList;
4040

41-
import static org.dizitart.no2.common.Constants.INDEX_PREFIX;
42-
import static org.dizitart.no2.common.Constants.STORE_INFO;
41+
import static org.dizitart.no2.common.Constants.*;
4342
import static org.dizitart.no2.common.util.ObjectUtils.convertToObjectArray;
4443
import static org.dizitart.no2.common.util.StringUtils.isNullOrEmpty;
4544

@@ -91,9 +90,12 @@ private static void copyData(MVMap oldMap, org.h2.mvstore.MVMap newMap) {
9190

9291
if (key instanceof Compat.NitriteId) {
9392
newKey = nitriteId((Compat.NitriteId) key);
93+
} else if (oldMap.getName().contains(INDEX_META_PREFIX)) {
94+
// index meta map, wrap with Field
95+
newKey = key == null ? Fields.withNames() : Fields.withNames((String) key);
9496
} else if (oldMap.getName().contains(INDEX_PREFIX)) {
9597
// index map, wrap with DBValue
96-
newKey = newKey == null ? DBNull.getInstance() : new DBValue((Comparable<?>) newKey);
98+
newKey = key == null ? DBNull.getInstance() : new DBValue((Comparable<?>) key);
9799
}
98100

99101
Object newValue = migrateValue(entry.getValue());

nitrite-mvstore-adapter/src/main/java/org/dizitart/no2/mvstore/compat/v1/mvstore/fs/FilePathDisk.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,14 +305,13 @@ public InputStream newInputStream() throws IOException {
305305
}
306306

307307
static void freeMemoryAndFinalize() {
308-
IOUtils.trace("freeMemoryAndFinalize", (String)null, (Object)null);
308+
IOUtils.trace("freeMemoryAndFinalize", null, null);
309309
Runtime var0 = Runtime.getRuntime();
310310
long var1 = var0.freeMemory();
311311

312312
for(int var3 = 0; var3 < 16; ++var3) {
313313
var0.gc();
314314
long var4 = var0.freeMemory();
315-
var0.runFinalization();
316315
if (var4 == var1) {
317316
break;
318317
}

nitrite-mvstore-adapter/src/test/java/org/dizitart/no2/NitriteTest.java

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
import static org.dizitart.no2.collection.Document.createDocument;
6464
import static org.dizitart.no2.common.Constants.INTERNAL_NAME_SEPARATOR;
6565
import static org.dizitart.no2.common.Constants.META_MAP_NAME;
66+
import static org.dizitart.no2.common.util.Iterables.listOf;
6667
import static org.dizitart.no2.filters.Filter.ALL;
6768
import static org.dizitart.no2.filters.Filter.and;
6869
import static org.dizitart.no2.filters.FluentFilter.where;
@@ -186,38 +187,53 @@ public void testReopen() throws ParseException {
186187
NitriteCollection testCollection = db.getCollection("test");
187188
assertNotNull(testCollection);
188189
long prevSize = testCollection.find().size();
190+
ObjectRepository<Receipt> repository = db.getRepository(Receipt.class);
191+
assertNotNull(repository);
192+
long prevRepoSize = repository.size();
189193

190194
db.close();
191-
192195
db = null;
193196

194-
db = TestUtil.createDb(fileName, "test-user", "test-password");
195-
197+
db = TestUtil.createDb(fileName, "test-user", "test-password", listOf(new Receipt.Converter()));
196198
assertNotNull(db);
197199
testCollection = db.getCollection("test");
198200
assertNotNull(testCollection);
199201
long sizeNow = testCollection.find().size();
200202
assertEquals(prevSize, sizeNow);
203+
repository = db.getRepository(Receipt.class);
204+
assertNotNull(repository);
205+
long repoSizeNow = repository.size();
206+
assertEquals(prevRepoSize, repoSizeNow);
201207

202208
db.close();
203209
db = null;
204-
db = TestUtil.createDb(fileName, "test-user", "test-password");
210+
db = TestUtil.createDb(fileName, "test-user", "test-password", listOf(new Receipt.Converter()));
205211

206212
testCollection = db.getCollection("test");
207213
testCollection.insert(createDocument("firstName", "fn12")
208214
.put("lastName", "ln12")
209215
.put("birthDay", simpleDateFormat.parse("2010-07-01T16:02:48.440Z"))
210216
.put("data", new byte[]{10, 20, 30})
211217
.put("body", "a quick brown fox jump over the lazy dog"));
218+
repository = db.getRepository(Receipt.class);
219+
Receipt r = new Receipt();
220+
r.status = Receipt.Status.COMPLETED;
221+
r.clientRef = "10";
222+
r.synced = false;
223+
repository.insert(r);
212224

213225
db.close();
214226
db = null;
215-
db = TestUtil.createDb(fileName, "test-user", "test-password");
227+
db = TestUtil.createDb(fileName, "test-user", "test-password", listOf(new Receipt.Converter()));
216228

217229
testCollection = db.getCollection("test");
218230
assertNotNull(testCollection);
219231
sizeNow = testCollection.find().size();
220232
assertEquals(prevSize + 1, sizeNow);
233+
repository = db.getRepository(Receipt.class);
234+
assertNotNull(repository);
235+
repoSizeNow = repository.size();
236+
assertEquals(prevRepoSize + 1, repoSizeNow);
221237
}
222238

223239
@Test
@@ -491,9 +507,7 @@ public void testReadCompatibility() throws IOException {
491507
Files.copy(stream, Paths.get(System.getProperty("java.io.tmpdir") + File.separator + "old.db"));
492508

493509
String oldDbFile = System.getProperty("java.io.tmpdir") + File.separator + "old.db";
494-
Nitrite db = TestUtil.createDb(oldDbFile, "test-user", "test-password");
495-
SimpleNitriteMapper documentMapper = (SimpleNitriteMapper) db.getConfig().nitriteMapper();
496-
documentMapper.registerEntityConverter(new Receipt.Converter());
510+
Nitrite db = TestUtil.createDb(oldDbFile, "test-user", "test-password", listOf(new Receipt.Converter()));
497511

498512
NitriteCollection collection = db.getCollection("test");
499513

@@ -517,6 +531,8 @@ public void testReadCompatibility() throws IOException {
517531
assertNotNull(repository.getAttributes());
518532

519533
db.close();
534+
535+
//TODO: CHeck reopen with repo. That should also fails too.
520536
}
521537

522538
@Test

nitrite-mvstore-adapter/src/test/java/org/dizitart/no2/integration/TestUtil.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import lombok.extern.slf4j.Slf4j;
2626
import org.dizitart.no2.Nitrite;
2727
import org.dizitart.no2.collection.Document;
28+
import org.dizitart.no2.common.mapper.EntityConverter;
2829
import org.dizitart.no2.exceptions.ObjectMappingException;
2930
import org.dizitart.no2.mvstore.MVStoreModule;
3031

@@ -125,6 +126,20 @@ public static Nitrite createDb(String filePath, String user, String password) {
125126
.openOrCreate(user, password);
126127
}
127128

129+
public static Nitrite createDb(String filePath, String user, String password,
130+
List<EntityConverter<?>> entityConverters) {
131+
MVStoreModule storeModule = MVStoreModule.withConfig()
132+
.filePath(filePath)
133+
.compress(true)
134+
.build();
135+
136+
return Nitrite.builder()
137+
.loadModule(storeModule)
138+
.loadModule(() -> new HashSet<>(entityConverters))
139+
.fieldSeparator(".")
140+
.openOrCreate(user, password);
141+
}
142+
128143
public static Document parse(String json) {
129144
try {
130145
ObjectMapper objectMapper = createObjectMapper();

nitrite-spatial/src/test/java/org/dizitart/no2/spatial/SpatialIndexNegativeTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.dizitart.no2.exceptions.IndexingException;
2323
import org.dizitart.no2.filters.FluentFilter;
2424
import org.dizitart.no2.index.IndexOptions;
25+
import org.dizitart.no2.index.IndexType;
2526
import org.dizitart.no2.repository.Cursor;
2627
import org.junit.Test;
2728
import org.locationtech.jts.geom.Geometry;
@@ -55,7 +56,7 @@ public void testNoIndex() throws ParseException {
5556
@Test(expected = IndexingException.class)
5657
public void testIndexExists() {
5758
collection.createIndex(IndexOptions.indexOptions(SPATIAL_INDEX), "location");
58-
collection.createIndex(IndexOptions.indexOptions(SPATIAL_INDEX), "location");
59+
collection.createIndex(IndexOptions.indexOptions(IndexType.UNIQUE), "location");
5960
}
6061

6162
@Test(expected = FilterException.class)

nitrite/src/main/java/org/dizitart/no2/collection/operation/IndexOperations.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,14 @@ void createIndex(Fields fields, String indexType) {
6161
// if no index create index
6262
indexDescriptor = indexManager.createIndexDescriptor(fields, indexType);
6363
} else {
64-
// if index already there throw
65-
throw new IndexingException("Index already exists on fields: " + fields);
64+
// if index already there check if it is of same type, if not throw exception
65+
if (!indexDescriptor.getIndexType().equals(indexType)) {
66+
throw new IndexingException("Index already exists on fields: " + fields
67+
+ " with type " + indexDescriptor.getIndexType());
68+
} else {
69+
// if index is of same type, return
70+
return;
71+
}
6672
}
6773

6874
buildIndex(indexDescriptor, false);

nitrite/src/main/java/org/dizitart/no2/filters/TextFilter.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
* @author Anindya Chatterjee
3535
* @since 1.0
3636
*/
37+
@Setter
3738
public class TextFilter extends StringFilter {
38-
@Setter
3939
private TextTokenizer textTokenizer;
4040

4141
/**
@@ -100,12 +100,7 @@ private LinkedHashSet<NitriteId> searchExactByIndex(NitriteMap<String, List<?>>
100100
List<NitriteId> nitriteIds = (List<NitriteId>) indexMap.get(word);
101101
if (nitriteIds != null) {
102102
for (NitriteId id : nitriteIds) {
103-
Integer score = scoreMap.get(id);
104-
if (score == null) {
105-
scoreMap.put(id, 1);
106-
} else {
107-
scoreMap.put(id, score + 1);
108-
}
103+
scoreMap.merge(id, 1, Integer::sum);
109104
}
110105
}
111106
}
@@ -185,7 +180,7 @@ private LinkedHashSet<NitriteId> searchContains(NitriteMap<String, List<?>> inde
185180

186181
private LinkedHashSet<NitriteId> sortedIdsByScore(Map<NitriteId, Integer> unsortedMap) {
187182
List<Map.Entry<NitriteId, Integer>> list = new LinkedList<>(unsortedMap.entrySet());
188-
Collections.sort(list, (e1, e2) -> (e2.getValue()).compareTo(e1.getValue()));
183+
list.sort((e1, e2) -> (e2.getValue()).compareTo(e1.getValue()));
189184

190185
LinkedHashSet<NitriteId> result = new LinkedHashSet<>();
191186
for (Map.Entry<NitriteId, Integer> entry : list) {

nitrite/src/main/java/org/dizitart/no2/index/TextIndex.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public LinkedHashSet<NitriteId> findNitriteIds(FindPlan findPlan) {
152152
textFilter.setTextTokenizer(textTokenizer);
153153
return textFilter.applyOnTextIndex(indexMap);
154154
}
155-
throw new FilterException("Text index only supports a single TextFilter");
155+
throw new FilterException("TextFilter can only be applied on text index.");
156156
}
157157

158158
private NitriteMap<String, List<?>> findIndexMap() {

0 commit comments

Comments
 (0)