1 // Copyright 2010 Google Inc. All Rights Reserved.
3 package com
.google
.appengine
.api
.search
.checkers
;
5 import com
.google
.appengine
.api
.search
.Util
;
6 import com
.google
.apphosting
.api
.search
.DocumentPb
;
9 * Checks values of a {@link com.google.appengine.api.search.Document}.
12 public final class DocumentChecker
{
14 private static final long MILLIS_UP_TO_1ST_JAN_2011
= 1293840000000L;
17 * The maximum length of a document id.
19 public static final int MAXIMUM_DOCUMENT_ID_LENGTH
= 500;
20 public static final int MAXIMUM_DOCUMENT_LENGTH
= 1 << 20;
23 * Checks whether a document id is valid. A document id is a
24 * non-null ASCII visible printable string of
25 * {@literal #MAXIMUM_DOCUMENT_ID_LENGTH} characters which does not start
26 * with '!' which is reserved for system documents.
28 * @param documentId the document id to check
29 * @return the checked document id
30 * @throw IllegalArgumentException if the document id is invalid
32 public static String
checkDocumentId(String documentId
) {
33 Preconditions
.checkArgument(!Util
.isNullOrEmpty(documentId
), "Document id is null or empty");
34 Preconditions
.checkArgument(documentId
.length() <= MAXIMUM_DOCUMENT_ID_LENGTH
,
35 "Document id is longer than %d: %s", MAXIMUM_DOCUMENT_ID_LENGTH
, documentId
);
36 Preconditions
.checkArgument(IndexChecker
.isAsciiVisiblePrintable(documentId
),
37 "documentId must be ASCII visible printable: %s", documentId
);
38 Preconditions
.checkArgument(!IndexChecker
.isReserved(documentId
),
39 "documentId must not start with !: %s", documentId
);
44 * Checks whether a {@link DocumentPb.Document} has a valid set
47 * @param pb the {@link DocumentPb.Document} protocol buffer to check
48 * @return the checked document
49 * @throws IllegalArgumentException if some field is invalid such as
50 * document id or fields
52 public static DocumentPb
.Document
checkValid(DocumentPb
.Document pb
) {
53 Preconditions
.checkArgument(pb
.getSerializedSize() <= MAXIMUM_DOCUMENT_LENGTH
,
54 "Document length %d is greater than the maximum %d bytes",
55 pb
.getSerializedSize(), MAXIMUM_DOCUMENT_LENGTH
);
57 DocumentChecker
.checkDocumentId(pb
.getId());
59 Preconditions
.checkArgument(pb
.getFieldList() != null,
60 "Null list of fields in document for indexing");
65 * @return the number of seconds since 2011/1/1
67 public static int getNumberOfSecondsSince() {
68 long millisSince
= Math
.max(0L,
69 (System
.currentTimeMillis() - MILLIS_UP_TO_1ST_JAN_2011
) / 1000L);
70 Preconditions
.checkArgument(millisSince
<= Integer
.MAX_VALUE
,
71 "API failure due to date conversion overflow");
72 return (int) millisSince
;