@Test
  public void getSliceRespectsAllBoundsInclusionArguments() throws Exception {
    // Test case where endColumn=startColumn+1
    ByteBuffer key = KeyColumnValueStoreUtil.longToByteBuffer(0);
    ByteBuffer columnBeforeStart = KeyColumnValueStoreUtil.longToByteBuffer(776);
    ByteBuffer columnStart = KeyColumnValueStoreUtil.longToByteBuffer(777);
    ByteBuffer columnEnd = KeyColumnValueStoreUtil.longToByteBuffer(778);
    ByteBuffer columnAfterEnd = KeyColumnValueStoreUtil.longToByteBuffer(779);

    // First insert four test Entries
    TransactionHandle txn = manager.beginTransaction();
    List<Entry> entries =
        Arrays.asList(
            new Entry(columnBeforeStart, columnBeforeStart),
            new Entry(columnStart, columnStart),
            new Entry(columnEnd, columnEnd),
            new Entry(columnAfterEnd, columnAfterEnd));
    store.mutate(key, entries, null, txn);
    txn.commit();

    // getSlice() with only start inclusive
    txn = manager.beginTransaction();
    List<Entry> result = store.getSlice(key, columnStart, columnEnd, txn);
    assertEquals(1, result.size());
    assertEquals(777, result.get(0).getColumn().getLong());
    txn.commit();
  }
  public void checkSlice(
      String[][] values, Set<KeyColumn> removed, int key, int start, int end, int limit) {
    List<Entry> entries;
    if (limit <= 0)
      entries =
          store.getSlice(
              KeyValueStoreUtil.getBuffer(key),
              KeyValueStoreUtil.getBuffer(start),
              KeyValueStoreUtil.getBuffer(end),
              tx);
    else
      entries =
          store.getSlice(
              KeyValueStoreUtil.getBuffer(key),
              KeyValueStoreUtil.getBuffer(start),
              KeyValueStoreUtil.getBuffer(end),
              limit,
              tx);

    int pos = 0;
    for (int i = start; i < end; i++) {
      if (removed.contains(new KeyColumn(key, i))) continue;
      if (limit <= 0 || pos < limit) {
        Entry entry = entries.get(pos);
        int col = KeyValueStoreUtil.getID(entry.getColumn());
        String str = KeyValueStoreUtil.getString(entry.getValue());
        assertEquals(i, col);
        assertEquals(values[key][i], str);
      }
      pos++;
    }
    assertNotNull(entries);
    if (limit > 0 && pos > limit) assertEquals(limit, entries.size());
    else assertEquals(pos, entries.size());
  }
 public void checkKeys(Set<Integer> removed) {
   for (int i = 0; i < numKeys; i++) {
     if (removed.contains(i)) {
       assertFalse(store.containsKey(KeyValueStoreUtil.getBuffer(i), tx));
     } else {
       assertTrue(store.containsKey(KeyValueStoreUtil.getBuffer(i), tx));
     }
   }
 }
  @Test
  public void containsKeyReturnsTrueOnExtantKey() throws Exception {
    ByteBuffer key1 = KeyColumnValueStoreUtil.longToByteBuffer(1);
    TransactionHandle txn = manager.beginTransaction();
    assertFalse(store.containsKey(key1.duplicate(), txn));
    KeyColumnValueStoreUtil.insert(store, txn, 1, "c", "v");
    txn.commit();

    txn = manager.beginTransaction();
    assertTrue(store.containsKey(key1.duplicate(), txn));
    txn.commit();
  }
  @Override
  public long[] indexRetrieval(Object key, TitanKey pt, InternalTitanTransaction tx) {
    Preconditions.checkArgument(
        pt.isSimple(), "Currently, only simple properties are supported for index retrieval");
    Preconditions.checkArgument(
        pt.hasIndex(), "Cannot retrieve for given property key - it does not have an index");

    long[] vertices = null;

    Preconditions.checkArgument(
        pt.getDataType().isInstance(key),
        "Specified object is incompatible with property data type [" + pt.getName() + "]");

    for (int readAttempt = 0; readAttempt < maxReadRetryAttempts; readAttempt++) {
      try {
        if (pt.isUnique()) {
          ByteBuffer value =
              propertyIndex.get(getIndexKey(key), getKeyedIndexColumn(pt), tx.getTxHandle());
          if (value != null) {
            vertices = new long[1];
            vertices[0] = VariableLong.readPositive(value);
          }
        } else {
          ByteBuffer startColumn = VariableLong.positiveByteBuffer(pt.getID());
          List<Entry> entries =
              propertyIndex.getSlice(
                  getIndexKey(key),
                  startColumn,
                  ByteBufferUtil.nextBiggerBuffer(startColumn),
                  tx.getTxHandle());
          vertices = new long[entries.size()];
          int i = 0;
          for (Entry ent : entries) {
            vertices[i++] = VariableLong.readPositive(ent.getValue());
          }
        }

        break;
      } catch (StorageException e) {
        if (e instanceof TemporaryStorageException) {
          if (readAttempt < maxReadRetryAttempts - 1) temporaryStorageException(e);
          else throw readException(e, maxReadRetryAttempts);
        } else throw readException(e);
      }
    }

    if (vertices == null) return new long[0];
    else return vertices;
  }
  private List<Entry> appendResults(
      ByteBuffer key,
      ByteBuffer columnStart,
      ByteBuffer columnEnd,
      List<Entry> entries,
      LimitTracker limit,
      TransactionHandle txh) {
    if (limit.limitExhausted()) return null;
    List<Entry> results = null;

    for (int readAttempt = 0; readAttempt < maxReadRetryAttempts; readAttempt++) {
      try {
        results = edgeStore.getSlice(key, columnStart, columnEnd, limit.getLimit(), txh);
        break;
      } catch (StorageException e) {
        if (e instanceof TemporaryStorageException) {
          if (readAttempt < maxReadRetryAttempts - 1) temporaryStorageException(e);
          else throw readException(e, maxReadRetryAttempts);
        } else throw readException(e);
      }
    }

    limit.retrieved(results.size());

    if (entries == null) return results;
    else {
      entries.addAll(results);
      return entries;
    }
  }
  @Override
  public synchronized void shutdown() throws TitanException {
    if (!isOpen) return;
    super.shutdown();
    etManager.close();
    idAssigner.close();

    try {
      edgeStore.close();
      propertyIndex.close();
      storage.close();
    } catch (StorageException e) {
      throw new TitanException("Could not close storage backend", e);
    }
    isOpen = false;
  }
 @Test
 public void containsKeyReturnsFalseOnNonexistentKey() throws Exception {
   TransactionHandle txn = manager.beginTransaction();
   ByteBuffer key1 = KeyColumnValueStoreUtil.longToByteBuffer(1);
   assertFalse(store.containsKey(key1.duplicate(), txn));
   txn.commit();
 }
 public void loadValues(String[][] values) {
   for (int i = 0; i < numKeys; i++) {
     List<Entry> entries = new ArrayList<Entry>();
     for (int j = 0; j < numColumns; j++) {
       entries.add(
           new Entry(KeyValueStoreUtil.getBuffer(j), KeyValueStoreUtil.getBuffer(values[i][j])));
     }
     store.mutate(KeyValueStoreUtil.getBuffer(i), entries, null, tx);
   }
 }
  @Test
  public void getSliceRespectsColumnLimit() throws Exception {
    TransactionHandle txn = manager.beginTransaction();
    ByteBuffer key = KeyColumnValueStoreUtil.longToByteBuffer(0);

    final int cols = 1024;

    List<Entry> entries = new LinkedList<Entry>();
    for (int i = 0; i < cols; i++) {
      ByteBuffer col = KeyColumnValueStoreUtil.longToByteBuffer(i);
      entries.add(new Entry(col, col));
    }
    store.mutate(key, entries, null, txn);
    txn.commit();

    txn = manager.beginTransaction();
    ByteBuffer columnStart = KeyColumnValueStoreUtil.longToByteBuffer(0);
    ByteBuffer columnEnd = KeyColumnValueStoreUtil.longToByteBuffer(cols);
    /*
     * When limit is greater than or equal to the matching column count,
     * all matching columns must be returned.
     */
    List<Entry> result = store.getSlice(key, columnStart, columnEnd, cols, txn);
    assertEquals(cols, result.size());
    assertEquals(entries, result);
    result = store.getSlice(key, columnStart, columnEnd, cols + 10, txn);
    assertEquals(cols, result.size());
    assertEquals(entries, result);

    /*
     * When limit is less the matching column count, the columns up to the
     * limit (ordered bytewise) must be returned.
     */
    result = store.getSlice(key, columnStart, columnEnd, cols - 1, txn);
    assertEquals(cols - 1, result.size());
    entries.remove(entries.size() - 1);
    assertEquals(entries, result);
    result = store.getSlice(key, columnStart, columnEnd, 1, txn);
    assertEquals(1, result.size());
    List<Entry> firstEntrySingleton = Arrays.asList(entries.get(0));
    assertEquals(firstEntrySingleton, result);
    txn.commit();
  }
 public void checkValues(String[][] values, Set<KeyColumn> removed) {
   for (int i = 0; i < numKeys; i++) {
     for (int j = 0; j < numColumns; j++) {
       ByteBuffer result =
           store.get(KeyValueStoreUtil.getBuffer(i), KeyValueStoreUtil.getBuffer(j), tx);
       if (removed.contains(new KeyColumn(i, j))) {
         assertNull(result);
       } else {
         Assert.assertEquals(values[i][j], KeyValueStoreUtil.getString(result));
       }
     }
   }
 }
 public Set<Integer> deleteKeys(int every) {
   Set<Integer> removed = new HashSet<Integer>();
   for (int i = 0; i < numKeys; i++) {
     if (i % every == 0) {
       removed.add(i);
       List<ByteBuffer> deletions = new ArrayList<ByteBuffer>();
       for (int j = 0; j < numColumns; j++) {
         deletions.add(KeyValueStoreUtil.getBuffer(j));
       }
       store.mutate(KeyValueStoreUtil.getBuffer(i), null, deletions, tx);
     }
   }
   return removed;
 }
 public void checkValueExistence(String[][] values, Set<KeyColumn> removed) {
   for (int i = 0; i < numKeys; i++) {
     for (int j = 0; j < numColumns; j++) {
       boolean result =
           store.containsKeyColumn(
               KeyValueStoreUtil.getBuffer(i), KeyValueStoreUtil.getBuffer(j), tx);
       if (removed.contains(new KeyColumn(i, j))) {
         assertFalse(result);
       } else {
         assertTrue(result);
       }
     }
   }
 }
  @Override
  public boolean containsVertexID(long id, InternalTitanTransaction tx) {
    log.trace("Checking node existence for {}", id);

    for (int readAttempt = 0; readAttempt < maxReadRetryAttempts; readAttempt++) {
      try {
        return edgeStore.containsKey(IDHandler.getKey(id), tx.getTxHandle());
      } catch (StorageException e) {
        if (e instanceof TemporaryStorageException) {
          if (readAttempt < maxReadRetryAttempts - 1) temporaryStorageException(e);
          else throw readException(e, maxReadRetryAttempts);
        } else throw readException(e);
      }
    }
    throw new AssertionError("Illegal program state");
  }
 public Set<KeyColumn> deleteValues(int every) {
   Set<KeyColumn> removed = new HashSet<KeyColumn>();
   int counter = 0;
   for (int i = 0; i < numKeys; i++) {
     List<ByteBuffer> deletions = new ArrayList<ByteBuffer>();
     for (int j = 0; j < numColumns; j++) {
       counter++;
       if (counter % every == 0) {
         // remove
         removed.add(new KeyColumn(i, j));
         deletions.add(KeyValueStoreUtil.getBuffer(j));
       }
     }
     store.mutate(KeyValueStoreUtil.getBuffer(i), null, deletions, tx);
   }
   return removed;
 }
 public void close() {
   if (tx != null) tx.commit();
   store.close();
   manager.close();
 }