[ 
https://issues.apache.org/jira/browse/GORA-513?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16089398#comment-16089398
 ] 

ASF GitHub Bot commented on GORA-513:
-------------------------------------

Github user djkevincr commented on a diff in the pull request:

    https://github.com/apache/gora/pull/112#discussion_r127641711
  
    --- Diff: 
gora-orientdb/src/main/java/org/apache/gora/orientdb/store/OrientDBStore.java 
---
    @@ -0,0 +1,922 @@
    +/**
    + * 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.gora.orientdb.store;
    +
    +import java.io.IOException;
    +import java.nio.ByteBuffer;
    +import java.util.Map;
    +import java.util.Properties;
    +import java.util.List;
    +import java.util.HashMap;
    +import java.util.HashSet;
    +import java.util.ArrayList;
    +import java.util.Date;
    +import java.util.Calendar;
    +import java.util.Collection;
    +import java.util.TimeZone;
    +import java.util.Locale;
    +
    +import com.github.raymanrt.orientqb.query.Parameter;
    +import com.gitub.raymanrt.orientqb.delete.Delete;
    +import com.orientechnologies.orient.client.remote.OServerAdmin;
    +import com.orientechnologies.orient.core.db.OPartitionedDatabasePool;
    +import 
com.orientechnologies.orient.core.db.OPartitionedDatabasePoolFactory;
    +import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
    +import com.orientechnologies.orient.core.db.record.OTrackedList;
    +import com.orientechnologies.orient.core.db.record.OTrackedMap;
    +import com.orientechnologies.orient.core.db.record.OTrackedSet;
    +import com.orientechnologies.orient.core.metadata.schema.OClass;
    +import com.orientechnologies.orient.core.metadata.schema.OType;
    +import com.orientechnologies.orient.core.record.impl.ODocument;
    +import com.orientechnologies.orient.core.sql.OCommandSQL;
    +import com.orientechnologies.orient.core.sql.query.OConcurrentResultSet;
    +import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;
    +import org.apache.avro.Schema;
    +import org.apache.avro.util.Utf8;
    +import org.apache.gora.orientdb.query.OrientDBQuery;
    +import org.apache.gora.orientdb.query.OrientDBResult;
    +import org.apache.gora.persistency.impl.BeanFactoryImpl;
    +import org.apache.gora.persistency.impl.DirtyListWrapper;
    +import org.apache.gora.persistency.impl.DirtyMapWrapper;
    +import org.apache.gora.persistency.impl.PersistentBase;
    +import org.apache.gora.query.PartitionQuery;
    +import org.apache.gora.query.Query;
    +import org.apache.gora.query.Result;
    +import org.apache.gora.query.impl.PartitionQueryImpl;
    +import org.apache.gora.store.impl.DataStoreBase;
    +import org.apache.gora.util.AvroUtils;
    +import org.apache.gora.util.ClassLoadingUtils;
    +
    +import javax.xml.bind.DatatypeConverter;
    +
    +import static com.github.raymanrt.orientqb.query.Projection.projection;
    +
    +/**
    + * {@link org.apache.gora.orientdb.store.OrientDBStore} is the primary 
class
    + * responsible for facilitating GORA CRUD operations on OrientDB documents.
    + */
    +public class OrientDBStore<K, T extends PersistentBase> extends 
DataStoreBase<K, T> {
    +
    +  public static final String DEFAULT_MAPPING_FILE = 
"/gora-orientdb-mapping.xml";
    +  private String ROOT_URL;
    +  private String ROOT_DATABASE_URL;
    +  private OrientDBStoreParameters orientDbStoreParams;
    +  private OrientDBMapping orientDBMapping;
    +  private OServerAdmin remoteServerAdmin;
    +  private OPartitionedDatabasePool connectionPool;
    +  private List<ODocument> docBatch = new ArrayList<>();
    +
    +  /**
    +   * Initialize the OrientDB dataStore by {@link Properties} parameters.
    +   *
    +   * @param keyClass key class type for dataStore.
    +   * @param persistentClass persistent class type for dataStore.
    +   * @param properties OrientDB dataStore properties EG:- OrientDB client 
credentials.
    +   */
    +  @Override
    +  public void initialize(Class<K> keyClass, Class<T> persistentClass, 
Properties properties) {
    +    super.initialize(keyClass, persistentClass, properties);
    +    try {
    +      orientDbStoreParams = OrientDBStoreParameters.load(properties);
    +      ROOT_URL = 
"remote:".concat(orientDbStoreParams.getServerHost()).concat(":")
    +              .concat(orientDbStoreParams.getServerPort());
    +      ROOT_DATABASE_URL = 
ROOT_URL.concat("/").concat(orientDbStoreParams.getDatabaseName());
    +      remoteServerAdmin = new 
OServerAdmin(ROOT_URL).connect(orientDbStoreParams.getUserName(),
    +              orientDbStoreParams.getUserPassword());
    +      if 
(!remoteServerAdmin.existsDatabase(orientDbStoreParams.getDatabaseName(), 
"memory")) {
    +        
remoteServerAdmin.createDatabase(orientDbStoreParams.getDatabaseName(), 
"document", "memory");
    +      }
    +
    +      if (orientDbStoreParams.getConnectionPoolSize() != null) {
    +        int connPoolSize = 
Integer.valueOf(orientDbStoreParams.getConnectionPoolSize());
    +        connectionPool = new OPartitionedDatabasePoolFactory(connPoolSize)
    +                .get(ROOT_DATABASE_URL, orientDbStoreParams.getUserName(),
    +                        orientDbStoreParams.getUserPassword());
    +      } else {
    +        connectionPool = new 
OPartitionedDatabasePoolFactory().get(ROOT_DATABASE_URL,
    +                orientDbStoreParams.getUserName(), 
orientDbStoreParams.getUserPassword());
    +      }
    +
    +      OrientDBMappingBuilder<K, T> builder = new 
OrientDBMappingBuilder<>(this);
    +      orientDBMapping = 
builder.fromFile(orientDbStoreParams.getMappingFile()).build();
    +
    +      if (!schemaExists()) {
    +        createSchema();
    +      }
    +    } catch (Exception e) {
    +      LOG.error("Error while initializing OrientDB dataStore: {}",
    +              new Object[]{e.getMessage()});
    +      throw new RuntimeException(e);
    +    }
    +  }
    +
    +  @Override
    +  public String getSchemaName(final String mappingSchemaName,
    +                              final Class<?> persistentClass) {
    +    return super.getSchemaName(mappingSchemaName, persistentClass);
    +  }
    +
    +  @Override
    +  public String getSchemaName() {
    +    return orientDBMapping.getDocumentClass();
    +  }
    +
    +  /**
    +   * Create a new class of OrientDB documents if necessary. Enforce 
specified schema over the document class.
    +   *
    +   */
    +  @Override
    +  public void createSchema() {
    +    if (schemaExists()) {
    +      return;
    +    }
    +
    +    ODatabaseDocumentTx schemaTx = connectionPool.acquire();
    +    schemaTx.activateOnCurrentThread();
    +    try {
    +
    +      OClass documentClass = 
schemaTx.getMetadata().getSchema().createClass(orientDBMapping.getDocumentClass());
    +      documentClass.createProperty("_id",
    +              
OType.getTypeByClass(super.getKeyClass())).createIndex(OClass.INDEX_TYPE.UNIQUE);
    +      for (String docField : orientDBMapping.getDocumentFields()) {
    +        documentClass.createProperty(docField,
    +                
OType.valueOf(orientDBMapping.getDocumentFieldType(docField).name()));
    +      }
    +      schemaTx.getMetadata().getSchema().reload();
    +    } finally {
    +      schemaTx.close();
    +    }
    +  }
    +
    +  /**
    +   * Deletes enforced schema over OrientDB Document class.
    +   *
    +   */
    +  @Override
    +  public void deleteSchema() {
    +    ODatabaseDocumentTx schemaTx = connectionPool.acquire();
    +    schemaTx.activateOnCurrentThread();
    +    try {
    +      
schemaTx.getMetadata().getSchema().dropClass(orientDBMapping.getDocumentClass());
    +    } finally {
    +      schemaTx.close();
    +    }
    +  }
    +
    +  /**
    +   * Check whether there exist a schema enforced over OrientDB document 
class.
    +   *
    +   */
    +  @Override
    +  public boolean schemaExists() {
    +    ODatabaseDocumentTx schemaTx = connectionPool.acquire();
    +    schemaTx.activateOnCurrentThread();
    +    try {
    +      return schemaTx.getMetadata().getSchema()
    +              .existsClass(orientDBMapping.getDocumentClass());
    +    } finally {
    +      schemaTx.close();
    +    }
    +  }
    +
    +  @Override
    +  public T get(K key, String[] fields) {
    +    String[] dbFields = getFieldsToQuery(fields);
    +    com.github.raymanrt.orientqb.query.Query selectQuery = new 
com.github.raymanrt.orientqb.query.Query();
    +    for (String k : dbFields) {
    +      String dbFieldName = orientDBMapping.getDocumentField(k);
    +      if (dbFieldName != null && dbFieldName.length() > 0) {
    +        selectQuery.select(dbFieldName);
    +      }
    +    }
    +    selectQuery.from(orientDBMapping.getDocumentClass())
    +            .where(projection("_id").eq(Parameter.parameter("key")));
    +    Map<String, Object> params = new HashMap<String, Object>();
    +    params.put("key", key);
    +    OSQLSynchQuery<ODocument> query = new 
OSQLSynchQuery<ODocument>(selectQuery.toString());
    +    ODatabaseDocumentTx selectTx = connectionPool.acquire();
    +    selectTx.activateOnCurrentThread();
    +    try {
    +      List<ODocument> result = selectTx.command(query).execute(params);
    +      if (result.size() == 1) {
    +        return convertOrientDocToAvroBean(result.get(0), dbFields);
    +      } else {
    +        return null;
    +      }
    +    } finally {
    +      selectTx.close();
    +    }
    +  }
    +
    +  @Override
    +  public void put(K key, T val) {
    +    if (val.isDirty()) {
    +      OrientDBQuery<K, T> dataStoreQuery = new OrientDBQuery<>(this);
    +      dataStoreQuery.setStartKey(key);
    +      dataStoreQuery.setEndKey(key);
    +      dataStoreQuery.populateOrientDBQuery(orientDBMapping, 
getFieldsToQuery(null), getFields());
    +
    +      ODatabaseDocumentTx selectTx = connectionPool.acquire();
    +      selectTx.activateOnCurrentThread();
    +      try {
    +        List<ODocument> result = 
selectTx.command(dataStoreQuery.getOrientDBQuery())
    +                .execute(dataStoreQuery.getParams());
    +        if (result.size() == 1) {
    +          ODocument document = updateOrientDocFromAvroBean(key, val, 
result.get(0));
    +          docBatch.add(document);
    +        } else {
    +          ODocument document = convertAvroBeanToOrientDoc(key, val);
    +          docBatch.add(document);
    +        }
    +      } finally {
    +        selectTx.close();
    +      }
    +    } else {
    +      LOG.info("Ignored putting persistent bean {} in the store as it is 
neither "
    --- End diff --
    
    +1, will do.


> Implement OrientDB Datastore.
> -----------------------------
>
>                 Key: GORA-513
>                 URL: https://issues.apache.org/jira/browse/GORA-513
>             Project: Apache Gora
>          Issue Type: New Feature
>            Reporter: Kevin Ratnasekera
>            Assignee: Kevin Ratnasekera
>
> OrientDB [1] client [2] query builder [3] comes under Apache license.
> [1] http://orientdb.com/orientdb/
> [2] https://github.com/orientechnologies/orientdb
> [3] https://github.com/raymanrt/orientqb



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Reply via email to