@Override public KeyType getKeyType() { return new UUIDKeyType(); }
@Override public KeyType getKeyType() { return new ComplexKeyType(); }
@Override public KeyType getKeyType() { return new LongKeyType(); }
bag.ensureIndex(new SimpleIndexField("field1", new StringKeyType()), false); bag.ensureIndex(new SimpleIndexField("field5", new LongKeyType()), false, new SimpleIndexField("field6", new LongKeyType())); bag.ensureIndex(new SimpleIndexField("field6", new LongKeyType()), false); bag.ensureIndex(new SimpleIndexField("age", new LongKeyType()), false); bag.ensureIndex(new SimpleIndexField("city", new StringKeyType()), false); bag.ensureIndex(new SimpleIndexField("embed.embeddedProperty", new StringKeyType()), false); bag.ensureIndex(new SimpleCompositeIndexField(new SimpleIndexField("age", new LongKeyType()), new SimpleIndexField("mainCity", new StringKeyType())), false);
@Override public void openWriter() throws JasDBStorageException { this.keyInfo = new KeyInfoImpl(new SimpleIndexField("__ID", new UUIDKeyType()), new SimpleIndexField("RECORD_POINTER", new LongKeyType())); this.index = new BTreeIndex(indexLocation, keyInfo); this.writer.openWriter(); this.index.openIndex(); }
@Override public void openWriter() throws JasDBStorageException { this.keyInfo = new KeyInfoImpl(new SimpleIndexField("__ID", new UUIDKeyType()), new SimpleIndexField("data", new DataKeyType())); try { this.index = new BTreeIndex(recordStorageLocation, keyInfo); this.index.openIndex(); } catch(OverlappingFileLockException e) { throw new RecordStoreInUseException("Record datastore: " + recordStorageLocation + " is already in use, cannot be opened"); } }
@Override public KeyType getKeyType() { return new StringKeyType(maxSize); }
@Override public KeyType getKeyType() { return new DataKeyType(); }
@Test public void testRandomPersistUpdate() throws Exception { String[] cities = new String[] {"Amsterdam", "Rotterdam", "Utrecht", "Groningen", "Haarlem", "Den Haag", "Maastricht", "Eindhoven"}; int testSize = 1000; DBSession session = sessionFactory.createSession(); EntityBag bag = session.createOrGetBag("testbag"); bag.ensureIndex(new SimpleIndexField("city", new StringKeyType()), false); bag.ensureIndex(new SimpleIndexField("itemId", new LongKeyType()), true); try { Map<String, Integer> cityCounts = generateCities(testSize, bag); assertCityIndexes(bag, cities, cityCounts); for(int i=200; i<400; i++) { QueryExecutor executor = bag.find(QueryBuilder.createBuilder().field("itemId").value((long)i)); QueryResult result = executor.execute(); for(Entity entity : result) { String city = entity.getProperty("city").getFirstValueObject().toString(); entity.setProperty("city", "unknown"); bag.updateEntity(entity); changeCityCount(city, cityCounts, false); changeCityCount("unknown", cityCounts, true); } } assertCityIndexes(bag, cities, cityCounts); } finally { JasDBMain.shutdown(); } }
private List<IndexField> guaranteeIdKey(IndexField... valueFields) throws JasDBStorageException { Set<String> fields = new HashSet<>(); List<IndexField> indexFields = new ArrayList<>(); for(IndexField valueField : valueFields) { fields.add(valueField.getField()); indexFields.add(valueField); } if(!fields.contains(SimpleEntity.DOCUMENT_ID)) { indexFields.add(new SimpleIndexField(SimpleEntity.DOCUMENT_ID, new UUIDKeyType())); } return indexFields; }
@Test public void testBagRemove() throws JasDBException { DBSession session = sessionFactory.createSession(); EntityBag bag = session.createOrGetBag("testbag1"); bag.ensureIndex(new SimpleIndexField("field1", new StringKeyType()), false); assertTrue(new File(jasdbHome, "testbag1.pjs").exists()); assertTrue(new File(jasdbHome, "testbag1_field1ID.idx").exists()); assertEquals(1, session.getBags().size()); session.removeBag("testbag1"); assertFalse(new File(jasdbHome, "testbag1.pjs").exists()); assertFalse(new File(jasdbHome, "testbag1_field1ID.idx").exists()); }
@Test public void testRandomPersistUpdateBigRecordUpdate() throws Exception { String htmlData = ResourceUtil.getContent("datasets/htmlpage.data", "UTF-8"); int testSize = 1000; DBSession session = sessionFactory.createSession(); EntityBag bag = session.createOrGetBag("testbag"); bag.ensureIndex(new SimpleIndexField("city", new StringKeyType()), false); bag.ensureIndex(new SimpleIndexField("itemId", new LongKeyType()), true); try { Map<String, Integer> cityCounts = generateCities(testSize, bag); assertCityIndexes(bag, cities, cityCounts); for(int i=200; i<400; i++) { QueryExecutor executor = bag.find(QueryBuilder.createBuilder().field("itemId").value((long)i)); QueryResult result = executor.execute(); for(Entity entity : result) { String city = entity.getProperty("city").getFirstValueObject().toString(); entity.setProperty("city", "unknown"); entity.setProperty("bigdatafield", htmlData); bag.updateEntity(entity); changeCityCount(city, cityCounts, false); changeCityCount("unknown", cityCounts, true); } } assertCityIndexes(bag, cities, cityCounts); } finally { JasDBMain.shutdown(); } }
@Override public Index createIndex(String bagName, IndexField indexField, boolean unique, IndexField... valueFields) throws JasDBStorageException { KeyInfo keyInfo; if(unique) { keyInfo = new KeyInfoImpl(indexField, guaranteeIdKey(valueFields)); } else { keyInfo = new KeyInfoImpl(Lists.newArrayList(indexField, new SimpleIndexField(SimpleEntity.DOCUMENT_ID, new UUIDKeyType())), Lists.newArrayList(valueFields)); } return createInStore(bagName, keyInfo); }
@Test public void testBagRemoveInstance() throws JasDBException, IOException { DBSession session = sessionFactory.createSession(); File newInstanceFolder = new File(storageLocation + "/.jasdb/myInstance"); session.addAndSwitchInstance(MY_INSTANCE); EntityBag bag = session.createOrGetBag("testbag1"); bag.ensureIndex(new SimpleIndexField("field1", new StringKeyType()), false); assertTrue(new File(newInstanceFolder, "testbag1.pjs").exists()); assertTrue(new File(newInstanceFolder, "testbag1_field1ID.idx").exists()); assertEquals(1, session.getBags().size()); session.removeBag("testbag1"); assertFalse(new File(newInstanceFolder, "testbag1.pjs").exists()); assertFalse(new File(newInstanceFolder, "testbag1_field1ID.idx").exists()); } }
@Test public void testNotEqualsMultiIndexes() throws Exception { try { DBSession session = sessionFactory.createSession(); EntityBag bag = session.createOrGetBag("websites"); bag.ensureIndex(new SimpleIndexField("url", new StringKeyType()), false); bag.ensureIndex( new SimpleCompositeIndexField( new SimpleIndexField("stepid", new StringKeyType()), new SimpleIndexField("workflow", new LongKeyType()) ),false); bag.addEntity(new SimpleEntity().addProperty("url", "").addProperty("stepid", 1L).addProperty("workflow", 1L)); bag.addEntity(new SimpleEntity().addProperty("url", "").addProperty("stepid", 1L).addProperty("workflow", 1L)); bag.addEntity(new SimpleEntity().addProperty("url", "").addProperty("stepid", 1L).addProperty("workflow", 1L)); bag.addEntity(new SimpleEntity().addProperty("url", "http://someurl.nl/1").addProperty("stepid", 1L).addProperty("workflow", 1L)); bag.addEntity(new SimpleEntity().addProperty("url", "http://someurl.nl/2").addProperty("stepid", 1L).addProperty("workflow", 1L)); QueryResult r = bag.find(QueryBuilder.createBuilder() .field("stepid").value(1L) .field("workflow").value(1L) .field("url").notEquals("")).execute(); assertThat(r.size(), is(2L)); } finally { JasDBMain.shutdown(); } }
@Override public Index createIndex(String bagName, CompositeIndexField compositeIndexFields, boolean unique, IndexField... values) throws JasDBStorageException { KeyInfo keyInfo; if(unique) { keyInfo = new KeyInfoImpl(compositeIndexFields.getIndexFields(), guaranteeIdKey(values)); } else { List<IndexField> indexFields = Lists.newArrayList(compositeIndexFields.getIndexFields()); indexFields.add(new SimpleIndexField(SimpleEntity.DOCUMENT_ID, new UUIDKeyType())); keyInfo = new KeyInfoImpl(indexFields, Lists.newArrayList(values)); } return createInStore(bagName, keyInfo); }
@Test public void testEnsureAndRemoveIndex() throws JasDBException { DBSession session = sessionFactory.createSession(); EntityBag bag = session.createOrGetBag("testbag"); bag.ensureIndex(new SimpleIndexField("field1", new StringKeyType()), true); bag.ensureIndex(new SimpleIndexField("field2", new StringKeyType()), false); bag.addEntity(new SimpleEntity().addProperty("field1", "value1").addProperty("field2", "testkey2value")); String jasdbHome = storageLocation + "/.jasdb"; File field1Index = new File(jasdbHome, "testbag_field1.idx"); File field2Index = new File(jasdbHome, "testbag_field2ID.idx"); assertTrue("Index 1 file should exist", field1Index.exists()); assertTrue("Index 2 file should exist", field2Index.exists()); bag.removeIndex("field1"); assertFalse("Index 1 file should no longer exist", field1Index.exists()); assertTrue("Index 2 file should exist", field2Index.exists()); bag.removeIndex("field2ID"); assertFalse("Index 1 file should no longer exist", field1Index.exists()); assertFalse("Index 2 file should no longer exist", field2Index.exists()); }
@Test public void testPersisterRemove() throws Exception { int testSize = 1000; DBSession session = sessionFactory.createSession(); EntityBag bag = session.createOrGetBag("testbag"); bag.ensureIndex(new SimpleIndexField("city", new StringKeyType(100)), false); bag.ensureIndex(new SimpleIndexField("testField", new LongKeyType()), true); Random rnd = new Random(); for(int i=0; i<testSize; i++) { int cityIdx = rnd.nextInt(SimpleBaseTest.possibleCities.length); String city = SimpleBaseTest.possibleCities[cityIdx]; SimpleEntity entity = new SimpleEntity(); entity.addProperty("city", city); entity.addProperty("testField", (long)i); bag.addEntity(entity); } for(String city : SimpleBaseTest.possibleCities) { QueryResult result = bag.find(QueryBuilder.createBuilder().field("city").value(city)).execute(); for(Entity foundEntity : result) { Long testFieldValue = foundEntity.getProperty("testField").getFirstValueObject(); bag.removeEntity(foundEntity); assertFalse("There should no longer be a result", bag.find(QueryBuilder.createBuilder().field("testField").value(testFieldValue)).execute().hasNext()); } result = bag.find(QueryBuilder.createBuilder().field("city").value(city)).execute(); assertEquals("There should no longer be any entity", (long) 0, result.size()); } }
@Test public void testDeleteEmptyIndexValue() throws Exception { try { DBSession session = sessionFactory.createSession(); EntityBag bag = session.createOrGetBag("somebag"); bag.ensureIndex(new SimpleIndexField("field", new StringKeyType()), false); String id = bag.addEntity(new SimpleEntity().addProperty("anotherfield", "somevalue")).getInternalId(); bag.removeEntity(id); } finally { JasDBMain.shutdown(); } }
@Test public void testPersistIndexNonUniqueQuery() throws JasDBException, InterruptedException { DBSession session = sessionFactory.createSession(); EntityBag bag = session.createOrGetBag("testbag"); bag.addEntity(new SimpleEntity().addProperty("city", "Amsterdam")); bag.addEntity(new SimpleEntity().addProperty("city", "Amsterdam")); bag.addEntity(new SimpleEntity().addProperty("city", "Rotterdam")); bag.addEntity(new SimpleEntity().addProperty("city", "Utrecht")); bag.addEntity(new SimpleEntity().addProperty("city", "Utrecht")); QueryResult result = bag.find(QueryBuilder.createBuilder().field("city").value("Amsterdam")).execute(); assertThat(result.size(), is(2L)); result.close(); bag.ensureIndex(new SimpleIndexField("city", new StringKeyType()), false); //let's give the index some time to build Thread.sleep(5000); result = bag.find(QueryBuilder.createBuilder().field("city").value("Amsterdam")).execute(); assertThat(result.size(), is(2L)); result.close(); } }