|
| 1 | +/* |
| 2 | + * Copyright (c) 2017-2021 Nitrite author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + */ |
| 17 | + |
| 18 | +package org.dizitart.no2.integration.collection; |
| 19 | + |
| 20 | +import org.dizitart.no2.Nitrite; |
| 21 | +import org.dizitart.no2.collection.Document; |
| 22 | +import org.dizitart.no2.collection.NitriteCollection; |
| 23 | +import org.dizitart.no2.index.IndexOptions; |
| 24 | +import org.dizitart.no2.index.IndexType; |
| 25 | +import org.junit.After; |
| 26 | +import org.junit.Before; |
| 27 | +import org.junit.Test; |
| 28 | + |
| 29 | +import static org.dizitart.no2.integration.TestUtil.createDb; |
| 30 | +import static org.dizitart.no2.integration.TestUtil.deleteDb; |
| 31 | +import static org.dizitart.no2.integration.TestUtil.getRandomTempDbFile; |
| 32 | +import static org.dizitart.no2.filters.FluentFilter.where; |
| 33 | +import static org.junit.Assert.assertEquals; |
| 34 | + |
| 35 | +/** |
| 36 | + * Issue #1260: inserting thousands of documents that share the same non-unique index key must |
| 37 | + * scale - the old array layout re-serialized an ever-growing id list on every insert (O(n²) on |
| 38 | + * a persistent store). The composite-key layout makes each insert an O(log n) point write. This |
| 39 | + * test exercises that path at scale on a file-backed MVStore and verifies it stays correct. |
| 40 | + * |
| 41 | + * @author Anindya Chatterjee |
| 42 | + */ |
| 43 | +public class NonUniqueIndexScaleTest { |
| 44 | + private String filePath; |
| 45 | + private Nitrite db; |
| 46 | + |
| 47 | + @Before |
| 48 | + public void setUp() { |
| 49 | + filePath = getRandomTempDbFile(); |
| 50 | + db = createDb(filePath); |
| 51 | + } |
| 52 | + |
| 53 | + @After |
| 54 | + public void tearDown() throws Exception { |
| 55 | + if (db != null && !db.isClosed()) { |
| 56 | + db.close(); |
| 57 | + } |
| 58 | + deleteDb(filePath); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void testManyDocumentsWithSameKey() { |
| 63 | + NitriteCollection collection = db.getCollection("scale"); |
| 64 | + collection.createIndex(IndexOptions.indexOptions(IndexType.NON_UNIQUE), "category"); |
| 65 | + |
| 66 | + int perCategory = 10000; |
| 67 | + for (int i = 0; i < perCategory; i++) { |
| 68 | + collection.insert(Document.createDocument("category", "hot").put("seq", i)); |
| 69 | + collection.insert(Document.createDocument("category", "cold").put("seq", i)); |
| 70 | + } |
| 71 | + db.commit(); |
| 72 | + |
| 73 | + // every id under a high-cardinality key must be retrievable through the index |
| 74 | + assertEquals(perCategory, collection.find(where("category").eq("hot")).size()); |
| 75 | + assertEquals(perCategory, collection.find(where("category").eq("cold")).size()); |
| 76 | + assertEquals(0, collection.find(where("category").eq("missing")).size()); |
| 77 | + |
| 78 | + // reopen the database: the on-disk composite index must survive a round trip |
| 79 | + db.close(); |
| 80 | + db = createDb(filePath); |
| 81 | + collection = db.getCollection("scale"); |
| 82 | + assertEquals(perCategory, collection.find(where("category").eq("hot")).size()); |
| 83 | + |
| 84 | + // removing one whole key must clear it without touching the other |
| 85 | + collection.remove(where("category").eq("hot")); |
| 86 | + db.commit(); |
| 87 | + assertEquals(0, collection.find(where("category").eq("hot")).size()); |
| 88 | + assertEquals(perCategory, collection.find(where("category").eq("cold")).size()); |
| 89 | + } |
| 90 | +} |
0 commit comments