Search in sources :

Example 1 with BeanFactoryImpl

use of org.apache.gora.persistency.impl.BeanFactoryImpl in project gora by apache.

the class MemStoreTest method testMemStoreDeleteByQueryFields.

@Test
public void testMemStoreDeleteByQueryFields() throws Exception {
    DataStore<String, WebPage> store = new MemStore<>();
    BeanFactory<String, WebPage> beanFactory = new BeanFactoryImpl<>(String.class, WebPage.class);
    store.setBeanFactory(beanFactory);
    Query<String, WebPage> query;
    // test 5 - delete all with some fields
    WebPageDataCreator.createWebPageData(store);
    query = store.newQuery();
    query.setFields("outlinks", "parsedContent", "content");
    Query<String, WebPage> newQuery = store.newQuery();
    newQuery.setStartKey(SORTED_URLS[0]);
    newQuery.setEndKey(SORTED_URLS[9]);
    newQuery.setFields("outlinks", "parsedContent", "content");
    DataStoreTestUtil.assertNumResults(newQuery, URLS.length);
    store.deleteByQuery(query);
    store.deleteByQuery(query);
    // don't you love that HBase sometimes does not delete arbitrarily
    store.deleteByQuery(query);
    store.flush();
    DataStoreTestUtil.assertNumResults(store.newQuery(), URLS.length);
    // assert that data is deleted
    for (String SORTED_URL : SORTED_URLS) {
        WebPage page = store.get(SORTED_URL);
        assertNotNull(page);
        assertNotNull(page.getUrl());
        assertEquals(page.getUrl().toString(), SORTED_URL);
        assertEquals("Map of Outlinks should have a size of '0' as the deleteByQuery " + "not only removes the data but also the data structure.", 0, page.getOutlinks().size());
        assertEquals(0, page.getParsedContent().size());
        if (page.getContent() != null) {
            LOG.info("url:" + page.getUrl().toString());
            LOG.info("limit:" + page.getContent().limit());
        } else {
            assertNull(page.getContent());
        }
    }
    // test 6 - delete some with some fields
    WebPageDataCreator.createWebPageData(store);
    query = store.newQuery();
    query.setFields("url");
    String startKey = SORTED_URLS[NUM_KEYS];
    String endKey = SORTED_URLS[SORTED_URLS.length - NUM_KEYS];
    query.setStartKey(startKey);
    query.setEndKey(endKey);
    DataStoreTestUtil.assertNumResults(store.newQuery(), URLS.length);
    store.deleteByQuery(query);
    store.deleteByQuery(query);
    // don't you love that HBase sometimes does not delete arbitrarily
    store.deleteByQuery(query);
    store.flush();
    DataStoreTestUtil.assertNumResults(store.newQuery(), URLS.length);
    // assert that data is deleted
    for (int i = 0; i < URLS.length; i++) {
        WebPage page = store.get(URLS[i]);
        assertNotNull(page);
        if (URLS[i].compareTo(startKey) < 0 || URLS[i].compareTo(endKey) > 0) {
            // not deleted
            DataStoreTestUtil.assertWebPage(page, i);
        } else {
            // deleted
            assertNull(page.getUrl());
            assertNotNull(page.getOutlinks());
            assertNotNull(page.getParsedContent());
            assertNotNull(page.getContent());
            assertTrue(page.getOutlinks().size() > 0);
            assertTrue(page.getParsedContent().size() > 0);
        }
    }
}
Also used : WebPage(org.apache.gora.examples.generated.WebPage) BeanFactoryImpl(org.apache.gora.persistency.impl.BeanFactoryImpl) Test(org.junit.Test)

Example 2 with BeanFactoryImpl

use of org.apache.gora.persistency.impl.BeanFactoryImpl in project gora by apache.

the class CouchDBStore method fromCouchDBRecord.

private Object fromCouchDBRecord(final Schema fieldSchema, final String docf, final Object value) throws GoraException {
    final Object innerValue = ((Map) value).get(docf);
    if (innerValue == null) {
        return null;
    }
    Class<?> clazz = null;
    try {
        clazz = ClassLoadingUtils.loadClass(fieldSchema.getFullName());
    } catch (ClassNotFoundException e) {
        throw new GoraException(e);
    }
    final PersistentBase record = (PersistentBase) new BeanFactoryImpl(keyClass, clazz).newPersistent();
    for (Field recField : fieldSchema.getFields()) {
        Schema innerSchema = recField.schema();
        record.put(recField.pos(), fromDBObject(innerSchema, recField, recField.name(), innerValue));
    }
    return record;
}
Also used : Field(org.apache.avro.Schema.Field) GoraException(org.apache.gora.util.GoraException) PersistentBase(org.apache.gora.persistency.impl.PersistentBase) Schema(org.apache.avro.Schema) BeanFactoryImpl(org.apache.gora.persistency.impl.BeanFactoryImpl) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 3 with BeanFactoryImpl

use of org.apache.gora.persistency.impl.BeanFactoryImpl in project gora by apache.

the class JCacheGoraDataStoreTest method testGetWithFields.

@Test
public void testGetWithFields() throws Exception {
    DataStore<String, WebPage> store = super.webPageStore;
    BeanFactory<String, WebPage> beanFactory = new BeanFactoryImpl<>(String.class, WebPage.class);
    store.setBeanFactory(beanFactory);
    WebPageDataCreator.createWebPageData(store);
    String[] interestFields = new String[2];
    interestFields[0] = "url";
    interestFields[1] = "content";
    WebPage page = store.get(URLS[1], interestFields);
    assertNotNull(page);
    assertNotNull(page.getUrl());
    assertEquals(page.getUrl().toString(), URLS[1]);
    assertNotNull(page.getContent());
    assertEquals("Map of Outlinks should have a size of '0' as it is omitted at retrieval", 0, page.getOutlinks().size());
    assertEquals("Map of Parsed Content should have a size of '0' as it is omitted at retrieval", 0, page.getParsedContent().size());
}
Also used : WebPage(org.apache.gora.examples.generated.WebPage) BeanFactoryImpl(org.apache.gora.persistency.impl.BeanFactoryImpl) Test(org.junit.Test)

Example 4 with BeanFactoryImpl

use of org.apache.gora.persistency.impl.BeanFactoryImpl in project gora by apache.

the class JCacheGoraDataStoreTest method testDeleteByQueryFields.

@Test
public void testDeleteByQueryFields() throws Exception {
    DataStore<String, WebPage> store = super.webPageStore;
    BeanFactory<String, WebPage> beanFactory = new BeanFactoryImpl<>(String.class, WebPage.class);
    store.setBeanFactory(beanFactory);
    Query<String, WebPage> query;
    WebPageDataCreator.createWebPageData(store);
    query = store.newQuery();
    query.setFields("outlinks", "parsedContent", "content");
    Query<String, WebPage> newQuery = store.newQuery();
    newQuery.setStartKey(SORTED_URLS[0]);
    newQuery.setEndKey(SORTED_URLS[9]);
    newQuery.setFields("outlinks", "parsedContent", "content");
    DataStoreTestUtil.assertNumResults(newQuery, URLS.length);
    store.deleteByQuery(query);
    store.deleteByQuery(query);
    store.deleteByQuery(query);
    store.flush();
    DataStoreTestUtil.assertNumResults(store.newQuery(), URLS.length);
    for (String SORTED_URL : SORTED_URLS) {
        WebPage page = store.get(SORTED_URL);
        assertNotNull(page);
        assertNotNull(page.getUrl());
        assertEquals(page.getUrl().toString(), SORTED_URL);
        assertEquals("Map of Outlinks should have a size of '0' as the deleteByQuery " + "not only removes the data but also the data structure.", 0, page.getOutlinks().size());
        assertEquals(0, page.getParsedContent().size());
        if (page.getContent() != null) {
            LOG.info("url:" + page.getUrl().toString());
            LOG.info("limit:" + page.getContent().limit());
        } else {
            assertNull(page.getContent());
        }
    }
    WebPageDataCreator.createWebPageData(store);
    query = store.newQuery();
    query.setFields("url");
    String startKey = SORTED_URLS[NUM_KEYS];
    String endKey = SORTED_URLS[SORTED_URLS.length - NUM_KEYS];
    query.setStartKey(startKey);
    query.setEndKey(endKey);
    DataStoreTestUtil.assertNumResults(store.newQuery(), URLS.length);
    store.deleteByQuery(query);
    store.deleteByQuery(query);
    store.deleteByQuery(query);
    store.flush();
    DataStoreTestUtil.assertNumResults(store.newQuery(), URLS.length);
    for (int i = 0; i < URLS.length; i++) {
        WebPage page = store.get(URLS[i]);
        assertNotNull(page);
        if (URLS[i].compareTo(startKey) < 0 || URLS[i].compareTo(endKey) > 0) {
            DataStoreTestUtil.assertWebPage(page, i);
        } else {
            assertNull(page.getUrl());
            assertNotNull(page.getOutlinks());
            assertNotNull(page.getParsedContent());
            assertNotNull(page.getContent());
            assertTrue(page.getOutlinks().size() > 0);
            assertTrue(page.getParsedContent().size() > 0);
        }
    }
}
Also used : WebPage(org.apache.gora.examples.generated.WebPage) BeanFactoryImpl(org.apache.gora.persistency.impl.BeanFactoryImpl) Test(org.junit.Test)

Example 5 with BeanFactoryImpl

use of org.apache.gora.persistency.impl.BeanFactoryImpl in project gora by apache.

the class MongoStore method fromMongoRecord.

@SuppressWarnings({ "unchecked", "rawtypes" })
private Object fromMongoRecord(final Schema fieldSchema, final String docf, final DBObject rec) {
    Object result;
    BSONDecorator innerBson = new BSONDecorator(rec);
    Class<?> clazz = null;
    try {
        clazz = ClassLoadingUtils.loadClass(fieldSchema.getFullName());
    } catch (ClassNotFoundException e) {
    }
    PersistentBase record = (PersistentBase) new BeanFactoryImpl(keyClass, clazz).newPersistent();
    for (Field recField : fieldSchema.getFields()) {
        Schema innerSchema = recField.schema();
        DocumentFieldType innerStoreType = mapping.getDocumentFieldType(innerSchema.getName());
        String innerDocField = mapping.getDocumentField(recField.name()) != null ? mapping.getDocumentField(recField.name()) : recField.name();
        String fieldPath = docf + "." + innerDocField;
        LOG.debug("Load from DBObject (RECORD), field:{}, schemaType:{}, docField:{}, storeType:{}", new Object[] { recField.name(), innerSchema.getType(), fieldPath, innerStoreType });
        record.put(recField.pos(), fromDBObject(innerSchema, innerStoreType, recField, innerDocField, innerBson));
    }
    result = record;
    return result;
}
Also used : Field(org.apache.avro.Schema.Field) PersistentBase(org.apache.gora.persistency.impl.PersistentBase) BSONDecorator(org.apache.gora.mongodb.utils.BSONDecorator) DocumentFieldType(org.apache.gora.mongodb.store.MongoMapping.DocumentFieldType) Schema(org.apache.avro.Schema) BeanFactoryImpl(org.apache.gora.persistency.impl.BeanFactoryImpl)

Aggregations

BeanFactoryImpl (org.apache.gora.persistency.impl.BeanFactoryImpl)12 PersistentBase (org.apache.gora.persistency.impl.PersistentBase)8 Schema (org.apache.avro.Schema)7 GoraException (org.apache.gora.util.GoraException)6 Field (org.apache.avro.Schema.Field)4 WebPage (org.apache.gora.examples.generated.WebPage)4 Test (org.junit.Test)4 IOException (java.io.IOException)2 DocumentFieldType (org.apache.gora.mongodb.store.MongoMapping.DocumentFieldType)2 BSONDecorator (org.apache.gora.mongodb.utils.BSONDecorator)2 MapObject (com.rethinkdb.model.MapObject)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Type (org.apache.avro.Schema.Type)1 Field (org.apache.gora.elasticsearch.mapping.Field)1 JSONObject (org.json.JSONObject)1