Skip to content

Commit 799ae2b

Browse files
committed
the beginnings of the bulk read api
1 parent fbd3ce3 commit 799ae2b

File tree

7 files changed

+411
-0
lines changed

7 files changed

+411
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2012-2014 MarkLogic Corporation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.marklogic.client;
17+
18+
import java.util.Iterator;
19+
20+
public interface Page<T> extends Iterable<T> {
21+
public Iterator<T> iterator();
22+
public long getStart();
23+
public long getPageSize();
24+
public long getTotalSize();
25+
public long size();
26+
public long getTotalPages();
27+
public boolean hasContent();
28+
public boolean hasNextPage();
29+
public boolean hasPreviousPage();
30+
public long getPageNumber();
31+
public boolean isFirstPage();
32+
public boolean isLastPage();
33+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2012-2014 MarkLogic Corporation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.marklogic.client.document;
17+
18+
import com.marklogic.client.Page;
19+
import com.marklogic.client.io.marker.AbstractReadHandle;
20+
21+
public interface DocumentPage extends Iterable<DocumentRecord>, Page<DocumentRecord> {
22+
public boolean hasNext();
23+
24+
public DocumentRecord next();
25+
26+
public <T extends AbstractReadHandle> T nextContent(T contentHandle);
27+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2012-2014 MarkLogic Corporation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.marklogic.client.document;
17+
18+
import com.marklogic.client.io.Format;
19+
import com.marklogic.client.io.marker.AbstractReadHandle;
20+
import com.marklogic.client.io.marker.DocumentMetadataReadHandle;
21+
22+
public interface DocumentRecord {
23+
public String getUri();
24+
25+
public Format getFormat();
26+
27+
public String getMimetype();
28+
29+
public <T extends DocumentMetadataReadHandle> T getMetadata(T metadataHandle);
30+
public <T extends AbstractReadHandle> T getContent(T contentHandle);
31+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright 2012-2014 MarkLogic Corporation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.marklogic.client.impl;
17+
18+
import java.util.Iterator;
19+
import com.marklogic.client.Page;
20+
21+
public class BasicPage<T> implements Page<T> {
22+
private Iterable<T> iterable;
23+
private long start;
24+
private long pageSize;
25+
private long totalSize;
26+
27+
public BasicPage(Iterable<T> iterable) {
28+
this.iterable = iterable;
29+
}
30+
31+
public Iterator<T> iterator() {
32+
return iterable.iterator();
33+
}
34+
35+
public long getStart() {
36+
return start;
37+
}
38+
39+
public BasicPage<T> setStart(long start) {
40+
this.start = start;
41+
return this;
42+
}
43+
44+
public long getPageSize() {
45+
return pageSize;
46+
}
47+
48+
public BasicPage<T> setPageSize(long pageSize) {
49+
this.pageSize = pageSize;
50+
return this;
51+
}
52+
53+
public long getTotalSize() {
54+
return totalSize;
55+
}
56+
57+
public BasicPage<T> setTotalSize(long totalSize) {
58+
this.totalSize = totalSize;
59+
return this;
60+
}
61+
62+
public long size() {
63+
if ( hasNextPage() ) {
64+
return getPageSize();
65+
} else if ((getTotalSize() % getPageSize()) == 0) {
66+
return getPageSize();
67+
} else {
68+
return getTotalSize() % getPageSize();
69+
}
70+
}
71+
72+
public long getTotalPages() {
73+
return (long) Math.ceil((double) getTotalSize() / (double) getPageSize());
74+
}
75+
76+
public boolean hasContent() {
77+
return size() > 0;
78+
}
79+
80+
public boolean hasNextPage() {
81+
return getPageNumber() < getTotalPages();
82+
}
83+
84+
public boolean hasPreviousPage() {
85+
return getPageNumber() > 0;
86+
}
87+
88+
public long getPageNumber() {
89+
return (long) Math.floor((double) start / (double) getPageSize()) + 1;
90+
}
91+
92+
public boolean isFirstPage() {
93+
return getPageNumber() == 1;
94+
}
95+
96+
public boolean isLastPage() {
97+
return getPageNumber() == getTotalPages();
98+
}
99+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2012-2014 MarkLogic Corporation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.marklogic.client.impl;
17+
18+
import java.util.Iterator;
19+
20+
import com.marklogic.client.document.DocumentPage;
21+
import com.marklogic.client.document.DocumentRecord;
22+
import com.marklogic.client.io.marker.AbstractReadHandle;
23+
24+
class DocumentPageImpl extends BasicPage<DocumentRecord> implements DocumentPage {
25+
private Iterable<DocumentRecord> iterable;
26+
private Iterator<DocumentRecord> iterator;
27+
28+
public DocumentPageImpl(Iterable<DocumentRecord> iterable) {
29+
super(iterable);
30+
this.iterable = iterable;
31+
this.iterator = iterable.iterator();
32+
}
33+
34+
public boolean hasNext() {
35+
return iterator.hasNext();
36+
}
37+
38+
public DocumentRecord next() {
39+
return iterator.next();
40+
}
41+
42+
public <T extends AbstractReadHandle> T nextContent(T contentHandle) {
43+
return iterator.next().getContent(contentHandle);
44+
}
45+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2012-2014 MarkLogic Corporation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.marklogic.client.impl;
17+
18+
import java.io.InputStream;
19+
20+
import org.slf4j.Logger;
21+
import org.slf4j.LoggerFactory;
22+
23+
import com.marklogic.client.document.DocumentRecord;
24+
import com.marklogic.client.io.Format;
25+
import com.marklogic.client.io.marker.AbstractReadHandle;
26+
import com.marklogic.client.io.marker.DocumentMetadataReadHandle;
27+
28+
public class DocumentRecordImpl implements DocumentRecord {
29+
private static final Logger logger = LoggerFactory.getLogger(DocumentRecordImpl.class);
30+
private String uri;
31+
private Format format;
32+
private String mimetype;
33+
private InputStream metadata;
34+
private InputStream content;
35+
36+
DocumentRecordImpl(String uri, Format format, String mimetype, InputStream metadata, InputStream content) {
37+
this.uri = uri;
38+
this.format = format;
39+
this.mimetype = mimetype;
40+
this.metadata = metadata;
41+
this.content = content;
42+
}
43+
44+
public String getUri() {
45+
return uri;
46+
}
47+
48+
public Format getFormat() {
49+
return format;
50+
}
51+
52+
public String getMimetype() {
53+
return mimetype;
54+
}
55+
56+
public <T extends DocumentMetadataReadHandle> T getMetadata(T metadataHandle) {
57+
HandleImplementation metadataBase = HandleAccessor.checkHandle(metadataHandle, "metadata");
58+
Format metadataFormat = metadataBase.getFormat();
59+
if (metadataFormat == null || (metadataFormat != Format.XML)) {
60+
if (logger.isWarnEnabled())
61+
logger.warn("Unsupported metadata format {}, using XML",metadataFormat.name());
62+
metadataBase.setFormat(Format.XML);
63+
}
64+
HandleAccessor.receiveContent(metadataHandle, metadata);
65+
return metadataHandle;
66+
}
67+
68+
public <T extends AbstractReadHandle> T getContent(T contentHandle) {
69+
HandleAccessor.checkHandle(contentHandle, "content");
70+
HandleAccessor.receiveContent(contentHandle, content);
71+
return contentHandle;
72+
}
73+
}
74+

0 commit comments

Comments
 (0)