Skip to content

Commit

Permalink
#96. Vine Configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
augustearth committed Aug 18, 2022
1 parent de596e7 commit 949c7fb
Show file tree
Hide file tree
Showing 9 changed files with 234 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,11 @@ class CarnivalNeo4j extends Carnival {
}
}

if (!Files.exists(graphPath)) {
if (!Files.exists(graphPath) && config.gremlin.neo4j.directoryCreateIfNotPresent) {
log.trace "Files.createDirectories ${graphPath}"
Files.createDirectories(graphPath)
}

assertDirectoryAttributes(graphPath)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class CarnivalNeo4jConfiguration {
@ToString(includeNames=true)
static class Neo4j {
String directory
Boolean directoryCreateIfNotPresent = true

@ToString(includeNames=true)
static class Conf {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package carnival.core.vine
import groovy.util.logging.Slf4j
import org.apache.commons.codec.digest.DigestUtils

import carnival.core.config.Defaults
import carnival.core.util.CoreUtil
import carnival.util.GenericDataTable
import carnival.util.MappedDataTable
Expand Down Expand Up @@ -90,25 +89,28 @@ abstract class DataTableVineMethod<T,U extends VineMethodCall> extends VineMetho


U _callCacheModeOptional() {
_cacheDirectoryInitialize()

U methodCall
DataTableFiles cacheFiles = findCacheFiles()
if (cacheFiles) {
methodCall = _readFromCache(cacheFiles)
} else {
methodCall = _fetchAndCache()
}

methodCall
}


U _callCacheModeRequired() {
_cacheDirectoryInitialize()

final String EXT = "cache-mode 'required' requires existing cache files."

File cacheDir = _cacheDirectory()
if (cacheDir == null) throw new RuntimeException("cache directory is null. ${EXT}")
if (!cacheDir.exists()) throw new RuntimeException("cache directory does not exist. ${EXT}")
if (!cacheDir.isDirectory()) throw new RuntimeException("cache directory is not a directory. ${EXT}")
if (!cacheDir.canRead()) throw new RuntimeException("cache directory is not readable. ${EXT}")
File cacheDir = _cacheDirectoryValidated()
if (cacheDir == null) throw new RuntimeException("cache directory invalid. ${cacheDir} ${EXT}")
if (!cacheDir.canRead()) throw new RuntimeException("cache directory is not readable. ${cacheDir} ${EXT}")

DataTableFiles cacheFiles = DataTableVineMethodCall.findFiles(cacheDir, this.class, this.arguments)
if (cacheFiles == null || cacheFiles.areNull()) throw new RuntimeException("cache files are null. ${EXT}")
Expand All @@ -130,17 +132,11 @@ abstract class DataTableVineMethod<T,U extends VineMethodCall> extends VineMetho


List<File> _writeCacheFile(U methodCall) {
File cacheDir = _cacheDirectory()
File cacheDir = _cacheDirectoryValidated()
if (cacheDir == null) {
log.warn "cache directory is not configured. no cache file will be written."
log.warn "cache directory is invalid. no cache file will be written."
return null
}
if (!cacheDir.exists()) {
log.warn "cache directory does not exist. no cache files will be written. ${cacheDir}"
return null
}
assert cacheDir.isDirectory()

methodCall.writeFiles(cacheDir)
}

Expand All @@ -149,11 +145,6 @@ abstract class DataTableVineMethod<T,U extends VineMethodCall> extends VineMetho
// CACHING
///////////////////////////////////////////////////////////////////////////

File _cacheDirectory() {
Defaults.getDataCacheDirectory()
}


DataTableFiles findCacheFiles() {
File cacheDir = _cacheDirectory()
if (cacheDir == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package carnival.core.vine
import groovy.util.logging.Slf4j
import org.apache.commons.codec.digest.DigestUtils

import carnival.core.config.Defaults
import carnival.core.util.CoreUtil


Expand Down Expand Up @@ -81,6 +80,7 @@ abstract class JsonVineMethod<T> extends VineMethod {


JsonVineMethodCall<T> _callCacheModeOptional() {
_cacheDirectoryInitialize()
JsonVineMethodCall<T> methodCall
File cacheFile = findCacheFile()
if (cacheFile) {
Expand All @@ -93,13 +93,13 @@ abstract class JsonVineMethod<T> extends VineMethod {


JsonVineMethodCall<T> _callCacheModeRequired() {
_cacheDirectoryInitialize()

final String EXT = "cache-mode 'required' requires an existing cache file."

File cacheDir = _cacheDirectory()
if (cacheDir == null) throw new RuntimeException("cache directory is null. ${EXT}")
if (!cacheDir.exists()) throw new RuntimeException("cache directory does not exist. ${EXT}")
if (!cacheDir.isDirectory()) throw new RuntimeException("cache directory is not a directory. ${EXT}")
if (!cacheDir.canRead()) throw new RuntimeException("cache directory is not readable. ${EXT}")
File cacheDir = _cacheDirectoryValidated()
if (cacheDir == null) throw new RuntimeException("cache directory is invalid. ${cacheDir} ${EXT}")
if (!cacheDir.canRead()) throw new RuntimeException("cache directory is not readable. ${cacheDir} ${EXT}")

File cacheFile = JsonVineMethodCall.findFile(cacheDir, this.class, this.arguments)
if (cacheFile == null) throw new RuntimeException("cache file does not exist. ${EXT}")
Expand Down Expand Up @@ -127,9 +127,9 @@ abstract class JsonVineMethod<T> extends VineMethod {


List<File> _writeCacheFile(JsonVineMethodCall<T> methodCall) {
File cacheDir = _cacheDirectory()
File cacheDir = _cacheDirectoryValidated()
if (cacheDir == null) {
log.warn "cache directory is null. no cache file will be written."
log.warn "cache directory is invalid. no cache file will be written."
return null
}

Expand All @@ -154,11 +154,6 @@ abstract class JsonVineMethod<T> extends VineMethod {
// CACHING
///////////////////////////////////////////////////////////////////////////

File _cacheDirectory() {
Defaults.getDataCacheDirectory()
}


File findCacheFile() {
File cacheDir = _cacheDirectory()
if (cacheDir == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ trait Vine extends MethodsHolder {
/** */
Map<String,Object> _dynamicVineMethodResources = new HashMap<String,Object>()

/** */
VineConfiguration vineConfiguration = VineConfiguration.defaultConfiguration()


///////////////////////////////////////////////////////////////////////////
// CLIENT INTERFACE
Expand Down Expand Up @@ -74,6 +77,7 @@ trait Vine extends MethodsHolder {

// create a vine method instance
def vm = vmc.newInstance(this)
vm.vineConfiguration = this.vineConfiguration

// should probably get rid of this
_dynamicVineMethodResources.each { String name, Object value ->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package carnival.core.vine



import java.nio.file.Path
import java.nio.file.Paths
import java.nio.file.Files
import groovy.util.logging.Slf4j
import groovy.transform.ToString



@Slf4j
@ToString(includeNames=true)
class VineConfiguration {

///////////////////////////////////////////////////////////////////////////
// STATIC
///////////////////////////////////////////////////////////////////////////

final static String CACHE_PATH_DEFAULT = "carnival-home/vine/cache"

static public VineConfiguration defaultConfiguration() {
Path currentRelativePath = Paths.get("");
Path cachePath = currentRelativePath.resolve(CACHE_PATH_DEFAULT)
String cachePathString = cachePath.toAbsolutePath().toString()

VineConfiguration config = new VineConfiguration()
config.cache.directory = cachePathString

return config
}

@ToString(includeNames=true)
static class Cache {
String mode = CacheMode.IGNORE.name()
String directory
Boolean directoryCreateIfNotPresent = true
}
Cache cache = new Cache()


///////////////////////////////////////////////////////////////////////////
// INSTANCE
///////////////////////////////////////////////////////////////////////////

/**
* Return the cache mode configuration item as a CacheMode object.
*
*/
public CacheMode getCacheMode() {
String cmStr = cache.mode
assert cmStr != null

CacheMode cm = Enum.valueOf(CacheMode, cmStr.trim().toUpperCase())
return cm
}


/**
* Return the cache directory configuration item as a File.
*
*/
public File getCacheDirectory() {
String dirPathString = cache.directory
assert dirPathString != null
Path relativePath = Paths.get(dirPathString.trim())
return relativePath.toFile()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package carnival.core.vine



import java.nio.file.Path
import java.nio.file.Paths
import java.nio.file.Files
import groovy.util.logging.Slf4j
import org.apache.commons.codec.digest.DigestUtils

import carnival.core.config.Defaults
import carnival.core.util.CoreUtil


Expand All @@ -23,25 +25,6 @@ import carnival.core.util.CoreUtil
@Slf4j
abstract class VineMethod {

///////////////////////////////////////////////////////////////////////////
// STATIC FIELDS
///////////////////////////////////////////////////////////////////////////

static final String DEFAULT_CACHE_MODE_CONFIG_KEY = 'carnival.cache-mode'


///////////////////////////////////////////////////////////////////////////
// STATIC METHODS
///////////////////////////////////////////////////////////////////////////

static public CacheMode defaultCacheMode() {
String str = Defaults.getConfigValue(DEFAULT_CACHE_MODE_CONFIG_KEY)
if (str == null) return CacheMode.IGNORE
CacheMode cm = Enum.valueOf(CacheMode, str)
if (cm) return cm
CacheMode.IGNORE
}


///////////////////////////////////////////////////////////////////////////
// ABSTRACT INTERFACE
Expand All @@ -55,19 +38,34 @@ abstract class VineMethod {
///////////////////////////////////////////////////////////////////////////

/** */
CacheMode cacheMode = defaultCacheMode()
VineConfiguration vineConfiguration = VineConfiguration.defaultConfiguration()

/** */
Map arguments = [:]


///////////////////////////////////////////////////////////////////////////
// METHODS CACHE MODE
// CACHE MODE
///////////////////////////////////////////////////////////////////////////

/** */
CacheMode cacheMode

/** */
public CacheMode getCacheMode() {
if (cacheMode != null) return cacheMode
assert vineConfiguration != null
vineConfiguration.getCacheMode()
}

/** */
public void setCacheMode(CacheMode cacheMode) {
this.cacheMode = cacheMode
}

VineMethod cacheMode(CacheMode cm) {
assert cm != null
this.cacheMode = cm
setCacheMode(cm)
this
}

Expand All @@ -76,6 +74,61 @@ abstract class VineMethod {
}


///////////////////////////////////////////////////////////////////////////
// CACHE DIRECTORY
///////////////////////////////////////////////////////////////////////////

File _cacheDirectory() {
assert vineConfiguration != null
vineConfiguration.getCacheDirectory()
}

File _cacheDirectoryValidated() {
File cacheDir = _cacheDirectory()
if (cacheDir == null) {
log.warn "cache directory is not configured."
return null
}
if (!cacheDir.exists()) {
log.warn "cache directory does not exist. ${cacheDir}"
return null
}
if (!cacheDir.isDirectory()) {
log.warn "cache directory is not a directory. ${cacheDir}"
return null
}
return cacheDir
}

void _cacheDirectoryInitialize() {
Path cachePath = Paths.get(vineConfiguration.cache.directory)
if (cachePath == null) throw new RuntimeException("cachePath is null")

def assertDirectoryAttributes = { Path dirPath ->
String dirPathString = dirPath.toAbsolutePath().toString()
if (!Files.exists(dirPath)) {
throw new RuntimeException("${dirPathString} does not exist")
}
if (!Files.isDirectory(dirPath)) {
throw new RuntimeException("${dirPathString} is not a directory")
}
if (!Files.isWritable(dirPath)) {
throw new RuntimeException("${dirPathString} is not writable")
}
if (!Files.isReadable(dirPath)) {
throw new RuntimeException("${dirPathString} is not readable")
}
}

if (!Files.exists(cachePath) && vineConfiguration.cache.directoryCreateIfNotPresent) {
log.trace "Files.createDirectories ${cachePath}"
Files.createDirectories(cachePath)
}

assertDirectoryAttributes(cachePath)
}


///////////////////////////////////////////////////////////////////////////
// METHODS ARGUMENTS
///////////////////////////////////////////////////////////////////////////
Expand Down
Loading

0 comments on commit 949c7fb

Please sign in to comment.