DefaultIndex implements Index { @Override public void add(Key key, Context context, KUID valueId) throws SQLException { long now = System.currentTimeMillis(); Date created = new Date(now); Timestamp modified = new Timestamp(now); context.addHeader(Constants.VALUE_ID, valueId.toHexString()); context.addHeader(Constants.DATE, DateUtils.format(now)); KUID bucketId = key.getId(); String bucket = key.getBucket(); URI uri = key.getURI(); KUID keyId = KeyId.valueOf(key); try { cm.beginTxn(); { PreparedStatement ps = cm.prepareStatement( factory.upsertBuckets()); try { setBytes(ps, 1, bucketId); ps.setString(2, bucket); ps.setDate(3, created); ps.setTimestamp(4, modified); ps.executeUpdate(); } finally { Utils.close(ps); } } { PreparedStatement ps = cm.prepareStatement( factory.upsertKeys()); try { setBytes(ps, 1, keyId); setBytes(ps, 2, bucketId); ps.setString(3, uri.toString()); ps.setDate(4, created); ps.setTimestamp(5, modified); ps.executeUpdate(); } finally { Utils.close(ps); } } { PreparedStatement ps = cm.prepareStatement(INSERT_VALUE); try { setBytes(ps, 1, valueId); setBytes(ps, 2, keyId); ps.setDate(3, created); ps.executeUpdate(); } finally { Utils.close(ps); } } { PreparedStatement ps = cm.prepareStatement(INSERT_PROPERTY); try { for (Header header : context.getHeaders()) { String name = header.getName(); String value = header.getValue(); setBytes(ps, 1, valueId); ps.setString(2, name); ps.setString(3, value); ps.addBatch(); } ps.executeBatch(); } finally { Utils.close(ps); } } cm.commit(); } finally { cm.endTxn(); } } private  DefaultIndex(StatementFactory factory, ConnectionManager connection); static DefaultIndex create(File dir); static DefaultIndex create(File dir, int length); @Override void close(); @Override void add(Key key, Context context, KUID valueId); @Override Keys keys(String marker, int maxCount); @Override Context get(Key key, KUID valueId); @Override Values values(Key key, KUID marker, int maxCount); @Override boolean delete(Key key, KUID valueId); @Override boolean remove(Key key, KUID valueId); static final int LENGTH; }
@Test public void add() throws Exception { final int count = 10; File dir = new File("data/test"); Index index = DefaultIndex.create(dir); Set<KUID> dst = new TreeSet<KUID>(); Key key = KeyFactory.parseKey("ardverk: for (int i = 0; i < count; i++) { KUID valueId = KUID.createRandom(key.getId()); Context context = new Context(); context.addHeader("X-Index", Integer.toString(i)); index.add(key, context, valueId); dst.add(valueId); } final int m = dst.size()/2-1; final int maxCount = 6; KUID marker = CollectionUtils.nth(dst, m); Values values = index.values(key, marker, maxCount); TestCase.assertEquals(marker, values.firstKey()); TestCase.assertTrue(values.size() <= maxCount); }