This repository has been archived by the owner on May 4, 2023. It is now read-only.
/
Database.java
146 lines (120 loc) · 5.25 KB
/
Database.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package com.github.rnewson.couchdb.lucene.couchdb;
/**
* Copyright 2009 Robert Newson
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.HttpResponseException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import com.github.rnewson.couchdb.lucene.util.Utils;
public final class Database {
private final HttpClient httpClient;
private final String url;
public Database(final HttpClient httpClient, final String url) {
this.httpClient = httpClient;
this.url = url.endsWith("/") ? url : url + "/";
}
public boolean create() throws IOException {
return HttpUtils.put(httpClient, url, null) == 201;
}
public boolean delete() throws IOException {
return HttpUtils.delete(httpClient, url) == 200;
}
public List<DesignDocument> getAllDesignDocuments() throws IOException {
final String body = HttpUtils.get(httpClient, String.format("%s_all_docs?startkey=%s&endkey=%s&include_docs=true", url,
Utils.urlEncode("\"_design\""), Utils.urlEncode("\"_design0\"")));
final JSONObject json = JSONObject.fromObject(body);
return toDesignDocuments(json);
}
public CouchDocument getDocument(final String id) throws IOException {
final String response = HttpUtils.get(httpClient, url + Utils.urlEncode(id));
return new CouchDocument(JSONObject.fromObject(response));
}
public DesignDocument getDesignDocument(final String id) throws IOException {
final String response = HttpUtils.get(httpClient, url + "_design/" + Utils.urlEncode(id));
return new DesignDocument(JSONObject.fromObject(response));
}
public List<CouchDocument> getDocuments(final String... ids) throws IOException {
final JSONArray keys = new JSONArray();
for (final String id : ids) {
keys.add(id);
}
final JSONObject req = new JSONObject();
req.element("keys", keys);
final String body = HttpUtils.post(httpClient, url + "_all_docs?include_docs=true", req.toString());
final JSONObject json = JSONObject.fromObject(body);
return toDocuments(json);
}
public JSONObject getInfo() throws IOException {
return JSONObject.fromObject(HttpUtils.get(httpClient, url));
}
public <T> T handleAttachment(final String doc, final String att, final ResponseHandler<T> handler) throws IOException {
final HttpGet get = new HttpGet(url + "/" + Utils.urlEncode(doc) + "/" + Utils.urlEncode(att));
return httpClient.execute(get, handler);
}
public HttpUriRequest getChangesRequest(final long since) throws IOException {
return new HttpGet(url + "_changes?feed=continuous&heartbeat=15000&include_docs=true&since=" + since);
}
public boolean saveDocument(final String id, final String body) throws IOException {
return HttpUtils.put(httpClient, url + Utils.urlEncode(id), body) == 201;
}
public UUID getUuid() throws IOException {
try {
final CouchDocument local = getDocument("_local/lucene");
return UUID.fromString(local.asJson().getString("uuid"));
} catch (final HttpResponseException e) {
switch (e.getStatusCode()) {
case HttpStatus.SC_NOT_FOUND:
return null;
default:
throw e;
}
}
}
public void createUuid() throws IOException {
final UUID uuid = UUID.randomUUID();
saveDocument("_local/lucene", String.format("{\"uuid\":\"%s\"}", uuid));
}
private List<DesignDocument> toDesignDocuments(final JSONObject json) {
final List<DesignDocument> result = new ArrayList<DesignDocument>();
for (final JSONObject doc : rows(json)) {
result.add(new DesignDocument(doc));
}
return result;
}
private List<CouchDocument> toDocuments(final JSONObject json) {
final List<CouchDocument> result = new ArrayList<CouchDocument>();
for (final JSONObject doc : rows(json)) {
result.add(new CouchDocument(doc));
}
return result;
}
private List<JSONObject> rows(final JSONObject json) {
final List<JSONObject> result = new ArrayList<JSONObject>();
final JSONArray rows = json.getJSONArray("rows");
for (int i = 0; i < rows.size(); i++) {
result.add(rows.getJSONObject(i).getJSONObject("doc"));
}
return result;
}
}