/** * @return a set of fetch options for the current limit and offset, or null if there is no limit * or offset. */ private FetchOptions fetchOptions() { // The FetchOptions builder interface is really, really awful if (this.cursor != null) { FetchOptions opts = FetchOptions.Builder.withCursor(this.cursor); if (this.limit != 0) opts = opts.limit(this.limit); if (this.offset != 0) opts = opts.offset(this.offset); return opts; } else if (this.limit != 0) { FetchOptions opts = FetchOptions.Builder.withLimit(this.limit); if (this.offset != 0) opts = opts.offset(this.offset); return opts; } else if (this.offset != 0) { FetchOptions opts = FetchOptions.Builder.withOffset(this.offset); return opts; } else { return null; } }
/* (non-Javadoc) * @see com.googlecode.objectify.Query#getKey() */ @Override public Key<T> getKey() { FetchOptions opts = FetchOptions.Builder.withLimit(1); if (this.offset > 0) opts = opts.offset(this.offset); Iterator<Entity> it = this.prepareKeysOnly().asIterator(opts); if (it.hasNext()) { Entity ent = it.next(); return this.factory.rawKeyToTypedKey(ent.getKey()); } else { return null; } }
/** * @return a set of fetch options for the current limit, offset, and cursors, based on the default * fetch options. There will always be options even if default. */ private FetchOptions fetchOptions() { FetchOptions opts = FetchOptions.Builder.withDefaults(); if (this.startAt != null) opts = opts.startCursor(this.startAt); if (this.endAt != null) opts = opts.endCursor(this.endAt); if (this.limit != 0) opts = opts.limit(this.limit); if (this.offset != 0) opts = opts.offset(this.offset); if (this.chunk == null) opts = opts.chunkSize(DEFAULT_CHUNK_SIZE); else opts = opts.chunkSize(this.chunk); return opts; }
/* (non-Javadoc) * @see com.googlecode.objectify.Query#get() */ @Override public T get() { // The underlying datastore is basically doing this for PreparedQuery.asSingleEntity(), // so let's do it ourselves and integrate offset() FetchOptions opts = FetchOptions.Builder.withLimit(1); if (this.offset > 0) opts = opts.offset(this.offset); if (this.cursor != null) opts = opts.cursor(this.cursor); Iterator<Entity> it = this.prepare().asIterator(opts); if (it.hasNext()) { Entity ent = it.next(); EntityMetadata<T> metadata = this.factory.getMetadata(ent.getKey()); return metadata.toObject(ent); } else { return null; } }