File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\mvstore\MVStoreBuilder.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.mvstore;
8
9 import java.util.HashMap;
10 import org.h2.mvstore.type.DataTypeFactory;
11 import org.h2.util.New;
12
13 /**
14 * A builder for an MVStore.
15 */
16 public class MVStoreBuilder {
17
18 private final HashMap<String, Object> config = New.hashMap();
19
20 /**
21 * Use the following file name. If the file does not exist, it is
22 * automatically created.
23 *
24 * @param fileName the file name
25 * @return this
26 */
27 public static MVStoreBuilder fileBased(String fileName) {
28 return new MVStoreBuilder().fileName(fileName);
29 }
30
31 /**
32 * Open the store in-memory. In this case, no data may be saved.
33 *
34 * @return the store
35 */
36 public static MVStoreBuilder inMemory() {
37 return new MVStoreBuilder();
38 }
39
40 private MVStoreBuilder set(String key, Object value) {
41 if (config.containsKey(key)) {
42 throw new IllegalArgumentException("Parameter " + config.get(key) + " is already set");
43 }
44 config.put(key, value);
45 return this;
46 }
47
48 private MVStoreBuilder fileName(String fileName) {
49 return set("fileName", fileName);
50 }
51
52 /**
53 * Open the file in read-only mode. In this case, a shared lock will be
54 * acquired to ensure the file is not concurrently opened in write mode.
55 * <p>
56 * If this option is not used, the file is locked exclusively.
57 * <p>
58 * Please note a store may only be opened once in every JVM (no matter
59 * whether it is opened in read-only or read-write mode), because each file
60 * may be locked only once in a process.
61 *
62 * @return this
63 */
64 public MVStoreBuilder readOnly() {
65 return set("openMode", "r");
66 }
67
68 /**
69 * Set the read cache size in MB. The default is 16 MB.
70 *
71 * @param mb the cache size
72 * @return this
73 */
74 public MVStoreBuilder cacheSizeMB(int mb) {
75 return set("cacheSize", Integer.toString(mb));
76 }
77
78 /**
79 * Use the given data type factory.
80 *
81 * @param factory the data type factory
82 * @return this
83 */
84 public MVStoreBuilder with(DataTypeFactory factory) {
85 return set("dataTypeFactory", factory);
86 }
87
88 /**
89 * Open the store.
90 *
91 * @return the opened store
92 */
93 public MVStore open() {
94 MVStore s = new MVStore(config);
95 s.open();
96 return s;
97 }
98
99 public String toString() {
100 return DataUtils.appendMap(new StringBuilder(), config).toString();
101 }
102
103 /**
104 * Read the configuration from a string.
105 *
106 * @param s the string representation
107 * @return the builder
108 */
109 public static MVStoreBuilder fromString(String s) {
110 HashMap<String, String> config = DataUtils.parseMap(s);
111 MVStoreBuilder builder = new MVStoreBuilder();
112 builder.config.putAll(config);
113 return builder;
114 }
115
116 }
117