Created
November 18, 2016 16:02
-
-
Save daschl/a42fa84ac7b01e627c3a40299d9f93cd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2016 Couchbase, Inc. | |
* | |
* 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 com.couchbase.client.core.message.kv.MutationToken; | |
import com.couchbase.client.java.document.AbstractDocument; | |
import java.io.IOException; | |
import java.io.ObjectInputStream; | |
import java.io.ObjectOutputStream; | |
import java.io.Serializable; | |
public class ByteJsonDocument extends AbstractDocument<byte[]> implements Serializable { | |
private static final long serialVersionUID = 375731014642624275L; | |
/** | |
* Creates a {@link ByteJsonDocument} which the document id. | |
* | |
* @param id the per-bucket unique document id. | |
* @return a {@link ByteJsonDocument}. | |
*/ | |
public static ByteJsonDocument create(String id) { | |
return new ByteJsonDocument(id, 0, null, 0, null); | |
} | |
/** | |
* Creates a {@link ByteJsonDocument} which the document id and JSON content. | |
* | |
* @param id the per-bucket unique document id. | |
* @param content the content of the document. | |
* @return a {@link ByteJsonDocument}. | |
*/ | |
public static ByteJsonDocument create(String id, byte[] content) { | |
return new ByteJsonDocument(id, 0, content, 0, null); | |
} | |
/** | |
* Creates a {@link ByteJsonDocument} which the document id, JSON content and the CAS value. | |
* | |
* @param id the per-bucket unique document id. | |
* @param content the content of the document. | |
* @param cas the CAS (compare and swap) value for optimistic concurrency. | |
* @return a {@link ByteJsonDocument}. | |
*/ | |
public static ByteJsonDocument create(String id, byte[] content, long cas) { | |
return new ByteJsonDocument(id, 0, content, cas, null); | |
} | |
/** | |
* Creates a {@link ByteJsonDocument} which the document id, JSON content and the expiration time. | |
* | |
* @param id the per-bucket unique document id. | |
* @param content the content of the document. | |
* @param expiry the expiration time of the document. | |
* @return a {@link ByteJsonDocument}. | |
*/ | |
public static ByteJsonDocument create(String id, int expiry, byte[] content) { | |
return new ByteJsonDocument(id, expiry, content, 0, null); | |
} | |
/** | |
* Creates a {@link ByteJsonDocument} which the document id, JSON content, CAS value, expiration time and status code. | |
* | |
* This factory method is normally only called within the client library when a response is analyzed and a document | |
* is returned which is enriched with the status code. It does not make sense to pre populate the status field from | |
* the user level code. | |
* | |
* @param id the per-bucket unique document id. | |
* @param content the content of the document. | |
* @param cas the CAS (compare and swap) value for optimistic concurrency. | |
* @param expiry the expiration time of the document. | |
* @return a {@link ByteJsonDocument}. | |
*/ | |
public static ByteJsonDocument create(String id, int expiry, byte[] content, long cas) { | |
return new ByteJsonDocument(id, expiry, content, cas, null); | |
} | |
/** | |
* Creates a {@link ByteJsonDocument} which the document id, JSON content, CAS value, expiration time and status code. | |
* | |
* This factory method is normally only called within the client library when a response is analyzed and a document | |
* is returned which is enriched with the status code. It does not make sense to pre populate the status field from | |
* the user level code. | |
* | |
* @param id the per-bucket unique document id. | |
* @param content the content of the document. | |
* @param cas the CAS (compare and swap) value for optimistic concurrency. | |
* @param expiry the expiration time of the document. | |
* @return a {@link ByteJsonDocument}. | |
*/ | |
public static ByteJsonDocument create(String id, int expiry, byte[] content, long cas, MutationToken mutationToken) { | |
return new ByteJsonDocument(id, expiry, content, cas, mutationToken); | |
} | |
/** | |
* Creates a copy from a different {@link ByteJsonDocument}, but changes the document ID and content. | |
* | |
* @param doc the original {@link ByteJsonDocument} to copy. | |
* @param id the per-bucket unique document id. | |
* @param content the content of the document. | |
* @return a copied {@link ByteJsonDocument} with the changed properties. | |
*/ | |
public static ByteJsonDocument from(ByteJsonDocument doc, String id, byte[] content) { | |
return ByteJsonDocument.create(id, doc.expiry(), content, doc.cas(), doc.mutationToken()); | |
} | |
/** | |
* Creates a copy from a different {@link ByteJsonDocument}, but changes the CAS value. | |
* | |
* @param doc the original {@link ByteJsonDocument} to copy. | |
* @param cas the CAS (compare and swap) value for optimistic concurrency. | |
* @return a copied {@link ByteJsonDocument} with the changed properties. | |
*/ | |
public static ByteJsonDocument from(ByteJsonDocument doc, long cas) { | |
return ByteJsonDocument.create(doc.id(), doc.expiry(), doc.content(), cas, doc.mutationToken()); | |
} | |
/** | |
* Private constructor which is called by the static factory methods eventually. | |
* | |
* @param id the per-bucket unique document id. | |
* @param content the content of the document. | |
* @param cas the CAS (compare and swap) value for optimistic concurrency. | |
* @param expiry the expiration time of the document. | |
*/ | |
private ByteJsonDocument(String id, int expiry, byte[] content, long cas, MutationToken mutationToken) { | |
super(id, expiry, content, cas, mutationToken); | |
} | |
private void writeObject(ObjectOutputStream stream) throws IOException { | |
writeToSerializedStream(stream); | |
} | |
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException { | |
readFromSerializedStream(stream); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2016 Couchbase, Inc. | |
* | |
* 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 com.couchbase.client.core.lang.Tuple; | |
import com.couchbase.client.core.lang.Tuple2; | |
import com.couchbase.client.core.message.ResponseStatus; | |
import com.couchbase.client.core.message.kv.MutationToken; | |
import com.couchbase.client.deps.io.netty.buffer.ByteBuf; | |
import com.couchbase.client.deps.io.netty.buffer.Unpooled; | |
import com.couchbase.client.deps.io.netty.util.CharsetUtil; | |
import com.couchbase.client.java.document.RawJsonDocument; | |
import com.couchbase.client.java.error.TranscodingException; | |
import com.couchbase.client.java.transcoder.AbstractTranscoder; | |
import com.couchbase.client.java.transcoder.TranscoderUtils; | |
public class ByteJsonTranscoder extends AbstractTranscoder<ByteJsonDocument, byte[]> { | |
@Override | |
protected Tuple2<ByteBuf, Integer> doEncode(ByteJsonDocument document) throws Exception { | |
return Tuple.create( | |
Unpooled.wrappedBuffer(document.content()), | |
TranscoderUtils.JSON_COMPAT_FLAGS | |
); | |
} | |
@Override | |
protected ByteJsonDocument doDecode(String id, ByteBuf content, long cas, int expiry, int flags, | |
ResponseStatus status) throws Exception { | |
if (!TranscoderUtils.hasJsonFlags(flags)) { | |
throw new TranscodingException("Flags (0x" + Integer.toHexString(flags) + ") indicate non-JSON document for " | |
+ "id " + id + ", could not decode."); | |
} | |
byte[] converted = new byte[content.readableBytes()]; | |
content.readBytes(converted); | |
return newDocument(id, expiry, converted, cas); | |
} | |
@Override | |
public ByteJsonDocument newDocument(String id, int expiry, byte[] content, long cas) { | |
return ByteJsonDocument.create(id, expiry, content, cas); | |
} | |
@Override | |
public ByteJsonDocument newDocument(String id, int expiry, byte[] content, long cas, | |
MutationToken mutationToken) { | |
return ByteJsonDocument.create(id, expiry, content, cas, mutationToken); | |
} | |
@Override | |
public Class<ByteJsonDocument> documentType() { | |
return ByteJsonDocument.class; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.couchbase.client.core.event.CouchbaseEvent; | |
import com.couchbase.client.core.event.metrics.NetworkLatencyMetricsEvent; | |
import com.couchbase.client.core.logging.CouchbaseLogLevel; | |
import com.couchbase.client.core.logging.CouchbaseLogger; | |
import com.couchbase.client.core.message.kv.subdoc.multi.Lookup; | |
import com.couchbase.client.core.metrics.DefaultLatencyMetricsCollectorConfig; | |
import com.couchbase.client.deps.io.netty.channel.EventLoopGroup; | |
import com.couchbase.client.deps.io.netty.channel.nio.NioEventLoopGroup; | |
import com.couchbase.client.deps.io.netty.util.CharsetUtil; | |
import com.couchbase.client.java.AsyncBucket; | |
import com.couchbase.client.java.Bucket; | |
import com.couchbase.client.java.CouchbaseCluster; | |
import com.couchbase.client.java.document.*; | |
import com.couchbase.client.java.document.json.JsonArray; | |
import com.couchbase.client.java.document.json.JsonObject; | |
import com.couchbase.client.java.env.CouchbaseEnvironment; | |
import com.couchbase.client.java.env.DefaultCouchbaseEnvironment; | |
import com.couchbase.client.java.error.TranscodingException; | |
import com.couchbase.client.java.query.AsyncN1qlQueryResult; | |
import com.couchbase.client.java.query.AsyncN1qlQueryRow; | |
import com.couchbase.client.java.query.N1qlQuery; | |
import com.couchbase.client.java.search.SearchQuery; | |
import com.couchbase.client.java.search.result.AsyncSearchQueryResult; | |
import com.couchbase.client.java.search.result.SearchQueryRow; | |
import com.couchbase.client.java.subdoc.DocumentFragment; | |
import com.couchbase.client.java.transcoder.Transcoder; | |
import org.apache.log4j.Logger; | |
import rx.Observable; | |
import rx.functions.Action1; | |
import rx.functions.Func0; | |
import rx.functions.Func1; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.concurrent.TimeUnit; | |
public class Example { | |
public static void main(String[] args) throws Exception { | |
CouchbaseCluster cluster = CouchbaseCluster.create(); | |
List<Transcoder<? extends Document, ?>> transcoders = new ArrayList<>(); | |
transcoders.add(new ByteJsonTranscoder()); | |
Bucket bucket = cluster.openBucket("beer-sample", "", transcoders); | |
ByteJsonDocument doc = bucket.get("21st_amendment_brewery_cafe", ByteJsonDocument.class); | |
System.out.println(new String(doc.content(), CharsetUtil.UTF_8)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment