Skip to content

Commit b885488

Browse files
committed
---
yaml --- r: 27 b: refs/heads/master c: ab71adf h: refs/heads/master i: 25: 9ae82d5 23: 756fa2e v: v3
1 parent 30df7ec commit b885488

32 files changed

Lines changed: 1052 additions & 256 deletions

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: c8c83045df5330a98888e6a3b73f126f9449a859
2+
refs/heads/master: ab71adf45cffda1dd40c193dd50b2bf59473c266

trunk/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@
3737
<scope>test</scope>
3838
<version>RELEASE</version>
3939
</dependency>
40+
<dependency>
41+
<groupId>joda-time</groupId>
42+
<artifactId>joda-time</artifactId>
43+
<version>RELEASE</version>
44+
</dependency>
4045
</dependencies>
4146
<build>
4247
<plugins>

trunk/src/main/java/com/google/gcloud/datastore/BatchWriter.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,10 @@
22

33
public interface BatchWriter extends DatastoreWriter {
44

5+
/**
6+
* Submit the batch to the Datastore.
7+
*
8+
* @throws DatastoreServiceException if there was any failure.
9+
*/
510
void submit();
6-
}
11+
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package com.google.gcloud.datastore;
2+
3+
import static com.google.api.client.util.Preconditions.checkArgument;
4+
import static com.google.common.base.Preconditions.checkNotNull;
5+
6+
import com.google.protobuf.ByteString;
7+
8+
import java.io.BufferedInputStream;
9+
import java.io.ByteArrayOutputStream;
10+
import java.io.IOException;
11+
import java.io.InputStream;
12+
import java.nio.ByteBuffer;
13+
import java.util.Objects;
14+
15+
/**
16+
* A Google Cloud Datastore Blob.
17+
* A Datastore blob is limited to {@value #MAX_LENGTH} bytes.
18+
* This class is immutable.
19+
*
20+
* @see <a href="https://cloud.google.com/datastore/docs/concepts/entities">Google Cloud Datastore Entities, Properties, and Keys</a>
21+
*/
22+
public final class Blob implements java.io.Serializable {
23+
24+
private static final long serialVersionUID = 3835421019618247721L;
25+
private static final int MAX_LENGTH = 1_000_000;
26+
27+
private final ByteString byteString;
28+
29+
Blob(ByteString byteString, boolean enforceLimits) {
30+
this.byteString = checkNotNull(byteString);
31+
if (enforceLimits) {
32+
checkArgument(byteString.size() <= MAX_LENGTH, "May be a maximum of 1,000,000 bytes");
33+
}
34+
}
35+
36+
@Override
37+
public String toString() {
38+
return byteString.toString();
39+
}
40+
41+
@Override
42+
public int hashCode() {
43+
return byteString.hashCode();
44+
}
45+
46+
@Override
47+
public boolean equals(Object obj) {
48+
if (!(obj instanceof Blob)) {
49+
return false;
50+
}
51+
return Objects.equals(byteString, ((Blob) obj).byteString);
52+
}
53+
54+
/**
55+
* Returns the size of this blob.
56+
*/
57+
public int length() {
58+
return byteString.size();
59+
}
60+
61+
/**
62+
* Returns a copy as byte array.
63+
*/
64+
public byte[] toByteArray() {
65+
return byteString.toByteArray();
66+
}
67+
68+
/**
69+
* Returns a read-only {@link ByteBuffer} for this blob content.
70+
*/
71+
public ByteBuffer asReadOnlyByteBuffer() {
72+
return byteString.asReadOnlyByteBuffer();
73+
}
74+
75+
/**
76+
* Returns an {@link InputStream} for this blob content.
77+
*/
78+
public InputStream asInputStream() {
79+
final ByteBuffer byteBuffer = asReadOnlyByteBuffer();
80+
return new InputStream() {
81+
@Override public int read() throws IOException {
82+
return !byteBuffer.hasRemaining() ? -1 : byteBuffer.get() & 0xFF;
83+
}
84+
};
85+
}
86+
87+
/**
88+
* Copies bytes into a ByteBuffer.
89+
*
90+
* @throws java.nio.ReadOnlyBufferException if the target is read-only
91+
* @throws java.nio.BufferOverflowException if the target's remaining() space is not large
92+
* enough to hold the data.
93+
*/
94+
public void copyTo(ByteBuffer target) {
95+
byteString.copyTo(target);
96+
}
97+
98+
/**
99+
* Copies bytes into a buffer.
100+
*
101+
* @throws java.io.IndexOutOfBoundsException if an offset or size is negative or too large
102+
*/
103+
public void copyTo(byte[] target) {
104+
byteString.copyTo(target, 0, 0, length());
105+
}
106+
107+
ByteString byteString() {
108+
return byteString;
109+
}
110+
111+
public static Blob copyFrom(byte[] bytes) {
112+
return new Blob(ByteString.copyFrom(bytes), true);
113+
}
114+
115+
public static Blob copyFrom(ByteBuffer bytes) {
116+
return new Blob(ByteString.copyFrom(bytes), true);
117+
}
118+
119+
public static Blob copyFrom(InputStream input) throws IOException {
120+
BufferedInputStream bufferedInput = new BufferedInputStream(input);
121+
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
122+
int value;
123+
while ((value = bufferedInput.read()) != -1) {
124+
bytes.write(value);
125+
}
126+
return copyFrom(bytes.toByteArray());
127+
}
128+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.google.gcloud.datastore;
2+
3+
import static com.google.api.services.datastore.DatastoreV1.Value.BLOB_VALUE_FIELD_NUMBER;
4+
5+
import com.google.api.services.datastore.DatastoreV1;
6+
7+
public final class BlobValue extends Value<Blob, BlobValue, BlobValue.Builder> {
8+
9+
private static final long serialVersionUID = -5096238337676649540L;
10+
11+
static final BaseMarshaller<Blob, BlobValue, Builder> MARSHALLER =
12+
new BaseMarshaller<Blob, BlobValue, Builder>() {
13+
14+
@Override
15+
public int getProtoFieldId() {
16+
return BLOB_VALUE_FIELD_NUMBER;
17+
}
18+
19+
@Override
20+
public Builder newBuilder(Blob value) {
21+
return new Builder(value);
22+
}
23+
24+
@Override
25+
protected Blob getValue(DatastoreV1.Value from) {
26+
return new Blob(from.getBlobValue(), false);
27+
}
28+
29+
@Override
30+
protected void setValue(BlobValue from, DatastoreV1.Value.Builder to) {
31+
to.setBlobValue(from.get().byteString());
32+
}
33+
};
34+
35+
public static final class Builder extends Value.BaseBuilder<Blob, BlobValue, Builder> {
36+
37+
public Builder(Blob blob) {
38+
super(Type.BLOB);
39+
set(blob);
40+
}
41+
42+
@Override
43+
public BlobValue build() {
44+
return new BlobValue(this);
45+
}
46+
}
47+
48+
public BlobValue(Blob blob) {
49+
this(new Builder(blob));
50+
}
51+
52+
BlobValue(Builder builder) {
53+
super(builder);
54+
}
55+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.google.gcloud.datastore;
2+
3+
import static com.google.api.services.datastore.DatastoreV1.Value.BOOLEAN_VALUE_FIELD_NUMBER;
4+
5+
import com.google.api.services.datastore.DatastoreV1;
6+
7+
public final class BooleanValue extends Value<Boolean, BooleanValue, BooleanValue.Builder> {
8+
9+
private static final long serialVersionUID = -542649497897250340L;
10+
11+
static final BaseMarshaller<Boolean, BooleanValue, Builder> MARSHALLER =
12+
new BaseMarshaller<Boolean, BooleanValue, Builder>() {
13+
14+
@Override
15+
public int getProtoFieldId() {
16+
return BOOLEAN_VALUE_FIELD_NUMBER;
17+
}
18+
19+
@Override
20+
public Builder newBuilder(Boolean value) {
21+
return new Builder(value);
22+
}
23+
24+
@Override
25+
protected Boolean getValue(DatastoreV1.Value from) {
26+
return from.getBooleanValue();
27+
}
28+
29+
@Override
30+
protected void setValue(BooleanValue from, DatastoreV1.Value.Builder to) {
31+
to.setBooleanValue(from.get());
32+
}
33+
};
34+
35+
public static final class Builder extends Value.BaseBuilder<Boolean, BooleanValue, Builder> {
36+
37+
public Builder(boolean value) {
38+
super(Type.BOOLEAN);
39+
set(value);
40+
}
41+
42+
@Override
43+
public BooleanValue build() {
44+
return new BooleanValue(this);
45+
}
46+
}
47+
48+
public BooleanValue(boolean value) {
49+
this(new Builder(value));
50+
}
51+
52+
BooleanValue(Builder builder) {
53+
super(builder);
54+
}
55+
}

trunk/src/main/java/com/google/gcloud/datastore/Cursor.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@
1111
import java.net.URLEncoder;
1212
import java.util.Arrays;
1313

14+
/**
15+
* A Google Cloud Datastore cursor.
16+
* The cursor can be used to as a starting point or an ending point for a {@link Query}
17+
*/
1418
public final class Cursor implements Serializable {
1519

1620
private static final long serialVersionUID = -1423744878777486541L;
1721

18-
private byte[] bytes;
22+
private final byte[] bytes;
1923

2024
public Cursor(byte[] bytes) {
2125
this.bytes = checkNotNull(bytes);
@@ -40,6 +44,9 @@ public String toString() {
4044
return toPb().toString();
4145
}
4246

47+
/**
48+
* Returns the cursor in an encoded form that can be used as part of a URL.
49+
*/
4350
public String toUrlSafe() {
4451
try {
4552
return URLEncoder.encode(toString(), UTF_8.name());
@@ -52,6 +59,9 @@ ByteString toPb() {
5259
return ByteString.copyFrom(bytes);
5360
}
5461

62+
/**
63+
* Create a {@code Cursor} given its URL safe encoded form.
64+
*/
5565
public static Cursor fromUrlSafe(String urlSafe) {
5666
try {
5767
String utf8Str = URLDecoder.decode(urlSafe, UTF_8.name());

trunk/src/main/java/com/google/gcloud/datastore/DatastoreReader.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,25 @@
77
*/
88
public interface DatastoreReader {
99

10+
/**
11+
* Returns an {@link Entity} for the given {@link Key} or {@code null} if does not exists.
12+
*
13+
* @throws DatastoreServiceException upon failure.
14+
*/
1015
Entity get(Key key);
1116

12-
// results are returned using request order
17+
/**
18+
* Returns an {@link Entity} for each given {@link Key} or {@code null} if does not exists
19+
* ordered by input.
20+
*
21+
* @throws DatastoreServiceException upon failure.
22+
*/
1323
Iterator<Entity> get(Key... key);
1424

25+
/**
26+
* Submit a {@link Query} and returns its result.
27+
*
28+
* @throws DatastoreServiceException upon failure.
29+
*/
1530
QueryResult<PartialEntity> runQuery(Query query);
1631
}

trunk/src/main/java/com/google/gcloud/datastore/DatastoreService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import java.util.Iterator;
44

55
/**
6-
* An interface for Google Cloud Datastore.
6+
* An interface for Google Cloud Datastore dataset.
77
*/
88
public interface DatastoreService extends DatastoreReader, DatastoreWriter {
99

trunk/src/main/java/com/google/gcloud/datastore/DatastoreServiceImpl.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,19 @@ public BatchWriter newBatchWriter(BatchWriteOption... batchWriteOption) {
3030

3131
@Override
3232
public Key allocateId(PartialKey key) {
33-
// TODO Auto-generated method stub
34-
return null;
33+
return allocateIds(key).next();
3534
}
3635

3736
@Override
3837
public Iterator<Key> allocateIds(PartialKey... key) {
3938
// TODO Auto-generated method stub
39+
// Will need to populate "force" after b/18594027 is fixed.
4040
return null;
4141
}
4242

4343
@Override
4444
public Entity get(Key key) {
45-
// TODO Auto-generated method stub
46-
return null;
45+
return get(new Key[]{key}).next();
4746
}
4847

4948
@Override

0 commit comments

Comments
 (0)