@ -1,15 +1,14 @@
package org.openrs2.cache
package org.openrs2.cache
import io.netty.buffer.ByteBuf
import io.netty.buffer.ByteBuf
import java.nio.file.attribute.FileTime
import org.apache.commons.compress.archivers.tar.TarArchiveEntry
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream
import java.time.Instant
import java.time.Instant
import java.util.zip.Deflater
import java.util.Date
import java.util.zip.ZipEntry
import java.util.zip.ZipOutputStream
/ * *
/ * *
* A specialised [ Store ] implementation that writes a cache in the
* A specialised [ Store ] implementation that writes a cache in the
* [ FlatFileStore ] format to a [ Zip OutputStream] .
* [ FlatFileStore ] format to a [ TarArchive OutputStream] .
*
*
* The cache is not buffered to disk .
* The cache is not buffered to disk .
*
*
@ -18,23 +17,17 @@ import java.util.zip.ZipOutputStream
*
*
* It is only intended for use by the cache archiving service ' s web interface .
* It is only intended for use by the cache archiving service ' s web interface .
* /
* /
public class FlatFileStoreZip Writer (
public class FlatFileStoreTar Writer (
private val out : Zip OutputStream,
private val out : TarArchive OutputStream,
private val prefix : String = " cache/ " ,
private val prefix : String = " cache/ " ,
level : Int = Deflater . BEST _COMPRESSION ,
timestamp : Instant = Instant . EPOCH
timestamp : Instant = Instant . EPOCH
) : Store {
) : Store {
private val timestamp = FileTim e. from ( timestamp )
private val timestamp = Dat e. from ( timestamp )
init {
private fun createTarEntry ( name : String , size : Int ) : TarArchiveEntry {
out . setLevel ( level )
val entry = TarArchiveEntry ( prefix + name )
}
entry . modTime = timestamp
entry . size = size . toLong ( )
private fun createZipEntry ( name : String ) : ZipEntry {
val entry = ZipEntry ( prefix + name )
entry . creationTime = timestamp
entry . lastAccessTime = timestamp
entry . lastModifiedTime = timestamp
return entry
return entry
}
}
@ -57,7 +50,8 @@ public class FlatFileStoreZipWriter(
override fun create ( archive : Int ) {
override fun create ( archive : Int ) {
require ( archive in 0. . Store . MAX _ARCHIVE )
require ( archive in 0. . Store . MAX _ARCHIVE )
out . putNextEntry ( createZipEntry ( " $archive / " ) )
out . putArchiveEntry ( createTarEntry ( " $archive / " , size = 0 ) )
out . closeArchiveEntry ( )
}
}
override fun read ( archive : Int , group : Int ) : ByteBuf {
override fun read ( archive : Int , group : Int ) : ByteBuf {
@ -69,8 +63,9 @@ public class FlatFileStoreZipWriter(
require ( group >= 0 )
require ( group >= 0 )
require ( buf . readableBytes ( ) <= Store . MAX _GROUP _SIZE )
require ( buf . readableBytes ( ) <= Store . MAX _GROUP _SIZE )
out . putNextEntry ( createZip Entry ( " $archive / $group .dat " ) )
out . putArchiveEntry ( createTar Entry ( " $archive / $group .dat " , buf . readableBytes ( ) ) )
buf . readBytes ( out , buf . readableBytes ( ) )
buf . readBytes ( out , buf . readableBytes ( ) )
out . closeArchiveEntry ( )
}
}
override fun remove ( archive : Int ) {
override fun remove ( archive : Int ) {