Skip to content

Commit

Permalink
Delete stops before adding them so that replications work.
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Conway committed Apr 22, 2012
1 parent f3fe802 commit 247d3af
Showing 1 changed file with 14 additions and 17 deletions.
31 changes: 14 additions & 17 deletions src/main/java/org/transitappliance/loader/CouchDbStopSaverImpl.java
Expand Up @@ -22,12 +22,12 @@
import com.fourspaces.couchdb.Session;
import com.fourspaces.couchdb.Database;
import com.fourspaces.couchdb.Document;
import com.fourspaces.couchdb.ViewResults;

public class CouchDbStopSaverImpl implements StopSaver {
private Session session;
private Database db;
private ArrayList<Document> stops = new ArrayList<Document>();
private Document agency;

// spring
private String dbHost;
Expand Down Expand Up @@ -136,31 +136,28 @@ public void saveAgency (TAAgency agency) {
doc.put("avl_service", agency.avl_service);
doc.put("rights_notice", agency.rights_notice);

this.agency = doc;
// it's not really a stop, but it should be serialized as well
stops.add(doc);
}

public void serialize () {
Document[] template = new Document[1];

// TODO: remove all documents

try {
if (bulkUpdate) {
db.bulkSaveDocuments(stops.toArray(template));
}

else {
// one-by-one, for CouchDB 1 compatibility
for (Document doc : stops) {
System.out.println("Saving stop");
db.saveDocument(doc);
}
// delete all existing stops
ViewResults allDocs = db.getAllDocuments();
for (Document d : allDocs.getResults()) {
try {
db.deleteDocument(d);
} catch (IOException e) {
System.out.println("Error deleting " + d.getId() +". Unpredictable things may occur.");
}
}

db.saveDocument(agency);

try {
db.bulkSaveDocuments(stops.toArray(template));
} catch (IOException e) {
System.out.println("Error saving stops");
System.exit(1);
}

System.out.println("Stops saved");
Expand Down

0 comments on commit 247d3af

Please sign in to comment.