org.apache.jackrabbit.oak.plugins.document.Document Maven / Gradle / Ivy
/*
* 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.jackrabbit.oak.plugins.document;
import java.util.Collections;
import java.util.Map;
import java.util.Map.Entry;
import java.util.NavigableMap;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.apache.jackrabbit.oak.cache.CacheValue;
import org.apache.jackrabbit.oak.plugins.document.util.Utils;
import com.google.common.collect.Maps;
/**
* A document corresponds to a node stored in the DocumentNodeStore. A document
* contains all the revisions of a node stored in the {@link DocumentStore}.
*/
public class Document implements CacheValue {
/**
* The name of the field that contains the document id (the primary key /
* the key). The id uniquely identifies a document within a collection. The
* requirements and limits of the id are documented in the
* {@link DocumentStore} class.
*
* For nodes, the document id contains the depth of the path (0 for root, 1
* for children of the root), and then the path.
*/
public static final String ID = "_id";
/**
* The modification count on the document. This field is optional and a
* {@link DocumentStore} implementation may use it to keep track of how many
* times a document is modified. See also {@link #getModCount()}.
*/
public static final String MOD_COUNT = "_modCount";
/**
* The data of this document.
*/
protected Map data = Maps.newHashMap();
/**
* Whether this document is sealed (immutable data).
*/
private AtomicBoolean sealed = new AtomicBoolean(false);
/**
* Get the id (the primary key) of this document.
*
* @return the id or null
if none is set.
*/
@CheckForNull
public String getId() {
return (String) get(ID);
}
/**
* Get the modification count of this document.
*
* @return the count or null
if
* none is set.
*/
@CheckForNull
public Long getModCount() {
return Utils.asLong((Number) get(MOD_COUNT));
}
/**
* Gets the data for the given key
.
*
* @param key the key.
* @return the data or null
.
*/
@CheckForNull
public Object get(String key) {
return data.get(key);
}
/**
* Sets the data for the given key
.
*
* @param key the key.
* @param value the value to set.
* @return the previous value or null
if there was none.
*/
@CheckForNull
public Object put(String key, Object value) {
return data.put(key, value);
}
/**
* Removes the given key
.
*
* @param key the key.
* @return the previous value or null
if there was none.
*/
@CheckForNull
public Object remove(String key) {
return data.remove(key);
}
/**
* @return a Set view of the keys contained in this document.
*/
public Set keySet() {
return data.keySet();
}
/**
* @return a Set view of the entries contained in this document.
*/
public Set> entrySet() {
return data.entrySet();
}
/**
* Seals this document and turns it into an immutable object. Any attempt
* to modify this document afterwards will result in an
* {@link UnsupportedOperationException}.
*/
public void seal() {
if (!sealed.getAndSet(true)) {
for (Map.Entry entry : data.entrySet()) {
if (entry.getValue() instanceof Map) {
@SuppressWarnings("unchecked")
Map