001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.solr.schema;
017:
018: import java.util.Map;
019: import java.util.HashMap;
020:
021: /**
022: * @author yonik
023: * @version $Id: FieldProperties.java 533571 2007-04-29 23:03:03Z ryan $
024: */
025: abstract class FieldProperties {
026:
027: // use a bitfield instead of many different boolean variables since
028: // many of the variables are independent or semi-independent.
029:
030: // bit values for boolean field properties.
031: final static int INDEXED = 0x00000001;
032: final static int TOKENIZED = 0x00000002;
033: final static int STORED = 0x00000004;
034: final static int BINARY = 0x00000008;
035: final static int COMPRESSED = 0x00000010;
036: final static int OMIT_NORMS = 0x00000020;
037: final static int STORE_TERMVECTORS = 0x00000040;
038: final static int STORE_TERMPOSITIONS = 0x00000080;
039: final static int STORE_TERMOFFSETS = 0x00000100;
040:
041: final static int MULTIVALUED = 0x00000200;
042: final static int SORT_MISSING_FIRST = 0x00000400;
043: final static int SORT_MISSING_LAST = 0x00000800;
044:
045: final static int REQUIRED = 0x00001000;
046:
047: static final String[] propertyNames = { "indexed", "tokenized",
048: "stored", "binary", "compressed", "omitNorms",
049: "termVectors", "termPositions", "termOffsets",
050: "multiValued", "sortMissingFirst", "sortMissingLast",
051: "required" };
052:
053: static final Map<String, Integer> propertyMap = new HashMap<String, Integer>();
054: static {
055: for (String prop : propertyNames) {
056: propertyMap.put(prop, propertyNameToInt(prop));
057: }
058: }
059:
060: /** Returns the symbolic name for the property. */
061: static String getPropertyName(int property) {
062: return propertyNames[Integer.numberOfTrailingZeros(property)];
063: }
064:
065: static int propertyNameToInt(String name) {
066: for (int i = 0; i < propertyNames.length; i++) {
067: if (propertyNames[i].equals(name)) {
068: return 1 << i;
069: }
070: }
071: return 0;
072: }
073:
074: static String propertiesToString(int properties) {
075: StringBuilder sb = new StringBuilder();
076: boolean first = true;
077: while (properties != 0) {
078: if (!first)
079: sb.append(',');
080: first = false;
081: int bitpos = Integer.numberOfTrailingZeros(properties);
082: sb.append(getPropertyName(1 << bitpos));
083: properties &= ~(1 << bitpos); // clear that bit position
084: }
085: return sb.toString();
086: }
087:
088: static boolean on(int bitfield, int props) {
089: return (bitfield & props) != 0;
090: }
091:
092: static boolean off(int bitfield, int props) {
093: return (bitfield & props) == 0;
094: }
095:
096: /***
097: static int normalize(int properties) {
098: int p = properties;
099: if (on(p,TOKENIZED) && off(p,INDEXED)) {
100: throw new RuntimeException("field must be indexed to be tokenized.");
101: }
102:
103: if (on(p,STORE_TERMPOSITIONS)) p|=STORE_TERMVECTORS;
104: if (on(p,STORE_TERMOFFSETS)) p|=STORE_TERMVECTORS;
105: if (on(p,STORE_TERMOFFSETS) && off(p,INDEXED)) {
106: throw new RuntimeException("field must be indexed to store term vectors.");
107: }
108:
109: if (on(p,OMIT_NORMS) && off(p,INDEXED)) {
110: throw new RuntimeException("field must be indexed for norms to be omitted.");
111: }
112:
113: if (on(p,SORT_MISSING_FIRST) && on(p,SORT_MISSING_LAST)) {
114: throw new RuntimeException("conflicting options sortMissingFirst,sortMissingLast.");
115: }
116:
117: if ((on(p,SORT_MISSING_FIRST) || on(p,SORT_MISSING_LAST)) && off(p,INDEXED)) {
118: throw new RuntimeException("field must be indexed to be sorted.");
119: }
120:
121: if ((on(p,BINARY) || on(p,COMPRESSED)) && off(p,STORED)) {
122: throw new RuntimeException("field must be stored for compressed or binary options.");
123: }
124:
125: return p;
126: }
127: ***/
128:
129: static int parseProperties(Map<String, String> properties,
130: boolean which) {
131: int props = 0;
132: for (String prop : properties.keySet()) {
133: if (propertyMap.get(prop) == null)
134: continue;
135: String val = properties.get(prop);
136: if (Boolean.parseBoolean(val) == which) {
137: props |= propertyNameToInt(prop);
138: }
139: }
140: return props;
141: }
142:
143: }
|