]> git.jsancho.org Git - mojodb.git/blobdiff - collection.py
msgpack instead cPickle (for multiple platforms) and str instead unicode (thinking...
[mojodb.git] / collection.py
index 664961a040680aebadf96e4e3724e1e765713b9c..bbc56cacfbc7fcf7aeb5619dc9932eea58a252ea 100644 (file)
 #
 ##############################################################################
 
-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)
@@ -36,13 +36,13 @@ class Collection(object):
 
     def _create_table(self):
         fields = [
-            {'name': 'id', 'type': 'char', 'size': 32, 'primary': True},
+            {'name': 'id', 'type': 'char', 'size': 512, '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 +50,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)
@@ -81,7 +81,8 @@ class Collection(object):
     def _insert_document(self, doc):
         table_id = '%s$_id' % self.table_name
         fields = self._get_fields()
-        self.database.connection._insert(self.database.db_name, table_id, {'id': doc['_id']})
+        coded_id = msgpack.dumps(doc['_id'])
+        self.database.connection._insert(self.database.db_name, table_id, {'id': coded_id})
         for f in doc:
             if f == '_id':
                 continue
@@ -89,8 +90,8 @@ class Collection(object):
                 self._create_field(f)
             table_f = '%s$%s' % (self.table_name, f)
             values = {
-                'id': doc['_id'],
-                'value': cPickle.dumps(doc[f]),
+                'id': coded_id,
+                'value': msgpack.dumps(doc[f]),
                 }
             if type(doc[f]) in (int, float):
                 values['number'] = doc[f]