Initial Commit

This commit is contained in:
Braydon 2023-06-02 19:19:21 -04:00
commit 15d6ff6988
3 changed files with 159 additions and 0 deletions

26
.gitignore vendored Normal file
View File

@ -0,0 +1,26 @@
*.class
*.log
*.ctxt
.mtj.tmp/
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
hs_err_pid*
replay_pid*
.idea
cmake-build-*/
*.iws
out/
build/
work/
.idea_modules/
atlassian-ide-plugin.xml
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
git.properties

68
pom.xml Normal file
View File

@ -0,0 +1,68 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>me.braydon</groupId>
<artifactId>hastebin-migrator</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>8</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<dependencies>
<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.28</version>
<scope>provided</scope>
</dependency>
<!-- Redis -->
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
<version>6.2.4.RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,65 @@
package me.braydon.migrator;
import io.lettuce.core.RedisClient;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.async.RedisAsyncCommands;
import lombok.NonNull;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;
/**
* @author Braydon
*/
public class Main {
public static void main(@NonNull String[] args) {
String redisUri = System.getenv("REDIS_URI"); // Get the redis uri env var
assert redisUri != null; // We need this
File dataDir = new File("data"); // The directory where the files are located
if (!dataDir.exists()) {
dataDir.mkdirs();
System.out.println("Didn't find the './data' directory, created it for you (place files here)");
return;
}
File[] files = dataDir.listFiles();
if (files == null || (files.length == 0)) { // No files to iterate
System.out.println("No files to copy");
return;
}
try (RedisClient redisClient = RedisClient.create(redisUri);
StatefulRedisConnection<String, String> connection = redisClient.connect()
) {
RedisAsyncCommands<String, String> asyncCommands = connection.async();
asyncCommands.multi(); // Start the transaction
int copied = 0; // The amount of files copied
for (File file : files) {
if (file.isDirectory()) { // Only care about files
continue;
}
String md5Hash = file.getName(); // The name is the md5 hash of the paste
try {
List<String> fileLines = Files.readAllLines(file.toPath()); // The lines of the line
if (fileLines.isEmpty()) { // Skip empty files
continue;
}
StringBuilder stringBuilder = new StringBuilder();
for (String fileLine : fileLines) {
stringBuilder.append(fileLine).append("\n"); // Append the line to the string builder
}
String fileContent = stringBuilder.toString();
System.out.println("Copying '" + md5Hash + "' to Redis"); // Log the copy
asyncCommands.set(md5Hash, fileContent); // Copy the file to Redis
copied++; // Increment the amount of files copied
System.out.println("Done!");
} catch (IOException ex) {
ex.printStackTrace();
}
}
asyncCommands.exec(); // Execute the transaction
System.out.println("Copied " + copied + "/" + files.length + " files to Redis"); // Log the copied files
}
}
}