Package ai.platon.gora.mongodb.store
Class MongoStore<K,T extends ai.platon.gora.persistency.impl.PersistentBase>
java.lang.Object
ai.platon.gora.store.impl.DataStoreBase<K,T>
ai.platon.gora.mongodb.store.MongoStore<K,T>
- Type Parameters:
K- class to be used for the keyT- class to be persisted within the store
- All Implemented Interfaces:
ai.platon.gora.store.DataStore<K,,T> Closeable,AutoCloseable,org.apache.hadoop.conf.Configurable,org.apache.hadoop.io.Writable
public class MongoStore<K,T extends ai.platon.gora.persistency.impl.PersistentBase>
extends ai.platon.gora.store.impl.DataStoreBase<K,T>
Implementation of a MongoDB data store to be used by gora.
- Author:
- Fabien Poulard fpoulard@dictanova.com, Damien Raude-Morvan draudemorvan@dictanova.com
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringDefault value for mapping filestatic final org.slf4j.Loggerstatic final StringKey at DataStore Properties (same as gora.properties) to hold a mapping loaded from memory, instead from filesystem like the one at PARSE_MAPPING_FILE_KEY.Fields inherited from class ai.platon.gora.store.impl.DataStoreBase
autoCreateSchema, beanFactory, conf, datumReader, datumWriter, fieldMap, keyClass, persistentClass, properties, schema -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Release the resources linked to this collectionvoidCreate a new collection in MongoDB if necessary.decodeFieldKey(String key) Ensure Key decoding -> middle dots replaced with dotsbooleanvoidDrop the collection.encodeFieldKey(String key) Ensure Key encoding -> dots replaced with middle dotsexecuteQuery(ai.platon.gora.query.Query<K, T> query) Execute the query and return the result.booleanvoidflush()Ensure the data is synced to disk.Retrieve an entry from the store with only selected fields.static com.mongodb.client.MongoClientgetClient(MongoStoreParameters params) Retrieve a client connected to the MongoDB server to be used.getPartitions(ai.platon.gora.query.Query<K, T> query) Partitions the given query and returns a list of PartitionQuerys, which will execute on local data.Accessor to the name of the collection used.getSchemaName(String mappingSchemaName, Class<?> persistentClass) voidinitialize(Class<K> keyClass, Class<T> pPersistentClass, Properties properties) Initialize the data store by reading the credentials, setting the client's properties up and reading the mapping file.newInstance(org.bson.Document obj, String[] fields) Build a new instance of the persisted class from theDocumentretrieved from the database.newQuery()Create a newQueryto query the datastore.voidPersist an object into the store.booleanCheck if the collection already exists or should be created.Methods inherited from class ai.platon.gora.store.impl.DataStoreBase
equals, get, getBeanFactory, getConf, getFields, getFieldsToQuery, getKeyClass, getOrCreateConf, getPersistentClass, newKey, newPersistent, readFields, setBeanFactory, setConf, setKeyClass, setPersistentClass, truncateSchema, write
-
Field Details
-
LOG
public static final org.slf4j.Logger LOG -
DEFAULT_MAPPING_FILE
Default value for mapping file- See Also:
-
XML_MAPPING_DEFINITION
Key at DataStore Properties (same as gora.properties) to hold a mapping loaded from memory, instead from filesystem like the one at PARSE_MAPPING_FILE_KEY. If this key is present, the mapping is loaded from the value instead of gora-mongodb-mapping.xml- See Also:
-
-
Constructor Details
-
MongoStore
public MongoStore()
-
-
Method Details
-
initialize
public void initialize(Class<K> keyClass, Class<T> pPersistentClass, Properties properties) throws ai.platon.gora.util.GoraException Initialize the data store by reading the credentials, setting the client's properties up and reading the mapping file.- Specified by:
initializein interfaceai.platon.gora.store.DataStore<K,T extends ai.platon.gora.persistency.impl.PersistentBase> - Overrides:
initializein classai.platon.gora.store.impl.DataStoreBase<K,T extends ai.platon.gora.persistency.impl.PersistentBase> - Throws:
ai.platon.gora.util.GoraException
-
getClient
Retrieve a client connected to the MongoDB server to be used.- Parameters:
params- This value should specify the host:port (at least one) for connecting to remote MongoDB.- Returns:
- a
MongoClientinstance connected to the server
-
getMapping
-
getSchemaName
Accessor to the name of the collection used. -
getSchemaName
-
createSchema
public void createSchema() throws ai.platon.gora.util.GoraExceptionCreate a new collection in MongoDB if necessary.- Throws:
ai.platon.gora.util.GoraException
-
deleteSchema
public void deleteSchema() throws ai.platon.gora.util.GoraExceptionDrop the collection.- Throws:
ai.platon.gora.util.GoraException
-
schemaExists
public boolean schemaExists() throws ai.platon.gora.util.GoraExceptionCheck if the collection already exists or should be created.- Throws:
ai.platon.gora.util.GoraException
-
flush
public void flush() throws ai.platon.gora.util.GoraExceptionEnsure the data is synced to disk.- Throws:
ai.platon.gora.util.GoraException
-
close
public void close()Release the resources linked to this collection -
get
Retrieve an entry from the store with only selected fields.- Parameters:
key- identifier of the document in the databasefields- list of fields to be loaded from the database- Throws:
ai.platon.gora.util.GoraException
-
exists
- Throws:
ai.platon.gora.util.GoraException
-
put
Persist an object into the store.- Parameters:
key- identifier of the object in the storeobj- the object to be inserted- Throws:
ai.platon.gora.util.GoraException
-
delete
- Throws:
ai.platon.gora.util.GoraException
-
executeQuery
public ai.platon.gora.query.Result<K,T> executeQuery(ai.platon.gora.query.Query<K, T> query) throws IOExceptionExecute the query and return the result.- Specified by:
executeQueryin classai.platon.gora.store.impl.DataStoreBase<K,T extends ai.platon.gora.persistency.impl.PersistentBase> - Throws:
IOException
-
newQuery
Create a newQueryto query the datastore. -
getPartitions
public List<ai.platon.gora.query.PartitionQuery<K,T>> getPartitions(ai.platon.gora.query.Query<K, T> query) throws IOExceptionPartitions the given query and returns a list of PartitionQuerys, which will execute on local data.- Throws:
IOException
-
newInstance
public T newInstance(org.bson.Document obj, String[] fields) throws ai.platon.gora.util.GoraException Build a new instance of the persisted class from theDocumentretrieved from the database.- Parameters:
obj- theDocumentthat results from the query to the databasefields- the list of fields to be mapped to the persistence class instance- Returns:
- a persistence class instance which content was deserialized from
the
Document - Throws:
ai.platon.gora.util.GoraException
-
encodeFieldKey
Ensure Key encoding -> dots replaced with middle dots- Parameters:
key- char with only dots.- Returns:
- encoded string with "·" chars..
-
decodeFieldKey
Ensure Key decoding -> middle dots replaced with dots- Parameters:
key- encoded string with "·" chars.- Returns:
- Cleanup up char with only dots.
-