+ public synchronized void save() throws IOException {
+
+ long start = System.nanoTime();
+
+ Collection<CacheEntryBase> caches = allCaches(new CacheCollectionResult()).toCollection();
+ Map<Long,List<CacheEntryBase>> cachesByCluster = new HashMap<>();
+ for(CacheEntryBase entry : caches) {
+ String clazz = entry.classId();
+ if(clazz == null)
+ continue;
+ long cluster = entry.cluster(this);
+ List<CacheEntryBase> queries = cachesByCluster.get(cluster);
+ if(queries == null) {
+ queries = new ArrayList<>();
+ cachesByCluster.put(cluster, queries);
+ }
+ queries.add(entry);
+ }
+
+ File workspace = Platform.getLocation().toFile();
+ File dir = new File(workspace, "queryData");
+ FileUtils.deleteAll(dir);
+
+ dir.mkdir();
+
+ for(Long cluster : cachesByCluster.keySet()) {
+
+ List<CacheEntryBase> queries = cachesByCluster.get(cluster);
+ QuerySerializer serializer = new QuerySerializer(this);
+ int count = 0;
+ int pos = serializer.writeUnknownSize();
+ for(CacheEntryBase entry : queries) {
+ String clazz = entry.classId();
+ if(clazz == null)
+ continue;
+ try {
+ entry.serialize(serializer);
+ count++;
+ } catch (IllegalStateException e) {
+ System.err.println(e.getMessage());
+ }
+ }
+ serializer.setUnknownSize(pos, count);
+
+ System.err.println(serializer.bytes().length + " bytes for cluster " + cluster);
+ FileUtils.writeFile(new File(dir, "" + cluster + ".queryData"), serializer.bytes());
+
+ }
+
+ long end = System.nanoTime();
+
+ System.err.println("saved queries in " + 1e-6*(end-start) + "ms.");
+
+ }
+
+ public void restore() throws IOException {
+
+ long start = System.nanoTime();
+
+ File workspace = Platform.getLocation().toFile();
+ File dir = new File(workspace, "queryData");
+ dir.mkdir();
+
+ for(File f : FileUtils.listFilesByExtension(dir, "queryData")) {
+ byte[] bytes = FileUtils.readFile(f);
+ QueryDeserializer qd = new QueryDeserializer(this, bytes);
+ qd.readHeaders();
+ qd.readQueries();
+ }
+
+ long end = System.nanoTime();
+
+ System.err.println("restored queries in " + 1e-6*(end-start) + "ms.");
+
+ }
+