0

I'm creating parallel Maps using MapDB - would like to know whether it's safe and can it be done in better way. also assume that each sales table query can result into million records.

public static void main(String[] args){
        DB db = DBMaker.newFileDB("mapdbFile").transactionDisable().mmapFileEnablePartial().make();
        getEmployees().parallelStream().forEach(e -> createRecords(e, db));
    }

private static List<Employee> getEmployees(){
    /* return employees list */
}

private static void createRecords(Employee employee, DB db) {
    JdbcTemplate template = /* spring JdbcTemplate creation */
    String query = "select name from sales where emp_id = "+employee.getId();        
    final BTreeMap<String, Long> map = db.createTreeMap("Employee" + employee.getId())
            .nodeSize(32).comparator(String.CASE_INSENSITIVE_ORDER).make();
    template.query(query, rs -> {
        put(map, rs.getString("product_name"), rs.getLong("sales"));
    });
}
Premraj
  • 7,802
  • 8
  • 45
  • 66

1 Answers1

0

DB object is synchronized and is thread safe. So yes, parallel map creation is safe.

Jan Kotek
  • 1,084
  • 7
  • 4