create web application

This commit is contained in:
2016-12-21 17:48:36 +01:00
parent 35054b00b8
commit d1e39513f3
65 changed files with 805 additions and 43 deletions

View File

@@ -1,9 +1,7 @@
dependencies {
//compile 'com.fasterxml.jackson.core:jackson-databind:2.8.5'
compile 'org.lucares:ludb:1.0.20161002111352'
//compile 'commons-io:commons-io:2.5'
//compile 'joda-time:joda-time:2.9.6'
compile project(':pdb-api')
compile 'org.lucares:ludb:1.0.20161217184921'
compile 'com.fasterxml.jackson.core:jackson-databind:2.8.5'
compile 'org.mapdb:mapdb:3.0.2'
}

View File

@@ -1,101 +0,0 @@
package org.lucares.performance.db;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Comparator;
public class Entry {
public static final Comparator<Entry> BY_DATE = new EntryByDateComparator();
/**
* A special {@link Entry} that can be used as poison object for
* {@link BlockingQueueIterator}.
*/
public static final Entry POISON = new Entry(0, -1);
public static final long MAX_VALUE = 0xFF_FF_FF_FFL;
private final long epochMilli;
private final long value;
private final Tags tags;
public Entry(final OffsetDateTime date, final long value, final Tags tags) {
this.tags = tags;
this.epochMilli = date.toInstant().toEpochMilli();
this.value = value;
}
Entry(final long epochMilli, final long value, final Tags tags) {
if (value < 0 || value > MAX_VALUE) {
throw new IllegalArgumentException("value must be between 0 and " + MAX_VALUE + ", but was " + value);
}
this.epochMilli = epochMilli;
this.value = value;
this.tags = tags;
}
private Entry(final long epochMilli, final long value) {
this.epochMilli = epochMilli;
this.value = value;
this.tags = null;
}
public OffsetDateTime getDate() {
final Instant instant = Instant.ofEpochMilli(epochMilli);
return OffsetDateTime.ofInstant(instant, ZoneOffset.UTC);
}
public long getValue() {
return value;
}
long getEpochMilli() {
return epochMilli;
}
public Tags getTags() {
return tags;
}
@Override
public String toString() {
final OffsetDateTime date = getDate();
return date.format(DateTimeFormatter.ISO_ZONED_DATE_TIME) + " = " + value + " (" + tags + ")";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (int) (epochMilli ^ (epochMilli >>> 32));
result = prime * result + ((tags == null) ? 0 : tags.hashCode());
result = prime * result + (int) (value ^ (value >>> 32));
return result;
}
@Override
public boolean equals(final Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final Entry other = (Entry) obj;
if (epochMilli != other.epochMilli)
return false;
if (tags == null) {
if (other.tags != null)
return false;
} else if (!tags.equals(other.tags))
return false;
if (value != other.value)
return false;
return true;
}
}

View File

@@ -2,6 +2,8 @@ package org.lucares.performance.db;
import java.util.List;
import org.lucares.pdb.api.Tags;
class Group {
private final Tags tags;

View File

@@ -1,32 +0,0 @@
package org.lucares.performance.db;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class GroupResult {
private final Tags groupedBy;
private final Stream<Entry> entries;
public GroupResult(final Stream<Entry> entries, final Tags groupedBy) {
this.entries = entries;
this.groupedBy = groupedBy;
}
/**
* @return {@link Stream} unbound, unordered and non-parallel
*/
public Stream<Entry> asStream() {
return entries;
}
public List<Entry> asList() {
return entries.collect(Collectors.toList());
}
public Tags getGroupedBy() {
return groupedBy;
}
}

View File

@@ -6,6 +6,8 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.lucares.pdb.api.Tags;
public class Grouping {
public static final String NO_GROUPING = null;

View File

@@ -2,6 +2,8 @@ package org.lucares.performance.db;
import java.io.File;
import org.lucares.pdb.api.Tags;
class PdbFile {
private final Tags tags;

View File

@@ -10,6 +10,9 @@ import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.lucares.pdb.api.Entry;
import org.lucares.pdb.api.Tags;
public class PdbFileIterator implements Iterator<Entry>, AutoCloseable {
private final static Logger LOGGER = Logger.getLogger(PdbFileIterator.class.getCanonicalName());

View File

@@ -8,6 +8,9 @@ import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.Optional;
import org.lucares.pdb.api.Entry;
import org.lucares.pdb.api.Tags;
class PdbReader implements AutoCloseable {
private static final int BYTES_PER_VALUE = 4;

View File

@@ -6,6 +6,8 @@ import java.io.IOException;
import java.io.OutputStream;
import java.util.logging.Logger;
import org.lucares.pdb.api.Entry;
class PdbWriter implements AutoCloseable {
private final static Logger LOGGER = Logger.getLogger(PdbWriter.class.getCanonicalName());

View File

@@ -9,6 +9,8 @@ import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.lucares.pdb.api.Tags;
public class PdbWriterManager implements AutoCloseable {
private final static Logger LOGGER = Logger.getLogger(PdbWriterManager.class.getCanonicalName());

View File

@@ -15,6 +15,10 @@ import java.util.logging.Logger;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import org.lucares.pdb.api.Entry;
import org.lucares.pdb.api.GroupResult;
import org.lucares.pdb.api.Result;
import org.lucares.pdb.api.Tags;
import org.lucares.performance.db.PdbWriterManager.PdbWriterSupplier;
public class PerformanceDb implements AutoCloseable {
@@ -84,6 +88,7 @@ public class PerformanceDb implements AutoCloseable {
final Entry entry = entryOptional.get();
final Tags tags = entry.getTags();
TagsUtils.ensureNoInternalFields(tags);
final OffsetDateTime date = entry.getDate();
final PdbWriter writer = manager.get(tags, date);

View File

@@ -1,5 +1,7 @@
package org.lucares.performance.db;
import org.lucares.pdb.api.Tags;
final class Query {
static String createQuery(final Tags tags) {
final StringBuilder result = new StringBuilder();

View File

@@ -1,30 +0,0 @@
package org.lucares.performance.db;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
public class Result {
private final List<GroupResult> groupResults;
public Result(final GroupResult... groupResults) {
this(Arrays.asList(groupResults));
}
public Result(final Collection<GroupResult> groupResults) {
this.groupResults = new ArrayList<>(groupResults);
}
public GroupResult singleGroup() {
if (groupResults.size() != 1) {
throw new IllegalStateException("the result does not contain exactly one group");
}
return groupResults.get(0);
}
public List<GroupResult> getGroups() {
return new ArrayList<>(groupResults);
}
}

View File

@@ -1,57 +0,0 @@
package org.lucares.performance.db;
public class Tag {
private final String key;
private final String value;
public Tag(final String key, final String value) {
this.key = key;
this.value = value;
}
public String getKey() {
return key;
}
public String getValue() {
return value;
}
@Override
public String toString() {
return key + "=" + value;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((key == null) ? 0 : key.hashCode());
result = prime * result + ((value == null) ? 0 : value.hashCode());
return result;
}
@Override
public boolean equals(final Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final Tag other = (Tag) obj;
if (key == null) {
if (other.key != null)
return false;
} else if (!key.equals(other.key))
return false;
if (value == null) {
if (other.value != null)
return false;
} else if (!value.equals(other.value))
return false;
return true;
}
}

View File

@@ -1,145 +0,0 @@
package org.lucares.performance.db;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.function.BiConsumer;
public class Tags {
static final Tags EMPTY = new Tags();
private final Map<String, Tag> tags;
private Tags() {
super();
tags = Collections.emptyMap();
}
private Tags(final Map<String, Tag> tags) {
this.tags = tags;
ensureNoInternalFields();
}
private void ensureNoInternalFields() {
tags.keySet().forEach(key -> {
if (Fields.isInternalField(key)) {
throw new IllegalArgumentException(key + " is an internal field. Choose another prefix.");
}
});
}
public static Tags create() {
return EMPTY;
}
public static Tags create(final String key1, final String value1, final String key2, final String value2) {
final Map<String, Tag> tags = new HashMap<>(2);
tags.put(key1, new Tag(key1, value1));
tags.put(key2, new Tag(key2, value2));
return new Tags(tags);
}
public static Tags create(final String key, final String value) {
final Map<String, Tag> tags = new HashMap<>(1);
tags.put(key, new Tag(key, value));
return new Tags(tags);
}
public Tags copyAdd(final String key, final String value) {
Objects.requireNonNull(key, "key must not be null");
Objects.requireNonNull(value, "value must not be null");
final Map<String, Tag> newTags = new HashMap<>(tags);
newTags.put(key, new Tag(key, value));
return new Tags(newTags);
}
public Tags copyAddIfNotNull(final String key, final String value) {
final Tags result;
if (value != null) {
result = copyAdd(key, value);
} else {
result = this;
}
return result;
}
public String getValue(final String key) {
final Tag tag = tags.get(key);
final String value = tag != null ? tag.getValue() : null;
return value;
}
public Set<String> getKeys() {
return new TreeSet<>(tags.keySet());
}
public void forEach(final BiConsumer<String, String> keyValueConsumer) {
for (final Map.Entry<String, Tag> e : tags.entrySet()) {
keyValueConsumer.accept(e.getKey(), e.getValue().getValue());
}
}
@Override
public String toString() {
return String.valueOf(tags);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((tags == null) ? 0 : tags.hashCode());
return result;
}
@Override
public boolean equals(final Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final Tags other = (Tags) obj;
if (tags == null) {
if (other.tags != null)
return false;
} else if (!tags.equals(other.tags))
return false;
return true;
}
public String abbreviatedRepresentation() {
final StringBuilder result = new StringBuilder();
final int maxLength = 200;
final SortedSet<String> keys = new TreeSet<>(tags.keySet());
final int cutAt = maxLength / (keys.size() * 2 + 2);
for (final String key : keys) {
final String value = tags.get(key).getValue();
result.append(substr(key, cutAt));
result.append("-");
result.append(substr(value, cutAt));
result.append("_");
}
return substr(result.toString(), maxLength);
}
private static String substr(final String s, final int maxLength) {
return s.substring(0, Math.min(maxLength, s.length()));
}
}

View File

@@ -17,8 +17,7 @@ import org.lucares.ludb.Field;
import org.lucares.ludb.FieldExistsException;
import org.lucares.ludb.FieldType;
import org.lucares.ludb.H2DB;
import liquibase.exception.LiquibaseException;
import org.lucares.pdb.api.Tags;
public class TagsToFile implements AutoCloseable, CollectionUtils {
@@ -28,16 +27,12 @@ public class TagsToFile implements AutoCloseable, CollectionUtils {
public TagsToFile(final Path dataDirectory) {
super();
this.dataDirectory = dataDirectory;
db = new H2DB(new File(dataDirectory.toFile(), "lu.db"));
try {
db = new H2DB(new File(dataDirectory.toFile(), "lu.db"));
try {
db.createField(Fields.DATE_OFFSET, FieldType.STRING);
} catch (final FieldExistsException e) {
// TODO @ahr ludb needs a hasField method, or a
// createIfNotExists
}
} catch (final LiquibaseException e) {
throw new RuntimeException(e);
db.createField(Fields.DATE_OFFSET, FieldType.STRING);
} catch (final FieldExistsException e) {
// TODO @ahr ludb needs a hasField method, or a
// createIfNotExists
}
}

View File

@@ -0,0 +1,13 @@
package org.lucares.performance.db;
import org.lucares.pdb.api.Tags;
class TagsUtils {
static void ensureNoInternalFields(final Tags tags) {
tags.getKeys().forEach(key -> {
if (Fields.isInternalField(key)) {
throw new IllegalArgumentException(key + " is an internal field. Choose another prefix.");
}
});
}
}

View File

@@ -2,7 +2,10 @@ package org.lucares.performance.db;
import java.util.Comparator;
import org.lucares.pdb.api.Entry;
public class EntryByDateComparator implements Comparator<Entry> {
public static final Comparator<Entry> INSTANCE = new EntryByDateComparator();
@Override
public int compare(final Entry o1, final Entry o2) {

View File

@@ -4,6 +4,7 @@ import java.io.File;
import java.io.Serializable;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
@@ -159,6 +160,7 @@ public class ObjectMapperTest {
private final ConcurrentMap<Long, TagEntry> mapObjects;
@SuppressWarnings("unchecked")
public TagDBMapDB() {
db = DBMaker.fileDB("/tmp/file4.db").fileMmapEnable().make();
@@ -227,7 +229,7 @@ public class ObjectMapperTest {
}
{
final long start = System.nanoTime();
final TagDBJson actualTagDB = om.readValue(file, TagDBJson.class);
om.readValue(file, TagDBJson.class);
System.out.println("json read: " + (System.nanoTime() - start) / 1_000_000.0 + "ms");
}
@@ -246,6 +248,7 @@ public class ObjectMapperTest {
{
final long start = System.nanoTime();
final TagDBH2 tagDB = new TagDBH2();
tagDB.close();
System.out.println("h2 read: " + (System.nanoTime() - start) / 1_000_000.0 + "ms");
}
@@ -301,19 +304,22 @@ public class ObjectMapperTest {
tagDB.addTagEntry(e);
}
}
}
}
}
public static void main(final String[] args) throws Exception {
// System.out.println(System.getProperty("java.class.path"));
System.out.println(System.getProperty("java.class.path"));
TimeUnit.SECONDS.sleep(5);
System.out.println("start: " + OffsetDateTime.now());
try (H2DB h2 = new H2DB(new File("/tmp/h2" + UUID.randomUUID().toString()))) {
System.out.println("done");
}
System.out.println("stop: " + OffsetDateTime.now());
TimeUnit.SECONDS.sleep(20);
TimeUnit.SECONDS.sleep(10);
System.out.println("closing");
}

View File

@@ -6,6 +6,8 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.time.OffsetDateTime;
import org.lucares.pdb.api.Entry;
import org.lucares.pdb.api.Tags;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;

View File

@@ -6,6 +6,7 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.time.OffsetDateTime;
import org.lucares.pdb.api.Tags;
import org.lucares.performance.db.PdbWriterManager.PdbWriterSupplier;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;

View File

@@ -12,6 +12,10 @@ import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import org.apache.commons.collections4.CollectionUtils;
import org.lucares.pdb.api.Entry;
import org.lucares.pdb.api.GroupResult;
import org.lucares.pdb.api.Result;
import org.lucares.pdb.api.Tags;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
@@ -147,9 +151,10 @@ public class PerformanceDbTest {
final List<Entry> actualEntriesAll = db.get(Query.createQuery(tagsCommon)).singleGroup().asList();
final List<Entry> expectedAll = CollectionUtils.collate(entriesOne,
CollectionUtils.collate(entriesTwo, entriesThree, Entry.BY_DATE), Entry.BY_DATE);
CollectionUtils.collate(entriesTwo, entriesThree, EntryByDateComparator.INSTANCE),
EntryByDateComparator.INSTANCE);
actualEntriesAll.sort(Entry.BY_DATE);
actualEntriesAll.sort(EntryByDateComparator.INSTANCE);
Assert.assertEquals(actualEntriesAll, expectedAll);
}

View File

@@ -6,6 +6,7 @@ import java.nio.file.Path;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import org.lucares.pdb.api.Tags;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;