X-Git-Url: https://git.jsancho.org/?p=mojodb.git;a=blobdiff_plain;f=collection.py;h=1dad48846b9077e9e08e3742c5819ca29c494926;hp=664961a040680aebadf96e4e3724e1e765713b9c;hb=2c1872fd5004c8464e146081b61783de47b87e2d;hpb=792f961fb96bb8533e540970c54f43b958d77296 diff --git a/collection.py b/collection.py index 664961a..1dad488 100644 --- a/collection.py +++ b/collection.py @@ -19,14 +19,14 @@ # ############################################################################## -import cPickle +import msgpack from cursor import Cursor import uuid class Collection(object): def __init__(self, database, table_name): self.database = database - self.table_name = unicode(table_name) + self.table_name = str(table_name) def __repr__(self): return "Collection(%r, %r)" % (self.database, self.table_name) @@ -34,15 +34,9 @@ class Collection(object): def exists(self): return (self.database.exists() and self.table_name in self.database.collection_names()) - def _create_table(self): - fields = [ - {'name': 'id', 'type': 'char', 'size': 32, 'primary': True}, - ] - return self.database.connection._create_table(self.database.db_name, '%s$_id' % self.table_name, fields) - def _create_field(self, field_name): fields = [ - {'name': 'id', 'type': 'char', 'size': 32, 'primary': True}, + {'name': 'id', 'type': 'char', 'size': 512, 'primary': True}, {'name': 'value', 'type': 'text', 'null': False}, {'name': 'number', 'type': 'float'}, ] @@ -50,7 +44,7 @@ class Collection(object): def _get_fields(self): tables = self.database.connection._get_tables(self.database.db_name) - return [unicode(x[x.find('$')+1:]) for x in filter(lambda x: x.startswith('%s$' % self.table_name), tables)] + return [str(x[x.find('$')+1:]) for x in filter(lambda x: x.startswith('%s$' % self.table_name), tables)] def count(self): return self.database.connection._count(self.database.db_name, self.table_name) @@ -62,36 +56,33 @@ class Collection(object): if not self.database.db_name in self.database.connection.database_names(): self.database._create_database() if not self.table_name in self.database.collection_names(): - self._create_table() + self._create_field('_id') if not type(doc_or_docs) in (list, tuple): docs = [doc_or_docs] else: docs = doc_or_docs for doc in docs: + doc_id = uuid.uuid4().hex if not '_id' in doc: - doc['_id'] = uuid.uuid4().hex - self._insert_document(doc) + doc['_id'] = doc_id + self._insert_document(doc_id, doc) if type(doc_or_docs) in (list, tuple): return [d['_id'] for d in docs] else: return docs[0]['_id'] - def _insert_document(self, doc): - table_id = '%s$_id' % self.table_name + def _insert_document(self, doc_id, doc): fields = self._get_fields() - self.database.connection._insert(self.database.db_name, table_id, {'id': doc['_id']}) for f in doc: - if f == '_id': - continue if not f in fields: self._create_field(f) - table_f = '%s$%s' % (self.table_name, f) values = { - 'id': doc['_id'], - 'value': cPickle.dumps(doc[f]), + 'id': doc_id, + 'value': msgpack.dumps(doc[f]), } if type(doc[f]) in (int, float): values['number'] = doc[f] + table_f = '%s$%s' % (self.table_name, f) self.database.connection._insert(self.database.db_name, table_f, values)