/
AbstractSSTableSimpleWriter.java
166 lines (146 loc) · 6.23 KB
/
AbstractSSTableSimpleWriter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
* 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.cassandra.io.sstable;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.HashSet;
import java.util.Set;
import org.apache.cassandra.config.CFMetaData;
import org.apache.cassandra.db.*;
import org.apache.cassandra.db.context.CounterContext;
import org.apache.cassandra.service.StorageService;
import org.apache.cassandra.utils.NodeId;
import org.apache.cassandra.utils.Pair;
public abstract class AbstractSSTableSimpleWriter
{
protected final File directory;
protected final CFMetaData metadata;
protected DecoratedKey currentKey;
protected ColumnFamily columnFamily;
protected SuperColumn currentSuperColumn;
protected final NodeId nodeid = NodeId.generate();
public AbstractSSTableSimpleWriter(File directory, CFMetaData metadata)
{
this.metadata = metadata;
this.directory = directory;
}
protected SSTableWriter getWriter() throws IOException
{
return new SSTableWriter(
makeFilename(directory, metadata.ksName, metadata.cfName),
0, // We don't care about the bloom filter
metadata,
StorageService.getPartitioner(),
SSTableMetadata.createCollector());
}
// find available generation and pick up filename from that
private static String makeFilename(File directory, final String keyspace, final String columnFamily)
{
final Set<Descriptor> existing = new HashSet<Descriptor>();
directory.list(new FilenameFilter()
{
public boolean accept(File dir, String name)
{
Pair<Descriptor, Component> p = SSTable.tryComponentFromFilename(dir, name);
Descriptor desc = p == null ? null : p.left;
if (desc == null)
return false;
if (desc.cfname.equals(columnFamily))
existing.add(desc);
return false;
}
});
int maxGen = 0;
for (Descriptor desc : existing)
maxGen = Math.max(maxGen, desc.generation);
return new Descriptor(directory, keyspace, columnFamily, maxGen + 1, true).filenameFor(Component.DATA);
}
/**
* Start a new row whose key is {@code key}.
* @param key the row key
*/
public void newRow(ByteBuffer key) throws IOException
{
if (currentKey != null && !columnFamily.isEmpty())
writeRow(currentKey, columnFamily);
currentKey = StorageService.getPartitioner().decorateKey(key);
columnFamily = getColumnFamily();
}
/**
* Start a new super column with name {@code name}.
* @param name the name for the super column
*/
public void newSuperColumn(ByteBuffer name)
{
if (!columnFamily.isSuper())
throw new IllegalStateException("Cannot add a super column to a standard column family");
currentSuperColumn = new SuperColumn(name, metadata.subcolumnComparator);
columnFamily.addColumn(currentSuperColumn);
}
private void addColumn(IColumn column)
{
if (columnFamily.isSuper() && currentSuperColumn == null)
throw new IllegalStateException("Trying to add a column to a super column family, but no super column has been started.");
IColumnContainer container = columnFamily.isSuper() ? currentSuperColumn : columnFamily;
container.addColumn(column);
}
/**
* Insert a new "regular" column to the current row (and super column if applicable).
* @param name the column name
* @param value the column value
* @param timestamp the column timestamp
*/
public void addColumn(ByteBuffer name, ByteBuffer value, long timestamp)
{
addColumn(new Column(name, value, timestamp));
}
/**
* Insert a new expiring column to the current row (and super column if applicable).
* @param name the column name
* @param value the column value
* @param timestamp the column timestamp
* @param ttl the column time to live in seconds
* @param expirationTimestampMS the local expiration timestamp in milliseconds. This is the server time timestamp used for actually
* expiring the column, and as a consequence should be synchronized with the cassandra servers time. If {@code timestamp} represents
* the insertion time in microseconds (which is not required), this should be {@code (timestamp / 1000) + (ttl * 1000)}.
*/
public void addExpiringColumn(ByteBuffer name, ByteBuffer value, long timestamp, int ttl, long expirationTimestampMS)
{
addColumn(new ExpiringColumn(name, value, timestamp, ttl, (int)(expirationTimestampMS / 1000)));
}
/**
* Insert a new counter column to the current row (and super column if applicable).
* @param name the column name
* @param value the value of the counter
*/
public void addCounterColumn(ByteBuffer name, long value)
{
addColumn(new CounterColumn(name, CounterContext.instance().create(nodeid, 1L, value, false), System.currentTimeMillis()));
}
/**
* Close this writer.
* This method should be called, otherwise the produced sstables are not
* guaranteed to be complete (and won't be in practice).
*/
public abstract void close() throws IOException;
protected abstract void writeRow(DecoratedKey key, ColumnFamily columnFamily) throws IOException;
protected abstract ColumnFamily getColumnFamily();
}