1 /*
2 * Copyright 2010 Ning, Inc.
3 *
4 * Ning licenses this file to you under the Apache License, version 2.0
5 * (the "License"); you may not use this file except in compliance with the
6 * License. You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations
14 * under the License.
15 */
16
17 package com.ning.metrics.goodwill.store;
18
19 import com.ning.metrics.goodwill.access.GoodwillSchema;
20 import com.ning.metrics.goodwill.sink.GoodwillSink;
21 import org.apache.log4j.Logger;
22 import org.codehaus.jackson.map.ObjectMapper;
23
24 import java.io.ByteArrayOutputStream;
25 import java.io.IOException;
26 import java.util.Collection;
27 import java.util.Map;
28
29 public abstract class GoodwillStore
30 {
31 private static Logger log = Logger.getLogger(GoodwillStore.class);
32
33 final ObjectMapper mapper = new ObjectMapper();
34
35 protected GoodwillSink sink;
36
37 protected Map<String, GoodwillSchema> goodwillSchemata;
38
39 /**
40 * Given a Thrift name, find it in the store
41 *
42 * @param typeName name of the Thrift to search
43 * @return the ThriftType if found, null otherwise
44 */
45 public GoodwillSchema findByName(String typeName)
46 {
47 try {
48 for (GoodwillSchema schema : getTypes()) {
49 if (schema.getName().equals(typeName)) {
50 return schema;
51 }
52 }
53 }
54 catch (IOException e) {
55 log.warn("Unable to fetch Thrift types", e);
56 }
57
58 return null;
59 }
60
61 /**
62 * Serialize all Thrifts in the store in JSON format
63 *
64 * @return JSONArray representation
65 * @throws IOException for serialization issues
66 */
67 public ByteArrayOutputStream toJSON() throws IOException
68 {
69 ByteArrayOutputStream out = new ByteArrayOutputStream();
70 mapper.writeValue(out, this);
71 return out;
72 }
73
74 public void setSink(GoodwillSink sink)
75 {
76 this.sink = sink;
77 }
78
79 public abstract Collection<GoodwillSchema> getTypes() throws IOException;
80
81 /**
82 * Add a new type to the store
83 *
84 * @param schema GoodwillSchema to add
85 */
86 public abstract void addType(GoodwillSchema schema);
87
88 /**
89 * Update a type to the store
90 *
91 * @param schema GoodwillSchema to update
92 * @return true is success, false otherwise
93 */
94 public abstract boolean updateType(GoodwillSchema schema);
95
96 /**
97 * Delete a type
98 *
99 * @param schema GoodwillSchema to delete
100 * @return true is success, false otherwise
101 */
102 public abstract boolean deleteType(GoodwillSchema schema);
103 }