Package org.xtreemfs.babudb.api.dev.transaction

Examples of org.xtreemfs.babudb.api.dev.transaction.InMemoryProcessing


     * Feed the transactionManager with the knowledge on how to handle snapshot related requests.
     */
    private void initializeTransactionManager() {
       
        dbs.getTransactionManager().registerInMemoryProcessing(Operation.TYPE_CREATE_SNAP,
                new InMemoryProcessing() {
           
            @Override
            public Object[] deserializeRequest(ReusableBuffer serialized) throws BabuDBException {
                ObjectInputStream oin = null;
                try {
                    oin = new ObjectInputStream(new ByteArrayInputStream(serialized.array()));
                    int dbId = oin.readInt();
                    SnapshotConfig snap = (SnapshotConfig) oin.readObject();
                   
                    return new Object[] { dbId, snap };
                } catch (Exception e) {
                    throw new BabuDBException(ErrorCode.IO_ERROR,
                            "Could not deserialize operation of type " + Operation.TYPE_CREATE_SNAP +
                                ", because: "+e.getMessage(), e);
                } finally {
                    try {
                        serialized.flip();
                        if (oin != null) oin.close();
                    } catch (IOException ioe) {
                        /* who cares? */
                    }
                }
            }
           
            @Override
            public OperationInternal convertToOperation(Object[] args) {
                return new BabuDBTransaction.BabuDBOperation(Operation.TYPE_CREATE_SNAP, (String) null,
                        new Object[] { args[0], args[1] });
            }
           
            @Override
            public Object process(OperationInternal operation) throws BabuDBException {
               
                Object[] args = operation.getParams();
               
                // parse args
                int dbId = (Integer) args[0];
                SnapshotConfig snap = (SnapshotConfig) args[1];
               
                // complete arguments
                if (dbId == InsertRecordGroup.DB_ID_UNKNOWN &&
                    operation.getDatabaseName() != null) {
                    dbId = dbs.getDatabaseManager().getDatabase(
                            operation.getDatabaseName()).getLSMDB().getDatabaseId();
                    operation.updateParams(new Object[] { dbId, snap });                 
                } else if (operation.getDatabaseName() == null) {
                    operation.updateDatabaseName(
                            dbs.getDatabaseManager().getDatabase(dbId).getName());
                }
               
                Map<String, Snapshot> snapMap = snapshotDBs.get(operation.getDatabaseName());
                if (snapMap == null) {
                    snapMap = new HashMap<String, Snapshot>();
                    snapshotDBs.put(operation.getDatabaseName(), snapMap);
                }
               
                // if the snapshot already exists ...
                if (snapMap.containsKey(snap.getName())) {
                   
                    throw new BabuDBException(ErrorCode.SNAP_EXISTS, "snapshot '" + snap.getName()
                        + "' already exists");
                }
               
                snapMap.put(snap.getName(), new Snapshot(null, dbs));
               
                // first, create new in-memory snapshots of all indices
                int[] snapIds = null;
                try {
                    dbs.getTransactionManager().lockService();
                   
                    // create the snapshot
                    snapIds = dbs.getDatabaseManager().getDatabase(dbId).getLSMDB().createSnapshot(
                            snap.getIndices());
                } catch (InterruptedException e) {
                    throw new BabuDBException(ErrorCode.INTERRUPTED, e.getMessage());
                } finally {
                    dbs.getTransactionManager().unlockService();
                }
               
                // then, enqueue a snapshot materialization request in the
                // checkpointer's queue
                dbs.getCheckpointer().addSnapshotMaterializationRequest(operation.getDatabaseName(),
                        snapIds, snap);
               
                // as long as the snapshot has not been persisted yet, add a view on
                // the current snapshot in the original database to the snapshot DB map
                synchronized (snapshotDBs) {
                   
                    Snapshot s = snapMap.get(snap.getName());
                    if (s.getView() == null) {
                        s.setView(new InMemoryView(dbs, operation.getDatabaseName(), snap,
                                snapIds));
                    }
                }
               
                return null;
            }
        });

        dbs.getTransactionManager().registerInMemoryProcessing(Operation.TYPE_DELETE_SNAP,
                new InMemoryProcessing() {
                       
            @Override
            public Object[] deserializeRequest(ReusableBuffer serialized) throws BabuDBException {
               
                byte[] payload = serialized.array();
View Full Code Here


     * Feed the transactionManager with the knowledge to handle database-modifying related requests.
     */
    private void initializeTransactionManager() {
       
        dbs.getTransactionManager().registerInMemoryProcessing(Operation.TYPE_CREATE_DB,
                new InMemoryProcessing() {
                       
            @Override
            public Object[] deserializeRequest(ReusableBuffer serialized) throws BabuDBException {
               
                serialized.getInt(); // do not use, deprecated
               
                String dbName = serialized.getString();
                int indices = serialized.getInt();
               
                serialized.flip();
               
                return new Object[] { dbName, indices, null };
            }
           
            @Override
            public OperationInternal convertToOperation(Object[] args) {
                return new BabuDBTransaction.BabuDBOperation(Operation.TYPE_CREATE_DB,
                        (String) args[0], new Object[] { args[1], args[2] });
            }
           
            @Override
            public DatabaseInternal process(OperationInternal operation) throws BabuDBException {
                // parse args
                Object[] args = operation.getParams();
                int numIndices = (Integer) args[0];
               
                ByteRangeComparator[] com = null;
                if (args.length > 2) {
                    com = (ByteRangeComparator[]) args[1];
                }
                if (com == null) {
                    ByteRangeComparator[] comps = new ByteRangeComparator[numIndices];
                    final ByteRangeComparator defaultComparator = compInstances
                            .get(DefaultByteRangeComparator.class.getName());
                    for (int i = 0; i < numIndices; i++) {
                        comps[i] = defaultComparator;
                    }
                   
                    com = comps;
                }
               
                DatabaseImpl db = null;
                synchronized (getDBModificationLock()) {
                    synchronized (dbs.getCheckpointer()) {
                        if (dbsByName.containsKey(operation.getDatabaseName())) {
                            throw new BabuDBException(ErrorCode.DB_EXISTS, "database '" +
                                    operation.getDatabaseName() + "' already exists");
                        }
                        final int dbId = nextDbId++;
                        db = new DatabaseImpl(dbs, new LSMDatabase(operation.getDatabaseName(),
                                dbId, dbs.getConfig().getBaseDir() + operation.getDatabaseName() +
                                File.separatorChar, numIndices, false, com, dbs.getConfig()
                                .getCompression(), dbs.getConfig().getMaxNumRecordsPerBlock(), dbs
                                .getConfig().getMaxBlockFileSize(),
                                dbs.getConfig().getDisableMMap(), dbs.getConfig().getMMapLimit()));
                        dbsById.put(dbId, db);
                        dbsByName.put(operation.getDatabaseName(), db);
                        dbs.getDBConfigFile().save();
                    }
                }
               
                return db;
            }
        });
       
        dbs.getTransactionManager().registerInMemoryProcessing(Operation.TYPE_DELETE_DB,
                new InMemoryProcessing() {
                       
            @Override
            public Object[] deserializeRequest(ReusableBuffer serialized) throws BabuDBException {
               
                serialized.getInt(); // do not use, deprecated
               
                String dbName = serialized.getString();
                serialized.flip();
               
                return new Object[] { dbName };
            }
           
            @Override
            public OperationInternal convertToOperation(Object[] args) {
                return new BabuDBTransaction.BabuDBOperation(Operation.TYPE_DELETE_DB, (String) args[0],
                        null);
            }

            @Override
            public Object process(OperationInternal operation) throws BabuDBException {
               
                int dbId = InsertRecordGroup.DB_ID_UNKNOWN;
                synchronized (getDBModificationLock()) {
                    synchronized (dbs.getCheckpointer()) {
                        if (!dbsByName.containsKey(operation.getDatabaseName())) {
                            throw new BabuDBException(ErrorCode.NO_SUCH_DB, "database '" +
                                    operation.getDatabaseName() + "' does not exists");
                        }
                        final LSMDatabase db = getDatabase(operation.getDatabaseName()).getLSMDB();
                        dbId = db.getDatabaseId();
                        dbsByName.remove(operation.getDatabaseName());
                        dbsById.remove(dbId);
                       
                        dbs.getSnapshotManager().deleteAllSnapshots(operation.getDatabaseName());
                       
                        dbs.getDBConfigFile().save();
                        File dbDir = new File(dbs.getConfig().getBaseDir(),
                                operation.getDatabaseName());
                       
                        if (dbDir.exists()) {
                            FSUtils.delTree(dbDir);
                        }
                    }
                }
               
                return null;
            }
        });
       
        dbs.getTransactionManager().registerInMemoryProcessing(Operation.TYPE_COPY_DB,
                new InMemoryProcessing() {
                       
            @Override
            public Object[] deserializeRequest(ReusableBuffer serialized) throws BabuDBException {
               
                serialized.getInt(); // do not use, deprecated
                serialized.getInt(); // do not use, deprecated
               
                String sourceDB = serialized.getString();
                String destDB = serialized.getString();
               
                serialized.flip();
               
                return new Object[] { sourceDB, destDB };
            }
           
            @Override
            public OperationInternal convertToOperation(Object[] args) {
                return new BabuDBTransaction.BabuDBOperation(Operation.TYPE_COPY_DB, (String) args[0],
                        new Object[] { args[1] });
            }

            @Override
            public Object process(OperationInternal operation) throws BabuDBException {
               
                // parse args
                String destDB = (String) operation.getParams()[0];
               
                DatabaseInternal sDB = getDatabase(operation.getDatabaseName());
               
                int dbId;
                synchronized (getDBModificationLock()) {
                    synchronized (dbs.getCheckpointer()) {
                        if (dbsByName.containsKey(destDB)) {
                            throw new BabuDBException(ErrorCode.DB_EXISTS, "database '" + destDB
                                + "' already exists");
                        }
                        dbId = nextDbId++;
                        // just "reserve" the name
                        dbsByName.put(destDB, null);
                        dbs.getDBConfigFile().save();
                    }
                }
                // materializing the snapshot takes some time, we should not
                // hold the
                // lock meanwhile!
                try {
                    sDB.proceedSnapshot(destDB);
                } catch (InterruptedException i) {
                    throw new BabuDBException(ErrorCode.INTERNAL_ERROR,
                            "Snapshot creation was interrupted.", i);
                }
               
                // create new DB and load from snapshot
                DatabaseInternal newDB = new DatabaseImpl(dbs, new LSMDatabase(destDB, dbId,
                        dbs.getConfig().getBaseDir() + destDB + File.separatorChar,
                        sDB.getLSMDB().getIndexCount(), true, sDB.getComparators(),
                        dbs.getConfig().getCompression(),
                        dbs.getConfig().getMaxNumRecordsPerBlock(),
                        dbs.getConfig().getMaxBlockFileSize(), dbs.getConfig().getDisableMMap(),
                        dbs.getConfig().getMMapLimit()));
               
                // insert real database
                synchronized (dbModificationLock) {
                    dbsById.put(dbId, newDB);
                    dbsByName.put(destDB, newDB);
                    dbs.getDBConfigFile().save();
                }
               
                return null;
            }
        });
       
        dbs.getTransactionManager().registerInMemoryProcessing(Operation.TYPE_GROUP_INSERT,
                new InMemoryProcessing() {
                       
            @Override
            public Object[] deserializeRequest(ReusableBuffer serialized) throws BabuDBException {
               
                InsertRecordGroup irg = InsertRecordGroup.deserialize(serialized);
View Full Code Here

                    } else if (type != PAYLOAD_TYPE_CREATE && type != PAYLOAD_TYPE_COPY
                        && type != PAYLOAD_TYPE_DELETE) {
                       
                        // get the processing logic for the dedicated logEntry
                        // type
                        InMemoryProcessing processingLogic = txnMan.getProcessingLogic().get(type);
                       
                        // deserialize the arguments retrieved from the logEntry
                        OperationInternal operation = processingLogic.convertToOperation(processingLogic
                                .deserializeRequest(le.getPayload()));
                       
                        // execute the in-memory logic
                        try {
                            processingLogic.process(operation);
                        } catch (BabuDBException be) {
                           
                            // there might be false positives if a snapshot to
                            // delete has already been deleted, a snapshot to
                            // create has already been created, or an insertion
View Full Code Here

            if (type != Operation.TYPE_COPY_DB &&
                type != Operation.TYPE_CREATE_DB &&
                type != Operation.TYPE_DELETE_DB) {
               
                // get processing logic
                InMemoryProcessing processing = inMemoryProcessing.get(type);
               
                // replay in-memory changes
                try {
                    processing.process(operation);  
                } catch (BabuDBException be) {
                   
                    // there might be false positives if a snapshot to delete has already been
                    // deleted or a snapshot to create has already been created.
                    // also there could be inserts for databases that have been deleted already.
View Full Code Here

TOP

Related Classes of org.xtreemfs.babudb.api.dev.transaction.InMemoryProcessing

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.