<commit_before>/*
    Copyright 2015, Strategic Gains, Inc.

	Licensed 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 com.orangerhymelabs.helenusdb.cassandra.document;

import java.nio.ByteBuffer;
import java.util.Date;

import org.bson.BSON;

import com.datastax.driver.core.BatchStatement;
import com.datastax.driver.core.BatchStatement.Type;
import com.datastax.driver.core.BoundStatement;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.ResultSetFuture;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.exceptions.InvalidTypeException;
import com.orangerhymelabs.helenusdb.cassandra.AbstractCassandraRepository;
import com.orangerhymelabs.helenusdb.cassandra.DataTypes;
import com.orangerhymelabs.helenusdb.cassandra.itable.ItableStatementFactory;
import com.orangerhymelabs.helenusdb.cassandra.table.Table;
import com.orangerhymelabs.helenusdb.exception.InvalidIdentifierException;
import com.orangerhymelabs.helenusdb.persistence.Identifier;

/**
 * Document repositories are unique per document/table and therefore must be cached by table.
 * 
 * @author tfredrich
 * @since Jun 8, 2015
 */
public class DocumentRepository
extends AbstractCassandraRepository<Document>
{
	public static class Schema
	{
		private static final String DROP_TABLE = "drop table if exists %s.%s;";
		private static final String CREATE_TABLE = "create table if not exists %s.%s" +
		"(" +
			"id %s," +
		    "object blob," +
		    // TODO: Add Location details to Document.
			"created_at timestamp," +
		    "updated_at timestamp," +
			"primary key (id)" +
		")";

		public boolean drop(Session session, String keyspace, String table)
        {
			ResultSet rs = session.execute(String.format(DROP_TABLE, keyspace, table));
	        return rs.wasApplied();
        }

        public boolean create(Session session, String keyspace, String table, DataTypes idType)
        {
			ResultSet rs = session.execute(String.format(CREATE_TABLE, keyspace, table, idType.cassandraType()));
			return rs.wasApplied();
        }
	}

	private class Columns
	{

		static final String ID = "id";
		static final String OBJECT = "object";
		static final String CREATED_AT = "created_at";
		static final String UPDATED_AT = "updated_at";
	}

	private static final String READ_CQL = "select * from %s.%s where id = ?";
	private static final String DELETE_CQL = "delete from %s.%s where id = ?";
	private static final String UPDATE_CQL = "update %s.%s set object = ?, updated_at = ? where id = ? if exists";
	private static final String CREATE_CQL = "insert into %s.%s (id, object, created_at, updated_at) values (?, ?, ?, ?) if not exists";

	private Table table;
	private ItableStatementFactory iTableStmtFactory;

	public DocumentRepository(Session session, String keyspace, Table table)
	{
		super(session, keyspace);<commit_msg>Refactored all schema and query statements to use Columns constants.<commit_after>/*
    Copyright 2015, Strategic Gains, Inc.

	Licensed 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 com.orangerhymelabs.helenusdb.cassandra.document;

import java.nio.ByteBuffer;
import java.util.Date;

import org.bson.BSON;

import com.datastax.driver.core.BatchStatement;
import com.datastax.driver.core.BatchStatement.Type;
import com.datastax.driver.core.BoundStatement;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.ResultSetFuture;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.exceptions.InvalidTypeException;
import com.orangerhymelabs.helenusdb.cassandra.AbstractCassandraRepository;
import com.orangerhymelabs.helenusdb.cassandra.DataTypes;
import com.orangerhymelabs.helenusdb.cassandra.itable.ItableStatementFactory;
import com.orangerhymelabs.helenusdb.cassandra.table.Table;
import com.orangerhymelabs.helenusdb.exception.InvalidIdentifierException;
import com.orangerhymelabs.helenusdb.persistence.Identifier;

/**
 * Document repositories are unique per document/table and therefore must be cached by table.
 * 
 * @author tfredrich
 * @since Jun 8, 2015
 */
public class DocumentRepository
extends AbstractCassandraRepository<Document>
{
	private class Columns
	{
		static final String ID = "id";
		static final String OBJECT = "object";
		static final String LUCENE = "lucene";
		static final String CREATED_AT = "created_at";
		static final String UPDATED_AT = "updated_at";
	}

	public static class Schema
	{
		private static final String DROP_TABLE = "drop table if exists %s.%s;";
		private static final String CREATE_TABLE = "create table if not exists %s.%s" +
		"(" +
			Columns.ID + " %s," +
		    Columns.OBJECT + " blob," +
		    // TODO: Add Location details to Document.
		    Columns.LUCENE + " text," + 			// Empty, but facilitates Lucene indexing / searches.
			Columns.CREATED_AT + " timestamp," +
		    Columns.UPDATED_AT + " timestamp," +
			"primary key (" + Columns.ID + ")" +
		")";

		public boolean drop(Session session, String keyspace, String table)
        {
			ResultSet rs = session.execute(String.format(DROP_TABLE, keyspace, table));
	        return rs.wasApplied();
        }

        public boolean create(Session session, String keyspace, String table, DataTypes idType)
        {
			ResultSet rs = session.execute(String.format(CREATE_TABLE, keyspace, table, idType.cassandraType()));
			return rs.wasApplied();
        }
	}

	private static final String READ_CQL = "select * from %s.%s where " + Columns.ID + " = ?";
	private static final String DELETE_CQL = "delete from %s.%s where " + Columns.ID + " = ?";
	private static final String UPDATE_CQL = "update %s.%s set " + Columns.OBJECT + " = ?, " + Columns.UPDATED_AT + " = ? where " + Columns.ID + " = ? if exists";
	private static final String CREATE_CQL = "insert into %s.%s (" + Columns.ID + ", " + Columns.OBJECT + ", " + Columns.CREATED_AT + ", " + Columns.UPDATED_AT + ") values (?, ?, ?, ?) if not exists";

	private Table table;
	private ItableStatementFactory iTableStmtFactory;

	public DocumentRepository(Session session, String keyspace, Table table)
	{
		super(session, keyspace);
{'fraction_non_alphanumeric': 0.09952340902719373, 'fraction_numerical': 0.0036445192038127277, 'mean_word_length': 4.304832713754647, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1224898', 'n_tokens_mistral': 2300, 'n_tokens_neox': 2139, 'n_words': 635}