Skip to content
This repository was archived by the owner on Mar 11, 2022. It is now read-only.

Commit f2d327d

Browse files
authored
Merge pull request #539 from cloudant/353-move-helpers
Hive off some utility methods to a "helpers" package
2 parents ecd204f + 67fc300 commit f2d327d

16 files changed

+280
-137
lines changed

cloudant-sync-datastore-core/src/main/java/com/cloudant/sync/internal/documentstore/AttachmentManager.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ protected static Attachment getAttachment(SQLDatabase db, String attachmentsDir,
316316
}
317317
}
318318

319-
protected static Map<String, ? extends Attachment> attachmentsForRevision(SQLDatabase db, String attachmentsDir,
319+
public static Map<String, ? extends Attachment> attachmentsForRevision(SQLDatabase db, String attachmentsDir,
320320
AttachmentStreamFactory attachmentStreamFactory,
321321
long sequence)
322322
throws AttachmentException {
@@ -334,7 +334,7 @@ protected static Attachment getAttachment(SQLDatabase db, String attachmentsDir,
334334
long encodedLength = c.getInt(c.getColumnIndex("encoded_length"));
335335
int revpos = c.getInt(c.getColumnIndex("revpos"));
336336
File file = fileFromKey(db, key, attachmentsDir, false);
337-
337+
338338
atts.put(filename, new SavedAttachment(sequence, filename, key, type, Attachment.Encoding
339339
.values()[encoding], length, encodedLength, revpos, file,
340340
attachmentStreamFactory));

cloudant-sync-datastore-core/src/main/java/com/cloudant/sync/internal/documentstore/DatabaseImpl.java

Lines changed: 1 addition & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright © 2016 IBM Corp. All rights reserved.
2+
* Copyright © 2016, 2017 IBM Corp. All rights reserved.
33
*
44
* Original iOS version by Jens Alfke, ported to Android by Marty Schoch
55
* Copyright © 2012 Couchbase, Inc. All rights reserved.
@@ -343,31 +343,6 @@ public List<String> getPossibleAncestorRevisionIDs(final String docId,
343343
}
344344
}
345345

346-
public static List<DocumentRevision> sortDocumentsAccordingToIdList(List<String> docIds,
347-
List<InternalDocumentRevision> docs) {
348-
Map<String, InternalDocumentRevision> idToDocs = putDocsIntoMap(docs);
349-
List<DocumentRevision> results = new ArrayList<DocumentRevision>();
350-
for (String id : docIds) {
351-
if (idToDocs.containsKey(id)) {
352-
results.add(idToDocs.remove(id));
353-
} else {
354-
logger.fine("No document found for id: " + id);
355-
}
356-
}
357-
assert idToDocs.size() == 0;
358-
return results;
359-
}
360-
361-
private static Map<String, InternalDocumentRevision> putDocsIntoMap(List<InternalDocumentRevision> docs) {
362-
Map<String, InternalDocumentRevision> map = new HashMap<String, InternalDocumentRevision>();
363-
for (InternalDocumentRevision doc : docs) {
364-
// ID should be unique cross all docs
365-
assert !map.containsKey(doc.getId());
366-
map.put(doc.getId(), doc);
367-
}
368-
return map;
369-
}
370-
371346
/**
372347
* <p>Returns the current winning revision of a local document.</p>
373348
*
@@ -499,43 +474,6 @@ public void deleteLocalDocument(final String docId) throws DocumentNotFoundExcep
499474

500475
}
501476

502-
public static InsertRevisionCallable insertStubRevisionAdaptor(long docNumericId, String revId, long
503-
parentSequence) {
504-
// don't copy attachments
505-
InsertRevisionCallable callable = new InsertRevisionCallable();
506-
callable.docNumericId = docNumericId;
507-
callable.revId = revId;
508-
callable.parentSequence = parentSequence;
509-
callable.deleted = false;
510-
callable.current = false;
511-
callable.data = JSONUtils.emptyJSONObjectAsBytes();
512-
callable.available = false;
513-
return callable;
514-
}
515-
516-
public static List<InternalDocumentRevision> getRevisionsFromRawQuery(SQLDatabase db, String sql, String[]
517-
args, String attachmentsDir, AttachmentStreamFactory attachmentStreamFactory)
518-
throws DocumentException,
519-
DocumentStoreException {
520-
List<InternalDocumentRevision> result = new ArrayList<InternalDocumentRevision>();
521-
Cursor cursor = null;
522-
try {
523-
cursor = db.rawQuery(sql, args);
524-
while (cursor.moveToNext()) {
525-
long sequence = cursor.getLong(3);
526-
Map<String, ? extends Attachment> atts = AttachmentManager.attachmentsForRevision(db, attachmentsDir, attachmentStreamFactory
527-
, sequence);
528-
InternalDocumentRevision row = getFullRevisionFromCurrentCursor(cursor, atts);
529-
result.add(row);
530-
}
531-
} catch (SQLException e) {
532-
throw new DocumentStoreException(e);
533-
} finally {
534-
DatabaseUtils.closeCursorQuietly(cursor);
535-
}
536-
return result;
537-
}
538-
539477
/**
540478
* <p>Returns the DocumentStore's unique identifier.</p>
541479
*
@@ -844,56 +782,6 @@ public Void call(SQLDatabase db) throws Exception {
844782

845783
}
846784

847-
public static InsertRevisionCallable insertNewWinnerRevisionAdaptor(DocumentBody newWinner,
848-
InternalDocumentRevision oldWinner)
849-
throws AttachmentException, DocumentStoreException {
850-
String newRevisionId = CouchUtils.generateNextRevisionId(oldWinner.getRevision());
851-
852-
InsertRevisionCallable callable = new InsertRevisionCallable();
853-
callable.docNumericId = oldWinner.getInternalNumericId();
854-
callable.revId = newRevisionId;
855-
callable.parentSequence = oldWinner.getSequence();
856-
callable.deleted = false;
857-
callable.current = true;
858-
callable.data = newWinner.asBytes();
859-
callable.available = true;
860-
return callable;
861-
}
862-
863-
public static InternalDocumentRevision getFullRevisionFromCurrentCursor(Cursor cursor,
864-
Map<String, ? extends Attachment>
865-
attachments) {
866-
String docId = cursor.getString(cursor.getColumnIndex("docid"));
867-
long internalId = cursor.getLong(cursor.getColumnIndex("doc_id"));
868-
String revId = cursor.getString(cursor.getColumnIndex("revid"));
869-
long sequence = cursor.getLong(cursor.getColumnIndex("sequence"));
870-
byte[] json = cursor.getBlob(cursor.getColumnIndex("json"));
871-
boolean current = cursor.getInt(cursor.getColumnIndex("current")) > 0;
872-
boolean deleted = cursor.getInt(cursor.getColumnIndex("deleted")) > 0;
873-
874-
long parent = -1L;
875-
if (cursor.columnType(cursor.getColumnIndex("parent")) == Cursor.FIELD_TYPE_INTEGER) {
876-
parent = cursor.getLong(cursor.getColumnIndex("parent"));
877-
} else if (cursor.columnType(cursor.getColumnIndex("parent")) == Cursor.FIELD_TYPE_NULL) {
878-
} else {
879-
throw new RuntimeException("Unexpected type: " + cursor.columnType(cursor
880-
.getColumnIndex("parent")));
881-
}
882-
883-
DocumentRevisionBuilder builder = new DocumentRevisionBuilder()
884-
.setDocId(docId)
885-
.setRevId(revId)
886-
.setBody(DocumentBodyImpl.bodyWith(json))
887-
.setDeleted(deleted)
888-
.setSequence(sequence)
889-
.setInternalId(internalId)
890-
.setCurrent(current)
891-
.setParent(parent)
892-
.setAttachments(attachments);
893-
894-
return builder.build();
895-
}
896-
897785
/**
898786
* <p>
899787
* Read attachment stream to a temporary location and calculate sha1,

cloudant-sync-datastore-core/src/main/java/com/cloudant/sync/internal/documentstore/callables/DoForceInsertNewDocumentWithHistoryCallable.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright © 2016 IBM Corp. All rights reserved.
2+
* Copyright © 2016, 2017 IBM Corp. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
55
* except in compliance with the License. You may obtain a copy of the License at
@@ -19,6 +19,7 @@
1919
import com.cloudant.sync.documentstore.DocumentStoreException;
2020
import com.cloudant.sync.internal.documentstore.DatabaseImpl;
2121
import com.cloudant.sync.internal.documentstore.InternalDocumentRevision;
22+
import com.cloudant.sync.internal.documentstore.helpers.InsertStubRevisionAdaptor;
2223
import com.cloudant.sync.internal.sqlite.SQLCallable;
2324
import com.cloudant.sync.internal.sqlite.SQLDatabase;
2425

@@ -59,7 +60,7 @@ public Long call(SQLDatabase db) throws AttachmentException, DocumentStoreExcept
5960
long parentSequence = 0L;
6061
for (int i = 0; i < revHistory.size() - 1; i++) {
6162
// Insert stub node
62-
parentSequence = DatabaseImpl.insertStubRevisionAdaptor(docNumericID, revHistory.get(i),
63+
parentSequence = InsertStubRevisionAdaptor.insert(docNumericID, revHistory.get(i),
6364
parentSequence).call(db);
6465
}
6566
// Insert the leaf node (don't copy attachments)

cloudant-sync-datastore-core/src/main/java/com/cloudant/sync/internal/documentstore/callables/ForceInsertCallable.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright © 2016 IBM Corp. All rights reserved.
2+
* Copyright © 2016, 2017 IBM Corp. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
55
* except in compliance with the License. You may obtain a copy of the License at
@@ -131,11 +131,9 @@ public List<DocumentModified> call(SQLDatabase db) throws Exception {
131131
String rev = key[1];
132132
try {
133133
InternalDocumentRevision doc = new GetDocumentCallable(id, rev, attachmentsDir, attachmentStreamFactory).call(db);
134-
if (doc != null) {
135134
AttachmentManager.addAttachmentsToRevision(db,
136135
attachmentsDir, doc, item
137136
.preparedAttachments.get(key));
138-
}
139137
} catch (DocumentNotFoundException e) {
140138
//safe to continue, previously getDocumentInQueue
141139
// could return

cloudant-sync-datastore-core/src/main/java/com/cloudant/sync/internal/documentstore/callables/GetAllDocumentsCallable.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright © 2016 IBM Corp. All rights reserved.
2+
* Copyright © 2016, 2017 IBM Corp. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
55
* except in compliance with the License. You may obtain a copy of the License at
@@ -18,6 +18,7 @@
1818
import com.cloudant.sync.internal.documentstore.AttachmentStreamFactory;
1919
import com.cloudant.sync.internal.documentstore.DatabaseImpl;
2020
import com.cloudant.sync.internal.documentstore.InternalDocumentRevision;
21+
import com.cloudant.sync.internal.documentstore.helpers.GetRevisionsFromRawQuery;
2122
import com.cloudant.sync.internal.sqlite.SQLCallable;
2223
import com.cloudant.sync.internal.sqlite.SQLDatabase;
2324

@@ -54,7 +55,7 @@ public List<DocumentRevision> call(SQLDatabase db) throws Exception {
5455
" ORDER BY docs.doc_id %1$s, revid DESC LIMIT %2$s OFFSET %3$s ",
5556
(descending ? "DESC" : "ASC"), limit, offset);
5657

57-
return new ArrayList<DocumentRevision>(DatabaseImpl.getRevisionsFromRawQuery(db, sql,
58+
return new ArrayList<DocumentRevision>(GetRevisionsFromRawQuery.get(db, sql,
5859
new String[]{}, attachmentsDir, attachmentStreamFactory));
5960

6061
}

cloudant-sync-datastore-core/src/main/java/com/cloudant/sync/internal/documentstore/callables/GetAllRevisionsOfDocumentCallable.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright © 2016 IBM Corp. All rights reserved.
2+
* Copyright © 2016, 2017 IBM Corp. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
55
* except in compliance with the License. You may obtain a copy of the License at
@@ -21,6 +21,7 @@
2121
import com.cloudant.sync.documentstore.DocumentStoreException;
2222
import com.cloudant.sync.internal.documentstore.InternalDocumentRevision;
2323
import com.cloudant.sync.internal.documentstore.DocumentRevisionTree;
24+
import com.cloudant.sync.internal.documentstore.helpers.GetFullRevisionFromCurrentCursor;
2425
import com.cloudant.sync.internal.sqlite.Cursor;
2526
import com.cloudant.sync.internal.sqlite.SQLCallable;
2627
import com.cloudant.sync.internal.sqlite.SQLDatabase;
@@ -71,7 +72,7 @@ public DocumentRevisionTree call(SQLDatabase db) throws DocumentStoreException,
7172
long sequence = cursor.getLong(3);
7273
Map<String, ? extends Attachment> atts = new AttachmentsForRevisionCallable(
7374
this.attachmentsDir, this.attachmentStreamFactory, sequence).call(db);
74-
InternalDocumentRevision rev = DatabaseImpl.getFullRevisionFromCurrentCursor(cursor, atts);
75+
InternalDocumentRevision rev = GetFullRevisionFromCurrentCursor.get(cursor, atts);
7576
logger.finer("Rev: " + rev);
7677
tree.add(rev);
7778
}

cloudant-sync-datastore-core/src/main/java/com/cloudant/sync/internal/documentstore/callables/GetDocumentCallable.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright © 2016 IBM Corp. All rights reserved.
2+
* Copyright © 2016, 2017 IBM Corp. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
55
* except in compliance with the License. You may obtain a copy of the License at
@@ -21,6 +21,7 @@
2121
import com.cloudant.sync.documentstore.DocumentStoreException;
2222
import com.cloudant.sync.documentstore.DocumentNotFoundException;
2323
import com.cloudant.sync.internal.documentstore.InternalDocumentRevision;
24+
import com.cloudant.sync.internal.documentstore.helpers.GetFullRevisionFromCurrentCursor;
2425
import com.cloudant.sync.internal.sqlite.Cursor;
2526
import com.cloudant.sync.internal.sqlite.SQLCallable;
2627
import com.cloudant.sync.internal.sqlite.SQLDatabase;
@@ -71,7 +72,7 @@ public InternalDocumentRevision call(SQLDatabase db) throws DocumentNotFoundExce
7172
long sequence = cursor.getLong(3);
7273
Map<String, ? extends Attachment> atts = new AttachmentsForRevisionCallable(
7374
this.attachmentsDir, this.attachmentStreamFactory, sequence).call(db);
74-
return DatabaseImpl.getFullRevisionFromCurrentCursor(cursor, atts);
75+
return GetFullRevisionFromCurrentCursor.get(cursor, atts);
7576
} else {
7677
throw new DocumentNotFoundException(id, rev);
7778
}

cloudant-sync-datastore-core/src/main/java/com/cloudant/sync/internal/documentstore/callables/GetDocumentsWithIdsCallable.java

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright © 2016 IBM Corp. All rights reserved.
2+
* Copyright © 2016, 2017 IBM Corp. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
55
* except in compliance with the License. You may obtain a copy of the License at
@@ -18,18 +18,25 @@
1818
import com.cloudant.sync.internal.documentstore.AttachmentStreamFactory;
1919
import com.cloudant.sync.internal.documentstore.DatabaseImpl;
2020
import com.cloudant.sync.internal.documentstore.InternalDocumentRevision;
21+
import com.cloudant.sync.internal.documentstore.helpers.GetRevisionsFromRawQuery;
2122
import com.cloudant.sync.internal.sqlite.SQLCallable;
2223
import com.cloudant.sync.internal.sqlite.SQLDatabase;
2324
import com.cloudant.sync.internal.util.DatabaseUtils;
2425
import com.cloudant.sync.internal.util.Misc;
2526

27+
import java.util.ArrayList;
28+
import java.util.HashMap;
2629
import java.util.List;
30+
import java.util.Map;
31+
import java.util.logging.Logger;
2732

2833
/**
2934
* Get a List of Document Revisions by Document ID
3035
*/
3136
public class GetDocumentsWithIdsCallable implements SQLCallable<List<DocumentRevision>> {
3237

38+
private static final Logger logger = Logger.getLogger(GetDocumentsWithIdsCallable.class.getCanonicalName());
39+
3340
private List<String> docIds;
3441

3542
private String attachmentsDir;
@@ -51,8 +58,36 @@ public List<DocumentRevision> call(SQLDatabase db) throws Exception {
5158
" ORDER BY docs.doc_id ", DatabaseUtils.makePlaceholders(docIds.size
5259
()));
5360
String[] args = docIds.toArray(new String[docIds.size()]);
54-
List<InternalDocumentRevision> docs = DatabaseImpl.getRevisionsFromRawQuery(db, sql, args, attachmentsDir, attachmentStreamFactory);
61+
List<InternalDocumentRevision> docs = GetRevisionsFromRawQuery.get(db, sql, args,
62+
attachmentsDir, attachmentStreamFactory);
5563
// Sort in memory since seems not able to sort them using SQL
56-
return DatabaseImpl.sortDocumentsAccordingToIdList(docIds, docs);
64+
return sortDocumentsAccordingToIdList(docIds, docs);
65+
}
66+
67+
private static List<DocumentRevision> sortDocumentsAccordingToIdList(List<String> docIds,
68+
List<InternalDocumentRevision> docs) {
69+
Map<String, InternalDocumentRevision> idToDocs = putDocsIntoMap(docs);
70+
List<DocumentRevision> results = new ArrayList<DocumentRevision>();
71+
for (String id : docIds) {
72+
if (idToDocs.containsKey(id)) {
73+
results.add(idToDocs.remove(id));
74+
} else {
75+
logger.fine("No document found for id: " + id);
76+
}
77+
}
78+
assert idToDocs.size() == 0;
79+
return results;
5780
}
81+
82+
private static Map<String, InternalDocumentRevision> putDocsIntoMap(List<InternalDocumentRevision> docs) {
83+
Map<String, InternalDocumentRevision> map = new HashMap<String, InternalDocumentRevision>();
84+
for (InternalDocumentRevision doc : docs) {
85+
// ID should be unique cross all docs
86+
assert !map.containsKey(doc.getId());
87+
map.put(doc.getId(), doc);
88+
}
89+
return map;
90+
}
91+
92+
5893
}

cloudant-sync-datastore-core/src/main/java/com/cloudant/sync/internal/documentstore/callables/GetDocumentsWithInternalIdsCallable.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright © 2016 IBM Corp. All rights reserved.
2+
* Copyright © 2016, 2017 IBM Corp. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
55
* except in compliance with the License. You may obtain a copy of the License at
@@ -19,6 +19,7 @@
1919
import com.cloudant.sync.internal.documentstore.DatabaseImpl;
2020
import com.cloudant.sync.documentstore.DocumentException;
2121
import com.cloudant.sync.internal.documentstore.InternalDocumentRevision;
22+
import com.cloudant.sync.internal.documentstore.helpers.GetRevisionsFromRawQuery;
2223
import com.cloudant.sync.internal.sqlite.SQLCallable;
2324
import com.cloudant.sync.internal.sqlite.SQLDatabase;
2425
import com.cloudant.sync.internal.util.CollectionUtils;
@@ -80,7 +81,7 @@ public List<InternalDocumentRevision> call(SQLDatabase db) throws DocumentStoreE
8081
for (int i = 0; i < batch.size(); i++) {
8182
args[i] = Long.toString(batch.get(i));
8283
}
83-
result.addAll(DatabaseImpl.getRevisionsFromRawQuery(db, sql, args, attachmentsDir,
84+
result.addAll(GetRevisionsFromRawQuery.get(db, sql, args, attachmentsDir,
8485
attachmentStreamFactory));
8586
}
8687

cloudant-sync-datastore-core/src/main/java/com/cloudant/sync/internal/documentstore/callables/InsertDocumentHistoryIntoExistingTreeCallable.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright © 2016 IBM Corp. All rights reserved.
2+
* Copyright © 2016, 2017 IBM Corp. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
55
* except in compliance with the License. You may obtain a copy of the License at
@@ -18,6 +18,7 @@
1818
import com.cloudant.sync.internal.documentstore.DatabaseImpl;
1919
import com.cloudant.sync.documentstore.DocumentStoreException;
2020
import com.cloudant.sync.internal.documentstore.InternalDocumentRevision;
21+
import com.cloudant.sync.internal.documentstore.helpers.InsertStubRevisionAdaptor;
2122
import com.cloudant.sync.internal.sqlite.SQLCallable;
2223
import com.cloudant.sync.internal.sqlite.SQLDatabase;
2324
import com.cloudant.sync.internal.util.Misc;
@@ -66,7 +67,7 @@ public Long call(SQLDatabase db) throws DocumentStoreException {
6667
String revId = revisions.get(i);
6768
long seq = new GetSequenceCallable(newRevision.getId(), revId).call(db);
6869
if (seq == -1) {
69-
seq = DatabaseImpl.insertStubRevisionAdaptor(docNumericID, revId, parentSeq).call(db);
70+
seq = InsertStubRevisionAdaptor.insert(docNumericID, revId, parentSeq).call(db);
7071
new SetCurrentCallable(parentSeq, false).call(db);
7172
}
7273
parentSeq = seq;

0 commit comments

Comments
 (0)