/** * Get the {@link SchemaModel} associated with this instance, derived from the annotations on the scanned classes. * * @return the associated schema model */ public SchemaModel getSchemaModel() { if (this.schemaModel == null) { final SchemaModel model = new SchemaModel(); for (JClass<?> jclass : this.jclasses.values()) { final SchemaObjectType schemaObjectType = jclass.toSchemaItem(this); model.getSchemaObjectTypes().put(schemaObjectType.getStorageId(), schemaObjectType); } this.schemaModel = model; this.schemaModel.lockDown(); this.log.debug("JSimpleDB schema generated from annotated classes:\n{}", this.schemaModel); } return this.schemaModel; }
/** * Change the schema version that this instance is configured to use. * * @param version schema version number of the schema derived from {@code classes}, * zero to use the highest version already recorded in the database, * or -1 to use an {@linkplain SchemaModel#autogenerateVersion auto-generated} schema version * @throws IllegalArgumentException if {@code version} is less than -1 */ public void setConfiguredVersion(int version) { Preconditions.checkArgument(version >= -1, "invalid schema version"); this.configuredVersion = version == -1 ? this.schemaModel.autogenerateVersion() : version; }
/** * Deserialize an instance from the given XML input and validate it. * * @param input XML input * @return deserialized schema model * @throws IOException if an I/O error occurs * @throws InvalidSchemaException if the XML input or decoded {@link SchemaModel} is invalid * @throws IllegalArgumentException if {@code input} is null */ public static SchemaModel fromXML(InputStream input) throws IOException { Preconditions.checkArgument(input != null, "null input"); final SchemaModel schemaModel = new SchemaModel(); try { final XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(input); schemaModel.readXML(reader); } catch (XMLStreamException e) { throw new InvalidSchemaException("error parsing schema model XML", e); } schemaModel.validate(); return schemaModel; }
Preconditions.checkArgument(schemaModel != null || version >= 0, "can't auto-generate version without schema model"); if (schemaModel != null) { schemaModel.validate(); if (version == -1) version = schemaModel.autogenerateVersion(); final SchemaModel dbSchemaModel = schemas.getVersion(version).getSchemaModel(); if (schemaModel != null) { if (!schemaModel.isCompatibleWith(dbSchemaModel)) { final Diffs diffs = schemaModel.differencesFrom(dbSchemaModel); this.log.error("schema mismatch:\n=== Database schema ===\n{}\n=== Provided schema ===\n{}" + "\n=== Differences ===\n{}", dbSchemaModel, schemaModel, diffs); throw new IllegalArgumentException("the provided schema is not compatible with the schema already recorded" + " in the database under version " + version + ":\n" + diffs); } else if (this.log.isTraceEnabled() && !schemaModel.equals(dbSchemaModel)) { final Diffs diffs = schemaModel.differencesFrom(dbSchemaModel); this.log.trace("the provided schema differs from, but is compatible with, the database schema:\n{}", diffs);
if (!schema.equals(forcedSchema)) { if (forcedSchema != null) { final byte[] newValue = Layout.encodeSchema(forcedSchema, info.getFormatVersion()); info.handle(new InvalidValue(pair, newValue).setDetail("forcibly override schema version " + version + " with provided version having these differences: " + forcedSchema.differencesFrom(schema))); schema = forcedSchema; } else { if ((objectType = schema.getSchemaObjectTypes().get(id.getStorageId())) == null) { info.handle(new InvalidValue(pair).setDetail("invalid object ID " + id + " with storage ID " + id.getStorageId() + ": no such object type exists in schema version "
try { try (InputStream input = new BufferedInputStream(new FileInputStream(this.schemaFile))) { schemaModel = SchemaModel.fromXML(input); if (!schemaModel.equals(jdb.getSchemaModel())) { System.err.println(this.getName() + ": schema from `" + this.schemaFile + "' conflicts with schema generated" + " from scanned classes"); System.err.println(schemaModel.differencesFrom(jdb.getSchemaModel())); return 1;
this.encodedXML = encodedXML; final SchemaModel lockedDownSchemaModel; if (schemaModel.isLockedDown()) lockedDownSchemaModel = schemaModel; else { lockedDownSchemaModel = schemaModel.clone(); lockedDownSchemaModel.lockDown(); for (SchemaObjectType schemaObjectType : this.schemaModel.getSchemaObjectTypes().values()) { final ObjType objType = new ObjType(schemaObjectType, this, fieldTypeRegistry); final int storageId = objType.getStorageId();
private void inventoryStorages(int schemaVersion, SchemaModel schema) { for (SchemaObjectType objectType : schema.getSchemaObjectTypes().values()) this.inventoryStorages(schemaVersion, objectType); }
&& !srcSchema.schemaModel.isCompatibleWith(dstSchema.schemaModel)) { throw new SchemaMismatchException("destination transaction schema version " + objectVersion + " does not match source schema version " + objectVersion + "\n" + dstSchema.schemaModel.differencesFrom(srcSchema.schemaModel));
@Override public void run(CliSession session) throws Exception { final SchemaModel schema1 = AbstractSchemaCommand.getSchemaModel(session, this.version1); final SchemaModel schema2 = AbstractSchemaCommand.getSchemaModel(session, this.version2); if (schema1 == null || schema2 == null) return; final String desc1 = this.version1 == 0 ? "the schema configured on this session" : "schema version " + this.version1; final String desc2 = this.version2 == 0 ? "the schema configured on this session" : "schema version " + this.version2; final Diffs diffs = schema2.differencesFrom(schema1); if (diffs.isEmpty()) session.getWriter().println("No differences found between " + desc1 + " and " + desc2); else session.getWriter().println("Found differences found between " + desc1 + " and " + desc2 + "\n" + diffs); } }
/** * Get the {@link NameIndex} for this instance's {@link SchemaModel}. * * @return name index for the schema model assumed by this session */ public NameIndex getNameIndex() { return this.nameIndex != null ? this.nameIndex : new NameIndex(new SchemaModel()); }
return SchemaModel.fromXML(new ByteArrayInputStream(value)); } catch (IOException e) { throw new RuntimeException("unexpected exception", e);
private int write(XMLStreamWriter writer, boolean nameFormat) throws XMLStreamException { // Gather all known object storage IDs final TreeSet<Integer> storageIds = new TreeSet<>(); for (Schema schema : this.tx.getSchemas().getVersions().values()) storageIds.addAll(schema.getSchemaModel().getSchemaObjectTypes().keySet()); // Get corresponding object sets final ArrayList<NavigableSet<ObjId>> sets = storageIds.stream() .map(this.tx::getAll) .collect(Collectors.toCollection(() -> new ArrayList<>(storageIds.size()))); // Output all objects return this.write(writer, nameFormat, Iterables.concat(sets)); }
for (SchemaObjectType type : schemaModel.getSchemaObjectTypes().values()) {
this.configuredVersion = version == -1 ? this.schemaModel.autogenerateVersion() : version;
@Override public void run(CliSession session) throws Exception { final PrintWriter writer = session.getWriter(); writer.println(" CLI Mode: " + session.getMode()); writer.println(" Database: " + session.getDatabaseDescription()); writer.println(" Access Mode: " + (session.isReadOnly() ? "Read-Only" : "Read/Write")); writer.println(" Verbose Mode: " + session.isVerbose()); if (session.getMode().equals(SessionMode.KEY_VALUE)) return; final int schemaVersion = InfoCommand.getSchemaVersion(session); writer.println(" Schema Version: " + (schemaVersion != 0 ? schemaVersion : "Undefined")); final SchemaModel schemaModel = InfoCommand.getSchemaModel(session); writer.println(" Schema Model: " + (schemaModel != null ? schemaModel.getSchemaObjectTypes().size() + " object type(s)" : "Undefined")); writer.println(" New Schema Allowed: " + (session.isAllowNewSchema() ? "Yes" : "No")); if (session.getJSimpleDB() != null) { writer.println(" Validation Mode: " + (session.getValidationMode() != null ? session.getValidationMode() : ValidationMode.AUTOMATIC)); } }
case CORE_API: if (this.schemaVersion == -1) this.schemaVersion = this.schemaModel.autogenerateVersion(); this.tx = this.db.createTransaction(this.schemaModel, this.schemaVersion, this.allowNewSchema, options); this.kvt = this.tx.getKVTransaction();
final SchemaObjectType schemaObjectType = schemaModel.getSchemaObjectTypes().get(objType.getStorageId()); while ((name = this.next(reader)) != null) { final QName fieldTagName = name;