Cassandra中的定义了一个IColumn的接口,所有的Column都实现了IColumn. 继承关系如下图:
这里我们关注比较简单也比较重要的Column,DeletedColumn和ExpiringColumn. SuperColumn将来可能不被推荐使用。
Column包含了如下信息。
protected final ByteBuffer name;
protected final ByteBuffer value;
protected final long timestamp;
从这些成员变量的定义上我们就可以看出,这个类的是不可变的。
接下来我们关注一下其他的重要的方法。
public boolean isMarkedForDelete()
{
return (int) (System.currentTimeMillis() / 1000) >= getLocalDeletionTime();
}
这个方法是用来判断一个column是否被Delete掉。现在我们还无法看出这个方法会return true/false. 因为他取决于getLocalDeletionTime()的返回。
我们再看看这个方法是如何实现的:
public int getLocalDeletionTime()
{
return Integer.MAX_VALUE;
}
噢~, 这个值很大啊。 (int) (System.currentTimeMillis() / 1000) >= Integer.MAX_VALUE 在我们的有生之年都会返回false. 所以Column是用来表示normal的Column的,Deleted Column需要用DeletedColumn.
DeletedColumn继承自Column。我们来看看修改了哪些。
public DeletedColumn(ByteBuffer name, int localDeletionTime, long timestamp)
{
this(name, ByteBufferUtil.bytes(localDeletionTime), timestamp);
}
public DeletedColumn(ByteBuffer name, ByteBuffer value, long timestamp)
{
super(name, value, timestamp);
}
@Override
public long getMarkedForDeleteAt()
{
return timestamp;
}
@Override
public int getLocalDeletionTime()
{
return value.getInt(value.position());
}
从上面不难看出,DeletedColumn的value变了,存储的是一个LocalDeletionTime, getLocalDeletionTime()返回的就是该值。
至于为什么要存储一个LocalDeletionTime我们可以思考一下,这里按下不表。
我们再来看看ExpiringColumn,
private final int localExpirationTime;
private final int timeToLive;
public ExpiringColumn(ByteBuffer name, ByteBuffer value, long timestamp, int timeToLive)
{
this(name, value, timestamp, timeToLive, (int) (System.currentTimeMillis() / 1000) + timeToLive);
}
public ExpiringColumn(ByteBuffer name, ByteBuffer value, long timestamp, int timeToLive, int localExpirationTime)
{
super(name, value, timestamp);
assert timeToLive > 0 : timeToLive;
assert localExpirationTime > 0 : localExpirationTime;
this.timeToLive = timeToLive;
this.localExpirationTime = localExpirationTime;
}
从上面看出ExpiringColumn还有两个额外信息。timeToLive和LocalExpirationTime. localExpirationTime = (int) (System.currentTimeMillis() / 1000) + timeToLive.
public int getLocalDeletionTime()
{
return localExpirationTime;
}
至此我们就分析完了这三个类。还是相当简单的。
Column包含了如下信息。
protected final ByteBuffer name;
protected final ByteBuffer value;
protected final long timestamp;
从这些成员变量的定义上我们就可以看出,这个类的是不可变的。
接下来我们关注一下其他的重要的方法。
public boolean isMarkedForDelete()
{
return (int) (System.currentTimeMillis() / 1000) >= getLocalDeletionTime();
}
这个方法是用来判断一个column是否被Delete掉。现在我们还无法看出这个方法会return true/false. 因为他取决于getLocalDeletionTime()的返回。
我们再看看这个方法是如何实现的:
public int getLocalDeletionTime()
{
return Integer.MAX_VALUE;
}
噢~, 这个值很大啊。 (int) (System.currentTimeMillis() / 1000) >= Integer.MAX_VALUE 在我们的有生之年都会返回false. 所以Column是用来表示normal的Column的,Deleted Column需要用DeletedColumn.
DeletedColumn继承自Column。我们来看看修改了哪些。
public DeletedColumn(ByteBuffer name, int localDeletionTime, long timestamp)
{
this(name, ByteBufferUtil.bytes(localDeletionTime), timestamp);
}
public DeletedColumn(ByteBuffer name, ByteBuffer value, long timestamp)
{
super(name, value, timestamp);
}
@Override
public long getMarkedForDeleteAt()
{
return timestamp;
}
@Override
public int getLocalDeletionTime()
{
return value.getInt(value.position());
}
从上面不难看出,DeletedColumn的value变了,存储的是一个LocalDeletionTime, getLocalDeletionTime()返回的就是该值。
至于为什么要存储一个LocalDeletionTime我们可以思考一下,这里按下不表。
我们再来看看ExpiringColumn,
private final int localExpirationTime;
private final int timeToLive;
public ExpiringColumn(ByteBuffer name, ByteBuffer value, long timestamp, int timeToLive)
{
this(name, value, timestamp, timeToLive, (int) (System.currentTimeMillis() / 1000) + timeToLive);
}
public ExpiringColumn(ByteBuffer name, ByteBuffer value, long timestamp, int timeToLive, int localExpirationTime)
{
super(name, value, timestamp);
assert timeToLive > 0 : timeToLive;
assert localExpirationTime > 0 : localExpirationTime;
this.timeToLive = timeToLive;
this.localExpirationTime = localExpirationTime;
}
从上面看出ExpiringColumn还有两个额外信息。timeToLive和LocalExpirationTime. localExpirationTime = (int) (System.currentTimeMillis() / 1000) + timeToLive.
public int getLocalDeletionTime()
{
return localExpirationTime;
}
至此我们就分析完了这三个类。还是相当简单的。