Tabnine Logo
EntityBag
Code IndexAdd Tabnine to your IDE (free)

How to use
EntityBag
in
com.oberasoftware.jasdb.api.session

Best Java code snippets using com.oberasoftware.jasdb.api.session.EntityBag (Showing top 20 results out of 315)

origin: oberasoftware/jasdb

  @Test
  public void testNoConditionsFullScan() throws Exception {
    DBSession session = sessionFactory.createSession();
    EntityBag bag = session.createOrGetBag("smallbag");
    bag.addEntity(new SimpleEntity(ID1).addProperty("type", "thing"));
    bag.addEntity(new SimpleEntity(ID2).addProperty("type", "thing"));
    bag.addEntity(new SimpleEntity(ID3).addProperty("type", "contribution"));
    bag.addEntity(new SimpleEntity(ID4).addProperty("type", "contribution"));

    try {
      QueryBuilder builder = QueryBuilder.createBuilder();

      QueryExecutor executor = bag.find(builder);
      List<Entity> entities = toList(executor.execute());
      List<String> entityIds = entities.stream().map(Entity::getInternalId).collect(Collectors.toList());

      assertThat(entityIds, hasItems(ID1, ID2, ID3, ID4));
    } finally {
      session.closeSession();
      JasDBMain.shutdown();
    }

  }
}
origin: oberasoftware/jasdb

@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"));
}
origin: oberasoftware/jasdb

@Override
public Entity persist(Object persistableObject) throws JasDBStorageException {
  MapResult mappedResult = ENTITY_MAPPER.mapTo(persistableObject);
  String bagName = mappedResult.getBagName();
  EntityBag bag = session.createOrGetBag(bagName);
  Entity persistedEntity;
  try {
    Entity entity = mappedResult.getJasDBEntity();
    if(StringUtils.stringNotEmpty(entity.getInternalId()) && bag.getEntity(entity.getInternalId()) != null) {
      //update
      persistedEntity = bag.updateEntity(mappedResult.getJasDBEntity());
      LOG.debug("Updated entity: {} in bag: {}", persistedEntity, bagName);
    } else {
      persistedEntity = bag.addEntity(mappedResult.getJasDBEntity());
      LOG.debug("Created entity: {} in bag: {}", persistedEntity, bagName);
    }
  } catch(RuntimeJasDBException e) {
    //we do this in case we have exactly two threads at same time trying to persist
    persistedEntity = bag.updateEntity(mappedResult.getJasDBEntity());
    LOG.debug("Updated entity: {} in bag: {}", persistedEntity, bagName);
  }
  //update the ID of the passed object
  ENTITY_MAPPER.updateId(persistedEntity.getInternalId(), persistableObject);
  return persistedEntity;
}
origin: oberasoftware/jasdb

@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);
}
origin: oberasoftware/jasdb

@Test
public void testInsertOrUpdatePersist() throws JasDBException {
  DBSession session = sessionFactory.createSession();
  EntityBag bag = session.createOrGetBag("insertOrUpdateBag");
  SimpleEntity entity = new SimpleEntity();
  entity.addProperty("Test", "value1");
  String id = bag.persist(entity).getInternalId();
  assertThat(bag.getEntities().size(), is(1L));
  assertThat(bag.getEntity(id).getProperty("Test").getFirstValueObject(), is("value1"));
  SimpleEntity updatedEntity = new SimpleEntity(id);
  updatedEntity.addProperty("AnotherProperty", "AnotherValue");
  bag.persist(updatedEntity);
  assertThat(bag.getEntities().size(), is(1L));
  assertThat(bag.getEntity(id).getProperty("AnotherProperty").getFirstValueObject(), is("AnotherValue"));
}
origin: oberasoftware/jasdb

@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();
  }
}
origin: oberasoftware/jasdb

private List<Entity> getEntities(EntityBag bag, QueryBuilder query, int start, int limit) throws JasDBStorageException {
  QueryExecutor executor = bag.find(query);
  if(start > 0 && limit > 0) {
    executor.paging(start, limit);
  } else if(limit > 0) {
    executor.limit(limit);
  }
  final List<Entity> entities = new ArrayList<>();
  try (QueryResult result = executor.execute()) {
    for (Entity entity : result) {
      entities.add(entity);
    }
  }
  return entities;
}
origin: oberasoftware/jasdb

@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());
  }
}

origin: oberasoftware/jasdb

  @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();
  }
}
origin: oberasoftware/jasdb

@Test
public void testPersistFindPerformance() throws Exception {
  DBSession pojoDb = sessionFactory.createSession();
  EntityBag bag = pojoDb.createOrGetBag("mybag");
  List<String> entityIds = new ArrayList<>();
  for(int i=0; i<NUMBER_ENTITIES; i++) {
    SimpleEntity entity = new SimpleEntity(UUID.randomUUID().toString());
    entity.addProperty("someProperty" + i, i);
    entity.addProperty("doubleId", entity.getInternalId());
    bag.addEntity(entity);
    entityIds.add(entity.getInternalId());
  }
  try {
    for(String id : entityIds) {
      Entity entity = bag.getEntity(id);
      Assert.assertNotNull("Entity for id: " + id + " should be found", entity);
      assertEquals("Id should match expected id", id, entity.getInternalId());
      Assert.assertNotNull("There should be a property doubleId", entity.getProperty("doubleId"));
      assertEquals("Property doubleId should match expected id", id, entity.getProperty("doubleId").getFirstValueObject());
    }
  } finally {
    JasDBMain.shutdown();
  }
}
origin: oberasoftware/jasdb

try {
  for(int i=0; i<INITIAL_SIZE; i++) {
    Entity entity = bag.addEntity(new SimpleEntity().addProperty("testfield", "test" + i));
    entities.add(entity.getInternalId());
try {
  for(int i=0; i<NUMBER_ENTITIES; i++) {
    Entity entity = bag.addEntity(new SimpleEntity().addProperty("testfield", "test" + (i + INITIAL_SIZE)));
    entities.add(entity.getInternalId());            
    Entity entity = bag.getEntity(id);
    Assert.assertNotNull("Entity for id: " + id + " should be found", entity);
    assertEquals("Id should match expected id", id, entity.getInternalId());
  for(Entity en : bag.getEntities()) {
    log.debug("Loaded entity: {}", en.getInternalId());
    recordsFound++;
origin: oberasoftware/jasdb

@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();
  }
}
origin: oberasoftware/jasdb

EntityBag bag = pojoDb.createOrGetBag("homeautotest");
bag.ensureIndex(
    new SimpleCompositeIndexField(
        new SimpleIndexField("controllerId", new StringKeyType()),
  String controllerEntityId = bag.addEntity(new SimpleEntity().addProperty("controllerId", "Renzes-MacBook-Pro-2.local")
      .addProperty("plugins", "7158f3ec-681f-4d9b-9ab1-6ab6c60288e3")
      .addProperty("type", "controller")).getInternalId();
  String pluginEntityId = bag.addEntity(new SimpleEntity()
      .addProperty("controllerId", "Renzes-MacBook-Pro-2.local").addProperty("pluginId", "zwave")
      .addProperty("name", "ZWave provider")
      .addProperty("type", "plugin")).getInternalId();
  String deviceEntityId = bag.addEntity(new SimpleEntity()
      .addProperty("controllerId", "Renzes-MacBook-Pro-2.local").addProperty("pluginId", "zwave")
      .addProperty("name", "ZWave provider")
  bag.updateEntity(new SimpleEntity(controllerEntityId).addProperty("controllerId", "Renzes-MacBook-Pro-2.local")
      .addProperty("plugins", "7158f3ec-681f-4d9b-9ab1-6ab6c60288e3").addProperty("type", "controller"));
  bag.updateEntity(new SimpleEntity(pluginEntityId)
      .addProperty("controllerId", "Renzes-MacBook-Pro-2.local").addProperty("pluginId", "zwave")
      .addProperty("name", "ZWave provider").addProperty("type", "plugin"));
origin: oberasoftware/jasdb

@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());
}
origin: oberasoftware/jasdb

DBSession pojoDb = sessionFactory.createSession();
EntityBag bag = pojoDb.createOrGetBag("testbag");
bag.ensureIndex(new SimpleIndexField("title", new StringKeyType()), true);
    bag.addEntity(entity);
  searchEntity.addProperty("MySearchProperty", "MySpecialSearchProperty");
  searchEntity.setInternalId(searchTestId);
  bag.addEntity(searchEntity);
} finally {
  JasDBMain.shutdown();
origin: oberasoftware/jasdb

private Map<String, Integer> generateCities(int testSize, EntityBag bag) throws JasDBStorageException {
  Map<String, Integer> cityCounts = new HashMap<>();
  Random rnd = new Random();
  for(int i=0; i<testSize; i++) {
    int cityIdx = rnd.nextInt(cities.length);
    String city = cities[cityIdx];
    SimpleEntity entity = new SimpleEntity();
    entity.addProperty("city", city);
    entity.addProperty("itemId", (long)i);
    changeCityCount(city, cityCounts, true);
    bag.addEntity(entity);
  }
  return cityCounts;
}
origin: oberasoftware/jasdb

@Test
public void testEntityManagerUpdate() throws JasDBException {
  DBSession session = sessionFactory.createSession();
  EntityManager entityManager = session.getEntityManager();
  String id = UUID.randomUUID().toString();
  TestEntity entity = new TestEntity(id, "Renze", "de Vries", newArrayList("programming", "model building", "biking"),
      new ImmutableMap.Builder<String, String>()
          .put("city", "Amsterdam")
          .put("street", "Secret passageway 10")
          .put("zipcode", "0000TT").build());
  assertThat(entityManager.persist(entity).getInternalId(), is(id));
  EntityBag testBag = session.createOrGetBag("TEST_BAG");
  assertThat(testBag.getSize(), is(1l));
  Entity mappedEntity = testBag.getEntity(id);
  assertThat(mappedEntity.getValue("firstName"), is("Renze"));
  assertThat(mappedEntity.getValue("lastName"), is("de Vries"));
  entity.setFirstName("Updated");
  entityManager.persist(entity);
  mappedEntity = testBag.getEntity(id);
  assertThat(mappedEntity.getValue("firstName"), is("Updated"));
  assertThat(mappedEntity.getValue("lastName"), is("de Vries"));
}
origin: oberasoftware/jasdb

private void assertFind(String someRandomId) throws Exception {
  JasDBMain.start();
  log.info("START INDEX READ TEST");
  DBSession pojoDb = sessionFactory.createSession();
  EntityBag bag = pojoDb.createOrGetBag("testbag");
  try {
    log.info("Starting search for: {}", searchTestId);
    long startSearch = System.nanoTime();
    Entity entity = bag.getEntity(searchTestId);
    long endSearch = System.nanoTime();
    log.info("Search finished in: {}", (endSearch - startSearch));
    Assert.assertNotNull("An entity should have been found for id: " + searchTestId, entity);
    assertEquals(searchTestId, entity.getInternalId());
    
    log.info("Starting search for random: {}", someRandomId);
    startSearch = System.nanoTime();
    entity = bag.getEntity(someRandomId);
    endSearch = System.nanoTime();
    log.info("Search finished for random: {}", (endSearch - startSearch));
    Assert.assertNotNull("An entity should have been found", entity);
    assertEquals(someRandomId, entity.getInternalId());
  } finally {
    JasDBMain.shutdown();
  }
}
origin: oberasoftware/jasdb

@RequestMapping(value = "/{instanceId}/{bag}")
public String findAll(@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.getEntities(DEFAULT_PAGE_SIZE);
    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";
}
origin: oberasoftware/jasdb

@Override
public void remove(Object persistableObject) throws JasDBStorageException {
  MapResult mappedResult = ENTITY_MAPPER.mapTo(persistableObject);
  String bagName = mappedResult.getBagName();
  EntityBag bag = session.createOrGetBag(bagName);
  bag.removeEntity(mappedResult.getJasDBEntity().getInternalId());
}
com.oberasoftware.jasdb.api.sessionEntityBag

Javadoc

The Entity bag is the main API for all interactions onto the Bag. Every bag contains a number of indexes. The EntityBag allows insertion, updates and removal operation of the entities inside the bag. Also an extensive Query API is available for building queries onto the Bag.

Most used methods

  • addEntity
    Adds an entity to the bag of entities
  • find
    Builds a query for document in the storage for a specific queryfield with optional sorting parameter
  • getEntities
    Execute a query returning all records in the bag with a given max
  • getEntity
    Retrieves a specific entity from the bag
  • removeEntity
    Removes the entity from the bag using the id
  • updateEntity
    Updates an entity in the bag of entities
  • ensureIndex
    Ensures there is an index present on a given field in this bag, will create if not existent, will do
  • flush
    Forcibly flushes all the data in the bag to the storage
  • getDiskSize
    Returns the size on the disk of the entities
  • getSize
    Returns the amount of entities in the bag
  • persist
    Persists the provided entity, if not exists will be created, if already exists it will be updated
  • removeIndex
    Removes the index from the bag
  • persist,
  • removeIndex

Popular in Java

  • Reactive rest calls using spring rest template
  • compareTo (BigDecimal)
  • onCreateOptionsMenu (Activity)
  • findViewById (Activity)
  • ConnectException (java.net)
    A ConnectException is thrown if a connection cannot be established to a remote host on a specific po
  • Calendar (java.util)
    Calendar is an abstract base class for converting between a Date object and a set of integer fields
  • PriorityQueue (java.util)
    A PriorityQueue holds elements on a priority heap, which orders the elements according to their natu
  • ResourceBundle (java.util)
    ResourceBundle is an abstract class which is the superclass of classes which provide Locale-specifi
  • TimerTask (java.util)
    The TimerTask class represents a task to run at a specified time. The task may be run once or repeat
  • Get (org.apache.hadoop.hbase.client)
    Used to perform Get operations on a single row. To get everything for a row, instantiate a Get objec
  • From CI to AI: The AI layer in your organization
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now