I copied the following code from ArangoGraphTest.java:
private static final String GRAPH_NAME = "db_collection_test";
private static final String EDGE_COL_1 = "db_edge1_collection_test";
private static final String EDGE_COL_2 = "db_edge2_collection_test";
private static final String EDGE_COL_3 = "db_edge3_collection_test";
private static final String VERTEX_COL_1 = "db_vertex1_collection_test";
private static final String VERTEX_COL_2 = "db_vertex2_collection_test";
private static final String VERTEX_COL_3 = "db_vertex3_collection_test";
private static final String VERTEX_COL_4 = "db_vertex4_collection_test";
public static void testGraphCreate() {
final ArangoDB arangoDB = new ArangoDB.Builder().user("root").password("root").build();
final Collection<EdgeDefinition> edgeDefinitions = new ArrayList<EdgeDefinition>();
edgeDefinitions.add(new EdgeDefinition().collection(EDGE_COL_1).from(VERTEX_COL_1).to(VERTEX_COL_2));
edgeDefinitions
.add(new EdgeDefinition().collection(EDGE_COL_2).from(VERTEX_COL_2).to(VERTEX_COL_1, VERTEX_COL_3));
final GraphCreateOptions options = new GraphCreateOptions();
ArangoDatabase db = arangoDB.db("TestArangoDB");
db.createGraph(GRAPH_NAME, edgeDefinitions, options);
}
After I ran this code the expected graph db_collection_test showed up in the web interface but it says "Your graph is empty." Is this expected and if so, how can I create a non-empty graph from Java?
This code just creates the collections in the database. There wont be any data unless you populate it. You should create all the vertices and edges as necessary.
Try creating some data with this snippet:
BaseDocument myObject = new BaseDocument();
myObject.setKey("myKey");
myObject.addAttribute("a", "Foo");
myObject.addAttribute("b", 42);
try {
arangoDB.db(dbName).collection(collectionName).insertDocument(myObject);
System.out.println("Document created");
} catch (ArangoDBException e) {
System.err.println("Failed to create document. " + e.getMessage());
}
Related
Java object to copy:
public class InfoDtcEx implements Serializable {
private static final long serialVersionUID = 1L;
private String infoCall="";
private String infoNotCall="";
private String infoTarget="";
private String infoTotal="";
private String infoValue="";
private ArrayList<String> valueList;
public InfoDtcEx(String infoCall, String infoNotCall,
String infoTarget, String infoTotal, String infoValue) {
this.infoCall = infoCall;
this.infoNotCall = infoNotCall;
this.infoTarget = infoTarget;
this.infoTotal = infoTotal;
this.infoValue = infoValue;
this.infoValueBefore = this.infoValue;
}
public InfoDtcEx(InfoDtc infoDtc) {
this.infoCall = infoDtc.getinfoDtcCall();
this.infoNotCall = infoDtc.getinfoDtcNotCall();
this.infoTotal = infoDtc.getinfoDtcTotal();
this.infoValue = infoDtc.getinfoDtcValue();
this.infoValueBefore = this.infoValue;
}
//getters and setters
}
I tried Using below method to deep copy as suggested at How to copy elements from an ArrayList to another one NOT by reference?:
private ArrayList<InfoDtcEx> copyInfoList(ArrayList<InfoDtcEx> infoListExChanged) {
infoListExChanged.clear();
for (int i = 0; i < infoListEx.size(); i++) {
String infoCall = infoListEx.get(i).getinfoCall();
if(infoCall != "Yes") {
infoListExChanged.add(infoListEx.get(i));
}
}
return infoListExChanged;
}
But, this is changing the actual list infoListEx as well.
You are not performing the deep copy as suggested in the post you linked to.
That post had the following line in the accepted answer :
copia.add(new Articulo_Venta(av.get(i)));
Notice the construction of the new Articulo_Venta. Your code is not calling new.
So try changing your line where you are adding to the list to create a new object, so :
infoListExChanged.add(new InfoDtcEx(infoListEx.get(i)));
I was trying to implement the query cache for large queries in ArangoDB.
When i check if the document cursor is cached or not, it shows that the cache is true. But i see no performance improvements in the query time processing.
However using the same query from arangodb web interface shows high performance improvements due to caching.
Edit :
Java Driver Version: 2.7.4
ArangoDb Version: 2.8.7
My Query is:
for t in MyStorage FILTER t.myDate>'2016-01-11' and t.myDate<'2016-06-01' and t.fraud!=null and t.fraud!='' and t.currency=='INR' return {myID:t.myID,myDate:t.myDate,amount:t.amount,fraud:t.fraud}
We tested Caching with the following Testcase and saw a performance improvement.
Can you post an example of your query?
public class ArangoDriverCacheTest {
private static final String COLLECTION_NAME = "unitTestCollection";
private static final String DATABASE_NAME = "unitTestDatabase";
private static ArangoConfigure configure;
private static ArangoDriver driver;
#BeforeClass
public static void setup() throws ArangoException {
configure = new ArangoConfigure();
configure.init();
driver = new ArangoDriver(configure);
// // create test database
try {
driver.createDatabase(DATABASE_NAME);
} catch (final ArangoException e) {
}
driver.setDefaultDatabase(DATABASE_NAME);
// create test collection
try {
driver.createCollection(COLLECTION_NAME);
} catch (final ArangoException e) {
}
driver.truncateCollection(COLLECTION_NAME);
// create some test data
for (int i = 0; i < 1000000; i++) {
final TestEntity value = new TestEntity("user_" + (i % 10), "desc" + (i % 10), i);
driver.createDocument(COLLECTION_NAME, value);
}
}
#AfterClass
public static void shutdown() {
try {
driver.deleteDatabase(DATABASE_NAME);
} catch (final ArangoException e) {
}
configure.shutdown();
}
private AqlQueryOptions createAqlQueryOptions(
final Boolean count,
final Integer batchSize,
final Boolean fullCount,
final Boolean cache) {
return new AqlQueryOptions().setCount(count).setBatchSize(batchSize).setFullCount(fullCount).setCache(cache);
}
#Test
public void test_withoutCache() throws ArangoException {
// set cache mode off
final QueryCachePropertiesEntity properties = new QueryCachePropertiesEntity();
properties.setMode("off");
driver.setQueryCacheProperties(properties);
exceuteQuery(false);
}
#Test
public void test_withCache() throws ArangoException {
// set cache mode on
final QueryCachePropertiesEntity properties = new QueryCachePropertiesEntity();
properties.setMode("on");
driver.setQueryCacheProperties(properties);
// set caching to true for the query
exceuteQuery(true);
}
private void exceuteQuery(final boolean cache) throws ArangoException {
final AqlQueryOptions aqlQueryOptions = createAqlQueryOptions(true, 1000, null, cache);
final String query = "FOR t IN " + COLLECTION_NAME + " FILTER t.age >= #age SORT t.age RETURN t";
final Map<String, Object> bindVars = new MapBuilder().put("age", 90).get();
DocumentCursor<TestEntity> rs = driver.executeDocumentQuery(query, bindVars, aqlQueryOptions, TestEntity.class);
// first time, the query isn't cached
Assert.assertEquals(false, rs.isCached());
final long start = System.currentTimeMillis();
// query the cached value
rs = driver.executeDocumentQuery(query, bindVars, aqlQueryOptions, TestEntity.class);
Assert.assertEquals(cache, rs.isCached());
// load all results
rs.asEntityList();
final long time = System.currentTimeMillis() - start;
System.out.println(String.format("time with cache=%s: %sms", cache, time));
}
private static class TestEntity {
private final String user;
private final String desc;
private final Integer age;
public TestEntity(final String user, final String desc, final Integer age) {
super();
this.user = user;
this.desc = desc;
this.age = age;
}
}
}
I get this error when I try to compile my code. I am not sure what is wrong with it and I don't understand what is the error saying. Here is the error messaged followed by the lines where it says it errors:
Exception in thread "AWT-EventQueue-0" java.lang.ExceptionInInitializerError
at g52gui.C_login.<clinit>(C_login.java:9)
at g52gui.V_login.<init>(V_login.java:6)
at g52gui.V_login$4.run(V_login.java:181)
...
and the lines where it errors:
line 9 C_login (controller): private static final C_home controller_home = new C_home();
line 6 V_login (view): private final C_login controller_login = new C_login();
line 181 V_login (view): new V_login().setVisible(true);
It seems like the problem comes from the C_home but there is no compilation errors in there.
EDIT
I think the problem might be here, in C_login:
public class C_login {
private static final V_login view_login = new V_login();
private static final M_login model_login = new M_login();
private final static C_home controller_home = new C_home();
private static final C_registration controller_regi = new C_registration();
private static final MySQLAccess sql_connection = new MySQLAccess();
public static void main(String[] args) throws Exception {
view_login.setVisible(true);
}
public static boolean login_button_clicked(String usrn, String psw){
String login = M_login.login(usrn, psw);
if (login == "ok"){
controller_home.start_view(usrn);
view_login.setVisible(false);
} else if (login == "fail" ) {
view_login.error_login();
return false;
} else {
view_login.error_connection();
return false;
}
return false;
}
I delcare C_home controller_home as static so I can access it later. Would there be another way to go around this and could this be the problem ?
EDIT
here are the Initializer in C_Home:
public class C_home {
private static final V_home view_home = new V_home();
private static final M_home model_home = new M_home();
private String username = "";
/* attributes for 3d model */
private BranchGroup sceneBranchGroup = null;
private RotationInterpolator rotator = null;
private Canvas3D offScreenCanvas3D = null;
private ImageComponent2D imageComponent = null;
private static final int offScreenWidth = 200;
private static final int offScreenHeight = 200;
I've been stuck at a seemingly simple problem for hours and I just can't find the solution. I'm trying to implement a very simple Forum in Java and I'm trying to load the entrys at the moment.
My forum is a JList that is filled with JPanels and that accepts entries via the JLists DefaultListModel and the addMessage method. So if I add an entry without the database it looks like this:
MessageList m = new MessageList();
m.addMessage("NAME AUTOR", "<html><body style='width: 675px;'>Lorem ipsum dolor sit amet.", "22.01.13", "SOA");
The messageList class looks like this:
public class MessageList extends JList{
DefaultListModel messageModel = new DefaultListModel();
MessageRenderer messageRenderer = new MessageRenderer();
public MessageList( ){
this.setCellRenderer(messageRenderer);
this.setModel(messageModel);
}
public void addMessage(String author, String text, String date, String tag){
messageModel.addElement(new Message(author, text, date, tag));
}
}
I've also written the Code for getting an ArrayList (called allBtr) with the Message Objects (called ConBeitrag) from the database:
ArrayList<ConBeitrag> allBtr = new ArrayList<ConBeitrag>();
ConBeitrag conBtr = new ConBeitrag();
try {
allBtr = conBtr.getAllBtr();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
The message objects look like this:
public class ConBeitrag {
private int beitragid;
private int projektid;
private int mitarbeiterid;
private String beitragText;
private String erstellt_am;
private String geaendert_am;
private String schlagwort1;
private String schlagwort2;
private MdBeitrag mdBtr = new MdBeitrag();
public ConBeitrag (){
}
public ConBeitrag(int beitragid, int projektid, int mitarbeiterid, String beitragText, String erstellt_am, String geaendert_am){
this.beitragid = beitragid;
this.projektid = projektid;
this.mitarbeiterid = mitarbeiterid;
this.erstellt_am = erstellt_am;
this.geaendert_am = geaendert_am;
this.beitragText = beitragText;
this.schlagwort1 = schlagwort1;
this.schlagwort2 = schlagwort2;
}
public ArrayList<ConBeitrag> getAllBtr() throws SQLException{
MdBtrInterface modInt;
modInt = new MdBeitrag();
ArrayList<ConBeitrag> AlBtr = modInt.getAllBtr();
for(ConBeitrag object: AlBtr){
System.out.println(object.beitragText);
}
return AlBtr;
}
}
Now what would be the smartest way to get the ArrayList into a form that I can pass into the addMessage method? I've kind of approached this from the GUI end, then from the database end, and now I'm stuck in the middle.
Overwritten toString() method:
#Override
public String toString() {
return mitarbeiterid + beitragstext + erstellt_am + schlagwort1 + schlagwort2;
}
"The messages are stored inside the ArrayList as Objects if that helps. So if I run "System.out.println(allBtr);" it gives me "[ConBeitrag#48f4104f, ConBeitrag#f5ad7f4, ConBeitrag#1517dc0c]"
You need to override the toString method in your ConGeitrag class. Something like this.
public class ConBeitrag {
...
#Override
public String toString(){
return author + ", " + text + ", " + date + ", " + tag;
}
}
You can make the return any format you want. Test this one out and make changes as desired to the format.
Try this out as a Helper method (after you've overridden the toString)
public JList createJList(ResultSet rs){
DefaultListModel model = new DefaultListModel();
while (rs.next()){
String author = rs.getString("author"); // Just an example. You may
String text = rs.getString("text"); // need to retrieve your
String date = rs.getString("date"); // data differently
String tag = rs.getString("tag");
Message message = new Message(author, text, date, tag);
model.addElement(message);
}
JList list = new JList(model);
return list;
}
I don't really see a need for a Custom JList for this situation.
Test run: output : 3testtestnullnull. Besides the formatting, it works fine
public class ConBeitragTest {
public static void main(String[] args) {
ConBeitrag con = new ConBeitrag(1, 2, 3, "test", "test", "test");
System.out.println(con);
}
}
class ConBeitrag {
private int beitragid;
private int projektid;
private int mitarbeiterid;
private String beitragText;
private String erstellt_am;
private String geaendert_am;
private String schlagwort1;
private String schlagwort2;
public ConBeitrag() {
}
public ConBeitrag(int beitragid, int projektid, int mitarbeiterid, String beitragText, String erstellt_am, String geaendert_am) {
this.beitragid = beitragid;
this.projektid = projektid;
this.mitarbeiterid = mitarbeiterid;
this.erstellt_am = erstellt_am;
this.geaendert_am = geaendert_am;
this.beitragText = beitragText;
this.schlagwort1 = schlagwort1; // This is null
this.schlagwort2 = schlagwort2; // This is null
}
#Override
public String toString() {
return mitarbeiterid + beitragText + erstellt_am + schlagwort1 + schlagwort2;
}
}
Well, I need this data to be used in at least two classes:
String navHome = "home";
String navAbout = "about";
String navUpload = "upload";
String navUploadProc = "uploadProc";
String navStartUpload = "startUpload";
String navContact = "contact";
String navSearch = "search";
String navManual = "manual";
String navBackToGmis = "backToGmis";
I was thinking of just pasting that into other classes, but then it violates the principles of programming, since it's repeating the same thing. Text and XML files are not comfortable to store data, inheritance isn't an option, since Java doesn't support multiple inheritance... Any ideas?
Yeah, I know it will sound retarded to most of you, but I need tipps on this.
If these are constants, then you can declare them in one class (or interface) and use them in another:
A.java:
public class A {
public static final String navHome = "home";
public static final String navAbout = "about";
public static final String navUpload = "upload";
public static final String navUploadProc = "uploadProc";
public static final String navStartUpload = "startUpload";
public static final String navContact = "contact";
public static final String navSearch = "search";
public static final String navManual = "manual";
public static final String navBackToGmis = "backToGmis";
. . .
}
B.java:
import static A.*; // or list each String in a separate import
public class B {
. . . // code can use nav* as if they were declared in class B
}