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() {
256 while (!Thread.currentThread().isInterrupted()) {
262 }
catch (
final InterruptedException ignored) {
263 Thread.currentThread().interrupt();
310 public void stop() throws InterruptedException {
318 if (!mapControl.isPickmap()) {
348 final File tmpMapsDirectory;
359 assert tmpMapsDirectory !=
null;
390 try (OutputStream outputStream =
new FileOutputStream(cacheFile)) {
391 try (ObjectOutputStream objectOutputStream =
new ObjectOutputStream(outputStream)) {
395 if (
LOG.isInfoEnabled()) {
398 }
catch (
final IOException ex) {
399 LOG.warn(cacheFile +
": cannot save cache file: " + ex.getMessage());
411 try (InputStream inputStream =
new FileInputStream(cacheFile)) {
412 try (ObjectInputStream objectInputStream =
new ObjectInputStream(inputStream)) {
416 if (
LOG.isInfoEnabled()) {
419 }
catch (
final FileNotFoundException ex) {
420 if (
LOG.isDebugEnabled()) {
421 LOG.debug(cacheFile +
": cache file does not exist: " + ex.getMessage());
424 }
catch (
final IOException ex) {
425 LOG.warn(cacheFile +
": cannot load cache file: " + ex.getMessage());
436 final File[] files = dir.getFile().listFiles();
441 for (
final File file : files) {
443 if (file.isFile() && !file.getName().endsWith(
"~")) {
445 }
else if (file.isDirectory() && !file.getName().equalsIgnoreCase(
".svn") && !file.getName().equalsIgnoreCase(
".dedit")) {
457 if (mapFile ==
null) {
463 final long timestamp = mapFile.
getFile().lastModified();
467 }
catch (
final IOException ex) {
468 if (
LOG.isInfoEnabled()) {
469 LOG.info(mapFile +
": load failed:" + ex.getMessage());
475 if (
LOG.isDebugEnabled()) {
476 LOG.debug(mapFile +
": indexing as '" + mapName +
"'");
491 final byte[] key =
new byte[16];
493 final byte[] data =
mapsDirectory.getAbsoluteFile().toString().getBytes(StandardCharsets.UTF_8);
494 final byte[] hash =
new byte[8];
495 final byte[] in =
new byte[8];
496 final byte[] out =
new byte[8];
498 for (i = 0; i + 8 < data.length; i++) {
499 System.arraycopy(data, i, in, 0, 8);
501 for (
int j = 0; j < 8; j++) {
505 final int len = data.length % 8;
506 System.arraycopy(data, i, in, 0, len);
508 Arrays.fill(in, len + 1, 8, (
byte) 0);
510 for (
int j = 0; j < 8; j++) {
514 final StringBuilder sb =
new StringBuilder(
"index/maps/");
515 for (
int j = 0; j < 8; j++) {
516 sb.append(String.format(
"%02x", hash[j] & 0xff));
520 final File dir = file.getParentFile();
521 if (dir !=
null && !dir.exists() && !dir.mkdirs()) {
522 LOG.warn(
"cannot create directory: " + dir);
533 if (this.state ==
state) {
547 if (
LOG.isDebugEnabled()) {