Skip to content

Commit

Permalink
add renaming of fields for changes to schema
Browse files Browse the repository at this point in the history
  • Loading branch information
casperwtf committed Aug 19, 2024
1 parent eceb3d6 commit d5824ed
Show file tree
Hide file tree
Showing 14 changed files with 424 additions and 6 deletions.
24 changes: 24 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,30 @@
<pattern>org.sqlite</pattern>
<shadedPattern>wtf.casper.storageapi.libs.sqlite</shadedPattern>
</relocation>
<relocation>
<pattern>com.google</pattern>
<shadedPattern>wtf.casper.storageapi.libs.google</shadedPattern>
</relocation>
<relocation>
<pattern>org.bson</pattern>
<shadedPattern>wtf.casper.storageapi.libs.bson</shadedPattern>
</relocation>
<relocation>
<pattern>org.checkerframework</pattern>
<shadedPattern>wtf.casper.storageapi.libs.checkerframework</shadedPattern>
</relocation>
<relocation>
<pattern>org.intellij</pattern>
<shadedPattern>wtf.casper.storageapi.libs.intellij</shadedPattern>
</relocation>
<relocation>
<pattern>org.jetbrains</pattern>
<shadedPattern>wtf.casper.storageapi.libs.jetbrains</shadedPattern>
</relocation>
<relocation>
<pattern>org.slf4j</pattern>
<shadedPattern>wtf.casper.storageapi.libs.slf4j</shadedPattern>
</relocation>
</relocations>
<minimizeJar>false</minimizeJar>
</configuration>
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/wtf/casper/storageapi/StatelessKVStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;

Expand Down Expand Up @@ -155,6 +156,10 @@ default CompletableFuture<Boolean> migrateFrom(Supplier<StatelessKVStorage<K, V>
}, StorageAPIConstants.DB_THREAD_POOL);
}

CompletableFuture<Void> renameField(String path, String newPath);

CompletableFuture<Void> renameFields(Map<String, String> pathToNewPath);

/**
* @return a future that will complete with a collection of all values in the storage.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import com.github.benmanes.caffeine.cache.Caffeine;
import com.google.gson.JsonObject;
import lombok.SneakyThrows;
import wtf.casper.storageapi.KVStorage;
import wtf.casper.storageapi.cache.Cache;
Expand All @@ -13,6 +14,7 @@
import java.io.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -197,4 +199,84 @@ public CompletableFuture<Collection<V>> allValues() {
return cache.asMap().values();
}, StorageAPIConstants.DB_THREAD_POOL);
}

@Override
public CompletableFuture<Void> renameField(String path, String newPath) {
return CompletableFuture.runAsync(() -> {
File[] files = dataFolder.listFiles();
if (files == null) {
return;
}

cache().invalidateAll();
for (File file : files) {
if (!file.getName().endsWith(".json")) {
continue;
}

try {
final Reader reader = new FileReader(file);
final JsonObject value = StorageAPIConstants.getGson().fromJson(reader, JsonObject.class);
reader.close();

if (value == null) {
continue;
}

if (value.has(path)) {
value.add(newPath, value.get(path));
value.remove(path);
}

final Writer writer = new FileWriter(file);
StorageAPIConstants.getGson().toJson(value, writer);
writer.close();
} catch (final Exception e) {
e.printStackTrace();
}
}
cache().invalidateAll();
}, StorageAPIConstants.DB_THREAD_POOL);
}

@Override
public CompletableFuture<Void> renameFields(Map<String, String> pathToNewPath) {
return CompletableFuture.runAsync(() -> {
File[] files = dataFolder.listFiles();
if (files == null) {
return;
}

cache().invalidateAll();
for (File file : files) {
if (!file.getName().endsWith(".json")) {
continue;
}

try {
final Reader reader = new FileReader(file);
final JsonObject value = StorageAPIConstants.getGson().fromJson(reader, JsonObject.class);
reader.close();

if (value == null) {
continue;
}

for (Map.Entry<String, String> entry : pathToNewPath.entrySet()) {
if (value.has(entry.getKey())) {
value.add(entry.getValue(), value.get(entry.getKey()));
value.remove(entry.getKey());
}
}

final Writer writer = new FileWriter(file);
StorageAPIConstants.getGson().toJson(value, writer);
writer.close();
} catch (final Exception e) {
e.printStackTrace();
}
}
cache().invalidateAll();
}, StorageAPIConstants.DB_THREAD_POOL);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
Expand Down Expand Up @@ -159,4 +160,27 @@ public CompletableFuture<Collection<V>> allValues() {
}, StorageAPIConstants.DB_THREAD_POOL);
}

@Override
public CompletableFuture<Void> renameField(String path, String newPath) {
return CompletableFuture.runAsync(() -> {
cache().invalidateAll();
this.execute("UPDATE " + this.table + " SET data = JSON_SET(data, '$." + newPath + "', JSON_EXTRACT(data, '$." + path + "'))," +
" data = JSON_REMOVE(data, '$." + path + "')", statement -> {
});
cache().invalidateAll();
}, StorageAPIConstants.DB_THREAD_POOL);
}

@Override
public CompletableFuture<Void> renameFields(Map<String, String> pathToNewPath) {
return CompletableFuture.runAsync(() -> {
cache().invalidateAll();
pathToNewPath.forEach((path, newPath) -> {
this.execute("UPDATE " + this.table + " SET data = JSON_SET(data, '$." + newPath + "', JSON_EXTRACT(data, '$." + path + "'))," +
" data = JSON_REMOVE(data, '$." + path + "')", statement -> {
});
});
cache().invalidateAll();
}, StorageAPIConstants.DB_THREAD_POOL);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -171,4 +172,30 @@ public CompletableFuture<Collection<V>> allValues() {
return collection;
}, StorageAPIConstants.DB_THREAD_POOL);
}

@Override
public CompletableFuture<Void> renameFields(Map<String, String> pathToNewPath) {
return CompletableFuture.runAsync(() -> {
for (Map.Entry<String, String> entry : pathToNewPath.entrySet()) {
cache().invalidateAll();
getCollection().updateMany(
new Document(),
new Document("$rename", new Document(entry.getKey(), entry.getValue()))
);
cache().invalidateAll();
}
}, StorageAPIConstants.DB_THREAD_POOL);
}

@Override
public CompletableFuture<Void> renameField(String path, String newPath) {
return CompletableFuture.runAsync(() -> {
cache().invalidateAll();
getCollection().updateMany(
new Document(),
new Document("$rename", new Document(path, newPath))
);
cache().invalidateAll();
}, StorageAPIConstants.DB_THREAD_POOL);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
Expand Down Expand Up @@ -155,4 +156,26 @@ public CompletableFuture<Collection<V>> allValues() {
return values;
}, StorageAPIConstants.DB_THREAD_POOL);
}

@Override
public CompletableFuture<Void> renameFields(Map<String, String> pathToNewPath) {
return CompletableFuture.runAsync(() -> {
cache().invalidateAll();
for (Map.Entry<String, String> entry : pathToNewPath.entrySet()) {
execute("UPDATE " + this.table + " SET data = JSON_SET(data, '$." + entry.getValue() + "', JSON_EXTRACT(data, '$." + entry.getKey() + "'));");
execute("UPDATE " + this.table + " SET data = JSON_REMOVE(data, '$." + entry.getKey() + "');");
}
cache().invalidateAll();
}, StorageAPIConstants.DB_THREAD_POOL);
}

@Override
public CompletableFuture<Void> renameField(String path, String newPath) {
return CompletableFuture.runAsync(() -> {
cache().invalidateAll();
execute("UPDATE " + this.table + " SET data = JSON_SET(data, '$." + newPath + "', JSON_EXTRACT(data, '$." + path + "'));");
execute("UPDATE " + this.table + " SET data = JSON_REMOVE(data, '$." + path + "');");
cache().invalidateAll();
}, StorageAPIConstants.DB_THREAD_POOL);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@
import java.io.File;
import java.lang.reflect.Field;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.UUID;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
Expand Down Expand Up @@ -180,4 +177,28 @@ public void createTable() {

execute("CREATE TABLE IF NOT EXISTS " + table() + " (" + idType + ", json TEXT NOT NULL);");
}

@Override
public CompletableFuture<Void> renameField(String path, String newPath) {
return CompletableFuture.runAsync(() -> {
cache().invalidateAll();
execute("UPDATE " + this.table + " SET json = JSON_SET(json, '$." + newPath + "', JSON_EXTRACT(json, '$." + path + "'))," +
" json = JSON_REMOVE(json, '$." + path + "')", statement -> {
});
cache().invalidateAll();
}, StorageAPIConstants.DB_THREAD_POOL);
}

@Override
public CompletableFuture<Void> renameFields(Map<String, String> pathToNewPath) {
return CompletableFuture.runAsync(() -> {
cache().invalidateAll();
pathToNewPath.forEach((path, newPath) -> {
execute("UPDATE " + this.table + " SET json = JSON_SET(json, '$." + newPath + "', JSON_EXTRACT(json, '$." + path + "'))," +
" json = JSON_REMOVE(json, '$." + path + "')", statement -> {
});
});
cache().invalidateAll();
}, StorageAPIConstants.DB_THREAD_POOL);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.logging.Logger;

Expand Down Expand Up @@ -112,4 +113,22 @@ public CompletableFuture<Collection<V>> allValues() {
return values;
}, StorageAPIConstants.DB_THREAD_POOL);
}

@Override
public CompletableFuture<Void> renameField(String path, String newPath) {
return CompletableFuture.runAsync(() -> {
execute("UPDATE " + this.table + " SET data = JSON_SET(data, '$." + newPath + "', JSON_EXTRACT(data, '$." + path + "'));");
}, StorageAPIConstants.DB_THREAD_POOL);
}

@Override
public CompletableFuture<Void> renameFields(Map<String, String> pathToNewPath) {
return CompletableFuture.runAsync(() -> {
pathToNewPath.forEach((path, newPath) -> {
this.execute("UPDATE " + this.table + " SET data = JSON_SET(data, '$." + newPath + "', JSON_EXTRACT(data, '$." + path + "'))," +
" data = JSON_REMOVE(data, '$." + path + "')", statement -> {
});
});
}, StorageAPIConstants.DB_THREAD_POOL);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

@Log
Expand Down Expand Up @@ -145,4 +146,26 @@ public CompletableFuture<Collection<V>> allValues() {
return collection;
}, StorageAPIConstants.DB_THREAD_POOL);
}

@Override
public CompletableFuture<Void> renameFields(Map<String, String> pathToNewPath) {
return CompletableFuture.runAsync(() -> {
for (Map.Entry<String, String> entry : pathToNewPath.entrySet()) {
getCollection().updateMany(
new Document(),
new Document("$rename", new Document(entry.getKey(), entry.getValue()))
);
}
}, StorageAPIConstants.DB_THREAD_POOL);
}

@Override
public CompletableFuture<Void> renameField(String path, String newPath) {
return CompletableFuture.runAsync(() -> {
getCollection().updateMany(
new Document(),
new Document("$rename", new Document(path, newPath))
);
}, StorageAPIConstants.DB_THREAD_POOL);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.logging.Logger;

Expand Down Expand Up @@ -111,4 +112,20 @@ public CompletableFuture<Collection<V>> allValues() {
return values;
}, StorageAPIConstants.DB_THREAD_POOL);
}

@Override
public CompletableFuture<Void> renameField(String path, String newPath) {
return CompletableFuture.runAsync(() -> {
execute("ALTER TABLE " + this.table + " CHANGE " + path + " " + newPath + " TEXT;");
}, StorageAPIConstants.DB_THREAD_POOL);
}

@Override
public CompletableFuture<Void> renameFields(Map<String, String> pathToNewPath) {
return CompletableFuture.runAsync(() -> {
for (Map.Entry<String, String> entry : pathToNewPath.entrySet()) {
execute("ALTER TABLE " + this.table + " CHANGE " + entry.getKey() + " " + entry.getValue() + " TEXT;");
}
}, StorageAPIConstants.DB_THREAD_POOL);
}
}
Loading

0 comments on commit d5824ed

Please sign in to comment.