Search in sources :

Example 1 with CachedOffHeapStorage

use of org.nustaq.reallive.impl.storage.CachedOffHeapStorage in project kontraktor by RuedigerMoeller.

the class EmbeddedRealLive method createTable.

/**
 * WARNING: never create more than one table using the same file. This will
 * result in corrupted data for sure. As actor refs (tables) are thread save,
 * just init a singleton containing all your tables once.
 *
 * @param desc
 * @param dataDir - if null use path from description
 * @return a thread save actor reference to a newly loaded or created table
 */
public IPromise<RealLiveTable> createTable(TableDescription desc, String dataDir) {
    RealLiveTableActor table = Actors.AsActor(RealLiveTableActor.class);
    Supplier<RecordStorage> memFactory;
    if (desc.getFilePath() == null) {
        Log.Info(this, "no file specified. all data in memory " + desc.getName());
        switch(desc.getStorageType()) {
            case CACHED:
                memFactory = () -> new CachedOffHeapStorage(new OffHeapRecordStorage(desc.getKeyLen(), desc.getSizeMB(), desc.getNumEntries()), new HeapRecordStorage());
                break;
            default:
                Log.Error(this, "unknown storage type " + desc.getStorageType() + " default to PERSIST");
            case PERSIST:
                memFactory = () -> new OffHeapRecordStorage(desc.getKeyLen(), desc.getSizeMB(), desc.getNumEntries());
                break;
            case TEMP:
                memFactory = () -> new HeapRecordStorage();
                break;
        }
    } else {
        String bp = dataDir == null ? desc.getFilePath() : dataDir;
        desc.filePath(bp);
        new File(bp).mkdirs();
        String file = bp + "/" + desc.getName() + "_" + desc.getShardNo() + ".bin";
        switch(desc.getStorageType()) {
            case CACHED:
                Log.Info(this, "memory mapping file " + file);
                memFactory = () -> new CachedOffHeapStorage(new OffHeapRecordStorage(file, desc.getKeyLen(), desc.getSizeMB(), desc.getNumEntries()), new HeapRecordStorage());
                break;
            default:
                Log.Error(this, "unknown storage type " + desc.getStorageType() + " default to PERSIST");
            case PERSIST:
                Log.Info(this, "memory mapping file " + file);
                memFactory = () -> new OffHeapRecordStorage(file, desc.getKeyLen(), desc.getSizeMB(), desc.getNumEntries());
                break;
            case TEMP:
                memFactory = () -> new HeapRecordStorage();
                break;
        }
    }
    table.init(memFactory, desc).await(30_000);
    return new Promise(table);
}
Also used : Promise(org.nustaq.kontraktor.Promise) IPromise(org.nustaq.kontraktor.IPromise) OffHeapRecordStorage(org.nustaq.reallive.impl.storage.OffHeapRecordStorage) HeapRecordStorage(org.nustaq.reallive.impl.storage.HeapRecordStorage) OffHeapRecordStorage(org.nustaq.reallive.impl.storage.OffHeapRecordStorage) RecordStorage(org.nustaq.reallive.api.RecordStorage) HeapRecordStorage(org.nustaq.reallive.impl.storage.HeapRecordStorage) File(java.io.File) CachedOffHeapStorage(org.nustaq.reallive.impl.storage.CachedOffHeapStorage) OffHeapRecordStorage(org.nustaq.reallive.impl.storage.OffHeapRecordStorage) RealLiveTableActor(org.nustaq.reallive.impl.actors.RealLiveTableActor)

Aggregations

File (java.io.File)1 IPromise (org.nustaq.kontraktor.IPromise)1 Promise (org.nustaq.kontraktor.Promise)1 RecordStorage (org.nustaq.reallive.api.RecordStorage)1 RealLiveTableActor (org.nustaq.reallive.impl.actors.RealLiveTableActor)1 CachedOffHeapStorage (org.nustaq.reallive.impl.storage.CachedOffHeapStorage)1 HeapRecordStorage (org.nustaq.reallive.impl.storage.HeapRecordStorage)1 OffHeapRecordStorage (org.nustaq.reallive.impl.storage.OffHeapRecordStorage)1