<commit_before>/**
 * Copyright 2015 Google Inc. All Rights Reserved.
 *
 * 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.example.managedvms.gettingstartedjava.daos;

import com.google.gcloud.datastore.Cursor;
import com.google.gcloud.datastore.Datastore;
import com.google.gcloud.datastore.DatastoreOptions;
import com.google.gcloud.datastore.Entity;
import com.google.gcloud.datastore.FullEntity;
import com.google.gcloud.datastore.IncompleteKey;
import com.google.gcloud.datastore.Key;
import com.google.gcloud.datastore.KeyFactory;
import com.google.gcloud.datastore.Query;
import com.google.gcloud.datastore.QueryResults;
import com.google.gcloud.datastore.StructuredQuery.OrderBy;
import com.google.gcloud.datastore.StructuredQuery.PropertyFilter;

import com.example.managedvms.gettingstartedjava.objects.Book;
import com.example.managedvms.gettingstartedjava.objects.Result;

import java.util.ArrayList;
import java.util.List;

// [START example]
public class DatastoreDao implements BookDao {

// [START constructor]
  private Datastore datastore;
  private KeyFactory keyFactory;

  public DatastoreDao() {
    datastore = DatastoreOptions.defaultInstance().service();  // Authorized Datastore service object
    keyFactory = datastore.newKeyFactory().kind("Book"); // Is used for creating keys later
  }
// [END constructor]
// [START create]
  @Override
  public Long createBook(Book book) {
    IncompleteKey key = keyFactory.newKey();          // Key will be assigned once written
    FullEntity<IncompleteKey> incBookEntity = Entity.builder(key)  // Create the Entity
        .set(Book.AUTHOR, book.getAuthor())           // Add Property ("author", book.getAuthor())
        .set(Book.CREATED_BY, book.getCreatedBy())
        .set(Book.CREATED_BY_ID, book.getCreatedById())
        .set(Book.DESCRIPTION, book.getDescription())
        .set(Book.PUBLISHED_DATE, book.getPublishedDate())
        .set(Book.TITLE, book.getTitle())
        .set(Book.IMAGE_URL, book.getImageUrl())
        .build();
    Entity bookEntity = datastore.add(incBookEntity); // Save the Entity
    return bookEntity.key().id();                     // The ID of the Key
  }
// [END create]
// [START read]
  @Override
  public Book readBook(Long bookId) {
    Entity bookEntity = datastore.get(keyFactory.newKey(bookId)); // Load an Entity for Key(id)
    return new Book.Builder()                                     // Convert to our form
        .author(bookEntity.getString(Book.AUTHOR))
        .createdBy(bookEntity.contains(Book.CREATED_BY) ? bookEntity.getString(Book.CREATED_BY) : "")
        .createdById(
            bookEntity.contains(Book.CREATED_BY_ID) ? bookEntity.getString(Book.CREATED_BY_ID) : "")
        .description(bookEntity.getString(Book.DESCRIPTION))
        .id(bookEntity.key().id())
        .publishedDate(bookEntity.getString(Book.PUBLISHED_DATE))
        .title(bookEntity.getString(Book.TITLE))
        .imageUrl(bookEntity.contains(Book.IMAGE_URL) ? bookEntity.getString(Book.IMAGE_URL) : null)
        .build();
  }
// [END read]
// [START update]
  @Override
  public void updateBook(Book book) {
    Key key = keyFactory.newKey(book.getId());  // From a book, create a Key
    Entity entity = Entity.builder(key)         // Convert our Book to an Entity
        .set(Book.AUTHOR, book.getAuthor())
        .set(Book.CREATED_BY, book.getCreatedBy())
        .set(Book.CREATED_BY_ID, book.getCreatedById())
        .set(Book.DESCRIPTION, book.getDescription())
        .set(Book.PUBLISHED_DATE, book.getPublishedDate())
        .set(Book.TITLE, book.getTitle())
        .set(Book.IMAGE_URL, book.getImageUrl())
        .build();
    datastore.update(entity);                   // Update the Entity
  }
// [END update]
// [START delete]
  @Override
  public void deleteBook(Long bookId) {
    Key key = keyFactory.newKey(bookId);        // Create the Key
    datastore.delete(key);                      // Delete the Entity
  }
// [END delete]
// [START entitiesToBooks]
  public List<Book> entitiesToBooks(QueryResults<Entity> resultList) {
    List<Book> resultBooks = new ArrayList<>();
    while (resultList.hasNext()) {
      Entity bookEntity = resultList.next();
      Book book = new Book.Builder()
          .author(bookEntity.getString(Book.AUTHOR))
          .createdBy(
              bookEntity.contains(Book.CREATED_BY) ? bookEntity.getString(Book.CREATED_BY) : "")
          .createdById(
              bookEntity.contains(
                  Book.CREATED_BY_ID) ? bookEntity.getString(Book.CREATED_BY_ID) : "")
          .description(bookEntity.getString(Book.DESCRIPTION))
          .id(bookEntity.key().id())
          .publishedDate(bookEntity.getString(Book.PUBLISHED_DATE))
          .title(bookEntity.getString(Book.TITLE))
          .imageUrl(
              bookEntity.contains(Book.IMAGE_URL) ? bookEntity.getString(Book.IMAGE_URL) : null)
          .build();
      resultBooks.add(book);
    }
    return resultBooks;
  }
// [END entitiesToBooks]
// [START listbooks]
  @Override
  public Result<Book> listBooks(String startCursorString) {
    Cursor startCursor = null;
    if (startCursorString != null && !startCursorString.equals("")) {
      startCursor = Cursor.fromUrlSafe(startCursorString);
    }
    Query<Entity> query = Query.entityQueryBuilder()
        .kind("Book")
        .limit(10)
        .startCursor(startCursor)
        .orderBy(OrderBy.asc("title"))
        .build();
    QueryResults<Entity> resultList = datastore.run(query);   // Run the query
    List<Book> resultBooks = entitiesToBooks(resultList);     // Retrieve and convert Entities
    Cursor cursor = resultList.cursorAfter();
    if (cursor != null && resultBooks.size() == 10) {         // Handle paging
      String cursorString = cursor.toUrlSafe();
      return new Result<>(resultBooks, cursorString);
    } else {
      return new Result<>(resultBooks);
    }
  }
// [END listbooks]
// [START listbyuser]
  @Override
  public Result<Book> listBooksByUser(String userId, String startCursorString) {
    Cursor startCursor = null;
    if (startCursorString != null && !startCursorString.equals("")) {
      startCursor = Cursor.fromUrlSafe(startCursorString);
    }
    Query<Entity> query = Query.entityQueryBuilder()
        .kind("Book")
        .filter(PropertyFilter.eq(Book.CREATED_BY_ID, userId))  // Only for this user
        .limit(10)
        .startCursor(startCursor)
        // a custom datastore index is required since you are filtering by one property
        // but ordering by another
        .orderBy(OrderBy.asc(Book.TITLE))
        .build();
    QueryResults<Entity> resultList = datastore.run(query);
    List<Book> resultBooks = entitiesToBooks(resultList);
    Cursor cursor = resultList.cursorAfter();
    if (cursor != null && resultBooks.size() == 10) {
      String cursorString = cursor.toUrlSafe();
      return new Result<>(resultBooks, cursorString);
    } else {
      return new Result<>(resultBooks);
    }
  }
// [END listbyuser]
}
// [END example]<commit_msg>Updated comments<commit_after>/**
 * Copyright 2015 Google Inc. All Rights Reserved.
 *
 * 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.example.managedvms.gettingstartedjava.daos;

import com.google.gcloud.datastore.Cursor;
import com.google.gcloud.datastore.Datastore;
import com.google.gcloud.datastore.DatastoreOptions;
import com.google.gcloud.datastore.Entity;
import com.google.gcloud.datastore.FullEntity;
import com.google.gcloud.datastore.IncompleteKey;
import com.google.gcloud.datastore.Key;
import com.google.gcloud.datastore.KeyFactory;
import com.google.gcloud.datastore.Query;
import com.google.gcloud.datastore.QueryResults;
import com.google.gcloud.datastore.StructuredQuery.OrderBy;
import com.google.gcloud.datastore.StructuredQuery.PropertyFilter;

import com.example.managedvms.gettingstartedjava.objects.Book;
import com.example.managedvms.gettingstartedjava.objects.Result;

import java.util.ArrayList;
import java.util.List;

// [START example]
public class DatastoreDao implements BookDao {

// [START constructor]
  private Datastore datastore;
  private KeyFactory keyFactory;

  public DatastoreDao() {
    datastore = DatastoreOptions.defaultInstance().service();  // Authorized Datastore service object
    keyFactory = datastore.newKeyFactory().kind("Book"); // Is used for creating keys later
  }
// [END constructor]
// [START create]
  @Override
  public Long createBook(Book book) {
    IncompleteKey key = keyFactory.newKey();          // Key will be assigned once written
    FullEntity<IncompleteKey> incBookEntity = Entity.builder(key)  // Create the Entity
        .set(Book.AUTHOR, book.getAuthor())           // Add Property ("author", book.getAuthor())
        .set(Book.CREATED_BY, book.getCreatedBy())
        .set(Book.CREATED_BY_ID, book.getCreatedById())
        .set(Book.DESCRIPTION, book.getDescription())
        .set(Book.PUBLISHED_DATE, book.getPublishedDate())
        .set(Book.TITLE, book.getTitle())
        .set(Book.IMAGE_URL, book.getImageUrl())
        .build();
    Entity bookEntity = datastore.add(incBookEntity); // Save the Entity
    return bookEntity.key().id();                     // The ID of the Key
  }
// [END create]
// [START read]
  @Override
  public Book readBook(Long bookId) {
    Entity bookEntity = datastore.get(keyFactory.newKey(bookId)); // Load an Entity for Key(id)
    return new Book.Builder()                                     // Convert to Book form
        .author(bookEntity.getString(Book.AUTHOR))
        .createdBy(bookEntity.contains(Book.CREATED_BY) ? bookEntity.getString(Book.CREATED_BY) : "")
        .createdById(
            bookEntity.contains(Book.CREATED_BY_ID) ? bookEntity.getString(Book.CREATED_BY_ID) : "")
        .description(bookEntity.getString(Book.DESCRIPTION))
        .id(bookEntity.key().id())
        .publishedDate(bookEntity.getString(Book.PUBLISHED_DATE))
        .title(bookEntity.getString(Book.TITLE))
        .imageUrl(bookEntity.contains(Book.IMAGE_URL) ? bookEntity.getString(Book.IMAGE_URL) : null)
        .build();
  }
// [END read]
// [START update]
  @Override
  public void updateBook(Book book) {
    Key key = keyFactory.newKey(book.getId());  // From a book, create a Key
    Entity entity = Entity.builder(key)         // Convert Book to an Entity
        .set(Book.AUTHOR, book.getAuthor())
        .set(Book.CREATED_BY, book.getCreatedBy())
        .set(Book.CREATED_BY_ID, book.getCreatedById())
        .set(Book.DESCRIPTION, book.getDescription())
        .set(Book.PUBLISHED_DATE, book.getPublishedDate())
        .set(Book.TITLE, book.getTitle())
        .set(Book.IMAGE_URL, book.getImageUrl())
        .build();
    datastore.update(entity);                   // Update the Entity
  }
// [END update]
// [START delete]
  @Override
  public void deleteBook(Long bookId) {
    Key key = keyFactory.newKey(bookId);        // Create the Key
    datastore.delete(key);                      // Delete the Entity
  }
// [END delete]
// [START entitiesToBooks]
  public List<Book> entitiesToBooks(QueryResults<Entity> resultList) {
    List<Book> resultBooks = new ArrayList<>();
    while (resultList.hasNext()) {  // We still have data
      Entity bookEntity = resultList.next();  // Retrieve next Entity
      Book book = new Book.Builder()
          .author(bookEntity.getString(Book.AUTHOR))
          .createdBy(
              bookEntity.contains(Book.CREATED_BY) ? bookEntity.getString(Book.CREATED_BY) : "")
          .createdById(
              bookEntity.contains(
                  Book.CREATED_BY_ID) ? bookEntity.getString(Book.CREATED_BY_ID) : "")
          .description(bookEntity.getString(Book.DESCRIPTION))
          .id(bookEntity.key().id())
          .publishedDate(bookEntity.getString(Book.PUBLISHED_DATE))
          .title(bookEntity.getString(Book.TITLE))
          .imageUrl(
              bookEntity.contains(Book.IMAGE_URL) ? bookEntity.getString(Book.IMAGE_URL) : null)
          .build();
      resultBooks.add(book);      // Add the Book to the List
    }
    return resultBooks;
  }
// [END entitiesToBooks]
// [START listbooks]
  @Override
  public Result<Book> listBooks(String startCursorString) {
    Cursor startCursor = null;
    if (startCursorString != null && !startCursorString.equals("")) {
      startCursor = Cursor.fromUrlSafe(startCursorString);    // Where we left off
    }
    Query<Entity> query = Query.entityQueryBuilder()          // Build the Query
        .kind("Book")                                         // We only care about Books
        .limit(10)                                            // Only show 10 at a time
        .startCursor(startCursor)                             // Where we left off
        .orderBy(OrderBy.asc(Book.TITLE))                     // Use default Index "title"
        .build();
    QueryResults<Entity> resultList = datastore.run(query);   // Run the query
    List<Book> resultBooks = entitiesToBooks(resultList);     // Retrieve and convert Entities
    Cursor cursor = resultList.cursorAfter();                 // Where to start next time
    if (cursor != null && resultBooks.size() == 10) {         // Are we paging? Save Cursor
      String cursorString = cursor.toUrlSafe();               // Cursors are WebSafe
      return new Result<>(resultBooks, cursorString);
    } else {
      return new Result<>(resultBooks);
    }
  }
// [END listbooks]
// [START listbyuser]
  @Override
  public Result<Book> listBooksByUser(String userId, String startCursorString) {
    Cursor startCursor = null;
    if (startCursorString != null && !startCursorString.equals("")) {
      startCursor = Cursor.fromUrlSafe(startCursorString);    // Where we left off
    }
    Query<Entity> query = Query.entityQueryBuilder()          // Build the Query
        .kind("Book")                                         // We only care about Books
        .filter(PropertyFilter.eq(Book.CREATED_BY_ID, userId))// Only for this user
        .limit(10)                                            // Only show 10 at a time
        .startCursor(startCursor)                             // Where we left off
        // a custom datastore index is required since you are filtering by one property
        // but ordering by another
        .orderBy(OrderBy.asc(Book.TITLE))
        .build();
    QueryResults<Entity> resultList = datastore.run(query);   // Run the Query
    List<Book> resultBooks = entitiesToBooks(resultList);     // Retrieve and convert Entities
    Cursor cursor = resultList.cursorAfter();                 // Where to start next time
    if (cursor != null && resultBooks.size() == 10) {         // Are we paging? Save Cursor
      String cursorString = cursor.toUrlSafe();               // Cursors are WebSafe
      return new Result<>(resultBooks, cursorString);
    } else {
      return new Result<>(resultBooks);
    }
  }
// [END listbyuser]
}
// [END example]
{'fraction_non_alphanumeric': 0.11602451838879159, 'fraction_numerical': 0.002251688766574931, 'mean_word_length': 2.8105338417540513, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6444416', 'n_tokens_mistral': 4646, 'n_tokens_neox': 4529, 'n_words': 1073}