Skip to content

Commit 1e671f4

Browse files
author
Hideki Itakura
committed
Merge branch 'sergio91pt-issue_592_unsaved_rev_attach_content'
2 parents 1ca3ed6 + 5b4ea13 commit 1e671f4

4 files changed

Lines changed: 61 additions & 30 deletions

File tree

src/main/java/com/couchbase/lite/Attachment.java

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.Collections;
2929
import java.util.HashMap;
3030
import java.util.Map;
31+
import java.util.zip.GZIPInputStream;
3132

3233
/**
3334
* A Couchbase Lite Document Attachment.
@@ -124,15 +125,36 @@ public String getContentType() {
124125
public InputStream getContent() throws CouchbaseLiteException {
125126
if (body != null) {
126127
return body;
127-
}
128-
else {
128+
} else {
129129
Database db = revision.getDatabase();
130-
Attachment attachment = db.getAttachmentForSequence(revision.getSequence(), this.name);
130+
long sequence = getAttachmentSequence();
131+
if (sequence == 0) {
132+
throw new CouchbaseLiteException(Status.INTERNAL_SERVER_ERROR);
133+
}
134+
Attachment attachment = db.getAttachmentForSequence(sequence, this.name);
131135
body = attachment.getContent();
136+
if (attachment.getGZipped()) {
137+
// Client does not expect a gzipped stream.
138+
// Only Router handles gzipped streams and uses getAttachmentForSequence directly.
139+
try {
140+
body = new GZIPInputStream(body);
141+
} catch (IOException e) {
142+
throw new CouchbaseLiteException(e.getMessage(), Status.STATUS_ATTACHMENT_ERROR);
143+
}
144+
}
145+
gzipped = false;
132146
return body;
133147
}
134148
}
135149

150+
private long getAttachmentSequence() {
151+
long sequence = revision.getSequence();
152+
if (sequence == 0) {
153+
sequence = revision.getParentSequence();
154+
}
155+
return sequence;
156+
}
157+
136158
/**
137159
* This is just for compatibility with iOS implementation.
138160
*
@@ -141,16 +163,16 @@ public InputStream getContent() throws CouchbaseLiteException {
141163
@InterfaceAudience.Private
142164
public URL getContentURL(){
143165
try {
144-
if (revision.getSequence() > 0) {
145-
//Database db = revision.getDatabase();
146-
//Attachment attachment = db.getAttachmentForSequence(revision.getSequence(), this.name);
147-
String path = revision.getDatabase().getAttachmentPathForSequence(revision.getSequence(), this.name);
166+
long sequence = getAttachmentSequence();
167+
if (sequence > 0) {
168+
Database db = revision.getDatabase();
169+
//Attachment attachment = db.getAttachmentForSequence(sequence, this.name);
170+
String path = db.getAttachmentPathForSequence(sequence, this.name);
148171
if (path != null) {
149172
return new File(path).toURI().toURL();
150173
}
151174
}
152-
}
153-
catch(Exception e){
175+
} catch(Exception e){
154176
Log.d(Log.TAG_DATABASE, e.getMessage());
155177
}
156178
return null;

src/main/java/com/couchbase/lite/Revision.java

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@
1717
*/
1818
public abstract class Revision {
1919

20-
/**re
21-
* The sequence number of this revision.
22-
*/
23-
protected long sequence;
24-
2520
/**
2621
* The document this is a revision of
2722
*/
@@ -67,7 +62,7 @@ public Document getDocument() {
6762
}
6863

6964
/**
70-
* Gets the Revision's id.
65+
* Gets the Revision's id. In the case of an unsaved revision, may return null.
7166
*/
7267
@InterfaceAudience.Public
7368
public abstract String getId();
@@ -191,6 +186,12 @@ private Attachment toAttachment(String name, Object attachment) {
191186
@InterfaceAudience.Public
192187
public abstract String getParentId();
193188

189+
/**
190+
* @exclude
191+
*/
192+
@InterfaceAudience.Private
193+
/* package */ abstract long getParentSequence();
194+
194195
/**
195196
* Returns the history of this document as an array of CBLRevisions, in forward order.
196197
* Older revisions are NOT guaranteed to have their properties available.
@@ -274,17 +275,7 @@ public String toString() {
274275
* @exclude
275276
*/
276277
@InterfaceAudience.Private
277-
/* package */ void setSequence(long sequence) {
278-
this.sequence = sequence;
279-
}
280-
281-
/**
282-
* @exclude
283-
*/
284-
@InterfaceAudience.Private
285-
/* package */ long getSequence() {
286-
return sequence;
287-
}
278+
/* package */ abstract long getSequence();
288279

289280
/**
290281
* Generation number: 1 for a new document, 2 for the 2nd revision, ...

src/main/java/com/couchbase/lite/SavedRevision.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,13 @@ public String getParentId() {
169169
return parRev.getRevId();
170170
}
171171

172+
@Override
173+
@InterfaceAudience.Private
174+
/* package */ long getParentSequence() {
175+
SavedRevision parent = getParent();
176+
return (parent != null) ? parent.getSequence() : 0L;
177+
}
178+
172179
@Override
173180
@InterfaceAudience.Public
174181
public long getSequence() {

src/main/java/com/couchbase/lite/UnsavedRevision.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
public final class UnsavedRevision extends Revision {
1818

19+
private final long parentSequence;
1920
private Map<String, Object> properties;
2021

2122
/**
@@ -29,8 +30,10 @@ public final class UnsavedRevision extends Revision {
2930

3031
if (parentRevision == null) {
3132
parentRevID = null;
33+
parentSequence = 0L;
3234
} else {
3335
parentRevID = parentRevision.getId();
36+
parentSequence = parentRevision.getSequence();
3437
}
3538

3639
Map<String, Object> parentRevisionProperties;
@@ -67,16 +70,18 @@ public void setIsDeletion(boolean isDeletion) {
6770
}
6871
}
6972

70-
/**
71-
* Get the id of the owning document. In the case of an unsaved revision, may return null.
72-
* @return
73-
*/
7473
@Override
7574
@InterfaceAudience.Public
7675
public String getId() {
7776
return null;
7877
}
7978

79+
@Override
80+
@InterfaceAudience.Private
81+
/* package */ long getSequence() {
82+
return 0L;
83+
}
84+
8085
/**
8186
* Set the properties for this revision
8287
*/
@@ -193,6 +198,12 @@ public String getParentId() {
193198
return parentRevID;
194199
}
195200

201+
@Override
202+
@InterfaceAudience.Private
203+
/* package */ long getParentSequence() {
204+
return parentSequence;
205+
}
206+
196207
@Override
197208
@InterfaceAudience.Public
198209
public List<SavedRevision> getRevisionHistory() throws CouchbaseLiteException {

0 commit comments

Comments
 (0)