private static StoreParams roundTrip(StoreParams params) { JsonObject obj = StoreParamsCodec.encodeToJson(params) ; StoreParams params2 = StoreParamsCodec.decode(obj) ; return params2 ; }
/** * Read from a file if possible. * Return null for memory locations, file not found or syntax errors. */ public static StoreParams read(Location location) { if ( location.isMem() ) return null ; return read(location.getPath(Names.TDB_CONFIG_FILE)) ; }
/** Write to a file */ public static void write(Location location, StoreParams params) { write(location.getPath(Names.TDB_CONFIG_FILE) ,params) ; }
public static JsonObject encodeToJson(StoreParams params) { JsonBuilder builder = new JsonBuilder() ; builder.startObject("StoreParams") ; // "StoreParams" is an internal alignment marker - not in the JSON. encode(builder, key(fFileMode), params.getFileMode().name()) ; encode(builder, key(fBlockSize), params.getBlockSize()) ; encode(builder, key(fBlockReadCacheSize), params.getBlockReadCacheSize()) ; encode(builder, key(fBlockWriteCacheSize), params.getBlockWriteCacheSize()) ; encode(builder, key(fNode2NodeIdCacheSize), params.getNode2NodeIdCacheSize()) ; encode(builder, key(fNodeId2NodeCacheSize), params.getNodeId2NodeCacheSize()) ; encode(builder, key(fNodeMissCacheSize), params.getNodeMissCacheSize()) ; encode(builder, key(fNodeTableBaseName), params.getNodeTableBaseName()) ; encode(builder, key(fPrimaryIndexTriples), params.getPrimaryIndexTriples()) ; encode(builder, key(fTripleIndexes), params.getTripleIndexes()) ; encode(builder, key(fPrimaryIndexQuads), params.getPrimaryIndexQuads()) ; encode(builder, key(fQuadIndexes), params.getQuadIndexes()) ; encode(builder, key(fPrefixTableBaseName), params.getPrefixTableBaseName()) ; encode(builder, key(fPrimaryIndexPrefix), params.getPrimaryIndexPrefix()) ; encode(builder, key(fPrefixIndexes), params.getPrefixIndexes()) ; builder.finishObject("StoreParams") ; return (JsonObject)builder.build() ; }
public static StoreParams decode(JsonObject json) { StoreParamsBuilder builder = StoreParams.builder() ; for ( String key : json.keys() ) { String short_key = unkey(key) ; switch(short_key) { case fFileMode : builder.fileMode(FileMode.valueOf(getString(json, key))) ; break ; case fBlockSize: builder.blockSize(getInt(json, key)) ; break ; case fBlockReadCacheSize: builder.blockReadCacheSize(getInt(json, key)) ; break ; case fBlockWriteCacheSize: builder.blockWriteCacheSize(getInt(json, key)) ; break ; case fNode2NodeIdCacheSize: builder.node2NodeIdCacheSize(getInt(json, key)) ; break ; case fNodeId2NodeCacheSize: builder.nodeId2NodeCacheSize(getInt(json, key)) ; break ; case fNodeMissCacheSize: builder.nodeMissCacheSize(getInt(json, key)) ; break ; case fNodeTableBaseName: builder.nodeTableBaseName(getString(json, key)) ; break ; case fPrimaryIndexTriples: builder.primaryIndexTriples(getString(json, key)) ; break ; case fTripleIndexes: builder.tripleIndexes(getStringArray(json, key)) ; break ; case fPrimaryIndexQuads: builder.primaryIndexQuads(getString(json, key)) ; break ; case fQuadIndexes: builder.quadIndexes(getStringArray(json, key)) ; break ; case fPrefixTableBaseName: builder.prefixTableBaseName(getString(json, key)) ; break ; case fPrimaryIndexPrefix: builder.primaryIndexPrefix(getString(json, key)) ; break ; case fPrefixIndexes: builder.prefixIndexes(getStringArray(json, key)) ; break ; default: throw new TDBException("StoreParams key no recognized: "+key) ; } } return builder.build() ; }
@Test public void params_choose_new_persist_2() { // new database, location defined. Location loc = Location.create(DIR) ; FileOps.clearAll(loc.getDirectoryPath()); StoreParamsCodec.write(loc, pLoc); // Clear. StoreParams p = StoreParamsFactory.decideStoreParams(loc, true, null, pLoc, pDft) ; // Check location still has a pLoc. String fn = loc.getPath(Names.TDB_CONFIG_FILE) ; assertTrue(FileOps.exists(fn)) ; StoreParams pLoc2 = StoreParamsCodec.read(loc) ; assertTrue(StoreParams.sameValues(pLoc, p)) ; }
static StoreParams read(Location location) { String fn = location.getPath(Names.TDB_CONFIG_FILE) ; JsonObject obj = JSON.read(fn) ; return StoreParamsCodec.decode(obj) ; } }
/** Write to a file */ public static void write(String filename, StoreParams params) { try (OutputStream out = new FileOutputStream(filename); OutputStream out2 = new BufferedOutputStream(out); ) { JsonObject object = encodeToJson(params) ; JSON.write(out2, object) ; out2.write('\n') ; } catch (IOException ex) { IO.exception(ex); } }
@Test public void params_choose_new_persist_2() { // new database, location defined. Location loc = Location.create(DIR) ; FileOps.clearAll(loc.getDirectoryPath()); StoreParamsCodec.write(loc, pLoc); // Clear. StoreParams p = StoreParamsFactory.decideStoreParams(loc, true, null, pLoc, pDft) ; // Check location still has a pLoc. String fn = loc.getPath(Names.TDB_CONFIG_FILE) ; assertTrue(FileOps.exists(fn)) ; StoreParams pLoc2 = StoreParamsCodec.read(loc) ; assertTrue(StoreParams.sameValues(pLoc, p)) ; }
static StoreParams read(Location location) { String fn = location.getPath(Names.TDB_CONFIG_FILE) ; JsonObject obj = JSON.read(fn) ; return StoreParamsCodec.decode(obj) ; } }
@Test public void params_choose_new_persist_3() { // new database, location defined, application modified. Location loc = Location.create(DIR) ; FileOps.clearAll(loc.getDirectoryPath()); StoreParamsCodec.write(loc, pLoc); // Clear. StoreParams p = StoreParamsFactory.decideStoreParams(loc, true, pApp, pLoc, pDft) ; // Check location still has a pLoc. String fn = loc.getPath(Names.TDB_CONFIG_FILE) ; assertTrue(FileOps.exists(fn)) ; StoreParams pLoc2 = StoreParamsCodec.read(loc) ; assertFalse(StoreParams.sameValues(pLoc, p)) ; assertEquals(0, p.getBlockSize().intValue()) ; // Location assertEquals(12, p.getNodeMissCacheSize().intValue()) ; // Application }
@Test public void store_params_13() { String xs = "{ \"tdb.triple_indexes\" : [ \"POS\" , \"PSO\"] } " ; JsonObject x = JSON.parse(xs) ; StoreParams params = StoreParamsCodec.decode(x) ; String[] expected = { "POS" , "PSO" } ; assertArrayEquals(expected, params.getTripleIndexes()) ; }
private static StoreParams roundTrip(StoreParams params) { JsonObject obj = StoreParamsCodec.encodeToJson(params) ; StoreParams params2 = StoreParamsCodec.decode(obj) ; return params2 ; }
public static DatasetGraphTxn build(Location location, StoreParams appParams) { StoreParams locParams = StoreParamsCodec.read(location); StoreParams dftParams = StoreParams.getDftStoreParams(); // This can write the chosen parameters if necessary (new database, appParams != null, locParams == null) boolean newArea = isNewDatabaseArea(location); if ( newArea ) { } StoreParams params = StoreParamsFactory.decideStoreParams(location, newArea, appParams, locParams, dftParams); return create(location, params).build$(); }
if ( ! location.isMem() ) { String filename = location.getPath(Names.TDB_CONFIG_FILE) ; StoreParamsCodec.write(filename, pApp) ;
@Test public void params_choose_new_persist_3() { // new database, location defined, application modified. Location loc = Location.create(DIR) ; FileOps.clearAll(loc.getDirectoryPath()); StoreParamsCodec.write(loc, pLoc); // Clear. StoreParams p = StoreParamsFactory.decideStoreParams(loc, true, pApp, pLoc, pDft) ; // Check location still has a pLoc. String fn = loc.getPath(Names.TDB_CONFIG_FILE) ; assertTrue(FileOps.exists(fn)) ; StoreParams pLoc2 = StoreParamsCodec.read(loc) ; assertFalse(StoreParams.sameValues(pLoc, p)) ; assertEquals(0, p.getBlockSize().intValue()) ; // Location assertEquals(12, p.getNodeMissCacheSize().intValue()) ; // Application }
@Test(expected=TDBException.class) public void store_params_14() { String xs = "{ \"tdb.triples_indexes\" : [ \"POS\" , \"PSO\"] } " ; // Misspelt. JsonObject x = JSON.parse(xs) ; StoreParams params = StoreParamsCodec.decode(x) ; String[] expected = { "POS" , "PSO" } ; assertArrayEquals(expected, params.getTripleIndexes()) ; }
@Test public void params_create_02() { StoreConnection.connectCreate(loc, pApp) ; // Check. Custom setup. assertTrue("DB directory", Files.exists(db)) ; assertTrue("Config file not found", Files.exists(cfg)) ; StoreParams pLoc = StoreParamsCodec.read(loc) ; assertTrue(StoreParams.sameValues(pLoc, pApp)) ; }
/** * Read from a file if possible. * Return null if the file is not found or has a syntax error. */ public static StoreParams read(String filename) { try { InputStream in = IO.openFileEx(filename); if ( in == null ) return null; JsonObject obj = JSON.parse(in) ; return StoreParamsCodec.decode(obj) ; } catch (FileNotFoundException ex) { return null; } catch (JsonParseException ex) { FmtLog.warn(StoreParamsCodec.class, "Ignoring store params : Syntax error in '%s': [line:%d, col:%d] %s", filename, ex.getLine(), ex.getColumn(), ex.getMessage()); return null ; } catch (IOException e) { IO.exception(e); return null; } }
@Test public void params_create_02() { StoreConnection.connectCreate(loc, pApp) ; // Check. Custom setup. assertTrue("DB directory", Files.exists(db)) ; assertTrue("Config file not found", Files.exists(cfg)) ; StoreParams pLoc = StoreParamsCodec.read(loc) ; assertTrue(StoreParams.sameValues(pLoc, pApp)) ; }