Skip to content

Commit

Permalink
Add compaction, file events.
Browse files Browse the repository at this point in the history
  • Loading branch information
io7m committed Aug 26, 2024
1 parent 8d7ab1f commit 993cfbc
Show file tree
Hide file tree
Showing 12 changed files with 348 additions and 39 deletions.
4 changes: 4 additions & 0 deletions com.io7m.laurel.filemodel/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>com.io7m.seltzer</groupId>
<artifactId>com.io7m.seltzer.api</artifactId>
</dependency>
<dependency>
<groupId>com.io7m.jattribute</groupId>
<artifactId>com.io7m.jattribute.core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright © 2024 Mark Raynsford <[email protected]> https://www.io7m.com
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/


package com.io7m.laurel.filemodel;

import java.util.Objects;
import java.util.OptionalDouble;

/**
* An event raised by the model.
*
* @param message The event message
* @param progress The progress value, if any
*/

public record LFileModelEvent(
String message,
OptionalDouble progress)
{
/**
* An event raised by the model.
*
* @param message The event message
* @param progress The progress value, if any
*/

public LFileModelEvent
{
Objects.requireNonNull(message, "message");
Objects.requireNonNull(progress, "progress");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Flow;

/**
* The interface to a file model.
Expand All @@ -35,6 +36,12 @@
public interface LFileModelType
extends AutoCloseable
{
/**
* @return The file model events
*/

Flow.Publisher<LFileModelEvent> events();

/**
* Add a tag.
*
Expand Down Expand Up @@ -123,6 +130,15 @@ void close()

CompletableFuture<?> redo();

/**
* Compact the file, deleting the undo/redo log and cleaning up any
* unused data in the file.
*
* @return The operation in progress
*/

CompletableFuture<?> compact();

/**
* @return Text describing the top of the redo stack, if any
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* Copyright © 2024 Mark Raynsford <[email protected]> https://www.io7m.com
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/


package com.io7m.laurel.filemodel.internal;

import com.io7m.darco.api.DDatabaseUnit;
import org.jooq.DSLContext;

import java.util.Properties;

import static com.io7m.laurel.filemodel.internal.Tables.IMAGES;
import static com.io7m.laurel.filemodel.internal.Tables.IMAGE_BLOBS;
import static com.io7m.laurel.filemodel.internal.Tables.REDO;
import static com.io7m.laurel.filemodel.internal.Tables.UNDO;

/**
* Compact a file.
*/

public final class LCommandCompact
extends LCommandAbstract<DDatabaseUnit>
{
/**
* Compact a file.
*/

public LCommandCompact()
{

}

/**
* @return A command factory
*/

public static LCommandFactoryType<DDatabaseUnit> provider()
{
return new LCommandFactory<>(
LCommandCompact.class.getCanonicalName(),
LCommandCompact::fromProperties
);
}

private static LCommandCompact fromProperties(
final Properties p)
{
final var c = new LCommandCompact();
c.setExecuted(true);
return c;
}

@Override
protected LCommandUndoable onExecute(
final LFileModel model,
final LDatabaseTransactionType transaction,
final DDatabaseUnit request)
{
final var context =
transaction.get(DSLContext.class);

context.truncate(UNDO)
.execute();
context.truncate(REDO)
.execute();
context.deleteFrom(IMAGE_BLOBS)
.where(IMAGE_BLOBS.IMAGE_BLOB_ID.notIn(
context.select(IMAGES.IMAGE_BLOB)
.from(IMAGES)
))
.execute();

model.clearUndo();
return LCommandUndoable.COMMAND_NOT_UNDOABLE;
}

@Override
protected void onUndo(
final LFileModel model,
final LDatabaseTransactionType transaction)
{
throw new UnsupportedOperationException();
}

@Override
protected void onRedo(
final LFileModel model,
final LDatabaseTransactionType transaction)
{
throw new UnsupportedOperationException();
}

@Override
public Properties toProperties()
{
return new Properties();
}

@Override
public String describe()
{
return "Compact a file.";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ protected LCommandUndoable onExecute(

final var file = request.file().toAbsolutePath();
model.setAttribute("ImageFile", file);
model.eventWithoutProgress("Adding image '%s'.", file);

final var imageBytes =
loadImage(model, request);
Expand Down Expand Up @@ -130,6 +131,7 @@ protected LCommandUndoable onExecute(
.get(IMAGES.IMAGE_ID);

model.setImagesAll(listImages(transaction));
model.eventWithoutProgress("Added image '%s'.", file);
return LCommandUndoable.COMMAND_UNDOABLE;
}

Expand Down Expand Up @@ -220,11 +222,14 @@ protected void onUndo(
final var context =
transaction.get(DSLContext.class);

model.eventWithoutProgress("Deleting image '%s'.", this.savedFile);

context.deleteFrom(IMAGES)
.where(IMAGES.IMAGE_ID.eq(this.savedImageId))
.execute();

model.setImagesAll(listImages(transaction));
model.eventWithoutProgress("Deleted image '%s'.", this.savedFile);
}

@Override
Expand All @@ -235,6 +240,7 @@ protected void onRedo(
final var context =
transaction.get(DSLContext.class);

model.eventWithoutProgress("Adding image '%s'.", this.savedFile);
context.insertInto(IMAGES)
.set(IMAGES.IMAGE_ID, this.savedImageId)
.set(IMAGES.IMAGE_BLOB, this.savedBlobId)
Expand All @@ -244,6 +250,7 @@ protected void onRedo(
.execute();

model.setImagesAll(listImages(transaction));
model.eventWithoutProgress("Added image '%s'.", this.savedFile);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ protected LCommandUndoable onExecute(
final var context =
transaction.get(DSLContext.class);

model.eventWithoutProgress("Adding tag '%s'.", tag);

final var recOpt =
context.insertInto(TAGS)
.set(TAGS.TAG_TEXT, tag.text())
Expand All @@ -99,6 +101,7 @@ protected LCommandUndoable onExecute(
.fetchOptional();

if (recOpt.isEmpty()) {
model.eventWithoutProgress("Tag '%s' already existed.", tag);
return LCommandUndoable.COMMAND_NOT_UNDOABLE;
}

Expand All @@ -107,6 +110,7 @@ protected LCommandUndoable onExecute(
this.text = tag.text();

model.setTagsAll(listTags(transaction));
model.eventWithoutProgress("Tag '%s' added.", tag);
return LCommandUndoable.COMMAND_UNDOABLE;
}

Expand All @@ -118,6 +122,8 @@ protected void onUndo(
final var context =
transaction.get(DSLContext.class);

model.eventWithoutProgress("Deleting tag '%s'.", this.text);

context.deleteFrom(TAGS)
.where(TAGS.TAG_ID.eq(this.id))
.execute();
Expand All @@ -133,6 +139,8 @@ protected void onRedo(
final var context =
transaction.get(DSLContext.class);

model.eventWithoutProgress("Adding tag '%s'.", this.text);

context.insertInto(TAGS)
.set(TAGS.TAG_ID, this.id)
.set(TAGS.TAG_TEXT, this.text)
Expand All @@ -141,6 +149,7 @@ protected void onRedo(
.execute();

model.setTagsAll(listTags(transaction));
model.eventWithoutProgress("Tag '%s' added.", this.text);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@

public interface LCommandType<P>
{
/**
* @return {@code true} if the database should be compacted after this command
*/

default boolean requiresCompaction()
{
return false;
}

/**
* Execute the command.
*
Expand Down
Loading

0 comments on commit 993cfbc

Please sign in to comment.