Package com.google.appengine.api.datastore

Examples of com.google.appengine.api.datastore.KeyRange


     * @throws Exception
     */
    @Test
    public void allocateIdsAsyncWithParentKey() throws Exception {
        Key parentKey = KeyFactory.createKey("Parent", 1);
        KeyRange range = Datastore.allocateIdsAsync(parentKey, "Hoge", 2).get();
        assertThat(range, is(notNullValue()));
        assertThat(range.getSize(), is(2L));

        range = Datastore.allocateIdsAsync(parentKey, Hoge.class, 2).get();
        assertThat(range, is(notNullValue()));
        assertEquals(2, range.getSize());

        range = Datastore.allocateIdsAsync(parentKey, meta, 2).get();
        assertThat(range, is(notNullValue()));
        assertThat(range.getSize(), is(2L));
    }
View Full Code Here


      // shouldn't happen
      throw new IllegalStateException("sequence name is null");
    }
    DatastoreServiceConfig config = ((DatastoreManager) storeMgr).getDefaultDatastoreServiceConfigForWrites();
    DatastoreService ds = DatastoreServiceFactoryInternal.getDatastoreService(config);
    KeyRange range = ds.allocateIds(sequenceName, size);
    // Too bad we can't pass an iterable and construct the ids
    // on demand.
    List<Long> ids = Utils.newArrayList();
    long current = range.getStart().getId();
    for (int i = 0; i < size; i++) {
      ids.add(current + i);
    }
    return new ValueGenerationBlock(ids);
  }
View Full Code Here

    if (sequenceName == null) {
      // shouldn't happen
      throw new IllegalStateException("sequence name is null");
    }
    DatastoreService ds = DatastoreServiceFactoryInternal.getDatastoreService();
    KeyRange range = ds.allocateIds(sequenceName, size);
    // Too bad we can't pass an iterable and construct the ids
    // on demand.
    List<Long> ids = Utils.newArrayList();
    long current = range.getStart().getId();
    for (int i = 0; i < size; i++) {
      ids.add(current + i);
    }
    return new ValueGenerationBlock(ids);
  }
View Full Code Here

      // shouldn't happen
      throw new IllegalStateException("sequence name is null");
    }
    DatastoreServiceConfig config = ((DatastoreManager) storeMgr).getDefaultDatastoreServiceConfigForWrites();
    DatastoreService ds = DatastoreServiceFactoryInternal.getDatastoreService(config);
    KeyRange range = ds.allocateIds(sequenceName, size);
    // Too bad we can't pass an iterable and construct the ids
    // on demand.
    List<Long> ids = Utils.newArrayList();
    long current = range.getStart().getId();
    for (int i = 0; i < size; i++) {
      ids.add(current + i);
    }
    return new ValueGenerationBlock(ids);
  }
View Full Code Here

    private static final String ALLOCATE_IDS_ENTITY = "AllocateIdsEntity";

    @Test
    public void testAllocateId() throws Exception {
        KeyRange firstBlock = service.allocateIds(ALLOCATE_IDS_ENTITY, 10L);

        Assert.assertNotNull(firstBlock);
        Assert.assertEquals(10, firstBlock.getEnd().getId() - firstBlock.getStart().getId() + 1);
        Assert.assertEquals(10, firstBlock.getSize());

        KeyRange secondBlock = service.allocateIds(ALLOCATE_IDS_ENTITY, 10L);
        Assert.assertNotNull(secondBlock);
        Assert.assertFalse("Allocated key ranges should not overlap.", rangeOverlap(firstBlock, secondBlock));
    }
View Full Code Here

    @Test
    public void testCheckKeyRange() throws Exception {
        long initialValue = getInitialValue("OtherKind");

        KeyRange kr1 = new KeyRange(null, "OtherKind", 1 + initialValue, 5 + initialValue);
        DatastoreService.KeyRangeState state1 = service.allocateIdRange(kr1);
        Assert.assertNotNull(state1);
        // imo, it could be either -- depending on the impl
        Assert.assertTrue(DatastoreService.KeyRangeState.CONTENTION == state1 || DatastoreService.KeyRangeState.EMPTY == state1);

        KeyRange kr2 = service.allocateIds("OtherKind", 6);
        Assert.assertNotNull(kr2);

        KeyRange kr3 = new KeyRange(null, "OtherKind", 2 + initialValue, 5 + initialValue);
        DatastoreService.KeyRangeState state2 = service.allocateIdRange(kr3);
        Assert.assertNotNull(state2);
        // can it be both, depending on the impl?
        Assert.assertTrue(DatastoreService.KeyRangeState.COLLISION == state2 || DatastoreService.KeyRangeState.CONTENTION == state2);
    }
View Full Code Here

        Entity parent = new Entity(ALLOCATE_IDS_ENTITY);
        parent.setProperty("name", "parent-" + System.currentTimeMillis());
        Key parentKey = service.put(parent);

        final int allocateSize = 10;
        KeyRange range = service.allocateIds(parentKey, ALLOCATE_IDS_ENTITY, allocateSize);

        Entity child = new Entity(range.getStart());
        Key key = service.put(child);

        // child with allocated key should have correct parent.
        Assert.assertEquals(parentKey, key.getParent());
    }
View Full Code Here

    @Test
    public void testOutOfRangeEntity() throws Exception {
        final long allocateNum = 5;

        // Range default namespace
        KeyRange range = service.allocateIds(ALLOCATE_IDS_ENTITY, allocateNum);

        Entity noParent = createTestEntity(ALLOCATE_IDS_ENTITY);
        assertEntityNotInRange(noParent, range);

        // Range with specified parent
        Entity parent = new Entity(ALLOCATE_IDS_ENTITY);
        Key parentKey = service.put(parent);
        KeyRange range2 = service.allocateIds(parentKey, ALLOCATE_IDS_ENTITY, allocateNum);

        Entity entity = createTestEntity(ALLOCATE_IDS_ENTITY, parentKey);
        assertEntityNotInRange(entity, range2);

        // In Range entity should have same parent
        Entity child = new Entity(range2.getStart());
        Key childKey = service.put(child);
        // child with allocated key should have correct parent.
        Assert.assertEquals(parentKey, childKey.getParent());
    }
View Full Code Here

    public void testDataAllocate() throws Exception {
        final long allocateNum = 5;

        // Range default namespace
        Future<KeyRange> futureRange = asyncService.allocateIds(ASYNC_ENTITY, allocateNum);
        KeyRange range = futureRange.get();
        assertTaskIsDoneAndNotCancelled(futureRange);

        Entity noParent = createTestEntity(ASYNC_ENTITY);
        assertEntityNotInRange(noParent, range);

        // Range with specified parent
        Entity parent = new Entity(ASYNC_ENTITY);
        parent.setProperty("name", "parent" + new Date());
        Key parentKey = service.put(parent);
        Future<KeyRange> futureRange2 = asyncService.allocateIds(parentKey, ASYNC_ENTITY, allocateNum);
        KeyRange range2 = futureRange2.get();
        assertTaskIsDoneAndNotCancelled(futureRange2);

        Entity noParent2 = createTestEntity(ASYNC_ENTITY, parentKey);
        assertEntityNotInRange(noParent2, range2);

        // In Range entity should have same parent
        Entity child = new Entity(range2.getStart());
        child.setProperty("name", "second" + new Date());
        Key childKey = service.put(child);
        // child with allocated key should have correct parent.
        assertEquals(parentKey, childKey.getParent());
    }
View Full Code Here

   * @param data
   */
  public void createKey(String kind, Object data) {
    Key key;
    try {
      KeyRange range = createIdRange(kind, data);
      key = range.getStart();
      // This is a 1-length range, so getting start value is by far
      // enough
      idField.set(data, key.getId());
    } catch (Exception e) {
      throw new UnableToSetFieldException(e, idField);
View Full Code Here

TOP

Related Classes of com.google.appengine.api.datastore.KeyRange

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.