]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ValueQuery.java
DB query swapping to file system
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / ValueQuery.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2018 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.impl.query;
13
14 import org.simantics.db.exception.DatabaseException;
15 import org.simantics.db.impl.graph.ReadGraphImpl;
16 import org.simantics.db.impl.procedure.InternalProcedure;
17
18 public final class ValueQuery extends UnaryQueryP<byte[]> {
19
20     ValueQuery(int resource) {
21         super(resource);
22     }
23
24     @Override
25     public final void removeEntry(QueryProcessor provider) {
26         provider.cache.remove(this);
27     }
28
29     @Override
30     public void compute(ReadGraphImpl graph, InternalProcedure<byte[]> procedure) throws DatabaseException {
31         computeForEach(graph, id, this, procedure);
32     }
33
34     public static byte[] computeForEach(ReadGraphImpl graph, final int r, final ValueQuery entry, final InternalProcedure<byte[]> procedure_) throws DatabaseException {
35
36         InternalProcedure<byte[]> procedure = entry != null ? entry : procedure_;
37
38         graph.ensureLoaded(r);
39
40         byte[] value = graph.getValue(r);
41         if(procedure != null) procedure.execute(graph, value);
42
43         if(entry != null && procedure_ != null) entry.performFromCache(graph, procedure_);
44
45         return value;
46
47     }
48     
49     @Override
50     public void serializeValue(QuerySerializer serializer) {
51         byte[] result = getResult();
52         if(result == null) {
53             serializer.writeLE(-1);
54         } else {
55             serializer.writeLE(result.length);
56             serializer.add(result);
57         }
58     }
59
60     @Override
61     public String toString() {
62         return "Value[" + id + "]";
63     }
64
65 }