use of java.time.Instant in project titan by thinkaurelius.
the class LockCleanerRunnableTest method testDeleteSingleLock.
/**
* Simplest case test of the lock cleaner.
*/
@Test
public void testDeleteSingleLock() throws BackendException {
Instant now = Instant.ofEpochMilli(1L);
Entry expiredLockCol = StaticArrayEntry.of(codec.toLockCol(now, defaultLockRid, TimestampProviders.MILLI), BufferUtil.getIntBuffer(0));
EntryList expiredSingleton = StaticArrayEntryList.of(expiredLockCol);
now = now.plusMillis(1);
del = new StandardLockCleanerRunnable(store, kc, tx, codec, now, TimestampProviders.MILLI);
expect(store.getSlice(eq(ksq), eq(tx))).andReturn(expiredSingleton);
store.mutate(eq(key), eq(ImmutableList.<Entry>of()), eq(ImmutableList.<StaticBuffer>of(expiredLockCol.getColumn())), anyObject(StoreTransaction.class));
ctrl.replay();
del.run();
}
use of java.time.Instant in project titan by thinkaurelius.
the class LockCleanerServiceTest method testCleanCooldownElapses.
@Test
public void testCleanCooldownElapses() throws InterruptedException {
final Instant cutoff = Instant.ofEpochMilli(1L);
Duration wait = Duration.ofMillis(500L);
svc = new StandardLockCleanerService(store, codec, exec, wait, TimestampProviders.MILLI);
expect(exec.submit(eq(new StandardLockCleanerRunnable(store, kc, tx, codec, cutoff, TimestampProviders.MILLI)))).andReturn(null);
expect(exec.submit(eq(new StandardLockCleanerRunnable(store, kc, tx, codec, cutoff.plusMillis(1), TimestampProviders.MILLI)))).andReturn(null);
ctrl.replay();
for (int i = 0; i < 2; i++) {
svc.clean(kc, cutoff, tx);
}
TimestampProviders.MILLI.sleepFor(wait.plusMillis(1));
for (int i = 0; i < 2; i++) {
svc.clean(kc, cutoff.plusMillis(1), tx);
}
}
use of java.time.Instant in project titan by thinkaurelius.
the class TestTimestamps method testRoundTrip.
private void testRoundTrip(TimestampProvider p) {
Instant now = p.getTime();
long time = p.getTime(now);
Instant now2 = p.getTime(time);
Assert.assertEquals(now, now2);
}
use of java.time.Instant in project titan by thinkaurelius.
the class LuceneIndex method convertQuery.
private final SearchParams convertQuery(Condition<?> condition, KeyInformation.StoreRetriever informations) {
SearchParams params = new SearchParams();
if (condition instanceof PredicateCondition) {
PredicateCondition<String, ?> atom = (PredicateCondition) condition;
Object value = atom.getValue();
String key = atom.getKey();
TitanPredicate titanPredicate = atom.getPredicate();
if (value instanceof Number) {
Preconditions.checkArgument(titanPredicate instanceof Cmp, "Relation not supported on numeric types: " + titanPredicate);
Preconditions.checkArgument(value instanceof Number);
params.addFilter(numericFilter(key, (Cmp) titanPredicate, (Number) value));
} else if (value instanceof String) {
Mapping map = Mapping.getMapping(informations.get(key));
if ((map == Mapping.DEFAULT || map == Mapping.TEXT) && !titanPredicate.toString().startsWith("CONTAINS"))
throw new IllegalArgumentException("Text mapped string values only support CONTAINS queries and not: " + titanPredicate);
if (map == Mapping.STRING && titanPredicate.toString().startsWith("CONTAINS"))
throw new IllegalArgumentException("String mapped string values do not support CONTAINS queries: " + titanPredicate);
if (titanPredicate == Text.CONTAINS) {
value = ((String) value).toLowerCase();
BooleanFilter b = new BooleanFilter();
for (String term : Text.tokenize((String) value)) {
b.add(new TermsFilter(new Term(key, term)), BooleanClause.Occur.MUST);
}
params.addFilter(b);
} else if (titanPredicate == Text.CONTAINS_PREFIX) {
value = ((String) value).toLowerCase();
params.addFilter(new PrefixFilter(new Term(key, (String) value)));
} else if (titanPredicate == Text.PREFIX) {
params.addFilter(new PrefixFilter(new Term(key, (String) value)));
} else if (titanPredicate == Text.REGEX) {
RegexpQuery rq = new RegexpQuery(new Term(key, (String) value));
params.addQuery(rq);
} else if (titanPredicate == Text.CONTAINS_REGEX) {
// This is terrible -- there is probably a better way
RegexpQuery rq = new RegexpQuery(new Term(key, ".*" + (value) + ".*"));
params.addQuery(rq);
} else if (titanPredicate == Cmp.EQUAL) {
params.addFilter(new TermsFilter(new Term(key, (String) value)));
} else if (titanPredicate == Cmp.NOT_EQUAL) {
BooleanFilter q = new BooleanFilter();
q.add(new TermsFilter(new Term(key, (String) value)), BooleanClause.Occur.MUST_NOT);
params.addFilter(q);
} else
throw new IllegalArgumentException("Relation is not supported for string value: " + titanPredicate);
} else if (value instanceof Geoshape) {
Preconditions.checkArgument(titanPredicate == Geo.WITHIN, "Relation is not supported for geo value: " + titanPredicate);
Shape shape = ((Geoshape) value).convert2Spatial4j();
SpatialArgs args = new SpatialArgs(SpatialOperation.IsWithin, shape);
params.addFilter(getSpatialStrategy(key).makeFilter(args));
} else if (value instanceof Date) {
Preconditions.checkArgument(titanPredicate instanceof Cmp, "Relation not supported on date types: " + titanPredicate);
params.addFilter(numericFilter(key, (Cmp) titanPredicate, ((Date) value).getTime()));
} else if (value instanceof Instant) {
Preconditions.checkArgument(titanPredicate instanceof Cmp, "Relation not supported on instant types: " + titanPredicate);
params.addFilter(numericFilter(key, (Cmp) titanPredicate, ((Instant) value).toEpochMilli()));
} else if (value instanceof Boolean) {
Preconditions.checkArgument(titanPredicate instanceof Cmp, "Relation not supported on boolean types: " + titanPredicate);
int intValue;
switch((Cmp) titanPredicate) {
case EQUAL:
intValue = ((Boolean) value) ? 1 : 0;
params.addFilter(NumericRangeFilter.newIntRange(key, intValue, intValue, true, true));
break;
case NOT_EQUAL:
intValue = ((Boolean) value) ? 0 : 1;
params.addFilter(NumericRangeFilter.newIntRange(key, intValue, intValue, true, true));
break;
default:
throw new IllegalArgumentException("Boolean types only support EQUAL or NOT_EQUAL");
}
} else if (value instanceof UUID) {
Preconditions.checkArgument(titanPredicate instanceof Cmp, "Relation not supported on UUID types: " + titanPredicate);
if (titanPredicate == Cmp.EQUAL) {
params.addFilter(new TermsFilter(new Term(key, value.toString())));
} else if (titanPredicate == Cmp.NOT_EQUAL) {
BooleanFilter q = new BooleanFilter();
q.add(new TermsFilter(new Term(key, value.toString())), BooleanClause.Occur.MUST_NOT);
params.addFilter(q);
} else {
throw new IllegalArgumentException("Relation is not supported for UUID type: " + titanPredicate);
}
} else {
throw new IllegalArgumentException("Unsupported type: " + value);
}
} else if (condition instanceof Not) {
SearchParams childParams = convertQuery(((Not) condition).getChild(), informations);
params.addParams(childParams, BooleanClause.Occur.MUST_NOT);
} else if (condition instanceof And) {
for (Condition c : condition.getChildren()) {
SearchParams childParams = convertQuery(c, informations);
params.addParams(childParams, BooleanClause.Occur.MUST);
}
} else if (condition instanceof Or) {
for (Condition c : condition.getChildren()) {
SearchParams childParams = convertQuery(c, informations);
params.addParams(childParams, BooleanClause.Occur.SHOULD);
}
} else
throw new IllegalArgumentException("Invalid condition: " + condition);
return params;
}
use of java.time.Instant in project titan by thinkaurelius.
the class LuceneIndex method addToDocument.
private void addToDocument(String store, String docID, Document doc, List<IndexEntry> content, Map<String, Shape> geofields, KeyInformation.IndexRetriever informations) {
Preconditions.checkNotNull(doc);
for (IndexEntry e : content) {
Preconditions.checkArgument(!e.hasMetaData(), "Lucene index does not support indexing meta data: %s", e);
if (log.isTraceEnabled())
log.trace("Adding field [{}] on document [{}]", e.field, docID);
if (doc.getField(e.field) != null)
doc.removeFields(e.field);
if (e.value instanceof Number) {
Field field;
if (AttributeUtil.isWholeNumber((Number) e.value)) {
field = new LongField(e.field, ((Number) e.value).longValue(), Field.Store.YES);
} else {
//double or float
field = new DoubleField(e.field, ((Number) e.value).doubleValue(), Field.Store.YES);
}
doc.add(field);
} else if (AttributeUtil.isString(e.value)) {
String str = (String) e.value;
Mapping mapping = Mapping.getMapping(store, e.field, informations);
Field field;
switch(mapping) {
case DEFAULT:
case TEXT:
field = new TextField(e.field, str, Field.Store.YES);
break;
case STRING:
field = new StringField(e.field, str, Field.Store.YES);
break;
default:
throw new IllegalArgumentException("Illegal mapping specified: " + mapping);
}
doc.add(field);
} else if (e.value instanceof Geoshape) {
Shape shape = ((Geoshape) e.value).convert2Spatial4j();
geofields.put(e.field, shape);
doc.add(new StoredField(e.field, GEOID + toWkt(shape)));
} else if (e.value instanceof Date) {
doc.add(new LongField(e.field, (((Date) e.value).getTime()), Field.Store.YES));
} else if (e.value instanceof Instant) {
doc.add(new LongField(e.field, (((Instant) e.value).toEpochMilli()), Field.Store.YES));
} else if (e.value instanceof Boolean) {
doc.add(new IntField(e.field, ((Boolean) e.value) ? 1 : 0, Field.Store.YES));
} else if (e.value instanceof UUID) {
//Solr stores UUIDs as strings, we we do the same.
Field field = new StringField(e.field, e.value.toString(), Field.Store.YES);
doc.add(field);
} else {
throw new IllegalArgumentException("Unsupported type: " + e.value);
}
}
for (Map.Entry<String, Shape> geo : geofields.entrySet()) {
if (log.isTraceEnabled())
log.trace("Updating geo-indexes for key {}", geo.getKey());
for (IndexableField f : getSpatialStrategy(geo.getKey()).createIndexableFields(geo.getValue())) doc.add(f);
}
}
Aggregations