introduce indexes
This commit is contained in:
@@ -20,55 +20,39 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.LinkedBlockingDeque;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.lucares.collections.LongList;
|
||||
import org.lucares.pdb.datastore.PdbIndexId;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
public class PdbTestUtil {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(PdbTestUtil.class);
|
||||
|
||||
static final Map<String, Object> POISON = new HashMap<>();
|
||||
|
||||
public static final void send(final String format, final Collection<Map<String, Object>> entries, final int port)
|
||||
throws IOException, InterruptedException {
|
||||
switch (format) {
|
||||
case "csv":
|
||||
sendAsCsv(entries, port);
|
||||
break;
|
||||
case "json":
|
||||
sendAsJson(entries, port);
|
||||
break;
|
||||
default:
|
||||
throw new IllegalStateException("unhandled format: " + format);
|
||||
}
|
||||
}
|
||||
|
||||
@SafeVarargs
|
||||
public static final void sendAsCsv(final int port, final Map<String, Object>... entries)
|
||||
public static final void sendAsCsv(final PdbIndexId indexId, final int port, final Map<String, Object>... entries)
|
||||
throws IOException, InterruptedException {
|
||||
sendAsCsv(Arrays.asList(entries), port);
|
||||
sendAsCsv(indexId, Arrays.asList(entries), port);
|
||||
}
|
||||
|
||||
public static final void sendAsCsv(final Collection<Map<String, Object>> entries, final int port)
|
||||
throws IOException, InterruptedException {
|
||||
public static final void sendAsCsv(final PdbIndexId indexId, final Collection<Map<String, Object>> entries,
|
||||
final int port) throws IOException, InterruptedException {
|
||||
|
||||
final Set<String> keys = entries.stream().map(Map::keySet).flatMap(Set::stream).collect(Collectors.toSet());
|
||||
|
||||
sendAsCsv(keys, entries, port);
|
||||
sendAsCsv(indexId, keys, entries, port);
|
||||
}
|
||||
|
||||
public static final void sendAsCsv(final Collection<String> keys, final Collection<Map<String, Object>> entries,
|
||||
final int port) throws IOException, InterruptedException {
|
||||
public static final void sendAsCsv(final PdbIndexId indexId, final Collection<String> keys,
|
||||
final Collection<Map<String, Object>> entries, final int port) throws IOException, InterruptedException {
|
||||
|
||||
final StringBuilder csv = new StringBuilder();
|
||||
|
||||
csv.append("\u0001" + indexId.getId());
|
||||
csv.append(String.join(",", keys));
|
||||
csv.append("\n");
|
||||
|
||||
@@ -85,48 +69,6 @@ public class PdbTestUtil {
|
||||
send(csv.toString(), port);
|
||||
}
|
||||
|
||||
@SafeVarargs
|
||||
public static final void sendAsJson(final int port, final Map<String, Object>... entries)
|
||||
throws IOException, InterruptedException {
|
||||
|
||||
sendAsJson(Arrays.asList(entries), port);
|
||||
}
|
||||
|
||||
public static final void sendAsJson(final Collection<Map<String, Object>> entries, final int port)
|
||||
throws IOException, InterruptedException {
|
||||
final LinkedBlockingDeque<Map<String, Object>> queue = new LinkedBlockingDeque<>(entries);
|
||||
queue.put(POISON);
|
||||
sendAsJson(queue, port);
|
||||
}
|
||||
|
||||
public static final void sendAsJson(final BlockingQueue<Map<String, Object>> aEntriesSupplier, final int port)
|
||||
throws IOException {
|
||||
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
final SocketChannel channel = connect(port);
|
||||
|
||||
Map<String, Object> entry;
|
||||
while ((entry = aEntriesSupplier.poll()) != POISON) {
|
||||
|
||||
final StringBuilder streamData = new StringBuilder();
|
||||
streamData.append(mapper.writeValueAsString(entry));
|
||||
streamData.append("\n");
|
||||
|
||||
final ByteBuffer src = ByteBuffer.wrap(streamData.toString().getBytes(StandardCharsets.UTF_8));
|
||||
channel.write(src);
|
||||
}
|
||||
|
||||
try {
|
||||
// ugly workaround: the channel was closed too early and not all
|
||||
// data was received
|
||||
TimeUnit.MILLISECONDS.sleep(10);
|
||||
} catch (final InterruptedException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
channel.close();
|
||||
LOGGER.trace("closed sender connection");
|
||||
}
|
||||
|
||||
public static final void send(final String data, final int port) throws IOException {
|
||||
|
||||
final SocketChannel channel = connect(port);
|
||||
|
||||
Reference in New Issue
Block a user