<commit_before>import rospy
import ros_datacentre_msgs.srv as dc_srv
import ros_datacentre.util as dc_util
from ros_datacentre_msgs.msg import StringPair, StringPairList, SerialisedMessage
from bson import json_util
from bson.objectid import ObjectId
import copy


class MessageStoreProxy:
	def __init__(self, service_prefix='/message_store', database='message_store', collection='message_store'):
		self.database = database
		self.collection = collection
		insert_service = service_prefix + '/insert'
		update_service = service_prefix + '/update'
		query_ids_service = service_prefix + '/query_messages'
		rospy.logdebug("Waiting for services...")
		rospy.wait_for_service(insert_service)
		rospy.wait_for_service(update_service)
		rospy.wait_for_service(query_ids_service)
		rospy.logdebug("Done")
		self.insert_srv = rospy.ServiceProxy(insert_service, dc_srv.MongoInsertMsg)
		self.update_srv = rospy.ServiceProxy(update_service, dc_srv.MongoUpdateMsg)
		self.query_id_srv = rospy.ServiceProxy(query_ids_service, dc_srv.MongoQueryMsg)


	def insert_named(self, name, message, meta = {}):
		# create a copy as we're modifying it
		meta_copy = copy.copy(meta)
		meta_copy["name"] = name
		return self.insert(message, meta_copy)
	

	def insert(self, message, meta = {}):
		# assume meta is a dict, convert k/v to tuple pairs 
		meta_tuple = (StringPair(dc_srv.MongoQueryMsgRequest.JSON_QUERY, json_util.dumps(meta)),)
		serialised_msg = dc_util.serialise_message(message)
		return self.insert_srv(self.database, self.collection, serialised_msg, StringPairList(meta_tuple))

	def query_id(self, id, type):
		return self.query(type, {'_id': ObjectId(id)})

	def query_named(self, name, type, single = True, meta = {}):
		# create a copy as we're modifying it
		meta_copy = copy.copy(meta)
		meta_copy["name"] = name
		return self.query(type, {}, meta_copy, single)

	def update_named(self, name, message, meta = {}, upsert = False):
		meta_query = {}
		meta_query["name"] = name

		# make sure the name goes into the meta info after update<commit_msg>Made id query return single element.<commit_after>import rospy
import ros_datacentre_msgs.srv as dc_srv
import ros_datacentre.util as dc_util
from ros_datacentre_msgs.msg import StringPair, StringPairList, SerialisedMessage
from bson import json_util
from bson.objectid import ObjectId
import copy


class MessageStoreProxy:
	def __init__(self, service_prefix='/message_store', database='message_store', collection='message_store'):
		self.database = database
		self.collection = collection
		insert_service = service_prefix + '/insert'
		update_service = service_prefix + '/update'
		query_ids_service = service_prefix + '/query_messages'
		rospy.logdebug("Waiting for services...")
		rospy.wait_for_service(insert_service)
		rospy.wait_for_service(update_service)
		rospy.wait_for_service(query_ids_service)
		rospy.logdebug("Done")
		self.insert_srv = rospy.ServiceProxy(insert_service, dc_srv.MongoInsertMsg)
		self.update_srv = rospy.ServiceProxy(update_service, dc_srv.MongoUpdateMsg)
		self.query_id_srv = rospy.ServiceProxy(query_ids_service, dc_srv.MongoQueryMsg)


	def insert_named(self, name, message, meta = {}):
		# create a copy as we're modifying it
		meta_copy = copy.copy(meta)
		meta_copy["name"] = name
		return self.insert(message, meta_copy)
	

	def insert(self, message, meta = {}):
		# assume meta is a dict, convert k/v to tuple pairs 
		meta_tuple = (StringPair(dc_srv.MongoQueryMsgRequest.JSON_QUERY, json_util.dumps(meta)),)
		serialised_msg = dc_util.serialise_message(message)
		return self.insert_srv(self.database, self.collection, serialised_msg, StringPairList(meta_tuple))

	def query_id(self, id, type):
		return self.query(type, {'_id': ObjectId(id)}, {}, True)

	def query_named(self, name, type, single = True, meta = {}):
		# create a copy as we're modifying it
		meta_copy = copy.copy(meta)
		meta_copy["name"] = name
		return self.query(type, {}, meta_copy, single)

	def update_named(self, name, message, meta = {}, upsert = False):
		meta_query = {}
		meta_query["name"] = name

		# make sure the name goes into the meta info after update
{'fraction_non_alphanumeric': 0.10530137981118373, 'fraction_numerical': 0.0, 'mean_word_length': 5.6537842190016105, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6640936', 'n_tokens_mistral': 1465, 'n_tokens_neox': 1376, 'n_words': 317}