Пример #1
0
 def insert(self, collection, obj):
     obj_id = self._make_id()
     obj = create_storage_obj(collection, obj, obj_id)
     collection_fn = self._get_file(collection)
     try:
         # Insert record into file
         to_json(obj, filename=collection_fn)
         return obj_id
     except Exception as e:
         raise error.InvalidSerialization(
             f"Problem inserting object into collection: {e}, {obj!r}")
Пример #2
0
    def insert(self, collection, obj):
        obj_id = self._make_id()
        obj = create_storage_obj(collection, obj, obj_id)
        try:
            obj = to_json(obj)
        except Exception as e:
            raise error.InvalidSerialization(
                f"Problem inserting object into collection: {e}, {obj!r}")

        with self.lock:
            self.collections.setdefault(collection, {})[obj_id] = obj
        return obj_id
Пример #3
0
    def insert_current(self, collection, obj, store_permanently=True):
        obj_id = self._make_id()
        obj = create_storage_obj(collection, obj, obj_id)
        try:
            obj = to_json(obj)
        except Exception as e:
            raise error.InvalidSerialization(
                f"Problem serializing object for insertion: {e} {obj!r}")

        with self.lock:
            self.current[collection] = obj
            if store_permanently:
                self.collections.setdefault(collection, {})[obj_id] = obj
        return obj_id
Пример #4
0
    def insert_current(self, collection, obj, store_permanently=True):
        obj_id = self._make_id()
        obj = create_storage_obj(collection, obj, obj_id)
        current_fn = self._get_file(collection, permanent=False)
        result = obj_id

        try:
            # Overwrite current collection file with obj.
            to_json(obj, filename=current_fn, append=False)
        except Exception as e:
            raise error.InvalidSerialization(
                f"Problem serializing object for insertion: {e} {current_fn} {obj!r}"
            )

        if not store_permanently:
            return result
        else:
            return self.insert(collection, obj)