Search in sources :

Example 1 with ByteHandlePipeline

use of org.sirix.io.bytepipe.ByteHandlePipeline in project sirix by sirixdb.

the class BerkeleyStorageFactory method createStorage.

/**
 * Create a new storage.
 *
 * @param resourceConfig the resource configuration
 * @return the berkeley DB storage
 * @throws NullPointerException if {@link ResourceConfiguration} is {@code null}
 * @throws SirixIOException if the storage couldn't be created because of an I/O exception
 */
public BerkeleyStorage createStorage(final ResourceConfiguration resourceConfig) {
    try {
        final Path repoFile = resourceConfig.mPath.resolve(ResourceConfiguration.ResourcePaths.DATA.getFile());
        if (!Files.exists(repoFile)) {
            Files.createDirectories(repoFile);
        }
        final ByteHandlePipeline byteHandler = checkNotNull(resourceConfig.mByteHandler);
        final DatabaseConfig conf = generateDBConf();
        final EnvironmentConfig config = generateEnvConf();
        final List<Path> path;
        try (final Stream<Path> stream = Files.list(repoFile)) {
            path = stream.collect(toList());
        }
        if (path.isEmpty() || (path.size() == 1 && "sirix.data".equals(path.get(0).getFileName().toString()))) {
            conf.setAllowCreate(true);
            config.setAllowCreate(true);
        }
        final Environment env = new Environment(repoFile.toFile(), config);
        final Database database = env.openDatabase(null, NAME, conf);
        return new BerkeleyStorage(env, database, byteHandler);
    } catch (final DatabaseException | IOException e) {
        throw new SirixIOException(e);
    }
}
Also used : Path(java.nio.file.Path) EnvironmentConfig(com.sleepycat.je.EnvironmentConfig) IOException(java.io.IOException) SirixIOException(org.sirix.exception.SirixIOException) SirixIOException(org.sirix.exception.SirixIOException) Database(com.sleepycat.je.Database) Environment(com.sleepycat.je.Environment) ByteHandlePipeline(org.sirix.io.bytepipe.ByteHandlePipeline) DatabaseException(com.sleepycat.je.DatabaseException) DatabaseConfig(com.sleepycat.je.DatabaseConfig)

Example 2 with ByteHandlePipeline

use of org.sirix.io.bytepipe.ByteHandlePipeline in project sirix by sirixdb.

the class PageWriteTrxImpl method createTrxIntentLog.

private TransactionIntentLog createTrxIntentLog(final XdmResourceManager resourceManager) {
    final Path logFile = resourceManager.getResourceConfig().mPath.resolve("log").resolve("intent-log");
    try {
        if (Files.exists(logFile)) {
            Files.delete(logFile);
            Files.createFile(logFile);
        }
        final RandomAccessFile file = new RandomAccessFile(logFile.toFile(), "rw");
        final FileWriter fileWriter = new FileWriter(file, new ByteHandlePipeline(resourceManager.getResourceConfig().mByteHandler), SerializationType.TRANSACTION_INTENT_LOG);
        final PersistentFileCache persistentFileCache = new PersistentFileCache(fileWriter, this);
        return new TransactionIntentLog(persistentFileCache);
    } catch (final IOException e) {
        throw new UncheckedIOException(e);
    }
}
Also used : Path(java.nio.file.Path) RandomAccessFile(java.io.RandomAccessFile) FileWriter(org.sirix.io.file.FileWriter) UncheckedIOException(java.io.UncheckedIOException) ByteHandlePipeline(org.sirix.io.bytepipe.ByteHandlePipeline) SirixIOException(org.sirix.exception.SirixIOException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) PersistentFileCache(org.sirix.cache.PersistentFileCache) TransactionIntentLog(org.sirix.cache.TransactionIntentLog)

Example 3 with ByteHandlePipeline

use of org.sirix.io.bytepipe.ByteHandlePipeline in project sirix by sirixdb.

the class ResourceConfiguration method serialize.

/**
 * Serialize the configuration.
 *
 * @param config configuration to serialize
 * @throws SirixIOException if an I/O error occurs
 */
public static void serialize(final ResourceConfiguration config) throws SirixIOException {
    final Path configFile = config.getConfigFile();
    try (final FileWriter fileWriter = new FileWriter(configFile.toFile());
        final JsonWriter jsonWriter = new JsonWriter(fileWriter)) {
        jsonWriter.beginObject();
        // Versioning.
        jsonWriter.name(JSONNAMES[0]);
        jsonWriter.beginObject();
        jsonWriter.name(JSONNAMES[1]).value(config.mRevisionKind.name());
        jsonWriter.name(JSONNAMES[2]).value(config.mRevisionsToRestore);
        jsonWriter.endObject();
        // ByteHandlers.
        final ByteHandlePipeline byteHandler = config.mByteHandler;
        jsonWriter.name(JSONNAMES[3]);
        jsonWriter.beginArray();
        for (final ByteHandler handler : byteHandler.getComponents()) {
            jsonWriter.value(handler.getClass().getName());
        }
        jsonWriter.endArray();
        // Storage type.
        jsonWriter.name(JSONNAMES[4]).value(config.mStorage.name());
        // Hashing type.
        jsonWriter.name(JSONNAMES[5]).value(config.mHashKind.name());
        // Text compression.
        jsonWriter.name(JSONNAMES[6]).value(config.mCompression);
        // Path summary.
        jsonWriter.name(JSONNAMES[7]).value(config.mPathSummary);
        // ID.
        jsonWriter.name(JSONNAMES[8]).value(config.mID);
        // Dewey IDs stored or not.
        jsonWriter.name(JSONNAMES[9]).value(config.mDeweyIDsStored);
        // Persistenter.
        jsonWriter.name(JSONNAMES[10]).value(config.mPersistenter.getClass().getName());
        jsonWriter.endObject();
    } catch (final IOException e) {
        throw new SirixIOException(e);
    }
    // Database config.
    DatabaseConfiguration.serialize(config.mDBConfig);
}
Also used : Path(java.nio.file.Path) FileWriter(java.io.FileWriter) ByteHandlePipeline(org.sirix.io.bytepipe.ByteHandlePipeline) ByteHandler(org.sirix.io.bytepipe.ByteHandler) SirixIOException(org.sirix.exception.SirixIOException) IOException(java.io.IOException) JsonWriter(com.google.gson.stream.JsonWriter) SirixIOException(org.sirix.exception.SirixIOException)

Example 4 with ByteHandlePipeline

use of org.sirix.io.bytepipe.ByteHandlePipeline in project sirix by sirixdb.

the class ResourceConfiguration method deserialize.

/**
 * Deserializing a Resource configuration from a JSON-file from the persistent storage.
 *
 * @param file where the resource lies in.
 * @return a complete {@link ResourceConfiguration} instance
 * @throws SirixIOException if an I/O error occurs
 */
public static ResourceConfiguration deserialize(final Path file) throws SirixIOException {
    try {
        final Path configFile = file.resolve(ResourcePaths.CONFIG_BINARY.getFile());
        final FileReader fileReader = new FileReader(configFile.toFile());
        final JsonReader jsonReader = new JsonReader(fileReader);
        jsonReader.beginObject();
        // Versioning.
        String name = jsonReader.nextName();
        assert name.equals(JSONNAMES[0]);
        jsonReader.beginObject();
        name = jsonReader.nextName();
        assert name.equals(JSONNAMES[1]);
        final Versioning revisioning = Versioning.valueOf(jsonReader.nextString());
        name = jsonReader.nextName();
        assert name.equals(JSONNAMES[2]);
        final int revisionToRestore = jsonReader.nextInt();
        jsonReader.endObject();
        // ByteHandlers.
        final List<ByteHandler> handlerList = new ArrayList<>();
        name = jsonReader.nextName();
        assert name.equals(JSONNAMES[3]);
        jsonReader.beginArray();
        while (jsonReader.hasNext()) {
            final Class<?> handlerClazz = Class.forName(jsonReader.nextString());
            final Constructor<?> handlerCons = handlerClazz.getConstructors()[0];
            handlerList.add((ByteHandler) handlerCons.newInstance());
        }
        jsonReader.endArray();
        final ByteHandlePipeline pipeline = new ByteHandlePipeline(handlerList.toArray(new ByteHandler[handlerList.size()]));
        // Storage type.
        name = jsonReader.nextName();
        assert name.equals(JSONNAMES[4]);
        final StorageType storage = StorageType.valueOf(jsonReader.nextString());
        // Hashing type.
        name = jsonReader.nextName();
        assert name.equals(JSONNAMES[5]);
        final HashKind hashing = HashKind.valueOf(jsonReader.nextString());
        // Text compression.
        name = jsonReader.nextName();
        assert name.equals(JSONNAMES[6]);
        final boolean compression = jsonReader.nextBoolean();
        // Path summary.
        name = jsonReader.nextName();
        assert name.equals(JSONNAMES[7]);
        final boolean pathSummary = jsonReader.nextBoolean();
        // Unique ID.
        name = jsonReader.nextName();
        assert name.equals(JSONNAMES[8]);
        final int ID = jsonReader.nextInt();
        name = jsonReader.nextName();
        assert name.equals(JSONNAMES[9]);
        final boolean deweyIDsStored = jsonReader.nextBoolean();
        name = jsonReader.nextName();
        assert name.equals(JSONNAMES[10]);
        final Class<?> persistenterClazz = Class.forName(jsonReader.nextString());
        final Constructor<?> persistenterConstr = persistenterClazz.getConstructors()[0];
        final RecordPersistenter persistenter = (RecordPersistenter) persistenterConstr.newInstance();
        jsonReader.endObject();
        jsonReader.close();
        fileReader.close();
        // Deserialize database config.
        final DatabaseConfiguration dbConfig = DatabaseConfiguration.deserialize(file.getParent().getParent());
        // Builder.
        final ResourceConfiguration.Builder builder = new ResourceConfiguration.Builder(file.getFileName().toString(), dbConfig);
        builder.byteHandlerPipeline(pipeline).hashKind(hashing).versioningApproach(revisioning).revisionsToRestore(revisionToRestore).storageType(storage).persistenter(persistenter).useTextCompression(compression).buildPathSummary(pathSummary).useDeweyIDs(deweyIDsStored);
        // Deserialized instance.
        final ResourceConfiguration config = new ResourceConfiguration(builder);
        return config.setID(ID);
    } catch (IOException | ClassNotFoundException | IllegalArgumentException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
        throw new SirixIOException(e);
    }
}
Also used : ArrayList(java.util.ArrayList) HashKind(org.sirix.access.HashKind) ByteHandler(org.sirix.io.bytepipe.ByteHandler) SirixIOException(org.sirix.exception.SirixIOException) JsonReader(com.google.gson.stream.JsonReader) FileReader(java.io.FileReader) ByteHandlePipeline(org.sirix.io.bytepipe.ByteHandlePipeline) Path(java.nio.file.Path) StorageType(org.sirix.io.StorageType) SirixIOException(org.sirix.exception.SirixIOException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Versioning(org.sirix.settings.Versioning) RecordPersistenter(org.sirix.node.interfaces.RecordPersistenter)

Aggregations

IOException (java.io.IOException)4 Path (java.nio.file.Path)4 SirixIOException (org.sirix.exception.SirixIOException)4 ByteHandlePipeline (org.sirix.io.bytepipe.ByteHandlePipeline)4 ByteHandler (org.sirix.io.bytepipe.ByteHandler)2 JsonReader (com.google.gson.stream.JsonReader)1 JsonWriter (com.google.gson.stream.JsonWriter)1 Database (com.sleepycat.je.Database)1 DatabaseConfig (com.sleepycat.je.DatabaseConfig)1 DatabaseException (com.sleepycat.je.DatabaseException)1 Environment (com.sleepycat.je.Environment)1 EnvironmentConfig (com.sleepycat.je.EnvironmentConfig)1 FileReader (java.io.FileReader)1 FileWriter (java.io.FileWriter)1 RandomAccessFile (java.io.RandomAccessFile)1 UncheckedIOException (java.io.UncheckedIOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 HashKind (org.sirix.access.HashKind)1 PersistentFileCache (org.sirix.cache.PersistentFileCache)1