[vlma-devel] commit: Do not save data for every added media when adding a set of medias. (Adrien Grand )

git version control git at videolan.org
Sun Sep 14 21:49:11 CEST 2008


vlma | branch: master | Adrien Grand <jpountz at videolan.org> | Sun Sep 14 21:37:57 2008 +0200| [f3861c97c9571996f1918f78379f8b3c4a3319d4] | committer: Adrien Grand 

Do not save data for every added media when adding a set of medias.

> http://git.videolan.org/gitweb.cgi/vlma.git/?a=commit;h=f3861c97c9571996f1918f78379f8b3c4a3319d4
---

 .../src/main/java/org/videolan/vlma/DataImpl.java  |   15 ++++++-----
 .../main/java/org/videolan/vlma/dao/VLMaDao.java   |    3 ++
 .../java/org/videolan/vlma/dao/VLMaDaoImpl.java    |   27 ++++++++++++++++++-
 3 files changed, 36 insertions(+), 9 deletions(-)

diff --git a/vlma-core/src/main/java/org/videolan/vlma/DataImpl.java b/vlma-core/src/main/java/org/videolan/vlma/DataImpl.java
index e2dbda5..077ac1b 100644
--- a/vlma-core/src/main/java/org/videolan/vlma/DataImpl.java
+++ b/vlma-core/src/main/java/org/videolan/vlma/DataImpl.java
@@ -300,9 +300,9 @@ public class DataImpl implements Data {
         tvFilter.add("R-DIG");
         tvFilter.add("R-DIG-CRYPT");
         tvFilter.add("TV-HD");
-        Set<SatChannel> newMedias = new HashSet<SatChannel>();
+        Set<Media> newMedias = new HashSet<Media>();
         newMedias.clear();
-        logger.info("Downloading " + source.toString() + ".");
+        logger.info("Downloading " + source.toString());
 
         try {
             Parser parser = new Parser(source.openConnection());
@@ -369,9 +369,9 @@ public class DataImpl implements Data {
         }
 
         List<Media> medias = vlmaDao.getMedias();
-        for (SatChannel ch : newMedias) {
+        for (Media ch : newMedias) {
             logger.debug("Adding " + ch.getName());
-            coverages.add(ch.getCoverage());
+            coverages.add(((SatChannel)ch).getCoverage());
             for (Media media : medias) {
                 if (ch.equals(media))
                     ch.setProgram(media.getProgram());
@@ -379,12 +379,13 @@ public class DataImpl implements Data {
         }
 
         logger.debug("Got " + newMedias.size() + " channels");
+        List<Media> mediasToRemove = new ArrayList<Media>();
         for (Media ch : medias) {
             if (ch instanceof SatChannel && coverages.contains(((SatChannel)ch).getCoverage()))
-                vlmaDao.remove(ch);
+                mediasToRemove.add(ch);
         }
-        for (Media media : newMedias)
-            vlmaDao.add(media);
+        vlmaDao.removeAll(mediasToRemove);
+        vlmaDao.addAll(newMedias);
         logger.info("Ending analysis of " + source.toString());
     }
 
diff --git a/vlma-core/src/main/java/org/videolan/vlma/dao/VLMaDao.java b/vlma-core/src/main/java/org/videolan/vlma/dao/VLMaDao.java
index df9447c..b160205 100644
--- a/vlma-core/src/main/java/org/videolan/vlma/dao/VLMaDao.java
+++ b/vlma-core/src/main/java/org/videolan/vlma/dao/VLMaDao.java
@@ -20,6 +20,7 @@
 
 package org.videolan.vlma.dao;
 
+import java.util.Collection;
 import java.util.List;
 
 import org.videolan.vlma.model.Media;
@@ -40,10 +41,12 @@ public interface VLMaDao {
     void update(Satellite satellite);
     void update(Server server);
 
+    void addAll(Collection<Media> medias);
     void add(Media media);
     void add(Satellite satellite);
     void add(Server server);
 
+    void removeAll(Collection<Media> medias);
     void remove(Media media);
     void remove(Satellite satellite);
     void remove(Server server);
diff --git a/vlma-core/src/main/java/org/videolan/vlma/dao/VLMaDaoImpl.java b/vlma-core/src/main/java/org/videolan/vlma/dao/VLMaDaoImpl.java
index 08b031b..681c95a 100644
--- a/vlma-core/src/main/java/org/videolan/vlma/dao/VLMaDaoImpl.java
+++ b/vlma-core/src/main/java/org/videolan/vlma/dao/VLMaDaoImpl.java
@@ -26,6 +26,7 @@ import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
@@ -93,7 +94,19 @@ public class VLMaDaoImpl implements VLMaDao {
         servers = new HashMap<Integer, Server>();
     }
 
+    public void addAll(Collection<Media> medias) {
+        for(Media media : medias) {
+            addInternal(media);
+        }
+        saveToDisk();
+    }
+
     public void add(Media media) {
+        addInternal(media);
+        saveToDisk();
+    }
+
+    private void addInternal(Media media) {
         Integer id;
         synchronized (medias) {
             do {
@@ -102,7 +115,6 @@ public class VLMaDaoImpl implements VLMaDao {
             media.setId(id);
             medias.put(id, media);
         }
-        saveToDisk();
     }
 
     public void add(Satellite satellite) {
@@ -186,12 +198,23 @@ public class VLMaDaoImpl implements VLMaDao {
         }
     }
 
+    public void removeAll(Collection<Media> medias) {
+        for(Media media : medias) {
+            removeInternal(media);
+        }
+        saveToDisk();
+    }
+
     public void remove(Media media) {
+        removeInternal(media);
+        saveToDisk();
+    }
+
+    private void removeInternal(Media media) {
         int id = media.getId();
         synchronized (medias) {
             if (medias.containsKey(id)) {
                 medias.remove(id);
-                saveToDisk();
             } else {
                 throw new NotFoundException("Media " + media.getName() + " is not in the database.");
             }



More information about the vlma-devel mailing list