ignite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From vkuliche...@apache.org
Subject [42/50] incubator-ignite git commit: # IGNITE-380 Added snippet generation.
Date Tue, 03 Mar 2015 20:28:46 GMT
# IGNITE-380 Added snippet generation.


Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/7488eddd
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/7488eddd
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/7488eddd

Branch: refs/heads/ignite-141
Commit: 7488eddd6e0224d18ecdd9b7fb51566e926ead5d
Parents: b2675bc
Author: AKuznetsov <akuznetsov@gridgain.com>
Authored: Tue Mar 3 14:49:27 2015 +0700
Committer: AKuznetsov <akuznetsov@gridgain.com>
Committed: Tue Mar 3 14:49:27 2015 +0700

----------------------------------------------------------------------
 .../ignite/cache/CacheTypeFieldMetadata.java    |   2 +-
 .../apache/ignite/cache/CacheTypeMetadata.java  |  18 +--
 .../ignite/schema/generator/PojoGenerator.java  |   3 +-
 .../schema/generator/SnippetGenerator.java      | 138 +++++++++++++++++++
 .../apache/ignite/schema/ui/SchemaLoadApp.java  |   2 +
 5 files changed, 151 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/7488eddd/modules/core/src/main/java/org/apache/ignite/cache/CacheTypeFieldMetadata.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/cache/CacheTypeFieldMetadata.java
b/modules/core/src/main/java/org/apache/ignite/cache/CacheTypeFieldMetadata.java
index 0041f10..625873b 100644
--- a/modules/core/src/main/java/org/apache/ignite/cache/CacheTypeFieldMetadata.java
+++ b/modules/core/src/main/java/org/apache/ignite/cache/CacheTypeFieldMetadata.java
@@ -50,7 +50,7 @@ public class CacheTypeFieldMetadata {
      * @param javaName Field name in java object.
      * @param javaType Field java type.
      */
-    public CacheTypeFieldMetadata(String javaName, Class<?> javaType, String dbName,
int dbType) {
+    public CacheTypeFieldMetadata(String dbName, int dbType, String javaName, Class<?>
javaType) {
         this.dbName = dbName;
         this.dbType = dbType;
         this.javaName = javaName;

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/7488eddd/modules/core/src/main/java/org/apache/ignite/cache/CacheTypeMetadata.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/cache/CacheTypeMetadata.java b/modules/core/src/main/java/org/apache/ignite/cache/CacheTypeMetadata.java
index 1e502c9..f25d9f4 100644
--- a/modules/core/src/main/java/org/apache/ignite/cache/CacheTypeMetadata.java
+++ b/modules/core/src/main/java/org/apache/ignite/cache/CacheTypeMetadata.java
@@ -230,6 +230,15 @@ public class CacheTypeMetadata {
     }
 
     /**
+     * Sets value fields.
+     *
+     * @param valFields New value fields.
+     */
+    public void setValueFields(Collection<CacheTypeFieldMetadata> valFields) {
+        this.valFields = valFields;
+    }
+
+    /**
      * Gets query-enabled fields.
      *
      * @return Collection of fields available for query.
@@ -318,13 +327,4 @@ public class CacheTypeMetadata {
     public void setGroups(Map<String, LinkedHashMap<String, IgniteBiTuple<Class<?>,
Boolean>>> grps) {
         this.grps = grps;
     }
-
-    /**
-     * Sets value fields.
-     *
-     * @param valFields New value fields.
-     */
-    public void setValueFields(Collection<CacheTypeFieldMetadata> valFields) {
-        this.valFields = valFields;
-    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/7488eddd/modules/schema-load/src/main/java/org/apache/ignite/schema/generator/PojoGenerator.java
----------------------------------------------------------------------
diff --git a/modules/schema-load/src/main/java/org/apache/ignite/schema/generator/PojoGenerator.java
b/modules/schema-load/src/main/java/org/apache/ignite/schema/generator/PojoGenerator.java
index 7d12e2c..500aa9a 100644
--- a/modules/schema-load/src/main/java/org/apache/ignite/schema/generator/PojoGenerator.java
+++ b/modules/schema-load/src/main/java/org/apache/ignite/schema/generator/PojoGenerator.java
@@ -401,8 +401,7 @@ public class PojoGenerator {
      * @throws IOException If failed to write generated code into file.
      */
     public static void generate(PojoDescriptor pojo, String outFolder, String pkg, boolean
constructor,
-        boolean includeKeys, ConfirmCallable askOverwrite)
-        throws IOException {
+        boolean includeKeys, ConfirmCallable askOverwrite) throws IOException {
         File pkgFolder = new File(outFolder, pkg.replace('.', File.separatorChar));
 
         if (!pkgFolder.exists() && !pkgFolder.mkdirs())

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/7488eddd/modules/schema-load/src/main/java/org/apache/ignite/schema/generator/SnippetGenerator.java
----------------------------------------------------------------------
diff --git a/modules/schema-load/src/main/java/org/apache/ignite/schema/generator/SnippetGenerator.java
b/modules/schema-load/src/main/java/org/apache/ignite/schema/generator/SnippetGenerator.java
new file mode 100644
index 0000000..e54c019
--- /dev/null
+++ b/modules/schema-load/src/main/java/org/apache/ignite/schema/generator/SnippetGenerator.java
@@ -0,0 +1,138 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.schema.generator;
+
+import org.apache.ignite.schema.model.*;
+import org.apache.ignite.schema.ui.*;
+
+import java.io.*;
+import java.util.*;
+
+import static org.apache.ignite.schema.ui.MessageBox.Result.*;
+
+/**
+ * Cache configuration snippet generator.
+ */
+public class SnippetGenerator {
+    /**
+     * Add type fields.
+     *
+     * @param src Source code lines.
+     * @param owner Fields owner collection.
+     * @param fields Fields metadata.
+     */
+    private static void addFields(Collection<String> src, String owner, Collection<PojoField>
fields) {
+        for (PojoField field : fields) {
+            String javaTypeName = field.javaTypeName();
+
+            if (javaTypeName.startsWith("java.lang."))
+                javaTypeName = javaTypeName.substring(10);
+
+            src.add(owner + ".add(new CacheTypeFieldMetadata(\"" + field.dbName() + "\",
" +
+                "java.sql.Types." + field.dbTypeName() + ",\"" +
+                field.javaName() + "\", " + javaTypeName + ".class));");
+        }
+    }
+
+    /**
+     * Generate java snippet for cache configuration with JDBC store.
+     *
+     * @param pojos POJO descriptors.
+     * @param pkg Types package.
+     * @param includeKeys {@code true} if key fields should be included into value class.
+     * @param out File to output snippet.
+     * @param askOverwrite Callback to ask user to confirm file overwrite.
+     * @throws IOException If generation failed.
+     */
+    public static void generate(Collection<PojoDescriptor> pojos, String pkg, boolean
includeKeys, File out,
+        ConfirmCallable askOverwrite) throws IOException {
+        if (out.exists()) {
+            MessageBox.Result choice = askOverwrite.confirm(out.getName());
+
+            if (CANCEL == choice)
+                throw new IllegalStateException("Java configuration snippet generation was
canceled!");
+
+            if (NO == choice || NO_TO_ALL == choice)
+                return;
+        }
+
+        Collection<String> src = new ArrayList<>(256);
+
+        src.add("// Code snippet for cache configuration.");
+        src.add("");
+        src.add("IgniteConfiguration cfg = new IgniteConfiguration();");
+        src.add("");
+        src.add("CacheConfiguration ccfg = new CacheConfiguration<>();");
+        src.add("");
+        src.add("DataSource dataSource = null; // TODO: Create data source for your database.");
+        src.add("");
+        src.add("// Create store. ");
+        src.add("CacheJdbcPojoStore store = new CacheJdbcPojoStore();");
+        src.add("store.setDataSource(dataSource);");
+        src.add("");
+        src.add("// Create store factory. ");
+        src.add("ccfg.setCacheStoreFactory(new FactoryBuilder.SingletonFactory<>(store));");
+        src.add("");
+        src.add("// Configure cache to use store. ");
+        src.add("ccfg.setReadThrough(true);");
+        src.add("ccfg.setWriteThrough(true);");
+        src.add("");
+        src.add("cfg.setCacheConfiguration(ccfg);");
+        src.add("");
+        src.add("// Configure cache types. ");
+        src.add("Collection<CacheTypeMetadata> meta = new ArrayList<>();");
+        src.add("");
+
+        boolean first = true;
+
+        for (PojoDescriptor pojo : pojos) {
+            String tbl = pojo.table();
+
+            src.add("// " + tbl + ".");
+            src.add((first ? "CacheTypeMetadata " : "") +  "type = new CacheTypeMetadata();");
+            src.add("type.setDatabaseSchema(\"" + pojo.schema() + "\");");
+            src.add("type.setDatabaseTable(\"" + tbl + "\");");
+            src.add("type.setKeyType(\"" + pkg + "." + pojo.keyClassName() + "\");");
+            src.add("type.setValueType(\"" +  pkg + "." + pojo.valueClassName() + "\");");
+            src.add("");
+
+            src.add("// Key fields for " + tbl + ".");
+            src.add((first ? "Collection<CacheTypeFieldMetadata> " : "") + "keys =
new ArrayList<>();");
+            addFields(src, "keys", pojo.valueFields(includeKeys));
+            src.add("type.setKeyFields(keys);");
+            src.add("");
+
+            src.add("// Value fields for " + tbl + ".");
+            src.add((first ? "Collection<CacheTypeFieldMetadata> " : "") + "vals =
new ArrayList<>();");
+            addFields(src, "vals", pojo.valueFields(includeKeys));
+            src.add("type.setValueFields(vals);");
+            src.add("");
+
+            first = false;
+        }
+
+        src.add("// Start Ignite node.");
+        src.add("Ignition.start(cfg);");
+
+        // Write generated code to file.
+        try (Writer writer = new BufferedWriter(new FileWriter(out))) {
+            for (String line : src)
+                writer.write(line + '\n');
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/7488eddd/modules/schema-load/src/main/java/org/apache/ignite/schema/ui/SchemaLoadApp.java
----------------------------------------------------------------------
diff --git a/modules/schema-load/src/main/java/org/apache/ignite/schema/ui/SchemaLoadApp.java
b/modules/schema-load/src/main/java/org/apache/ignite/schema/ui/SchemaLoadApp.java
index 9c88624..f43c5ca 100644
--- a/modules/schema-load/src/main/java/org/apache/ignite/schema/ui/SchemaLoadApp.java
+++ b/modules/schema-load/src/main/java/org/apache/ignite/schema/ui/SchemaLoadApp.java
@@ -435,6 +435,8 @@ public class SchemaLoadApp extends Application {
                 if (singleXml)
                     XmlGenerator.generate(pkg, all, includeKeys, new File(outFolder, "Ignite.xml"),
askOverwrite);
 
+                SnippetGenerator.generate(all, pkg, includeKeys, new File(outFolder, "Ignite.snippet"),
askOverwrite);
+
                 perceptualDelay(started);
 
                 return null;


Mime
View raw message