def increment(self, _id, field, inc): str_doc = OJAIDocument().set_id(_id=_id).as_json_str() from mapr.ojai.document.OJAIDocumentMutation import \ OJAIDocumentMutation str_mutation = self.__get_str_mutation( OJAIDocumentMutation().increment(field_path=field, inc=inc)) self.__execute_update(_id=str_doc, mutation=str_mutation)
def test_update_document_merge(self): connection = ConnectionFactory.get_connection( connection_str=CONNECTION_STR, options=CONNECTION_OPTIONS) if connection.is_store_exists(store_path=UpdateTest.store_name): document_store = connection.get_store( store_path=UpdateTest.store_name) else: document_store = connection.create_store( store_path=UpdateTest.store_name) self.assertEqual( { '_id': 'id11', 'test_int': 51, 'test_str': 'strstr', 'test_dict': { 'test_int': 5 } }, document_store.find_by_id('id11')) mutation = OJAIDocumentMutation().merge('test_dict', { 'd': 55, 'g': 'text' }) document_store.update('id11', mutation) self.assertEqual( { '_id': 'id11', 'test_int': 51, 'test_str': 'strstr', 'test_dict': { 'test_int': 5, 'd': 55, 'g': 'text' } }, document_store.find_by_id('id11'))
def test_update_document_put(self): connection = ConnectionFactory.get_connection( connection_str=CONNECTION_STR, options=CONNECTION_OPTIONS) if connection.is_store_exists(store_path=UpdateTest.store_name): document_store = connection.get_store( store_path=UpdateTest.store_name) else: document_store = connection.create_store( store_path=UpdateTest.store_name) self.assertEqual({ '_id': 'id06', 'test_int': 51, 'test_str': 'strstr' }, document_store.find_by_id('id06')) mutation = OJAIDocumentMutation().set_or_replace('test_int', 50) document_store.update('id06', mutation) self.assertEqual({ '_id': 'id06', 'test_int': 50, 'test_str': 'strstr' }, document_store.find_by_id('id06')) document_store.update('id06', {"$put": {"a.x": 1}}) self.assertEqual( { '_id': 'id06', 'test_int': 50, 'test_str': 'strstr', 'a': { 'x': 1 } }, document_store.find_by_id('id06'))
def test_update_document_append(self): connection = ConnectionFactory.get_connection( connection_str=CONNECTION_STR, options=CONNECTION_OPTIONS) if connection.is_store_exists(store_path=UpdateTest.store_name): document_store = connection.get_store( store_path=UpdateTest.store_name) else: document_store = connection.create_store( store_path=UpdateTest.store_name) self.assertEqual( { '_id': 'id09', 'test_int': 51, 'test_str': 'strstr', 'test_list': [5, 6] }, document_store.find_by_id('id09')) mutation = OJAIDocumentMutation().append('test_list', [{ 'name': 'Jo' }, 7, 8]) document_store.update('id09', mutation) self.assertEqual( { '_id': 'id09', 'test_int': 51, 'test_str': 'strstr', 'test_list': [5, 6, { 'name': 'Jo' }, 7, 8] }, document_store.find_by_id('id09'))
def test_update_bytearray(self): connection = ConnectionFactory.get_connection( connection_str=CONNECTION_STR, options=CONNECTION_OPTIONS) if connection.is_store_exists(store_path=UpdateTest.store_name): document_store = connection.get_store( store_path=UpdateTest.store_name) else: document_store = connection.create_store( store_path=UpdateTest.store_name) doct = {'_id': 'Hty', 'oho': 55} doc = connection.new_document(dictionary=doct) document_store.insert_or_replace(doc) mut = OJAIDocumentMutation() mut.set_or_replace('b', bytearray(b'\x06\x06')) document_store.update(_id='Hty', mutation=mut) res = document_store.find_by_id(_id='Hty') self.assertEqual({ '_id': 'Hty', 'oho': 55, 'b': bytearray(b'\x06\x06') }, res)
def test_check_and_update(self): connection = ConnectionFactory.get_connection( connection_str=CONNECTION_STR, options=CONNECTION_OPTIONS) if connection.is_store_exists(store_path=UpdateTest.store_name): document_store = connection.get_store( store_path=UpdateTest.store_name) else: document_store = connection.create_store( store_path=UpdateTest.store_name) self.assertEqual( { '_id': 'id02', 'mystr': 'str', 'test_int': 51, 'test_str': 'strstr' }, document_store.find_by_id('id02')) mutation = OJAIDocumentMutation().set_or_replace( 'new_field', { 'd': 55, 'g': 'text' }) from mapr.ojai.ojai_query.OJAIQueryCondition import OJAIQueryCondition false_condition = OJAIQueryCondition().equals_( 'test_str', 'rtsrts').close().build() self.assertFalse( document_store.check_and_update('id02', mutation=mutation, query_condition=false_condition)) self.assertEqual( { '_id': 'id02', 'mystr': 'str', 'test_int': 51, 'test_str': 'strstr' }, document_store.find_by_id('id02')) true_condition = OJAIQueryCondition().equals_( 'test_str', 'strstr').close().build() document_store.check_and_update('id02', mutation=mutation, query_condition=true_condition) self.assertEqual( { '_id': 'id02', 'mystr': 'str', 'test_int': 51, 'test_str': 'strstr', 'new_field': { 'd': 55, 'g': 'text' } }, document_store.find_by_id('id02'))
def test_huge_operations(self): connection = ConnectionFactory.get_connection(CONNECTION_STR) if connection.is_store_exists(store_path='/long-run-test-store'): connection.delete_store(store_path='/long-run-test-store') document_store = connection.create_store(store_path='/long-run-test-store') document_list = [] print('Start generation.') for i in range(0, 10000): document_list.append(connection.new_document(dictionary={'_id': 'id00%s' % i, 'test_int': i, 'test_str': 'strstr', 'test_dict': {'test_int': i}, 'test_list': [5, 6], 'test_null': None})) print('Finish generation.') print('List size {0}'.format(len(document_list))) print('Start insert_or_replace') document_store.insert_or_replace(doc_stream=document_list) print('Finish insert_or_replace') print('Start replace') document_store.replace(doc_stream=document_list) print('Finish replace') print('Start find') doc_stream = document_store.find() print('Finish loop.') stream_size = 0 print('Start loop through document stream.') for _ in doc_stream: stream_size += 1 self.assertEqual(stream_size, 10000) print('Finish loop through document stream.') mutation = OJAIDocumentMutation().set_or_replace('test_str', 'new_string') print('Start update through loop.') for i in range(0, 10000): document_store.update('id00{0}'.format(i), mutation) print('Finish update through loop.') print('Start find after update.') doc_stream = document_store.find() print('Finsih find after update.') stream_size = 0 print('Start loop through document stream after update.') for doc in doc_stream: stream_size += 1 self.assertEqual(doc['test_str'], 'new_string') print('Finish loop through document stream after update.') self.assertEqual(stream_size, 10000)
def test_update_document_increment_decrement(self): connection = ConnectionFactory.get_connection( connection_str=CONNECTION_STR, options=CONNECTION_OPTIONS) if connection.is_store_exists(store_path=UpdateTest.store_name): document_store = connection.get_store( store_path=UpdateTest.store_name) else: document_store = connection.create_store( store_path=UpdateTest.store_name) self.assertEqual({ '_id': 'id07', 'test_int': 51, 'test_str': 'strstr' }, document_store.find_by_id('id07')) mutation = OJAIDocumentMutation().increment('test_int', 3) document_store.update('id07', mutation) self.assertEqual({ '_id': 'id07', 'test_int': 54, 'test_str': 'strstr' }, document_store.find_by_id('id07')) document_store.increment('id07', 'test_int', -3) self.assertEqual({ '_id': 'id07', 'test_int': 51, 'test_str': 'strstr' }, document_store.find_by_id('id07')) document_store.update('id07', OJAIDocumentMutation().decrement('test_int', 1)) self.assertEqual({ '_id': 'id07', 'test_int': 50, 'test_str': 'strstr' }, document_store.find_by_id('id07'))
def test_update_document_set(self): connection = ConnectionFactory.get_connection( connection_str=CONNECTION_STR, options=CONNECTION_OPTIONS) if connection.is_store_exists(store_path=UpdateTest.store_name): connection.delete_store(store_path=UpdateTest.store_name) document_store = connection.create_store( store_path=UpdateTest.store_name) for doc in DICT_STREAM_UPDATE: document = connection.new_document(dictionary=doc) document_store.insert_or_replace(doc=document) self.assertEqual( { '_id': 'id08', 'test_int': 51, 'test_str': 'strstr', 'test_dict': { 'test_int': 5 } }, document_store.find_by_id('id08')) mutation = OJAIDocumentMutation().set('a.b.c', 50) document_store.update('id08', mutation) self.assertEqual( { '_id': 'id08', 'test_int': 51, 'test_str': 'strstr', 'test_dict': { 'test_int': 5 }, 'a': { 'b': { 'c': 50 } } }, document_store.find_by_id('id08'))
def new_mutation(self): return OJAIDocumentMutation()