Example #1
0
    def insert_current(self, collection, obj, store_permanently=True):
        self.validate_collection(collection)
        obj = create_storage_obj(collection, obj)
        try:
            # Update `current` record. If one doesn't exist, insert one. This
            # combo is known as UPSERT (i.e. UPDATE or INSERT).
            upsert = True
            obj_id = self.current.replace_one({'type': collection}, obj, upsert).upserted_id
            if not store_permanently and not obj_id:
                # There wasn't a pre-existing record, so upserted_id was None.
                obj_id = self.get_current(collection)['_id']
        except Exception as e:
            self._warn("Problem inserting object into current collection: {}, {!r}".format(e, obj))
            obj_id = None

        if store_permanently:
            try:
                col = getattr(self, collection)
                obj_id = col.insert_one(obj).inserted_id
            except Exception as e:
                self._warn("Problem inserting object into collection: {}, {!r}".format(e, obj))
                obj_id = None

        if obj_id:
            return str(obj_id)
        return None
Example #2
0
    def insert_current(self, collection, obj, store_permanently=True):
        self.validate_collection(collection)
        obj_id = self._make_id()
        obj = create_storage_obj(collection, obj, obj_id=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:
            self._warn(
                f"Problem serializing object for insertion: {e} {current_fn} {obj!r}"
            )
            result = None

        if not store_permanently:
            return result

        collection_fn = self._get_file(collection)
        try:
            # Append obj to collection file.
            to_json(obj, filename=collection_fn, append=True)
            return obj_id
        except Exception as e:
            self._warn(
                "Problem inserting object into collection: {}, {!r}".format(
                    e, obj))
            return None
Example #3
0
 def insert(self, collection, obj):
     self.validate_collection(collection)
     try:
         obj = create_storage_obj(collection, obj)
         # Insert record into db
         col = getattr(self, collection)
         return col.insert_one(obj).inserted_id
     except Exception as e:
         self._warn("Problem inserting object into collection: {}, {!r}".format(e, obj))
         return None
Example #4
0
 def insert(self, collection, obj):
     self.validate_collection(collection)
     obj_id = self._make_id()
     obj = create_storage_obj(collection, obj, obj_id=obj_id)
     try:
         obj = to_json(obj)
     except Exception as e:
         self._warn("Problem inserting object into collection: {}, {!r}".format(e, obj))
         return None
     with self.lock:
         self.collections.setdefault(collection, {})[obj_id] = obj
     return obj_id
Example #5
0
 def insert(self, collection, obj):
     self.validate_collection(collection)
     obj_id = self._make_id()
     obj = create_storage_obj(collection, obj, obj_id=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:
         self._warn(
             "Problem inserting object into collection: {}, {!r}".format(
                 e, obj))
         return None