
io.druid.data.input.MapPopulator Maven / Gradle / Ivy
Show all versions of druid-lookups-cached-global Show documentation
/*
* Licensed to Metamarkets Group Inc. (Metamarkets) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Metamarkets licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package io.druid.data.input;
import com.google.common.base.Charsets;
import com.google.common.io.ByteSource;
import com.google.common.io.LineProcessor;
import io.druid.java.util.common.ISE;
import io.druid.java.util.common.parsers.Parser;
import java.io.IOException;
import java.util.Map;
/**
* Simple class that takes a `ByteSource` and uses a `Parser` to populate a `Map`
* The `ByteSource` must be UTF-8 encoded
*
* If this is handy for other use cases pleaes move this class into a common module
*/
public class MapPopulator
{
private final Parser parser;
public MapPopulator(
Parser parser
)
{
this.parser = parser;
}
public static class PopulateResult
{
private final int lines;
private final int entries;
public PopulateResult(int lines, int entries)
{
this.lines = lines;
this.entries = entries;
}
public int getLines()
{
return lines;
}
public int getEntries()
{
return entries;
}
}
/**
* Read through the `source` line by line and populate `map` with the data returned from the `parser`
*
* @param source The ByteSource to read lines from
* @param map The map to populate
*
* @return number of lines read and entries parsed
*
* @throws IOException
*/
public PopulateResult populate(final ByteSource source, final Map map) throws IOException
{
return source.asCharSource(Charsets.UTF_8).readLines(
new LineProcessor()
{
private int lines = 0;
private int entries = 0;
@Override
public boolean processLine(String line) throws IOException
{
if (lines == Integer.MAX_VALUE) {
throw new ISE("Cannot read more than %,d lines", Integer.MAX_VALUE);
}
final Map kvMap = parser.parseToMap(line);
map.putAll(kvMap);
lines++;
entries += kvMap.size();
return true;
}
@Override
public PopulateResult getResult()
{
return new PopulateResult(lines, entries);
}
}
);
}
}