I'm trying to override Google Guava's CacheLoader loadAll() method. I'm wanting to do a bulk lookup and retrieve all the documents in the Employee collection that were not specifically requested like it states in CachesExplained:
Note that you can write a CacheLoader.loadAll implementation that loads values for keys that were not specifically requested. For example, if computing the value of any key from some group gives you the value for all keys in the group, loadAll might load the rest of the group at the same time.
I've scoured the internet and the only post I've found that was remotely related to my issue was Google Guava's CacheLoader loadAll() method Implementation Issues. However, I still cannot bulk retrieve all the documents from my collection. Whenever I run my code, the only values retrieved from the database are the ones that were specifically passed in via a List.
Let's say I have a Mongo collection name Employees with 3 employee documents in it:
{"_id" : "123", "John Doe", "CEO"}
{"_id" : "456", "Jane Doe", "President"}
{"_id" : "789", "Jimmy Doe", "CFO"}
Employee class:
public class Employee extends MongoDbModel {
@Id
private final String employeeNumber;
private final String name;
private final String type;
public Employee(String employeeNumber, String name, String type){
this.employeeNumber = employeeNumber;
this.name = name;
this.type = type;
}
public String getEmployeeNumber() {
return employeeNumber;
}
public String getName() {
return name;
}
public String getType() {
return type;
}
public static LoadingCache<String, Employee> employeeCacheMap;
static {
employeeCacheMap = CacheBuilder.newBuilder()
.concurrencyLevel(4)
.weakKeys()
.maximumSize(10000)
.expireAfterWrite(5, TimeUnit.MINUTES)
.build(
new CacheLoader<String, Employee>() {
@Override
public Employee load(String key) throws ExecutionException{
return getOneFromDatabase(key);
}
@Override
public Map<String, Employee> loadAll(Iterable<? extends String> key){
return getAllEmployeesFromDatabase();
}
}
);
}
private static Employee getOneFromDatabase(String employeeNumber){
return MongoDbStore.createQuery(Employee.class).filter("_id", employeeNumber).first();
}
private static Map<String, Employee> getAllEmployeesFromDatabase(){
final List<Employee> list = MongoDbStore.createQuery(Employee.class).asList();
final Map<String, Employee> map = new HashMap<String,Employee>(list.size());
for(final Employee employee : list){
map.put(employee.getName(), employee);
}
return map;
}
public static LoadingCache<String, Employee> getLoadingCache(){
return employeeCacheMap;
}
}
Whenever I run this method:
public void getAllEmployees(){
List<String> myList = new ArrayList<>();
myList.add("123");
myList.add("456");
Map<String, Employee> employeeMap = null;
try {
employeeMap = Employee.getLoadingCache().getAll(myList);
} catch (ExecutionException e) {
e.printStackTrace();
}
for(Employee employee : employeeMap.values()){
System.out.println(employee.getName());
}
}
It only prints out:
"John Doe"
"Jane Doe"
instead of all three names as described in the above quote from CachesExplained.
Am I misunderstanding the above quote and the CacheLoader only retrieves the values that are passed into the loadAll() method instead of retrieving all of the values from the collection? Any help will be much appreciated