20 package net.sf.gridarta.model.index;
23 import java.io.FileInputStream;
24 import java.io.FileNotFoundException;
25 import java.io.FileOutputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.ObjectInputStream;
29 import java.io.ObjectOutputStream;
30 import java.io.OutputStream;
31 import java.nio.charset.StandardCharsets;
32 import java.util.Arrays;
33 import java.util.concurrent.Semaphore;
47 import org.apache.log4j.Category;
48 import org.apache.log4j.Logger;
49 import org.jetbrains.annotations.NotNull;
50 import org.jetbrains.annotations.Nullable;
162 public void mapCreated(@NotNull
final MapControl<G, A, R> mapControl,
final boolean interactive) {
163 if (!mapControl.isPickmap()) {
175 if (!mapControl.isPickmap()) {
190 final MapFile mapFile = mapControl.getMapModel().getMapFile();
191 if (mapFile !=
null) {
206 public void mapsDirectoryChanged(@NotNull
final File
mapsDirectory) {
222 public void valueAdded(@NotNull
final MapFile value) {
227 public void valueRemoved(@NotNull
final MapFile value) {
232 public void nameChanged() {
237 public void pendingChanged() {
242 public void indexingFinished() {
253 while (!Thread.currentThread().isInterrupted()) {
259 }
catch (
final InterruptedException ignored) {
260 Thread.currentThread().interrupt();
305 public void stop() throws InterruptedException {
313 if (!mapControl.isPickmap()) {
343 final File tmpMapsDirectory;
354 assert tmpMapsDirectory !=
null;
385 try (OutputStream outputStream =
new FileOutputStream(cacheFile)) {
386 try (ObjectOutputStream objectOutputStream =
new ObjectOutputStream(outputStream)) {
390 if (
LOG.isInfoEnabled()) {
393 }
catch (
final IOException ex) {
394 LOG.warn(cacheFile +
": cannot save cache file: " + ex.getMessage());
406 try (InputStream inputStream =
new FileInputStream(cacheFile)) {
407 try (ObjectInputStream objectInputStream =
new ObjectInputStream(inputStream)) {
411 if (
LOG.isInfoEnabled()) {
414 }
catch (
final FileNotFoundException ex) {
415 if (
LOG.isDebugEnabled()) {
416 LOG.debug(cacheFile +
": cache file does not exist: " + ex.getMessage());
419 }
catch (
final IOException ex) {
420 LOG.warn(cacheFile +
": cannot load cache file: " + ex.getMessage());
431 final File[]
files = dir.getFile().listFiles();
436 for (
final File file :
files) {
438 if (file.isFile() && !file.getName().endsWith(
"~")) {
440 }
else if (file.isDirectory() && !file.getName().equalsIgnoreCase(
".svn") && !file.getName().equalsIgnoreCase(
".dedit")) {
452 if (mapFile ==
null) {
458 final long timestamp = mapFile.
getFile().lastModified();
462 }
catch (
final IOException ex) {
463 if (
LOG.isInfoEnabled()) {
464 LOG.info(mapFile +
": load failed:" + ex.getMessage());
470 if (
LOG.isDebugEnabled()) {
471 LOG.debug(mapFile +
": indexing as '" + mapName +
"'");
486 final byte[] key =
new byte[16];
488 final byte[] data =
mapsDirectory.getAbsoluteFile().toString().getBytes(StandardCharsets.UTF_8);
489 final byte[] hash =
new byte[8];
490 final byte[] in =
new byte[8];
491 final byte[] out =
new byte[8];
493 for (i = 0; i + 8 < data.length; i++) {
494 System.arraycopy(data, i, in, 0, 8);
496 for (
int j = 0; j < 8; j++) {
500 final int len = data.length % 8;
501 System.arraycopy(data, i, in, 0, len);
503 Arrays.fill(in, len + 1, 8, (
byte) 0);
505 for (
int j = 0; j < 8; j++) {
509 final StringBuilder sb =
new StringBuilder(
"index/maps/");
510 for (
int j = 0; j < 8; j++) {
511 sb.append(String.format(
"%02x", hash[j] & 0xff));
515 final File dir = file.getParentFile();
516 if (dir !=
null && !dir.exists() && !dir.mkdirs()) {
517 LOG.warn(
"cannot create directory: " + dir);
528 if (this.state ==
state) {
542 if (
LOG.isDebugEnabled()) {