From c38a452d4f4cad4a92ea7d3dc646de966a25d962 Mon Sep 17 00:00:00 2001
From: "Antoine R. Dumont (@ardumont)" <antoine.romain.dumont@gmail.com>
Date: Sat, 25 Feb 2017 01:04:51 +0100
Subject: [PATCH] archiver.storage: Add content_archive_content_add endpoint

Related T494
---
 swh/storage/archiver/db.py      | 29 ++++++++++++++++++++++++++++-
 swh/storage/archiver/storage.py | 15 +++++++++++++++
 2 files changed, 43 insertions(+), 1 deletion(-)

diff --git a/swh/storage/archiver/db.py b/swh/storage/archiver/db.py
index a18aabee4..917e06f9b 100644
--- a/swh/storage/archiver/db.py
+++ b/swh/storage/archiver/db.py
@@ -1,9 +1,10 @@
-# Copyright (C) 2015-2016  The Software Heritage developers
+# Copyright (C) 2015-2017  The Software Heritage developers
 # See the AUTHORS file at the top-level directory of this distribution
 # License: GNU General Public License version 3, or any later version
 # See top-level LICENSE file for more information
 
 
+import json
 import time
 
 from swh.core import hashutil
@@ -258,3 +259,29 @@ class ArchiverDb(BaseDb):
 
         cur = self._cursor(cur)
         cur.execute(query)
+
+    def content_archive_content_add(
+            self, content_id, sources_present, sources_missing, cur=None):
+
+        if isinstance(content_id, bytes):
+            content_id = '\\x%s' % hashutil.hash_to_hex(content_id)
+
+        copies = {}
+        num_present = 0
+        for source in sources_present:
+            copies[source] = {
+                "status": "present",
+                "mtime": int(time.time()),
+            }
+            num_present += 1
+
+        for source in sources_missing:
+            copies[source] = {
+                "status": "absent",
+            }
+
+        query = """INSERT INTO content_archive(content_id, copies, num_present)
+                   VALUES('%s', '%s', %s)
+                    """ % (content_id, json.dumps(copies), num_present)
+        cur = self._cursor(cur)
+        cur.execute(query)
diff --git a/swh/storage/archiver/storage.py b/swh/storage/archiver/storage.py
index b207a7047..fce3c94a4 100644
--- a/swh/storage/archiver/storage.py
+++ b/swh/storage/archiver/storage.py
@@ -166,3 +166,18 @@ class ArchiverStorage():
 
         """
         self.db.content_archive_insert(content_id, source, status, cur)
+
+    @db_transaction
+    def content_archive_content_add(
+            self, content_ids, sources_present, sources_missing, cur=None):
+        """Insert a new entry in db about content_id.
+
+        Args:
+            content_ids ([bytes|str]): content identifiers
+            sources_present: name of the source where the contents are present
+            sources_missing: name of the source where the contents are missing
+
+        """
+        for content_id in content_ids:
+            self.db.content_archive_content_add(
+                content_id, sources_present, sources_missing)
-- 
GitLab