@RequestMapping(value = "/{instanceId}/delete", method = RequestMethod.GET) public String deleteInstance(@PathVariable String instanceId) throws JasDBException { DBSession session = sessionFactory.createSession(); session.deleteInstance(instanceId); return "redirect:/data/"; }
@RequestMapping(value = "/{instanceId}/{bag}/delete", method = RequestMethod.GET) public String deleteBag(@PathVariable String instanceId, @PathVariable String bag) throws JasDBException { DBSession session = sessionFactory.createSession(instanceId); session.removeBag(instanceId, bag); return "redirect:/data/"; }
@Test(expected = JasDBException.class) public void testGetNonExistingInstance() throws JasDBException, IOException { sessionFactory.createSession("myNotExistingInstance"); }
@RequestMapping(value="/{instanceId}", method = RequestMethod.GET) public String instanceData(@PathVariable String instanceId, Model model) throws JasDBException { DBSession session = sessionFactory.createSession(instanceId); model.addAttribute("bags", session.getBags(instanceId)); return buildIndex(session, model); }
@RequestMapping(value = "/", method = RequestMethod.POST) public String createInstance(@Valid WebInstance instance) throws JasDBException { DBSession session = sessionFactory.createSession(); session.addInstance(instance.getName()); return "redirect:/data/"; }
@RequestMapping(value="/", method=RequestMethod.GET) public String indexPage(Model model) throws JasDBException { DBSession session = sessionFactory.createSession(); model.addAttribute("bags", session.getBags()); return buildIndex(session, model); }
@RequestMapping(value="/{instanceId}/createBag", method = RequestMethod.POST) public String createBag(Bag bag, @PathVariable String instanceId) throws JasDBException { DBSession session = sessionFactory.createSession(instanceId); session.createOrGetBag(bag.getName()); return "redirect:/data/"; }
@Test public void addInstance() throws JasDBException, IOException { DBSession session = sessionFactory.createSession(); session.addInstance(MY_INSTANCE); List<Instance> instanceList = session.getInstances(); assertEquals(2, instanceList.size()); session.createOrGetBag(MY_INSTANCE, "testbag"); File instanceLocation = new File(storageLocation + "/.jasdb/myInstance"); assertThat(instanceLocation.exists(), is(true)); assertThat(new File(instanceLocation, "testbag.pjs").exists(), is(true)); }
@Test public void testBagCreateOrGet() throws JasDBException { DBSession session = sessionFactory.createSession(); session.createOrGetBag("testbag1"); assertTrue(new File(jasdbHome, "testbag1.pjs").exists()); assertEquals(1, session.getBags().size()); }
@RequestMapping(value = "/{instanceId}/createEntity", method = RequestMethod.POST) public String createData(@Valid WebEntity entity, @PathVariable String instanceId) throws JasDBException { DBSession session = sessionFactory.createSession(instanceId); EntityBag entityBag = session.createOrGetBag(entity.getBag()); entityBag.addEntity(SimpleEntity.fromJson(entity.getData())); return "redirect:/data/"; }
@Test public void testBagGetNonExisting() throws JasDBException { DBSession session = sessionFactory.createSession(); assertNull(session.getBag("testbag1")); assertNull(session.getBag("testbag2")); assertEquals(0, session.getBags().size()); assertFalse(new File(jasdbHome, "testbag1.pjs").exists()); assertFalse(new File(jasdbHome, "testbag1.pjsm").exists()); assertFalse(new File(jasdbHome, "testbag2.pjs").exists()); assertFalse(new File(jasdbHome, "testbag2.pjsm").exists()); }
@Test public void testGetInstances() throws JasDBException { DBSession session = sessionFactory.createSession(); List<Instance> instanceList = session.getInstances(); assertEquals(1, instanceList.size()); Instance instance = instanceList.get(0); assertEquals("default", instance.getInstanceId()); assertEquals(jasdbHome, new File(instance.getPath()).toString()); }
@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 testGetBagList() throws JasDBException, IOException { DBSession session = sessionFactory.createSession(); session.addAndSwitchInstance(MY_INSTANCE); session.createOrGetBag(BAG_1); session.createOrGetBag("bag2"); assertEquals(2, session.getBags().size()); assertEquals(0, session.getBags("default").size()); assertEquals(2, session.getBags(MY_INSTANCE).size()); }
@RequestMapping(value = "/{instanceId}/{bag}", method = RequestMethod.POST) public String searchFieldValue(SearchForm searchForm, @PathVariable String instanceId, @PathVariable String bag, Model model) throws JasDBException { DBSession session = sessionFactory.createSession(instanceId); EntityBag entityBag = session.getBag(bag); PageResult result = new PageResult(); model.addAttribute("page", result); if(entityBag != null) { QueryResult queryResult = entityBag.find(QueryBuilder.createBuilder().field(searchForm.getField()).value(searchForm.getValue())).limit(DEFAULT_PAGE_SIZE).execute(); result.setEntities(loadEntities(queryResult)); } else { result.setMessage(String.format("Unable to load Bag: %s on instance: %s as it does not exist", bag, instanceId)); } return "data/query"; }
@Test public void testCreateAndInsertEntities() throws JasDBException, IOException { DBSession session = sessionFactory.createSession(); session.addInstance(MY_INSTANCE); EntityBag bag = session.createOrGetBag(MY_INSTANCE, BAG_1); bag.addEntity(new SimpleEntity().addProperty("test", "value")); QueryResult result = bag.getEntities(); assertThat(result.size(), is(1l)); Entity entity = result.next(); assertThat(entity, notNullValue()); assertThat(entity.getProperty("test").getFirstValue().toString(), is("value")); }
@Test public void testCreateAndGetInstanceBag() throws JasDBException, IOException { DBSession session = sessionFactory.createSession(); session.addInstance(MY_INSTANCE); session.createOrGetBag(MY_INSTANCE, BAG_1); //default instance should be null assertNull(session.getBag(BAG_1)); assertNotNull(MY_INSTANCE, session.getBag(MY_INSTANCE, BAG_1)); }
@Test public void testBagFlush() throws JasDBException { DBSession session = sessionFactory.createSession(); EntityBag bag = session.createOrGetBag("testbag"); long sizeBefore = bag.getDiskSize(); int TEST_SIZE = 1000; for(int i=0; i<TEST_SIZE; i++) { bag.addEntity(new SimpleEntity()); } bag.flush(); assertTrue(bag.getDiskSize() > sizeBefore); }
@Test public void testSwitchInstance() throws JasDBException, IOException { DBSession session = sessionFactory.createSession(); session.addInstance(MY_INSTANCE); assertEquals("default", session.getInstanceId()); session.switchInstance(MY_INSTANCE); assertEquals(MY_INSTANCE, session.getInstanceId()); session.addAndSwitchInstance("anotherInstance"); assertEquals("anotherInstance", session.getInstanceId()); }
@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(); } }