Decides if the input should be cached before being written to the final database. @param parser parser reference @return result of check
(final Parser parser)
| 140 | * @return result of check |
| 141 | */ |
| 142 | private boolean cache(final Parser parser) { |
| 143 | // main memory mode: never write to disk |
| 144 | if(options.get(MainOptions.MAINMEM)) return false; |
| 145 | // explicit caching |
| 146 | if(options.get(MainOptions.ADDCACHE)) return true; |
| 147 | |
| 148 | // create disk instances for large documents |
| 149 | // (does not work for input streams and directories) |
| 150 | final IO source = parser.source(); |
| 151 | long fl = source.length(); |
| 152 | if(source instanceof final IOFile src && src.isDir()) { |
| 153 | for(final String path : src.descendants()) fl += new IOFile(src, path).length(); |
| 154 | } |
| 155 | |
| 156 | // check free memory |
| 157 | final Runtime rt = Runtime.getRuntime(); |
| 158 | final long max = rt.maxMemory(); |
| 159 | if(fl < (max - rt.freeMemory()) / 2) return false; |
| 160 | // if caching may be necessary, run garbage collection and try again |
| 161 | Performance.gc(2); |
| 162 | return fl > (max - rt.freeMemory()) / 2; |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public void build(final CmdBuilder cb) { |