Gitlab Community Edition Instance

Commit 993eb8f5 authored by mhellka's avatar mhellka
Browse files

Make ArchiveListener aware of which file a property was changed on.

parent 14769745
......@@ -130,7 +130,7 @@ public interface CDStarFile extends CDStarAnnotateable {
/**
* Return a {@link WritableByteChannel} opened for writing (appending) data to
* the file.
*
*
* The returned {@link WritableByteChannel} will be blocking and always write
* all bytes before returning, as defined by {@link WritableByteChannel#write(ByteBuffer)}.
*
......@@ -219,6 +219,10 @@ public interface CDStarFile extends CDStarAnnotateable {
void remove();
default boolean isRemoved() {
return getName() == null;
}
CDStarArchive getArchive();
boolean isAvailable();
......
......@@ -24,7 +24,7 @@ public interface ArchiveListener {
}
default void propertyChanged(CDStarAttribute property, List<String> originalValues) {
default void propertyChanged(CDStarAttribute property, CDStarFile file, List<String> originalValues) {
}
default void fileCreated(CDStarFile file) {
......
......@@ -6,6 +6,7 @@ import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;
......@@ -332,6 +333,20 @@ class ArchiveImpl implements CDStarArchive {
return filesCache;
}
Optional<FileImpl> getInternalFileById(String fileId) {
for (FileImpl file : getInternalFileList())
if (fileId.equals(file.getID()))
return Optional.of(file);
return Optional.empty();
}
Optional<FileImpl> getInternalFileByName(String fileName) {
for (FileImpl file : getInternalFileList())
if (fileName.equals(file.getName()))
return Optional.of(file);
return Optional.empty();
}
synchronized AttributeCache getMeta() {
if (metaCache == null) {
checkPermission(ArchivePermission.READ_META);
......
......@@ -6,6 +6,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.nio.channels.Channels;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
......@@ -16,6 +17,7 @@ import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import de.gwdg.cdstar.SharedObjectMapper;
import de.gwdg.cdstar.Utils;
import de.gwdg.cdstar.pool.BackendError;
import de.gwdg.cdstar.pool.Resource;
import de.gwdg.cdstar.runtime.client.auth.ArchivePermission;
......@@ -29,11 +31,18 @@ class AttributeCache {
AttributeJsonFormat<AttributeImpl> jsonFormat = new AttributeJsonFormat<AttributeImpl>() {
@Override
public AttributeImpl makeAttr(String fileId, String attrName, List<String> values) {
return new AttributeImpl(AttributeCache.this, attrName, values);
FileImpl file = null;
if (Utils.notNullOrEmpty(fileId)) {
file = a.getInternalFileById(fileId).orElseThrow(() -> new BackendError.DamagedDataError(
"Unable to load metadata table. Reference to unknown file id: " + fileId));
}
return new AttributeImpl(AttributeCache.this, file, attrName, values);
}
@Override
public List<String> getValues(AttributeImpl attr) {
if (attr.getFile() != null && attr.getFile().isRemoved())
return Collections.emptyList();
return attr.values();
}
};
......@@ -92,9 +101,8 @@ class AttributeCache {
throw new IllegalArgumentException("Not a valid attribute name: " + name);
final String key = file == null ? "" : file.getID();
return parse()
.computeIfAbsent(key, f -> new HashMap<>())
.computeIfAbsent(name, propName -> new AttributeImpl(this, name, new ArrayList<>()));
return parse().computeIfAbsent(key, f -> new HashMap<>()).computeIfAbsent(name,
propName -> new AttributeImpl(this, file, name, new ArrayList<>()));
}
public synchronized Set<CDStarAttribute> getAttributes(FileImpl file) {
......@@ -115,7 +123,7 @@ class AttributeCache {
modified = true;
a.markContentModified();
}
a.forEachListener(l -> l.propertyChanged(attr, attr.getOrigValues()));
a.forEachListener(l -> l.propertyChanged(attr, attr.getFile(), attr.getOrigValues()));
}
}
......@@ -12,9 +12,11 @@ class AttributeImpl implements CDStarAttribute {
private final List<String> origValues;
private final String name;
private final AttributeCache parent;
private final FileImpl file;
public AttributeImpl(AttributeCache parent, String name, List<String> values) {
public AttributeImpl(AttributeCache parent, FileImpl file, String name, List<String> values) {
this.parent = parent;
this.file = file;
this.name = name;
origValues = values;
}
......@@ -23,6 +25,10 @@ class AttributeImpl implements CDStarAttribute {
return origValues;
}
CDStarFile getFile() {
return file;
}
@Override
public List<String> values() {
return Collections.unmodifiableList(newValues != null ? newValues : origValues);
......@@ -68,4 +74,5 @@ class AttributeImpl implements CDStarAttribute {
return name;
}
}
......@@ -157,8 +157,8 @@ public class ListenerTest {
Mockito.verify(listener).fileCreated(fh);
Mockito.verify(listener).fileSizeChanged(fh, 11);
Mockito.verify(listener).propertyChanged(arg, Collections.emptyList());
Mockito.verify(listener).propertyChanged(arg2, Collections.emptyList());
Mockito.verify(listener).propertyChanged(arg, null, Collections.emptyList());
Mockito.verify(listener).propertyChanged(arg2, fh, Collections.emptyList());
Mockito.verifyNoMoreInteractions(listener);
final CDStarSession ses2 = getSession();
......@@ -176,8 +176,8 @@ public class ListenerTest {
Mockito.verify(listener).fileNameChanged(fh, "test.txt");
Mockito.verify(listener).fileSizeChanged(fh, 1);
Mockito.verify(listener).propertyChanged(arg, Arrays.asList("myArchive"));
Mockito.verify(listener).propertyChanged(arg2, Arrays.asList("myFile"));
Mockito.verify(listener).propertyChanged(arg, null, Arrays.asList("myArchive"));
Mockito.verify(listener).propertyChanged(arg2, fh, Arrays.asList("myFile"));
Mockito.verifyNoMoreInteractions(listener);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment