-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue 22779 improve db metadata (#22834)
* #22779 adding first changes * #22779 the file can holds a binary buffer or a file * #22779 adding more changes * #22779 just adding a comment for future * #22779 adding a normalization of the file repo manager, now it is grouping on directories * #22779 minor changes to handle the hashes on folders * #22779 adding more unit test * #22779 adding more unit test * #22779 adding a fix for an unit test * #22779 adding more doc --------- Co-authored-by: fabrizzio-dotCMS <fabrizzio@dotCMS.com>
- Loading branch information
1 parent
6b2a9be
commit 6590a84
Showing
13 changed files
with
535 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
dotCMS/src/integration-test/java/com/dotcms/storage/BinaryBlockFilJoinerImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.dotcms.storage; | ||
|
||
import com.dotcms.util.IntegrationTestInitService; | ||
import com.dotmarketing.util.FileUtil; | ||
import com.google.common.io.Files; | ||
import org.junit.Assert; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
public class BinaryBlockFilJoinerImplTest { | ||
|
||
|
||
@BeforeClass | ||
public static void prepare() throws Exception { | ||
IntegrationTestInitService.getInstance().init(); | ||
} | ||
|
||
/** | ||
* Method to test: {@link BinaryBlockFileJoinerImpl#join(byte[], int, int)} | ||
* Given Scenario: Adds less than 1000 bytes | ||
* ExpectedResult: should return the binary and not a file | ||
* | ||
*/ | ||
@Test | ||
public void Test_Easy_Path() throws IOException { | ||
|
||
final File file = FileUtil.createTemporaryFile("dot-db-storage-recovery", ".tmp", true); | ||
final BinaryBlockFileJoinerImpl binaryBlockFileJoiner = new BinaryBlockFileJoinerImpl(file, 1000); | ||
|
||
final String s1 = "one,two,three,four,five"; | ||
binaryBlockFileJoiner.join(s1.getBytes(StandardCharsets.UTF_8), 0, s1.length()); | ||
|
||
final String s2 = "six,seven,eight,nine,ten"; | ||
binaryBlockFileJoiner.join(s2.getBytes(StandardCharsets.UTF_8), 0, s2.length()); | ||
|
||
final byte [] bytes = binaryBlockFileJoiner.getBytes(); | ||
Assert.assertNotNull(bytes); | ||
Assert.assertEquals(new String(bytes,StandardCharsets.UTF_8).trim(),s1+s2); | ||
} | ||
|
||
/** | ||
* Method to test: {@link BinaryBlockFileJoinerImpl#join(byte[], int, int)} | ||
* Given Scenario: Adds less than 1000 bytes | ||
* ExpectedResult: should return the binary and not a file | ||
* | ||
*/ | ||
@Test | ||
public void Test_Flush() throws IOException { | ||
|
||
final File file = FileUtil.createTemporaryFile("dot-db-storage-recovery", ".tmp", true); | ||
final BinaryBlockFileJoinerImpl binaryBlockFileJoiner = new BinaryBlockFileJoinerImpl(file, 1000); | ||
final StringBuilder builder = new StringBuilder(); | ||
for (int i=0;i<100;++i) { | ||
final String s1 = "one,two,three,four,five,six,seven,eight,nine,ten,one,two,three,four,five,six,seven,eight,nine,ten,one,two,three,four," + | ||
"five,six,seven,eight,nine,ten,one,two,three,four,five,six,seven,eight,nine,ten" + "five,six,seven,eight,nine,ten,one,two,three,four,five,six,seven,eight,nine,ten" + | ||
"five,six,seven,eight,nine,ten,one,two,three,four,five,six,seven,eight,nine,ten"; | ||
builder.append(s1); | ||
binaryBlockFileJoiner.join(s1.getBytes(StandardCharsets.UTF_8), 0, s1.length()); | ||
} | ||
|
||
final byte [] bytes = binaryBlockFileJoiner.getBytes(); // | ||
Assert.assertNull(bytes); | ||
|
||
binaryBlockFileJoiner.flush(); | ||
binaryBlockFileJoiner.close(); | ||
|
||
Assert.assertTrue(file.exists()); | ||
final String fileContent = Files.asCharSource(file, StandardCharsets.UTF_8).read(); | ||
|
||
Assert.assertEquals(fileContent, builder.toString()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
...gration-test/java/com/dotcms/storage/repository/HashedLocalFileRepositoryManagerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.dotcms.storage.repository; | ||
|
||
import com.dotcms.IntegrationTestBase; | ||
import com.dotmarketing.util.FileUtil; | ||
import com.dotmarketing.util.UUIDUtil; | ||
import io.vavr.control.Try; | ||
import org.apache.commons.io.FileUtils; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.io.File; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
public class HashedLocalFileRepositoryManagerTest extends IntegrationTestBase { | ||
|
||
/** | ||
* Method to test: {@link HashedLocalFileRepositoryManager#normalizeFileInSubDirectoryAndFile(String)} | ||
* Given Scenario: Test diff scenarios for the method | ||
* ExpectedResult: The results perform based on the expected invariant | ||
* | ||
*/ | ||
@Test | ||
public void test_normalizeFileInSubDirectoryAndFile() { | ||
|
||
final HashedLocalFileRepositoryManager hashedLocalFileRepositoryManager = new HashedLocalFileRepositoryManager("./"); | ||
|
||
Assert.assertNull(hashedLocalFileRepositoryManager.normalizeFileInSubDirectoryAndFile(null)); | ||
final String shortHash1 = "1"; | ||
Assert.assertEquals(shortHash1, hashedLocalFileRepositoryManager.normalizeFileInSubDirectoryAndFile(shortHash1)); | ||
final String shortHash2 = "12"; | ||
Assert.assertEquals(shortHash2, hashedLocalFileRepositoryManager.normalizeFileInSubDirectoryAndFile(shortHash2)); | ||
final String shortHash3 = "1234"; | ||
Assert.assertEquals(shortHash3, hashedLocalFileRepositoryManager.normalizeFileInSubDirectoryAndFile(shortHash3)); | ||
final String hash = "f813463c714e009df1227f706e290e01"; | ||
Assert.assertEquals("/f813/" + hash , hashedLocalFileRepositoryManager.normalizeFileInSubDirectoryAndFile(hash)); | ||
} | ||
|
||
/** | ||
* Method to test: {@link HashedLocalFileRepositoryManager#exists(String)} (String)} {@link HashedLocalFileRepositoryManager#getOrCreateFile(String)} | ||
* Given Scenario: Test with non exiting hash, then get or create it, then checks it exists and retrieve again | ||
* ExpectedResult: The methods should return the right information based on the scenarios | ||
* | ||
*/ | ||
@Test | ||
public void test_GetOrCreate_and_Exits_() { | ||
|
||
final String basePath = "./" + UUIDUtil.uuid(); | ||
|
||
try { | ||
final HashedLocalFileRepositoryManager hashedLocalFileRepositoryManager = new HashedLocalFileRepositoryManager(basePath); | ||
final String hash = "f813463c714e009df1227f706e290e01"; | ||
Assert.assertFalse(hashedLocalFileRepositoryManager.exists(hash)); | ||
final File file = hashedLocalFileRepositoryManager.getOrCreateFile(hash); | ||
Assert.assertNotNull(file); | ||
Assert.assertFalse(file.exists()); | ||
Try.run(()->FileUtils.write(file, "2" + | ||
"test", StandardCharsets.UTF_8)); | ||
Assert.assertTrue(hashedLocalFileRepositoryManager.exists(hash)); | ||
Assert.assertEquals(hash, file.getName()); | ||
Assert.assertEquals("f813", file.getParentFile().getName()); | ||
|
||
|
||
final File file2 = hashedLocalFileRepositoryManager.getOrCreateFile(hash); | ||
Assert.assertNotNull(file2); | ||
Assert.assertTrue(file2.exists()); | ||
Assert.assertEquals(hash, file.getName()); | ||
Assert.assertEquals("f813", file.getParentFile().getName()); | ||
|
||
} finally { | ||
|
||
Try.run(()->FileUtil.deleteDir(basePath)); | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
dotCMS/src/main/java/com/dotcms/storage/BinaryBlockFileJoinerImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.dotcms.storage; | ||
|
||
import com.dotcms.util.FileJoiner; | ||
import com.dotcms.util.FileJoinerImpl; | ||
import org.apache.commons.io.output.DeferredFileOutputStream; | ||
|
||
import java.io.File; | ||
|
||
/** | ||
* Join into the bytes into a memory until reached the max size, switch to a file | ||
* {@link BinaryBlockFileJoinerImpl#getBytes()} is not null when the file contents is on memory, otherwise is null and the content is into the file. | ||
* @author jsanca | ||
*/ | ||
public class BinaryBlockFileJoinerImpl extends FileJoinerImpl implements FileJoiner { | ||
|
||
|
||
public BinaryBlockFileJoinerImpl(final File file, final int maxSize) { | ||
super(new DeferredFileOutputStream(maxSize, file)); | ||
} | ||
|
||
public byte [] getBytes() { | ||
|
||
final DeferredFileOutputStream outputStream = (DeferredFileOutputStream)this.getOutputStream(); | ||
return outputStream.isInMemory()?outputStream.getData():null; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
dotCMS/src/main/java/com/dotcms/storage/repository/BinaryFileWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.dotcms.storage.repository; | ||
|
||
import java.io.File; | ||
|
||
/** | ||
* This implementation of a File, make the File object as state pattern | ||
* - one state is the default File implementation | ||
* - the other state is a chunk of bytes | ||
* | ||
* This pattern allows to cache in memory, the small files; so upper layers may decide if reads the whole file from the file system | ||
* or just get the mem buffer, this avoids cpu cycles and I/O | ||
* @author jsanca | ||
*/ | ||
public class BinaryFileWrapper extends File { | ||
|
||
private final byte[] bufferByte; | ||
public BinaryFileWrapper(final File file, final byte[] bufferByte) { | ||
super(file.getPath()); | ||
this.bufferByte = bufferByte; | ||
} | ||
|
||
public byte[] getBufferByte() { | ||
return bufferByte; | ||
} | ||
} |
Oops, something went wrong.